setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / video / vfb.c
1 /*
2  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
3  *
4  *      Copyright (C) 1997 Geert Uytterhoeven
5  *
6  *  This file is subject to the terms and conditions of the GNU General Public
7  *  License. See the file COPYING in the main directory of this archive for
8  *  more details.
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/tty.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <asm/uaccess.h>
22 #include <linux/fb.h>
23 #include <linux/init.h>
24
25 #include <video/fbcon.h>
26 #include <video/fbcon-mfb.h>
27 #include <video/fbcon-cfb2.h>
28 #include <video/fbcon-cfb4.h>
29 #include <video/fbcon-cfb8.h>
30 #include <video/fbcon-cfb16.h>
31 #include <video/fbcon-cfb24.h>
32 #include <video/fbcon-cfb32.h>
33
34
35     /*
36      *  RAM we reserve for the frame buffer. This defines the maximum screen
37      *  size
38      *
39      *  The default can be overridden if the driver is compiled as a module
40      */
41
42 #define VIDEOMEMSIZE    (1*1024*1024)   /* 1 MB */
43
44 static u_long videomemory, videomemorysize = VIDEOMEMSIZE;
45 MODULE_PARM(videomemorysize, "l");
46 static int currcon = 0;
47 static struct display disp;
48 static struct fb_info fb_info;
49 static struct { u_char red, green, blue, pad; } palette[256];
50 static union {
51 #ifdef FBCON_HAS_CFB16
52     u16 cfb16[16];
53 #endif
54 #ifdef FBCON_HAS_CFB24
55     u32 cfb24[16];
56 #endif
57 #ifdef FBCON_HAS_CFB32
58     u32 cfb32[16];
59 #endif
60 } fbcon_cmap;
61 static char vfb_name[16] = "Virtual FB";
62
63 static struct fb_var_screeninfo vfb_default = {
64     /* 640x480, 8 bpp */
65     640, 480, 640, 480, 0, 0, 8, 0,
66     {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
67     0, 0, -1, -1, 0, 20000, 64, 64, 32, 32, 64, 2,
68     0, FB_VMODE_NONINTERLACED
69 };
70
71 static int vfb_enable = 0;      /* disabled by default */
72
73
74     /*
75      *  Interface used by the world
76      */
77
78 int vfb_setup(char*);
79
80 static int vfb_get_fix(struct fb_fix_screeninfo *fix, int con,
81                        struct fb_info *info);
82 static int vfb_get_var(struct fb_var_screeninfo *var, int con,
83                        struct fb_info *info);
84 static int vfb_set_var(struct fb_var_screeninfo *var, int con,
85                        struct fb_info *info);
86 static int vfb_pan_display(struct fb_var_screeninfo *var, int con,
87                            struct fb_info *info);
88 static int vfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
89                         struct fb_info *info);
90 static int vfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
91                         struct fb_info *info);
92
93
94     /*
95      *  Interface to the low level console driver
96      */
97
98 int vfb_init(void);
99 static int vfbcon_switch(int con, struct fb_info *info);
100 static int vfbcon_updatevar(int con, struct fb_info *info);
101 static void vfbcon_blank(int blank, struct fb_info *info);
102
103
104     /*
105      *  Internal routines
106      */
107
108 static u_long get_line_length(int xres_virtual, int bpp);
109 static void vfb_encode_fix(struct fb_fix_screeninfo *fix,
110                            struct fb_var_screeninfo *var);
111 static void set_color_bitfields(struct fb_var_screeninfo *var);
112 static int vfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
113                          u_int *transp, struct fb_info *info);
114 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
115                          u_int transp, struct fb_info *info);
116 static void do_install_cmap(int con, struct fb_info *info);
117
118
119 static struct fb_ops vfb_ops = {
120         owner:          THIS_MODULE,
121         fb_get_fix:     vfb_get_fix,
122         fb_get_var:     vfb_get_var,
123         fb_set_var:     vfb_set_var,
124         fb_get_cmap:    vfb_get_cmap,
125         fb_set_cmap:    vfb_set_cmap,
126         fb_pan_display: vfb_pan_display,
127 };
128
129     /*
130      *  Get the Fixed Part of the Display
131      */
132
133 static int vfb_get_fix(struct fb_fix_screeninfo *fix, int con,
134                        struct fb_info *info)
135 {
136     struct fb_var_screeninfo *var;
137
138     if (con == -1)
139         var = &vfb_default;
140     else
141         var = &fb_display[con].var;
142     vfb_encode_fix(fix, var);
143     return 0;
144 }
145
146
147     /*
148      *  Get the User Defined Part of the Display
149      */
150
151 static int vfb_get_var(struct fb_var_screeninfo *var, int con,
152                        struct fb_info *info)
153 {
154     if (con == -1)
155         *var = vfb_default;
156     else
157         *var = fb_display[con].var;
158     set_color_bitfields(var);
159     return 0;
160 }
161
162
163     /*
164      *  Set the User Defined Part of the Display
165      */
166
167 static int vfb_set_var(struct fb_var_screeninfo *var, int con,
168                        struct fb_info *info)
169 {
170     int err, activate = var->activate;
171     int oldxres, oldyres, oldvxres, oldvyres, oldbpp;
172     u_long line_length;
173
174     struct display *display;
175     if (con >= 0)
176         display = &fb_display[con];
177     else
178         display = &disp;        /* used during initialization */
179
180     /*
181      *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
182      *  as FB_VMODE_SMOOTH_XPAN is only used internally
183      */
184
185     if (var->vmode & FB_VMODE_CONUPDATE) {
186         var->vmode |= FB_VMODE_YWRAP;
187         var->xoffset = display->var.xoffset;
188         var->yoffset = display->var.yoffset;
189     }
190
191     /*
192      *  Some very basic checks
193      */
194     if (!var->xres)
195         var->xres = 1;
196     if (!var->yres)
197         var->yres = 1;
198     if (var->xres > var->xres_virtual)
199         var->xres_virtual = var->xres;
200     if (var->yres > var->yres_virtual)
201         var->yres_virtual = var->yres;
202     if (var->bits_per_pixel <= 1)
203         var->bits_per_pixel = 1;
204     else if (var->bits_per_pixel <= 8)
205         var->bits_per_pixel = 8;
206     else if (var->bits_per_pixel <= 16)
207         var->bits_per_pixel = 16;
208 #if 0
209     /* fbcon doesn't support this (yet) */
210     else if (var->bits_per_pixel <= 24)
211         var->bits_per_pixel = 24;
212     else if (var->bits_per_pixel <= 32)
213         var->bits_per_pixel = 32;
214 #endif
215     else
216         return -EINVAL;
217
218     /*
219      *  Memory limit
220      */
221     line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
222     if (line_length*var->yres_virtual > videomemorysize)
223         return -ENOMEM;
224
225     set_color_bitfields(var);
226
227     if ((activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
228         oldxres = display->var.xres;
229         oldyres = display->var.yres;
230         oldvxres = display->var.xres_virtual;
231         oldvyres = display->var.yres_virtual;
232         oldbpp = display->var.bits_per_pixel;
233         display->var = *var;
234         if (oldxres != var->xres || oldyres != var->yres ||
235             oldvxres != var->xres_virtual || oldvyres != var->yres_virtual ||
236             oldbpp != var->bits_per_pixel) {
237             struct fb_fix_screeninfo fix;
238
239             vfb_encode_fix(&fix, var);
240             display->screen_base = (char *)videomemory;
241             display->visual = fix.visual;
242             display->type = fix.type;
243             display->type_aux = fix.type_aux;
244             display->ypanstep = fix.ypanstep;
245             display->ywrapstep = fix.ywrapstep;
246             display->line_length = fix.line_length;
247             display->can_soft_blank = 1;
248             display->inverse = 0;
249             switch (var->bits_per_pixel) {
250 #ifdef FBCON_HAS_MFB
251                 case 1:
252                     display->dispsw = &fbcon_mfb;
253                     break;
254 #endif
255 #ifdef FBCON_HAS_CFB2
256                 case 2:
257                     display->dispsw = &fbcon_cfb2;
258                     break;
259 #endif
260 #ifdef FBCON_HAS_CFB4
261                 case 4:
262                     display->dispsw = &fbcon_cfb4;
263                     break;
264 #endif
265 #ifdef FBCON_HAS_CFB8
266                 case 8:
267                     display->dispsw = &fbcon_cfb8;
268                     break;
269 #endif
270 #ifdef FBCON_HAS_CFB16
271                 case 16:
272                     display->dispsw = &fbcon_cfb16;
273                     display->dispsw_data = fbcon_cmap.cfb16;
274                     break;
275 #endif
276 #ifdef FBCON_HAS_CFB24
277                 case 24:
278                     display->dispsw = &fbcon_cfb24;
279                     display->dispsw_data = fbcon_cmap.cfb24;
280                     break;
281 #endif
282 #ifdef FBCON_HAS_CFB32
283                 case 32:
284                     display->dispsw = &fbcon_cfb32;
285                     display->dispsw_data = fbcon_cmap.cfb32;
286                     break;
287 #endif
288                 default:
289                     display->dispsw = &fbcon_dummy;
290                     break;
291             }
292             if (fb_info.changevar)
293                 (*fb_info.changevar)(con);
294         }
295         if (oldbpp != var->bits_per_pixel) {
296             if ((err = fb_alloc_cmap(&display->cmap, 0, 0)))
297                 return err;
298             do_install_cmap(con, info);
299         }
300     }
301     return 0;
302 }
303
304
305     /*
306      *  Pan or Wrap the Display
307      *
308      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
309      */
310
311 static int vfb_pan_display(struct fb_var_screeninfo *var, int con,
312                            struct fb_info *info)
313 {
314     if (var->vmode & FB_VMODE_YWRAP) {
315         if (var->yoffset < 0 ||
316             var->yoffset >= fb_display[con].var.yres_virtual ||
317             var->xoffset)
318             return -EINVAL;
319     } else {
320         if (var->xoffset+fb_display[con].var.xres >
321             fb_display[con].var.xres_virtual ||
322             var->yoffset+fb_display[con].var.yres >
323             fb_display[con].var.yres_virtual)
324             return -EINVAL;
325     }
326     fb_display[con].var.xoffset = var->xoffset;
327     fb_display[con].var.yoffset = var->yoffset;
328     if (var->vmode & FB_VMODE_YWRAP)
329         fb_display[con].var.vmode |= FB_VMODE_YWRAP;
330     else
331         fb_display[con].var.vmode &= ~FB_VMODE_YWRAP;
332     return 0;
333 }
334
335     /*
336      *  Get the Colormap
337      */
338
339 static int vfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
340                         struct fb_info *info)
341 {
342     if (con == currcon) /* current console? */
343         return fb_get_cmap(cmap, kspc, vfb_getcolreg, info);
344     else if (fb_display[con].cmap.len) /* non default colormap? */
345         fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
346     else
347         fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
348                      cmap, kspc ? 0 : 2);
349     return 0;
350 }
351
352     /*
353      *  Set the Colormap
354      */
355
356 static int vfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
357                         struct fb_info *info)
358 {
359     int err;
360
361     if (!fb_display[con].cmap.len) {    /* no colormap allocated? */
362         if ((err = fb_alloc_cmap(&fb_display[con].cmap,
363                               1<<fb_display[con].var.bits_per_pixel, 0)))
364             return err;
365     }
366     if (con == currcon)                 /* current console? */
367         return fb_set_cmap(cmap, kspc, vfb_setcolreg, info);
368     else
369         fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
370     return 0;
371 }
372
373
374 int __init vfb_setup(char *options)
375 {
376     char *this_opt;
377
378     fb_info.fontname[0] = '\0';
379
380     vfb_enable = 1;
381
382     if (!options || !*options)
383         return 0;
384
385     while ((this_opt = strsep(&options, ",")) != NULL) {
386         if (!strncmp(this_opt, "font:", 5))
387             strcpy(fb_info.fontname, this_opt+5);
388     }
389     return 0;
390 }
391
392
393     /*
394      *  Initialisation
395      */
396
397 int __init vfb_init(void)
398 {
399     if (!vfb_enable)
400         return -ENXIO;
401
402     if (!(videomemory = (u_long)vmalloc(videomemorysize)))
403         return -ENOMEM;
404
405     strcpy(fb_info.modename, vfb_name);
406     fb_info.changevar = NULL;
407     fb_info.node = -1;
408     fb_info.fbops = &vfb_ops;
409     fb_info.disp = &disp;
410     fb_info.switch_con = &vfbcon_switch;
411     fb_info.updatevar = &vfbcon_updatevar;
412     fb_info.blank = &vfbcon_blank;
413     fb_info.flags = FBINFO_FLAG_DEFAULT;
414
415     vfb_set_var(&vfb_default, -1, &fb_info);
416
417     if (register_framebuffer(&fb_info) < 0) {
418         vfree((void *)videomemory);
419         return -EINVAL;
420     }
421
422     printk(KERN_INFO "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
423            GET_FB_IDX(fb_info.node), videomemorysize>>10);
424     return 0;
425 }
426
427
428 static int vfbcon_switch(int con, struct fb_info *info)
429 {
430     /* Do we have to save the colormap? */
431     if (fb_display[currcon].cmap.len)
432         fb_get_cmap(&fb_display[currcon].cmap, 1, vfb_getcolreg, info);
433
434     currcon = con;
435     /* Install new colormap */
436     do_install_cmap(con, info);
437     return 0;
438 }
439
440     /*
441      *  Update the `var' structure (called by fbcon.c)
442      */
443
444 static int vfbcon_updatevar(int con, struct fb_info *info)
445 {
446     /* Nothing */
447     return 0;
448 }
449
450     /*
451      *  Blank the display.
452      */
453
454 static void vfbcon_blank(int blank, struct fb_info *info)
455 {
456     /* Nothing */
457 }
458
459 static u_long get_line_length(int xres_virtual, int bpp)
460 {
461     u_long length;
462     
463     length = xres_virtual*bpp;
464     length = (length+31)&-32;
465     length >>= 3;
466     return(length);
467 }
468
469 static void vfb_encode_fix(struct fb_fix_screeninfo *fix,
470                            struct fb_var_screeninfo *var)
471 {
472     memset(fix, 0, sizeof(struct fb_fix_screeninfo));
473     strcpy(fix->id, vfb_name);
474     fix->smem_start = videomemory;
475     fix->smem_len = videomemorysize;
476     fix->type = FB_TYPE_PACKED_PIXELS;
477     fix->type_aux = 0;
478     switch (var->bits_per_pixel) {
479         case 1:
480             fix->visual = FB_VISUAL_MONO01;
481             break;
482         case 2:
483         case 4:
484         case 8:
485             fix->visual = FB_VISUAL_PSEUDOCOLOR;
486             break;
487         case 16:
488         case 24:
489         case 32:
490             fix->visual = FB_VISUAL_TRUECOLOR;
491             break;
492     }
493     fix->ywrapstep = 1;
494     fix->xpanstep = 1;
495     fix->ypanstep = 1;
496     fix->line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
497 }
498
499 static void set_color_bitfields(struct fb_var_screeninfo *var)
500 {
501     switch (var->bits_per_pixel) {
502         case 1:
503         case 8:
504             var->red.offset = 0;
505             var->red.length = 8;
506             var->green.offset = 0;
507             var->green.length = 8;
508             var->blue.offset = 0;
509             var->blue.length = 8;
510             var->transp.offset = 0;
511             var->transp.length = 0;
512             break;
513         case 16:        /* RGB 565 */
514             var->red.offset = 0;
515             var->red.length = 5;
516             var->green.offset = 5;
517             var->green.length = 6;
518             var->blue.offset = 11;
519             var->blue.length = 5;
520             var->transp.offset = 0;
521             var->transp.length = 0;
522             break;
523         case 24:        /* RGB 888 */
524             var->red.offset = 0;
525             var->red.length = 8;
526             var->green.offset = 8;
527             var->green.length = 8;
528             var->blue.offset = 16;
529             var->blue.length = 8;
530             var->transp.offset = 0;
531             var->transp.length = 0;
532             break;
533         case 32:        /* RGBA 8888 */
534             var->red.offset = 0;
535             var->red.length = 8;
536             var->green.offset = 8;
537             var->green.length = 8;
538             var->blue.offset = 16;
539             var->blue.length = 8;
540             var->transp.offset = 24;
541             var->transp.length = 8;
542             break;
543     }
544     var->red.msb_right = 0;
545     var->green.msb_right = 0;
546     var->blue.msb_right = 0;
547     var->transp.msb_right = 0;
548 }
549
550
551     /*
552      *  Read a single color register and split it into
553      *  colors/transparent. Return != 0 for invalid regno.
554      */
555
556 static int vfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
557                          u_int *transp, struct fb_info *info)
558 {
559     if (regno > 255)
560         return 1;
561     *red = (palette[regno].red<<8) | palette[regno].red;
562     *green = (palette[regno].green<<8) | palette[regno].green;
563     *blue = (palette[regno].blue<<8) | palette[regno].blue;
564     *transp = 0;
565     return 0;
566 }
567
568
569     /*
570      *  Set a single color register. The values supplied are already
571      *  rounded down to the hardware's capabilities (according to the
572      *  entries in the var structure). Return != 0 for invalid regno.
573      */
574
575 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
576                          u_int transp, struct fb_info *info)
577 {
578     if (regno > 255)
579         return 1;
580     red >>= 8;
581     green >>= 8;
582     blue >>= 8;
583     palette[regno].red = red;
584     palette[regno].green = green;
585     palette[regno].blue = blue;
586     return 0;
587 }
588
589
590 static void do_install_cmap(int con, struct fb_info *info)
591 {
592     if (con != currcon)
593         return;
594     if (fb_display[con].cmap.len)
595         fb_set_cmap(&fb_display[con].cmap, 1, vfb_setcolreg, info);
596     else
597         fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel), 1,
598                     vfb_setcolreg, info);
599 }
600
601
602 #ifdef MODULE
603 MODULE_LICENSE("GPL");
604
605 int init_module(void)
606 {
607     return vfb_init();
608 }
609
610 void cleanup_module(void)
611 {
612     unregister_framebuffer(&fb_info);
613     vfree((void *)videomemory);
614 }
615
616 #endif /* MODULE */