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