]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/media/platform/vivid/vivid-vid-cap.c
[media] vivid: add new format fields
[mirror_ubuntu-focal-kernel.git] / drivers / media / platform / vivid / vivid-vid-cap.c
CommitLineData
ef834f78
HV
1/*
2 * vivid-vid-cap.c - video capture 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>
5754d0d5 23#include <linux/vmalloc.h>
ef834f78
HV
24#include <linux/videodev2.h>
25#include <linux/v4l2-dv-timings.h>
26#include <media/v4l2-common.h>
27#include <media/v4l2-event.h>
28#include <media/v4l2-dv-timings.h>
29
30#include "vivid-core.h"
31#include "vivid-vid-common.h"
32#include "vivid-kthread-cap.h"
33#include "vivid-vid-cap.h"
34
35/* timeperframe: min/max and default */
36static const struct v4l2_fract
37 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
38 tpf_max = {.numerator = FPS_MAX, .denominator = 1},
39 tpf_default = {.numerator = 1, .denominator = 30};
40
41static const struct vivid_fmt formats_ovl[] = {
42 {
43 .name = "RGB565 (LE)",
44 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
96c76efa
HV
45 .vdownsampling = { 1 },
46 .bit_depth = { 16 },
ef834f78 47 .planes = 1,
96c76efa 48 .buffers = 1,
ef834f78
HV
49 },
50 {
51 .name = "XRGB555 (LE)",
52 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
96c76efa
HV
53 .vdownsampling = { 1 },
54 .bit_depth = { 16 },
ef834f78 55 .planes = 1,
96c76efa 56 .buffers = 1,
ef834f78
HV
57 },
58 {
59 .name = "ARGB555 (LE)",
60 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
96c76efa
HV
61 .vdownsampling = { 1 },
62 .bit_depth = { 16 },
ef834f78 63 .planes = 1,
96c76efa 64 .buffers = 1,
ef834f78
HV
65 },
66};
67
68/* The number of discrete webcam framesizes */
69#define VIVID_WEBCAM_SIZES 3
70/* The number of discrete webcam frameintervals */
71#define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
72
73/* Sizes must be in increasing order */
74static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
75 { 320, 180 },
76 { 640, 360 },
77 { 1280, 720 },
78};
79
80/*
81 * Intervals must be in increasing order and there must be twice as many
82 * elements in this array as there are in webcam_sizes.
83 */
84static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
85 { 1, 10 },
86 { 1, 15 },
87 { 1, 25 },
88 { 1, 30 },
89 { 1, 50 },
90 { 1, 60 },
91};
92
93static const struct v4l2_discrete_probe webcam_probe = {
94 webcam_sizes,
95 VIVID_WEBCAM_SIZES
96};
97
98static int vid_cap_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
99 unsigned *nbuffers, unsigned *nplanes,
100 unsigned sizes[], void *alloc_ctxs[])
101{
102 struct vivid_dev *dev = vb2_get_drv_priv(vq);
103 unsigned planes = tpg_g_planes(&dev->tpg);
104 unsigned h = dev->fmt_cap_rect.height;
105 unsigned p;
106
107 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
108 /*
109 * You cannot use read() with FIELD_ALTERNATE since the field
110 * information (TOP/BOTTOM) cannot be passed back to the user.
111 */
112 if (vb2_fileio_is_active(vq))
113 return -EINVAL;
114 }
115
116 if (dev->queue_setup_error) {
117 /*
118 * Error injection: test what happens if queue_setup() returns
119 * an error.
120 */
121 dev->queue_setup_error = false;
122 return -EINVAL;
123 }
124 if (fmt) {
125 const struct v4l2_pix_format_mplane *mp;
126 struct v4l2_format mp_fmt;
127 const struct vivid_fmt *vfmt;
128
129 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
130 fmt_sp2mp(fmt, &mp_fmt);
131 fmt = &mp_fmt;
132 }
133 mp = &fmt->fmt.pix_mp;
134 /*
135 * Check if the number of planes in the specified format match
136 * the number of planes in the current format. You can't mix that.
137 */
138 if (mp->num_planes != planes)
139 return -EINVAL;
1fc78bc9 140 vfmt = vivid_get_format(dev, mp->pixelformat);
ef834f78
HV
141 for (p = 0; p < planes; p++) {
142 sizes[p] = mp->plane_fmt[p].sizeimage;
143 if (sizes[0] < tpg_g_bytesperline(&dev->tpg, 0) * h +
144 vfmt->data_offset[p])
145 return -EINVAL;
146 }
147 } else {
148 for (p = 0; p < planes; p++)
149 sizes[p] = tpg_g_bytesperline(&dev->tpg, p) * h +
150 dev->fmt_cap->data_offset[p];
151 }
152
153 if (vq->num_buffers + *nbuffers < 2)
154 *nbuffers = 2 - vq->num_buffers;
155
156 *nplanes = planes;
157
158 /*
159 * videobuf2-vmalloc allocator is context-less so no need to set
160 * alloc_ctxs array.
161 */
162
163 if (planes == 2)
164 dprintk(dev, 1, "%s, count=%d, sizes=%u, %u\n", __func__,
165 *nbuffers, sizes[0], sizes[1]);
166 else
167 dprintk(dev, 1, "%s, count=%d, size=%u\n", __func__,
168 *nbuffers, sizes[0]);
169
170 return 0;
171}
172
173static int vid_cap_buf_prepare(struct vb2_buffer *vb)
174{
175 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
176 unsigned long size;
177 unsigned planes = tpg_g_planes(&dev->tpg);
178 unsigned p;
179
180 dprintk(dev, 1, "%s\n", __func__);
181
182 if (WARN_ON(NULL == dev->fmt_cap))
183 return -EINVAL;
184
185 if (dev->buf_prepare_error) {
186 /*
187 * Error injection: test what happens if buf_prepare() returns
188 * an error.
189 */
190 dev->buf_prepare_error = false;
191 return -EINVAL;
192 }
193 for (p = 0; p < planes; p++) {
194 size = tpg_g_bytesperline(&dev->tpg, p) * dev->fmt_cap_rect.height +
195 dev->fmt_cap->data_offset[p];
196
360565fc 197 if (vb2_plane_size(vb, p) < size) {
ef834f78 198 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
360565fc 199 __func__, p, vb2_plane_size(vb, p), size);
ef834f78
HV
200 return -EINVAL;
201 }
202
203 vb2_set_plane_payload(vb, p, size);
204 vb->v4l2_planes[p].data_offset = dev->fmt_cap->data_offset[p];
205 }
206
207 return 0;
208}
209
210static void vid_cap_buf_finish(struct vb2_buffer *vb)
211{
212 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
213 struct v4l2_timecode *tc = &vb->v4l2_buf.timecode;
214 unsigned fps = 25;
215 unsigned seq = vb->v4l2_buf.sequence;
216
217 if (!vivid_is_sdtv_cap(dev))
218 return;
219
220 /*
221 * Set the timecode. Rarely used, so it is interesting to
222 * test this.
223 */
224 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_TIMECODE;
225 if (dev->std_cap & V4L2_STD_525_60)
226 fps = 30;
227 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
228 tc->flags = 0;
229 tc->frames = seq % fps;
230 tc->seconds = (seq / fps) % 60;
231 tc->minutes = (seq / (60 * fps)) % 60;
232 tc->hours = (seq / (60 * 60 * fps)) % 24;
233}
234
235static void vid_cap_buf_queue(struct vb2_buffer *vb)
236{
237 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
238 struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
239
240 dprintk(dev, 1, "%s\n", __func__);
241
242 spin_lock(&dev->slock);
243 list_add_tail(&buf->list, &dev->vid_cap_active);
244 spin_unlock(&dev->slock);
245}
246
247static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
248{
249 struct vivid_dev *dev = vb2_get_drv_priv(vq);
250 unsigned i;
251 int err;
252
253 if (vb2_is_streaming(&dev->vb_vid_out_q))
254 dev->can_loop_video = vivid_vid_can_loop(dev);
255
256 if (dev->kthread_vid_cap)
257 return 0;
258
259 dev->vid_cap_seq_count = 0;
260 dprintk(dev, 1, "%s\n", __func__);
261 for (i = 0; i < VIDEO_MAX_FRAME; i++)
262 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
263 if (dev->start_streaming_error) {
264 dev->start_streaming_error = false;
265 err = -EINVAL;
266 } else {
267 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
268 }
269 if (err) {
270 struct vivid_buffer *buf, *tmp;
271
272 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
273 list_del(&buf->list);
274 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
275 }
276 }
277 return err;
278}
279
280/* abort streaming and wait for last buffer */
281static void vid_cap_stop_streaming(struct vb2_queue *vq)
282{
283 struct vivid_dev *dev = vb2_get_drv_priv(vq);
284
285 dprintk(dev, 1, "%s\n", __func__);
286 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
287 dev->can_loop_video = false;
288}
289
290const struct vb2_ops vivid_vid_cap_qops = {
291 .queue_setup = vid_cap_queue_setup,
292 .buf_prepare = vid_cap_buf_prepare,
293 .buf_finish = vid_cap_buf_finish,
294 .buf_queue = vid_cap_buf_queue,
295 .start_streaming = vid_cap_start_streaming,
296 .stop_streaming = vid_cap_stop_streaming,
6dfa5131
PL
297 .wait_prepare = vb2_ops_wait_prepare,
298 .wait_finish = vb2_ops_wait_finish,
ef834f78
HV
299};
300
301/*
302 * Determine the 'picture' quality based on the current TV frequency: either
303 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
304 * signal or NOISE for no signal.
305 */
306void vivid_update_quality(struct vivid_dev *dev)
307{
308 unsigned freq_modulus;
309
310 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
311 /*
312 * The 'noise' will only be replaced by the actual video
313 * if the output video matches the input video settings.
314 */
315 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
316 return;
317 }
318 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
319 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
320 return;
321 }
322 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
323 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
324 return;
325 }
326 if (!vivid_is_tv_cap(dev)) {
327 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
328 return;
329 }
330
331 /*
332 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
333 * From +/- 0.25 MHz around the channel there is color, and from
334 * +/- 1 MHz there is grayscale (chroma is lost).
335 * Everywhere else it is just noise.
336 */
337 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
338 if (freq_modulus > 2 * 16) {
339 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
340 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
341 return;
342 }
343 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
344 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
345 else
346 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
347}
348
349/*
350 * Get the current picture quality and the associated afc value.
351 */
352static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
353{
354 unsigned freq_modulus;
355
356 if (afc)
357 *afc = 0;
358 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
359 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
360 return tpg_g_quality(&dev->tpg);
361
362 /*
363 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
364 * From +/- 0.25 MHz around the channel there is color, and from
365 * +/- 1 MHz there is grayscale (chroma is lost).
366 * Everywhere else it is just gray.
367 */
368 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
369 if (afc)
370 *afc = freq_modulus - 1 * 16;
371 return TPG_QUAL_GRAY;
372}
373
374enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
375{
376 if (vivid_is_sdtv_cap(dev))
377 return dev->std_aspect_ratio;
378
379 if (vivid_is_hdmi_cap(dev))
380 return dev->dv_timings_aspect_ratio;
381
382 return TPG_VIDEO_ASPECT_IMAGE;
383}
384
385static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
386{
387 if (vivid_is_sdtv_cap(dev))
388 return (dev->std_cap & V4L2_STD_525_60) ?
389 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
390
391 if (vivid_is_hdmi_cap(dev) &&
392 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
393 return dev->src_rect.height == 480 ?
394 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
395
396 return TPG_PIXEL_ASPECT_SQUARE;
397}
398
399/*
400 * Called whenever the format has to be reset which can occur when
401 * changing inputs, standard, timings, etc.
402 */
403void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
404{
405 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
406 unsigned size;
407
408 switch (dev->input_type[dev->input]) {
409 case WEBCAM:
410 default:
411 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
412 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
413 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
414 dev->field_cap = V4L2_FIELD_NONE;
415 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
416 break;
417 case TV:
418 case SVID:
419 dev->field_cap = dev->tv_field_cap;
420 dev->src_rect.width = 720;
421 if (dev->std_cap & V4L2_STD_525_60) {
422 dev->src_rect.height = 480;
423 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
424 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
425 } else {
426 dev->src_rect.height = 576;
427 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
62f28725 428 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
ef834f78
HV
429 }
430 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
431 break;
432 case HDMI:
433 dev->src_rect.width = bt->width;
434 dev->src_rect.height = bt->height;
435 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
436 dev->timeperframe_vid_cap = (struct v4l2_fract) {
437 size / 100, (u32)bt->pixelclock / 100
438 };
439 if (bt->interlaced)
440 dev->field_cap = V4L2_FIELD_ALTERNATE;
441 else
442 dev->field_cap = V4L2_FIELD_NONE;
443
444 /*
445 * We can be called from within s_ctrl, in that case we can't
446 * set/get controls. Luckily we don't need to in that case.
447 */
448 if (keep_controls || !dev->colorspace)
449 break;
450 if (bt->standards & V4L2_DV_BT_STD_CEA861) {
451 if (bt->width == 720 && bt->height <= 576)
cd8adbe7 452 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
ef834f78 453 else
cd8adbe7 454 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
ef834f78
HV
455 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
456 } else {
cd8adbe7 457 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
ef834f78
HV
458 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
459 }
460 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
461 break;
462 }
463 vivid_update_quality(dev);
464 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
465 dev->crop_cap = dev->src_rect;
466 dev->crop_bounds_cap = dev->src_rect;
467 dev->compose_cap = dev->crop_cap;
468 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
469 dev->compose_cap.height /= 2;
470 dev->fmt_cap_rect = dev->compose_cap;
471 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
472 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
473 tpg_update_mv_step(&dev->tpg);
474}
475
476/* Map the field to something that is valid for the current input */
477static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
478{
479 if (vivid_is_sdtv_cap(dev)) {
480 switch (field) {
481 case V4L2_FIELD_INTERLACED_TB:
482 case V4L2_FIELD_INTERLACED_BT:
483 case V4L2_FIELD_SEQ_TB:
484 case V4L2_FIELD_SEQ_BT:
485 case V4L2_FIELD_TOP:
486 case V4L2_FIELD_BOTTOM:
487 case V4L2_FIELD_ALTERNATE:
488 return field;
489 case V4L2_FIELD_INTERLACED:
490 default:
491 return V4L2_FIELD_INTERLACED;
492 }
493 }
494 if (vivid_is_hdmi_cap(dev))
495 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
496 V4L2_FIELD_NONE;
497 return V4L2_FIELD_NONE;
498}
499
500static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
501{
502 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
503 return tpg_g_colorspace(&dev->tpg);
504 return dev->colorspace_out;
505}
506
3e8a78d1
HV
507static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
508{
509 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
510 return tpg_g_ycbcr_enc(&dev->tpg);
511 return dev->ycbcr_enc_out;
512}
513
514static unsigned vivid_quantization_cap(struct vivid_dev *dev)
515{
516 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
517 return tpg_g_quantization(&dev->tpg);
518 return dev->quantization_out;
519}
520
ef834f78
HV
521int vivid_g_fmt_vid_cap(struct file *file, void *priv,
522 struct v4l2_format *f)
523{
524 struct vivid_dev *dev = video_drvdata(file);
525 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
526 unsigned p;
527
528 mp->width = dev->fmt_cap_rect.width;
529 mp->height = dev->fmt_cap_rect.height;
530 mp->field = dev->field_cap;
531 mp->pixelformat = dev->fmt_cap->fourcc;
532 mp->colorspace = vivid_colorspace_cap(dev);
3e8a78d1
HV
533 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
534 mp->quantization = vivid_quantization_cap(dev);
ef834f78
HV
535 mp->num_planes = dev->fmt_cap->planes;
536 for (p = 0; p < mp->num_planes; p++) {
537 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
538 mp->plane_fmt[p].sizeimage =
539 mp->plane_fmt[p].bytesperline * mp->height +
540 dev->fmt_cap->data_offset[p];
541 }
542 return 0;
543}
544
545int vivid_try_fmt_vid_cap(struct file *file, void *priv,
546 struct v4l2_format *f)
547{
548 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
549 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
550 struct vivid_dev *dev = video_drvdata(file);
551 const struct vivid_fmt *fmt;
552 unsigned bytesperline, max_bpl;
553 unsigned factor = 1;
554 unsigned w, h;
555 unsigned p;
556
1fc78bc9 557 fmt = vivid_get_format(dev, mp->pixelformat);
ef834f78
HV
558 if (!fmt) {
559 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
560 mp->pixelformat);
561 mp->pixelformat = V4L2_PIX_FMT_YUYV;
1fc78bc9 562 fmt = vivid_get_format(dev, mp->pixelformat);
ef834f78
HV
563 }
564
565 mp->field = vivid_field_cap(dev, mp->field);
566 if (vivid_is_webcam(dev)) {
567 const struct v4l2_frmsize_discrete *sz =
568 v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
569
570 w = sz->width;
571 h = sz->height;
572 } else if (vivid_is_sdtv_cap(dev)) {
573 w = 720;
574 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
575 } else {
576 w = dev->src_rect.width;
577 h = dev->src_rect.height;
578 }
579 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
580 factor = 2;
581 if (vivid_is_webcam(dev) ||
582 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
583 mp->width = w;
584 mp->height = h / factor;
585 } else {
586 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
587
588 rect_set_min_size(&r, &vivid_min_rect);
589 rect_set_max_size(&r, &vivid_max_rect);
590 if (dev->has_scaler_cap && !dev->has_compose_cap) {
591 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
592
593 rect_set_max_size(&r, &max_r);
594 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
595 rect_set_max_size(&r, &dev->src_rect);
596 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
597 rect_set_min_size(&r, &dev->src_rect);
598 }
599 mp->width = r.width;
600 mp->height = r.height / factor;
601 }
602
603 /* This driver supports custom bytesperline values */
604
605 /* Calculate the minimum supported bytesperline value */
96c76efa 606 bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
ef834f78 607 /* Calculate the maximum supported bytesperline value */
96c76efa 608 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
ef834f78
HV
609 mp->num_planes = fmt->planes;
610 for (p = 0; p < mp->num_planes; p++) {
611 if (pfmt[p].bytesperline > max_bpl)
612 pfmt[p].bytesperline = max_bpl;
613 if (pfmt[p].bytesperline < bytesperline)
614 pfmt[p].bytesperline = bytesperline;
615 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height +
616 fmt->data_offset[p];
617 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
618 }
619 mp->colorspace = vivid_colorspace_cap(dev);
3e8a78d1
HV
620 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
621 mp->quantization = vivid_quantization_cap(dev);
ef834f78
HV
622 memset(mp->reserved, 0, sizeof(mp->reserved));
623 return 0;
624}
625
626int vivid_s_fmt_vid_cap(struct file *file, void *priv,
627 struct v4l2_format *f)
628{
629 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
630 struct vivid_dev *dev = video_drvdata(file);
631 struct v4l2_rect *crop = &dev->crop_cap;
632 struct v4l2_rect *compose = &dev->compose_cap;
633 struct vb2_queue *q = &dev->vb_vid_cap_q;
634 int ret = vivid_try_fmt_vid_cap(file, priv, f);
635 unsigned factor = 1;
636 unsigned i;
637
638 if (ret < 0)
639 return ret;
640
641 if (vb2_is_busy(q)) {
642 dprintk(dev, 1, "%s device busy\n", __func__);
643 return -EBUSY;
644 }
645
646 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
647 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
648 return -EBUSY;
649 }
650
1fc78bc9 651 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
ef834f78
HV
652 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
653 factor = 2;
654
655 /* Note: the webcam input doesn't support scaling, cropping or composing */
656
657 if (!vivid_is_webcam(dev) &&
658 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
659 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
660
661 if (dev->has_scaler_cap) {
662 if (dev->has_compose_cap)
663 rect_map_inside(compose, &r);
664 else
665 *compose = r;
666 if (dev->has_crop_cap && !dev->has_compose_cap) {
667 struct v4l2_rect min_r = {
668 0, 0,
669 r.width / MAX_ZOOM,
670 factor * r.height / MAX_ZOOM
671 };
672 struct v4l2_rect max_r = {
673 0, 0,
674 r.width * MAX_ZOOM,
675 factor * r.height * MAX_ZOOM
676 };
677
678 rect_set_min_size(crop, &min_r);
679 rect_set_max_size(crop, &max_r);
680 rect_map_inside(crop, &dev->crop_bounds_cap);
681 } else if (dev->has_crop_cap) {
682 struct v4l2_rect min_r = {
683 0, 0,
684 compose->width / MAX_ZOOM,
685 factor * compose->height / MAX_ZOOM
686 };
687 struct v4l2_rect max_r = {
688 0, 0,
689 compose->width * MAX_ZOOM,
690 factor * compose->height * MAX_ZOOM
691 };
692
693 rect_set_min_size(crop, &min_r);
694 rect_set_max_size(crop, &max_r);
695 rect_map_inside(crop, &dev->crop_bounds_cap);
696 }
697 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
698 r.height *= factor;
699 rect_set_size_to(crop, &r);
700 rect_map_inside(crop, &dev->crop_bounds_cap);
701 r = *crop;
702 r.height /= factor;
703 rect_set_size_to(compose, &r);
704 } else if (!dev->has_crop_cap) {
705 rect_map_inside(compose, &r);
706 } else {
707 r.height *= factor;
708 rect_set_max_size(crop, &r);
709 rect_map_inside(crop, &dev->crop_bounds_cap);
710 compose->top *= factor;
711 compose->height *= factor;
712 rect_set_size_to(compose, crop);
713 rect_map_inside(compose, &r);
714 compose->top /= factor;
715 compose->height /= factor;
716 }
717 } else if (vivid_is_webcam(dev)) {
718 /* Guaranteed to be a match */
719 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
720 if (webcam_sizes[i].width == mp->width &&
721 webcam_sizes[i].height == mp->height)
722 break;
723 dev->webcam_size_idx = i;
724 if (dev->webcam_ival_idx >= 2 * (3 - i))
725 dev->webcam_ival_idx = 2 * (3 - i) - 1;
726 vivid_update_format_cap(dev, false);
727 } else {
728 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
729
730 rect_set_size_to(compose, &r);
731 r.height *= factor;
732 rect_set_size_to(crop, &r);
733 }
734
735 dev->fmt_cap_rect.width = mp->width;
736 dev->fmt_cap_rect.height = mp->height;
737 tpg_s_buf_height(&dev->tpg, mp->height);
738 tpg_s_bytesperline(&dev->tpg, 0, mp->plane_fmt[0].bytesperline);
739 if (tpg_g_planes(&dev->tpg) > 1)
740 tpg_s_bytesperline(&dev->tpg, 1, mp->plane_fmt[1].bytesperline);
741 dev->field_cap = mp->field;
43047f6b
HV
742 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
743 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
744 else
745 tpg_s_field(&dev->tpg, dev->field_cap, false);
ef834f78
HV
746 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
747 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
748 if (vivid_is_sdtv_cap(dev))
749 dev->tv_field_cap = mp->field;
750 tpg_update_mv_step(&dev->tpg);
751 return 0;
752}
753
754int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
755 struct v4l2_format *f)
756{
757 struct vivid_dev *dev = video_drvdata(file);
758
759 if (!dev->multiplanar)
760 return -ENOTTY;
761 return vivid_g_fmt_vid_cap(file, priv, f);
762}
763
764int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
765 struct v4l2_format *f)
766{
767 struct vivid_dev *dev = video_drvdata(file);
768
769 if (!dev->multiplanar)
770 return -ENOTTY;
771 return vivid_try_fmt_vid_cap(file, priv, f);
772}
773
774int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
775 struct v4l2_format *f)
776{
777 struct vivid_dev *dev = video_drvdata(file);
778
779 if (!dev->multiplanar)
780 return -ENOTTY;
781 return vivid_s_fmt_vid_cap(file, priv, f);
782}
783
784int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
785 struct v4l2_format *f)
786{
787 struct vivid_dev *dev = video_drvdata(file);
788
789 if (dev->multiplanar)
790 return -ENOTTY;
791 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
792}
793
794int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
795 struct v4l2_format *f)
796{
797 struct vivid_dev *dev = video_drvdata(file);
798
799 if (dev->multiplanar)
800 return -ENOTTY;
801 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
802}
803
804int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
805 struct v4l2_format *f)
806{
807 struct vivid_dev *dev = video_drvdata(file);
808
809 if (dev->multiplanar)
810 return -ENOTTY;
811 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
812}
813
814int vivid_vid_cap_g_selection(struct file *file, void *priv,
815 struct v4l2_selection *sel)
816{
817 struct vivid_dev *dev = video_drvdata(file);
818
819 if (!dev->has_crop_cap && !dev->has_compose_cap)
820 return -ENOTTY;
821 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
822 return -EINVAL;
823 if (vivid_is_webcam(dev))
824 return -EINVAL;
825
826 sel->r.left = sel->r.top = 0;
827 switch (sel->target) {
828 case V4L2_SEL_TGT_CROP:
829 if (!dev->has_crop_cap)
830 return -EINVAL;
831 sel->r = dev->crop_cap;
832 break;
833 case V4L2_SEL_TGT_CROP_DEFAULT:
834 case V4L2_SEL_TGT_CROP_BOUNDS:
835 if (!dev->has_crop_cap)
836 return -EINVAL;
837 sel->r = dev->src_rect;
838 break;
839 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
840 if (!dev->has_compose_cap)
841 return -EINVAL;
842 sel->r = vivid_max_rect;
843 break;
844 case V4L2_SEL_TGT_COMPOSE:
845 if (!dev->has_compose_cap)
846 return -EINVAL;
847 sel->r = dev->compose_cap;
848 break;
849 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
850 if (!dev->has_compose_cap)
851 return -EINVAL;
852 sel->r = dev->fmt_cap_rect;
853 break;
854 default:
855 return -EINVAL;
856 }
857 return 0;
858}
859
860int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
861{
862 struct vivid_dev *dev = video_drvdata(file);
863 struct v4l2_rect *crop = &dev->crop_cap;
864 struct v4l2_rect *compose = &dev->compose_cap;
865 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
866 int ret;
867
868 if (!dev->has_crop_cap && !dev->has_compose_cap)
869 return -ENOTTY;
870 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
871 return -EINVAL;
872 if (vivid_is_webcam(dev))
873 return -EINVAL;
874
875 switch (s->target) {
876 case V4L2_SEL_TGT_CROP:
877 if (!dev->has_crop_cap)
878 return -EINVAL;
879 ret = vivid_vid_adjust_sel(s->flags, &s->r);
880 if (ret)
881 return ret;
882 rect_set_min_size(&s->r, &vivid_min_rect);
883 rect_set_max_size(&s->r, &dev->src_rect);
884 rect_map_inside(&s->r, &dev->crop_bounds_cap);
885 s->r.top /= factor;
886 s->r.height /= factor;
887 if (dev->has_scaler_cap) {
888 struct v4l2_rect fmt = dev->fmt_cap_rect;
889 struct v4l2_rect max_rect = {
890 0, 0,
891 s->r.width * MAX_ZOOM,
892 s->r.height * MAX_ZOOM
893 };
894 struct v4l2_rect min_rect = {
895 0, 0,
896 s->r.width / MAX_ZOOM,
897 s->r.height / MAX_ZOOM
898 };
899
900 rect_set_min_size(&fmt, &min_rect);
901 if (!dev->has_compose_cap)
902 rect_set_max_size(&fmt, &max_rect);
903 if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
904 vb2_is_busy(&dev->vb_vid_cap_q))
905 return -EBUSY;
906 if (dev->has_compose_cap) {
907 rect_set_min_size(compose, &min_rect);
908 rect_set_max_size(compose, &max_rect);
909 }
910 dev->fmt_cap_rect = fmt;
911 tpg_s_buf_height(&dev->tpg, fmt.height);
912 } else if (dev->has_compose_cap) {
913 struct v4l2_rect fmt = dev->fmt_cap_rect;
914
915 rect_set_min_size(&fmt, &s->r);
916 if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
917 vb2_is_busy(&dev->vb_vid_cap_q))
918 return -EBUSY;
919 dev->fmt_cap_rect = fmt;
920 tpg_s_buf_height(&dev->tpg, fmt.height);
921 rect_set_size_to(compose, &s->r);
922 rect_map_inside(compose, &dev->fmt_cap_rect);
923 } else {
924 if (!rect_same_size(&s->r, &dev->fmt_cap_rect) &&
925 vb2_is_busy(&dev->vb_vid_cap_q))
926 return -EBUSY;
927 rect_set_size_to(&dev->fmt_cap_rect, &s->r);
928 rect_set_size_to(compose, &s->r);
929 rect_map_inside(compose, &dev->fmt_cap_rect);
930 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
931 }
932 s->r.top *= factor;
933 s->r.height *= factor;
934 *crop = s->r;
935 break;
936 case V4L2_SEL_TGT_COMPOSE:
937 if (!dev->has_compose_cap)
938 return -EINVAL;
939 ret = vivid_vid_adjust_sel(s->flags, &s->r);
940 if (ret)
941 return ret;
942 rect_set_min_size(&s->r, &vivid_min_rect);
943 rect_set_max_size(&s->r, &dev->fmt_cap_rect);
944 if (dev->has_scaler_cap) {
945 struct v4l2_rect max_rect = {
946 0, 0,
947 dev->src_rect.width * MAX_ZOOM,
948 (dev->src_rect.height / factor) * MAX_ZOOM
949 };
950
951 rect_set_max_size(&s->r, &max_rect);
952 if (dev->has_crop_cap) {
953 struct v4l2_rect min_rect = {
954 0, 0,
955 s->r.width / MAX_ZOOM,
956 (s->r.height * factor) / MAX_ZOOM
957 };
958 struct v4l2_rect max_rect = {
959 0, 0,
960 s->r.width * MAX_ZOOM,
961 (s->r.height * factor) * MAX_ZOOM
962 };
963
964 rect_set_min_size(crop, &min_rect);
965 rect_set_max_size(crop, &max_rect);
966 rect_map_inside(crop, &dev->crop_bounds_cap);
967 }
968 } else if (dev->has_crop_cap) {
969 s->r.top *= factor;
970 s->r.height *= factor;
971 rect_set_max_size(&s->r, &dev->src_rect);
972 rect_set_size_to(crop, &s->r);
973 rect_map_inside(crop, &dev->crop_bounds_cap);
974 s->r.top /= factor;
975 s->r.height /= factor;
976 } else {
977 rect_set_size_to(&s->r, &dev->src_rect);
978 s->r.height /= factor;
979 }
980 rect_map_inside(&s->r, &dev->fmt_cap_rect);
981 if (dev->bitmap_cap && (compose->width != s->r.width ||
982 compose->height != s->r.height)) {
983 kfree(dev->bitmap_cap);
984 dev->bitmap_cap = NULL;
985 }
986 *compose = s->r;
987 break;
988 default:
989 return -EINVAL;
990 }
991
992 tpg_s_crop_compose(&dev->tpg, crop, compose);
993 return 0;
994}
995
996int vivid_vid_cap_cropcap(struct file *file, void *priv,
997 struct v4l2_cropcap *cap)
998{
999 struct vivid_dev *dev = video_drvdata(file);
1000
1001 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1002 return -EINVAL;
1003
1004 switch (vivid_get_pixel_aspect(dev)) {
1005 case TPG_PIXEL_ASPECT_NTSC:
1006 cap->pixelaspect.numerator = 11;
1007 cap->pixelaspect.denominator = 10;
1008 break;
1009 case TPG_PIXEL_ASPECT_PAL:
1010 cap->pixelaspect.numerator = 54;
1011 cap->pixelaspect.denominator = 59;
1012 break;
1013 case TPG_PIXEL_ASPECT_SQUARE:
1014 cap->pixelaspect.numerator = 1;
1015 cap->pixelaspect.denominator = 1;
1016 break;
1017 }
1018 return 0;
1019}
1020
1021int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1022 struct v4l2_fmtdesc *f)
1023{
9832e0e0 1024 struct vivid_dev *dev = video_drvdata(file);
ef834f78
HV
1025 const struct vivid_fmt *fmt;
1026
9832e0e0
HV
1027 if (dev->multiplanar)
1028 return -ENOTTY;
1029
ef834f78
HV
1030 if (f->index >= ARRAY_SIZE(formats_ovl))
1031 return -EINVAL;
1032
1033 fmt = &formats_ovl[f->index];
1034
1035 strlcpy(f->description, fmt->name, sizeof(f->description));
1036 f->pixelformat = fmt->fourcc;
1037 return 0;
1038}
1039
1040int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1041 struct v4l2_format *f)
1042{
1043 struct vivid_dev *dev = video_drvdata(file);
1044 const struct v4l2_rect *compose = &dev->compose_cap;
1045 struct v4l2_window *win = &f->fmt.win;
1046 unsigned clipcount = win->clipcount;
1047
9832e0e0
HV
1048 if (dev->multiplanar)
1049 return -ENOTTY;
1050
ef834f78
HV
1051 win->w.top = dev->overlay_cap_top;
1052 win->w.left = dev->overlay_cap_left;
1053 win->w.width = compose->width;
1054 win->w.height = compose->height;
1055 win->field = dev->overlay_cap_field;
1056 win->clipcount = dev->clipcount_cap;
1057 if (clipcount > dev->clipcount_cap)
1058 clipcount = dev->clipcount_cap;
1059 if (dev->bitmap_cap == NULL)
1060 win->bitmap = NULL;
1061 else if (win->bitmap) {
1062 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1063 ((compose->width + 7) / 8) * compose->height))
1064 return -EFAULT;
1065 }
1066 if (clipcount && win->clips) {
1067 if (copy_to_user(win->clips, dev->clips_cap,
1068 clipcount * sizeof(dev->clips_cap[0])))
1069 return -EFAULT;
1070 }
1071 return 0;
1072}
1073
1074int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1075 struct v4l2_format *f)
1076{
1077 struct vivid_dev *dev = video_drvdata(file);
1078 const struct v4l2_rect *compose = &dev->compose_cap;
1079 struct v4l2_window *win = &f->fmt.win;
1080 int i, j;
1081
9832e0e0
HV
1082 if (dev->multiplanar)
1083 return -ENOTTY;
1084
ef834f78
HV
1085 win->w.left = clamp_t(int, win->w.left,
1086 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1087 win->w.top = clamp_t(int, win->w.top,
1088 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1089 win->w.width = compose->width;
1090 win->w.height = compose->height;
1091 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1092 win->field = V4L2_FIELD_ANY;
1093 win->chromakey = 0;
1094 win->global_alpha = 0;
1095 if (win->clipcount && !win->clips)
1096 win->clipcount = 0;
1097 if (win->clipcount > MAX_CLIPS)
1098 win->clipcount = MAX_CLIPS;
1099 if (win->clipcount) {
1100 if (copy_from_user(dev->try_clips_cap, win->clips,
1101 win->clipcount * sizeof(dev->clips_cap[0])))
1102 return -EFAULT;
1103 for (i = 0; i < win->clipcount; i++) {
1104 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1105
1106 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1107 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1108 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1109 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1110 }
1111 /*
1112 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1113 * number and it's typically a one-time deal.
1114 */
1115 for (i = 0; i < win->clipcount - 1; i++) {
1116 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1117
1118 for (j = i + 1; j < win->clipcount; j++) {
1119 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1120
1121 if (rect_overlap(r1, r2))
1122 return -EINVAL;
1123 }
1124 }
1125 if (copy_to_user(win->clips, dev->try_clips_cap,
1126 win->clipcount * sizeof(dev->clips_cap[0])))
1127 return -EFAULT;
1128 }
1129 return 0;
1130}
1131
1132int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1133 struct v4l2_format *f)
1134{
1135 struct vivid_dev *dev = video_drvdata(file);
1136 const struct v4l2_rect *compose = &dev->compose_cap;
1137 struct v4l2_window *win = &f->fmt.win;
1138 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1139 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1140 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1141 void *new_bitmap = NULL;
1142
1143 if (ret)
1144 return ret;
1145
1146 if (win->bitmap) {
1147 new_bitmap = vzalloc(bitmap_size);
1148
1149 if (new_bitmap == NULL)
1150 return -ENOMEM;
1151 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1152 vfree(new_bitmap);
1153 return -EFAULT;
1154 }
1155 }
1156
1157 dev->overlay_cap_top = win->w.top;
1158 dev->overlay_cap_left = win->w.left;
1159 dev->overlay_cap_field = win->field;
1160 vfree(dev->bitmap_cap);
1161 dev->bitmap_cap = new_bitmap;
1162 dev->clipcount_cap = win->clipcount;
1163 if (dev->clipcount_cap)
1164 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1165 return 0;
1166}
1167
1168int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1169{
1170 struct vivid_dev *dev = video_drvdata(file);
1171
9832e0e0
HV
1172 if (dev->multiplanar)
1173 return -ENOTTY;
1174
ef834f78
HV
1175 if (i && dev->fb_vbase_cap == NULL)
1176 return -EINVAL;
1177
1178 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1179 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1180 return -EINVAL;
1181 }
1182
1183 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1184 return -EBUSY;
1185 dev->overlay_cap_owner = i ? fh : NULL;
1186 return 0;
1187}
1188
1189int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1190 struct v4l2_framebuffer *a)
1191{
1192 struct vivid_dev *dev = video_drvdata(file);
1193
9832e0e0
HV
1194 if (dev->multiplanar)
1195 return -ENOTTY;
1196
ef834f78
HV
1197 *a = dev->fb_cap;
1198 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1199 V4L2_FBUF_CAP_LIST_CLIPPING;
1200 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1201 a->fmt.field = V4L2_FIELD_NONE;
1202 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1203 a->fmt.priv = 0;
1204 return 0;
1205}
1206
1207int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1208 const struct v4l2_framebuffer *a)
1209{
1210 struct vivid_dev *dev = video_drvdata(file);
1211 const struct vivid_fmt *fmt;
1212
9832e0e0
HV
1213 if (dev->multiplanar)
1214 return -ENOTTY;
1215
ef834f78
HV
1216 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1217 return -EPERM;
1218
1219 if (dev->overlay_cap_owner)
1220 return -EBUSY;
1221
1222 if (a->base == NULL) {
1223 dev->fb_cap.base = NULL;
1224 dev->fb_vbase_cap = NULL;
1225 return 0;
1226 }
1227
1228 if (a->fmt.width < 48 || a->fmt.height < 32)
1229 return -EINVAL;
1fc78bc9 1230 fmt = vivid_get_format(dev, a->fmt.pixelformat);
ef834f78
HV
1231 if (!fmt || !fmt->can_do_overlay)
1232 return -EINVAL;
96c76efa 1233 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
ef834f78
HV
1234 return -EINVAL;
1235 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1236 return -EINVAL;
1237
1238 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1239 dev->fb_cap = *a;
1240 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1241 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1242 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1243 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1244 return 0;
1245}
1246
1247static const struct v4l2_audio vivid_audio_inputs[] = {
1248 { 0, "TV", V4L2_AUDCAP_STEREO },
1249 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1250};
1251
1252int vidioc_enum_input(struct file *file, void *priv,
1253 struct v4l2_input *inp)
1254{
1255 struct vivid_dev *dev = video_drvdata(file);
1256
1257 if (inp->index >= dev->num_inputs)
1258 return -EINVAL;
1259
1260 inp->type = V4L2_INPUT_TYPE_CAMERA;
1261 switch (dev->input_type[inp->index]) {
1262 case WEBCAM:
1263 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1264 dev->input_name_counter[inp->index]);
1265 inp->capabilities = 0;
1266 break;
1267 case TV:
1268 snprintf(inp->name, sizeof(inp->name), "TV %u",
1269 dev->input_name_counter[inp->index]);
1270 inp->type = V4L2_INPUT_TYPE_TUNER;
1271 inp->std = V4L2_STD_ALL;
1272 if (dev->has_audio_inputs)
1273 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1274 inp->capabilities = V4L2_IN_CAP_STD;
1275 break;
1276 case SVID:
1277 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1278 dev->input_name_counter[inp->index]);
1279 inp->std = V4L2_STD_ALL;
1280 if (dev->has_audio_inputs)
1281 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1282 inp->capabilities = V4L2_IN_CAP_STD;
1283 break;
1284 case HDMI:
1285 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1286 dev->input_name_counter[inp->index]);
1287 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1288 if (dev->edid_blocks == 0 ||
1289 dev->dv_timings_signal_mode == NO_SIGNAL)
1290 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1291 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1292 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1293 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1294 break;
1295 }
1296 if (dev->sensor_hflip)
1297 inp->status |= V4L2_IN_ST_HFLIP;
1298 if (dev->sensor_vflip)
1299 inp->status |= V4L2_IN_ST_VFLIP;
1300 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1301 if (dev->std_signal_mode == NO_SIGNAL) {
1302 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1303 } else if (dev->std_signal_mode == NO_LOCK) {
1304 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1305 } else if (vivid_is_tv_cap(dev)) {
1306 switch (tpg_g_quality(&dev->tpg)) {
1307 case TPG_QUAL_GRAY:
1308 inp->status |= V4L2_IN_ST_COLOR_KILL;
1309 break;
1310 case TPG_QUAL_NOISE:
1311 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1312 break;
1313 default:
1314 break;
1315 }
1316 }
1317 }
1318 return 0;
1319}
1320
1321int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1322{
1323 struct vivid_dev *dev = video_drvdata(file);
1324
1325 *i = dev->input;
1326 return 0;
1327}
1328
1329int vidioc_s_input(struct file *file, void *priv, unsigned i)
1330{
1331 struct vivid_dev *dev = video_drvdata(file);
1332 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1333 unsigned brightness;
1334
1335 if (i >= dev->num_inputs)
1336 return -EINVAL;
1337
1338 if (i == dev->input)
1339 return 0;
1340
1341 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1342 return -EBUSY;
1343
1344 dev->input = i;
1345 dev->vid_cap_dev.tvnorms = 0;
1346 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1347 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1348 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1349 }
1350 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1351 vivid_update_format_cap(dev, false);
1352
1353 if (dev->colorspace) {
1354 switch (dev->input_type[i]) {
1355 case WEBCAM:
cd8adbe7 1356 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
ef834f78
HV
1357 break;
1358 case TV:
1359 case SVID:
cd8adbe7 1360 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
ef834f78
HV
1361 break;
1362 case HDMI:
1363 if (bt->standards & V4L2_DV_BT_STD_CEA861) {
1364 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
cd8adbe7 1365 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
ef834f78 1366 else
cd8adbe7 1367 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
ef834f78 1368 } else {
cd8adbe7 1369 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
ef834f78
HV
1370 }
1371 break;
1372 }
1373 }
1374
1375 /*
1376 * Modify the brightness range depending on the input.
1377 * This makes it easy to use vivid to test if applications can
1378 * handle control range modifications and is also how this is
1379 * typically used in practice as different inputs may be hooked
1380 * up to different receivers with different control ranges.
1381 */
1382 brightness = 128 * i + dev->input_brightness[i];
1383 v4l2_ctrl_modify_range(dev->brightness,
1384 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1385 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1386 return 0;
1387}
1388
1389int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1390{
1391 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1392 return -EINVAL;
1393 *vin = vivid_audio_inputs[vin->index];
1394 return 0;
1395}
1396
1397int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1398{
1399 struct vivid_dev *dev = video_drvdata(file);
1400
1401 if (!vivid_is_sdtv_cap(dev))
1402 return -EINVAL;
1403 *vin = vivid_audio_inputs[dev->tv_audio_input];
1404 return 0;
1405}
1406
1407int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1408{
1409 struct vivid_dev *dev = video_drvdata(file);
1410
1411 if (!vivid_is_sdtv_cap(dev))
1412 return -EINVAL;
1413 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1414 return -EINVAL;
1415 dev->tv_audio_input = vin->index;
1416 return 0;
1417}
1418
1419int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1420{
1421 struct vivid_dev *dev = video_drvdata(file);
1422
1423 if (vf->tuner != 0)
1424 return -EINVAL;
1425 vf->frequency = dev->tv_freq;
1426 return 0;
1427}
1428
1429int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1430{
1431 struct vivid_dev *dev = video_drvdata(file);
1432
1433 if (vf->tuner != 0)
1434 return -EINVAL;
1435 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1436 if (vivid_is_tv_cap(dev))
1437 vivid_update_quality(dev);
1438 return 0;
1439}
1440
1441int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1442{
1443 struct vivid_dev *dev = video_drvdata(file);
1444
1445 if (vt->index != 0)
1446 return -EINVAL;
1447 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1448 return -EINVAL;
1449 dev->tv_audmode = vt->audmode;
1450 return 0;
1451}
1452
1453int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1454{
1455 struct vivid_dev *dev = video_drvdata(file);
1456 enum tpg_quality qual;
1457
1458 if (vt->index != 0)
1459 return -EINVAL;
1460
1461 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1462 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1463 vt->audmode = dev->tv_audmode;
1464 vt->rangelow = MIN_TV_FREQ;
1465 vt->rangehigh = MAX_TV_FREQ;
1466 qual = vivid_get_quality(dev, &vt->afc);
1467 if (qual == TPG_QUAL_COLOR)
1468 vt->signal = 0xffff;
1469 else if (qual == TPG_QUAL_GRAY)
1470 vt->signal = 0x8000;
1471 else
1472 vt->signal = 0;
1473 if (qual == TPG_QUAL_NOISE) {
1474 vt->rxsubchans = 0;
1475 } else if (qual == TPG_QUAL_GRAY) {
1476 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1477 } else {
1478 unsigned channel_nr = dev->tv_freq / (6 * 16);
1479 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1480
1481 switch (channel_nr % options) {
1482 case 0:
1483 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1484 break;
1485 case 1:
1486 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1487 break;
1488 case 2:
1489 if (dev->std_cap & V4L2_STD_NTSC_M)
1490 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1491 else
1492 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1493 break;
1494 case 3:
1495 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1496 break;
1497 }
1498 }
1499 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1500 return 0;
1501}
1502
1503/* Must remain in sync with the vivid_ctrl_standard_strings array */
1504const v4l2_std_id vivid_standard[] = {
1505 V4L2_STD_NTSC_M,
1506 V4L2_STD_NTSC_M_JP,
1507 V4L2_STD_NTSC_M_KR,
1508 V4L2_STD_NTSC_443,
1509 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1510 V4L2_STD_PAL_I,
1511 V4L2_STD_PAL_DK,
1512 V4L2_STD_PAL_M,
1513 V4L2_STD_PAL_N,
1514 V4L2_STD_PAL_Nc,
1515 V4L2_STD_PAL_60,
1516 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1517 V4L2_STD_SECAM_DK,
1518 V4L2_STD_SECAM_L,
1519 V4L2_STD_SECAM_LC,
1520 V4L2_STD_UNKNOWN
1521};
1522
1523/* Must remain in sync with the vivid_standard array */
1524const char * const vivid_ctrl_standard_strings[] = {
1525 "NTSC-M",
1526 "NTSC-M-JP",
1527 "NTSC-M-KR",
1528 "NTSC-443",
1529 "PAL-BGH",
1530 "PAL-I",
1531 "PAL-DK",
1532 "PAL-M",
1533 "PAL-N",
1534 "PAL-Nc",
1535 "PAL-60",
1536 "SECAM-BGH",
1537 "SECAM-DK",
1538 "SECAM-L",
1539 "SECAM-Lc",
1540 NULL,
1541};
1542
1543int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1544{
1545 struct vivid_dev *dev = video_drvdata(file);
1546
1547 if (!vivid_is_sdtv_cap(dev))
1548 return -ENODATA;
1549 if (dev->std_signal_mode == NO_SIGNAL ||
1550 dev->std_signal_mode == NO_LOCK) {
1551 *id = V4L2_STD_UNKNOWN;
1552 return 0;
1553 }
1554 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1555 *id = V4L2_STD_UNKNOWN;
1556 } else if (dev->std_signal_mode == CURRENT_STD) {
1557 *id = dev->std_cap;
1558 } else if (dev->std_signal_mode == SELECTED_STD) {
1559 *id = dev->query_std;
1560 } else {
1561 *id = vivid_standard[dev->query_std_last];
1562 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1563 }
1564
1565 return 0;
1566}
1567
1568int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1569{
1570 struct vivid_dev *dev = video_drvdata(file);
1571
1572 if (!vivid_is_sdtv_cap(dev))
1573 return -ENODATA;
1574 if (dev->std_cap == id)
1575 return 0;
1576 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1577 return -EBUSY;
1578 dev->std_cap = id;
1579 vivid_update_format_cap(dev, false);
1580 return 0;
1581}
1582
1583int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1584 struct v4l2_dv_timings *timings)
1585{
1586 struct vivid_dev *dev = video_drvdata(file);
1587
1588 if (!vivid_is_hdmi_cap(dev))
1589 return -ENODATA;
1590 if (vb2_is_busy(&dev->vb_vid_cap_q))
1591 return -EBUSY;
1592 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1593 0, NULL, NULL))
1594 return -EINVAL;
1595 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0))
1596 return 0;
1597 dev->dv_timings_cap = *timings;
1598 vivid_update_format_cap(dev, false);
1599 return 0;
1600}
1601
1602int vidioc_query_dv_timings(struct file *file, void *_fh,
1603 struct v4l2_dv_timings *timings)
1604{
1605 struct vivid_dev *dev = video_drvdata(file);
1606
1607 if (!vivid_is_hdmi_cap(dev))
1608 return -ENODATA;
1609 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1610 dev->edid_blocks == 0)
1611 return -ENOLINK;
1612 if (dev->dv_timings_signal_mode == NO_LOCK)
1613 return -ENOLCK;
1614 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1615 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1616 return -ERANGE;
1617 }
1618 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1619 *timings = dev->dv_timings_cap;
1620 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1621 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1622 } else {
1623 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1624 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1625 dev->query_dv_timings_size;
1626 }
1627 return 0;
1628}
1629
1630int vidioc_s_edid(struct file *file, void *_fh,
1631 struct v4l2_edid *edid)
1632{
1633 struct vivid_dev *dev = video_drvdata(file);
1634
1635 memset(edid->reserved, 0, sizeof(edid->reserved));
1636 if (edid->pad >= dev->num_inputs)
1637 return -EINVAL;
1638 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1639 return -EINVAL;
1640 if (edid->blocks == 0) {
1641 dev->edid_blocks = 0;
1642 return 0;
1643 }
1644 if (edid->blocks > dev->edid_max_blocks) {
1645 edid->blocks = dev->edid_max_blocks;
1646 return -E2BIG;
1647 }
1648 dev->edid_blocks = edid->blocks;
1649 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1650 return 0;
1651}
1652
1653int vidioc_enum_framesizes(struct file *file, void *fh,
1654 struct v4l2_frmsizeenum *fsize)
1655{
1656 struct vivid_dev *dev = video_drvdata(file);
1657
1658 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1659 return -EINVAL;
1fc78bc9 1660 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
ef834f78
HV
1661 return -EINVAL;
1662 if (vivid_is_webcam(dev)) {
1663 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1664 return -EINVAL;
1665 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1666 fsize->discrete = webcam_sizes[fsize->index];
1667 return 0;
1668 }
1669 if (fsize->index)
1670 return -EINVAL;
1671 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1672 fsize->stepwise.min_width = MIN_WIDTH;
1673 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1674 fsize->stepwise.step_width = 2;
1675 fsize->stepwise.min_height = MIN_HEIGHT;
1676 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1677 fsize->stepwise.step_height = 2;
1678 return 0;
1679}
1680
1681/* timeperframe is arbitrary and continuous */
1682int vidioc_enum_frameintervals(struct file *file, void *priv,
1683 struct v4l2_frmivalenum *fival)
1684{
1685 struct vivid_dev *dev = video_drvdata(file);
1686 const struct vivid_fmt *fmt;
1687 int i;
1688
1fc78bc9 1689 fmt = vivid_get_format(dev, fival->pixel_format);
ef834f78
HV
1690 if (!fmt)
1691 return -EINVAL;
1692
1693 if (!vivid_is_webcam(dev)) {
1694 static const struct v4l2_fract step = { 1, 1 };
1695
1696 if (fival->index)
1697 return -EINVAL;
1698 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1699 return -EINVAL;
1700 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1701 return -EINVAL;
1702 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1703 fival->stepwise.min = tpf_min;
1704 fival->stepwise.max = tpf_max;
1705 fival->stepwise.step = step;
1706 return 0;
1707 }
1708
1709 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1710 if (fival->width == webcam_sizes[i].width &&
1711 fival->height == webcam_sizes[i].height)
1712 break;
1713 if (i == ARRAY_SIZE(webcam_sizes))
1714 return -EINVAL;
1715 if (fival->index >= 2 * (3 - i))
1716 return -EINVAL;
1717 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1718 fival->discrete = webcam_intervals[fival->index];
1719 return 0;
1720}
1721
1722int vivid_vid_cap_g_parm(struct file *file, void *priv,
1723 struct v4l2_streamparm *parm)
1724{
1725 struct vivid_dev *dev = video_drvdata(file);
1726
1727 if (parm->type != (dev->multiplanar ?
1728 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1729 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1730 return -EINVAL;
1731
1732 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1733 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1734 parm->parm.capture.readbuffers = 1;
1735 return 0;
1736}
1737
1738#define FRACT_CMP(a, OP, b) \
1739 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1740
1741int vivid_vid_cap_s_parm(struct file *file, void *priv,
1742 struct v4l2_streamparm *parm)
1743{
1744 struct vivid_dev *dev = video_drvdata(file);
1745 unsigned ival_sz = 2 * (3 - dev->webcam_size_idx);
1746 struct v4l2_fract tpf;
1747 unsigned i;
1748
1749 if (parm->type != (dev->multiplanar ?
1750 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1751 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1752 return -EINVAL;
1753 if (!vivid_is_webcam(dev))
1754 return vivid_vid_cap_g_parm(file, priv, parm);
1755
1756 tpf = parm->parm.capture.timeperframe;
1757
1758 if (tpf.denominator == 0)
1759 tpf = webcam_intervals[ival_sz - 1];
1760 for (i = 0; i < ival_sz; i++)
1761 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1762 break;
1763 if (i == ival_sz)
1764 i = ival_sz - 1;
1765 dev->webcam_ival_idx = i;
1766 tpf = webcam_intervals[dev->webcam_ival_idx];
1767 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1768 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1769
1770 /* resync the thread's timings */
1771 dev->cap_seq_resync = true;
1772 dev->timeperframe_vid_cap = tpf;
1773 parm->parm.capture.timeperframe = tpf;
1774 parm->parm.capture.readbuffers = 1;
1775 return 0;
1776}