[media] media/vivid: Add support for Y16_BE format
[deliverable/linux.git] / drivers / media / platform / vivid / vivid-vid-common.c
1 /*
2 * vivid-vid-common.c - common video support functions.
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/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>
28
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31
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)
40 };
41
42 /* ------------------------------------------------------------------
43 Basic structures
44 ------------------------------------------------------------------*/
45
46 struct vivid_fmt vivid_formats[] = {
47 {
48 .fourcc = V4L2_PIX_FMT_YUYV,
49 .vdownsampling = { 1 },
50 .bit_depth = { 16 },
51 .is_yuv = true,
52 .planes = 1,
53 .buffers = 1,
54 .data_offset = { PLANE0_DATA_OFFSET },
55 },
56 {
57 .fourcc = V4L2_PIX_FMT_UYVY,
58 .vdownsampling = { 1 },
59 .bit_depth = { 16 },
60 .is_yuv = true,
61 .planes = 1,
62 .buffers = 1,
63 },
64 {
65 .fourcc = V4L2_PIX_FMT_YVYU,
66 .vdownsampling = { 1 },
67 .bit_depth = { 16 },
68 .is_yuv = true,
69 .planes = 1,
70 .buffers = 1,
71 },
72 {
73 .fourcc = V4L2_PIX_FMT_VYUY,
74 .vdownsampling = { 1 },
75 .bit_depth = { 16 },
76 .is_yuv = true,
77 .planes = 1,
78 .buffers = 1,
79 },
80 {
81 .fourcc = V4L2_PIX_FMT_YUV422P,
82 .vdownsampling = { 1, 1, 1 },
83 .bit_depth = { 8, 4, 4 },
84 .is_yuv = true,
85 .planes = 3,
86 .buffers = 1,
87 },
88 {
89 .fourcc = V4L2_PIX_FMT_YUV420,
90 .vdownsampling = { 1, 2, 2 },
91 .bit_depth = { 8, 4, 4 },
92 .is_yuv = true,
93 .planes = 3,
94 .buffers = 1,
95 },
96 {
97 .fourcc = V4L2_PIX_FMT_YVU420,
98 .vdownsampling = { 1, 2, 2 },
99 .bit_depth = { 8, 4, 4 },
100 .is_yuv = true,
101 .planes = 3,
102 .buffers = 1,
103 },
104 {
105 .fourcc = V4L2_PIX_FMT_NV12,
106 .vdownsampling = { 1, 2 },
107 .bit_depth = { 8, 8 },
108 .is_yuv = true,
109 .planes = 2,
110 .buffers = 1,
111 },
112 {
113 .fourcc = V4L2_PIX_FMT_NV21,
114 .vdownsampling = { 1, 2 },
115 .bit_depth = { 8, 8 },
116 .is_yuv = true,
117 .planes = 2,
118 .buffers = 1,
119 },
120 {
121 .fourcc = V4L2_PIX_FMT_NV16,
122 .vdownsampling = { 1, 1 },
123 .bit_depth = { 8, 8 },
124 .is_yuv = true,
125 .planes = 2,
126 .buffers = 1,
127 },
128 {
129 .fourcc = V4L2_PIX_FMT_NV61,
130 .vdownsampling = { 1, 1 },
131 .bit_depth = { 8, 8 },
132 .is_yuv = true,
133 .planes = 2,
134 .buffers = 1,
135 },
136 {
137 .fourcc = V4L2_PIX_FMT_NV24,
138 .vdownsampling = { 1, 1 },
139 .bit_depth = { 8, 16 },
140 .is_yuv = true,
141 .planes = 2,
142 .buffers = 1,
143 },
144 {
145 .fourcc = V4L2_PIX_FMT_NV42,
146 .vdownsampling = { 1, 1 },
147 .bit_depth = { 8, 16 },
148 .is_yuv = true,
149 .planes = 2,
150 .buffers = 1,
151 },
152 {
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,
159 },
160 {
161 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
162 .vdownsampling = { 1 },
163 .bit_depth = { 16 },
164 .planes = 1,
165 .buffers = 1,
166 },
167 {
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,
174 },
175 {
176 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
177 .vdownsampling = { 1 },
178 .bit_depth = { 32 },
179 .planes = 1,
180 .buffers = 1,
181 .alpha_mask = 0x000000ff,
182 },
183 {
184 .fourcc = V4L2_PIX_FMT_GREY,
185 .vdownsampling = { 1 },
186 .bit_depth = { 8 },
187 .is_yuv = true,
188 .planes = 1,
189 .buffers = 1,
190 },
191 {
192 .fourcc = V4L2_PIX_FMT_Y16,
193 .vdownsampling = { 1 },
194 .bit_depth = { 16 },
195 .is_yuv = true,
196 .planes = 1,
197 .buffers = 1,
198 },
199 {
200 .fourcc = V4L2_PIX_FMT_Y16_BE,
201 .vdownsampling = { 1 },
202 .bit_depth = { 16 },
203 .is_yuv = true,
204 .planes = 1,
205 .buffers = 1,
206 },
207 {
208 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
209 .vdownsampling = { 1 },
210 .bit_depth = { 8 },
211 .planes = 1,
212 .buffers = 1,
213 },
214 {
215 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
216 .vdownsampling = { 1 },
217 .bit_depth = { 16 },
218 .planes = 1,
219 .buffers = 1,
220 .can_do_overlay = true,
221 },
222 {
223 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
224 .vdownsampling = { 1 },
225 .bit_depth = { 16 },
226 .planes = 1,
227 .buffers = 1,
228 .can_do_overlay = true,
229 },
230 {
231 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
232 .vdownsampling = { 1 },
233 .bit_depth = { 16 },
234 .planes = 1,
235 .buffers = 1,
236 },
237 {
238 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
239 .vdownsampling = { 1 },
240 .bit_depth = { 16 },
241 .planes = 1,
242 .buffers = 1,
243 },
244 {
245 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
246 .vdownsampling = { 1 },
247 .bit_depth = { 16 },
248 .planes = 1,
249 .buffers = 1,
250 .alpha_mask = 0x00f0,
251 },
252 {
253 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
254 .vdownsampling = { 1 },
255 .bit_depth = { 16 },
256 .planes = 1,
257 .buffers = 1,
258 .can_do_overlay = true,
259 },
260 {
261 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
262 .vdownsampling = { 1 },
263 .bit_depth = { 16 },
264 .planes = 1,
265 .buffers = 1,
266 .can_do_overlay = true,
267 },
268 {
269 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
270 .vdownsampling = { 1 },
271 .bit_depth = { 16 },
272 .planes = 1,
273 .buffers = 1,
274 .can_do_overlay = true,
275 .alpha_mask = 0x8000,
276 },
277 {
278 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
279 .vdownsampling = { 1 },
280 .bit_depth = { 16 },
281 .planes = 1,
282 .buffers = 1,
283 },
284 {
285 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
286 .vdownsampling = { 1 },
287 .bit_depth = { 16 },
288 .planes = 1,
289 .buffers = 1,
290 },
291 {
292 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
293 .vdownsampling = { 1 },
294 .bit_depth = { 16 },
295 .planes = 1,
296 .buffers = 1,
297 .alpha_mask = 0x0080,
298 },
299 {
300 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
301 .vdownsampling = { 1 },
302 .bit_depth = { 24 },
303 .planes = 1,
304 .buffers = 1,
305 },
306 {
307 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
308 .vdownsampling = { 1 },
309 .bit_depth = { 24 },
310 .planes = 1,
311 .buffers = 1,
312 },
313 {
314 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
315 .vdownsampling = { 1 },
316 .bit_depth = { 32 },
317 .planes = 1,
318 .buffers = 1,
319 },
320 {
321 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
322 .vdownsampling = { 1 },
323 .bit_depth = { 32 },
324 .planes = 1,
325 .buffers = 1,
326 },
327 {
328 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
329 .vdownsampling = { 1 },
330 .bit_depth = { 32 },
331 .planes = 1,
332 .buffers = 1,
333 },
334 {
335 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
336 .vdownsampling = { 1 },
337 .bit_depth = { 32 },
338 .planes = 1,
339 .buffers = 1,
340 },
341 {
342 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
343 .vdownsampling = { 1 },
344 .bit_depth = { 32 },
345 .planes = 1,
346 .buffers = 1,
347 },
348 {
349 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
350 .vdownsampling = { 1 },
351 .bit_depth = { 32 },
352 .planes = 1,
353 .buffers = 1,
354 .alpha_mask = 0x000000ff,
355 },
356 {
357 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
358 .vdownsampling = { 1 },
359 .bit_depth = { 32 },
360 .planes = 1,
361 .buffers = 1,
362 .alpha_mask = 0xff000000,
363 },
364 {
365 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
366 .vdownsampling = { 1 },
367 .bit_depth = { 8 },
368 .planes = 1,
369 .buffers = 1,
370 },
371 {
372 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
373 .vdownsampling = { 1 },
374 .bit_depth = { 8 },
375 .planes = 1,
376 .buffers = 1,
377 },
378 {
379 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
380 .vdownsampling = { 1 },
381 .bit_depth = { 8 },
382 .planes = 1,
383 .buffers = 1,
384 },
385 {
386 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
387 .vdownsampling = { 1 },
388 .bit_depth = { 8 },
389 .planes = 1,
390 .buffers = 1,
391 },
392 {
393 .fourcc = V4L2_PIX_FMT_NV16M,
394 .vdownsampling = { 1, 1 },
395 .bit_depth = { 8, 8 },
396 .is_yuv = true,
397 .planes = 2,
398 .buffers = 2,
399 .data_offset = { PLANE0_DATA_OFFSET, 0 },
400 },
401 {
402 .fourcc = V4L2_PIX_FMT_NV61M,
403 .vdownsampling = { 1, 1 },
404 .bit_depth = { 8, 8 },
405 .is_yuv = true,
406 .planes = 2,
407 .buffers = 2,
408 .data_offset = { 0, PLANE0_DATA_OFFSET },
409 },
410 {
411 .fourcc = V4L2_PIX_FMT_YUV420M,
412 .vdownsampling = { 1, 2, 2 },
413 .bit_depth = { 8, 4, 4 },
414 .is_yuv = true,
415 .planes = 3,
416 .buffers = 3,
417 },
418 {
419 .fourcc = V4L2_PIX_FMT_YVU420M,
420 .vdownsampling = { 1, 2, 2 },
421 .bit_depth = { 8, 4, 4 },
422 .is_yuv = true,
423 .planes = 3,
424 .buffers = 3,
425 },
426 {
427 .fourcc = V4L2_PIX_FMT_NV12M,
428 .vdownsampling = { 1, 2 },
429 .bit_depth = { 8, 8 },
430 .is_yuv = true,
431 .planes = 2,
432 .buffers = 2,
433 },
434 {
435 .fourcc = V4L2_PIX_FMT_NV21M,
436 .vdownsampling = { 1, 2 },
437 .bit_depth = { 8, 8 },
438 .is_yuv = true,
439 .planes = 2,
440 .buffers = 2,
441 },
442 };
443
444 /* There are 6 multiplanar formats in the list */
445 #define VIVID_MPLANAR_FORMATS 6
446
447 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
448 {
449 const struct vivid_fmt *fmt;
450 unsigned k;
451
452 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
453 fmt = &vivid_formats[k];
454 if (fmt->fourcc == pixelformat)
455 if (fmt->buffers == 1 || dev->multiplanar)
456 return fmt;
457 }
458
459 return NULL;
460 }
461
462 bool vivid_vid_can_loop(struct vivid_dev *dev)
463 {
464 if (dev->src_rect.width != dev->sink_rect.width ||
465 dev->src_rect.height != dev->sink_rect.height)
466 return false;
467 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
468 return false;
469 if (dev->field_cap != dev->field_out)
470 return false;
471 /*
472 * While this can be supported, it is just too much work
473 * to actually implement.
474 */
475 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
476 dev->field_cap == V4L2_FIELD_SEQ_BT)
477 return false;
478 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
479 if (!(dev->std_cap & V4L2_STD_525_60) !=
480 !(dev->std_out & V4L2_STD_525_60))
481 return false;
482 return true;
483 }
484 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
485 return true;
486 return false;
487 }
488
489 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
490 {
491 struct v4l2_event ev = {
492 .type = V4L2_EVENT_SOURCE_CHANGE,
493 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
494 };
495 unsigned i;
496
497 for (i = 0; i < dev->num_inputs; i++) {
498 ev.id = i;
499 if (dev->input_type[i] == type) {
500 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
501 v4l2_event_queue(&dev->vid_cap_dev, &ev);
502 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
503 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
504 }
505 }
506 }
507
508 /*
509 * Conversion function that converts a single-planar format to a
510 * single-plane multiplanar format.
511 */
512 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
513 {
514 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
515 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
516 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
517 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
518
519 memset(mp->reserved, 0, sizeof(mp->reserved));
520 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
521 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
522 mp->width = pix->width;
523 mp->height = pix->height;
524 mp->pixelformat = pix->pixelformat;
525 mp->field = pix->field;
526 mp->colorspace = pix->colorspace;
527 mp->ycbcr_enc = pix->ycbcr_enc;
528 mp->quantization = pix->quantization;
529 mp->num_planes = 1;
530 mp->flags = pix->flags;
531 ppix->sizeimage = pix->sizeimage;
532 ppix->bytesperline = pix->bytesperline;
533 memset(ppix->reserved, 0, sizeof(ppix->reserved));
534 }
535
536 int fmt_sp2mp_func(struct file *file, void *priv,
537 struct v4l2_format *f, fmtfunc func)
538 {
539 struct v4l2_format fmt;
540 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
541 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
542 struct v4l2_pix_format *pix = &f->fmt.pix;
543 int ret;
544
545 /* Converts to a mplane format */
546 fmt_sp2mp(f, &fmt);
547 /* Passes it to the generic mplane format function */
548 ret = func(file, priv, &fmt);
549 /* Copies back the mplane data to the single plane format */
550 pix->width = mp->width;
551 pix->height = mp->height;
552 pix->pixelformat = mp->pixelformat;
553 pix->field = mp->field;
554 pix->colorspace = mp->colorspace;
555 pix->ycbcr_enc = mp->ycbcr_enc;
556 pix->quantization = mp->quantization;
557 pix->sizeimage = ppix->sizeimage;
558 pix->bytesperline = ppix->bytesperline;
559 pix->flags = mp->flags;
560 return ret;
561 }
562
563 /* v4l2_rect helper function: copy the width/height values */
564 void rect_set_size_to(struct v4l2_rect *r, const struct v4l2_rect *size)
565 {
566 r->width = size->width;
567 r->height = size->height;
568 }
569
570 /* v4l2_rect helper function: width and height of r should be >= min_size */
571 void rect_set_min_size(struct v4l2_rect *r, const struct v4l2_rect *min_size)
572 {
573 if (r->width < min_size->width)
574 r->width = min_size->width;
575 if (r->height < min_size->height)
576 r->height = min_size->height;
577 }
578
579 /* v4l2_rect helper function: width and height of r should be <= max_size */
580 void rect_set_max_size(struct v4l2_rect *r, const struct v4l2_rect *max_size)
581 {
582 if (r->width > max_size->width)
583 r->width = max_size->width;
584 if (r->height > max_size->height)
585 r->height = max_size->height;
586 }
587
588 /* v4l2_rect helper function: r should be inside boundary */
589 void rect_map_inside(struct v4l2_rect *r, const struct v4l2_rect *boundary)
590 {
591 rect_set_max_size(r, boundary);
592 if (r->left < boundary->left)
593 r->left = boundary->left;
594 if (r->top < boundary->top)
595 r->top = boundary->top;
596 if (r->left + r->width > boundary->width)
597 r->left = boundary->width - r->width;
598 if (r->top + r->height > boundary->height)
599 r->top = boundary->height - r->height;
600 }
601
602 /* v4l2_rect helper function: return true if r1 has the same size as r2 */
603 bool rect_same_size(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
604 {
605 return r1->width == r2->width && r1->height == r2->height;
606 }
607
608 /* v4l2_rect helper function: calculate the intersection of two rects */
609 struct v4l2_rect rect_intersect(const struct v4l2_rect *a, const struct v4l2_rect *b)
610 {
611 struct v4l2_rect r;
612 int right, bottom;
613
614 r.top = max(a->top, b->top);
615 r.left = max(a->left, b->left);
616 bottom = min(a->top + a->height, b->top + b->height);
617 right = min(a->left + a->width, b->left + b->width);
618 r.height = max(0, bottom - r.top);
619 r.width = max(0, right - r.left);
620 return r;
621 }
622
623 /*
624 * v4l2_rect helper function: scale rect r by to->width / from->width and
625 * to->height / from->height.
626 */
627 void rect_scale(struct v4l2_rect *r, const struct v4l2_rect *from,
628 const struct v4l2_rect *to)
629 {
630 if (from->width == 0 || from->height == 0) {
631 r->left = r->top = r->width = r->height = 0;
632 return;
633 }
634 r->left = (((r->left - from->left) * to->width) / from->width) & ~1;
635 r->width = ((r->width * to->width) / from->width) & ~1;
636 r->top = ((r->top - from->top) * to->height) / from->height;
637 r->height = (r->height * to->height) / from->height;
638 }
639
640 bool rect_overlap(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
641 {
642 /*
643 * IF the left side of r1 is to the right of the right side of r2 OR
644 * the left side of r2 is to the right of the right side of r1 THEN
645 * they do not overlap.
646 */
647 if (r1->left >= r2->left + r2->width ||
648 r2->left >= r1->left + r1->width)
649 return false;
650 /*
651 * IF the top side of r1 is below the bottom of r2 OR
652 * the top side of r2 is below the bottom of r1 THEN
653 * they do not overlap.
654 */
655 if (r1->top >= r2->top + r2->height ||
656 r2->top >= r1->top + r1->height)
657 return false;
658 return true;
659 }
660 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
661 {
662 unsigned w = r->width;
663 unsigned h = r->height;
664
665 /* sanitize w and h in case someone passes ~0 as the value */
666 w &= 0xffff;
667 h &= 0xffff;
668 if (!(flags & V4L2_SEL_FLAG_LE)) {
669 w++;
670 h++;
671 if (w < 2)
672 w = 2;
673 if (h < 2)
674 h = 2;
675 }
676 if (!(flags & V4L2_SEL_FLAG_GE)) {
677 if (w > MAX_WIDTH)
678 w = MAX_WIDTH;
679 if (h > MAX_HEIGHT)
680 h = MAX_HEIGHT;
681 }
682 w = w & ~1;
683 h = h & ~1;
684 if (w < 2 || h < 2)
685 return -ERANGE;
686 if (w > MAX_WIDTH || h > MAX_HEIGHT)
687 return -ERANGE;
688 if (r->top < 0)
689 r->top = 0;
690 if (r->left < 0)
691 r->left = 0;
692 /* sanitize left and top in case someone passes ~0 as the value */
693 r->left &= 0xfffe;
694 r->top &= 0xfffe;
695 if (r->left + w > MAX_WIDTH)
696 r->left = MAX_WIDTH - w;
697 if (r->top + h > MAX_HEIGHT)
698 r->top = MAX_HEIGHT - h;
699 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
700 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
701 (r->width != w || r->height != h))
702 return -ERANGE;
703 r->width = w;
704 r->height = h;
705 return 0;
706 }
707
708 int vivid_enum_fmt_vid(struct file *file, void *priv,
709 struct v4l2_fmtdesc *f)
710 {
711 struct vivid_dev *dev = video_drvdata(file);
712 const struct vivid_fmt *fmt;
713
714 if (f->index >= ARRAY_SIZE(vivid_formats) -
715 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
716 return -EINVAL;
717
718 fmt = &vivid_formats[f->index];
719
720 f->pixelformat = fmt->fourcc;
721 return 0;
722 }
723
724 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
725 struct v4l2_fmtdesc *f)
726 {
727 struct vivid_dev *dev = video_drvdata(file);
728
729 if (!dev->multiplanar)
730 return -ENOTTY;
731 return vivid_enum_fmt_vid(file, priv, f);
732 }
733
734 int vidioc_enum_fmt_vid(struct file *file, void *priv,
735 struct v4l2_fmtdesc *f)
736 {
737 struct vivid_dev *dev = video_drvdata(file);
738
739 if (dev->multiplanar)
740 return -ENOTTY;
741 return vivid_enum_fmt_vid(file, priv, f);
742 }
743
744 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
745 {
746 struct vivid_dev *dev = video_drvdata(file);
747 struct video_device *vdev = video_devdata(file);
748
749 if (vdev->vfl_dir == VFL_DIR_RX) {
750 if (!vivid_is_sdtv_cap(dev))
751 return -ENODATA;
752 *id = dev->std_cap;
753 } else {
754 if (!vivid_is_svid_out(dev))
755 return -ENODATA;
756 *id = dev->std_out;
757 }
758 return 0;
759 }
760
761 int vidioc_g_dv_timings(struct file *file, void *_fh,
762 struct v4l2_dv_timings *timings)
763 {
764 struct vivid_dev *dev = video_drvdata(file);
765 struct video_device *vdev = video_devdata(file);
766
767 if (vdev->vfl_dir == VFL_DIR_RX) {
768 if (!vivid_is_hdmi_cap(dev))
769 return -ENODATA;
770 *timings = dev->dv_timings_cap;
771 } else {
772 if (!vivid_is_hdmi_out(dev))
773 return -ENODATA;
774 *timings = dev->dv_timings_out;
775 }
776 return 0;
777 }
778
779 int vidioc_enum_dv_timings(struct file *file, void *_fh,
780 struct v4l2_enum_dv_timings *timings)
781 {
782 struct vivid_dev *dev = video_drvdata(file);
783 struct video_device *vdev = video_devdata(file);
784
785 if (vdev->vfl_dir == VFL_DIR_RX) {
786 if (!vivid_is_hdmi_cap(dev))
787 return -ENODATA;
788 } else {
789 if (!vivid_is_hdmi_out(dev))
790 return -ENODATA;
791 }
792 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
793 NULL, NULL);
794 }
795
796 int vidioc_dv_timings_cap(struct file *file, void *_fh,
797 struct v4l2_dv_timings_cap *cap)
798 {
799 struct vivid_dev *dev = video_drvdata(file);
800 struct video_device *vdev = video_devdata(file);
801
802 if (vdev->vfl_dir == VFL_DIR_RX) {
803 if (!vivid_is_hdmi_cap(dev))
804 return -ENODATA;
805 } else {
806 if (!vivid_is_hdmi_out(dev))
807 return -ENODATA;
808 }
809 *cap = vivid_dv_timings_cap;
810 return 0;
811 }
812
813 int vidioc_g_edid(struct file *file, void *_fh,
814 struct v4l2_edid *edid)
815 {
816 struct vivid_dev *dev = video_drvdata(file);
817 struct video_device *vdev = video_devdata(file);
818
819 memset(edid->reserved, 0, sizeof(edid->reserved));
820 if (vdev->vfl_dir == VFL_DIR_RX) {
821 if (edid->pad >= dev->num_inputs)
822 return -EINVAL;
823 if (dev->input_type[edid->pad] != HDMI)
824 return -EINVAL;
825 } else {
826 if (edid->pad >= dev->num_outputs)
827 return -EINVAL;
828 if (dev->output_type[edid->pad] != HDMI)
829 return -EINVAL;
830 }
831 if (edid->start_block == 0 && edid->blocks == 0) {
832 edid->blocks = dev->edid_blocks;
833 return 0;
834 }
835 if (dev->edid_blocks == 0)
836 return -ENODATA;
837 if (edid->start_block >= dev->edid_blocks)
838 return -EINVAL;
839 if (edid->start_block + edid->blocks > dev->edid_blocks)
840 edid->blocks = dev->edid_blocks - edid->start_block;
841 memcpy(edid->edid, dev->edid, edid->blocks * 128);
842 return 0;
843 }
This page took 0.137122 seconds and 5 git commands to generate.