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