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