[media] vivid: use v4l2_device.release to clean up the driver
[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 },
dde72bd7
HV
146 {
147 .name = "YUV 4:4:4 biplanar",
148 .fourcc = V4L2_PIX_FMT_NV24,
149 .vdownsampling = { 1, 1 },
150 .bit_depth = { 8, 16 },
151 .is_yuv = true,
152 .planes = 2,
153 .buffers = 1,
154 },
155 {
156 .name = "YVU 4:4:4 biplanar",
157 .fourcc = V4L2_PIX_FMT_NV42,
158 .vdownsampling = { 1, 1 },
159 .bit_depth = { 8, 16 },
160 .is_yuv = true,
161 .planes = 2,
162 .buffers = 1,
163 },
628821c8
HV
164 {
165 .name = "YUV555 (LE)",
166 .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
167 .vdownsampling = { 1 },
168 .bit_depth = { 16 },
169 .planes = 1,
170 .buffers = 1,
171 .alpha_mask = 0x8000,
172 },
173 {
174 .name = "YUV565 (LE)",
175 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
176 .vdownsampling = { 1 },
177 .bit_depth = { 16 },
178 .planes = 1,
179 .buffers = 1,
180 },
181 {
182 .name = "YUV444",
183 .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
184 .vdownsampling = { 1 },
185 .bit_depth = { 16 },
186 .planes = 1,
187 .buffers = 1,
188 .alpha_mask = 0xf000,
189 },
190 {
191 .name = "YUV32 (LE)",
192 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
193 .vdownsampling = { 1 },
194 .bit_depth = { 32 },
195 .planes = 1,
196 .buffers = 1,
197 .alpha_mask = 0x000000ff,
198 },
b521c375
HV
199 {
200 .name = "Monochrome",
201 .fourcc = V4L2_PIX_FMT_GREY,
202 .vdownsampling = { 1 },
203 .bit_depth = { 8 },
204 .is_yuv = true,
205 .planes = 1,
206 .buffers = 1,
207 },
71491063
HV
208 {
209 .name = "RGB332",
210 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
211 .vdownsampling = { 1 },
212 .bit_depth = { 8 },
213 .planes = 1,
214 .buffers = 1,
215 },
ef834f78
HV
216 {
217 .name = "RGB565 (LE)",
218 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
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 },
225 {
226 .name = "RGB565 (BE)",
227 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
96c76efa
HV
228 .vdownsampling = { 1 },
229 .bit_depth = { 16 },
ef834f78 230 .planes = 1,
96c76efa 231 .buffers = 1,
ef834f78
HV
232 .can_do_overlay = true,
233 },
8aca230b
HV
234 {
235 .name = "RGB444",
236 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
237 .vdownsampling = { 1 },
238 .bit_depth = { 16 },
239 .planes = 1,
240 .buffers = 1,
241 },
242 {
243 .name = "XRGB444",
244 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
245 .vdownsampling = { 1 },
246 .bit_depth = { 16 },
247 .planes = 1,
248 .buffers = 1,
249 },
250 {
251 .name = "ARGB444",
252 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
253 .vdownsampling = { 1 },
254 .bit_depth = { 16 },
255 .planes = 1,
256 .buffers = 1,
257 .alpha_mask = 0x00f0,
258 },
ef834f78
HV
259 {
260 .name = "RGB555 (LE)",
9e1b73cf 261 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
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 },
268 {
269 .name = "XRGB555 (LE)",
9e1b73cf 270 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
96c76efa
HV
271 .vdownsampling = { 1 },
272 .bit_depth = { 16 },
ef834f78 273 .planes = 1,
96c76efa 274 .buffers = 1,
ef834f78
HV
275 .can_do_overlay = true,
276 },
277 {
278 .name = "ARGB555 (LE)",
279 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
96c76efa
HV
280 .vdownsampling = { 1 },
281 .bit_depth = { 16 },
ef834f78 282 .planes = 1,
96c76efa 283 .buffers = 1,
ef834f78
HV
284 .can_do_overlay = true,
285 .alpha_mask = 0x8000,
286 },
287 {
288 .name = "RGB555 (BE)",
9e1b73cf 289 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
96c76efa
HV
290 .vdownsampling = { 1 },
291 .bit_depth = { 16 },
ef834f78 292 .planes = 1,
96c76efa 293 .buffers = 1,
8f1ff543
HV
294 },
295 {
296 .name = "XRGB555 (BE)",
297 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
298 .vdownsampling = { 1 },
299 .bit_depth = { 16 },
300 .planes = 1,
301 .buffers = 1,
302 },
303 {
304 .name = "ARGB555 (BE)",
305 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
306 .vdownsampling = { 1 },
307 .bit_depth = { 16 },
308 .planes = 1,
309 .buffers = 1,
310 .alpha_mask = 0x0080,
ef834f78
HV
311 },
312 {
313 .name = "RGB24 (LE)",
314 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
96c76efa
HV
315 .vdownsampling = { 1 },
316 .bit_depth = { 24 },
ef834f78 317 .planes = 1,
96c76efa 318 .buffers = 1,
ef834f78
HV
319 },
320 {
321 .name = "RGB24 (BE)",
322 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
96c76efa
HV
323 .vdownsampling = { 1 },
324 .bit_depth = { 24 },
ef834f78 325 .planes = 1,
96c76efa 326 .buffers = 1,
ef834f78 327 },
68cd4e9f
HV
328 {
329 .name = "BGR666",
330 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
331 .vdownsampling = { 1 },
332 .bit_depth = { 32 },
333 .planes = 1,
334 .buffers = 1,
335 },
ef834f78
HV
336 {
337 .name = "RGB32 (LE)",
9e1b73cf 338 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
96c76efa
HV
339 .vdownsampling = { 1 },
340 .bit_depth = { 32 },
ef834f78 341 .planes = 1,
96c76efa 342 .buffers = 1,
ef834f78
HV
343 },
344 {
345 .name = "RGB32 (BE)",
9e1b73cf 346 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
96c76efa
HV
347 .vdownsampling = { 1 },
348 .bit_depth = { 32 },
ef834f78 349 .planes = 1,
96c76efa 350 .buffers = 1,
ef834f78
HV
351 },
352 {
353 .name = "XRGB32 (LE)",
9e1b73cf 354 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
96c76efa
HV
355 .vdownsampling = { 1 },
356 .bit_depth = { 32 },
ef834f78 357 .planes = 1,
96c76efa 358 .buffers = 1,
ef834f78
HV
359 },
360 {
361 .name = "XRGB32 (BE)",
9e1b73cf 362 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
96c76efa
HV
363 .vdownsampling = { 1 },
364 .bit_depth = { 32 },
ef834f78 365 .planes = 1,
96c76efa 366 .buffers = 1,
ef834f78
HV
367 },
368 {
369 .name = "ARGB32 (LE)",
370 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
96c76efa
HV
371 .vdownsampling = { 1 },
372 .bit_depth = { 32 },
ef834f78 373 .planes = 1,
96c76efa 374 .buffers = 1,
ef834f78
HV
375 .alpha_mask = 0x000000ff,
376 },
377 {
378 .name = "ARGB32 (BE)",
379 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
96c76efa
HV
380 .vdownsampling = { 1 },
381 .bit_depth = { 32 },
ef834f78 382 .planes = 1,
96c76efa 383 .buffers = 1,
ef834f78
HV
384 .alpha_mask = 0xff000000,
385 },
386 {
96c76efa 387 .name = "4:2:2, biplanar, YUV",
ef834f78 388 .fourcc = V4L2_PIX_FMT_NV16M,
96c76efa
HV
389 .vdownsampling = { 1, 1 },
390 .bit_depth = { 8, 8 },
ef834f78
HV
391 .is_yuv = true,
392 .planes = 2,
96c76efa 393 .buffers = 2,
ef834f78
HV
394 .data_offset = { PLANE0_DATA_OFFSET, 0 },
395 },
396 {
96c76efa 397 .name = "4:2:2, biplanar, YVU",
ef834f78 398 .fourcc = V4L2_PIX_FMT_NV61M,
96c76efa
HV
399 .vdownsampling = { 1, 1 },
400 .bit_depth = { 8, 8 },
ef834f78
HV
401 .is_yuv = true,
402 .planes = 2,
96c76efa 403 .buffers = 2,
ef834f78
HV
404 .data_offset = { 0, PLANE0_DATA_OFFSET },
405 },
b521c375
HV
406 {
407 .name = "4:2:0, triplanar, YUV",
408 .fourcc = V4L2_PIX_FMT_YUV420M,
409 .vdownsampling = { 1, 2, 2 },
410 .bit_depth = { 8, 4, 4 },
411 .is_yuv = true,
412 .planes = 3,
413 .buffers = 3,
414 },
415 {
416 .name = "4:2:0, triplanar, YVU",
417 .fourcc = V4L2_PIX_FMT_YVU420M,
418 .vdownsampling = { 1, 2, 2 },
419 .bit_depth = { 8, 4, 4 },
420 .is_yuv = true,
421 .planes = 3,
422 .buffers = 3,
423 },
424 {
425 .name = "4:2:0, biplanar, YUV",
426 .fourcc = V4L2_PIX_FMT_NV12M,
427 .vdownsampling = { 1, 2 },
428 .bit_depth = { 8, 8 },
429 .is_yuv = true,
430 .planes = 2,
431 .buffers = 2,
432 },
433 {
434 .name = "4:2:0, biplanar, YVU",
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 },
ef834f78
HV
442};
443
b521c375
HV
444/* There are 6 multiplanar formats in the list */
445#define VIVID_MPLANAR_FORMATS 6
ef834f78 446
1fc78bc9 447const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
ef834f78
HV
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)
ddcaee9d 455 if (fmt->buffers == 1 || dev->multiplanar)
ef834f78
HV
456 return fmt;
457 }
458
459 return NULL;
460}
461
462bool 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;
ba24b442
HV
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;
ef834f78
HV
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
489void 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 */
512void 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;
3e8a78d1
HV
527 mp->ycbcr_enc = pix->ycbcr_enc;
528 mp->quantization = pix->quantization;
ef834f78
HV
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
536int 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;
3e8a78d1
HV
555 pix->ycbcr_enc = mp->ycbcr_enc;
556 pix->quantization = mp->quantization;
ef834f78
HV
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 */
564void 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 */
571void 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 */
580void 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 */
589void 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 */
603bool 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 */
609struct 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 */
627void 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
640bool 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}
660int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
661{
662 unsigned w = r->width;
663 unsigned h = r->height;
664
665 if (!(flags & V4L2_SEL_FLAG_LE)) {
666 w++;
667 h++;
668 if (w < 2)
669 w = 2;
670 if (h < 2)
671 h = 2;
672 }
673 if (!(flags & V4L2_SEL_FLAG_GE)) {
674 if (w > MAX_WIDTH)
675 w = MAX_WIDTH;
676 if (h > MAX_HEIGHT)
677 h = MAX_HEIGHT;
678 }
679 w = w & ~1;
680 h = h & ~1;
681 if (w < 2 || h < 2)
682 return -ERANGE;
683 if (w > MAX_WIDTH || h > MAX_HEIGHT)
684 return -ERANGE;
685 if (r->top < 0)
686 r->top = 0;
687 if (r->left < 0)
688 r->left = 0;
689 r->left &= ~1;
690 r->top &= ~1;
691 if (r->left + w > MAX_WIDTH)
692 r->left = MAX_WIDTH - w;
693 if (r->top + h > MAX_HEIGHT)
694 r->top = MAX_HEIGHT - h;
695 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
696 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
697 (r->width != w || r->height != h))
698 return -ERANGE;
699 r->width = w;
700 r->height = h;
701 return 0;
702}
703
704int vivid_enum_fmt_vid(struct file *file, void *priv,
705 struct v4l2_fmtdesc *f)
706{
707 struct vivid_dev *dev = video_drvdata(file);
708 const struct vivid_fmt *fmt;
709
710 if (f->index >= ARRAY_SIZE(vivid_formats) -
711 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
712 return -EINVAL;
713
714 fmt = &vivid_formats[f->index];
715
716 strlcpy(f->description, fmt->name, sizeof(f->description));
717 f->pixelformat = fmt->fourcc;
718 return 0;
719}
720
721int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
722 struct v4l2_fmtdesc *f)
723{
724 struct vivid_dev *dev = video_drvdata(file);
725
726 if (!dev->multiplanar)
727 return -ENOTTY;
728 return vivid_enum_fmt_vid(file, priv, f);
729}
730
731int vidioc_enum_fmt_vid(struct file *file, void *priv,
732 struct v4l2_fmtdesc *f)
733{
734 struct vivid_dev *dev = video_drvdata(file);
735
736 if (dev->multiplanar)
737 return -ENOTTY;
738 return vivid_enum_fmt_vid(file, priv, f);
739}
740
741int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
742{
743 struct vivid_dev *dev = video_drvdata(file);
744 struct video_device *vdev = video_devdata(file);
745
746 if (vdev->vfl_dir == VFL_DIR_RX) {
747 if (!vivid_is_sdtv_cap(dev))
748 return -ENODATA;
749 *id = dev->std_cap;
750 } else {
751 if (!vivid_is_svid_out(dev))
752 return -ENODATA;
753 *id = dev->std_out;
754 }
755 return 0;
756}
757
758int vidioc_g_dv_timings(struct file *file, void *_fh,
759 struct v4l2_dv_timings *timings)
760{
761 struct vivid_dev *dev = video_drvdata(file);
762 struct video_device *vdev = video_devdata(file);
763
764 if (vdev->vfl_dir == VFL_DIR_RX) {
765 if (!vivid_is_hdmi_cap(dev))
766 return -ENODATA;
767 *timings = dev->dv_timings_cap;
768 } else {
769 if (!vivid_is_hdmi_out(dev))
770 return -ENODATA;
771 *timings = dev->dv_timings_out;
772 }
773 return 0;
774}
775
776int vidioc_enum_dv_timings(struct file *file, void *_fh,
777 struct v4l2_enum_dv_timings *timings)
778{
779 struct vivid_dev *dev = video_drvdata(file);
780 struct video_device *vdev = video_devdata(file);
781
782 if (vdev->vfl_dir == VFL_DIR_RX) {
783 if (!vivid_is_hdmi_cap(dev))
784 return -ENODATA;
785 } else {
786 if (!vivid_is_hdmi_out(dev))
787 return -ENODATA;
788 }
789 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
790 NULL, NULL);
791}
792
793int vidioc_dv_timings_cap(struct file *file, void *_fh,
794 struct v4l2_dv_timings_cap *cap)
795{
796 struct vivid_dev *dev = video_drvdata(file);
797 struct video_device *vdev = video_devdata(file);
798
799 if (vdev->vfl_dir == VFL_DIR_RX) {
800 if (!vivid_is_hdmi_cap(dev))
801 return -ENODATA;
802 } else {
803 if (!vivid_is_hdmi_out(dev))
804 return -ENODATA;
805 }
806 *cap = vivid_dv_timings_cap;
807 return 0;
808}
809
810int vidioc_g_edid(struct file *file, void *_fh,
811 struct v4l2_edid *edid)
812{
813 struct vivid_dev *dev = video_drvdata(file);
814 struct video_device *vdev = video_devdata(file);
815
816 memset(edid->reserved, 0, sizeof(edid->reserved));
817 if (vdev->vfl_dir == VFL_DIR_RX) {
818 if (edid->pad >= dev->num_inputs)
819 return -EINVAL;
820 if (dev->input_type[edid->pad] != HDMI)
821 return -EINVAL;
822 } else {
823 if (edid->pad >= dev->num_outputs)
824 return -EINVAL;
825 if (dev->output_type[edid->pad] != HDMI)
826 return -EINVAL;
827 }
828 if (edid->start_block == 0 && edid->blocks == 0) {
829 edid->blocks = dev->edid_blocks;
830 return 0;
831 }
832 if (dev->edid_blocks == 0)
833 return -ENODATA;
834 if (edid->start_block >= dev->edid_blocks)
835 return -EINVAL;
836 if (edid->start_block + edid->blocks > dev->edid_blocks)
837 edid->blocks = dev->edid_blocks - edid->start_block;
838 memcpy(edid->edid, dev->edid, edid->blocks * 128);
839 return 0;
840}
This page took 0.158808 seconds and 5 git commands to generate.