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