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/slab.h>
 
  19 #include <linux/vmalloc.h>
 
  20 #include <linux/delay.h>
 
  21 #include <linux/interrupt.h>
 
  22 #include <linux/platform_device.h>
 
  24 #include <asm/uaccess.h>
 
  26 #include <linux/init.h>
 
  29      *  RAM we reserve for the frame buffer. This defines the maximum screen
 
  32      *  The default can be overridden if the driver is compiled as a module
 
  35 #define VIDEOMEMSIZE    (1*1024*1024)   /* 1 MB */
 
  37 static void *videomemory;
 
  38 static u_long videomemorysize = VIDEOMEMSIZE;
 
  39 module_param(videomemorysize, ulong, 0);
 
  41 static struct fb_var_screeninfo vfb_default __initdata = {
 
  50         .activate =     FB_ACTIVATE_TEST,
 
  60         .vmode =        FB_VMODE_NONINTERLACED,
 
  63 static struct fb_fix_screeninfo vfb_fix __initdata = {
 
  65         .type =         FB_TYPE_PACKED_PIXELS,
 
  66         .visual =       FB_VISUAL_PSEUDOCOLOR,
 
  70         .accel =        FB_ACCEL_NONE,
 
  73 static int vfb_enable __initdata = 0;   /* disabled by default */
 
  74 module_param(vfb_enable, bool, 0);
 
  76 static int vfb_check_var(struct fb_var_screeninfo *var,
 
  77                          struct fb_info *info);
 
  78 static int vfb_set_par(struct fb_info *info);
 
  79 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 
  80                          u_int transp, struct fb_info *info);
 
  81 static int vfb_pan_display(struct fb_var_screeninfo *var,
 
  82                            struct fb_info *info);
 
  83 static int vfb_mmap(struct fb_info *info,
 
  84                     struct vm_area_struct *vma);
 
  86 static struct fb_ops vfb_ops = {
 
  87         .fb_check_var   = vfb_check_var,
 
  88         .fb_set_par     = vfb_set_par,
 
  89         .fb_setcolreg   = vfb_setcolreg,
 
  90         .fb_pan_display = vfb_pan_display,
 
  91         .fb_fillrect    = cfb_fillrect,
 
  92         .fb_copyarea    = cfb_copyarea,
 
  93         .fb_imageblit   = cfb_imageblit,
 
 101 static u_long get_line_length(int xres_virtual, int bpp)
 
 105         length = xres_virtual * bpp;
 
 106         length = (length + 31) & ~31;
 
 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. 
 
 119 static int vfb_check_var(struct fb_var_screeninfo *var,
 
 120                          struct fb_info *info)
 
 125          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
 
 126          *  as FB_VMODE_SMOOTH_XPAN is only used internally
 
 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;
 
 136          *  Some very basic checks
 
 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;
 
 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;
 
 168             get_line_length(var->xres_virtual, var->bits_per_pixel);
 
 169         if (line_length * var->yres_virtual > videomemorysize)
 
 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.
 
 177         switch (var->bits_per_pixel) {
 
 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;
 
 189         case 16:                /* RGBA 5551 */
 
 190                 if (var->transp.length) {
 
 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 */
 
 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;
 
 210         case 24:                /* RGB 888 */
 
 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;
 
 220         case 32:                /* RGBA 8888 */
 
 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;
 
 231         var->red.msb_right = 0;
 
 232         var->green.msb_right = 0;
 
 233         var->blue.msb_right = 0;
 
 234         var->transp.msb_right = 0;
 
 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. 
 
 243 static int vfb_set_par(struct fb_info *info)
 
 245         info->fix.line_length = get_line_length(info->var.xres_virtual,
 
 246                                                 info->var.bits_per_pixel);
 
 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.
 
 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)
 
 259         if (regno >= 256)       /* no. of hw registers */
 
 262          * Program hardware... do anything you want with transp
 
 265         /* grayscale works only partially under directcolor */
 
 266         if (info->var.grayscale) {
 
 267                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
 
 269                     (red * 77 + green * 151 + blue * 28) >> 8;
 
 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)
 
 280          *    uses offset = 0 && length = RAMDAC register width.
 
 281          *    var->{color}.offset is 0
 
 282          *    var->{color}.length contains widht of DAC
 
 284          *    RAMDAC[X] is programmed to (red, green, blue)
 
 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
 
 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);
 
 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);
 
 311         /* Truecolor has hardware independent palette */
 
 312         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
 
 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) {
 
 326                         ((u32 *) (info->pseudo_palette))[regno] = v;
 
 330                         ((u32 *) (info->pseudo_palette))[regno] = v;
 
 339      *  Pan or Wrap the Display
 
 341      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
 
 344 static int vfb_pan_display(struct fb_var_screeninfo *var,
 
 345                            struct fb_info *info)
 
 347         if (var->vmode & FB_VMODE_YWRAP) {
 
 349                     || var->yoffset >= info->var.yres_virtual
 
 353                 if (var->xoffset + var->xres > info->var.xres_virtual ||
 
 354                     var->yoffset + var->yres > info->var.yres_virtual)
 
 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;
 
 362                 info->var.vmode &= ~FB_VMODE_YWRAP;
 
 367      *  Most drivers don't need their own mmap function 
 
 370 static int vfb_mmap(struct fb_info *info,
 
 371                     struct vm_area_struct *vma)
 
 377 static int __init vfb_setup(char *options)
 
 383         if (!options || !*options)
 
 386         while ((this_opt = strsep(&options, ",")) != NULL) {
 
 389                 if (!strncmp(this_opt, "disable", 7))
 
 400 static int __init vfb_probe(struct platform_device *dev)
 
 402         struct fb_info *info;
 
 403         int retval = -ENOMEM;
 
 406          * For real video cards we use ioremap.
 
 408         if (!(videomemory = vmalloc(videomemorysize)))
 
 412          * VFB must clear memory to prevent kernel info
 
 413          * leakage into userspace
 
 414          * VGA-based drivers MUST NOT clear memory if
 
 415          * they want to be able to take over vgacon
 
 417         memset(videomemory, 0, videomemorysize);
 
 419         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
 
 423         info->screen_base = (char __iomem *)videomemory;
 
 424         info->fbops = &vfb_ops;
 
 426         retval = fb_find_mode(&info->var, info, NULL,
 
 429         if (!retval || (retval == 4))
 
 430                 info->var = vfb_default;
 
 432         info->pseudo_palette = info->par;
 
 434         info->flags = FBINFO_FLAG_DEFAULT;
 
 436         retval = fb_alloc_cmap(&info->cmap, 256, 0);
 
 440         retval = register_framebuffer(info);
 
 443         platform_set_drvdata(dev, info);
 
 446                "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
 
 447                info->node, videomemorysize >> 10);
 
 450         fb_dealloc_cmap(&info->cmap);
 
 452         framebuffer_release(info);
 
 458 static int vfb_remove(struct platform_device *dev)
 
 460         struct fb_info *info = platform_get_drvdata(dev);
 
 463                 unregister_framebuffer(info);
 
 465                 framebuffer_release(info);
 
 470 static struct platform_driver vfb_driver = {
 
 472         .remove = vfb_remove,
 
 478 static struct platform_device *vfb_device;
 
 480 static int __init vfb_init(void)
 
 487         if (fb_get_options("vfb", &option))
 
 495         ret = platform_driver_register(&vfb_driver);
 
 498                 vfb_device = platform_device_alloc("vfb", 0);
 
 501                         ret = platform_device_add(vfb_device);
 
 506                         platform_device_put(vfb_device);
 
 507                         platform_driver_unregister(&vfb_driver);
 
 514 module_init(vfb_init);
 
 517 static void __exit vfb_exit(void)
 
 519         platform_device_unregister(vfb_device);
 
 520         platform_driver_unregister(&vfb_driver);
 
 523 module_exit(vfb_exit);
 
 525 MODULE_LICENSE("GPL");