Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1 | /* |
| 2 | * vivid-vid-cap.c - video capture support functions. |
| 3 | * |
| 4 | * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you may redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 11 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 12 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 13 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 14 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 16 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | * SOFTWARE. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/sched.h> |
Hans Verkuil | 5754d0d | 2014-09-03 04:29:00 -0300 | [diff] [blame] | 23 | #include <linux/vmalloc.h> |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 24 | #include <linux/videodev2.h> |
| 25 | #include <linux/v4l2-dv-timings.h> |
| 26 | #include <media/v4l2-common.h> |
| 27 | #include <media/v4l2-event.h> |
| 28 | #include <media/v4l2-dv-timings.h> |
| 29 | |
| 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 */ |
| 36 | static 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 | |
| 41 | static 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 */ |
| 68 | static 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 | */ |
| 78 | static 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 | |
| 87 | static const struct v4l2_discrete_probe webcam_probe = { |
| 88 | webcam_sizes, |
| 89 | VIVID_WEBCAM_SIZES |
| 90 | }; |
| 91 | |
| 92 | static 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 Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 134 | vfmt = vivid_get_format(dev, mp->pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 135 | 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 | |
| 167 | static 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 | |
Hans Verkuil | 360565f | 2015-03-07 12:19:36 -0300 | [diff] [blame] | 191 | if (vb2_plane_size(vb, p) < size) { |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 192 | dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n", |
Hans Verkuil | 360565f | 2015-03-07 12:19:36 -0300 | [diff] [blame] | 193 | __func__, p, vb2_plane_size(vb, p), size); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 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 | |
| 204 | static 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 | |
| 229 | static 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 | |
| 241 | static 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 */ |
| 275 | static 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 | |
| 284 | const 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 Lad | 6dfa513 | 2014-11-18 08:23:39 -0300 | [diff] [blame] | 291 | .wait_prepare = vb2_ops_wait_prepare, |
| 292 | .wait_finish = vb2_ops_wait_finish, |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 293 | }; |
| 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 | */ |
| 300 | void 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 | */ |
| 346 | static 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 | |
| 368 | enum 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 | |
| 379 | static 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 | */ |
| 397 | void 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 Verkuil | 62f2872 | 2014-09-20 06:11:44 -0300 | [diff] [blame] | 422 | dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B; |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 423 | } |
| 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 Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 446 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 447 | else |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 448 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 449 | v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1); |
| 450 | } else { |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 451 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 452 | 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 */ |
| 471 | static 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 | |
| 494 | static 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 Verkuil | 3e8a78d | 2014-11-17 10:26:01 -0300 | [diff] [blame] | 501 | static 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 | |
| 508 | static 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 Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 515 | int 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 Verkuil | 3e8a78d | 2014-11-17 10:26:01 -0300 | [diff] [blame] | 527 | mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev); |
| 528 | mp->quantization = vivid_quantization_cap(dev); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 529 | 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 | |
| 539 | int 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 Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 551 | fmt = vivid_get_format(dev, mp->pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 552 | if (!fmt) { |
| 553 | dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n", |
| 554 | mp->pixelformat); |
| 555 | mp->pixelformat = V4L2_PIX_FMT_YUYV; |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 556 | fmt = vivid_get_format(dev, mp->pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 557 | } |
| 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 Verkuil | 3e8a78d | 2014-11-17 10:26:01 -0300 | [diff] [blame] | 614 | mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev); |
| 615 | mp->quantization = vivid_quantization_cap(dev); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 616 | memset(mp->reserved, 0, sizeof(mp->reserved)); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | int 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 Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 645 | dev->fmt_cap = vivid_get_format(dev, mp->pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 646 | 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; |
Hans Verkuil | 43047f6 | 2015-03-07 12:38:42 -0300 | [diff] [blame^] | 736 | if (dev->field_cap == V4L2_FIELD_ALTERNATE) |
| 737 | tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true); |
| 738 | else |
| 739 | tpg_s_field(&dev->tpg, dev->field_cap, false); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 740 | tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap); |
| 741 | tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc); |
| 742 | if (vivid_is_sdtv_cap(dev)) |
| 743 | dev->tv_field_cap = mp->field; |
| 744 | tpg_update_mv_step(&dev->tpg); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv, |
| 749 | struct v4l2_format *f) |
| 750 | { |
| 751 | struct vivid_dev *dev = video_drvdata(file); |
| 752 | |
| 753 | if (!dev->multiplanar) |
| 754 | return -ENOTTY; |
| 755 | return vivid_g_fmt_vid_cap(file, priv, f); |
| 756 | } |
| 757 | |
| 758 | int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv, |
| 759 | struct v4l2_format *f) |
| 760 | { |
| 761 | struct vivid_dev *dev = video_drvdata(file); |
| 762 | |
| 763 | if (!dev->multiplanar) |
| 764 | return -ENOTTY; |
| 765 | return vivid_try_fmt_vid_cap(file, priv, f); |
| 766 | } |
| 767 | |
| 768 | int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv, |
| 769 | struct v4l2_format *f) |
| 770 | { |
| 771 | struct vivid_dev *dev = video_drvdata(file); |
| 772 | |
| 773 | if (!dev->multiplanar) |
| 774 | return -ENOTTY; |
| 775 | return vivid_s_fmt_vid_cap(file, priv, f); |
| 776 | } |
| 777 | |
| 778 | int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 779 | struct v4l2_format *f) |
| 780 | { |
| 781 | struct vivid_dev *dev = video_drvdata(file); |
| 782 | |
| 783 | if (dev->multiplanar) |
| 784 | return -ENOTTY; |
| 785 | return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap); |
| 786 | } |
| 787 | |
| 788 | int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 789 | struct v4l2_format *f) |
| 790 | { |
| 791 | struct vivid_dev *dev = video_drvdata(file); |
| 792 | |
| 793 | if (dev->multiplanar) |
| 794 | return -ENOTTY; |
| 795 | return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap); |
| 796 | } |
| 797 | |
| 798 | int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 799 | struct v4l2_format *f) |
| 800 | { |
| 801 | struct vivid_dev *dev = video_drvdata(file); |
| 802 | |
| 803 | if (dev->multiplanar) |
| 804 | return -ENOTTY; |
| 805 | return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap); |
| 806 | } |
| 807 | |
| 808 | int vivid_vid_cap_g_selection(struct file *file, void *priv, |
| 809 | struct v4l2_selection *sel) |
| 810 | { |
| 811 | struct vivid_dev *dev = video_drvdata(file); |
| 812 | |
| 813 | if (!dev->has_crop_cap && !dev->has_compose_cap) |
| 814 | return -ENOTTY; |
| 815 | if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 816 | return -EINVAL; |
| 817 | if (vivid_is_webcam(dev)) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | sel->r.left = sel->r.top = 0; |
| 821 | switch (sel->target) { |
| 822 | case V4L2_SEL_TGT_CROP: |
| 823 | if (!dev->has_crop_cap) |
| 824 | return -EINVAL; |
| 825 | sel->r = dev->crop_cap; |
| 826 | break; |
| 827 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 828 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 829 | if (!dev->has_crop_cap) |
| 830 | return -EINVAL; |
| 831 | sel->r = dev->src_rect; |
| 832 | break; |
| 833 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 834 | if (!dev->has_compose_cap) |
| 835 | return -EINVAL; |
| 836 | sel->r = vivid_max_rect; |
| 837 | break; |
| 838 | case V4L2_SEL_TGT_COMPOSE: |
| 839 | if (!dev->has_compose_cap) |
| 840 | return -EINVAL; |
| 841 | sel->r = dev->compose_cap; |
| 842 | break; |
| 843 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 844 | if (!dev->has_compose_cap) |
| 845 | return -EINVAL; |
| 846 | sel->r = dev->fmt_cap_rect; |
| 847 | break; |
| 848 | default: |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s) |
| 855 | { |
| 856 | struct vivid_dev *dev = video_drvdata(file); |
| 857 | struct v4l2_rect *crop = &dev->crop_cap; |
| 858 | struct v4l2_rect *compose = &dev->compose_cap; |
| 859 | unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1; |
| 860 | int ret; |
| 861 | |
| 862 | if (!dev->has_crop_cap && !dev->has_compose_cap) |
| 863 | return -ENOTTY; |
| 864 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 865 | return -EINVAL; |
| 866 | if (vivid_is_webcam(dev)) |
| 867 | return -EINVAL; |
| 868 | |
| 869 | switch (s->target) { |
| 870 | case V4L2_SEL_TGT_CROP: |
| 871 | if (!dev->has_crop_cap) |
| 872 | return -EINVAL; |
| 873 | ret = vivid_vid_adjust_sel(s->flags, &s->r); |
| 874 | if (ret) |
| 875 | return ret; |
| 876 | rect_set_min_size(&s->r, &vivid_min_rect); |
| 877 | rect_set_max_size(&s->r, &dev->src_rect); |
| 878 | rect_map_inside(&s->r, &dev->crop_bounds_cap); |
| 879 | s->r.top /= factor; |
| 880 | s->r.height /= factor; |
| 881 | if (dev->has_scaler_cap) { |
| 882 | struct v4l2_rect fmt = dev->fmt_cap_rect; |
| 883 | struct v4l2_rect max_rect = { |
| 884 | 0, 0, |
| 885 | s->r.width * MAX_ZOOM, |
| 886 | s->r.height * MAX_ZOOM |
| 887 | }; |
| 888 | struct v4l2_rect min_rect = { |
| 889 | 0, 0, |
| 890 | s->r.width / MAX_ZOOM, |
| 891 | s->r.height / MAX_ZOOM |
| 892 | }; |
| 893 | |
| 894 | rect_set_min_size(&fmt, &min_rect); |
| 895 | if (!dev->has_compose_cap) |
| 896 | rect_set_max_size(&fmt, &max_rect); |
| 897 | if (!rect_same_size(&dev->fmt_cap_rect, &fmt) && |
| 898 | vb2_is_busy(&dev->vb_vid_cap_q)) |
| 899 | return -EBUSY; |
| 900 | if (dev->has_compose_cap) { |
| 901 | rect_set_min_size(compose, &min_rect); |
| 902 | rect_set_max_size(compose, &max_rect); |
| 903 | } |
| 904 | dev->fmt_cap_rect = fmt; |
| 905 | tpg_s_buf_height(&dev->tpg, fmt.height); |
| 906 | } else if (dev->has_compose_cap) { |
| 907 | struct v4l2_rect fmt = dev->fmt_cap_rect; |
| 908 | |
| 909 | rect_set_min_size(&fmt, &s->r); |
| 910 | if (!rect_same_size(&dev->fmt_cap_rect, &fmt) && |
| 911 | vb2_is_busy(&dev->vb_vid_cap_q)) |
| 912 | return -EBUSY; |
| 913 | dev->fmt_cap_rect = fmt; |
| 914 | tpg_s_buf_height(&dev->tpg, fmt.height); |
| 915 | rect_set_size_to(compose, &s->r); |
| 916 | rect_map_inside(compose, &dev->fmt_cap_rect); |
| 917 | } else { |
| 918 | if (!rect_same_size(&s->r, &dev->fmt_cap_rect) && |
| 919 | vb2_is_busy(&dev->vb_vid_cap_q)) |
| 920 | return -EBUSY; |
| 921 | rect_set_size_to(&dev->fmt_cap_rect, &s->r); |
| 922 | rect_set_size_to(compose, &s->r); |
| 923 | rect_map_inside(compose, &dev->fmt_cap_rect); |
| 924 | tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height); |
| 925 | } |
| 926 | s->r.top *= factor; |
| 927 | s->r.height *= factor; |
| 928 | *crop = s->r; |
| 929 | break; |
| 930 | case V4L2_SEL_TGT_COMPOSE: |
| 931 | if (!dev->has_compose_cap) |
| 932 | return -EINVAL; |
| 933 | ret = vivid_vid_adjust_sel(s->flags, &s->r); |
| 934 | if (ret) |
| 935 | return ret; |
| 936 | rect_set_min_size(&s->r, &vivid_min_rect); |
| 937 | rect_set_max_size(&s->r, &dev->fmt_cap_rect); |
| 938 | if (dev->has_scaler_cap) { |
| 939 | struct v4l2_rect max_rect = { |
| 940 | 0, 0, |
| 941 | dev->src_rect.width * MAX_ZOOM, |
| 942 | (dev->src_rect.height / factor) * MAX_ZOOM |
| 943 | }; |
| 944 | |
| 945 | rect_set_max_size(&s->r, &max_rect); |
| 946 | if (dev->has_crop_cap) { |
| 947 | struct v4l2_rect min_rect = { |
| 948 | 0, 0, |
| 949 | s->r.width / MAX_ZOOM, |
| 950 | (s->r.height * factor) / MAX_ZOOM |
| 951 | }; |
| 952 | struct v4l2_rect max_rect = { |
| 953 | 0, 0, |
| 954 | s->r.width * MAX_ZOOM, |
| 955 | (s->r.height * factor) * MAX_ZOOM |
| 956 | }; |
| 957 | |
| 958 | rect_set_min_size(crop, &min_rect); |
| 959 | rect_set_max_size(crop, &max_rect); |
| 960 | rect_map_inside(crop, &dev->crop_bounds_cap); |
| 961 | } |
| 962 | } else if (dev->has_crop_cap) { |
| 963 | s->r.top *= factor; |
| 964 | s->r.height *= factor; |
| 965 | rect_set_max_size(&s->r, &dev->src_rect); |
| 966 | rect_set_size_to(crop, &s->r); |
| 967 | rect_map_inside(crop, &dev->crop_bounds_cap); |
| 968 | s->r.top /= factor; |
| 969 | s->r.height /= factor; |
| 970 | } else { |
| 971 | rect_set_size_to(&s->r, &dev->src_rect); |
| 972 | s->r.height /= factor; |
| 973 | } |
| 974 | rect_map_inside(&s->r, &dev->fmt_cap_rect); |
| 975 | if (dev->bitmap_cap && (compose->width != s->r.width || |
| 976 | compose->height != s->r.height)) { |
| 977 | kfree(dev->bitmap_cap); |
| 978 | dev->bitmap_cap = NULL; |
| 979 | } |
| 980 | *compose = s->r; |
| 981 | break; |
| 982 | default: |
| 983 | return -EINVAL; |
| 984 | } |
| 985 | |
| 986 | tpg_s_crop_compose(&dev->tpg, crop, compose); |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | int vivid_vid_cap_cropcap(struct file *file, void *priv, |
| 991 | struct v4l2_cropcap *cap) |
| 992 | { |
| 993 | struct vivid_dev *dev = video_drvdata(file); |
| 994 | |
| 995 | if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 996 | return -EINVAL; |
| 997 | |
| 998 | switch (vivid_get_pixel_aspect(dev)) { |
| 999 | case TPG_PIXEL_ASPECT_NTSC: |
| 1000 | cap->pixelaspect.numerator = 11; |
| 1001 | cap->pixelaspect.denominator = 10; |
| 1002 | break; |
| 1003 | case TPG_PIXEL_ASPECT_PAL: |
| 1004 | cap->pixelaspect.numerator = 54; |
| 1005 | cap->pixelaspect.denominator = 59; |
| 1006 | break; |
| 1007 | case TPG_PIXEL_ASPECT_SQUARE: |
| 1008 | cap->pixelaspect.numerator = 1; |
| 1009 | cap->pixelaspect.denominator = 1; |
| 1010 | break; |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv, |
| 1016 | struct v4l2_fmtdesc *f) |
| 1017 | { |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1018 | struct vivid_dev *dev = video_drvdata(file); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1019 | const struct vivid_fmt *fmt; |
| 1020 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1021 | if (dev->multiplanar) |
| 1022 | return -ENOTTY; |
| 1023 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1024 | if (f->index >= ARRAY_SIZE(formats_ovl)) |
| 1025 | return -EINVAL; |
| 1026 | |
| 1027 | fmt = &formats_ovl[f->index]; |
| 1028 | |
| 1029 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 1030 | f->pixelformat = fmt->fourcc; |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | int vidioc_g_fmt_vid_overlay(struct file *file, void *priv, |
| 1035 | struct v4l2_format *f) |
| 1036 | { |
| 1037 | struct vivid_dev *dev = video_drvdata(file); |
| 1038 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1039 | struct v4l2_window *win = &f->fmt.win; |
| 1040 | unsigned clipcount = win->clipcount; |
| 1041 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1042 | if (dev->multiplanar) |
| 1043 | return -ENOTTY; |
| 1044 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1045 | win->w.top = dev->overlay_cap_top; |
| 1046 | win->w.left = dev->overlay_cap_left; |
| 1047 | win->w.width = compose->width; |
| 1048 | win->w.height = compose->height; |
| 1049 | win->field = dev->overlay_cap_field; |
| 1050 | win->clipcount = dev->clipcount_cap; |
| 1051 | if (clipcount > dev->clipcount_cap) |
| 1052 | clipcount = dev->clipcount_cap; |
| 1053 | if (dev->bitmap_cap == NULL) |
| 1054 | win->bitmap = NULL; |
| 1055 | else if (win->bitmap) { |
| 1056 | if (copy_to_user(win->bitmap, dev->bitmap_cap, |
| 1057 | ((compose->width + 7) / 8) * compose->height)) |
| 1058 | return -EFAULT; |
| 1059 | } |
| 1060 | if (clipcount && win->clips) { |
| 1061 | if (copy_to_user(win->clips, dev->clips_cap, |
| 1062 | clipcount * sizeof(dev->clips_cap[0]))) |
| 1063 | return -EFAULT; |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | int vidioc_try_fmt_vid_overlay(struct file *file, void *priv, |
| 1069 | struct v4l2_format *f) |
| 1070 | { |
| 1071 | struct vivid_dev *dev = video_drvdata(file); |
| 1072 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1073 | struct v4l2_window *win = &f->fmt.win; |
| 1074 | int i, j; |
| 1075 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1076 | if (dev->multiplanar) |
| 1077 | return -ENOTTY; |
| 1078 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1079 | win->w.left = clamp_t(int, win->w.left, |
| 1080 | -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width); |
| 1081 | win->w.top = clamp_t(int, win->w.top, |
| 1082 | -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height); |
| 1083 | win->w.width = compose->width; |
| 1084 | win->w.height = compose->height; |
| 1085 | if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP) |
| 1086 | win->field = V4L2_FIELD_ANY; |
| 1087 | win->chromakey = 0; |
| 1088 | win->global_alpha = 0; |
| 1089 | if (win->clipcount && !win->clips) |
| 1090 | win->clipcount = 0; |
| 1091 | if (win->clipcount > MAX_CLIPS) |
| 1092 | win->clipcount = MAX_CLIPS; |
| 1093 | if (win->clipcount) { |
| 1094 | if (copy_from_user(dev->try_clips_cap, win->clips, |
| 1095 | win->clipcount * sizeof(dev->clips_cap[0]))) |
| 1096 | return -EFAULT; |
| 1097 | for (i = 0; i < win->clipcount; i++) { |
| 1098 | struct v4l2_rect *r = &dev->try_clips_cap[i].c; |
| 1099 | |
| 1100 | r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1); |
| 1101 | r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top); |
| 1102 | r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1); |
| 1103 | r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left); |
| 1104 | } |
| 1105 | /* |
| 1106 | * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small |
| 1107 | * number and it's typically a one-time deal. |
| 1108 | */ |
| 1109 | for (i = 0; i < win->clipcount - 1; i++) { |
| 1110 | struct v4l2_rect *r1 = &dev->try_clips_cap[i].c; |
| 1111 | |
| 1112 | for (j = i + 1; j < win->clipcount; j++) { |
| 1113 | struct v4l2_rect *r2 = &dev->try_clips_cap[j].c; |
| 1114 | |
| 1115 | if (rect_overlap(r1, r2)) |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | } |
| 1119 | if (copy_to_user(win->clips, dev->try_clips_cap, |
| 1120 | win->clipcount * sizeof(dev->clips_cap[0]))) |
| 1121 | return -EFAULT; |
| 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | int vidioc_s_fmt_vid_overlay(struct file *file, void *priv, |
| 1127 | struct v4l2_format *f) |
| 1128 | { |
| 1129 | struct vivid_dev *dev = video_drvdata(file); |
| 1130 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1131 | struct v4l2_window *win = &f->fmt.win; |
| 1132 | int ret = vidioc_try_fmt_vid_overlay(file, priv, f); |
| 1133 | unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height; |
| 1134 | unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]); |
| 1135 | void *new_bitmap = NULL; |
| 1136 | |
| 1137 | if (ret) |
| 1138 | return ret; |
| 1139 | |
| 1140 | if (win->bitmap) { |
| 1141 | new_bitmap = vzalloc(bitmap_size); |
| 1142 | |
| 1143 | if (new_bitmap == NULL) |
| 1144 | return -ENOMEM; |
| 1145 | if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) { |
| 1146 | vfree(new_bitmap); |
| 1147 | return -EFAULT; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | dev->overlay_cap_top = win->w.top; |
| 1152 | dev->overlay_cap_left = win->w.left; |
| 1153 | dev->overlay_cap_field = win->field; |
| 1154 | vfree(dev->bitmap_cap); |
| 1155 | dev->bitmap_cap = new_bitmap; |
| 1156 | dev->clipcount_cap = win->clipcount; |
| 1157 | if (dev->clipcount_cap) |
| 1158 | memcpy(dev->clips_cap, dev->try_clips_cap, clips_size); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i) |
| 1163 | { |
| 1164 | struct vivid_dev *dev = video_drvdata(file); |
| 1165 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1166 | if (dev->multiplanar) |
| 1167 | return -ENOTTY; |
| 1168 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1169 | if (i && dev->fb_vbase_cap == NULL) |
| 1170 | return -EINVAL; |
| 1171 | |
| 1172 | if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) { |
| 1173 | dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n"); |
| 1174 | return -EINVAL; |
| 1175 | } |
| 1176 | |
| 1177 | if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh) |
| 1178 | return -EBUSY; |
| 1179 | dev->overlay_cap_owner = i ? fh : NULL; |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | int vivid_vid_cap_g_fbuf(struct file *file, void *fh, |
| 1184 | struct v4l2_framebuffer *a) |
| 1185 | { |
| 1186 | struct vivid_dev *dev = video_drvdata(file); |
| 1187 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1188 | if (dev->multiplanar) |
| 1189 | return -ENOTTY; |
| 1190 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1191 | *a = dev->fb_cap; |
| 1192 | a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING | |
| 1193 | V4L2_FBUF_CAP_LIST_CLIPPING; |
| 1194 | a->flags = V4L2_FBUF_FLAG_PRIMARY; |
| 1195 | a->fmt.field = V4L2_FIELD_NONE; |
| 1196 | a->fmt.colorspace = V4L2_COLORSPACE_SRGB; |
| 1197 | a->fmt.priv = 0; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | int vivid_vid_cap_s_fbuf(struct file *file, void *fh, |
| 1202 | const struct v4l2_framebuffer *a) |
| 1203 | { |
| 1204 | struct vivid_dev *dev = video_drvdata(file); |
| 1205 | const struct vivid_fmt *fmt; |
| 1206 | |
Hans Verkuil | 9832e0e | 2015-03-09 12:34:32 -0300 | [diff] [blame] | 1207 | if (dev->multiplanar) |
| 1208 | return -ENOTTY; |
| 1209 | |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1210 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO)) |
| 1211 | return -EPERM; |
| 1212 | |
| 1213 | if (dev->overlay_cap_owner) |
| 1214 | return -EBUSY; |
| 1215 | |
| 1216 | if (a->base == NULL) { |
| 1217 | dev->fb_cap.base = NULL; |
| 1218 | dev->fb_vbase_cap = NULL; |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | if (a->fmt.width < 48 || a->fmt.height < 32) |
| 1223 | return -EINVAL; |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1224 | fmt = vivid_get_format(dev, a->fmt.pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1225 | if (!fmt || !fmt->can_do_overlay) |
| 1226 | return -EINVAL; |
| 1227 | if (a->fmt.bytesperline < (a->fmt.width * fmt->depth) / 8) |
| 1228 | return -EINVAL; |
| 1229 | if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage) |
| 1230 | return -EINVAL; |
| 1231 | |
| 1232 | dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base); |
| 1233 | dev->fb_cap = *a; |
| 1234 | dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left, |
| 1235 | -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width); |
| 1236 | dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top, |
| 1237 | -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static const struct v4l2_audio vivid_audio_inputs[] = { |
| 1242 | { 0, "TV", V4L2_AUDCAP_STEREO }, |
| 1243 | { 1, "Line-In", V4L2_AUDCAP_STEREO }, |
| 1244 | }; |
| 1245 | |
| 1246 | int vidioc_enum_input(struct file *file, void *priv, |
| 1247 | struct v4l2_input *inp) |
| 1248 | { |
| 1249 | struct vivid_dev *dev = video_drvdata(file); |
| 1250 | |
| 1251 | if (inp->index >= dev->num_inputs) |
| 1252 | return -EINVAL; |
| 1253 | |
| 1254 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 1255 | switch (dev->input_type[inp->index]) { |
| 1256 | case WEBCAM: |
| 1257 | snprintf(inp->name, sizeof(inp->name), "Webcam %u", |
| 1258 | dev->input_name_counter[inp->index]); |
| 1259 | inp->capabilities = 0; |
| 1260 | break; |
| 1261 | case TV: |
| 1262 | snprintf(inp->name, sizeof(inp->name), "TV %u", |
| 1263 | dev->input_name_counter[inp->index]); |
| 1264 | inp->type = V4L2_INPUT_TYPE_TUNER; |
| 1265 | inp->std = V4L2_STD_ALL; |
| 1266 | if (dev->has_audio_inputs) |
| 1267 | inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1; |
| 1268 | inp->capabilities = V4L2_IN_CAP_STD; |
| 1269 | break; |
| 1270 | case SVID: |
| 1271 | snprintf(inp->name, sizeof(inp->name), "S-Video %u", |
| 1272 | dev->input_name_counter[inp->index]); |
| 1273 | inp->std = V4L2_STD_ALL; |
| 1274 | if (dev->has_audio_inputs) |
| 1275 | inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1; |
| 1276 | inp->capabilities = V4L2_IN_CAP_STD; |
| 1277 | break; |
| 1278 | case HDMI: |
| 1279 | snprintf(inp->name, sizeof(inp->name), "HDMI %u", |
| 1280 | dev->input_name_counter[inp->index]); |
| 1281 | inp->capabilities = V4L2_IN_CAP_DV_TIMINGS; |
| 1282 | if (dev->edid_blocks == 0 || |
| 1283 | dev->dv_timings_signal_mode == NO_SIGNAL) |
| 1284 | inp->status |= V4L2_IN_ST_NO_SIGNAL; |
| 1285 | else if (dev->dv_timings_signal_mode == NO_LOCK || |
| 1286 | dev->dv_timings_signal_mode == OUT_OF_RANGE) |
| 1287 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1288 | break; |
| 1289 | } |
| 1290 | if (dev->sensor_hflip) |
| 1291 | inp->status |= V4L2_IN_ST_HFLIP; |
| 1292 | if (dev->sensor_vflip) |
| 1293 | inp->status |= V4L2_IN_ST_VFLIP; |
| 1294 | if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) { |
| 1295 | if (dev->std_signal_mode == NO_SIGNAL) { |
| 1296 | inp->status |= V4L2_IN_ST_NO_SIGNAL; |
| 1297 | } else if (dev->std_signal_mode == NO_LOCK) { |
| 1298 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1299 | } else if (vivid_is_tv_cap(dev)) { |
| 1300 | switch (tpg_g_quality(&dev->tpg)) { |
| 1301 | case TPG_QUAL_GRAY: |
| 1302 | inp->status |= V4L2_IN_ST_COLOR_KILL; |
| 1303 | break; |
| 1304 | case TPG_QUAL_NOISE: |
| 1305 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1306 | break; |
| 1307 | default: |
| 1308 | break; |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | int vidioc_g_input(struct file *file, void *priv, unsigned *i) |
| 1316 | { |
| 1317 | struct vivid_dev *dev = video_drvdata(file); |
| 1318 | |
| 1319 | *i = dev->input; |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | int vidioc_s_input(struct file *file, void *priv, unsigned i) |
| 1324 | { |
| 1325 | struct vivid_dev *dev = video_drvdata(file); |
| 1326 | struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt; |
| 1327 | unsigned brightness; |
| 1328 | |
| 1329 | if (i >= dev->num_inputs) |
| 1330 | return -EINVAL; |
| 1331 | |
| 1332 | if (i == dev->input) |
| 1333 | return 0; |
| 1334 | |
| 1335 | if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q)) |
| 1336 | return -EBUSY; |
| 1337 | |
| 1338 | dev->input = i; |
| 1339 | dev->vid_cap_dev.tvnorms = 0; |
| 1340 | if (dev->input_type[i] == TV || dev->input_type[i] == SVID) { |
| 1341 | dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1; |
| 1342 | dev->vid_cap_dev.tvnorms = V4L2_STD_ALL; |
| 1343 | } |
| 1344 | dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms; |
| 1345 | vivid_update_format_cap(dev, false); |
| 1346 | |
| 1347 | if (dev->colorspace) { |
| 1348 | switch (dev->input_type[i]) { |
| 1349 | case WEBCAM: |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1350 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1351 | break; |
| 1352 | case TV: |
| 1353 | case SVID: |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1354 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1355 | break; |
| 1356 | case HDMI: |
| 1357 | if (bt->standards & V4L2_DV_BT_STD_CEA861) { |
| 1358 | if (dev->src_rect.width == 720 && dev->src_rect.height <= 576) |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1359 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1360 | else |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1361 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1362 | } else { |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1363 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1364 | } |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * Modify the brightness range depending on the input. |
| 1371 | * This makes it easy to use vivid to test if applications can |
| 1372 | * handle control range modifications and is also how this is |
| 1373 | * typically used in practice as different inputs may be hooked |
| 1374 | * up to different receivers with different control ranges. |
| 1375 | */ |
| 1376 | brightness = 128 * i + dev->input_brightness[i]; |
| 1377 | v4l2_ctrl_modify_range(dev->brightness, |
| 1378 | 128 * i, 255 + 128 * i, 1, 128 + 128 * i); |
| 1379 | v4l2_ctrl_s_ctrl(dev->brightness, brightness); |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin) |
| 1384 | { |
| 1385 | if (vin->index >= ARRAY_SIZE(vivid_audio_inputs)) |
| 1386 | return -EINVAL; |
| 1387 | *vin = vivid_audio_inputs[vin->index]; |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin) |
| 1392 | { |
| 1393 | struct vivid_dev *dev = video_drvdata(file); |
| 1394 | |
| 1395 | if (!vivid_is_sdtv_cap(dev)) |
| 1396 | return -EINVAL; |
| 1397 | *vin = vivid_audio_inputs[dev->tv_audio_input]; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin) |
| 1402 | { |
| 1403 | struct vivid_dev *dev = video_drvdata(file); |
| 1404 | |
| 1405 | if (!vivid_is_sdtv_cap(dev)) |
| 1406 | return -EINVAL; |
| 1407 | if (vin->index >= ARRAY_SIZE(vivid_audio_inputs)) |
| 1408 | return -EINVAL; |
| 1409 | dev->tv_audio_input = vin->index; |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf) |
| 1414 | { |
| 1415 | struct vivid_dev *dev = video_drvdata(file); |
| 1416 | |
| 1417 | if (vf->tuner != 0) |
| 1418 | return -EINVAL; |
| 1419 | vf->frequency = dev->tv_freq; |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf) |
| 1424 | { |
| 1425 | struct vivid_dev *dev = video_drvdata(file); |
| 1426 | |
| 1427 | if (vf->tuner != 0) |
| 1428 | return -EINVAL; |
| 1429 | dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ); |
| 1430 | if (vivid_is_tv_cap(dev)) |
| 1431 | vivid_update_quality(dev); |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt) |
| 1436 | { |
| 1437 | struct vivid_dev *dev = video_drvdata(file); |
| 1438 | |
| 1439 | if (vt->index != 0) |
| 1440 | return -EINVAL; |
| 1441 | if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2) |
| 1442 | return -EINVAL; |
| 1443 | dev->tv_audmode = vt->audmode; |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt) |
| 1448 | { |
| 1449 | struct vivid_dev *dev = video_drvdata(file); |
| 1450 | enum tpg_quality qual; |
| 1451 | |
| 1452 | if (vt->index != 0) |
| 1453 | return -EINVAL; |
| 1454 | |
| 1455 | vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO | |
| 1456 | V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2; |
| 1457 | vt->audmode = dev->tv_audmode; |
| 1458 | vt->rangelow = MIN_TV_FREQ; |
| 1459 | vt->rangehigh = MAX_TV_FREQ; |
| 1460 | qual = vivid_get_quality(dev, &vt->afc); |
| 1461 | if (qual == TPG_QUAL_COLOR) |
| 1462 | vt->signal = 0xffff; |
| 1463 | else if (qual == TPG_QUAL_GRAY) |
| 1464 | vt->signal = 0x8000; |
| 1465 | else |
| 1466 | vt->signal = 0; |
| 1467 | if (qual == TPG_QUAL_NOISE) { |
| 1468 | vt->rxsubchans = 0; |
| 1469 | } else if (qual == TPG_QUAL_GRAY) { |
| 1470 | vt->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 1471 | } else { |
| 1472 | unsigned channel_nr = dev->tv_freq / (6 * 16); |
| 1473 | unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3; |
| 1474 | |
| 1475 | switch (channel_nr % options) { |
| 1476 | case 0: |
| 1477 | vt->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 1478 | break; |
| 1479 | case 1: |
| 1480 | vt->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 1481 | break; |
| 1482 | case 2: |
| 1483 | if (dev->std_cap & V4L2_STD_NTSC_M) |
| 1484 | vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP; |
| 1485 | else |
| 1486 | vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; |
| 1487 | break; |
| 1488 | case 3: |
| 1489 | vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP; |
| 1490 | break; |
| 1491 | } |
| 1492 | } |
| 1493 | strlcpy(vt->name, "TV Tuner", sizeof(vt->name)); |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* Must remain in sync with the vivid_ctrl_standard_strings array */ |
| 1498 | const v4l2_std_id vivid_standard[] = { |
| 1499 | V4L2_STD_NTSC_M, |
| 1500 | V4L2_STD_NTSC_M_JP, |
| 1501 | V4L2_STD_NTSC_M_KR, |
| 1502 | V4L2_STD_NTSC_443, |
| 1503 | V4L2_STD_PAL_BG | V4L2_STD_PAL_H, |
| 1504 | V4L2_STD_PAL_I, |
| 1505 | V4L2_STD_PAL_DK, |
| 1506 | V4L2_STD_PAL_M, |
| 1507 | V4L2_STD_PAL_N, |
| 1508 | V4L2_STD_PAL_Nc, |
| 1509 | V4L2_STD_PAL_60, |
| 1510 | V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, |
| 1511 | V4L2_STD_SECAM_DK, |
| 1512 | V4L2_STD_SECAM_L, |
| 1513 | V4L2_STD_SECAM_LC, |
| 1514 | V4L2_STD_UNKNOWN |
| 1515 | }; |
| 1516 | |
| 1517 | /* Must remain in sync with the vivid_standard array */ |
| 1518 | const char * const vivid_ctrl_standard_strings[] = { |
| 1519 | "NTSC-M", |
| 1520 | "NTSC-M-JP", |
| 1521 | "NTSC-M-KR", |
| 1522 | "NTSC-443", |
| 1523 | "PAL-BGH", |
| 1524 | "PAL-I", |
| 1525 | "PAL-DK", |
| 1526 | "PAL-M", |
| 1527 | "PAL-N", |
| 1528 | "PAL-Nc", |
| 1529 | "PAL-60", |
| 1530 | "SECAM-BGH", |
| 1531 | "SECAM-DK", |
| 1532 | "SECAM-L", |
| 1533 | "SECAM-Lc", |
| 1534 | NULL, |
| 1535 | }; |
| 1536 | |
| 1537 | int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id) |
| 1538 | { |
| 1539 | struct vivid_dev *dev = video_drvdata(file); |
| 1540 | |
| 1541 | if (!vivid_is_sdtv_cap(dev)) |
| 1542 | return -ENODATA; |
| 1543 | if (dev->std_signal_mode == NO_SIGNAL || |
| 1544 | dev->std_signal_mode == NO_LOCK) { |
| 1545 | *id = V4L2_STD_UNKNOWN; |
| 1546 | return 0; |
| 1547 | } |
| 1548 | if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) { |
| 1549 | *id = V4L2_STD_UNKNOWN; |
| 1550 | } else if (dev->std_signal_mode == CURRENT_STD) { |
| 1551 | *id = dev->std_cap; |
| 1552 | } else if (dev->std_signal_mode == SELECTED_STD) { |
| 1553 | *id = dev->query_std; |
| 1554 | } else { |
| 1555 | *id = vivid_standard[dev->query_std_last]; |
| 1556 | dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard); |
| 1557 | } |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id) |
| 1563 | { |
| 1564 | struct vivid_dev *dev = video_drvdata(file); |
| 1565 | |
| 1566 | if (!vivid_is_sdtv_cap(dev)) |
| 1567 | return -ENODATA; |
| 1568 | if (dev->std_cap == id) |
| 1569 | return 0; |
| 1570 | if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q)) |
| 1571 | return -EBUSY; |
| 1572 | dev->std_cap = id; |
| 1573 | vivid_update_format_cap(dev, false); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh, |
| 1578 | struct v4l2_dv_timings *timings) |
| 1579 | { |
| 1580 | struct vivid_dev *dev = video_drvdata(file); |
| 1581 | |
| 1582 | if (!vivid_is_hdmi_cap(dev)) |
| 1583 | return -ENODATA; |
| 1584 | if (vb2_is_busy(&dev->vb_vid_cap_q)) |
| 1585 | return -EBUSY; |
| 1586 | if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap, |
| 1587 | 0, NULL, NULL)) |
| 1588 | return -EINVAL; |
| 1589 | if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0)) |
| 1590 | return 0; |
| 1591 | dev->dv_timings_cap = *timings; |
| 1592 | vivid_update_format_cap(dev, false); |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
| 1596 | int vidioc_query_dv_timings(struct file *file, void *_fh, |
| 1597 | struct v4l2_dv_timings *timings) |
| 1598 | { |
| 1599 | struct vivid_dev *dev = video_drvdata(file); |
| 1600 | |
| 1601 | if (!vivid_is_hdmi_cap(dev)) |
| 1602 | return -ENODATA; |
| 1603 | if (dev->dv_timings_signal_mode == NO_SIGNAL || |
| 1604 | dev->edid_blocks == 0) |
| 1605 | return -ENOLINK; |
| 1606 | if (dev->dv_timings_signal_mode == NO_LOCK) |
| 1607 | return -ENOLCK; |
| 1608 | if (dev->dv_timings_signal_mode == OUT_OF_RANGE) { |
| 1609 | timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2; |
| 1610 | return -ERANGE; |
| 1611 | } |
| 1612 | if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) { |
| 1613 | *timings = dev->dv_timings_cap; |
| 1614 | } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) { |
| 1615 | *timings = v4l2_dv_timings_presets[dev->query_dv_timings]; |
| 1616 | } else { |
| 1617 | *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last]; |
| 1618 | dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) % |
| 1619 | dev->query_dv_timings_size; |
| 1620 | } |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | int vidioc_s_edid(struct file *file, void *_fh, |
| 1625 | struct v4l2_edid *edid) |
| 1626 | { |
| 1627 | struct vivid_dev *dev = video_drvdata(file); |
| 1628 | |
| 1629 | memset(edid->reserved, 0, sizeof(edid->reserved)); |
| 1630 | if (edid->pad >= dev->num_inputs) |
| 1631 | return -EINVAL; |
| 1632 | if (dev->input_type[edid->pad] != HDMI || edid->start_block) |
| 1633 | return -EINVAL; |
| 1634 | if (edid->blocks == 0) { |
| 1635 | dev->edid_blocks = 0; |
| 1636 | return 0; |
| 1637 | } |
| 1638 | if (edid->blocks > dev->edid_max_blocks) { |
| 1639 | edid->blocks = dev->edid_max_blocks; |
| 1640 | return -E2BIG; |
| 1641 | } |
| 1642 | dev->edid_blocks = edid->blocks; |
| 1643 | memcpy(dev->edid, edid->edid, edid->blocks * 128); |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | int vidioc_enum_framesizes(struct file *file, void *fh, |
| 1648 | struct v4l2_frmsizeenum *fsize) |
| 1649 | { |
| 1650 | struct vivid_dev *dev = video_drvdata(file); |
| 1651 | |
| 1652 | if (!vivid_is_webcam(dev) && !dev->has_scaler_cap) |
| 1653 | return -EINVAL; |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1654 | if (vivid_get_format(dev, fsize->pixel_format) == NULL) |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1655 | return -EINVAL; |
| 1656 | if (vivid_is_webcam(dev)) { |
| 1657 | if (fsize->index >= ARRAY_SIZE(webcam_sizes)) |
| 1658 | return -EINVAL; |
| 1659 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1660 | fsize->discrete = webcam_sizes[fsize->index]; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | if (fsize->index) |
| 1664 | return -EINVAL; |
| 1665 | fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; |
| 1666 | fsize->stepwise.min_width = MIN_WIDTH; |
| 1667 | fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM; |
| 1668 | fsize->stepwise.step_width = 2; |
| 1669 | fsize->stepwise.min_height = MIN_HEIGHT; |
| 1670 | fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM; |
| 1671 | fsize->stepwise.step_height = 2; |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | /* timeperframe is arbitrary and continuous */ |
| 1676 | int vidioc_enum_frameintervals(struct file *file, void *priv, |
| 1677 | struct v4l2_frmivalenum *fival) |
| 1678 | { |
| 1679 | struct vivid_dev *dev = video_drvdata(file); |
| 1680 | const struct vivid_fmt *fmt; |
| 1681 | int i; |
| 1682 | |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1683 | fmt = vivid_get_format(dev, fival->pixel_format); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1684 | if (!fmt) |
| 1685 | return -EINVAL; |
| 1686 | |
| 1687 | if (!vivid_is_webcam(dev)) { |
| 1688 | static const struct v4l2_fract step = { 1, 1 }; |
| 1689 | |
| 1690 | if (fival->index) |
| 1691 | return -EINVAL; |
| 1692 | if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM) |
| 1693 | return -EINVAL; |
| 1694 | if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM) |
| 1695 | return -EINVAL; |
| 1696 | fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS; |
| 1697 | fival->stepwise.min = tpf_min; |
| 1698 | fival->stepwise.max = tpf_max; |
| 1699 | fival->stepwise.step = step; |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++) |
| 1704 | if (fival->width == webcam_sizes[i].width && |
| 1705 | fival->height == webcam_sizes[i].height) |
| 1706 | break; |
| 1707 | if (i == ARRAY_SIZE(webcam_sizes)) |
| 1708 | return -EINVAL; |
| 1709 | if (fival->index >= 2 * (3 - i)) |
| 1710 | return -EINVAL; |
| 1711 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 1712 | fival->discrete = webcam_intervals[fival->index]; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | int vivid_vid_cap_g_parm(struct file *file, void *priv, |
| 1717 | struct v4l2_streamparm *parm) |
| 1718 | { |
| 1719 | struct vivid_dev *dev = video_drvdata(file); |
| 1720 | |
| 1721 | if (parm->type != (dev->multiplanar ? |
| 1722 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
| 1723 | V4L2_BUF_TYPE_VIDEO_CAPTURE)) |
| 1724 | return -EINVAL; |
| 1725 | |
| 1726 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 1727 | parm->parm.capture.timeperframe = dev->timeperframe_vid_cap; |
| 1728 | parm->parm.capture.readbuffers = 1; |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | #define FRACT_CMP(a, OP, b) \ |
| 1733 | ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator) |
| 1734 | |
| 1735 | int vivid_vid_cap_s_parm(struct file *file, void *priv, |
| 1736 | struct v4l2_streamparm *parm) |
| 1737 | { |
| 1738 | struct vivid_dev *dev = video_drvdata(file); |
| 1739 | unsigned ival_sz = 2 * (3 - dev->webcam_size_idx); |
| 1740 | struct v4l2_fract tpf; |
| 1741 | unsigned i; |
| 1742 | |
| 1743 | if (parm->type != (dev->multiplanar ? |
| 1744 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
| 1745 | V4L2_BUF_TYPE_VIDEO_CAPTURE)) |
| 1746 | return -EINVAL; |
| 1747 | if (!vivid_is_webcam(dev)) |
| 1748 | return vivid_vid_cap_g_parm(file, priv, parm); |
| 1749 | |
| 1750 | tpf = parm->parm.capture.timeperframe; |
| 1751 | |
| 1752 | if (tpf.denominator == 0) |
| 1753 | tpf = webcam_intervals[ival_sz - 1]; |
| 1754 | for (i = 0; i < ival_sz; i++) |
| 1755 | if (FRACT_CMP(tpf, >=, webcam_intervals[i])) |
| 1756 | break; |
| 1757 | if (i == ival_sz) |
| 1758 | i = ival_sz - 1; |
| 1759 | dev->webcam_ival_idx = i; |
| 1760 | tpf = webcam_intervals[dev->webcam_ival_idx]; |
| 1761 | tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf; |
| 1762 | tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf; |
| 1763 | |
| 1764 | /* resync the thread's timings */ |
| 1765 | dev->cap_seq_resync = true; |
| 1766 | dev->timeperframe_vid_cap = tpf; |
| 1767 | parm->parm.capture.timeperframe = tpf; |
| 1768 | parm->parm.capture.readbuffers = 1; |
| 1769 | return 0; |
| 1770 | } |