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);
39 /**********************************************************************
43 **********************************************************************/
44 static void *rvmalloc(unsigned long size
)
49 size
= PAGE_ALIGN(size
);
50 mem
= vmalloc_32(size
);
54 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
55 adr
= (unsigned long) mem
;
57 SetPageReserved(vmalloc_to_page((void *)adr
));
65 static void rvfree(void *mem
, unsigned long size
)
72 adr
= (unsigned long) mem
;
73 while ((long) size
> 0) {
74 ClearPageReserved(vmalloc_to_page((void *)adr
));
81 static struct fb_var_screeninfo vfb_default __devinitdata
= {
90 .activate
= FB_ACTIVATE_TEST
,
100 .vmode
= FB_VMODE_NONINTERLACED
,
103 static struct fb_fix_screeninfo vfb_fix __devinitdata
= {
105 .type
= FB_TYPE_PACKED_PIXELS
,
106 .visual
= FB_VISUAL_PSEUDOCOLOR
,
110 .accel
= FB_ACCEL_NONE
,
113 static bool vfb_enable __initdata
= 0; /* disabled by default */
114 module_param(vfb_enable
, bool, 0);
116 static int vfb_check_var(struct fb_var_screeninfo
*var
,
117 struct fb_info
*info
);
118 static int vfb_set_par(struct fb_info
*info
);
119 static int vfb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
120 u_int transp
, struct fb_info
*info
);
121 static int vfb_pan_display(struct fb_var_screeninfo
*var
,
122 struct fb_info
*info
);
123 static int vfb_mmap(struct fb_info
*info
,
124 struct vm_area_struct
*vma
);
126 static struct fb_ops vfb_ops
= {
127 .fb_read
= fb_sys_read
,
128 .fb_write
= fb_sys_write
,
129 .fb_check_var
= vfb_check_var
,
130 .fb_set_par
= vfb_set_par
,
131 .fb_setcolreg
= vfb_setcolreg
,
132 .fb_pan_display
= vfb_pan_display
,
133 .fb_fillrect
= sys_fillrect
,
134 .fb_copyarea
= sys_copyarea
,
135 .fb_imageblit
= sys_imageblit
,
143 static u_long
get_line_length(int xres_virtual
, int bpp
)
147 length
= xres_virtual
* bpp
;
148 length
= (length
+ 31) & ~31;
154 * Setting the video mode has been split into two parts.
155 * First part, xxxfb_check_var, must not write anything
156 * to hardware, it should only verify and adjust var.
157 * This means it doesn't alter par but it does use hardware
158 * data from it to check this var.
161 static int vfb_check_var(struct fb_var_screeninfo
*var
,
162 struct fb_info
*info
)
167 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
168 * as FB_VMODE_SMOOTH_XPAN is only used internally
171 if (var
->vmode
& FB_VMODE_CONUPDATE
) {
172 var
->vmode
|= FB_VMODE_YWRAP
;
173 var
->xoffset
= info
->var
.xoffset
;
174 var
->yoffset
= info
->var
.yoffset
;
178 * Some very basic checks
184 if (var
->xres
> var
->xres_virtual
)
185 var
->xres_virtual
= var
->xres
;
186 if (var
->yres
> var
->yres_virtual
)
187 var
->yres_virtual
= var
->yres
;
188 if (var
->bits_per_pixel
<= 1)
189 var
->bits_per_pixel
= 1;
190 else if (var
->bits_per_pixel
<= 8)
191 var
->bits_per_pixel
= 8;
192 else if (var
->bits_per_pixel
<= 16)
193 var
->bits_per_pixel
= 16;
194 else if (var
->bits_per_pixel
<= 24)
195 var
->bits_per_pixel
= 24;
196 else if (var
->bits_per_pixel
<= 32)
197 var
->bits_per_pixel
= 32;
201 if (var
->xres_virtual
< var
->xoffset
+ var
->xres
)
202 var
->xres_virtual
= var
->xoffset
+ var
->xres
;
203 if (var
->yres_virtual
< var
->yoffset
+ var
->yres
)
204 var
->yres_virtual
= var
->yoffset
+ var
->yres
;
210 get_line_length(var
->xres_virtual
, var
->bits_per_pixel
);
211 if (line_length
* var
->yres_virtual
> videomemorysize
)
215 * Now that we checked it we alter var. The reason being is that the video
216 * mode passed in might not work but slight changes to it might make it
217 * work. This way we let the user know what is acceptable.
219 switch (var
->bits_per_pixel
) {
224 var
->green
.offset
= 0;
225 var
->green
.length
= 8;
226 var
->blue
.offset
= 0;
227 var
->blue
.length
= 8;
228 var
->transp
.offset
= 0;
229 var
->transp
.length
= 0;
231 case 16: /* RGBA 5551 */
232 if (var
->transp
.length
) {
235 var
->green
.offset
= 5;
236 var
->green
.length
= 5;
237 var
->blue
.offset
= 10;
238 var
->blue
.length
= 5;
239 var
->transp
.offset
= 15;
240 var
->transp
.length
= 1;
241 } else { /* RGB 565 */
244 var
->green
.offset
= 5;
245 var
->green
.length
= 6;
246 var
->blue
.offset
= 11;
247 var
->blue
.length
= 5;
248 var
->transp
.offset
= 0;
249 var
->transp
.length
= 0;
252 case 24: /* RGB 888 */
255 var
->green
.offset
= 8;
256 var
->green
.length
= 8;
257 var
->blue
.offset
= 16;
258 var
->blue
.length
= 8;
259 var
->transp
.offset
= 0;
260 var
->transp
.length
= 0;
262 case 32: /* RGBA 8888 */
265 var
->green
.offset
= 8;
266 var
->green
.length
= 8;
267 var
->blue
.offset
= 16;
268 var
->blue
.length
= 8;
269 var
->transp
.offset
= 24;
270 var
->transp
.length
= 8;
273 var
->red
.msb_right
= 0;
274 var
->green
.msb_right
= 0;
275 var
->blue
.msb_right
= 0;
276 var
->transp
.msb_right
= 0;
281 /* This routine actually sets the video mode. It's in here where we
282 * the hardware state info->par and fix which can be affected by the
283 * change in par. For this driver it doesn't do much.
285 static int vfb_set_par(struct fb_info
*info
)
287 info
->fix
.line_length
= get_line_length(info
->var
.xres_virtual
,
288 info
->var
.bits_per_pixel
);
293 * Set a single color register. The values supplied are already
294 * rounded down to the hardware's capabilities (according to the
295 * entries in the var structure). Return != 0 for invalid regno.
298 static int vfb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
299 u_int transp
, struct fb_info
*info
)
301 if (regno
>= 256) /* no. of hw registers */
304 * Program hardware... do anything you want with transp
307 /* grayscale works only partially under directcolor */
308 if (info
->var
.grayscale
) {
309 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
311 (red
* 77 + green
* 151 + blue
* 28) >> 8;
315 * var->{color}.offset contains start of bitfield
316 * var->{color}.length contains length of bitfield
317 * {hardwarespecific} contains width of RAMDAC
318 * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
319 * RAMDAC[X] is programmed to (red, green, blue)
322 * var->{color}.offset is 0 unless the palette index takes less than
323 * bits_per_pixel bits and is stored in the upper
324 * bits of the pixel value
325 * var->{color}.length is set so that 1 << length is the number of available
328 * RAMDAC[X] is programmed to (red, green, blue)
331 * does not use DAC. Usually 3 are present.
332 * var->{color}.offset contains start of bitfield
333 * var->{color}.length contains length of bitfield
334 * cmap is programmed to (red << red.offset) | (green << green.offset) |
335 * (blue << blue.offset) | (transp << transp.offset)
336 * RAMDAC does not exist
338 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
339 switch (info
->fix
.visual
) {
340 case FB_VISUAL_TRUECOLOR
:
341 case FB_VISUAL_PSEUDOCOLOR
:
342 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
343 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
344 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
345 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
347 case FB_VISUAL_DIRECTCOLOR
:
348 red
= CNVT_TOHW(red
, 8); /* expect 8 bit DAC */
349 green
= CNVT_TOHW(green
, 8);
350 blue
= CNVT_TOHW(blue
, 8);
351 /* hey, there is bug in transp handling... */
352 transp
= CNVT_TOHW(transp
, 8);
356 /* Truecolor has hardware independent palette */
357 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
363 v
= (red
<< info
->var
.red
.offset
) |
364 (green
<< info
->var
.green
.offset
) |
365 (blue
<< info
->var
.blue
.offset
) |
366 (transp
<< info
->var
.transp
.offset
);
367 switch (info
->var
.bits_per_pixel
) {
371 ((u32
*) (info
->pseudo_palette
))[regno
] = v
;
375 ((u32
*) (info
->pseudo_palette
))[regno
] = v
;
384 * Pan or Wrap the Display
386 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
389 static int vfb_pan_display(struct fb_var_screeninfo
*var
,
390 struct fb_info
*info
)
392 if (var
->vmode
& FB_VMODE_YWRAP
) {
394 || var
->yoffset
>= info
->var
.yres_virtual
398 if (var
->xoffset
+ info
->var
.xres
> info
->var
.xres_virtual
||
399 var
->yoffset
+ info
->var
.yres
> info
->var
.yres_virtual
)
402 info
->var
.xoffset
= var
->xoffset
;
403 info
->var
.yoffset
= var
->yoffset
;
404 if (var
->vmode
& FB_VMODE_YWRAP
)
405 info
->var
.vmode
|= FB_VMODE_YWRAP
;
407 info
->var
.vmode
&= ~FB_VMODE_YWRAP
;
412 * Most drivers don't need their own mmap function
415 static int vfb_mmap(struct fb_info
*info
,
416 struct vm_area_struct
*vma
)
418 unsigned long start
= vma
->vm_start
;
419 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
420 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
421 unsigned long page
, pos
;
423 if (offset
+ size
> info
->fix
.smem_len
) {
427 pos
= (unsigned long)info
->fix
.smem_start
+ offset
;
430 page
= vmalloc_to_pfn((void *)pos
);
431 if (remap_pfn_range(vma
, start
, page
, PAGE_SIZE
, PAGE_SHARED
)) {
436 if (size
> PAGE_SIZE
)
442 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
449 * The virtual framebuffer driver is only enabled if explicitly
450 * requested by passing 'video=vfb:' (or any actual options).
452 static int __init
vfb_setup(char *options
)
466 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
469 /* Test disable for backwards compatibility */
470 if (!strcmp(this_opt
, "disable"))
481 static int __devinit
vfb_probe(struct platform_device
*dev
)
483 struct fb_info
*info
;
484 int retval
= -ENOMEM
;
487 * For real video cards we use ioremap.
489 if (!(videomemory
= rvmalloc(videomemorysize
)))
493 * VFB must clear memory to prevent kernel info
494 * leakage into userspace
495 * VGA-based drivers MUST NOT clear memory if
496 * they want to be able to take over vgacon
498 memset(videomemory
, 0, videomemorysize
);
500 info
= framebuffer_alloc(sizeof(u32
) * 256, &dev
->dev
);
504 info
->screen_base
= (char __iomem
*)videomemory
;
505 info
->fbops
= &vfb_ops
;
507 retval
= fb_find_mode(&info
->var
, info
, NULL
,
510 if (!retval
|| (retval
== 4))
511 info
->var
= vfb_default
;
512 vfb_fix
.smem_start
= (unsigned long) videomemory
;
513 vfb_fix
.smem_len
= videomemorysize
;
515 info
->pseudo_palette
= info
->par
;
517 info
->flags
= FBINFO_FLAG_DEFAULT
;
519 retval
= fb_alloc_cmap(&info
->cmap
, 256, 0);
523 retval
= register_framebuffer(info
);
526 platform_set_drvdata(dev
, info
);
529 "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
530 info
->node
, videomemorysize
>> 10);
533 fb_dealloc_cmap(&info
->cmap
);
535 framebuffer_release(info
);
537 rvfree(videomemory
, videomemorysize
);
541 static int vfb_remove(struct platform_device
*dev
)
543 struct fb_info
*info
= platform_get_drvdata(dev
);
546 unregister_framebuffer(info
);
547 rvfree(videomemory
, videomemorysize
);
548 fb_dealloc_cmap(&info
->cmap
);
549 framebuffer_release(info
);
554 static struct platform_driver vfb_driver
= {
556 .remove
= vfb_remove
,
562 static struct platform_device
*vfb_device
;
564 static int __init
vfb_init(void)
571 if (fb_get_options("vfb", &option
))
579 ret
= platform_driver_register(&vfb_driver
);
582 vfb_device
= platform_device_alloc("vfb", 0);
585 ret
= platform_device_add(vfb_device
);
590 platform_device_put(vfb_device
);
591 platform_driver_unregister(&vfb_driver
);
598 module_init(vfb_init
);
601 static void __exit
vfb_exit(void)
603 platform_device_unregister(vfb_device
);
604 platform_driver_unregister(&vfb_driver
);
607 module_exit(vfb_exit
);
609 MODULE_LICENSE("GPL");