vfb.c 15 KB

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