vfb.c 12 KB

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