2 * linux/drivers/video/vfb.c -- Virtual frame buffer device
4 * Copyright (C) 1997 Geert Uytterhoeven
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
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.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>
23 #include <linux/init.h>
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>
36 * RAM we reserve for the frame buffer. This defines the maximum screen
39 * The default can be overridden if the driver is compiled as a module
42 #define VIDEOMEMSIZE (1*1024*1024) /* 1 MB */
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];
51 #ifdef FBCON_HAS_CFB16
54 #ifdef FBCON_HAS_CFB24
57 #ifdef FBCON_HAS_CFB32
61 static char vfb_name[16] = "Virtual FB";
63 static struct fb_var_screeninfo vfb_default = {
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
71 static int vfb_enable = 0; /* disabled by default */
75 * Interface used by the world
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);
95 * Interface to the low level console driver
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);
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);
119 static struct fb_ops vfb_ops = {
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,
130 * Get the Fixed Part of the Display
133 static int vfb_get_fix(struct fb_fix_screeninfo *fix, int con,
134 struct fb_info *info)
136 struct fb_var_screeninfo *var;
141 var = &fb_display[con].var;
142 vfb_encode_fix(fix, var);
148 * Get the User Defined Part of the Display
151 static int vfb_get_var(struct fb_var_screeninfo *var, int con,
152 struct fb_info *info)
157 *var = fb_display[con].var;
158 set_color_bitfields(var);
164 * Set the User Defined Part of the Display
167 static int vfb_set_var(struct fb_var_screeninfo *var, int con,
168 struct fb_info *info)
170 int err, activate = var->activate;
171 int oldxres, oldyres, oldvxres, oldvyres, oldbpp;
174 struct display *display;
176 display = &fb_display[con];
178 display = &disp; /* used during initialization */
181 * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
182 * as FB_VMODE_SMOOTH_XPAN is only used internally
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;
192 * Some very basic checks
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;
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;
221 line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
222 if (line_length*var->yres_virtual > videomemorysize)
225 set_color_bitfields(var);
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;
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;
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) {
252 display->dispsw = &fbcon_mfb;
255 #ifdef FBCON_HAS_CFB2
257 display->dispsw = &fbcon_cfb2;
260 #ifdef FBCON_HAS_CFB4
262 display->dispsw = &fbcon_cfb4;
265 #ifdef FBCON_HAS_CFB8
267 display->dispsw = &fbcon_cfb8;
270 #ifdef FBCON_HAS_CFB16
272 display->dispsw = &fbcon_cfb16;
273 display->dispsw_data = fbcon_cmap.cfb16;
276 #ifdef FBCON_HAS_CFB24
278 display->dispsw = &fbcon_cfb24;
279 display->dispsw_data = fbcon_cmap.cfb24;
282 #ifdef FBCON_HAS_CFB32
284 display->dispsw = &fbcon_cfb32;
285 display->dispsw_data = fbcon_cmap.cfb32;
289 display->dispsw = &fbcon_dummy;
292 if (fb_info.changevar)
293 (*fb_info.changevar)(con);
295 if (oldbpp != var->bits_per_pixel) {
296 if ((err = fb_alloc_cmap(&display->cmap, 0, 0)))
298 do_install_cmap(con, info);
306 * Pan or Wrap the Display
308 * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
311 static int vfb_pan_display(struct fb_var_screeninfo *var, int con,
312 struct fb_info *info)
314 if (var->vmode & FB_VMODE_YWRAP) {
315 if (var->yoffset < 0 ||
316 var->yoffset >= fb_display[con].var.yres_virtual ||
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)
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;
331 fb_display[con].var.vmode &= ~FB_VMODE_YWRAP;
339 static int vfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
340 struct fb_info *info)
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);
347 fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
356 static int vfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
357 struct fb_info *info)
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)))
366 if (con == currcon) /* current console? */
367 return fb_set_cmap(cmap, kspc, vfb_setcolreg, info);
369 fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
374 int __init vfb_setup(char *options)
378 fb_info.fontname[0] = '\0';
382 if (!options || !*options)
385 while ((this_opt = strsep(&options, ",")) != NULL) {
386 if (!strncmp(this_opt, "font:", 5))
387 strcpy(fb_info.fontname, this_opt+5);
397 int __init vfb_init(void)
402 if (!(videomemory = (u_long)vmalloc(videomemorysize)))
405 strcpy(fb_info.modename, vfb_name);
406 fb_info.changevar = NULL;
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;
415 vfb_set_var(&vfb_default, -1, &fb_info);
417 if (register_framebuffer(&fb_info) < 0) {
418 vfree((void *)videomemory);
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);
428 static int vfbcon_switch(int con, struct fb_info *info)
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);
435 /* Install new colormap */
436 do_install_cmap(con, info);
441 * Update the `var' structure (called by fbcon.c)
444 static int vfbcon_updatevar(int con, struct fb_info *info)
454 static void vfbcon_blank(int blank, struct fb_info *info)
459 static u_long get_line_length(int xres_virtual, int bpp)
463 length = xres_virtual*bpp;
464 length = (length+31)&-32;
469 static void vfb_encode_fix(struct fb_fix_screeninfo *fix,
470 struct fb_var_screeninfo *var)
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;
478 switch (var->bits_per_pixel) {
480 fix->visual = FB_VISUAL_MONO01;
485 fix->visual = FB_VISUAL_PSEUDOCOLOR;
490 fix->visual = FB_VISUAL_TRUECOLOR;
496 fix->line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
499 static void set_color_bitfields(struct fb_var_screeninfo *var)
501 switch (var->bits_per_pixel) {
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;
513 case 16: /* RGB 565 */
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;
523 case 24: /* RGB 888 */
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;
533 case 32: /* RGBA 8888 */
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;
544 var->red.msb_right = 0;
545 var->green.msb_right = 0;
546 var->blue.msb_right = 0;
547 var->transp.msb_right = 0;
552 * Read a single color register and split it into
553 * colors/transparent. Return != 0 for invalid regno.
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)
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;
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.
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)
583 palette[regno].red = red;
584 palette[regno].green = green;
585 palette[regno].blue = blue;
590 static void do_install_cmap(int con, struct fb_info *info)
594 if (fb_display[con].cmap.len)
595 fb_set_cmap(&fb_display[con].cmap, 1, vfb_setcolreg, info);
597 fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel), 1,
598 vfb_setcolreg, info);
603 MODULE_LICENSE("GPL");
605 int init_module(void)
610 void cleanup_module(void)
612 unregister_framebuffer(&fb_info);
613 vfree((void *)videomemory);