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