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 | |
| 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 | |
| 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; |
| 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 | |
| 745 | int 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 | |
| 755 | int 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 | |
| 765 | int 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 | |
| 775 | int 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 | |
| 785 | int 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 | |
| 795 | int 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 | |
| 805 | int 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 | |
| 851 | int 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 | |
| 987 | int 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 | |
| 1012 | int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv, |
| 1013 | struct v4l2_fmtdesc *f) |
| 1014 | { |
| 1015 | const struct vivid_fmt *fmt; |
| 1016 | |
| 1017 | if (f->index >= ARRAY_SIZE(formats_ovl)) |
| 1018 | return -EINVAL; |
| 1019 | |
| 1020 | fmt = &formats_ovl[f->index]; |
| 1021 | |
| 1022 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 1023 | f->pixelformat = fmt->fourcc; |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | int vidioc_g_fmt_vid_overlay(struct file *file, void *priv, |
| 1028 | struct v4l2_format *f) |
| 1029 | { |
| 1030 | struct vivid_dev *dev = video_drvdata(file); |
| 1031 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1032 | struct v4l2_window *win = &f->fmt.win; |
| 1033 | unsigned clipcount = win->clipcount; |
| 1034 | |
| 1035 | win->w.top = dev->overlay_cap_top; |
| 1036 | win->w.left = dev->overlay_cap_left; |
| 1037 | win->w.width = compose->width; |
| 1038 | win->w.height = compose->height; |
| 1039 | win->field = dev->overlay_cap_field; |
| 1040 | win->clipcount = dev->clipcount_cap; |
| 1041 | if (clipcount > dev->clipcount_cap) |
| 1042 | clipcount = dev->clipcount_cap; |
| 1043 | if (dev->bitmap_cap == NULL) |
| 1044 | win->bitmap = NULL; |
| 1045 | else if (win->bitmap) { |
| 1046 | if (copy_to_user(win->bitmap, dev->bitmap_cap, |
| 1047 | ((compose->width + 7) / 8) * compose->height)) |
| 1048 | return -EFAULT; |
| 1049 | } |
| 1050 | if (clipcount && win->clips) { |
| 1051 | if (copy_to_user(win->clips, dev->clips_cap, |
| 1052 | clipcount * sizeof(dev->clips_cap[0]))) |
| 1053 | return -EFAULT; |
| 1054 | } |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
| 1058 | int vidioc_try_fmt_vid_overlay(struct file *file, void *priv, |
| 1059 | struct v4l2_format *f) |
| 1060 | { |
| 1061 | struct vivid_dev *dev = video_drvdata(file); |
| 1062 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1063 | struct v4l2_window *win = &f->fmt.win; |
| 1064 | int i, j; |
| 1065 | |
| 1066 | win->w.left = clamp_t(int, win->w.left, |
| 1067 | -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width); |
| 1068 | win->w.top = clamp_t(int, win->w.top, |
| 1069 | -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height); |
| 1070 | win->w.width = compose->width; |
| 1071 | win->w.height = compose->height; |
| 1072 | if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP) |
| 1073 | win->field = V4L2_FIELD_ANY; |
| 1074 | win->chromakey = 0; |
| 1075 | win->global_alpha = 0; |
| 1076 | if (win->clipcount && !win->clips) |
| 1077 | win->clipcount = 0; |
| 1078 | if (win->clipcount > MAX_CLIPS) |
| 1079 | win->clipcount = MAX_CLIPS; |
| 1080 | if (win->clipcount) { |
| 1081 | if (copy_from_user(dev->try_clips_cap, win->clips, |
| 1082 | win->clipcount * sizeof(dev->clips_cap[0]))) |
| 1083 | return -EFAULT; |
| 1084 | for (i = 0; i < win->clipcount; i++) { |
| 1085 | struct v4l2_rect *r = &dev->try_clips_cap[i].c; |
| 1086 | |
| 1087 | r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1); |
| 1088 | r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top); |
| 1089 | r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1); |
| 1090 | r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left); |
| 1091 | } |
| 1092 | /* |
| 1093 | * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small |
| 1094 | * number and it's typically a one-time deal. |
| 1095 | */ |
| 1096 | for (i = 0; i < win->clipcount - 1; i++) { |
| 1097 | struct v4l2_rect *r1 = &dev->try_clips_cap[i].c; |
| 1098 | |
| 1099 | for (j = i + 1; j < win->clipcount; j++) { |
| 1100 | struct v4l2_rect *r2 = &dev->try_clips_cap[j].c; |
| 1101 | |
| 1102 | if (rect_overlap(r1, r2)) |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | } |
| 1106 | if (copy_to_user(win->clips, dev->try_clips_cap, |
| 1107 | win->clipcount * sizeof(dev->clips_cap[0]))) |
| 1108 | return -EFAULT; |
| 1109 | } |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | int vidioc_s_fmt_vid_overlay(struct file *file, void *priv, |
| 1114 | struct v4l2_format *f) |
| 1115 | { |
| 1116 | struct vivid_dev *dev = video_drvdata(file); |
| 1117 | const struct v4l2_rect *compose = &dev->compose_cap; |
| 1118 | struct v4l2_window *win = &f->fmt.win; |
| 1119 | int ret = vidioc_try_fmt_vid_overlay(file, priv, f); |
| 1120 | unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height; |
| 1121 | unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]); |
| 1122 | void *new_bitmap = NULL; |
| 1123 | |
| 1124 | if (ret) |
| 1125 | return ret; |
| 1126 | |
| 1127 | if (win->bitmap) { |
| 1128 | new_bitmap = vzalloc(bitmap_size); |
| 1129 | |
| 1130 | if (new_bitmap == NULL) |
| 1131 | return -ENOMEM; |
| 1132 | if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) { |
| 1133 | vfree(new_bitmap); |
| 1134 | return -EFAULT; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | dev->overlay_cap_top = win->w.top; |
| 1139 | dev->overlay_cap_left = win->w.left; |
| 1140 | dev->overlay_cap_field = win->field; |
| 1141 | vfree(dev->bitmap_cap); |
| 1142 | dev->bitmap_cap = new_bitmap; |
| 1143 | dev->clipcount_cap = win->clipcount; |
| 1144 | if (dev->clipcount_cap) |
| 1145 | memcpy(dev->clips_cap, dev->try_clips_cap, clips_size); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i) |
| 1150 | { |
| 1151 | struct vivid_dev *dev = video_drvdata(file); |
| 1152 | |
| 1153 | if (i && dev->fb_vbase_cap == NULL) |
| 1154 | return -EINVAL; |
| 1155 | |
| 1156 | if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) { |
| 1157 | dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n"); |
| 1158 | return -EINVAL; |
| 1159 | } |
| 1160 | |
| 1161 | if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh) |
| 1162 | return -EBUSY; |
| 1163 | dev->overlay_cap_owner = i ? fh : NULL; |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | int vivid_vid_cap_g_fbuf(struct file *file, void *fh, |
| 1168 | struct v4l2_framebuffer *a) |
| 1169 | { |
| 1170 | struct vivid_dev *dev = video_drvdata(file); |
| 1171 | |
| 1172 | *a = dev->fb_cap; |
| 1173 | a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING | |
| 1174 | V4L2_FBUF_CAP_LIST_CLIPPING; |
| 1175 | a->flags = V4L2_FBUF_FLAG_PRIMARY; |
| 1176 | a->fmt.field = V4L2_FIELD_NONE; |
| 1177 | a->fmt.colorspace = V4L2_COLORSPACE_SRGB; |
| 1178 | a->fmt.priv = 0; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | int vivid_vid_cap_s_fbuf(struct file *file, void *fh, |
| 1183 | const struct v4l2_framebuffer *a) |
| 1184 | { |
| 1185 | struct vivid_dev *dev = video_drvdata(file); |
| 1186 | const struct vivid_fmt *fmt; |
| 1187 | |
| 1188 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO)) |
| 1189 | return -EPERM; |
| 1190 | |
| 1191 | if (dev->overlay_cap_owner) |
| 1192 | return -EBUSY; |
| 1193 | |
| 1194 | if (a->base == NULL) { |
| 1195 | dev->fb_cap.base = NULL; |
| 1196 | dev->fb_vbase_cap = NULL; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | if (a->fmt.width < 48 || a->fmt.height < 32) |
| 1201 | return -EINVAL; |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1202 | fmt = vivid_get_format(dev, a->fmt.pixelformat); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1203 | if (!fmt || !fmt->can_do_overlay) |
| 1204 | return -EINVAL; |
| 1205 | if (a->fmt.bytesperline < (a->fmt.width * fmt->depth) / 8) |
| 1206 | return -EINVAL; |
| 1207 | if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage) |
| 1208 | return -EINVAL; |
| 1209 | |
| 1210 | dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base); |
| 1211 | dev->fb_cap = *a; |
| 1212 | dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left, |
| 1213 | -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width); |
| 1214 | dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top, |
| 1215 | -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height); |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static const struct v4l2_audio vivid_audio_inputs[] = { |
| 1220 | { 0, "TV", V4L2_AUDCAP_STEREO }, |
| 1221 | { 1, "Line-In", V4L2_AUDCAP_STEREO }, |
| 1222 | }; |
| 1223 | |
| 1224 | int vidioc_enum_input(struct file *file, void *priv, |
| 1225 | struct v4l2_input *inp) |
| 1226 | { |
| 1227 | struct vivid_dev *dev = video_drvdata(file); |
| 1228 | |
| 1229 | if (inp->index >= dev->num_inputs) |
| 1230 | return -EINVAL; |
| 1231 | |
| 1232 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 1233 | switch (dev->input_type[inp->index]) { |
| 1234 | case WEBCAM: |
| 1235 | snprintf(inp->name, sizeof(inp->name), "Webcam %u", |
| 1236 | dev->input_name_counter[inp->index]); |
| 1237 | inp->capabilities = 0; |
| 1238 | break; |
| 1239 | case TV: |
| 1240 | snprintf(inp->name, sizeof(inp->name), "TV %u", |
| 1241 | dev->input_name_counter[inp->index]); |
| 1242 | inp->type = V4L2_INPUT_TYPE_TUNER; |
| 1243 | inp->std = V4L2_STD_ALL; |
| 1244 | if (dev->has_audio_inputs) |
| 1245 | inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1; |
| 1246 | inp->capabilities = V4L2_IN_CAP_STD; |
| 1247 | break; |
| 1248 | case SVID: |
| 1249 | snprintf(inp->name, sizeof(inp->name), "S-Video %u", |
| 1250 | dev->input_name_counter[inp->index]); |
| 1251 | inp->std = V4L2_STD_ALL; |
| 1252 | if (dev->has_audio_inputs) |
| 1253 | inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1; |
| 1254 | inp->capabilities = V4L2_IN_CAP_STD; |
| 1255 | break; |
| 1256 | case HDMI: |
| 1257 | snprintf(inp->name, sizeof(inp->name), "HDMI %u", |
| 1258 | dev->input_name_counter[inp->index]); |
| 1259 | inp->capabilities = V4L2_IN_CAP_DV_TIMINGS; |
| 1260 | if (dev->edid_blocks == 0 || |
| 1261 | dev->dv_timings_signal_mode == NO_SIGNAL) |
| 1262 | inp->status |= V4L2_IN_ST_NO_SIGNAL; |
| 1263 | else if (dev->dv_timings_signal_mode == NO_LOCK || |
| 1264 | dev->dv_timings_signal_mode == OUT_OF_RANGE) |
| 1265 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1266 | break; |
| 1267 | } |
| 1268 | if (dev->sensor_hflip) |
| 1269 | inp->status |= V4L2_IN_ST_HFLIP; |
| 1270 | if (dev->sensor_vflip) |
| 1271 | inp->status |= V4L2_IN_ST_VFLIP; |
| 1272 | if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) { |
| 1273 | if (dev->std_signal_mode == NO_SIGNAL) { |
| 1274 | inp->status |= V4L2_IN_ST_NO_SIGNAL; |
| 1275 | } else if (dev->std_signal_mode == NO_LOCK) { |
| 1276 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1277 | } else if (vivid_is_tv_cap(dev)) { |
| 1278 | switch (tpg_g_quality(&dev->tpg)) { |
| 1279 | case TPG_QUAL_GRAY: |
| 1280 | inp->status |= V4L2_IN_ST_COLOR_KILL; |
| 1281 | break; |
| 1282 | case TPG_QUAL_NOISE: |
| 1283 | inp->status |= V4L2_IN_ST_NO_H_LOCK; |
| 1284 | break; |
| 1285 | default: |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | int vidioc_g_input(struct file *file, void *priv, unsigned *i) |
| 1294 | { |
| 1295 | struct vivid_dev *dev = video_drvdata(file); |
| 1296 | |
| 1297 | *i = dev->input; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | int vidioc_s_input(struct file *file, void *priv, unsigned i) |
| 1302 | { |
| 1303 | struct vivid_dev *dev = video_drvdata(file); |
| 1304 | struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt; |
| 1305 | unsigned brightness; |
| 1306 | |
| 1307 | if (i >= dev->num_inputs) |
| 1308 | return -EINVAL; |
| 1309 | |
| 1310 | if (i == dev->input) |
| 1311 | return 0; |
| 1312 | |
| 1313 | if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q)) |
| 1314 | return -EBUSY; |
| 1315 | |
| 1316 | dev->input = i; |
| 1317 | dev->vid_cap_dev.tvnorms = 0; |
| 1318 | if (dev->input_type[i] == TV || dev->input_type[i] == SVID) { |
| 1319 | dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1; |
| 1320 | dev->vid_cap_dev.tvnorms = V4L2_STD_ALL; |
| 1321 | } |
| 1322 | dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms; |
| 1323 | vivid_update_format_cap(dev, false); |
| 1324 | |
| 1325 | if (dev->colorspace) { |
| 1326 | switch (dev->input_type[i]) { |
| 1327 | case WEBCAM: |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1328 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1329 | break; |
| 1330 | case TV: |
| 1331 | case SVID: |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1332 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1333 | break; |
| 1334 | case HDMI: |
| 1335 | if (bt->standards & V4L2_DV_BT_STD_CEA861) { |
| 1336 | if (dev->src_rect.width == 720 && dev->src_rect.height <= 576) |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1337 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1338 | else |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1339 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1340 | } else { |
Hans Verkuil | cd8adbe | 2014-11-17 10:21:19 -0300 | [diff] [blame] | 1341 | v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1342 | } |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Modify the brightness range depending on the input. |
| 1349 | * This makes it easy to use vivid to test if applications can |
| 1350 | * handle control range modifications and is also how this is |
| 1351 | * typically used in practice as different inputs may be hooked |
| 1352 | * up to different receivers with different control ranges. |
| 1353 | */ |
| 1354 | brightness = 128 * i + dev->input_brightness[i]; |
| 1355 | v4l2_ctrl_modify_range(dev->brightness, |
| 1356 | 128 * i, 255 + 128 * i, 1, 128 + 128 * i); |
| 1357 | v4l2_ctrl_s_ctrl(dev->brightness, brightness); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin) |
| 1362 | { |
| 1363 | if (vin->index >= ARRAY_SIZE(vivid_audio_inputs)) |
| 1364 | return -EINVAL; |
| 1365 | *vin = vivid_audio_inputs[vin->index]; |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin) |
| 1370 | { |
| 1371 | struct vivid_dev *dev = video_drvdata(file); |
| 1372 | |
| 1373 | if (!vivid_is_sdtv_cap(dev)) |
| 1374 | return -EINVAL; |
| 1375 | *vin = vivid_audio_inputs[dev->tv_audio_input]; |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin) |
| 1380 | { |
| 1381 | struct vivid_dev *dev = video_drvdata(file); |
| 1382 | |
| 1383 | if (!vivid_is_sdtv_cap(dev)) |
| 1384 | return -EINVAL; |
| 1385 | if (vin->index >= ARRAY_SIZE(vivid_audio_inputs)) |
| 1386 | return -EINVAL; |
| 1387 | dev->tv_audio_input = vin->index; |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf) |
| 1392 | { |
| 1393 | struct vivid_dev *dev = video_drvdata(file); |
| 1394 | |
| 1395 | if (vf->tuner != 0) |
| 1396 | return -EINVAL; |
| 1397 | vf->frequency = dev->tv_freq; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf) |
| 1402 | { |
| 1403 | struct vivid_dev *dev = video_drvdata(file); |
| 1404 | |
| 1405 | if (vf->tuner != 0) |
| 1406 | return -EINVAL; |
| 1407 | dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ); |
| 1408 | if (vivid_is_tv_cap(dev)) |
| 1409 | vivid_update_quality(dev); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt) |
| 1414 | { |
| 1415 | struct vivid_dev *dev = video_drvdata(file); |
| 1416 | |
| 1417 | if (vt->index != 0) |
| 1418 | return -EINVAL; |
| 1419 | if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2) |
| 1420 | return -EINVAL; |
| 1421 | dev->tv_audmode = vt->audmode; |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt) |
| 1426 | { |
| 1427 | struct vivid_dev *dev = video_drvdata(file); |
| 1428 | enum tpg_quality qual; |
| 1429 | |
| 1430 | if (vt->index != 0) |
| 1431 | return -EINVAL; |
| 1432 | |
| 1433 | vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO | |
| 1434 | V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2; |
| 1435 | vt->audmode = dev->tv_audmode; |
| 1436 | vt->rangelow = MIN_TV_FREQ; |
| 1437 | vt->rangehigh = MAX_TV_FREQ; |
| 1438 | qual = vivid_get_quality(dev, &vt->afc); |
| 1439 | if (qual == TPG_QUAL_COLOR) |
| 1440 | vt->signal = 0xffff; |
| 1441 | else if (qual == TPG_QUAL_GRAY) |
| 1442 | vt->signal = 0x8000; |
| 1443 | else |
| 1444 | vt->signal = 0; |
| 1445 | if (qual == TPG_QUAL_NOISE) { |
| 1446 | vt->rxsubchans = 0; |
| 1447 | } else if (qual == TPG_QUAL_GRAY) { |
| 1448 | vt->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 1449 | } else { |
| 1450 | unsigned channel_nr = dev->tv_freq / (6 * 16); |
| 1451 | unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3; |
| 1452 | |
| 1453 | switch (channel_nr % options) { |
| 1454 | case 0: |
| 1455 | vt->rxsubchans = V4L2_TUNER_SUB_MONO; |
| 1456 | break; |
| 1457 | case 1: |
| 1458 | vt->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 1459 | break; |
| 1460 | case 2: |
| 1461 | if (dev->std_cap & V4L2_STD_NTSC_M) |
| 1462 | vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP; |
| 1463 | else |
| 1464 | vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; |
| 1465 | break; |
| 1466 | case 3: |
| 1467 | vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP; |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | strlcpy(vt->name, "TV Tuner", sizeof(vt->name)); |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | /* Must remain in sync with the vivid_ctrl_standard_strings array */ |
| 1476 | const v4l2_std_id vivid_standard[] = { |
| 1477 | V4L2_STD_NTSC_M, |
| 1478 | V4L2_STD_NTSC_M_JP, |
| 1479 | V4L2_STD_NTSC_M_KR, |
| 1480 | V4L2_STD_NTSC_443, |
| 1481 | V4L2_STD_PAL_BG | V4L2_STD_PAL_H, |
| 1482 | V4L2_STD_PAL_I, |
| 1483 | V4L2_STD_PAL_DK, |
| 1484 | V4L2_STD_PAL_M, |
| 1485 | V4L2_STD_PAL_N, |
| 1486 | V4L2_STD_PAL_Nc, |
| 1487 | V4L2_STD_PAL_60, |
| 1488 | V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, |
| 1489 | V4L2_STD_SECAM_DK, |
| 1490 | V4L2_STD_SECAM_L, |
| 1491 | V4L2_STD_SECAM_LC, |
| 1492 | V4L2_STD_UNKNOWN |
| 1493 | }; |
| 1494 | |
| 1495 | /* Must remain in sync with the vivid_standard array */ |
| 1496 | const char * const vivid_ctrl_standard_strings[] = { |
| 1497 | "NTSC-M", |
| 1498 | "NTSC-M-JP", |
| 1499 | "NTSC-M-KR", |
| 1500 | "NTSC-443", |
| 1501 | "PAL-BGH", |
| 1502 | "PAL-I", |
| 1503 | "PAL-DK", |
| 1504 | "PAL-M", |
| 1505 | "PAL-N", |
| 1506 | "PAL-Nc", |
| 1507 | "PAL-60", |
| 1508 | "SECAM-BGH", |
| 1509 | "SECAM-DK", |
| 1510 | "SECAM-L", |
| 1511 | "SECAM-Lc", |
| 1512 | NULL, |
| 1513 | }; |
| 1514 | |
| 1515 | int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id) |
| 1516 | { |
| 1517 | struct vivid_dev *dev = video_drvdata(file); |
| 1518 | |
| 1519 | if (!vivid_is_sdtv_cap(dev)) |
| 1520 | return -ENODATA; |
| 1521 | if (dev->std_signal_mode == NO_SIGNAL || |
| 1522 | dev->std_signal_mode == NO_LOCK) { |
| 1523 | *id = V4L2_STD_UNKNOWN; |
| 1524 | return 0; |
| 1525 | } |
| 1526 | if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) { |
| 1527 | *id = V4L2_STD_UNKNOWN; |
| 1528 | } else if (dev->std_signal_mode == CURRENT_STD) { |
| 1529 | *id = dev->std_cap; |
| 1530 | } else if (dev->std_signal_mode == SELECTED_STD) { |
| 1531 | *id = dev->query_std; |
| 1532 | } else { |
| 1533 | *id = vivid_standard[dev->query_std_last]; |
| 1534 | dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard); |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id) |
| 1541 | { |
| 1542 | struct vivid_dev *dev = video_drvdata(file); |
| 1543 | |
| 1544 | if (!vivid_is_sdtv_cap(dev)) |
| 1545 | return -ENODATA; |
| 1546 | if (dev->std_cap == id) |
| 1547 | return 0; |
| 1548 | if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q)) |
| 1549 | return -EBUSY; |
| 1550 | dev->std_cap = id; |
| 1551 | vivid_update_format_cap(dev, false); |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh, |
| 1556 | struct v4l2_dv_timings *timings) |
| 1557 | { |
| 1558 | struct vivid_dev *dev = video_drvdata(file); |
| 1559 | |
| 1560 | if (!vivid_is_hdmi_cap(dev)) |
| 1561 | return -ENODATA; |
| 1562 | if (vb2_is_busy(&dev->vb_vid_cap_q)) |
| 1563 | return -EBUSY; |
| 1564 | if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap, |
| 1565 | 0, NULL, NULL)) |
| 1566 | return -EINVAL; |
| 1567 | if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0)) |
| 1568 | return 0; |
| 1569 | dev->dv_timings_cap = *timings; |
| 1570 | vivid_update_format_cap(dev, false); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | int vidioc_query_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 (dev->dv_timings_signal_mode == NO_SIGNAL || |
| 1582 | dev->edid_blocks == 0) |
| 1583 | return -ENOLINK; |
| 1584 | if (dev->dv_timings_signal_mode == NO_LOCK) |
| 1585 | return -ENOLCK; |
| 1586 | if (dev->dv_timings_signal_mode == OUT_OF_RANGE) { |
| 1587 | timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2; |
| 1588 | return -ERANGE; |
| 1589 | } |
| 1590 | if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) { |
| 1591 | *timings = dev->dv_timings_cap; |
| 1592 | } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) { |
| 1593 | *timings = v4l2_dv_timings_presets[dev->query_dv_timings]; |
| 1594 | } else { |
| 1595 | *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last]; |
| 1596 | dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) % |
| 1597 | dev->query_dv_timings_size; |
| 1598 | } |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | int vidioc_s_edid(struct file *file, void *_fh, |
| 1603 | struct v4l2_edid *edid) |
| 1604 | { |
| 1605 | struct vivid_dev *dev = video_drvdata(file); |
| 1606 | |
| 1607 | memset(edid->reserved, 0, sizeof(edid->reserved)); |
| 1608 | if (edid->pad >= dev->num_inputs) |
| 1609 | return -EINVAL; |
| 1610 | if (dev->input_type[edid->pad] != HDMI || edid->start_block) |
| 1611 | return -EINVAL; |
| 1612 | if (edid->blocks == 0) { |
| 1613 | dev->edid_blocks = 0; |
| 1614 | return 0; |
| 1615 | } |
| 1616 | if (edid->blocks > dev->edid_max_blocks) { |
| 1617 | edid->blocks = dev->edid_max_blocks; |
| 1618 | return -E2BIG; |
| 1619 | } |
| 1620 | dev->edid_blocks = edid->blocks; |
| 1621 | memcpy(dev->edid, edid->edid, edid->blocks * 128); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | int vidioc_enum_framesizes(struct file *file, void *fh, |
| 1626 | struct v4l2_frmsizeenum *fsize) |
| 1627 | { |
| 1628 | struct vivid_dev *dev = video_drvdata(file); |
| 1629 | |
| 1630 | if (!vivid_is_webcam(dev) && !dev->has_scaler_cap) |
| 1631 | return -EINVAL; |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1632 | if (vivid_get_format(dev, fsize->pixel_format) == NULL) |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1633 | return -EINVAL; |
| 1634 | if (vivid_is_webcam(dev)) { |
| 1635 | if (fsize->index >= ARRAY_SIZE(webcam_sizes)) |
| 1636 | return -EINVAL; |
| 1637 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; |
| 1638 | fsize->discrete = webcam_sizes[fsize->index]; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | if (fsize->index) |
| 1642 | return -EINVAL; |
| 1643 | fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; |
| 1644 | fsize->stepwise.min_width = MIN_WIDTH; |
| 1645 | fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM; |
| 1646 | fsize->stepwise.step_width = 2; |
| 1647 | fsize->stepwise.min_height = MIN_HEIGHT; |
| 1648 | fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM; |
| 1649 | fsize->stepwise.step_height = 2; |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | /* timeperframe is arbitrary and continuous */ |
| 1654 | int vidioc_enum_frameintervals(struct file *file, void *priv, |
| 1655 | struct v4l2_frmivalenum *fival) |
| 1656 | { |
| 1657 | struct vivid_dev *dev = video_drvdata(file); |
| 1658 | const struct vivid_fmt *fmt; |
| 1659 | int i; |
| 1660 | |
Mauro Carvalho Chehab | 1fc78bc | 2014-09-02 17:52:07 -0300 | [diff] [blame] | 1661 | fmt = vivid_get_format(dev, fival->pixel_format); |
Hans Verkuil | ef834f7 | 2014-08-25 07:56:18 -0300 | [diff] [blame] | 1662 | if (!fmt) |
| 1663 | return -EINVAL; |
| 1664 | |
| 1665 | if (!vivid_is_webcam(dev)) { |
| 1666 | static const struct v4l2_fract step = { 1, 1 }; |
| 1667 | |
| 1668 | if (fival->index) |
| 1669 | return -EINVAL; |
| 1670 | if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM) |
| 1671 | return -EINVAL; |
| 1672 | if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM) |
| 1673 | return -EINVAL; |
| 1674 | fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS; |
| 1675 | fival->stepwise.min = tpf_min; |
| 1676 | fival->stepwise.max = tpf_max; |
| 1677 | fival->stepwise.step = step; |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++) |
| 1682 | if (fival->width == webcam_sizes[i].width && |
| 1683 | fival->height == webcam_sizes[i].height) |
| 1684 | break; |
| 1685 | if (i == ARRAY_SIZE(webcam_sizes)) |
| 1686 | return -EINVAL; |
| 1687 | if (fival->index >= 2 * (3 - i)) |
| 1688 | return -EINVAL; |
| 1689 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 1690 | fival->discrete = webcam_intervals[fival->index]; |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | int vivid_vid_cap_g_parm(struct file *file, void *priv, |
| 1695 | struct v4l2_streamparm *parm) |
| 1696 | { |
| 1697 | struct vivid_dev *dev = video_drvdata(file); |
| 1698 | |
| 1699 | if (parm->type != (dev->multiplanar ? |
| 1700 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
| 1701 | V4L2_BUF_TYPE_VIDEO_CAPTURE)) |
| 1702 | return -EINVAL; |
| 1703 | |
| 1704 | parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
| 1705 | parm->parm.capture.timeperframe = dev->timeperframe_vid_cap; |
| 1706 | parm->parm.capture.readbuffers = 1; |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | #define FRACT_CMP(a, OP, b) \ |
| 1711 | ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator) |
| 1712 | |
| 1713 | int vivid_vid_cap_s_parm(struct file *file, void *priv, |
| 1714 | struct v4l2_streamparm *parm) |
| 1715 | { |
| 1716 | struct vivid_dev *dev = video_drvdata(file); |
| 1717 | unsigned ival_sz = 2 * (3 - dev->webcam_size_idx); |
| 1718 | struct v4l2_fract tpf; |
| 1719 | unsigned i; |
| 1720 | |
| 1721 | if (parm->type != (dev->multiplanar ? |
| 1722 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
| 1723 | V4L2_BUF_TYPE_VIDEO_CAPTURE)) |
| 1724 | return -EINVAL; |
| 1725 | if (!vivid_is_webcam(dev)) |
| 1726 | return vivid_vid_cap_g_parm(file, priv, parm); |
| 1727 | |
| 1728 | tpf = parm->parm.capture.timeperframe; |
| 1729 | |
| 1730 | if (tpf.denominator == 0) |
| 1731 | tpf = webcam_intervals[ival_sz - 1]; |
| 1732 | for (i = 0; i < ival_sz; i++) |
| 1733 | if (FRACT_CMP(tpf, >=, webcam_intervals[i])) |
| 1734 | break; |
| 1735 | if (i == ival_sz) |
| 1736 | i = ival_sz - 1; |
| 1737 | dev->webcam_ival_idx = i; |
| 1738 | tpf = webcam_intervals[dev->webcam_ival_idx]; |
| 1739 | tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf; |
| 1740 | tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf; |
| 1741 | |
| 1742 | /* resync the thread's timings */ |
| 1743 | dev->cap_seq_resync = true; |
| 1744 | dev->timeperframe_vid_cap = tpf; |
| 1745 | parm->parm.capture.timeperframe = tpf; |
| 1746 | parm->parm.capture.readbuffers = 1; |
| 1747 | return 0; |
| 1748 | } |