Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / vivid / vivid-vid-common.c
bloba651527d80db108b728c18de93369ee089a6b7eb
1 /*
2 * vivid-vid-common.c - common video support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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.
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-dv-timings.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-dv-timings.h>
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
32 const struct v4l2_dv_timings_cap vivid_dv_timings_cap = {
33 .type = V4L2_DV_BT_656_1120,
34 /* keep this initialization for compatibility with GCC < 4.4.6 */
35 .reserved = { 0 },
36 V4L2_INIT_BT_TIMINGS(0, MAX_WIDTH, 0, MAX_HEIGHT, 14000000, 775000000,
37 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
38 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
39 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
42 /* ------------------------------------------------------------------
43 Basic structures
44 ------------------------------------------------------------------*/
46 struct vivid_fmt vivid_formats[] = {
48 .fourcc = V4L2_PIX_FMT_YUYV,
49 .vdownsampling = { 1 },
50 .bit_depth = { 16 },
51 .color_enc = TGP_COLOR_ENC_YCBCR,
52 .planes = 1,
53 .buffers = 1,
54 .data_offset = { PLANE0_DATA_OFFSET },
57 .fourcc = V4L2_PIX_FMT_UYVY,
58 .vdownsampling = { 1 },
59 .bit_depth = { 16 },
60 .color_enc = TGP_COLOR_ENC_YCBCR,
61 .planes = 1,
62 .buffers = 1,
65 .fourcc = V4L2_PIX_FMT_YVYU,
66 .vdownsampling = { 1 },
67 .bit_depth = { 16 },
68 .color_enc = TGP_COLOR_ENC_YCBCR,
69 .planes = 1,
70 .buffers = 1,
73 .fourcc = V4L2_PIX_FMT_VYUY,
74 .vdownsampling = { 1 },
75 .bit_depth = { 16 },
76 .color_enc = TGP_COLOR_ENC_YCBCR,
77 .planes = 1,
78 .buffers = 1,
81 .fourcc = V4L2_PIX_FMT_YUV422P,
82 .vdownsampling = { 1, 1, 1 },
83 .bit_depth = { 8, 4, 4 },
84 .color_enc = TGP_COLOR_ENC_YCBCR,
85 .planes = 3,
86 .buffers = 1,
89 .fourcc = V4L2_PIX_FMT_YUV420,
90 .vdownsampling = { 1, 2, 2 },
91 .bit_depth = { 8, 4, 4 },
92 .color_enc = TGP_COLOR_ENC_YCBCR,
93 .planes = 3,
94 .buffers = 1,
97 .fourcc = V4L2_PIX_FMT_YVU420,
98 .vdownsampling = { 1, 2, 2 },
99 .bit_depth = { 8, 4, 4 },
100 .color_enc = TGP_COLOR_ENC_YCBCR,
101 .planes = 3,
102 .buffers = 1,
105 .fourcc = V4L2_PIX_FMT_NV12,
106 .vdownsampling = { 1, 2 },
107 .bit_depth = { 8, 8 },
108 .color_enc = TGP_COLOR_ENC_YCBCR,
109 .planes = 2,
110 .buffers = 1,
113 .fourcc = V4L2_PIX_FMT_NV21,
114 .vdownsampling = { 1, 2 },
115 .bit_depth = { 8, 8 },
116 .color_enc = TGP_COLOR_ENC_YCBCR,
117 .planes = 2,
118 .buffers = 1,
121 .fourcc = V4L2_PIX_FMT_NV16,
122 .vdownsampling = { 1, 1 },
123 .bit_depth = { 8, 8 },
124 .color_enc = TGP_COLOR_ENC_YCBCR,
125 .planes = 2,
126 .buffers = 1,
129 .fourcc = V4L2_PIX_FMT_NV61,
130 .vdownsampling = { 1, 1 },
131 .bit_depth = { 8, 8 },
132 .color_enc = TGP_COLOR_ENC_YCBCR,
133 .planes = 2,
134 .buffers = 1,
137 .fourcc = V4L2_PIX_FMT_NV24,
138 .vdownsampling = { 1, 1 },
139 .bit_depth = { 8, 16 },
140 .color_enc = TGP_COLOR_ENC_YCBCR,
141 .planes = 2,
142 .buffers = 1,
145 .fourcc = V4L2_PIX_FMT_NV42,
146 .vdownsampling = { 1, 1 },
147 .bit_depth = { 8, 16 },
148 .color_enc = TGP_COLOR_ENC_YCBCR,
149 .planes = 2,
150 .buffers = 1,
153 .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
154 .vdownsampling = { 1 },
155 .bit_depth = { 16 },
156 .planes = 1,
157 .buffers = 1,
158 .alpha_mask = 0x8000,
161 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
162 .vdownsampling = { 1 },
163 .bit_depth = { 16 },
164 .planes = 1,
165 .buffers = 1,
168 .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
169 .vdownsampling = { 1 },
170 .bit_depth = { 16 },
171 .planes = 1,
172 .buffers = 1,
173 .alpha_mask = 0xf000,
176 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
177 .vdownsampling = { 1 },
178 .bit_depth = { 32 },
179 .planes = 1,
180 .buffers = 1,
181 .alpha_mask = 0x000000ff,
184 .fourcc = V4L2_PIX_FMT_GREY,
185 .vdownsampling = { 1 },
186 .bit_depth = { 8 },
187 .color_enc = TGP_COLOR_ENC_LUMA,
188 .planes = 1,
189 .buffers = 1,
192 .fourcc = V4L2_PIX_FMT_Y10,
193 .vdownsampling = { 1 },
194 .bit_depth = { 16 },
195 .color_enc = TGP_COLOR_ENC_LUMA,
196 .planes = 1,
197 .buffers = 1,
200 .fourcc = V4L2_PIX_FMT_Y12,
201 .vdownsampling = { 1 },
202 .bit_depth = { 16 },
203 .color_enc = TGP_COLOR_ENC_LUMA,
204 .planes = 1,
205 .buffers = 1,
208 .fourcc = V4L2_PIX_FMT_Y16,
209 .vdownsampling = { 1 },
210 .bit_depth = { 16 },
211 .color_enc = TGP_COLOR_ENC_LUMA,
212 .planes = 1,
213 .buffers = 1,
216 .fourcc = V4L2_PIX_FMT_Y16_BE,
217 .vdownsampling = { 1 },
218 .bit_depth = { 16 },
219 .color_enc = TGP_COLOR_ENC_LUMA,
220 .planes = 1,
221 .buffers = 1,
224 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
225 .vdownsampling = { 1 },
226 .bit_depth = { 8 },
227 .planes = 1,
228 .buffers = 1,
231 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
232 .vdownsampling = { 1 },
233 .bit_depth = { 16 },
234 .planes = 1,
235 .buffers = 1,
236 .can_do_overlay = true,
239 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
240 .vdownsampling = { 1 },
241 .bit_depth = { 16 },
242 .planes = 1,
243 .buffers = 1,
244 .can_do_overlay = true,
247 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
248 .vdownsampling = { 1 },
249 .bit_depth = { 16 },
250 .planes = 1,
251 .buffers = 1,
254 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
255 .vdownsampling = { 1 },
256 .bit_depth = { 16 },
257 .planes = 1,
258 .buffers = 1,
261 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
262 .vdownsampling = { 1 },
263 .bit_depth = { 16 },
264 .planes = 1,
265 .buffers = 1,
266 .alpha_mask = 0x00f0,
269 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
270 .vdownsampling = { 1 },
271 .bit_depth = { 16 },
272 .planes = 1,
273 .buffers = 1,
274 .can_do_overlay = true,
277 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
278 .vdownsampling = { 1 },
279 .bit_depth = { 16 },
280 .planes = 1,
281 .buffers = 1,
282 .can_do_overlay = true,
285 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
286 .vdownsampling = { 1 },
287 .bit_depth = { 16 },
288 .planes = 1,
289 .buffers = 1,
290 .can_do_overlay = true,
291 .alpha_mask = 0x8000,
294 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
295 .vdownsampling = { 1 },
296 .bit_depth = { 16 },
297 .planes = 1,
298 .buffers = 1,
301 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
302 .vdownsampling = { 1 },
303 .bit_depth = { 16 },
304 .planes = 1,
305 .buffers = 1,
308 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
309 .vdownsampling = { 1 },
310 .bit_depth = { 16 },
311 .planes = 1,
312 .buffers = 1,
313 .alpha_mask = 0x0080,
316 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
317 .vdownsampling = { 1 },
318 .bit_depth = { 24 },
319 .planes = 1,
320 .buffers = 1,
323 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
324 .vdownsampling = { 1 },
325 .bit_depth = { 24 },
326 .planes = 1,
327 .buffers = 1,
330 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
331 .vdownsampling = { 1 },
332 .bit_depth = { 32 },
333 .planes = 1,
334 .buffers = 1,
337 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
338 .vdownsampling = { 1 },
339 .bit_depth = { 32 },
340 .planes = 1,
341 .buffers = 1,
344 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
345 .vdownsampling = { 1 },
346 .bit_depth = { 32 },
347 .planes = 1,
348 .buffers = 1,
351 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
352 .vdownsampling = { 1 },
353 .bit_depth = { 32 },
354 .planes = 1,
355 .buffers = 1,
358 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
359 .vdownsampling = { 1 },
360 .bit_depth = { 32 },
361 .planes = 1,
362 .buffers = 1,
365 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
366 .vdownsampling = { 1 },
367 .bit_depth = { 32 },
368 .planes = 1,
369 .buffers = 1,
370 .alpha_mask = 0x000000ff,
373 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
374 .vdownsampling = { 1 },
375 .bit_depth = { 32 },
376 .planes = 1,
377 .buffers = 1,
378 .alpha_mask = 0xff000000,
381 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
382 .vdownsampling = { 1 },
383 .bit_depth = { 8 },
384 .planes = 1,
385 .buffers = 1,
388 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
389 .vdownsampling = { 1 },
390 .bit_depth = { 8 },
391 .planes = 1,
392 .buffers = 1,
395 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
396 .vdownsampling = { 1 },
397 .bit_depth = { 8 },
398 .planes = 1,
399 .buffers = 1,
402 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
403 .vdownsampling = { 1 },
404 .bit_depth = { 8 },
405 .planes = 1,
406 .buffers = 1,
409 .fourcc = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
410 .vdownsampling = { 1 },
411 .bit_depth = { 16 },
412 .planes = 1,
413 .buffers = 1,
416 .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
417 .vdownsampling = { 1 },
418 .bit_depth = { 16 },
419 .planes = 1,
420 .buffers = 1,
423 .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
424 .vdownsampling = { 1 },
425 .bit_depth = { 16 },
426 .planes = 1,
427 .buffers = 1,
430 .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
431 .vdownsampling = { 1 },
432 .bit_depth = { 16 },
433 .planes = 1,
434 .buffers = 1,
437 .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
438 .vdownsampling = { 1 },
439 .bit_depth = { 16 },
440 .planes = 1,
441 .buffers = 1,
444 .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
445 .vdownsampling = { 1 },
446 .bit_depth = { 16 },
447 .planes = 1,
448 .buffers = 1,
451 .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
452 .vdownsampling = { 1 },
453 .bit_depth = { 16 },
454 .planes = 1,
455 .buffers = 1,
458 .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
459 .vdownsampling = { 1 },
460 .bit_depth = { 16 },
461 .planes = 1,
462 .buffers = 1,
465 .fourcc = V4L2_PIX_FMT_HSV24, /* HSV 24bits */
466 .color_enc = TGP_COLOR_ENC_HSV,
467 .vdownsampling = { 1 },
468 .bit_depth = { 24 },
469 .planes = 1,
470 .buffers = 1,
473 .fourcc = V4L2_PIX_FMT_HSV32, /* HSV 32bits */
474 .color_enc = TGP_COLOR_ENC_HSV,
475 .vdownsampling = { 1 },
476 .bit_depth = { 32 },
477 .planes = 1,
478 .buffers = 1,
481 /* Multiplanar formats */
484 .fourcc = V4L2_PIX_FMT_NV16M,
485 .vdownsampling = { 1, 1 },
486 .bit_depth = { 8, 8 },
487 .color_enc = TGP_COLOR_ENC_YCBCR,
488 .planes = 2,
489 .buffers = 2,
490 .data_offset = { PLANE0_DATA_OFFSET, 0 },
493 .fourcc = V4L2_PIX_FMT_NV61M,
494 .vdownsampling = { 1, 1 },
495 .bit_depth = { 8, 8 },
496 .color_enc = TGP_COLOR_ENC_YCBCR,
497 .planes = 2,
498 .buffers = 2,
499 .data_offset = { 0, PLANE0_DATA_OFFSET },
502 .fourcc = V4L2_PIX_FMT_YUV420M,
503 .vdownsampling = { 1, 2, 2 },
504 .bit_depth = { 8, 4, 4 },
505 .color_enc = TGP_COLOR_ENC_YCBCR,
506 .planes = 3,
507 .buffers = 3,
510 .fourcc = V4L2_PIX_FMT_YVU420M,
511 .vdownsampling = { 1, 2, 2 },
512 .bit_depth = { 8, 4, 4 },
513 .color_enc = TGP_COLOR_ENC_YCBCR,
514 .planes = 3,
515 .buffers = 3,
518 .fourcc = V4L2_PIX_FMT_NV12M,
519 .vdownsampling = { 1, 2 },
520 .bit_depth = { 8, 8 },
521 .color_enc = TGP_COLOR_ENC_YCBCR,
522 .planes = 2,
523 .buffers = 2,
526 .fourcc = V4L2_PIX_FMT_NV21M,
527 .vdownsampling = { 1, 2 },
528 .bit_depth = { 8, 8 },
529 .color_enc = TGP_COLOR_ENC_YCBCR,
530 .planes = 2,
531 .buffers = 2,
534 .fourcc = V4L2_PIX_FMT_YUV422M,
535 .vdownsampling = { 1, 1, 1 },
536 .bit_depth = { 8, 4, 4 },
537 .color_enc = TGP_COLOR_ENC_YCBCR,
538 .planes = 3,
539 .buffers = 3,
542 .fourcc = V4L2_PIX_FMT_YVU422M,
543 .vdownsampling = { 1, 1, 1 },
544 .bit_depth = { 8, 4, 4 },
545 .color_enc = TGP_COLOR_ENC_YCBCR,
546 .planes = 3,
547 .buffers = 3,
550 .fourcc = V4L2_PIX_FMT_YUV444M,
551 .vdownsampling = { 1, 1, 1 },
552 .bit_depth = { 8, 8, 8 },
553 .color_enc = TGP_COLOR_ENC_YCBCR,
554 .planes = 3,
555 .buffers = 3,
558 .fourcc = V4L2_PIX_FMT_YVU444M,
559 .vdownsampling = { 1, 1, 1 },
560 .bit_depth = { 8, 8, 8 },
561 .color_enc = TGP_COLOR_ENC_YCBCR,
562 .planes = 3,
563 .buffers = 3,
567 /* There are this many multiplanar formats in the list */
568 #define VIVID_MPLANAR_FORMATS 10
570 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
572 const struct vivid_fmt *fmt;
573 unsigned k;
575 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
576 fmt = &vivid_formats[k];
577 if (fmt->fourcc == pixelformat)
578 if (fmt->buffers == 1 || dev->multiplanar)
579 return fmt;
582 return NULL;
585 bool vivid_vid_can_loop(struct vivid_dev *dev)
587 if (dev->src_rect.width != dev->sink_rect.width ||
588 dev->src_rect.height != dev->sink_rect.height)
589 return false;
590 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
591 return false;
592 if (dev->field_cap != dev->field_out)
593 return false;
595 * While this can be supported, it is just too much work
596 * to actually implement.
598 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
599 dev->field_cap == V4L2_FIELD_SEQ_BT)
600 return false;
601 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
602 if (!(dev->std_cap & V4L2_STD_525_60) !=
603 !(dev->std_out & V4L2_STD_525_60))
604 return false;
605 return true;
607 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
608 return true;
609 return false;
612 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
614 struct v4l2_event ev = {
615 .type = V4L2_EVENT_SOURCE_CHANGE,
616 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
618 unsigned i;
620 for (i = 0; i < dev->num_inputs; i++) {
621 ev.id = i;
622 if (dev->input_type[i] == type) {
623 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
624 v4l2_event_queue(&dev->vid_cap_dev, &ev);
625 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
626 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
632 * Conversion function that converts a single-planar format to a
633 * single-plane multiplanar format.
635 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
637 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
638 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
639 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
640 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
642 memset(mp->reserved, 0, sizeof(mp->reserved));
643 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
644 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
645 mp->width = pix->width;
646 mp->height = pix->height;
647 mp->pixelformat = pix->pixelformat;
648 mp->field = pix->field;
649 mp->colorspace = pix->colorspace;
650 mp->xfer_func = pix->xfer_func;
651 /* Also copies hsv_enc */
652 mp->ycbcr_enc = pix->ycbcr_enc;
653 mp->quantization = pix->quantization;
654 mp->num_planes = 1;
655 mp->flags = pix->flags;
656 ppix->sizeimage = pix->sizeimage;
657 ppix->bytesperline = pix->bytesperline;
658 memset(ppix->reserved, 0, sizeof(ppix->reserved));
661 int fmt_sp2mp_func(struct file *file, void *priv,
662 struct v4l2_format *f, fmtfunc func)
664 struct v4l2_format fmt;
665 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
666 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
667 struct v4l2_pix_format *pix = &f->fmt.pix;
668 int ret;
670 /* Converts to a mplane format */
671 fmt_sp2mp(f, &fmt);
672 /* Passes it to the generic mplane format function */
673 ret = func(file, priv, &fmt);
674 /* Copies back the mplane data to the single plane format */
675 pix->width = mp->width;
676 pix->height = mp->height;
677 pix->pixelformat = mp->pixelformat;
678 pix->field = mp->field;
679 pix->colorspace = mp->colorspace;
680 pix->xfer_func = mp->xfer_func;
681 /* Also copies hsv_enc */
682 pix->ycbcr_enc = mp->ycbcr_enc;
683 pix->quantization = mp->quantization;
684 pix->sizeimage = ppix->sizeimage;
685 pix->bytesperline = ppix->bytesperline;
686 pix->flags = mp->flags;
687 return ret;
690 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
692 unsigned w = r->width;
693 unsigned h = r->height;
695 /* sanitize w and h in case someone passes ~0 as the value */
696 w &= 0xffff;
697 h &= 0xffff;
698 if (!(flags & V4L2_SEL_FLAG_LE)) {
699 w++;
700 h++;
701 if (w < 2)
702 w = 2;
703 if (h < 2)
704 h = 2;
706 if (!(flags & V4L2_SEL_FLAG_GE)) {
707 if (w > MAX_WIDTH)
708 w = MAX_WIDTH;
709 if (h > MAX_HEIGHT)
710 h = MAX_HEIGHT;
712 w = w & ~1;
713 h = h & ~1;
714 if (w < 2 || h < 2)
715 return -ERANGE;
716 if (w > MAX_WIDTH || h > MAX_HEIGHT)
717 return -ERANGE;
718 if (r->top < 0)
719 r->top = 0;
720 if (r->left < 0)
721 r->left = 0;
722 /* sanitize left and top in case someone passes ~0 as the value */
723 r->left &= 0xfffe;
724 r->top &= 0xfffe;
725 if (r->left + w > MAX_WIDTH)
726 r->left = MAX_WIDTH - w;
727 if (r->top + h > MAX_HEIGHT)
728 r->top = MAX_HEIGHT - h;
729 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
730 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
731 (r->width != w || r->height != h))
732 return -ERANGE;
733 r->width = w;
734 r->height = h;
735 return 0;
738 int vivid_enum_fmt_vid(struct file *file, void *priv,
739 struct v4l2_fmtdesc *f)
741 struct vivid_dev *dev = video_drvdata(file);
742 const struct vivid_fmt *fmt;
744 if (f->index >= ARRAY_SIZE(vivid_formats) -
745 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
746 return -EINVAL;
748 fmt = &vivid_formats[f->index];
750 f->pixelformat = fmt->fourcc;
751 return 0;
754 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
755 struct v4l2_fmtdesc *f)
757 struct vivid_dev *dev = video_drvdata(file);
759 if (!dev->multiplanar)
760 return -ENOTTY;
761 return vivid_enum_fmt_vid(file, priv, f);
764 int vidioc_enum_fmt_vid(struct file *file, void *priv,
765 struct v4l2_fmtdesc *f)
767 struct vivid_dev *dev = video_drvdata(file);
769 if (dev->multiplanar)
770 return -ENOTTY;
771 return vivid_enum_fmt_vid(file, priv, f);
774 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
776 struct vivid_dev *dev = video_drvdata(file);
777 struct video_device *vdev = video_devdata(file);
779 if (vdev->vfl_dir == VFL_DIR_RX) {
780 if (!vivid_is_sdtv_cap(dev))
781 return -ENODATA;
782 *id = dev->std_cap;
783 } else {
784 if (!vivid_is_svid_out(dev))
785 return -ENODATA;
786 *id = dev->std_out;
788 return 0;
791 int vidioc_g_dv_timings(struct file *file, void *_fh,
792 struct v4l2_dv_timings *timings)
794 struct vivid_dev *dev = video_drvdata(file);
795 struct video_device *vdev = video_devdata(file);
797 if (vdev->vfl_dir == VFL_DIR_RX) {
798 if (!vivid_is_hdmi_cap(dev))
799 return -ENODATA;
800 *timings = dev->dv_timings_cap;
801 } else {
802 if (!vivid_is_hdmi_out(dev))
803 return -ENODATA;
804 *timings = dev->dv_timings_out;
806 return 0;
809 int vidioc_enum_dv_timings(struct file *file, void *_fh,
810 struct v4l2_enum_dv_timings *timings)
812 struct vivid_dev *dev = video_drvdata(file);
813 struct video_device *vdev = video_devdata(file);
815 if (vdev->vfl_dir == VFL_DIR_RX) {
816 if (!vivid_is_hdmi_cap(dev))
817 return -ENODATA;
818 } else {
819 if (!vivid_is_hdmi_out(dev))
820 return -ENODATA;
822 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
823 NULL, NULL);
826 int vidioc_dv_timings_cap(struct file *file, void *_fh,
827 struct v4l2_dv_timings_cap *cap)
829 struct vivid_dev *dev = video_drvdata(file);
830 struct video_device *vdev = video_devdata(file);
832 if (vdev->vfl_dir == VFL_DIR_RX) {
833 if (!vivid_is_hdmi_cap(dev))
834 return -ENODATA;
835 } else {
836 if (!vivid_is_hdmi_out(dev))
837 return -ENODATA;
839 *cap = vivid_dv_timings_cap;
840 return 0;
843 int vidioc_g_edid(struct file *file, void *_fh,
844 struct v4l2_edid *edid)
846 struct vivid_dev *dev = video_drvdata(file);
847 struct video_device *vdev = video_devdata(file);
848 struct cec_adapter *adap;
850 memset(edid->reserved, 0, sizeof(edid->reserved));
851 if (vdev->vfl_dir == VFL_DIR_RX) {
852 if (edid->pad >= dev->num_inputs)
853 return -EINVAL;
854 if (dev->input_type[edid->pad] != HDMI)
855 return -EINVAL;
856 adap = dev->cec_rx_adap;
857 } else {
858 unsigned int bus_idx;
860 if (edid->pad >= dev->num_outputs)
861 return -EINVAL;
862 if (dev->output_type[edid->pad] != HDMI)
863 return -EINVAL;
864 bus_idx = dev->cec_output2bus_map[edid->pad];
865 adap = dev->cec_tx_adap[bus_idx];
867 if (edid->start_block == 0 && edid->blocks == 0) {
868 edid->blocks = dev->edid_blocks;
869 return 0;
871 if (dev->edid_blocks == 0)
872 return -ENODATA;
873 if (edid->start_block >= dev->edid_blocks)
874 return -EINVAL;
875 if (edid->start_block + edid->blocks > dev->edid_blocks)
876 edid->blocks = dev->edid_blocks - edid->start_block;
877 cec_set_edid_phys_addr(dev->edid, dev->edid_blocks * 128, adap->phys_addr);
878 memcpy(edid->edid, dev->edid + edid->start_block * 128, edid->blocks * 128);
879 return 0;