vfb.c 14 KB

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