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