ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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_PARM(videomemorysize, "l");
39
40 static struct fb_info fb_info;
41 static u32 vfb_pseudo_palette[17];
42
43 static struct fb_var_screeninfo vfb_default __initdata = {
44         .xres =         640,
45         .yres =         480,
46         .xres_virtual = 640,
47         .yres_virtual = 480,
48         .bits_per_pixel = 8,
49         .red =          { 0, 8, 0 },
50         .green =        { 0, 8, 0 },
51         .blue =         { 0, 8, 0 },
52         .activate =     FB_ACTIVATE_TEST,
53         .height =       -1,
54         .width =        -1,
55         .pixclock =     20000,
56         .left_margin =  64,
57         .right_margin = 64,
58         .upper_margin = 32,
59         .lower_margin = 32,
60         .hsync_len =    64,
61         .vsync_len =    2,
62         .vmode =        FB_VMODE_NONINTERLACED,
63 };
64
65 static struct fb_fix_screeninfo vfb_fix __initdata = {
66         .id =           "Virtual FB",
67         .type =         FB_TYPE_PACKED_PIXELS,
68         .visual =       FB_VISUAL_PSEUDOCOLOR,
69         .xpanstep =     1,
70         .ypanstep =     1,
71         .ywrapstep =    1,
72         .accel =        FB_ACCEL_NONE,
73 };
74
75 static int vfb_enable __initdata = 0;   /* disabled by default */
76 MODULE_PARM(vfb_enable, "i");
77
78     /*
79      *  Interface used by the world
80      */
81 int vfb_init(void);
82 int vfb_setup(char *);
83
84 static int vfb_check_var(struct fb_var_screeninfo *var,
85                          struct fb_info *info);
86 static int vfb_set_par(struct fb_info *info);
87 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
88                          u_int transp, struct fb_info *info);
89 static int vfb_pan_display(struct fb_var_screeninfo *var,
90                            struct fb_info *info);
91 static int vfb_mmap(struct fb_info *info, struct file *file,
92                     struct vm_area_struct *vma);
93
94 static struct fb_ops vfb_ops = {
95         .fb_check_var   = vfb_check_var,
96         .fb_set_par     = vfb_set_par,
97         .fb_setcolreg   = vfb_setcolreg,
98         .fb_pan_display = vfb_pan_display,
99         .fb_fillrect    = cfb_fillrect,
100         .fb_copyarea    = cfb_copyarea,
101         .fb_imageblit   = cfb_imageblit,
102         .fb_cursor      = soft_cursor,
103         .fb_mmap        = vfb_mmap,
104 };
105
106     /*
107      *  Internal routines
108      */
109
110 static u_long get_line_length(int xres_virtual, int bpp)
111 {
112         u_long length;
113
114         length = xres_virtual * bpp;
115         length = (length + 31) & ~31;
116         length >>= 3;
117         return (length);
118 }
119
120     /*
121      *  Setting the video mode has been split into two parts.
122      *  First part, xxxfb_check_var, must not write anything
123      *  to hardware, it should only verify and adjust var.
124      *  This means it doesn't alter par but it does use hardware
125      *  data from it to check this var. 
126      */
127
128 static int vfb_check_var(struct fb_var_screeninfo *var,
129                          struct fb_info *info)
130 {
131         u_long line_length;
132
133         /*
134          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
135          *  as FB_VMODE_SMOOTH_XPAN is only used internally
136          */
137
138         if (var->vmode & FB_VMODE_CONUPDATE) {
139                 var->vmode |= FB_VMODE_YWRAP;
140                 var->xoffset = info->var.xoffset;
141                 var->yoffset = info->var.yoffset;
142         }
143
144         /*
145          *  Some very basic checks
146          */
147         if (!var->xres)
148                 var->xres = 1;
149         if (!var->yres)
150                 var->yres = 1;
151         if (var->xres > var->xres_virtual)
152                 var->xres_virtual = var->xres;
153         if (var->yres > var->yres_virtual)
154                 var->yres_virtual = var->yres;
155         if (var->bits_per_pixel <= 1)
156                 var->bits_per_pixel = 1;
157         else if (var->bits_per_pixel <= 8)
158                 var->bits_per_pixel = 8;
159         else if (var->bits_per_pixel <= 16)
160                 var->bits_per_pixel = 16;
161         else if (var->bits_per_pixel <= 24)
162                 var->bits_per_pixel = 24;
163         else if (var->bits_per_pixel <= 32)
164                 var->bits_per_pixel = 32;
165         else
166                 return -EINVAL;
167
168         if (var->xres_virtual < var->xoffset + var->xres)
169                 var->xres_virtual = var->xoffset + var->xres;
170         if (var->yres_virtual < var->yoffset + var->yres)
171                 var->yres_virtual = var->yoffset + var->yres;
172
173         /*
174          *  Memory limit
175          */
176         line_length =
177             get_line_length(var->xres_virtual, var->bits_per_pixel);
178         if (line_length * var->yres_virtual > videomemorysize)
179                 return -ENOMEM;
180
181         /*
182          * Now that we checked it we alter var. The reason being is that the video
183          * mode passed in might not work but slight changes to it might make it 
184          * work. This way we let the user know what is acceptable.
185          */
186         switch (var->bits_per_pixel) {
187         case 1:
188         case 8:
189                 var->red.offset = 0;
190                 var->red.length = 8;
191                 var->green.offset = 0;
192                 var->green.length = 8;
193                 var->blue.offset = 0;
194                 var->blue.length = 8;
195                 var->transp.offset = 0;
196                 var->transp.length = 0;
197                 break;
198         case 16:                /* RGBA 5551 */
199                 if (var->transp.length) {
200                         var->red.offset = 0;
201                         var->red.length = 5;
202                         var->green.offset = 5;
203                         var->green.length = 5;
204                         var->blue.offset = 10;
205                         var->blue.length = 5;
206                         var->transp.offset = 15;
207                         var->transp.length = 1;
208                 } else {        /* RGB 565 */
209                         var->red.offset = 0;
210                         var->red.length = 5;
211                         var->green.offset = 5;
212                         var->green.length = 6;
213                         var->blue.offset = 11;
214                         var->blue.length = 5;
215                         var->transp.offset = 0;
216                         var->transp.length = 0;
217                 }
218                 break;
219         case 24:                /* RGB 888 */
220                 var->red.offset = 0;
221                 var->red.length = 8;
222                 var->green.offset = 8;
223                 var->green.length = 8;
224                 var->blue.offset = 16;
225                 var->blue.length = 8;
226                 var->transp.offset = 0;
227                 var->transp.length = 0;
228                 break;
229         case 32:                /* RGBA 8888 */
230                 var->red.offset = 0;
231                 var->red.length = 8;
232                 var->green.offset = 8;
233                 var->green.length = 8;
234                 var->blue.offset = 16;
235                 var->blue.length = 8;
236                 var->transp.offset = 24;
237                 var->transp.length = 8;
238                 break;
239         }
240         var->red.msb_right = 0;
241         var->green.msb_right = 0;
242         var->blue.msb_right = 0;
243         var->transp.msb_right = 0;
244
245         return 0;
246 }
247
248 /* This routine actually sets the video mode. It's in here where we
249  * the hardware state info->par and fix which can be affected by the 
250  * change in par. For this driver it doesn't do much. 
251  */
252 static int vfb_set_par(struct fb_info *info)
253 {
254         info->fix.line_length = get_line_length(info->var.xres_virtual,
255                                                 info->var.bits_per_pixel);
256         return 0;
257 }
258
259     /*
260      *  Set a single color register. The values supplied are already
261      *  rounded down to the hardware's capabilities (according to the
262      *  entries in the var structure). Return != 0 for invalid regno.
263      */
264
265 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
266                          u_int transp, struct fb_info *info)
267 {
268         if (regno >= 256)       /* no. of hw registers */
269                 return 1;
270         /*
271          * Program hardware... do anything you want with transp
272          */
273
274         /* grayscale works only partially under directcolor */
275         if (info->var.grayscale) {
276                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
277                 red = green = blue =
278                     (red * 77 + green * 151 + blue * 28) >> 8;
279         }
280
281         /* Directcolor:
282          *   var->{color}.offset contains start of bitfield
283          *   var->{color}.length contains length of bitfield
284          *   {hardwarespecific} contains width of RAMDAC
285          *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
286          *   RAMDAC[X] is programmed to (red, green, blue)
287          * 
288          * Pseudocolor:
289          *    uses offset = 0 && length = RAMDAC register width.
290          *    var->{color}.offset is 0
291          *    var->{color}.length contains widht of DAC
292          *    cmap is not used
293          *    RAMDAC[X] is programmed to (red, green, blue)
294          * Truecolor:
295          *    does not use DAC. Usually 3 are present.
296          *    var->{color}.offset contains start of bitfield
297          *    var->{color}.length contains length of bitfield
298          *    cmap is programmed to (red << red.offset) | (green << green.offset) |
299          *                      (blue << blue.offset) | (transp << transp.offset)
300          *    RAMDAC does not exist
301          */
302 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
303         switch (info->fix.visual) {
304         case FB_VISUAL_TRUECOLOR:
305         case FB_VISUAL_PSEUDOCOLOR:
306                 red = CNVT_TOHW(red, info->var.red.length);
307                 green = CNVT_TOHW(green, info->var.green.length);
308                 blue = CNVT_TOHW(blue, info->var.blue.length);
309                 transp = CNVT_TOHW(transp, info->var.transp.length);
310                 break;
311         case FB_VISUAL_DIRECTCOLOR:
312                 red = CNVT_TOHW(red, 8);        /* expect 8 bit DAC */
313                 green = CNVT_TOHW(green, 8);
314                 blue = CNVT_TOHW(blue, 8);
315                 /* hey, there is bug in transp handling... */
316                 transp = CNVT_TOHW(transp, 8);
317                 break;
318         }
319 #undef CNVT_TOHW
320         /* Truecolor has hardware independent palette */
321         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
322                 u32 v;
323
324                 if (regno >= 16)
325                         return 1;
326
327                 v = (red << info->var.red.offset) |
328                     (green << info->var.green.offset) |
329                     (blue << info->var.blue.offset) |
330                     (transp << info->var.transp.offset);
331                 switch (info->var.bits_per_pixel) {
332                 case 8:
333                         break;
334                 case 16:
335                         ((u32 *) (info->pseudo_palette))[regno] = v;
336                         break;
337                 case 24:
338                 case 32:
339                         ((u32 *) (info->pseudo_palette))[regno] = v;
340                         break;
341                 }
342                 return 0;
343         }
344         return 0;
345 }
346
347     /*
348      *  Pan or Wrap the Display
349      *
350      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
351      */
352
353 static int vfb_pan_display(struct fb_var_screeninfo *var,
354                            struct fb_info *info)
355 {
356         if (var->vmode & FB_VMODE_YWRAP) {
357                 if (var->yoffset < 0
358                     || var->yoffset >= info->var.yres_virtual
359                     || var->xoffset)
360                         return -EINVAL;
361         } else {
362                 if (var->xoffset + var->xres > info->var.xres_virtual ||
363                     var->yoffset + var->yres > info->var.yres_virtual)
364                         return -EINVAL;
365         }
366         info->var.xoffset = var->xoffset;
367         info->var.yoffset = var->yoffset;
368         if (var->vmode & FB_VMODE_YWRAP)
369                 info->var.vmode |= FB_VMODE_YWRAP;
370         else
371                 info->var.vmode &= ~FB_VMODE_YWRAP;
372         return 0;
373 }
374
375     /*
376      *  Most drivers don't need their own mmap function 
377      */
378
379 static int vfb_mmap(struct fb_info *info, struct file *file,
380                     struct vm_area_struct *vma)
381 {
382         return -EINVAL;
383 }
384
385 int __init vfb_setup(char *options)
386 {
387         char *this_opt;
388
389         vfb_enable = 1;
390
391         if (!options || !*options)
392                 return 1;
393
394         while ((this_opt = strsep(&options, ",")) != NULL) {
395                 if (!*this_opt)
396                         continue;
397                 if (!strncmp(this_opt, "disable", 7))
398                         vfb_enable = 0;
399         }
400         return 1;
401 }
402
403     /*
404      *  Initialisation
405      */
406
407 int __init vfb_init(void)
408 {
409         int retval;
410
411         if (!vfb_enable)
412                 return -ENXIO;
413
414         /*
415          * For real video cards we use ioremap.
416          */
417         if (!(videomemory = vmalloc(videomemorysize)))
418                 return -ENOMEM;
419
420         /*
421          * VFB must clear memory to prevent kernel info
422          * leakage into userspace
423          * VGA-based drivers MUST NOT clear memory if
424          * they want to be able to take over vgacon
425          */
426         memset(videomemory, 0, videomemorysize);
427
428         fb_info.screen_base = videomemory;
429         fb_info.fbops = &vfb_ops;
430
431         retval = fb_find_mode(&fb_info.var, &fb_info, NULL,
432                               NULL, 0, NULL, 8);
433
434         if (!retval || (retval == 4))
435                 fb_info.var = vfb_default;
436         fb_info.fix = vfb_fix;
437         fb_info.pseudo_palette = &vfb_pseudo_palette;
438         fb_info.flags = FBINFO_FLAG_DEFAULT;
439
440         fb_alloc_cmap(&fb_info.cmap, 256, 0);
441
442         if (register_framebuffer(&fb_info) < 0) {
443                 vfree(videomemory);
444                 return -EINVAL;
445         }
446
447         printk(KERN_INFO
448                "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
449                fb_info.node, videomemorysize >> 10);
450         return 0;
451 }
452
453 #ifdef MODULE
454
455 static void __exit vfb_cleanup(void)
456 {
457         unregister_framebuffer(&fb_info);
458         vfree(videomemory);
459 }
460
461 module_init(vfb_init);
462 module_exit(vfb_cleanup);
463
464 MODULE_LICENSE("GPL");
465 #endif                          /* MODULE */