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