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