vfb.c 13 KB

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