]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/platform/omap/omap_vout.c
media: omap_vout: potential buffer overflow in vidioc_dqbuf()
[mirror_ubuntu-bionic-kernel.git] / drivers / media / platform / omap / omap_vout.c
CommitLineData
5c7ab634
VH
1/*
2 * omap_vout.c
3 *
4 * Copyright (C) 2005-2010 Texas Instruments.
5 *
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
9 *
10 * Leveraged code from the OMAP2 camera driver
11 * Video-for-Linux (Version 2) camera capture driver for
12 * the OMAP24xx camera controller.
13 *
14 * Author: Andy Lowe (source@mvista.com)
15 *
16 * Copyright (C) 2004 MontaVista Software, Inc.
17 * Copyright (C) 2010 Texas Instruments.
18 *
19 * History:
20 * 20-APR-2006 Khasim Modified VRFB based Rotation,
21 * The image data is always read from 0 degree
22 * view and written
23 * to the virtual space of desired rotation angle
24 * 4-DEC-2006 Jian Changed to support better memory management
25 *
26 * 17-Nov-2008 Hardik Changed driver to use video_ioctl2
27 *
28 * 23-Feb-2010 Vaibhav H Modified to use new DSS2 interface
29 *
30 */
31
32#include <linux/init.h>
33#include <linux/module.h>
34#include <linux/vmalloc.h>
35#include <linux/sched.h>
36#include <linux/types.h>
37#include <linux/platform_device.h>
5c7ab634
VH
38#include <linux/irq.h>
39#include <linux/videodev2.h>
72915e85 40#include <linux/dma-mapping.h>
d1ee8878 41#include <linux/slab.h>
5c7ab634 42
dd880dd4 43#include <media/videobuf-dma-contig.h>
5c7ab634
VH
44#include <media/v4l2-device.h>
45#include <media/v4l2-ioctl.h>
46
6a1c9f6d 47#include <video/omapvrfb.h>
781a1622 48#include <video/omapfb_dss.h>
5c7ab634
VH
49
50#include "omap_voutlib.h"
51#include "omap_voutdef.h"
445e258f 52#include "omap_vout_vrfb.h"
5c7ab634
VH
53
54MODULE_AUTHOR("Texas Instruments");
55MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
56MODULE_LICENSE("GPL");
57
5c7ab634
VH
58/* Driver Configuration macros */
59#define VOUT_NAME "omap_vout"
60
61enum omap_vout_channels {
62 OMAP_VIDEO1,
63 OMAP_VIDEO2,
64};
65
5c7ab634
VH
66static struct videobuf_queue_ops video_vbq_ops;
67/* Variables configurable through module params*/
68static u32 video1_numbuffers = 3;
69static u32 video2_numbuffers = 3;
70static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
71static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
90ab5ee9
RR
72static bool vid1_static_vrfb_alloc;
73static bool vid2_static_vrfb_alloc;
74static bool debug;
5c7ab634
VH
75
76/* Module parameters */
77module_param(video1_numbuffers, uint, S_IRUGO);
78MODULE_PARM_DESC(video1_numbuffers,
79 "Number of buffers to be allocated at init time for Video1 device.");
80
81module_param(video2_numbuffers, uint, S_IRUGO);
82MODULE_PARM_DESC(video2_numbuffers,
83 "Number of buffers to be allocated at init time for Video2 device.");
84
85module_param(video1_bufsize, uint, S_IRUGO);
86MODULE_PARM_DESC(video1_bufsize,
87 "Size of the buffer to be allocated for video1 device");
88
89module_param(video2_bufsize, uint, S_IRUGO);
90MODULE_PARM_DESC(video2_bufsize,
91 "Size of the buffer to be allocated for video2 device");
92
93module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
94MODULE_PARM_DESC(vid1_static_vrfb_alloc,
95 "Static allocation of the VRFB buffer for video1 device");
96
97module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
98MODULE_PARM_DESC(vid2_static_vrfb_alloc,
99 "Static allocation of the VRFB buffer for video2 device");
100
101module_param(debug, bool, S_IRUGO);
102MODULE_PARM_DESC(debug, "Debug level (0-1)");
103
104/* list of image formats supported by OMAP2 video pipelines */
0d334f7f 105static const struct v4l2_fmtdesc omap_formats[] = {
5c7ab634
VH
106 {
107 /* Note: V4L2 defines RGB565 as:
108 *
109 * Byte 0 Byte 1
110 * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
111 *
112 * We interpret RGB565 as:
113 *
114 * Byte 0 Byte 1
115 * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
116 */
117 .description = "RGB565, le",
118 .pixelformat = V4L2_PIX_FMT_RGB565,
119 },
120 {
121 /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
122 * this for RGB24 unpack mode, the last 8 bits are ignored
123 * */
124 .description = "RGB32, le",
125 .pixelformat = V4L2_PIX_FMT_RGB32,
126 },
127 {
128 /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
129 * this for RGB24 packed mode
130 *
131 */
132 .description = "RGB24, le",
133 .pixelformat = V4L2_PIX_FMT_RGB24,
134 },
135 {
136 .description = "YUYV (YUV 4:2:2), packed",
137 .pixelformat = V4L2_PIX_FMT_YUYV,
138 },
139 {
140 .description = "UYVY, packed",
141 .pixelformat = V4L2_PIX_FMT_UYVY,
142 },
143};
144
145#define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
146
5c7ab634
VH
147/*
148 * Try format
149 */
150static int omap_vout_try_format(struct v4l2_pix_format *pix)
151{
152 int ifmt, bpp = 0;
153
154 pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
155 (u32)VID_MAX_HEIGHT);
156 pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
157
158 for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
159 if (pix->pixelformat == omap_formats[ifmt].pixelformat)
160 break;
161 }
162
163 if (ifmt == NUM_OUTPUT_FORMATS)
164 ifmt = 0;
165
166 pix->pixelformat = omap_formats[ifmt].pixelformat;
167 pix->field = V4L2_FIELD_ANY;
5c7ab634
VH
168
169 switch (pix->pixelformat) {
170 case V4L2_PIX_FMT_YUYV:
171 case V4L2_PIX_FMT_UYVY:
172 default:
173 pix->colorspace = V4L2_COLORSPACE_JPEG;
174 bpp = YUYV_BPP;
175 break;
176 case V4L2_PIX_FMT_RGB565:
177 case V4L2_PIX_FMT_RGB565X:
178 pix->colorspace = V4L2_COLORSPACE_SRGB;
179 bpp = RGB565_BPP;
180 break;
181 case V4L2_PIX_FMT_RGB24:
182 pix->colorspace = V4L2_COLORSPACE_SRGB;
183 bpp = RGB24_BPP;
184 break;
185 case V4L2_PIX_FMT_RGB32:
186 case V4L2_PIX_FMT_BGR32:
187 pix->colorspace = V4L2_COLORSPACE_SRGB;
188 bpp = RGB32_BPP;
189 break;
190 }
191 pix->bytesperline = pix->width * bpp;
192 pix->sizeimage = pix->bytesperline * pix->height;
193
194 return bpp;
195}
196
197/*
8a677b6e
JK
198 * omap_vout_get_userptr: Convert user space virtual address to physical
199 * address.
5c7ab634 200 */
8a677b6e
JK
201static int omap_vout_get_userptr(struct videobuf_buffer *vb, u32 virtp,
202 u32 *physp)
5c7ab634 203{
8a677b6e
JK
204 struct frame_vector *vec;
205 int ret;
5c7ab634 206
5c7ab634 207 /* For kernel direct-mapped memory, take the easy way */
8a677b6e
JK
208 if (virtp >= PAGE_OFFSET) {
209 *physp = virt_to_phys((void *)virtp);
210 return 0;
211 }
212
213 vec = frame_vector_create(1);
214 if (!vec)
215 return -ENOMEM;
216
7f23b350 217 ret = get_vaddr_frames(virtp, 1, FOLL_WRITE, vec);
8a677b6e
JK
218 if (ret != 1) {
219 frame_vector_destroy(vec);
220 return -EINVAL;
5c7ab634 221 }
8a677b6e
JK
222 *physp = __pfn_to_phys(frame_vector_pfns(vec)[0]);
223 vb->priv = vec;
5c7ab634 224
8a677b6e 225 return 0;
5c7ab634
VH
226}
227
5c7ab634
VH
228/*
229 * Free the V4L2 buffers
230 */
445e258f 231void omap_vout_free_buffers(struct omap_vout_device *vout)
5c7ab634
VH
232{
233 int i, numbuffers;
234
235 /* Allocate memory for the buffers */
236 numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
237 vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
238
239 for (i = 0; i < numbuffers; i++) {
240 omap_vout_free_buffer(vout->buf_virt_addr[i],
241 vout->buffer_size);
242 vout->buf_phy_addr[i] = 0;
243 vout->buf_virt_addr[i] = 0;
244 }
245}
246
5c7ab634
VH
247/*
248 * Convert V4L2 rotation to DSS rotation
249 * V4L2 understand 0, 90, 180, 270.
25985edc 250 * Convert to 0, 1, 2 and 3 respectively for DSS
5c7ab634
VH
251 */
252static int v4l2_rot_to_dss_rot(int v4l2_rotation,
253 enum dss_rotation *rotation, bool mirror)
254{
255 int ret = 0;
256
257 switch (v4l2_rotation) {
258 case 90:
259 *rotation = dss_rotation_90_degree;
260 break;
261 case 180:
262 *rotation = dss_rotation_180_degree;
263 break;
264 case 270:
265 *rotation = dss_rotation_270_degree;
266 break;
267 case 0:
268 *rotation = dss_rotation_0_degree;
269 break;
270 default:
271 ret = -EINVAL;
272 }
273 return ret;
274}
275
5c7ab634
VH
276static int omap_vout_calculate_offset(struct omap_vout_device *vout)
277{
5c7ab634 278 struct omapvideo_info *ovid;
5c7ab634
VH
279 struct v4l2_rect *crop = &vout->crop;
280 struct v4l2_pix_format *pix = &vout->pix;
281 int *cropped_offset = &vout->cropped_offset;
445e258f 282 int ps = 2, line_length = 0;
5c7ab634
VH
283
284 ovid = &vout->vid_info;
5c7ab634 285
445e258f
AT
286 if (ovid->rotation_type == VOUT_ROT_VRFB) {
287 omap_vout_calculate_vrfb_offset(vout);
288 } else {
289 vout->line_length = line_length = pix->width;
5c7ab634 290
445e258f
AT
291 if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
292 V4L2_PIX_FMT_UYVY == pix->pixelformat)
293 ps = 2;
294 else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
5c7ab634 295 ps = 4;
445e258f
AT
296 else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
297 ps = 3;
5c7ab634 298
445e258f
AT
299 vout->ps = ps;
300
301 *cropped_offset = (line_length * ps) *
302 crop->top + crop->left * ps;
5c7ab634 303 }
445e258f 304
5c7ab634 305 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
445e258f
AT
306 __func__, vout->cropped_offset);
307
5c7ab634
VH
308 return 0;
309}
310
311/*
312 * Convert V4L2 pixel format to DSS pixel format
313 */
72fcf2a8 314static int video_mode_to_dss_mode(struct omap_vout_device *vout)
5c7ab634
VH
315{
316 struct omap_overlay *ovl;
317 struct omapvideo_info *ovid;
318 struct v4l2_pix_format *pix = &vout->pix;
319 enum omap_color_mode mode;
320
321 ovid = &vout->vid_info;
322 ovl = ovid->overlays[0];
323
324 switch (pix->pixelformat) {
5c7ab634
VH
325 case V4L2_PIX_FMT_YUYV:
326 mode = OMAP_DSS_COLOR_YUV2;
327 break;
328 case V4L2_PIX_FMT_UYVY:
329 mode = OMAP_DSS_COLOR_UYVY;
330 break;
331 case V4L2_PIX_FMT_RGB565:
332 mode = OMAP_DSS_COLOR_RGB16;
333 break;
334 case V4L2_PIX_FMT_RGB24:
335 mode = OMAP_DSS_COLOR_RGB24P;
336 break;
337 case V4L2_PIX_FMT_RGB32:
338 mode = (ovl->id == OMAP_DSS_VIDEO1) ?
339 OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
340 break;
341 case V4L2_PIX_FMT_BGR32:
342 mode = OMAP_DSS_COLOR_RGBX32;
343 break;
344 default:
345 mode = -EINVAL;
fe653786 346 break;
5c7ab634
VH
347 }
348 return mode;
349}
350
351/*
352 * Setup the overlay
353 */
a137ac87 354static int omapvid_setup_overlay(struct omap_vout_device *vout,
5c7ab634
VH
355 struct omap_overlay *ovl, int posx, int posy, int outw,
356 int outh, u32 addr)
357{
358 int ret = 0;
359 struct omap_overlay_info info;
52334bb3 360 int cropheight, cropwidth, pixwidth;
5c7ab634
VH
361
362 if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
363 (outw != vout->pix.width || outh != vout->pix.height)) {
364 ret = -EINVAL;
365 goto setup_ovl_err;
366 }
367
368 vout->dss_mode = video_mode_to_dss_mode(vout);
369 if (vout->dss_mode == -EINVAL) {
370 ret = -EINVAL;
371 goto setup_ovl_err;
372 }
373
374 /* Setup the input plane parameters according to
375 * rotation value selected.
376 */
b366888a 377 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
378 cropheight = vout->crop.width;
379 cropwidth = vout->crop.height;
5c7ab634
VH
380 pixwidth = vout->pix.height;
381 } else {
382 cropheight = vout->crop.height;
383 cropwidth = vout->crop.width;
5c7ab634
VH
384 pixwidth = vout->pix.width;
385 }
386
387 ovl->get_overlay_info(ovl, &info);
388 info.paddr = addr;
5c7ab634
VH
389 info.width = cropwidth;
390 info.height = cropheight;
391 info.color_mode = vout->dss_mode;
392 info.mirror = vout->mirror;
393 info.pos_x = posx;
394 info.pos_y = posy;
395 info.out_width = outw;
396 info.out_height = outh;
397 info.global_alpha = vout->win.global_alpha;
b366888a 398 if (!is_rotation_enabled(vout)) {
5c7ab634
VH
399 info.rotation = 0;
400 info.rotation_type = OMAP_DSS_ROT_DMA;
401 info.screen_width = pixwidth;
402 } else {
403 info.rotation = vout->rotation;
404 info.rotation_type = OMAP_DSS_ROT_VRFB;
405 info.screen_width = 2048;
406 }
407
408 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
e8bd888a 409 "%s enable=%d addr=%pad width=%d\n height=%d color_mode=%d\n"
5c7ab634 410 "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
bc39030b
MCC
411 "out_height=%d rotation_type=%d screen_width=%d\n", __func__,
412 ovl->is_enabled(ovl), &info.paddr, info.width, info.height,
5c7ab634
VH
413 info.color_mode, info.rotation, info.mirror, info.pos_x,
414 info.pos_y, info.out_width, info.out_height, info.rotation_type,
415 info.screen_width);
416
417 ret = ovl->set_overlay_info(ovl, &info);
418 if (ret)
419 goto setup_ovl_err;
420
421 return 0;
422
423setup_ovl_err:
424 v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
425 return ret;
426}
427
428/*
429 * Initialize the overlay structure
430 */
a137ac87 431static int omapvid_init(struct omap_vout_device *vout, u32 addr)
5c7ab634
VH
432{
433 int ret = 0, i;
434 struct v4l2_window *win;
435 struct omap_overlay *ovl;
cc182196 436 int posx, posy, outw, outh;
5c7ab634
VH
437 struct omap_video_timings *timing;
438 struct omapvideo_info *ovid = &vout->vid_info;
439
440 win = &vout->win;
441 for (i = 0; i < ovid->num_overlays; i++) {
d629f03f
AT
442 struct omap_dss_device *dssdev;
443
5c7ab634 444 ovl = ovid->overlays[i];
d629f03f
AT
445 dssdev = ovl->get_device(ovl);
446
447 if (!dssdev)
5c7ab634
VH
448 return -EINVAL;
449
d629f03f 450 timing = &dssdev->panel.timings;
5c7ab634
VH
451
452 outw = win->w.width;
453 outh = win->w.height;
454 switch (vout->rotation) {
455 case dss_rotation_90_degree:
456 /* Invert the height and width for 90
457 * and 270 degree rotation
458 */
cc182196 459 swap(outw, outh);
5c7ab634
VH
460 posy = (timing->y_res - win->w.width) - win->w.left;
461 posx = win->w.top;
462 break;
463
464 case dss_rotation_180_degree:
465 posx = (timing->x_res - win->w.width) - win->w.left;
466 posy = (timing->y_res - win->w.height) - win->w.top;
467 break;
468
469 case dss_rotation_270_degree:
cc182196 470 swap(outw, outh);
5c7ab634
VH
471 posy = win->w.left;
472 posx = (timing->x_res - win->w.height) - win->w.top;
473 break;
474
475 default:
476 posx = win->w.left;
477 posy = win->w.top;
478 break;
479 }
480
481 ret = omapvid_setup_overlay(vout, ovl, posx, posy,
482 outw, outh, addr);
483 if (ret)
484 goto omapvid_init_err;
485 }
486 return 0;
487
488omapvid_init_err:
489 v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
490 return ret;
491}
492
493/*
494 * Apply the changes set the go bit of DSS
495 */
a137ac87 496static int omapvid_apply_changes(struct omap_vout_device *vout)
5c7ab634
VH
497{
498 int i;
499 struct omap_overlay *ovl;
500 struct omapvideo_info *ovid = &vout->vid_info;
501
502 for (i = 0; i < ovid->num_overlays; i++) {
d629f03f
AT
503 struct omap_dss_device *dssdev;
504
5c7ab634 505 ovl = ovid->overlays[i];
d629f03f
AT
506 dssdev = ovl->get_device(ovl);
507 if (!dssdev)
5c7ab634
VH
508 return -EINVAL;
509 ovl->manager->apply(ovl->manager);
510 }
511
512 return 0;
513}
514
27801688
AT
515static int omapvid_handle_interlace_display(struct omap_vout_device *vout,
516 unsigned int irqstatus, struct timeval timevalue)
517{
518 u32 fid;
519
520 if (vout->first_int) {
521 vout->first_int = 0;
522 goto err;
523 }
524
525 if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
526 fid = 1;
527 else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
528 fid = 0;
529 else
530 goto err;
531
532 vout->field_id ^= 1;
533 if (fid != vout->field_id) {
534 if (fid == 0)
535 vout->field_id = fid;
536 } else if (0 == fid) {
537 if (vout->cur_frm == vout->next_frm)
538 goto err;
539
540 vout->cur_frm->ts = timevalue;
541 vout->cur_frm->state = VIDEOBUF_DONE;
542 wake_up_interruptible(&vout->cur_frm->done);
543 vout->cur_frm = vout->next_frm;
544 } else {
545 if (list_empty(&vout->dma_queue) ||
546 (vout->cur_frm != vout->next_frm))
547 goto err;
548 }
549
550 return vout->field_id;
551err:
552 return 0;
553}
554
a137ac87 555static void omap_vout_isr(void *arg, unsigned int irqstatus)
5c7ab634 556{
e144ca64
AT
557 int ret, fid, mgr_id;
558 u32 addr, irq;
5c7ab634
VH
559 struct omap_overlay *ovl;
560 struct timeval timevalue;
561 struct omapvideo_info *ovid;
562 struct omap_dss_device *cur_display;
563 struct omap_vout_device *vout = (struct omap_vout_device *)arg;
564
565 if (!vout->streaming)
566 return;
567
568 ovid = &vout->vid_info;
569 ovl = ovid->overlays[0];
5c7ab634 570
e144ca64 571 mgr_id = ovl->manager->id;
d629f03f
AT
572
573 /* get the display device attached to the overlay */
574 cur_display = ovl->get_device(ovl);
575
576 if (!cur_display)
577 return;
5c7ab634
VH
578
579 spin_lock(&vout->vbq_lock);
8e6057b5 580 v4l2_get_timestamp(&timevalue);
5c7ab634 581
27801688 582 switch (cur_display->type) {
881a9640 583 case OMAP_DISPLAY_TYPE_DSI:
27801688 584 case OMAP_DISPLAY_TYPE_DPI:
c6a328a0 585 case OMAP_DISPLAY_TYPE_DVI:
e144ca64
AT
586 if (mgr_id == OMAP_DSS_CHANNEL_LCD)
587 irq = DISPC_IRQ_VSYNC;
588 else if (mgr_id == OMAP_DSS_CHANNEL_LCD2)
589 irq = DISPC_IRQ_VSYNC2;
590 else
5c7ab634
VH
591 goto vout_isr_err;
592
e144ca64 593 if (!(irqstatus & irq))
5251dd6c 594 goto vout_isr_err;
27801688
AT
595 break;
596 case OMAP_DISPLAY_TYPE_VENC:
597 fid = omapvid_handle_interlace_display(vout, irqstatus,
598 timevalue);
599 if (!fid)
5c7ab634 600 goto vout_isr_err;
27801688
AT
601 break;
602 case OMAP_DISPLAY_TYPE_HDMI:
603 if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
604 goto vout_isr_err;
605 break;
606 default:
607 goto vout_isr_err;
608 }
5c7ab634 609
27801688
AT
610 if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
611 vout->cur_frm->ts = timevalue;
612 vout->cur_frm->state = VIDEOBUF_DONE;
613 wake_up_interruptible(&vout->cur_frm->done);
614 vout->cur_frm = vout->next_frm;
615 }
5c7ab634 616
27801688
AT
617 vout->first_int = 0;
618 if (list_empty(&vout->dma_queue))
619 goto vout_isr_err;
5c7ab634 620
27801688
AT
621 vout->next_frm = list_entry(vout->dma_queue.next,
622 struct videobuf_buffer, queue);
623 list_del(&vout->next_frm->queue);
5c7ab634 624
27801688 625 vout->next_frm->state = VIDEOBUF_ACTIVE;
5c7ab634 626
27801688
AT
627 addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
628 + vout->cropped_offset;
5c7ab634 629
27801688
AT
630 /* First save the configuration in ovelray structure */
631 ret = omapvid_init(vout, addr);
15170025 632 if (ret) {
27801688
AT
633 printk(KERN_ERR VOUT_NAME
634 "failed to set overlay info\n");
15170025
FF
635 goto vout_isr_err;
636 }
637
27801688
AT
638 /* Enable the pipeline and set the Go bit */
639 ret = omapvid_apply_changes(vout);
640 if (ret)
641 printk(KERN_ERR VOUT_NAME "failed to change mode\n");
5c7ab634
VH
642
643vout_isr_err:
644 spin_unlock(&vout->vbq_lock);
645}
646
5c7ab634
VH
647/* Video buffer call backs */
648
649/*
650 * Buffer setup function is called by videobuf layer when REQBUF ioctl is
651 * called. This is used to setup buffers and return size and count of
652 * buffers allocated. After the call to this buffer, videobuf layer will
653 * setup buffer queue depending on the size and count of buffers
654 */
655static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
656 unsigned int *size)
657{
658 int startindex = 0, i, j;
659 u32 phy_addr = 0, virt_addr = 0;
660 struct omap_vout_device *vout = q->priv_data;
445e258f 661 struct omapvideo_info *ovid = &vout->vid_info;
d06db7ec 662 int vid_max_buf_size;
5c7ab634
VH
663
664 if (!vout)
665 return -EINVAL;
666
d06db7ec
AT
667 vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
668 video2_bufsize;
669
5c7ab634
VH
670 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
671 return -EINVAL;
672
673 startindex = (vout->vid == OMAP_VIDEO1) ?
674 video1_numbuffers : video2_numbuffers;
675 if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
676 *count = startindex;
677
445e258f 678 if (ovid->rotation_type == VOUT_ROT_VRFB) {
5c7ab634
VH
679 if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
680 return -ENOMEM;
445e258f 681 }
5c7ab634
VH
682
683 if (V4L2_MEMORY_MMAP != vout->memory)
684 return 0;
685
686 /* Now allocated the V4L2 buffers */
687 *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
688 startindex = (vout->vid == OMAP_VIDEO1) ?
689 video1_numbuffers : video2_numbuffers;
690
383e4f69 691 /* Check the size of the buffer */
d06db7ec 692 if (*size > vid_max_buf_size) {
383e4f69
VH
693 v4l2_err(&vout->vid_dev->v4l2_dev,
694 "buffer allocation mismatch [%u] [%u]\n",
695 *size, vout->buffer_size);
696 return -ENOMEM;
697 }
698
5c7ab634
VH
699 for (i = startindex; i < *count; i++) {
700 vout->buffer_size = *size;
701
702 virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
703 &phy_addr);
704 if (!virt_addr) {
445e258f 705 if (ovid->rotation_type == VOUT_ROT_NONE) {
5c7ab634 706 break;
445e258f
AT
707 } else {
708 if (!is_rotation_enabled(vout))
709 break;
5c7ab634
VH
710 /* Free the VRFB buffers if no space for V4L2 buffers */
711 for (j = i; j < *count; j++) {
712 omap_vout_free_buffer(
713 vout->smsshado_virt_addr[j],
714 vout->smsshado_size);
715 vout->smsshado_virt_addr[j] = 0;
716 vout->smsshado_phy_addr[j] = 0;
445e258f 717 }
5c7ab634
VH
718 }
719 }
720 vout->buf_virt_addr[i] = virt_addr;
721 vout->buf_phy_addr[i] = phy_addr;
722 }
723 *count = vout->buffer_allocated = i;
724
725 return 0;
726}
727
728/*
729 * Free the V4L2 buffers additionally allocated than default
445e258f 730 * number of buffers
5c7ab634 731 */
445e258f 732static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
5c7ab634
VH
733{
734 int num_buffers = 0, i;
735
736 num_buffers = (vout->vid == OMAP_VIDEO1) ?
737 video1_numbuffers : video2_numbuffers;
738
739 for (i = num_buffers; i < vout->buffer_allocated; i++) {
740 if (vout->buf_virt_addr[i])
741 omap_vout_free_buffer(vout->buf_virt_addr[i],
742 vout->buffer_size);
743
744 vout->buf_virt_addr[i] = 0;
745 vout->buf_phy_addr[i] = 0;
746 }
5c7ab634
VH
747 vout->buffer_allocated = num_buffers;
748}
749
750/*
751 * This function will be called when VIDIOC_QBUF ioctl is called.
752 * It prepare buffers before give out for the display. This function
753 * converts user space virtual address into physical address if userptr memory
754 * exchange mechanism is used. If rotation is enabled, it copies entire
755 * buffer into VRFB memory space before giving it to the DSS.
756 */
757static int omap_vout_buffer_prepare(struct videobuf_queue *q,
445e258f
AT
758 struct videobuf_buffer *vb,
759 enum v4l2_field field)
5c7ab634 760{
5c7ab634 761 struct omap_vout_device *vout = q->priv_data;
445e258f 762 struct omapvideo_info *ovid = &vout->vid_info;
5c7ab634
VH
763
764 if (VIDEOBUF_NEEDS_INIT == vb->state) {
765 vb->width = vout->pix.width;
766 vb->height = vout->pix.height;
767 vb->size = vb->width * vb->height * vout->bpp;
768 vb->field = field;
769 }
770 vb->state = VIDEOBUF_PREPARED;
771 /* if user pointer memory mechanism is used, get the physical
772 * address of the buffer
773 */
774 if (V4L2_MEMORY_USERPTR == vb->memory) {
8a677b6e
JK
775 int ret;
776
5c7ab634
VH
777 if (0 == vb->baddr)
778 return -EINVAL;
5c7ab634 779 /* Physical address */
8a677b6e
JK
780 ret = omap_vout_get_userptr(vb, vb->baddr,
781 (u32 *)&vout->queued_buf_addr[vb->i]);
782 if (ret < 0)
783 return ret;
dd880dd4 784 } else {
e8bd888a 785 unsigned long addr, dma_addr;
72915e85
AJ
786 unsigned long size;
787
788 addr = (unsigned long) vout->buf_virt_addr[vb->i];
789 size = (unsigned long) vb->size;
790
791 dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
792 size, DMA_TO_DEVICE);
793 if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
bc39030b
MCC
794 v4l2_err(&vout->vid_dev->v4l2_dev,
795 "dma_map_single failed\n");
72915e85 796
dd880dd4 797 vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
5c7ab634
VH
798 }
799
445e258f
AT
800 if (ovid->rotation_type == VOUT_ROT_VRFB)
801 return omap_vout_prepare_vrfb(vout, vb);
802 else
5c7ab634 803 return 0;
5c7ab634
VH
804}
805
806/*
25985edc 807 * Buffer queue function will be called from the videobuf layer when _QBUF
5c7ab634
VH
808 * ioctl is called. It is used to enqueue buffer, which is ready to be
809 * displayed.
810 */
811static void omap_vout_buffer_queue(struct videobuf_queue *q,
812 struct videobuf_buffer *vb)
813{
814 struct omap_vout_device *vout = q->priv_data;
815
816 /* Driver is also maintainig a queue. So enqueue buffer in the driver
817 * queue */
818 list_add_tail(&vb->queue, &vout->dma_queue);
819
820 vb->state = VIDEOBUF_QUEUED;
821}
822
823/*
824 * Buffer release function is called from videobuf layer to release buffer
825 * which are already allocated
826 */
827static void omap_vout_buffer_release(struct videobuf_queue *q,
828 struct videobuf_buffer *vb)
829{
5c7ab634 830 vb->state = VIDEOBUF_NEEDS_INIT;
8a677b6e
JK
831 if (vb->memory == V4L2_MEMORY_USERPTR && vb->priv) {
832 struct frame_vector *vec = vb->priv;
5c7ab634 833
8a677b6e
JK
834 put_vaddr_frames(vec);
835 frame_vector_destroy(vec);
836 }
5c7ab634
VH
837}
838
839/*
840 * File operations
841 */
94f3f48f
LP
842static unsigned int omap_vout_poll(struct file *file,
843 struct poll_table_struct *wait)
844{
845 struct omap_vout_device *vout = file->private_data;
846 struct videobuf_queue *q = &vout->vbq;
847
848 return videobuf_poll_stream(file, q, wait);
849}
850
5c7ab634
VH
851static void omap_vout_vm_open(struct vm_area_struct *vma)
852{
853 struct omap_vout_device *vout = vma->vm_private_data;
854
855 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
856 "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
857 vout->mmap_count++;
858}
859
860static void omap_vout_vm_close(struct vm_area_struct *vma)
861{
862 struct omap_vout_device *vout = vma->vm_private_data;
863
864 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
865 "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
866 vout->mmap_count--;
867}
868
7cbea8dc 869static const struct vm_operations_struct omap_vout_vm_ops = {
5c7ab634
VH
870 .open = omap_vout_vm_open,
871 .close = omap_vout_vm_close,
872};
873
874static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
875{
876 int i;
877 void *pos;
878 unsigned long start = vma->vm_start;
879 unsigned long size = (vma->vm_end - vma->vm_start);
5c7ab634
VH
880 struct omap_vout_device *vout = file->private_data;
881 struct videobuf_queue *q = &vout->vbq;
882
883 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
884 " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
885 vma->vm_pgoff, vma->vm_start, vma->vm_end);
886
887 /* look for the buffer to map */
888 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
889 if (NULL == q->bufs[i])
890 continue;
891 if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
892 continue;
893 if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
894 break;
895 }
896
897 if (VIDEO_MAX_FRAME == i) {
898 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
899 "offset invalid [offset=0x%lx]\n",
900 (vma->vm_pgoff << PAGE_SHIFT));
901 return -EINVAL;
902 }
383e4f69
VH
903 /* Check the size of the buffer */
904 if (size > vout->buffer_size) {
905 v4l2_err(&vout->vid_dev->v4l2_dev,
906 "insufficient memory [%lu] [%u]\n",
907 size, vout->buffer_size);
908 return -ENOMEM;
909 }
910
5c7ab634
VH
911 q->bufs[i]->baddr = vma->vm_start;
912
314e51b9 913 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
5c7ab634
VH
914 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
915 vma->vm_ops = &omap_vout_vm_ops;
916 vma->vm_private_data = (void *) vout;
dd880dd4 917 pos = (void *)vout->buf_virt_addr[i];
5c7ab634
VH
918 vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
919 while (size > 0) {
920 unsigned long pfn;
921 pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
922 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
923 return -EAGAIN;
924 start += PAGE_SIZE;
925 pos += PAGE_SIZE;
926 size -= PAGE_SIZE;
927 }
928 vout->mmap_count++;
929 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
930
931 return 0;
932}
933
934static int omap_vout_release(struct file *file)
935{
936 unsigned int ret, i;
937 struct videobuf_queue *q;
938 struct omapvideo_info *ovid;
939 struct omap_vout_device *vout = file->private_data;
940
941 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
942 ovid = &vout->vid_info;
943
944 if (!vout)
945 return 0;
946
947 q = &vout->vbq;
948 /* Disable all the overlay managers connected with this interface */
949 for (i = 0; i < ovid->num_overlays; i++) {
950 struct omap_overlay *ovl = ovid->overlays[i];
d629f03f
AT
951 struct omap_dss_device *dssdev = ovl->get_device(ovl);
952
953 if (dssdev)
aaa874a9 954 ovl->disable(ovl);
5c7ab634
VH
955 }
956 /* Turn off the pipeline */
957 ret = omapvid_apply_changes(vout);
958 if (ret)
959 v4l2_warn(&vout->vid_dev->v4l2_dev,
960 "Unable to apply changes\n");
961
962 /* Free all buffers */
445e258f
AT
963 omap_vout_free_extra_buffers(vout);
964
965 /* Free the VRFB buffers only if they are allocated
966 * during reqbufs. Don't free if init time allocated
967 */
968 if (ovid->rotation_type == VOUT_ROT_VRFB) {
969 if (!vout->vrfb_static_allocation)
970 omap_vout_free_vrfb_buffers(vout);
971 }
5c7ab634
VH
972 videobuf_mmap_free(q);
973
974 /* Even if apply changes fails we should continue
b595076a 975 freeing allocated memory */
5c7ab634
VH
976 if (vout->streaming) {
977 u32 mask = 0;
978
979 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
5251dd6c 980 DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
5c7ab634 981 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
160ac0ba 982 vout->streaming = false;
5c7ab634
VH
983
984 videobuf_streamoff(q);
985 videobuf_queue_cancel(q);
986 }
987
988 if (vout->mmap_count != 0)
989 vout->mmap_count = 0;
990
991 vout->opened -= 1;
992 file->private_data = NULL;
993
994 if (vout->buffer_allocated)
995 videobuf_mmap_free(q);
996
997 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
998 return ret;
999}
1000
1001static int omap_vout_open(struct file *file)
1002{
1003 struct videobuf_queue *q;
1004 struct omap_vout_device *vout = NULL;
1005
1006 vout = video_drvdata(file);
5c7ab634
VH
1007
1008 if (vout == NULL)
1009 return -ENODEV;
1010
bfba2b3e
ME
1011 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
1012
5c7ab634
VH
1013 /* for now, we only support single open */
1014 if (vout->opened)
1015 return -EBUSY;
1016
1017 vout->opened += 1;
1018
1019 file->private_data = vout;
1020 vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1021
1022 q = &vout->vbq;
1023 video_vbq_ops.buf_setup = omap_vout_buffer_setup;
1024 video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
1025 video_vbq_ops.buf_release = omap_vout_buffer_release;
1026 video_vbq_ops.buf_queue = omap_vout_buffer_queue;
1027 spin_lock_init(&vout->vbq_lock);
1028
dd880dd4
VH
1029 videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
1030 &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
e3cfd447 1031 sizeof(struct videobuf_buffer), vout, NULL);
5c7ab634
VH
1032
1033 v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
1034 return 0;
1035}
1036
1037/*
1038 * V4L2 ioctls
1039 */
1040static int vidioc_querycap(struct file *file, void *fh,
1041 struct v4l2_capability *cap)
1042{
1043 struct omap_vout_device *vout = fh;
1044
1045 strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
1046 strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
1047 cap->bus_info[0] = '\0';
a020c747 1048 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
047a01fd 1049 V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
a020c747 1050 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
5c7ab634
VH
1051
1052 return 0;
1053}
1054
1055static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
1056 struct v4l2_fmtdesc *fmt)
1057{
1058 int index = fmt->index;
5c7ab634 1059
5c7ab634
VH
1060 if (index >= NUM_OUTPUT_FORMATS)
1061 return -EINVAL;
1062
1063 fmt->flags = omap_formats[index].flags;
1064 strlcpy(fmt->description, omap_formats[index].description,
1065 sizeof(fmt->description));
1066 fmt->pixelformat = omap_formats[index].pixelformat;
1067
1068 return 0;
1069}
1070
1071static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
1072 struct v4l2_format *f)
1073{
1074 struct omap_vout_device *vout = fh;
1075
1076 f->fmt.pix = vout->pix;
1077 return 0;
1078
1079}
1080
1081static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
1082 struct v4l2_format *f)
1083{
1084 struct omap_overlay *ovl;
1085 struct omapvideo_info *ovid;
1086 struct omap_video_timings *timing;
1087 struct omap_vout_device *vout = fh;
d629f03f 1088 struct omap_dss_device *dssdev;
5c7ab634
VH
1089
1090 ovid = &vout->vid_info;
1091 ovl = ovid->overlays[0];
d629f03f
AT
1092 /* get the display device attached to the overlay */
1093 dssdev = ovl->get_device(ovl);
5c7ab634 1094
d629f03f 1095 if (!dssdev)
5c7ab634 1096 return -EINVAL;
d629f03f
AT
1097
1098 timing = &dssdev->panel.timings;
5c7ab634
VH
1099
1100 vout->fbuf.fmt.height = timing->y_res;
1101 vout->fbuf.fmt.width = timing->x_res;
1102
1103 omap_vout_try_format(&f->fmt.pix);
1104 return 0;
1105}
1106
1107static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
1108 struct v4l2_format *f)
1109{
1110 int ret, bpp;
1111 struct omap_overlay *ovl;
1112 struct omapvideo_info *ovid;
1113 struct omap_video_timings *timing;
1114 struct omap_vout_device *vout = fh;
d629f03f 1115 struct omap_dss_device *dssdev;
5c7ab634
VH
1116
1117 if (vout->streaming)
1118 return -EBUSY;
1119
1120 mutex_lock(&vout->lock);
1121
1122 ovid = &vout->vid_info;
1123 ovl = ovid->overlays[0];
d629f03f 1124 dssdev = ovl->get_device(ovl);
5c7ab634
VH
1125
1126 /* get the display device attached to the overlay */
d629f03f 1127 if (!dssdev) {
5c7ab634
VH
1128 ret = -EINVAL;
1129 goto s_fmt_vid_out_exit;
1130 }
d629f03f 1131 timing = &dssdev->panel.timings;
5c7ab634
VH
1132
1133 /* We dont support RGB24-packed mode if vrfb rotation
1134 * is enabled*/
b366888a 1135 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1136 f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1137 ret = -EINVAL;
1138 goto s_fmt_vid_out_exit;
1139 }
1140
1141 /* get the framebuffer parameters */
1142
b366888a 1143 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1144 vout->fbuf.fmt.height = timing->x_res;
1145 vout->fbuf.fmt.width = timing->y_res;
1146 } else {
1147 vout->fbuf.fmt.height = timing->y_res;
1148 vout->fbuf.fmt.width = timing->x_res;
1149 }
1150
1151 /* change to samller size is OK */
1152
1153 bpp = omap_vout_try_format(&f->fmt.pix);
1154 f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
1155
1156 /* try & set the new output format */
1157 vout->bpp = bpp;
1158 vout->pix = f->fmt.pix;
1159 vout->vrfb_bpp = 1;
1160
1161 /* If YUYV then vrfb bpp is 2, for others its 1 */
1162 if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
1163 V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
1164 vout->vrfb_bpp = 2;
1165
1166 /* set default crop and win */
1167 omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
1168
5c7ab634
VH
1169 ret = 0;
1170
1171s_fmt_vid_out_exit:
1172 mutex_unlock(&vout->lock);
1173 return ret;
1174}
1175
1176static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
1177 struct v4l2_format *f)
1178{
1179 int ret = 0;
1180 struct omap_vout_device *vout = fh;
11354dd5
AT
1181 struct omap_overlay *ovl;
1182 struct omapvideo_info *ovid;
5c7ab634
VH
1183 struct v4l2_window *win = &f->fmt.win;
1184
11354dd5
AT
1185 ovid = &vout->vid_info;
1186 ovl = ovid->overlays[0];
1187
5c7ab634
VH
1188 ret = omap_vout_try_window(&vout->fbuf, win);
1189
1190 if (!ret) {
11354dd5 1191 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
5c7ab634
VH
1192 win->global_alpha = 255;
1193 else
1194 win->global_alpha = f->fmt.win.global_alpha;
1195 }
1196
1197 return ret;
1198}
1199
1200static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
1201 struct v4l2_format *f)
1202{
1203 int ret = 0;
1204 struct omap_overlay *ovl;
1205 struct omapvideo_info *ovid;
1206 struct omap_vout_device *vout = fh;
1207 struct v4l2_window *win = &f->fmt.win;
1208
1209 mutex_lock(&vout->lock);
1210 ovid = &vout->vid_info;
1211 ovl = ovid->overlays[0];
1212
1213 ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
1214 if (!ret) {
11354dd5
AT
1215 /* Video1 plane does not support global alpha on OMAP3 */
1216 if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
5c7ab634
VH
1217 vout->win.global_alpha = 255;
1218 else
1219 vout->win.global_alpha = f->fmt.win.global_alpha;
1220
1221 vout->win.chromakey = f->fmt.win.chromakey;
1222 }
1223 mutex_unlock(&vout->lock);
1224 return ret;
1225}
1226
5c7ab634
VH
1227static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
1228 struct v4l2_format *f)
1229{
1230 u32 key_value = 0;
1231 struct omap_overlay *ovl;
1232 struct omapvideo_info *ovid;
1233 struct omap_vout_device *vout = fh;
1234 struct omap_overlay_manager_info info;
1235 struct v4l2_window *win = &f->fmt.win;
1236
1237 ovid = &vout->vid_info;
1238 ovl = ovid->overlays[0];
1239
1240 win->w = vout->win.w;
1241 win->field = vout->win.field;
1242 win->global_alpha = vout->win.global_alpha;
1243
1244 if (ovl->manager && ovl->manager->get_manager_info) {
1245 ovl->manager->get_manager_info(ovl->manager, &info);
1246 key_value = info.trans_key;
1247 }
1248 win->chromakey = key_value;
1249 return 0;
1250}
1251
d1d09461 1252static int vidioc_g_selection(struct file *file, void *fh, struct v4l2_selection *sel)
5c7ab634
VH
1253{
1254 struct omap_vout_device *vout = fh;
1255 struct v4l2_pix_format *pix = &vout->pix;
1256
d1d09461 1257 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
5c7ab634
VH
1258 return -EINVAL;
1259
d1d09461
HV
1260 switch (sel->target) {
1261 case V4L2_SEL_TGT_CROP:
1262 sel->r = vout->crop;
1263 break;
1264 case V4L2_SEL_TGT_CROP_DEFAULT:
1265 omap_vout_default_crop(&vout->pix, &vout->fbuf, &sel->r);
1266 break;
1267 case V4L2_SEL_TGT_CROP_BOUNDS:
1268 /* Width and height are always even */
1269 sel->r.width = pix->width & ~1;
1270 sel->r.height = pix->height & ~1;
1271 break;
1272 default:
5c7ab634 1273 return -EINVAL;
d1d09461 1274 }
5c7ab634
VH
1275 return 0;
1276}
1277
d1d09461 1278static int vidioc_s_selection(struct file *file, void *fh, struct v4l2_selection *sel)
5c7ab634
VH
1279{
1280 int ret = -EINVAL;
1281 struct omap_vout_device *vout = fh;
1282 struct omapvideo_info *ovid;
1283 struct omap_overlay *ovl;
1284 struct omap_video_timings *timing;
d629f03f 1285 struct omap_dss_device *dssdev;
5c7ab634 1286
d1d09461
HV
1287 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1288 return -EINVAL;
1289
1290 if (sel->target != V4L2_SEL_TGT_CROP)
1291 return -EINVAL;
1292
5c7ab634
VH
1293 if (vout->streaming)
1294 return -EBUSY;
1295
1296 mutex_lock(&vout->lock);
1297 ovid = &vout->vid_info;
1298 ovl = ovid->overlays[0];
d629f03f
AT
1299 /* get the display device attached to the overlay */
1300 dssdev = ovl->get_device(ovl);
5c7ab634 1301
d629f03f 1302 if (!dssdev) {
5c7ab634
VH
1303 ret = -EINVAL;
1304 goto s_crop_err;
1305 }
d629f03f
AT
1306
1307 timing = &dssdev->panel.timings;
5c7ab634 1308
b366888a 1309 if (is_rotation_90_or_270(vout)) {
5c7ab634
VH
1310 vout->fbuf.fmt.height = timing->x_res;
1311 vout->fbuf.fmt.width = timing->y_res;
1312 } else {
1313 vout->fbuf.fmt.height = timing->y_res;
1314 vout->fbuf.fmt.width = timing->x_res;
1315 }
1316
d1d09461
HV
1317 ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
1318 &vout->fbuf, &sel->r);
5c7ab634
VH
1319
1320s_crop_err:
1321 mutex_unlock(&vout->lock);
1322 return ret;
1323}
1324
77430f03 1325static int omap_vout_s_ctrl(struct v4l2_ctrl *ctrl)
5c7ab634 1326{
77430f03
HV
1327 struct omap_vout_device *vout =
1328 container_of(ctrl->handler, struct omap_vout_device, ctrl_handler);
5c7ab634
VH
1329 int ret = 0;
1330
1331 switch (ctrl->id) {
77430f03 1332 case V4L2_CID_ROTATE: {
445e258f 1333 struct omapvideo_info *ovid;
77430f03 1334 int rotation = ctrl->val;
5c7ab634 1335
445e258f
AT
1336 ovid = &vout->vid_info;
1337
5c7ab634 1338 mutex_lock(&vout->lock);
445e258f
AT
1339 if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
1340 mutex_unlock(&vout->lock);
1341 ret = -ERANGE;
1342 break;
1343 }
5c7ab634
VH
1344
1345 if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1346 mutex_unlock(&vout->lock);
1347 ret = -EINVAL;
1348 break;
1349 }
1350
1351 if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
1352 vout->mirror)) {
1353 mutex_unlock(&vout->lock);
1354 ret = -EINVAL;
1355 break;
1356 }
5c7ab634
VH
1357 mutex_unlock(&vout->lock);
1358 break;
1359 }
1360 case V4L2_CID_BG_COLOR:
1361 {
1362 struct omap_overlay *ovl;
77430f03 1363 unsigned int color = ctrl->val;
5c7ab634
VH
1364 struct omap_overlay_manager_info info;
1365
1366 ovl = vout->vid_info.overlays[0];
1367
1368 mutex_lock(&vout->lock);
1369 if (!ovl->manager || !ovl->manager->get_manager_info) {
1370 mutex_unlock(&vout->lock);
1371 ret = -EINVAL;
1372 break;
1373 }
1374
1375 ovl->manager->get_manager_info(ovl->manager, &info);
1376 info.default_color = color;
1377 if (ovl->manager->set_manager_info(ovl->manager, &info)) {
1378 mutex_unlock(&vout->lock);
1379 ret = -EINVAL;
1380 break;
1381 }
5c7ab634
VH
1382 mutex_unlock(&vout->lock);
1383 break;
1384 }
1385 case V4L2_CID_VFLIP:
1386 {
5c7ab634 1387 struct omapvideo_info *ovid;
77430f03 1388 unsigned int mirror = ctrl->val;
5c7ab634
VH
1389
1390 ovid = &vout->vid_info;
5c7ab634
VH
1391
1392 mutex_lock(&vout->lock);
445e258f
AT
1393 if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
1394 mutex_unlock(&vout->lock);
1395 ret = -ERANGE;
1396 break;
1397 }
5c7ab634
VH
1398
1399 if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
1400 mutex_unlock(&vout->lock);
1401 ret = -EINVAL;
1402 break;
1403 }
1404 vout->mirror = mirror;
5c7ab634
VH
1405 mutex_unlock(&vout->lock);
1406 break;
1407 }
1408 default:
77430f03 1409 return -EINVAL;
5c7ab634
VH
1410 }
1411 return ret;
1412}
1413
77430f03
HV
1414static const struct v4l2_ctrl_ops omap_vout_ctrl_ops = {
1415 .s_ctrl = omap_vout_s_ctrl,
1416};
1417
5c7ab634
VH
1418static int vidioc_reqbufs(struct file *file, void *fh,
1419 struct v4l2_requestbuffers *req)
1420{
1421 int ret = 0;
1422 unsigned int i, num_buffers = 0;
1423 struct omap_vout_device *vout = fh;
1424 struct videobuf_queue *q = &vout->vbq;
5c7ab634 1425
52334bb3 1426 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
5c7ab634
VH
1427 return -EINVAL;
1428 /* if memory is not mmp or userptr
1429 return error */
1430 if ((V4L2_MEMORY_MMAP != req->memory) &&
1431 (V4L2_MEMORY_USERPTR != req->memory))
1432 return -EINVAL;
1433
1434 mutex_lock(&vout->lock);
1435 /* Cannot be requested when streaming is on */
1436 if (vout->streaming) {
1437 ret = -EBUSY;
1438 goto reqbuf_err;
1439 }
1440
1441 /* If buffers are already allocated free them */
1442 if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
1443 if (vout->mmap_count) {
1444 ret = -EBUSY;
1445 goto reqbuf_err;
1446 }
1447 num_buffers = (vout->vid == OMAP_VIDEO1) ?
1448 video1_numbuffers : video2_numbuffers;
1449 for (i = num_buffers; i < vout->buffer_allocated; i++) {
dd880dd4 1450 omap_vout_free_buffer(vout->buf_virt_addr[i],
5c7ab634
VH
1451 vout->buffer_size);
1452 vout->buf_virt_addr[i] = 0;
1453 vout->buf_phy_addr[i] = 0;
1454 }
1455 vout->buffer_allocated = num_buffers;
1456 videobuf_mmap_free(q);
1457 } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
1458 if (vout->buffer_allocated) {
1459 videobuf_mmap_free(q);
1460 for (i = 0; i < vout->buffer_allocated; i++) {
1461 kfree(q->bufs[i]);
1462 q->bufs[i] = NULL;
1463 }
1464 vout->buffer_allocated = 0;
1465 }
1466 }
1467
1468 /*store the memory type in data structure */
1469 vout->memory = req->memory;
1470
1471 INIT_LIST_HEAD(&vout->dma_queue);
1472
1473 /* call videobuf_reqbufs api */
1474 ret = videobuf_reqbufs(q, req);
1475 if (ret < 0)
1476 goto reqbuf_err;
1477
1478 vout->buffer_allocated = req->count;
dd880dd4 1479
5c7ab634
VH
1480reqbuf_err:
1481 mutex_unlock(&vout->lock);
1482 return ret;
1483}
1484
1485static int vidioc_querybuf(struct file *file, void *fh,
1486 struct v4l2_buffer *b)
1487{
1488 struct omap_vout_device *vout = fh;
1489
1490 return videobuf_querybuf(&vout->vbq, b);
1491}
1492
1493static int vidioc_qbuf(struct file *file, void *fh,
1494 struct v4l2_buffer *buffer)
1495{
1496 struct omap_vout_device *vout = fh;
1497 struct videobuf_queue *q = &vout->vbq;
1498
1499 if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
1500 (buffer->index >= vout->buffer_allocated) ||
1501 (q->bufs[buffer->index]->memory != buffer->memory)) {
1502 return -EINVAL;
1503 }
1504 if (V4L2_MEMORY_USERPTR == buffer->memory) {
1505 if ((buffer->length < vout->pix.sizeimage) ||
1506 (0 == buffer->m.userptr)) {
1507 return -EINVAL;
1508 }
1509 }
1510
b366888a 1511 if ((is_rotation_enabled(vout)) &&
5c7ab634
VH
1512 vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
1513 v4l2_warn(&vout->vid_dev->v4l2_dev,
1514 "DMA Channel not allocated for Rotation\n");
1515 return -EINVAL;
1516 }
1517
1518 return videobuf_qbuf(q, buffer);
1519}
1520
1521static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1522{
1523 struct omap_vout_device *vout = fh;
1524 struct videobuf_queue *q = &vout->vbq;
1525
72915e85
AJ
1526 int ret;
1527 u32 addr;
1528 unsigned long size;
1529 struct videobuf_buffer *vb;
1530
5c7ab634
VH
1531 if (!vout->streaming)
1532 return -EINVAL;
1533
ed8d3576
DC
1534 ret = videobuf_dqbuf(q, b, !!(file->f_flags & O_NONBLOCK));
1535 if (ret)
1536 return ret;
1537
1538 vb = q->bufs[b->index];
72915e85
AJ
1539
1540 addr = (unsigned long) vout->buf_phy_addr[vb->i];
1541 size = (unsigned long) vb->size;
1542 dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
1543 size, DMA_TO_DEVICE);
ed8d3576 1544 return 0;
5c7ab634
VH
1545}
1546
1547static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1548{
1549 int ret = 0, j;
1550 u32 addr = 0, mask = 0;
1551 struct omap_vout_device *vout = fh;
1552 struct videobuf_queue *q = &vout->vbq;
1553 struct omapvideo_info *ovid = &vout->vid_info;
1554
1555 mutex_lock(&vout->lock);
1556
1557 if (vout->streaming) {
1558 ret = -EBUSY;
1559 goto streamon_err;
1560 }
1561
1562 ret = videobuf_streamon(q);
1563 if (ret)
1564 goto streamon_err;
1565
1566 if (list_empty(&vout->dma_queue)) {
1567 ret = -EIO;
1568 goto streamon_err1;
1569 }
1570
1571 /* Get the next frame from the buffer queue */
1572 vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
1573 struct videobuf_buffer, queue);
1574 /* Remove buffer from the buffer queue */
1575 list_del(&vout->cur_frm->queue);
1576 /* Mark state of the current frame to active */
1577 vout->cur_frm->state = VIDEOBUF_ACTIVE;
1578 /* Initialize field_id and started member */
1579 vout->field_id = 0;
1580
1581 /* set flag here. Next QBUF will start DMA */
160ac0ba 1582 vout->streaming = true;
5c7ab634
VH
1583
1584 vout->first_int = 1;
1585
1586 if (omap_vout_calculate_offset(vout)) {
1587 ret = -EINVAL;
1588 goto streamon_err1;
1589 }
1590 addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
1591 + vout->cropped_offset;
1592
5251dd6c
AJ
1593 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1594 | DISPC_IRQ_VSYNC2;
5c7ab634 1595
5c7ab634
VH
1596 /* First save the configuration in ovelray structure */
1597 ret = omapvid_init(vout, addr);
15170025 1598 if (ret) {
5c7ab634
VH
1599 v4l2_err(&vout->vid_dev->v4l2_dev,
1600 "failed to set overlay info\n");
15170025
FF
1601 goto streamon_err1;
1602 }
1603
1604 omap_dispc_register_isr(omap_vout_isr, vout, mask);
1605
5c7ab634
VH
1606 /* Enable the pipeline and set the Go bit */
1607 ret = omapvid_apply_changes(vout);
1608 if (ret)
1609 v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
1610
aaa874a9
TV
1611 for (j = 0; j < ovid->num_overlays; j++) {
1612 struct omap_overlay *ovl = ovid->overlays[j];
d629f03f 1613 struct omap_dss_device *dssdev = ovl->get_device(ovl);
aaa874a9 1614
d629f03f 1615 if (dssdev) {
aaa874a9
TV
1616 ret = ovl->enable(ovl);
1617 if (ret)
1618 goto streamon_err1;
1619 }
1620 }
1621
5c7ab634
VH
1622 ret = 0;
1623
1624streamon_err1:
1625 if (ret)
1626 ret = videobuf_streamoff(q);
1627streamon_err:
1628 mutex_unlock(&vout->lock);
1629 return ret;
1630}
1631
1632static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1633{
1634 u32 mask = 0;
1635 int ret = 0, j;
1636 struct omap_vout_device *vout = fh;
1637 struct omapvideo_info *ovid = &vout->vid_info;
1638
1639 if (!vout->streaming)
1640 return -EINVAL;
1641
160ac0ba 1642 vout->streaming = false;
5251dd6c
AJ
1643 mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
1644 | DISPC_IRQ_VSYNC2;
5c7ab634
VH
1645
1646 omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
1647
1648 for (j = 0; j < ovid->num_overlays; j++) {
1649 struct omap_overlay *ovl = ovid->overlays[j];
d629f03f 1650 struct omap_dss_device *dssdev = ovl->get_device(ovl);
5c7ab634 1651
d629f03f 1652 if (dssdev)
aaa874a9 1653 ovl->disable(ovl);
5c7ab634
VH
1654 }
1655
1656 /* Turn of the pipeline */
1657 ret = omapvid_apply_changes(vout);
1658 if (ret)
bc39030b
MCC
1659 v4l2_err(&vout->vid_dev->v4l2_dev,
1660 "failed to change mode in streamoff\n");
5c7ab634
VH
1661
1662 INIT_LIST_HEAD(&vout->dma_queue);
1663 ret = videobuf_streamoff(&vout->vbq);
1664
1665 return ret;
1666}
1667
1668static int vidioc_s_fbuf(struct file *file, void *fh,
e6eb28c2 1669 const struct v4l2_framebuffer *a)
5c7ab634
VH
1670{
1671 int enable = 0;
1672 struct omap_overlay *ovl;
1673 struct omapvideo_info *ovid;
1674 struct omap_vout_device *vout = fh;
1675 struct omap_overlay_manager_info info;
1676 enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1677
1678 ovid = &vout->vid_info;
1679 ovl = ovid->overlays[0];
1680
1681 /* OMAP DSS doesn't support Source and Destination color
1682 key together */
1683 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
1684 (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
1685 return -EINVAL;
1686 /* OMAP DSS Doesn't support the Destination color key
1687 and alpha blending together */
1688 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
1689 (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
1690 return -EINVAL;
1691
1692 if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
1693 vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1694 key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
1695 } else
1696 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1697
1698 if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
1699 vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1700 key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
1701 } else
1702 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
1703
1704 if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
1705 V4L2_FBUF_FLAG_SRC_CHROMAKEY))
1706 enable = 1;
1707 else
1708 enable = 0;
1709 if (ovl->manager && ovl->manager->get_manager_info &&
1710 ovl->manager->set_manager_info) {
1711
1712 ovl->manager->get_manager_info(ovl->manager, &info);
1713 info.trans_enabled = enable;
1714 info.trans_key_type = key_type;
1715 info.trans_key = vout->win.chromakey;
1716
1717 if (ovl->manager->set_manager_info(ovl->manager, &info))
1718 return -EINVAL;
1719 }
1720 if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
1721 vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1722 enable = 1;
1723 } else {
1724 vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
1725 enable = 0;
1726 }
1727 if (ovl->manager && ovl->manager->get_manager_info &&
1728 ovl->manager->set_manager_info) {
1729 ovl->manager->get_manager_info(ovl->manager, &info);
11354dd5
AT
1730 /* enable this only if there is no zorder cap */
1731 if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1732 info.partial_alpha_enabled = enable;
5c7ab634
VH
1733 if (ovl->manager->set_manager_info(ovl->manager, &info))
1734 return -EINVAL;
1735 }
1736
1737 return 0;
1738}
1739
1740static int vidioc_g_fbuf(struct file *file, void *fh,
1741 struct v4l2_framebuffer *a)
1742{
1743 struct omap_overlay *ovl;
1744 struct omapvideo_info *ovid;
1745 struct omap_vout_device *vout = fh;
1746 struct omap_overlay_manager_info info;
1747
1748 ovid = &vout->vid_info;
1749 ovl = ovid->overlays[0];
1750
047a01fd
HV
1751 /* The video overlay must stay within the framebuffer and can't be
1752 positioned independently. */
1753 a->flags = V4L2_FBUF_FLAG_OVERLAY;
5c7ab634
VH
1754 a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
1755 | V4L2_FBUF_CAP_SRC_CHROMAKEY;
1756
1757 if (ovl->manager && ovl->manager->get_manager_info) {
1758 ovl->manager->get_manager_info(ovl->manager, &info);
1759 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
1760 a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1761 if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
1762 a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1763 }
1764 if (ovl->manager && ovl->manager->get_manager_info) {
1765 ovl->manager->get_manager_info(ovl->manager, &info);
11354dd5 1766 if (info.partial_alpha_enabled)
5c7ab634
VH
1767 a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1768 }
1769
1770 return 0;
1771}
1772
1773static const struct v4l2_ioctl_ops vout_ioctl_ops = {
1774 .vidioc_querycap = vidioc_querycap,
1775 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1776 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
1777 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
1778 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
5c7ab634
VH
1779 .vidioc_s_fbuf = vidioc_s_fbuf,
1780 .vidioc_g_fbuf = vidioc_g_fbuf,
7b2607c9
LP
1781 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
1782 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
1783 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
d1d09461
HV
1784 .vidioc_g_selection = vidioc_g_selection,
1785 .vidioc_s_selection = vidioc_s_selection,
5c7ab634
VH
1786 .vidioc_reqbufs = vidioc_reqbufs,
1787 .vidioc_querybuf = vidioc_querybuf,
1788 .vidioc_qbuf = vidioc_qbuf,
1789 .vidioc_dqbuf = vidioc_dqbuf,
1790 .vidioc_streamon = vidioc_streamon,
1791 .vidioc_streamoff = vidioc_streamoff,
1792};
1793
1794static const struct v4l2_file_operations omap_vout_fops = {
1795 .owner = THIS_MODULE,
94f3f48f 1796 .poll = omap_vout_poll,
5c7ab634
VH
1797 .unlocked_ioctl = video_ioctl2,
1798 .mmap = omap_vout_mmap,
1799 .open = omap_vout_open,
1800 .release = omap_vout_release,
1801};
1802
1803/* Init functions used during driver initialization */
1804/* Initial setup of video_data */
1805static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
1806{
1807 struct video_device *vfd;
1808 struct v4l2_pix_format *pix;
d629f03f
AT
1809 struct omap_overlay *ovl = vout->vid_info.overlays[0];
1810 struct omap_dss_device *display = ovl->get_device(ovl);
77430f03 1811 struct v4l2_ctrl_handler *hdl;
5c7ab634
VH
1812
1813 /* set the default pix */
1814 pix = &vout->pix;
1815
1816 /* Set the default picture of QVGA */
1817 pix->width = QQVGA_WIDTH;
1818 pix->height = QQVGA_HEIGHT;
1819
1820 /* Default pixel format is RGB 5-6-5 */
1821 pix->pixelformat = V4L2_PIX_FMT_RGB565;
1822 pix->field = V4L2_FIELD_ANY;
1823 pix->bytesperline = pix->width * 2;
1824 pix->sizeimage = pix->bytesperline * pix->height;
5c7ab634
VH
1825 pix->colorspace = V4L2_COLORSPACE_JPEG;
1826
1827 vout->bpp = RGB565_BPP;
1828 vout->fbuf.fmt.width = display->panel.timings.x_res;
1829 vout->fbuf.fmt.height = display->panel.timings.y_res;
1830
1831 /* Set the data structures for the overlay parameters*/
1832 vout->win.global_alpha = 255;
1833 vout->fbuf.flags = 0;
1834 vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
1835 V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
1836 vout->win.chromakey = 0;
1837
1838 omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
1839
77430f03
HV
1840 hdl = &vout->ctrl_handler;
1841 v4l2_ctrl_handler_init(hdl, 3);
1842 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1843 V4L2_CID_ROTATE, 0, 270, 90, 0);
1844 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1845 V4L2_CID_BG_COLOR, 0, 0xffffff, 1, 0);
1846 v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
1847 V4L2_CID_VFLIP, 0, 1, 1, 0);
1848 if (hdl->error)
1849 return hdl->error;
1850
5c7ab634 1851 vout->rotation = 0;
160ac0ba 1852 vout->mirror = false;
445e258f
AT
1853 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
1854 vout->vrfb_bpp = 2;
5c7ab634 1855
5c7ab634
VH
1856 /* initialize the video_device struct */
1857 vfd = vout->vfd = video_device_alloc();
1858
1859 if (!vfd) {
bc39030b
MCC
1860 printk(KERN_ERR VOUT_NAME
1861 ": could not allocate video device struct\n");
77430f03 1862 v4l2_ctrl_handler_free(hdl);
5c7ab634
VH
1863 return -ENOMEM;
1864 }
77430f03 1865 vfd->ctrl_handler = hdl;
5c7ab634
VH
1866 vfd->release = video_device_release;
1867 vfd->ioctl_ops = &vout_ioctl_ops;
1868
1869 strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
1870
5c7ab634
VH
1871 vfd->fops = &omap_vout_fops;
1872 vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
954f340f 1873 vfd->vfl_dir = VFL_DIR_TX;
5c7ab634
VH
1874 mutex_init(&vout->lock);
1875
1876 vfd->minor = -1;
1877 return 0;
1878
1879}
1880
1881/* Setup video buffers */
1882static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
1883 int vid_num)
1884{
1885 u32 numbuffers;
445e258f
AT
1886 int ret = 0, i;
1887 struct omapvideo_info *ovid;
5c7ab634 1888 struct omap_vout_device *vout;
5c7ab634
VH
1889 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1890 struct omap2video_device *vid_dev =
1891 container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
1892
1893 vout = vid_dev->vouts[vid_num];
445e258f 1894 ovid = &vout->vid_info;
5c7ab634
VH
1895
1896 numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
1897 vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
1898 dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
1899
1900 for (i = 0; i < numbuffers; i++) {
1901 vout->buf_virt_addr[i] =
1902 omap_vout_alloc_buffer(vout->buffer_size,
1903 (u32 *) &vout->buf_phy_addr[i]);
1904 if (!vout->buf_virt_addr[i]) {
1905 numbuffers = i;
1906 ret = -ENOMEM;
1907 goto free_buffers;
1908 }
1909 }
1910
5c7ab634
VH
1911 vout->cropped_offset = 0;
1912
445e258f 1913 if (ovid->rotation_type == VOUT_ROT_VRFB) {
ac0b2b30 1914 bool static_vrfb_allocation = (vid_num == 0) ?
445e258f
AT
1915 vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
1916 ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
1917 static_vrfb_allocation);
5c7ab634 1918 }
5c7ab634 1919
445e258f 1920 return ret;
5c7ab634
VH
1921
1922free_buffers:
1923 for (i = 0; i < numbuffers; i++) {
1924 omap_vout_free_buffer(vout->buf_virt_addr[i],
1925 vout->buffer_size);
1926 vout->buf_virt_addr[i] = 0;
1927 vout->buf_phy_addr[i] = 0;
1928 }
1929 return ret;
1930
1931}
1932
1933/* Create video out devices */
1934static int __init omap_vout_create_video_devices(struct platform_device *pdev)
1935{
1936 int ret = 0, k;
1937 struct omap_vout_device *vout;
1938 struct video_device *vfd = NULL;
1939 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1940 struct omap2video_device *vid_dev = container_of(v4l2_dev,
1941 struct omap2video_device, v4l2_dev);
1942
1943 for (k = 0; k < pdev->num_resources; k++) {
1944
2ef17c9f 1945 vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
5c7ab634
VH
1946 if (!vout) {
1947 dev_err(&pdev->dev, ": could not allocate memory\n");
1948 return -ENOMEM;
1949 }
5c7ab634
VH
1950
1951 vout->vid = k;
1952 vid_dev->vouts[k] = vout;
1953 vout->vid_dev = vid_dev;
1954 /* Select video2 if only 1 overlay is controlled by V4L2 */
1955 if (pdev->num_resources == 1)
1956 vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
1957 else
1958 /* Else select video1 and video2 one by one. */
1959 vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
1960 vout->vid_info.num_overlays = 1;
1961 vout->vid_info.id = k + 1;
1962
445e258f 1963 /* Set VRFB as rotation_type for omap2 and omap3 */
950e2fb4 1964 if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
445e258f
AT
1965 vout->vid_info.rotation_type = VOUT_ROT_VRFB;
1966
5c7ab634
VH
1967 /* Setup the default configuration for the video devices
1968 */
1969 if (omap_vout_setup_video_data(vout) != 0) {
1970 ret = -ENOMEM;
1971 goto error;
1972 }
1973
1974 /* Allocate default number of buffers for the video streaming
1975 * and reserve the VRFB space for rotation
1976 */
1977 if (omap_vout_setup_video_bufs(pdev, k) != 0) {
1978 ret = -ENOMEM;
1979 goto error1;
1980 }
1981
1982 /* Register the Video device with V4L2
1983 */
1984 vfd = vout->vfd;
8f3a307b 1985 if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
bc39030b
MCC
1986 dev_err(&pdev->dev,
1987 ": Could not register Video for Linux device\n");
5c7ab634
VH
1988 vfd->minor = -1;
1989 ret = -ENODEV;
1990 goto error2;
1991 }
1992 video_set_drvdata(vfd, vout);
1993
bc39030b
MCC
1994 dev_info(&pdev->dev,
1995 ": registered and initialized video device %d\n",
1996 vfd->minor);
32d640de
AT
1997 if (k == (pdev->num_resources - 1))
1998 return 0;
5c7ab634 1999
32d640de 2000 continue;
5c7ab634 2001error2:
445e258f
AT
2002 if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
2003 omap_vout_release_vrfb(vout);
5c7ab634
VH
2004 omap_vout_free_buffers(vout);
2005error1:
2006 video_device_release(vfd);
2007error:
2008 kfree(vout);
2009 return ret;
5c7ab634
VH
2010 }
2011
2012 return -ENODEV;
2013}
2014/* Driver functions */
2015static void omap_vout_cleanup_device(struct omap_vout_device *vout)
2016{
2017 struct video_device *vfd;
445e258f 2018 struct omapvideo_info *ovid;
5c7ab634
VH
2019
2020 if (!vout)
2021 return;
2022
2023 vfd = vout->vfd;
445e258f 2024 ovid = &vout->vid_info;
5c7ab634
VH
2025 if (vfd) {
2026 if (!video_is_registered(vfd)) {
2027 /*
2028 * The device was never registered, so release the
2029 * video_device struct directly.
2030 */
2031 video_device_release(vfd);
2032 } else {
2033 /*
2034 * The unregister function will release the video_device
2035 * struct as well as unregistering it.
2036 */
2037 video_unregister_device(vfd);
2038 }
2039 }
77430f03 2040 v4l2_ctrl_handler_free(&vout->ctrl_handler);
445e258f
AT
2041 if (ovid->rotation_type == VOUT_ROT_VRFB) {
2042 omap_vout_release_vrfb(vout);
2043 /* Free the VRFB buffer if allocated
2044 * init time
2045 */
2046 if (vout->vrfb_static_allocation)
2047 omap_vout_free_vrfb_buffers(vout);
2048 }
5c7ab634 2049 omap_vout_free_buffers(vout);
5c7ab634
VH
2050
2051 kfree(vout);
2052}
2053
2054static int omap_vout_remove(struct platform_device *pdev)
2055{
2056 int k;
2057 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
2058 struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
2059 omap2video_device, v4l2_dev);
2060
2061 v4l2_device_unregister(v4l2_dev);
2062 for (k = 0; k < pdev->num_resources; k++)
2063 omap_vout_cleanup_device(vid_dev->vouts[k]);
2064
2065 for (k = 0; k < vid_dev->num_displays; k++) {
2066 if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
5ba9bb0e 2067 vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
5c7ab634
VH
2068
2069 omap_dss_put_device(vid_dev->displays[k]);
2070 }
2071 kfree(vid_dev);
2072 return 0;
2073}
2074
2075static int __init omap_vout_probe(struct platform_device *pdev)
2076{
2077 int ret = 0, i;
2078 struct omap_overlay *ovl;
2079 struct omap_dss_device *dssdev = NULL;
2080 struct omap_dss_device *def_display;
2081 struct omap2video_device *vid_dev = NULL;
2082
591a0ac7
TV
2083 if (omapdss_is_initialized() == false)
2084 return -EPROBE_DEFER;
2085
a9ee9f08
TV
2086 ret = omapdss_compat_init();
2087 if (ret) {
2088 dev_err(&pdev->dev, "failed to init dss\n");
2089 return ret;
2090 }
2091
5c7ab634
VH
2092 if (pdev->num_resources == 0) {
2093 dev_err(&pdev->dev, "probed for an unknown device\n");
a9ee9f08
TV
2094 ret = -ENODEV;
2095 goto err_dss_init;
5c7ab634
VH
2096 }
2097
2098 vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
a9ee9f08
TV
2099 if (vid_dev == NULL) {
2100 ret = -ENOMEM;
2101 goto err_dss_init;
2102 }
5c7ab634
VH
2103
2104 vid_dev->num_displays = 0;
2105 for_each_dss_dev(dssdev) {
2106 omap_dss_get_device(dssdev);
71c7a972
TV
2107
2108 if (!dssdev->driver) {
2109 dev_warn(&pdev->dev, "no driver for display: %s\n",
2110 dssdev->name);
2111 omap_dss_put_device(dssdev);
2112 continue;
2113 }
2114
5c7ab634
VH
2115 vid_dev->displays[vid_dev->num_displays++] = dssdev;
2116 }
2117
2118 if (vid_dev->num_displays == 0) {
2119 dev_err(&pdev->dev, "no displays\n");
2120 ret = -EINVAL;
2121 goto probe_err0;
2122 }
2123
2124 vid_dev->num_overlays = omap_dss_get_num_overlays();
2125 for (i = 0; i < vid_dev->num_overlays; i++)
2126 vid_dev->overlays[i] = omap_dss_get_overlay(i);
2127
2128 vid_dev->num_managers = omap_dss_get_num_overlay_managers();
2129 for (i = 0; i < vid_dev->num_managers; i++)
2130 vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
2131
2132 /* Get the Video1 overlay and video2 overlay.
2133 * Setup the Display attached to that overlays
2134 */
2135 for (i = 1; i < vid_dev->num_overlays; i++) {
2136 ovl = omap_dss_get_overlay(i);
d629f03f
AT
2137 dssdev = ovl->get_device(ovl);
2138
2139 if (dssdev) {
2140 def_display = dssdev;
5c7ab634
VH
2141 } else {
2142 dev_warn(&pdev->dev, "cannot find display\n");
2143 def_display = NULL;
2144 }
2145 if (def_display) {
5ba9bb0e
VH
2146 struct omap_dss_driver *dssdrv = def_display->driver;
2147
2148 ret = dssdrv->enable(def_display);
5c7ab634
VH
2149 if (ret) {
2150 /* Here we are not considering a error
2151 * as display may be enabled by frame
2152 * buffer driver
2153 */
2154 dev_warn(&pdev->dev,
2155 "'%s' Display already enabled\n",
2156 def_display->name);
2157 }
5c7ab634
VH
2158 }
2159 }
2160
2161 if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
2162 dev_err(&pdev->dev, "v4l2_device_register failed\n");
2163 ret = -ENODEV;
2164 goto probe_err1;
2165 }
2166
2167 ret = omap_vout_create_video_devices(pdev);
2168 if (ret)
2169 goto probe_err2;
2170
2171 for (i = 0; i < vid_dev->num_displays; i++) {
2172 struct omap_dss_device *display = vid_dev->displays[i];
2173
5ba9bb0e
VH
2174 if (display->driver->update)
2175 display->driver->update(display, 0, 0,
5c7ab634
VH
2176 display->panel.timings.x_res,
2177 display->panel.timings.y_res);
2178 }
2179 return 0;
2180
2181probe_err2:
2182 v4l2_device_unregister(&vid_dev->v4l2_dev);
2183probe_err1:
2184 for (i = 1; i < vid_dev->num_overlays; i++) {
2185 def_display = NULL;
2186 ovl = omap_dss_get_overlay(i);
d629f03f
AT
2187 dssdev = ovl->get_device(ovl);
2188
2189 if (dssdev)
2190 def_display = dssdev;
5c7ab634 2191
5ba9bb0e
VH
2192 if (def_display && def_display->driver)
2193 def_display->driver->disable(def_display);
5c7ab634
VH
2194 }
2195probe_err0:
2196 kfree(vid_dev);
a9ee9f08
TV
2197err_dss_init:
2198 omapdss_compat_uninit();
5c7ab634
VH
2199 return ret;
2200}
2201
2202static struct platform_driver omap_vout_driver = {
2203 .driver = {
2204 .name = VOUT_NAME,
2205 },
5c7ab634
VH
2206 .remove = omap_vout_remove,
2207};
2208
2209static int __init omap_vout_init(void)
2210{
93596ef7 2211 if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
5c7ab634
VH
2212 printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
2213 return -EINVAL;
2214 }
2215 return 0;
2216}
2217
2218static void omap_vout_cleanup(void)
2219{
2220 platform_driver_unregister(&omap_vout_driver);
2221}
2222
2223late_initcall(omap_vout_init);
2224module_exit(omap_vout_cleanup);