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