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