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