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