vfb.c 13 KB

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