2 * linux/drivers/video/vfb.c -- Virtual frame buffer device
4 * Copyright (C) 2002 James Simmons
6 * Copyright (C) 1997 Geert Uytterhoeven
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
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
18 #include <linux/vmalloc.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
24 #include <linux/init.h>
27 * RAM we reserve for the frame buffer. This defines the maximum screen
30 * The default can be overridden if the driver is compiled as a module
33 #define VIDEOMEMSIZE (1*1024*1024) /* 1 MB */
35 static void *videomemory;
36 static u_long videomemorysize = VIDEOMEMSIZE;
37 module_param(videomemorysize, ulong, 0);
38 MODULE_PARM_DESC(videomemorysize, "RAM available to frame buffer (in bytes)");
40 static char *mode_option = NULL;
41 module_param(mode_option, charp, 0);
42 MODULE_PARM_DESC(mode_option, "Preferred video mode (e.g. 640x480-8@60)");
44 static const struct fb_videomode vfb_default = {
54 .vmode = FB_VMODE_NONINTERLACED,
57 static struct fb_fix_screeninfo vfb_fix = {
59 .type = FB_TYPE_PACKED_PIXELS,
60 .visual = FB_VISUAL_PSEUDOCOLOR,
64 .accel = FB_ACCEL_NONE,
67 static bool vfb_enable __initdata = 0; /* disabled by default */
68 module_param(vfb_enable, bool, 0);
69 MODULE_PARM_DESC(vfb_enable, "Enable Virtual FB driver");
71 static int vfb_check_var(struct fb_var_screeninfo *var,
72 struct fb_info *info);
73 static int vfb_set_par(struct fb_info *info);
74 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
75 u_int transp, struct fb_info *info);
76 static int vfb_pan_display(struct fb_var_screeninfo *var,
77 struct fb_info *info);
78 static int vfb_mmap(struct fb_info *info,
79 struct vm_area_struct *vma);
81 static const struct fb_ops vfb_ops = {
83 __FB_DEFAULT_SYSMEM_OPS_RDWR,
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_DEFAULT_SYSMEM_OPS_DRAW,
96 static u_long get_line_length(int xres_virtual, int bpp)
100 length = xres_virtual * bpp;
101 length = (length + 31) & ~31;
107 * Setting the video mode has been split into two parts.
108 * First part, xxxfb_check_var, must not write anything
109 * to hardware, it should only verify and adjust var.
110 * This means it doesn't alter par but it does use hardware
111 * data from it to check this var.
114 static int vfb_check_var(struct fb_var_screeninfo *var,
115 struct fb_info *info)
120 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
121 * as FB_VMODE_SMOOTH_XPAN is only used internally
124 if (var->vmode & FB_VMODE_CONUPDATE) {
125 var->vmode |= FB_VMODE_YWRAP;
126 var->xoffset = info->var.xoffset;
127 var->yoffset = info->var.yoffset;
131 * Some very basic checks
137 if (var->xres > var->xres_virtual)
138 var->xres_virtual = var->xres;
139 if (var->yres > var->yres_virtual)
140 var->yres_virtual = var->yres;
141 if (var->bits_per_pixel <= 1)
142 var->bits_per_pixel = 1;
143 else if (var->bits_per_pixel <= 8)
144 var->bits_per_pixel = 8;
145 else if (var->bits_per_pixel <= 16)
146 var->bits_per_pixel = 16;
147 else if (var->bits_per_pixel <= 24)
148 var->bits_per_pixel = 24;
149 else if (var->bits_per_pixel <= 32)
150 var->bits_per_pixel = 32;
154 if (var->xres_virtual < var->xoffset + var->xres)
155 var->xres_virtual = var->xoffset + var->xres;
156 if (var->yres_virtual < var->yoffset + var->yres)
157 var->yres_virtual = var->yoffset + var->yres;
163 get_line_length(var->xres_virtual, var->bits_per_pixel);
164 if (line_length * var->yres_virtual > videomemorysize)
168 * Now that we checked it we alter var. The reason being is that the video
169 * mode passed in might not work but slight changes to it might make it
170 * work. This way we let the user know what is acceptable.
172 switch (var->bits_per_pixel) {
177 var->green.offset = 0;
178 var->green.length = 8;
179 var->blue.offset = 0;
180 var->blue.length = 8;
181 var->transp.offset = 0;
182 var->transp.length = 0;
184 case 16: /* RGBA 5551 */
185 if (var->transp.length) {
188 var->green.offset = 5;
189 var->green.length = 5;
190 var->blue.offset = 10;
191 var->blue.length = 5;
192 var->transp.offset = 15;
193 var->transp.length = 1;
194 } else { /* RGB 565 */
197 var->green.offset = 5;
198 var->green.length = 6;
199 var->blue.offset = 11;
200 var->blue.length = 5;
201 var->transp.offset = 0;
202 var->transp.length = 0;
205 case 24: /* RGB 888 */
208 var->green.offset = 8;
209 var->green.length = 8;
210 var->blue.offset = 16;
211 var->blue.length = 8;
212 var->transp.offset = 0;
213 var->transp.length = 0;
215 case 32: /* RGBA 8888 */
218 var->green.offset = 8;
219 var->green.length = 8;
220 var->blue.offset = 16;
221 var->blue.length = 8;
222 var->transp.offset = 24;
223 var->transp.length = 8;
226 var->red.msb_right = 0;
227 var->green.msb_right = 0;
228 var->blue.msb_right = 0;
229 var->transp.msb_right = 0;
234 /* This routine actually sets the video mode. It's in here where we
235 * the hardware state info->par and fix which can be affected by the
236 * change in par. For this driver it doesn't do much.
238 static int vfb_set_par(struct fb_info *info)
240 switch (info->var.bits_per_pixel) {
242 info->fix.visual = FB_VISUAL_MONO01;
245 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
250 info->fix.visual = FB_VISUAL_TRUECOLOR;
254 info->fix.line_length = get_line_length(info->var.xres_virtual,
255 info->var.bits_per_pixel);
261 * Set a single color register. The values supplied are already
262 * rounded down to the hardware's capabilities (according to the
263 * entries in the var structure). Return != 0 for invalid regno.
266 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
267 u_int transp, struct fb_info *info)
269 if (regno >= 256) /* no. of hw registers */
272 * Program hardware... do anything you want with transp
275 /* grayscale works only partially under directcolor */
276 if (info->var.grayscale) {
277 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
279 (red * 77 + green * 151 + blue * 28) >> 8;
283 * var->{color}.offset contains start of bitfield
284 * var->{color}.length contains length of bitfield
285 * {hardwarespecific} contains width of RAMDAC
286 * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
287 * RAMDAC[X] is programmed to (red, green, blue)
290 * var->{color}.offset is 0 unless the palette index takes less than
291 * bits_per_pixel bits and is stored in the upper
292 * bits of the pixel value
293 * var->{color}.length is set so that 1 << length is the number of available
296 * RAMDAC[X] is programmed to (red, green, blue)
299 * does not use DAC. Usually 3 are present.
300 * var->{color}.offset contains start of bitfield
301 * var->{color}.length contains length of bitfield
302 * cmap is programmed to (red << red.offset) | (green << green.offset) |
303 * (blue << blue.offset) | (transp << transp.offset)
304 * RAMDAC does not exist
306 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
307 switch (info->fix.visual) {
308 case FB_VISUAL_TRUECOLOR:
309 case FB_VISUAL_PSEUDOCOLOR:
310 red = CNVT_TOHW(red, info->var.red.length);
311 green = CNVT_TOHW(green, info->var.green.length);
312 blue = CNVT_TOHW(blue, info->var.blue.length);
313 transp = CNVT_TOHW(transp, info->var.transp.length);
315 case FB_VISUAL_DIRECTCOLOR:
316 red = CNVT_TOHW(red, 8); /* expect 8 bit DAC */
317 green = CNVT_TOHW(green, 8);
318 blue = CNVT_TOHW(blue, 8);
319 /* hey, there is bug in transp handling... */
320 transp = CNVT_TOHW(transp, 8);
324 /* Truecolor has hardware independent palette */
325 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
331 v = (red << info->var.red.offset) |
332 (green << info->var.green.offset) |
333 (blue << info->var.blue.offset) |
334 (transp << info->var.transp.offset);
335 switch (info->var.bits_per_pixel) {
339 ((u32 *) (info->pseudo_palette))[regno] = v;
343 ((u32 *) (info->pseudo_palette))[regno] = v;
352 * Pan or Wrap the Display
354 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
357 static int vfb_pan_display(struct fb_var_screeninfo *var,
358 struct fb_info *info)
360 if (var->vmode & FB_VMODE_YWRAP) {
361 if (var->yoffset >= info->var.yres_virtual ||
365 if (var->xoffset + info->var.xres > info->var.xres_virtual ||
366 var->yoffset + info->var.yres > info->var.yres_virtual)
369 info->var.xoffset = var->xoffset;
370 info->var.yoffset = var->yoffset;
371 if (var->vmode & FB_VMODE_YWRAP)
372 info->var.vmode |= FB_VMODE_YWRAP;
374 info->var.vmode &= ~FB_VMODE_YWRAP;
379 * Most drivers don't need their own mmap function
382 static int vfb_mmap(struct fb_info *info,
383 struct vm_area_struct *vma)
385 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
387 return remap_vmalloc_range(vma, (void *)info->fix.smem_start, vma->vm_pgoff);
392 * The virtual framebuffer driver is only enabled if explicitly
393 * requested by passing 'video=vfb:' (or any actual options).
395 static int __init vfb_setup(char *options)
409 while ((this_opt = strsep(&options, ",")) != NULL) {
412 /* Test disable for backwards compatibility */
413 if (!strcmp(this_opt, "disable"))
416 mode_option = this_opt;
426 static int vfb_probe(struct platform_device *dev)
428 struct fb_info *info;
429 unsigned int size = PAGE_ALIGN(videomemorysize);
430 int retval = -ENOMEM;
433 * For real video cards we use ioremap.
435 if (!(videomemory = vmalloc_32_user(size)))
438 info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
442 info->flags |= FBINFO_VIRTFB;
443 info->screen_buffer = videomemory;
444 info->fbops = &vfb_ops;
446 if (!fb_find_mode(&info->var, info, mode_option,
447 NULL, 0, &vfb_default, 8)){
448 fb_err(info, "Unable to find usable video mode.\n");
453 vfb_fix.smem_start = (unsigned long) videomemory;
454 vfb_fix.smem_len = videomemorysize;
456 info->pseudo_palette = info->par;
459 retval = fb_alloc_cmap(&info->cmap, 256, 0);
463 retval = register_framebuffer(info);
466 platform_set_drvdata(dev, info);
470 fb_info(info, "Virtual frame buffer device, using %ldK of video memory\n",
471 videomemorysize >> 10);
474 fb_dealloc_cmap(&info->cmap);
476 framebuffer_release(info);
482 static void vfb_remove(struct platform_device *dev)
484 struct fb_info *info = platform_get_drvdata(dev);
487 unregister_framebuffer(info);
489 fb_dealloc_cmap(&info->cmap);
490 framebuffer_release(info);
494 static struct platform_driver vfb_driver = {
496 .remove = vfb_remove,
502 static struct platform_device *vfb_device;
504 static int __init vfb_init(void)
511 if (fb_get_options("vfb", &option))
519 ret = platform_driver_register(&vfb_driver);
522 vfb_device = platform_device_alloc("vfb", 0);
525 ret = platform_device_add(vfb_device);
530 platform_device_put(vfb_device);
531 platform_driver_unregister(&vfb_driver);
538 module_init(vfb_init);
541 static void __exit vfb_exit(void)
543 platform_device_unregister(vfb_device);
544 platform_driver_unregister(&vfb_driver);
547 module_exit(vfb_exit);
549 MODULE_DESCRIPTION("Virtual Frame Buffer driver");
550 MODULE_LICENSE("GPL");