Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * linux/drivers/video/vfb.c -- Virtual frame buffer device
3 *
4 * Copyright (C) 2002 James Simmons
5 *
6 * Copyright (C) 1997 Geert Uytterhoeven
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/mm.h>
18#include <linux/vmalloc.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22
23#include <linux/fb.h>
24#include <linux/init.h>
25
26 /*
27 * RAM we reserve for the frame buffer. This defines the maximum screen
28 * size
29 *
30 * The default can be overridden if the driver is compiled as a module
31 */
32
33#define VIDEOMEMSIZE (1*1024*1024) /* 1 MB */
34
35static void *videomemory;
36static u_long videomemorysize = VIDEOMEMSIZE;
37module_param(videomemorysize, ulong, 0);
38MODULE_PARM_DESC(videomemorysize, "RAM available to frame buffer (in bytes)");
39
40static char *mode_option = NULL;
41module_param(mode_option, charp, 0);
42MODULE_PARM_DESC(mode_option, "Preferred video mode (e.g. 640x480-8@60)");
43
44static const struct fb_videomode vfb_default = {
45 .xres = 640,
46 .yres = 480,
47 .pixclock = 20000,
48 .left_margin = 64,
49 .right_margin = 64,
50 .upper_margin = 32,
51 .lower_margin = 32,
52 .hsync_len = 64,
53 .vsync_len = 2,
54 .vmode = FB_VMODE_NONINTERLACED,
55};
56
57static struct fb_fix_screeninfo vfb_fix = {
58 .id = "Virtual FB",
59 .type = FB_TYPE_PACKED_PIXELS,
60 .visual = FB_VISUAL_PSEUDOCOLOR,
61 .xpanstep = 1,
62 .ypanstep = 1,
63 .ywrapstep = 1,
64 .accel = FB_ACCEL_NONE,
65};
66
67static bool vfb_enable __initdata = 0; /* disabled by default */
68module_param(vfb_enable, bool, 0);
69MODULE_PARM_DESC(vfb_enable, "Enable Virtual FB driver");
70
71static int vfb_check_var(struct fb_var_screeninfo *var,
72 struct fb_info *info);
73static int vfb_set_par(struct fb_info *info);
74static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
75 u_int transp, struct fb_info *info);
76static int vfb_pan_display(struct fb_var_screeninfo *var,
77 struct fb_info *info);
78static int vfb_mmap(struct fb_info *info,
79 struct vm_area_struct *vma);
80
81static struct fb_ops vfb_ops = {
82 .fb_read = fb_sys_read,
83 .fb_write = fb_sys_write,
84 .fb_check_var = vfb_check_var,
85 .fb_set_par = vfb_set_par,
86 .fb_setcolreg = vfb_setcolreg,
87 .fb_pan_display = vfb_pan_display,
88 .fb_fillrect = sys_fillrect,
89 .fb_copyarea = sys_copyarea,
90 .fb_imageblit = sys_imageblit,
91 .fb_mmap = vfb_mmap,
92};
93
94 /*
95 * Internal routines
96 */
97
98static u_long get_line_length(int xres_virtual, int bpp)
99{
100 u_long length;
101
102 length = xres_virtual * bpp;
103 length = (length + 31) & ~31;
104 length >>= 3;
105 return (length);
106}
107
108 /*
109 * Setting the video mode has been split into two parts.
110 * First part, xxxfb_check_var, must not write anything
111 * to hardware, it should only verify and adjust var.
112 * This means it doesn't alter par but it does use hardware
113 * data from it to check this var.
114 */
115
116static int vfb_check_var(struct fb_var_screeninfo *var,
117 struct fb_info *info)
118{
119 u_long line_length;
120
121 /*
122 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
123 * as FB_VMODE_SMOOTH_XPAN is only used internally
124 */
125
126 if (var->vmode & FB_VMODE_CONUPDATE) {
127 var->vmode |= FB_VMODE_YWRAP;
128 var->xoffset = info->var.xoffset;
129 var->yoffset = info->var.yoffset;
130 }
131
132 /*
133 * Some very basic checks
134 */
135 if (!var->xres)
136 var->xres = 1;
137 if (!var->yres)
138 var->yres = 1;
139 if (var->xres > var->xres_virtual)
140 var->xres_virtual = var->xres;
141 if (var->yres > var->yres_virtual)
142 var->yres_virtual = var->yres;
143 if (var->bits_per_pixel <= 1)
144 var->bits_per_pixel = 1;
145 else if (var->bits_per_pixel <= 8)
146 var->bits_per_pixel = 8;
147 else if (var->bits_per_pixel <= 16)
148 var->bits_per_pixel = 16;
149 else if (var->bits_per_pixel <= 24)
150 var->bits_per_pixel = 24;
151 else if (var->bits_per_pixel <= 32)
152 var->bits_per_pixel = 32;
153 else
154 return -EINVAL;
155
156 if (var->xres_virtual < var->xoffset + var->xres)
157 var->xres_virtual = var->xoffset + var->xres;
158 if (var->yres_virtual < var->yoffset + var->yres)
159 var->yres_virtual = var->yoffset + var->yres;
160
161 /*
162 * Memory limit
163 */
164 line_length =
165 get_line_length(var->xres_virtual, var->bits_per_pixel);
166 if (line_length * var->yres_virtual > videomemorysize)
167 return -ENOMEM;
168
169 /*
170 * Now that we checked it we alter var. The reason being is that the video
171 * mode passed in might not work but slight changes to it might make it
172 * work. This way we let the user know what is acceptable.
173 */
174 switch (var->bits_per_pixel) {
175 case 1:
176 case 8:
177 var->red.offset = 0;
178 var->red.length = 8;
179 var->green.offset = 0;
180 var->green.length = 8;
181 var->blue.offset = 0;
182 var->blue.length = 8;
183 var->transp.offset = 0;
184 var->transp.length = 0;
185 break;
186 case 16: /* RGBA 5551 */
187 if (var->transp.length) {
188 var->red.offset = 0;
189 var->red.length = 5;
190 var->green.offset = 5;
191 var->green.length = 5;
192 var->blue.offset = 10;
193 var->blue.length = 5;
194 var->transp.offset = 15;
195 var->transp.length = 1;
196 } else { /* RGB 565 */
197 var->red.offset = 0;
198 var->red.length = 5;
199 var->green.offset = 5;
200 var->green.length = 6;
201 var->blue.offset = 11;
202 var->blue.length = 5;
203 var->transp.offset = 0;
204 var->transp.length = 0;
205 }
206 break;
207 case 24: /* RGB 888 */
208 var->red.offset = 0;
209 var->red.length = 8;
210 var->green.offset = 8;
211 var->green.length = 8;
212 var->blue.offset = 16;
213 var->blue.length = 8;
214 var->transp.offset = 0;
215 var->transp.length = 0;
216 break;
217 case 32: /* RGBA 8888 */
218 var->red.offset = 0;
219 var->red.length = 8;
220 var->green.offset = 8;
221 var->green.length = 8;
222 var->blue.offset = 16;
223 var->blue.length = 8;
224 var->transp.offset = 24;
225 var->transp.length = 8;
226 break;
227 }
228 var->red.msb_right = 0;
229 var->green.msb_right = 0;
230 var->blue.msb_right = 0;
231 var->transp.msb_right = 0;
232
233 return 0;
234}
235
236/* This routine actually sets the video mode. It's in here where we
237 * the hardware state info->par and fix which can be affected by the
238 * change in par. For this driver it doesn't do much.
239 */
240static int vfb_set_par(struct fb_info *info)
241{
242 info->fix.line_length = get_line_length(info->var.xres_virtual,
243 info->var.bits_per_pixel);
244 return 0;
245}
246
247 /*
248 * Set a single color register. The values supplied are already
249 * rounded down to the hardware's capabilities (according to the
250 * entries in the var structure). Return != 0 for invalid regno.
251 */
252
253static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
254 u_int transp, struct fb_info *info)
255{
256 if (regno >= 256) /* no. of hw registers */
257 return 1;
258 /*
259 * Program hardware... do anything you want with transp
260 */
261
262 /* grayscale works only partially under directcolor */
263 if (info->var.grayscale) {
264 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
265 red = green = blue =
266 (red * 77 + green * 151 + blue * 28) >> 8;
267 }
268
269 /* Directcolor:
270 * var->{color}.offset contains start of bitfield
271 * var->{color}.length contains length of bitfield
272 * {hardwarespecific} contains width of RAMDAC
273 * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
274 * RAMDAC[X] is programmed to (red, green, blue)
275 *
276 * Pseudocolor:
277 * var->{color}.offset is 0 unless the palette index takes less than
278 * bits_per_pixel bits and is stored in the upper
279 * bits of the pixel value
280 * var->{color}.length is set so that 1 << length is the number of available
281 * palette entries
282 * cmap is not used
283 * RAMDAC[X] is programmed to (red, green, blue)
284 *
285 * Truecolor:
286 * does not use DAC. Usually 3 are present.
287 * var->{color}.offset contains start of bitfield
288 * var->{color}.length contains length of bitfield
289 * cmap is programmed to (red << red.offset) | (green << green.offset) |
290 * (blue << blue.offset) | (transp << transp.offset)
291 * RAMDAC does not exist
292 */
293#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
294 switch (info->fix.visual) {
295 case FB_VISUAL_TRUECOLOR:
296 case FB_VISUAL_PSEUDOCOLOR:
297 red = CNVT_TOHW(red, info->var.red.length);
298 green = CNVT_TOHW(green, info->var.green.length);
299 blue = CNVT_TOHW(blue, info->var.blue.length);
300 transp = CNVT_TOHW(transp, info->var.transp.length);
301 break;
302 case FB_VISUAL_DIRECTCOLOR:
303 red = CNVT_TOHW(red, 8); /* expect 8 bit DAC */
304 green = CNVT_TOHW(green, 8);
305 blue = CNVT_TOHW(blue, 8);
306 /* hey, there is bug in transp handling... */
307 transp = CNVT_TOHW(transp, 8);
308 break;
309 }
310#undef CNVT_TOHW
311 /* Truecolor has hardware independent palette */
312 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
313 u32 v;
314
315 if (regno >= 16)
316 return 1;
317
318 v = (red << info->var.red.offset) |
319 (green << info->var.green.offset) |
320 (blue << info->var.blue.offset) |
321 (transp << info->var.transp.offset);
322 switch (info->var.bits_per_pixel) {
323 case 8:
324 break;
325 case 16:
326 ((u32 *) (info->pseudo_palette))[regno] = v;
327 break;
328 case 24:
329 case 32:
330 ((u32 *) (info->pseudo_palette))[regno] = v;
331 break;
332 }
333 return 0;
334 }
335 return 0;
336}
337
338 /*
339 * Pan or Wrap the Display
340 *
341 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
342 */
343
344static int vfb_pan_display(struct fb_var_screeninfo *var,
345 struct fb_info *info)
346{
347 if (var->vmode & FB_VMODE_YWRAP) {
348 if (var->yoffset >= info->var.yres_virtual ||
349 var->xoffset)
350 return -EINVAL;
351 } else {
352 if (var->xoffset + info->var.xres > info->var.xres_virtual ||
353 var->yoffset + info->var.yres > info->var.yres_virtual)
354 return -EINVAL;
355 }
356 info->var.xoffset = var->xoffset;
357 info->var.yoffset = var->yoffset;
358 if (var->vmode & FB_VMODE_YWRAP)
359 info->var.vmode |= FB_VMODE_YWRAP;
360 else
361 info->var.vmode &= ~FB_VMODE_YWRAP;
362 return 0;
363}
364
365 /*
366 * Most drivers don't need their own mmap function
367 */
368
369static int vfb_mmap(struct fb_info *info,
370 struct vm_area_struct *vma)
371{
372 return remap_vmalloc_range(vma, (void *)info->fix.smem_start, vma->vm_pgoff);
373}
374
375#ifndef MODULE
376/*
377 * The virtual framebuffer driver is only enabled if explicitly
378 * requested by passing 'video=vfb:' (or any actual options).
379 */
380static int __init vfb_setup(char *options)
381{
382 char *this_opt;
383
384 vfb_enable = 0;
385
386 if (!options)
387 return 1;
388
389 vfb_enable = 1;
390
391 if (!*options)
392 return 1;
393
394 while ((this_opt = strsep(&options, ",")) != NULL) {
395 if (!*this_opt)
396 continue;
397 /* Test disable for backwards compatibility */
398 if (!strcmp(this_opt, "disable"))
399 vfb_enable = 0;
400 else
401 mode_option = this_opt;
402 }
403 return 1;
404}
405#endif /* MODULE */
406
407 /*
408 * Initialisation
409 */
410
411static int vfb_probe(struct platform_device *dev)
412{
413 struct fb_info *info;
414 unsigned int size = PAGE_ALIGN(videomemorysize);
415 int retval = -ENOMEM;
416
417 /*
418 * For real video cards we use ioremap.
419 */
420 if (!(videomemory = vmalloc_32_user(size)))
421 return retval;
422
423 info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
424 if (!info)
425 goto err;
426
427 info->screen_base = (char __iomem *)videomemory;
428 info->fbops = &vfb_ops;
429
430 if (!fb_find_mode(&info->var, info, mode_option,
431 NULL, 0, &vfb_default, 8)){
432 fb_err(info, "Unable to find usable video mode.\n");
433 retval = -EINVAL;
434 goto err1;
435 }
436
437 vfb_fix.smem_start = (unsigned long) videomemory;
438 vfb_fix.smem_len = videomemorysize;
439 info->fix = vfb_fix;
440 info->pseudo_palette = info->par;
441 info->par = NULL;
442 info->flags = FBINFO_FLAG_DEFAULT;
443
444 retval = fb_alloc_cmap(&info->cmap, 256, 0);
445 if (retval < 0)
446 goto err1;
447
448 retval = register_framebuffer(info);
449 if (retval < 0)
450 goto err2;
451 platform_set_drvdata(dev, info);
452
453 fb_info(info, "Virtual frame buffer device, using %ldK of video memory\n",
454 videomemorysize >> 10);
455 return 0;
456err2:
457 fb_dealloc_cmap(&info->cmap);
458err1:
459 framebuffer_release(info);
460err:
461 vfree(videomemory);
462 return retval;
463}
464
465static int vfb_remove(struct platform_device *dev)
466{
467 struct fb_info *info = platform_get_drvdata(dev);
468
469 if (info) {
470 unregister_framebuffer(info);
471 vfree(videomemory);
472 fb_dealloc_cmap(&info->cmap);
473 framebuffer_release(info);
474 }
475 return 0;
476}
477
478static struct platform_driver vfb_driver = {
479 .probe = vfb_probe,
480 .remove = vfb_remove,
481 .driver = {
482 .name = "vfb",
483 },
484};
485
486static struct platform_device *vfb_device;
487
488static int __init vfb_init(void)
489{
490 int ret = 0;
491
492#ifndef MODULE
493 char *option = NULL;
494
495 if (fb_get_options("vfb", &option))
496 return -ENODEV;
497 vfb_setup(option);
498#endif
499
500 if (!vfb_enable)
501 return -ENXIO;
502
503 ret = platform_driver_register(&vfb_driver);
504
505 if (!ret) {
506 vfb_device = platform_device_alloc("vfb", 0);
507
508 if (vfb_device)
509 ret = platform_device_add(vfb_device);
510 else
511 ret = -ENOMEM;
512
513 if (ret) {
514 platform_device_put(vfb_device);
515 platform_driver_unregister(&vfb_driver);
516 }
517 }
518
519 return ret;
520}
521
522module_init(vfb_init);
523
524#ifdef MODULE
525static void __exit vfb_exit(void)
526{
527 platform_device_unregister(vfb_device);
528 platform_driver_unregister(&vfb_driver);
529}
530
531module_exit(vfb_exit);
532
533MODULE_LICENSE("GPL");
534#endif /* MODULE */