vivid-osd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * vivid-osd.c - osd support for testing overlays.
  3. *
  4. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/font.h>
  26. #include <linux/mutex.h>
  27. #include <linux/videodev2.h>
  28. #include <linux/kthread.h>
  29. #include <linux/freezer.h>
  30. #include <linux/fb.h>
  31. #include <media/videobuf2-vmalloc.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-ctrls.h>
  35. #include <media/v4l2-fh.h>
  36. #include <media/v4l2-event.h>
  37. #include <media/v4l2-common.h>
  38. #include "vivid-core.h"
  39. #include "vivid-osd.h"
  40. #define MAX_OSD_WIDTH 720
  41. #define MAX_OSD_HEIGHT 576
  42. /*
  43. * Order: white, yellow, cyan, green, magenta, red, blue, black,
  44. * and same again with the alpha bit set (if any)
  45. */
  46. static const u16 rgb555[16] = {
  47. 0x7fff, 0x7fe0, 0x03ff, 0x03e0, 0x7c1f, 0x7c00, 0x001f, 0x0000,
  48. 0xffff, 0xffe0, 0x83ff, 0x83e0, 0xfc1f, 0xfc00, 0x801f, 0x8000
  49. };
  50. static const u16 rgb565[16] = {
  51. 0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000,
  52. 0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000
  53. };
  54. void vivid_clear_fb(struct vivid_dev *dev)
  55. {
  56. void *p = dev->video_vbase;
  57. const u16 *rgb = rgb555;
  58. unsigned x, y;
  59. if (dev->fb_defined.green.length == 6)
  60. rgb = rgb565;
  61. for (y = 0; y < dev->display_height; y++) {
  62. u16 *d = p;
  63. for (x = 0; x < dev->display_width; x++)
  64. d[x] = rgb[(y / 16 + x / 16) % 16];
  65. p += dev->display_byte_stride;
  66. }
  67. }
  68. /* --------------------------------------------------------------------- */
  69. static int vivid_fb_ioctl(struct fb_info *info, unsigned cmd, unsigned long arg)
  70. {
  71. struct vivid_dev *dev = (struct vivid_dev *)info->par;
  72. switch (cmd) {
  73. case FBIOGET_VBLANK: {
  74. struct fb_vblank vblank;
  75. memset(&vblank, 0, sizeof(vblank));
  76. vblank.flags = FB_VBLANK_HAVE_COUNT | FB_VBLANK_HAVE_VCOUNT |
  77. FB_VBLANK_HAVE_VSYNC;
  78. vblank.count = 0;
  79. vblank.vcount = 0;
  80. vblank.hcount = 0;
  81. if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
  82. return -EFAULT;
  83. return 0;
  84. }
  85. default:
  86. dprintk(dev, 1, "Unknown ioctl %08x\n", cmd);
  87. return -EINVAL;
  88. }
  89. return 0;
  90. }
  91. /* Framebuffer device handling */
  92. static int vivid_fb_set_var(struct vivid_dev *dev, struct fb_var_screeninfo *var)
  93. {
  94. dprintk(dev, 1, "vivid_fb_set_var\n");
  95. if (var->bits_per_pixel != 16) {
  96. dprintk(dev, 1, "vivid_fb_set_var - Invalid bpp\n");
  97. return -EINVAL;
  98. }
  99. dev->display_byte_stride = var->xres * dev->bytes_per_pixel;
  100. return 0;
  101. }
  102. static int vivid_fb_get_fix(struct vivid_dev *dev, struct fb_fix_screeninfo *fix)
  103. {
  104. dprintk(dev, 1, "vivid_fb_get_fix\n");
  105. memset(fix, 0, sizeof(struct fb_fix_screeninfo));
  106. strlcpy(fix->id, "vioverlay fb", sizeof(fix->id));
  107. fix->smem_start = dev->video_pbase;
  108. fix->smem_len = dev->video_buffer_size;
  109. fix->type = FB_TYPE_PACKED_PIXELS;
  110. fix->visual = FB_VISUAL_TRUECOLOR;
  111. fix->xpanstep = 1;
  112. fix->ypanstep = 1;
  113. fix->ywrapstep = 0;
  114. fix->line_length = dev->display_byte_stride;
  115. fix->accel = FB_ACCEL_NONE;
  116. return 0;
  117. }
  118. /* Check the requested display mode, returning -EINVAL if we can't
  119. handle it. */
  120. static int _vivid_fb_check_var(struct fb_var_screeninfo *var, struct vivid_dev *dev)
  121. {
  122. dprintk(dev, 1, "vivid_fb_check_var\n");
  123. var->bits_per_pixel = 16;
  124. if (var->green.length == 5) {
  125. var->red.offset = 10;
  126. var->red.length = 5;
  127. var->green.offset = 5;
  128. var->green.length = 5;
  129. var->blue.offset = 0;
  130. var->blue.length = 5;
  131. var->transp.offset = 15;
  132. var->transp.length = 1;
  133. } else {
  134. var->red.offset = 11;
  135. var->red.length = 5;
  136. var->green.offset = 5;
  137. var->green.length = 6;
  138. var->blue.offset = 0;
  139. var->blue.length = 5;
  140. var->transp.offset = 0;
  141. var->transp.length = 0;
  142. }
  143. var->xoffset = var->yoffset = 0;
  144. var->left_margin = var->upper_margin = 0;
  145. var->nonstd = 0;
  146. var->vmode &= ~FB_VMODE_MASK;
  147. var->vmode = FB_VMODE_NONINTERLACED;
  148. /* Dummy values */
  149. var->hsync_len = 24;
  150. var->vsync_len = 2;
  151. var->pixclock = 84316;
  152. var->right_margin = 776;
  153. var->lower_margin = 591;
  154. return 0;
  155. }
  156. static int vivid_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  157. {
  158. struct vivid_dev *dev = (struct vivid_dev *) info->par;
  159. dprintk(dev, 1, "vivid_fb_check_var\n");
  160. return _vivid_fb_check_var(var, dev);
  161. }
  162. static int vivid_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  163. {
  164. return 0;
  165. }
  166. static int vivid_fb_set_par(struct fb_info *info)
  167. {
  168. int rc = 0;
  169. struct vivid_dev *dev = (struct vivid_dev *) info->par;
  170. dprintk(dev, 1, "vivid_fb_set_par\n");
  171. rc = vivid_fb_set_var(dev, &info->var);
  172. vivid_fb_get_fix(dev, &info->fix);
  173. return rc;
  174. }
  175. static int vivid_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  176. unsigned blue, unsigned transp,
  177. struct fb_info *info)
  178. {
  179. u32 color, *palette;
  180. if (regno >= info->cmap.len)
  181. return -EINVAL;
  182. color = ((transp & 0xFF00) << 16) | ((red & 0xFF00) << 8) |
  183. (green & 0xFF00) | ((blue & 0xFF00) >> 8);
  184. if (regno >= 16)
  185. return -EINVAL;
  186. palette = info->pseudo_palette;
  187. if (info->var.bits_per_pixel == 16) {
  188. switch (info->var.green.length) {
  189. case 6:
  190. color = (red & 0xf800) |
  191. ((green & 0xfc00) >> 5) |
  192. ((blue & 0xf800) >> 11);
  193. break;
  194. case 5:
  195. color = ((red & 0xf800) >> 1) |
  196. ((green & 0xf800) >> 6) |
  197. ((blue & 0xf800) >> 11) |
  198. (transp ? 0x8000 : 0);
  199. break;
  200. }
  201. }
  202. palette[regno] = color;
  203. return 0;
  204. }
  205. /* We don't really support blanking. All this does is enable or
  206. disable the OSD. */
  207. static int vivid_fb_blank(int blank_mode, struct fb_info *info)
  208. {
  209. struct vivid_dev *dev = (struct vivid_dev *)info->par;
  210. dprintk(dev, 1, "Set blanking mode : %d\n", blank_mode);
  211. switch (blank_mode) {
  212. case FB_BLANK_UNBLANK:
  213. break;
  214. case FB_BLANK_NORMAL:
  215. case FB_BLANK_HSYNC_SUSPEND:
  216. case FB_BLANK_VSYNC_SUSPEND:
  217. case FB_BLANK_POWERDOWN:
  218. break;
  219. }
  220. return 0;
  221. }
  222. static struct fb_ops vivid_fb_ops = {
  223. .owner = THIS_MODULE,
  224. .fb_check_var = vivid_fb_check_var,
  225. .fb_set_par = vivid_fb_set_par,
  226. .fb_setcolreg = vivid_fb_setcolreg,
  227. .fb_fillrect = cfb_fillrect,
  228. .fb_copyarea = cfb_copyarea,
  229. .fb_imageblit = cfb_imageblit,
  230. .fb_cursor = NULL,
  231. .fb_ioctl = vivid_fb_ioctl,
  232. .fb_pan_display = vivid_fb_pan_display,
  233. .fb_blank = vivid_fb_blank,
  234. };
  235. /* Initialization */
  236. /* Setup our initial video mode */
  237. static int vivid_fb_init_vidmode(struct vivid_dev *dev)
  238. {
  239. struct v4l2_rect start_window;
  240. /* Color mode */
  241. dev->bits_per_pixel = 16;
  242. dev->bytes_per_pixel = dev->bits_per_pixel / 8;
  243. start_window.width = MAX_OSD_WIDTH;
  244. start_window.left = 0;
  245. dev->display_byte_stride = start_window.width * dev->bytes_per_pixel;
  246. /* Vertical size & position */
  247. start_window.height = MAX_OSD_HEIGHT;
  248. start_window.top = 0;
  249. dev->display_width = start_window.width;
  250. dev->display_height = start_window.height;
  251. /* Generate a valid fb_var_screeninfo */
  252. dev->fb_defined.xres = dev->display_width;
  253. dev->fb_defined.yres = dev->display_height;
  254. dev->fb_defined.xres_virtual = dev->display_width;
  255. dev->fb_defined.yres_virtual = dev->display_height;
  256. dev->fb_defined.bits_per_pixel = dev->bits_per_pixel;
  257. dev->fb_defined.vmode = FB_VMODE_NONINTERLACED;
  258. dev->fb_defined.left_margin = start_window.left + 1;
  259. dev->fb_defined.upper_margin = start_window.top + 1;
  260. dev->fb_defined.accel_flags = FB_ACCEL_NONE;
  261. dev->fb_defined.nonstd = 0;
  262. /* set default to 1:5:5:5 */
  263. dev->fb_defined.green.length = 5;
  264. /* We've filled in the most data, let the usual mode check
  265. routine fill in the rest. */
  266. _vivid_fb_check_var(&dev->fb_defined, dev);
  267. /* Generate valid fb_fix_screeninfo */
  268. vivid_fb_get_fix(dev, &dev->fb_fix);
  269. /* Generate valid fb_info */
  270. dev->fb_info.node = -1;
  271. dev->fb_info.flags = FBINFO_FLAG_DEFAULT;
  272. dev->fb_info.fbops = &vivid_fb_ops;
  273. dev->fb_info.par = dev;
  274. dev->fb_info.var = dev->fb_defined;
  275. dev->fb_info.fix = dev->fb_fix;
  276. dev->fb_info.screen_base = (u8 __iomem *)dev->video_vbase;
  277. dev->fb_info.fbops = &vivid_fb_ops;
  278. /* Supply some monitor specs. Bogus values will do for now */
  279. dev->fb_info.monspecs.hfmin = 8000;
  280. dev->fb_info.monspecs.hfmax = 70000;
  281. dev->fb_info.monspecs.vfmin = 10;
  282. dev->fb_info.monspecs.vfmax = 100;
  283. /* Allocate color map */
  284. if (fb_alloc_cmap(&dev->fb_info.cmap, 256, 1)) {
  285. pr_err("abort, unable to alloc cmap\n");
  286. return -ENOMEM;
  287. }
  288. /* Allocate the pseudo palette */
  289. dev->fb_info.pseudo_palette = kmalloc_array(16, sizeof(u32), GFP_KERNEL);
  290. return dev->fb_info.pseudo_palette ? 0 : -ENOMEM;
  291. }
  292. /* Release any memory we've grabbed */
  293. void vivid_fb_release_buffers(struct vivid_dev *dev)
  294. {
  295. if (dev->video_vbase == NULL)
  296. return;
  297. /* Release cmap */
  298. if (dev->fb_info.cmap.len)
  299. fb_dealloc_cmap(&dev->fb_info.cmap);
  300. /* Release pseudo palette */
  301. kfree(dev->fb_info.pseudo_palette);
  302. kfree((void *)dev->video_vbase);
  303. }
  304. /* Initialize the specified card */
  305. int vivid_fb_init(struct vivid_dev *dev)
  306. {
  307. int ret;
  308. dev->video_buffer_size = MAX_OSD_HEIGHT * MAX_OSD_WIDTH * 2;
  309. dev->video_vbase = kzalloc(dev->video_buffer_size, GFP_KERNEL | GFP_DMA32);
  310. if (dev->video_vbase == NULL)
  311. return -ENOMEM;
  312. dev->video_pbase = virt_to_phys(dev->video_vbase);
  313. pr_info("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
  314. dev->video_pbase, dev->video_vbase,
  315. dev->video_buffer_size / 1024);
  316. /* Set the startup video mode information */
  317. ret = vivid_fb_init_vidmode(dev);
  318. if (ret) {
  319. vivid_fb_release_buffers(dev);
  320. return ret;
  321. }
  322. vivid_clear_fb(dev);
  323. /* Register the framebuffer */
  324. if (register_framebuffer(&dev->fb_info) < 0) {
  325. vivid_fb_release_buffers(dev);
  326. return -EINVAL;
  327. }
  328. /* Set the card to the requested mode */
  329. vivid_fb_set_par(&dev->fb_info);
  330. return 0;
  331. }