Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / drivers / video / vfb.c
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/tty.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <asm/uaccess.h>
24 #include <linux/fb.h>
25 #include <linux/init.h>
26
27     /*
28      *  RAM we reserve for the frame buffer. This defines the maximum screen
29      *  size
30      *
31      *  The default can be overridden if the driver is compiled as a module
32      */
33
34 #define VIDEOMEMSIZE    (1*1024*1024)   /* 1 MB */
35
36 static void *videomemory;
37 static u_long videomemorysize = VIDEOMEMSIZE;
38 module_param(videomemorysize, ulong, 0);
39
40 static struct fb_var_screeninfo vfb_default __initdata = {
41         .xres =         640,
42         .yres =         480,
43         .xres_virtual = 640,
44         .yres_virtual = 480,
45         .bits_per_pixel = 8,
46         .red =          { 0, 8, 0 },
47         .green =        { 0, 8, 0 },
48         .blue =         { 0, 8, 0 },
49         .activate =     FB_ACTIVATE_TEST,
50         .height =       -1,
51         .width =        -1,
52         .pixclock =     20000,
53         .left_margin =  64,
54         .right_margin = 64,
55         .upper_margin = 32,
56         .lower_margin = 32,
57         .hsync_len =    64,
58         .vsync_len =    2,
59         .vmode =        FB_VMODE_NONINTERLACED,
60 };
61
62 static struct fb_fix_screeninfo vfb_fix __initdata = {
63         .id =           "Virtual FB",
64         .type =         FB_TYPE_PACKED_PIXELS,
65         .visual =       FB_VISUAL_PSEUDOCOLOR,
66         .xpanstep =     1,
67         .ypanstep =     1,
68         .ywrapstep =    1,
69         .accel =        FB_ACCEL_NONE,
70 };
71
72 static int vfb_enable __initdata = 0;   /* disabled by default */
73 module_param(vfb_enable, bool, 0);
74
75 static int vfb_check_var(struct fb_var_screeninfo *var,
76                          struct fb_info *info);
77 static int vfb_set_par(struct fb_info *info);
78 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
79                          u_int transp, struct fb_info *info);
80 static int vfb_pan_display(struct fb_var_screeninfo *var,
81                            struct fb_info *info);
82 static int vfb_mmap(struct fb_info *info, struct file *file,
83                     struct vm_area_struct *vma);
84
85 static struct fb_ops vfb_ops = {
86         .fb_check_var   = vfb_check_var,
87         .fb_set_par     = vfb_set_par,
88         .fb_setcolreg   = vfb_setcolreg,
89         .fb_pan_display = vfb_pan_display,
90         .fb_fillrect    = cfb_fillrect,
91         .fb_copyarea    = cfb_copyarea,
92         .fb_imageblit   = cfb_imageblit,
93         .fb_cursor      = soft_cursor,
94         .fb_mmap        = vfb_mmap,
95 };
96
97     /*
98      *  Internal routines
99      */
100
101 static u_long get_line_length(int xres_virtual, int bpp)
102 {
103         u_long length;
104
105         length = xres_virtual * bpp;
106         length = (length + 31) & ~31;
107         length >>= 3;
108         return (length);
109 }
110
111     /*
112      *  Setting the video mode has been split into two parts.
113      *  First part, xxxfb_check_var, must not write anything
114      *  to hardware, it should only verify and adjust var.
115      *  This means it doesn't alter par but it does use hardware
116      *  data from it to check this var. 
117      */
118
119 static int vfb_check_var(struct fb_var_screeninfo *var,
120                          struct fb_info *info)
121 {
122         u_long line_length;
123
124         /*
125          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
126          *  as FB_VMODE_SMOOTH_XPAN is only used internally
127          */
128
129         if (var->vmode & FB_VMODE_CONUPDATE) {
130                 var->vmode |= FB_VMODE_YWRAP;
131                 var->xoffset = info->var.xoffset;
132                 var->yoffset = info->var.yoffset;
133         }
134
135         /*
136          *  Some very basic checks
137          */
138         if (!var->xres)
139                 var->xres = 1;
140         if (!var->yres)
141                 var->yres = 1;
142         if (var->xres > var->xres_virtual)
143                 var->xres_virtual = var->xres;
144         if (var->yres > var->yres_virtual)
145                 var->yres_virtual = var->yres;
146         if (var->bits_per_pixel <= 1)
147                 var->bits_per_pixel = 1;
148         else if (var->bits_per_pixel <= 8)
149                 var->bits_per_pixel = 8;
150         else if (var->bits_per_pixel <= 16)
151                 var->bits_per_pixel = 16;
152         else if (var->bits_per_pixel <= 24)
153                 var->bits_per_pixel = 24;
154         else if (var->bits_per_pixel <= 32)
155                 var->bits_per_pixel = 32;
156         else
157                 return -EINVAL;
158
159         if (var->xres_virtual < var->xoffset + var->xres)
160                 var->xres_virtual = var->xoffset + var->xres;
161         if (var->yres_virtual < var->yoffset + var->yres)
162                 var->yres_virtual = var->yoffset + var->yres;
163
164         /*
165          *  Memory limit
166          */
167         line_length =
168             get_line_length(var->xres_virtual, var->bits_per_pixel);
169         if (line_length * var->yres_virtual > videomemorysize)
170                 return -ENOMEM;
171
172         /*
173          * Now that we checked it we alter var. The reason being is that the video
174          * mode passed in might not work but slight changes to it might make it 
175          * work. This way we let the user know what is acceptable.
176          */
177         switch (var->bits_per_pixel) {
178         case 1:
179         case 8:
180                 var->red.offset = 0;
181                 var->red.length = 8;
182                 var->green.offset = 0;
183                 var->green.length = 8;
184                 var->blue.offset = 0;
185                 var->blue.length = 8;
186                 var->transp.offset = 0;
187                 var->transp.length = 0;
188                 break;
189         case 16:                /* RGBA 5551 */
190                 if (var->transp.length) {
191                         var->red.offset = 0;
192                         var->red.length = 5;
193                         var->green.offset = 5;
194                         var->green.length = 5;
195                         var->blue.offset = 10;
196                         var->blue.length = 5;
197                         var->transp.offset = 15;
198                         var->transp.length = 1;
199                 } else {        /* RGB 565 */
200                         var->red.offset = 0;
201                         var->red.length = 5;
202                         var->green.offset = 5;
203                         var->green.length = 6;
204                         var->blue.offset = 11;
205                         var->blue.length = 5;
206                         var->transp.offset = 0;
207                         var->transp.length = 0;
208                 }
209                 break;
210         case 24:                /* RGB 888 */
211                 var->red.offset = 0;
212                 var->red.length = 8;
213                 var->green.offset = 8;
214                 var->green.length = 8;
215                 var->blue.offset = 16;
216                 var->blue.length = 8;
217                 var->transp.offset = 0;
218                 var->transp.length = 0;
219                 break;
220         case 32:                /* RGBA 8888 */
221                 var->red.offset = 0;
222                 var->red.length = 8;
223                 var->green.offset = 8;
224                 var->green.length = 8;
225                 var->blue.offset = 16;
226                 var->blue.length = 8;
227                 var->transp.offset = 24;
228                 var->transp.length = 8;
229                 break;
230         }
231         var->red.msb_right = 0;
232         var->green.msb_right = 0;
233         var->blue.msb_right = 0;
234         var->transp.msb_right = 0;
235
236         return 0;
237 }
238
239 /* This routine actually sets the video mode. It's in here where we
240  * the hardware state info->par and fix which can be affected by the 
241  * change in par. For this driver it doesn't do much. 
242  */
243 static int vfb_set_par(struct fb_info *info)
244 {
245         info->fix.line_length = get_line_length(info->var.xres_virtual,
246                                                 info->var.bits_per_pixel);
247         return 0;
248 }
249
250     /*
251      *  Set a single color register. The values supplied are already
252      *  rounded down to the hardware's capabilities (according to the
253      *  entries in the var structure). Return != 0 for invalid regno.
254      */
255
256 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
257                          u_int transp, struct fb_info *info)
258 {
259         if (regno >= 256)       /* no. of hw registers */
260                 return 1;
261         /*
262          * Program hardware... do anything you want with transp
263          */
264
265         /* grayscale works only partially under directcolor */
266         if (info->var.grayscale) {
267                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
268                 red = green = blue =
269                     (red * 77 + green * 151 + blue * 28) >> 8;
270         }
271
272         /* Directcolor:
273          *   var->{color}.offset contains start of bitfield
274          *   var->{color}.length contains length of bitfield
275          *   {hardwarespecific} contains width of RAMDAC
276          *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
277          *   RAMDAC[X] is programmed to (red, green, blue)
278          * 
279          * Pseudocolor:
280          *    uses offset = 0 && length = RAMDAC register width.
281          *    var->{color}.offset is 0
282          *    var->{color}.length contains widht of DAC
283          *    cmap is not used
284          *    RAMDAC[X] is programmed to (red, green, blue)
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
344 static 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 < 0
349                     || var->yoffset >= info->var.yres_virtual
350                     || var->xoffset)
351                         return -EINVAL;
352         } else {
353                 if (var->xoffset + var->xres > info->var.xres_virtual ||
354                     var->yoffset + var->yres > info->var.yres_virtual)
355                         return -EINVAL;
356         }
357         info->var.xoffset = var->xoffset;
358         info->var.yoffset = var->yoffset;
359         if (var->vmode & FB_VMODE_YWRAP)
360                 info->var.vmode |= FB_VMODE_YWRAP;
361         else
362                 info->var.vmode &= ~FB_VMODE_YWRAP;
363         return 0;
364 }
365
366     /*
367      *  Most drivers don't need their own mmap function 
368      */
369
370 static int vfb_mmap(struct fb_info *info, struct file *file,
371                     struct vm_area_struct *vma)
372 {
373         return -EINVAL;
374 }
375
376 #ifndef MODULE
377 static int __init vfb_setup(char *options)
378 {
379         char *this_opt;
380
381         vfb_enable = 1;
382
383         if (!options || !*options)
384                 return 1;
385
386         while ((this_opt = strsep(&options, ",")) != NULL) {
387                 if (!*this_opt)
388                         continue;
389                 if (!strncmp(this_opt, "disable", 7))
390                         vfb_enable = 0;
391         }
392         return 1;
393 }
394 #endif  /*  MODULE  */
395
396     /*
397      *  Initialisation
398      */
399
400 static void vfb_platform_release(struct device *device)
401 {
402         // This is called when the reference count goes to zero.
403 }
404
405 static int __init vfb_probe(struct device *device)
406 {
407         struct platform_device *dev = to_platform_device(device);
408         struct fb_info *info;
409         int retval = -ENOMEM;
410
411         /*
412          * For real video cards we use ioremap.
413          */
414         if (!(videomemory = vmalloc(videomemorysize)))
415                 return retval;
416
417         /*
418          * VFB must clear memory to prevent kernel info
419          * leakage into userspace
420          * VGA-based drivers MUST NOT clear memory if
421          * they want to be able to take over vgacon
422          */
423         memset(videomemory, 0, videomemorysize);
424
425         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
426         if (!info)
427                 goto err;
428
429         info->screen_base = (char __iomem *)videomemory;
430         info->fbops = &vfb_ops;
431
432         retval = fb_find_mode(&info->var, info, NULL,
433                               NULL, 0, NULL, 8);
434
435         if (!retval || (retval == 4))
436                 info->var = vfb_default;
437         info->fix = vfb_fix;
438         info->pseudo_palette = info->par;
439         info->par = NULL;
440         info->flags = FBINFO_FLAG_DEFAULT;
441
442         retval = fb_alloc_cmap(&info->cmap, 256, 0);
443         if (retval < 0)
444                 goto err1;
445
446         retval = register_framebuffer(info);
447         if (retval < 0)
448                 goto err2;
449         dev_set_drvdata(&dev->dev, info);
450
451         printk(KERN_INFO
452                "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
453                info->node, videomemorysize >> 10);
454         return 0;
455 err2:
456         fb_dealloc_cmap(&info->cmap);
457 err1:
458         framebuffer_release(info);
459 err:
460         vfree(videomemory);
461         return retval;
462 }
463
464 static int vfb_remove(struct device *device)
465 {
466         struct fb_info *info = dev_get_drvdata(device);
467
468         if (info) {
469                 unregister_framebuffer(info);
470                 vfree(videomemory);
471                 framebuffer_release(info);
472         }
473         return 0;
474 }
475
476 static struct device_driver vfb_driver = {
477         .name   = "vfb",
478         .bus    = &platform_bus_type,
479         .probe  = vfb_probe,
480         .remove = vfb_remove,
481 };
482
483 static struct platform_device vfb_device = {
484         .name   = "vfb",
485         .id     = 0,
486         .dev    = {
487                 .release = vfb_platform_release,
488         }
489 };
490
491 static int __init vfb_init(void)
492 {
493         int ret = 0;
494
495 #ifndef MODULE
496         char *option = NULL;
497
498         if (fb_get_options("vfb", &option))
499                 return -ENODEV;
500         vfb_setup(option);
501 #endif
502
503         if (!vfb_enable)
504                 return -ENXIO;
505
506         ret = driver_register(&vfb_driver);
507
508         if (!ret) {
509                 ret = platform_device_register(&vfb_device);
510                 if (ret)
511                         driver_unregister(&vfb_driver);
512         }
513         return ret;
514 }
515
516 module_init(vfb_init);
517
518 #ifdef MODULE
519 static void __exit vfb_exit(void)
520 {
521         platform_device_unregister(&vfb_device);
522         driver_unregister(&vfb_driver);
523 }
524
525 module_exit(vfb_exit);
526
527 MODULE_LICENSE("GPL");
528 #endif                          /* MODULE */