vfb.c 14 KB

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