vfb.c 13 KB

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