• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/font.h>
27 #include <linux/mutex.h>
28 #include <linux/videodev2.h>
29 #include <linux/kthread.h>
30 #include <linux/freezer.h>
31 #include <linux/fb.h>
32 #include <media/videobuf2-vmalloc.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-fh.h>
37 #include <media/v4l2-event.h>
38 #include <media/v4l2-common.h>
39 
40 #include "vivid-core.h"
41 #include "vivid-osd.h"
42 
43 #define MAX_OSD_WIDTH  720
44 #define MAX_OSD_HEIGHT 576
45 
46 /*
47  * Order: white, yellow, cyan, green, magenta, red, blue, black,
48  * and same again with the alpha bit set (if any)
49  */
50 static const u16 rgb555[16] = {
51 	0x7fff, 0x7fe0, 0x03ff, 0x03e0, 0x7c1f, 0x7c00, 0x001f, 0x0000,
52 	0xffff, 0xffe0, 0x83ff, 0x83e0, 0xfc1f, 0xfc00, 0x801f, 0x8000
53 };
54 
55 static const u16 rgb565[16] = {
56 	0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000,
57 	0xffff, 0xffe0, 0x07ff, 0x07e0, 0xf81f, 0xf800, 0x001f, 0x0000
58 };
59 
vivid_clear_fb(struct vivid_dev * dev)60 void vivid_clear_fb(struct vivid_dev *dev)
61 {
62 	void *p = dev->video_vbase;
63 	const u16 *rgb = rgb555;
64 	unsigned x, y;
65 
66 	if (dev->fb_defined.green.length == 6)
67 		rgb = rgb565;
68 
69 	for (y = 0; y < dev->display_height; y++) {
70 		u16 *d = p;
71 
72 		for (x = 0; x < dev->display_width; x++)
73 			d[x] = rgb[(y / 16 + x / 16) % 16];
74 		p += dev->display_byte_stride;
75 	}
76 }
77 
78 /* --------------------------------------------------------------------- */
79 
vivid_fb_ioctl(struct fb_info * info,unsigned cmd,unsigned long arg)80 static int vivid_fb_ioctl(struct fb_info *info, unsigned cmd, unsigned long arg)
81 {
82 	struct vivid_dev *dev = (struct vivid_dev *)info->par;
83 
84 	switch (cmd) {
85 	case FBIOGET_VBLANK: {
86 		struct fb_vblank vblank;
87 
88 		vblank.flags = FB_VBLANK_HAVE_COUNT | FB_VBLANK_HAVE_VCOUNT |
89 			FB_VBLANK_HAVE_VSYNC;
90 		vblank.count = 0;
91 		vblank.vcount = 0;
92 		vblank.hcount = 0;
93 		if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
94 			return -EFAULT;
95 		return 0;
96 	}
97 
98 	default:
99 		dprintk(dev, 1, "Unknown ioctl %08x\n", cmd);
100 		return -EINVAL;
101 	}
102 	return 0;
103 }
104 
105 /* Framebuffer device handling */
106 
vivid_fb_set_var(struct vivid_dev * dev,struct fb_var_screeninfo * var)107 static int vivid_fb_set_var(struct vivid_dev *dev, struct fb_var_screeninfo *var)
108 {
109 	dprintk(dev, 1, "vivid_fb_set_var\n");
110 
111 	if (var->bits_per_pixel != 16) {
112 		dprintk(dev, 1, "vivid_fb_set_var - Invalid bpp\n");
113 		return -EINVAL;
114 	}
115 	dev->display_byte_stride = var->xres * dev->bytes_per_pixel;
116 
117 	return 0;
118 }
119 
vivid_fb_get_fix(struct vivid_dev * dev,struct fb_fix_screeninfo * fix)120 static int vivid_fb_get_fix(struct vivid_dev *dev, struct fb_fix_screeninfo *fix)
121 {
122 	dprintk(dev, 1, "vivid_fb_get_fix\n");
123 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
124 	strlcpy(fix->id, "vioverlay fb", sizeof(fix->id));
125 	fix->smem_start = dev->video_pbase;
126 	fix->smem_len = dev->video_buffer_size;
127 	fix->type = FB_TYPE_PACKED_PIXELS;
128 	fix->visual = FB_VISUAL_TRUECOLOR;
129 	fix->xpanstep = 1;
130 	fix->ypanstep = 1;
131 	fix->ywrapstep = 0;
132 	fix->line_length = dev->display_byte_stride;
133 	fix->accel = FB_ACCEL_NONE;
134 	return 0;
135 }
136 
137 /* Check the requested display mode, returning -EINVAL if we can't
138    handle it. */
139 
_vivid_fb_check_var(struct fb_var_screeninfo * var,struct vivid_dev * dev)140 static int _vivid_fb_check_var(struct fb_var_screeninfo *var, struct vivid_dev *dev)
141 {
142 	dprintk(dev, 1, "vivid_fb_check_var\n");
143 
144 	var->bits_per_pixel = 16;
145 	if (var->green.length == 5) {
146 		var->red.offset = 10;
147 		var->red.length = 5;
148 		var->green.offset = 5;
149 		var->green.length = 5;
150 		var->blue.offset = 0;
151 		var->blue.length = 5;
152 		var->transp.offset = 15;
153 		var->transp.length = 1;
154 	} else {
155 		var->red.offset = 11;
156 		var->red.length = 5;
157 		var->green.offset = 5;
158 		var->green.length = 6;
159 		var->blue.offset = 0;
160 		var->blue.length = 5;
161 		var->transp.offset = 0;
162 		var->transp.length = 0;
163 	}
164 	var->xoffset = var->yoffset = 0;
165 	var->left_margin = var->upper_margin = 0;
166 	var->nonstd = 0;
167 
168 	var->vmode &= ~FB_VMODE_MASK;
169 	var->vmode = FB_VMODE_NONINTERLACED;
170 
171 	/* Dummy values */
172 	var->hsync_len = 24;
173 	var->vsync_len = 2;
174 	var->pixclock = 84316;
175 	var->right_margin = 776;
176 	var->lower_margin = 591;
177 	return 0;
178 }
179 
vivid_fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)180 static int vivid_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
181 {
182 	struct vivid_dev *dev = (struct vivid_dev *) info->par;
183 
184 	dprintk(dev, 1, "vivid_fb_check_var\n");
185 	return _vivid_fb_check_var(var, dev);
186 }
187 
vivid_fb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)188 static int vivid_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
189 {
190 	return 0;
191 }
192 
vivid_fb_set_par(struct fb_info * info)193 static int vivid_fb_set_par(struct fb_info *info)
194 {
195 	int rc = 0;
196 	struct vivid_dev *dev = (struct vivid_dev *) info->par;
197 
198 	dprintk(dev, 1, "vivid_fb_set_par\n");
199 
200 	rc = vivid_fb_set_var(dev, &info->var);
201 	vivid_fb_get_fix(dev, &info->fix);
202 	return rc;
203 }
204 
vivid_fb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)205 static int vivid_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
206 				unsigned blue, unsigned transp,
207 				struct fb_info *info)
208 {
209 	u32 color, *palette;
210 
211 	if (regno >= info->cmap.len)
212 		return -EINVAL;
213 
214 	color = ((transp & 0xFF00) << 16) | ((red & 0xFF00) << 8) |
215 		 (green & 0xFF00) | ((blue & 0xFF00) >> 8);
216 	if (regno >= 16)
217 		return -EINVAL;
218 
219 	palette = info->pseudo_palette;
220 	if (info->var.bits_per_pixel == 16) {
221 		switch (info->var.green.length) {
222 		case 6:
223 			color = (red & 0xf800) |
224 				((green & 0xfc00) >> 5) |
225 				((blue & 0xf800) >> 11);
226 			break;
227 		case 5:
228 			color = ((red & 0xf800) >> 1) |
229 				((green & 0xf800) >> 6) |
230 				((blue & 0xf800) >> 11) |
231 				(transp ? 0x8000 : 0);
232 			break;
233 		}
234 	}
235 	palette[regno] = color;
236 	return 0;
237 }
238 
239 /* We don't really support blanking. All this does is enable or
240    disable the OSD. */
vivid_fb_blank(int blank_mode,struct fb_info * info)241 static int vivid_fb_blank(int blank_mode, struct fb_info *info)
242 {
243 	struct vivid_dev *dev = (struct vivid_dev *)info->par;
244 
245 	dprintk(dev, 1, "Set blanking mode : %d\n", blank_mode);
246 	switch (blank_mode) {
247 	case FB_BLANK_UNBLANK:
248 		break;
249 	case FB_BLANK_NORMAL:
250 	case FB_BLANK_HSYNC_SUSPEND:
251 	case FB_BLANK_VSYNC_SUSPEND:
252 	case FB_BLANK_POWERDOWN:
253 		break;
254 	}
255 	return 0;
256 }
257 
258 static struct fb_ops vivid_fb_ops = {
259 	.owner = THIS_MODULE,
260 	.fb_check_var   = vivid_fb_check_var,
261 	.fb_set_par     = vivid_fb_set_par,
262 	.fb_setcolreg   = vivid_fb_setcolreg,
263 	.fb_fillrect    = cfb_fillrect,
264 	.fb_copyarea    = cfb_copyarea,
265 	.fb_imageblit   = cfb_imageblit,
266 	.fb_cursor      = NULL,
267 	.fb_ioctl       = vivid_fb_ioctl,
268 	.fb_pan_display = vivid_fb_pan_display,
269 	.fb_blank       = vivid_fb_blank,
270 };
271 
272 /* Initialization */
273 
274 
275 /* Setup our initial video mode */
vivid_fb_init_vidmode(struct vivid_dev * dev)276 static int vivid_fb_init_vidmode(struct vivid_dev *dev)
277 {
278 	struct v4l2_rect start_window;
279 
280 	/* Color mode */
281 
282 	dev->bits_per_pixel = 16;
283 	dev->bytes_per_pixel = dev->bits_per_pixel / 8;
284 
285 	start_window.width = MAX_OSD_WIDTH;
286 	start_window.left = 0;
287 
288 	dev->display_byte_stride = start_window.width * dev->bytes_per_pixel;
289 
290 	/* Vertical size & position */
291 
292 	start_window.height = MAX_OSD_HEIGHT;
293 	start_window.top = 0;
294 
295 	dev->display_width = start_window.width;
296 	dev->display_height = start_window.height;
297 
298 	/* Generate a valid fb_var_screeninfo */
299 
300 	dev->fb_defined.xres = dev->display_width;
301 	dev->fb_defined.yres = dev->display_height;
302 	dev->fb_defined.xres_virtual = dev->display_width;
303 	dev->fb_defined.yres_virtual = dev->display_height;
304 	dev->fb_defined.bits_per_pixel = dev->bits_per_pixel;
305 	dev->fb_defined.vmode = FB_VMODE_NONINTERLACED;
306 	dev->fb_defined.left_margin = start_window.left + 1;
307 	dev->fb_defined.upper_margin = start_window.top + 1;
308 	dev->fb_defined.accel_flags = FB_ACCEL_NONE;
309 	dev->fb_defined.nonstd = 0;
310 	/* set default to 1:5:5:5 */
311 	dev->fb_defined.green.length = 5;
312 
313 	/* We've filled in the most data, let the usual mode check
314 	   routine fill in the rest. */
315 	_vivid_fb_check_var(&dev->fb_defined, dev);
316 
317 	/* Generate valid fb_fix_screeninfo */
318 
319 	vivid_fb_get_fix(dev, &dev->fb_fix);
320 
321 	/* Generate valid fb_info */
322 
323 	dev->fb_info.node = -1;
324 	dev->fb_info.flags = FBINFO_FLAG_DEFAULT;
325 	dev->fb_info.fbops = &vivid_fb_ops;
326 	dev->fb_info.par = dev;
327 	dev->fb_info.var = dev->fb_defined;
328 	dev->fb_info.fix = dev->fb_fix;
329 	dev->fb_info.screen_base = (u8 __iomem *)dev->video_vbase;
330 	dev->fb_info.fbops = &vivid_fb_ops;
331 
332 	/* Supply some monitor specs. Bogus values will do for now */
333 	dev->fb_info.monspecs.hfmin = 8000;
334 	dev->fb_info.monspecs.hfmax = 70000;
335 	dev->fb_info.monspecs.vfmin = 10;
336 	dev->fb_info.monspecs.vfmax = 100;
337 
338 	/* Allocate color map */
339 	if (fb_alloc_cmap(&dev->fb_info.cmap, 256, 1)) {
340 		pr_err("abort, unable to alloc cmap\n");
341 		return -ENOMEM;
342 	}
343 
344 	/* Allocate the pseudo palette */
345 	dev->fb_info.pseudo_palette = kmalloc_array(16, sizeof(u32), GFP_KERNEL);
346 
347 	return dev->fb_info.pseudo_palette ? 0 : -ENOMEM;
348 }
349 
350 /* Release any memory we've grabbed */
vivid_fb_release_buffers(struct vivid_dev * dev)351 void vivid_fb_release_buffers(struct vivid_dev *dev)
352 {
353 	if (dev->video_vbase == NULL)
354 		return;
355 
356 	/* Release cmap */
357 	if (dev->fb_info.cmap.len)
358 		fb_dealloc_cmap(&dev->fb_info.cmap);
359 
360 	/* Release pseudo palette */
361 	kfree(dev->fb_info.pseudo_palette);
362 	kfree((void *)dev->video_vbase);
363 }
364 
365 /* Initialize the specified card */
366 
vivid_fb_init(struct vivid_dev * dev)367 int vivid_fb_init(struct vivid_dev *dev)
368 {
369 	int ret;
370 
371 	dev->video_buffer_size = MAX_OSD_HEIGHT * MAX_OSD_WIDTH * 2;
372 	dev->video_vbase = kzalloc(dev->video_buffer_size, GFP_KERNEL | GFP_DMA32);
373 	if (dev->video_vbase == NULL)
374 		return -ENOMEM;
375 	dev->video_pbase = virt_to_phys(dev->video_vbase);
376 
377 	pr_info("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
378 			dev->video_pbase, dev->video_vbase,
379 			dev->video_buffer_size / 1024);
380 
381 	/* Set the startup video mode information */
382 	ret = vivid_fb_init_vidmode(dev);
383 	if (ret) {
384 		vivid_fb_release_buffers(dev);
385 		return ret;
386 	}
387 
388 	vivid_clear_fb(dev);
389 
390 	/* Register the framebuffer */
391 	if (register_framebuffer(&dev->fb_info) < 0) {
392 		vivid_fb_release_buffers(dev);
393 		return -EINVAL;
394 	}
395 
396 	/* Set the card to the requested mode */
397 	vivid_fb_set_par(&dev->fb_info);
398 	return 0;
399 
400 }
401