blob: 8f388ff31ebb8732fd1793778c578f9395df777a [file] [log] [blame]
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001/*
2 * Video capture interface for Linux version 2
3 *
4 * A generic framework to process V4L2 ioctl commands.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
Alan Coxd9b01442008-10-27 15:13:47 -030011 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil35ea11f2008-07-20 08:12:02 -030012 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030017#include <linux/types.h>
18#include <linux/kernel.h>
Mauro Carvalho Chehabae6db512011-06-24 13:14:14 -030019#include <linux/version.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030020
Hans Verkuil35ea11f2008-07-20 08:12:02 -030021#include <linux/videodev2.h>
22
Hans Verkuil35ea11f2008-07-20 08:12:02 -030023#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030025#include <media/v4l2-ctrls.h>
Sakari Ailusd3d7c962010-03-27 11:02:10 -030026#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
Hans Verkuil99cd47bc2011-03-11 19:00:56 -030028#include <media/v4l2-device.h>
Hans Verkuil80b36e02009-02-07 11:00:02 -030029#include <media/v4l2-chip-ident.h>
Hans Verkuil5a5adf62012-06-22 07:29:35 -030030#include <media/videobuf2-core.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030031
Lucas De Marchi25985ed2011-03-30 22:57:33 -030032/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030033 * not including, the specified field is cleared. */
34#define CLEAR_AFTER_FIELD(p, field) \
35 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
36 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
37
Hans Verkuil35ea11f2008-07-20 08:12:02 -030038struct std_descr {
39 v4l2_std_id std;
40 const char *descr;
41};
42
43static const struct std_descr standards[] = {
44 { V4L2_STD_NTSC, "NTSC" },
45 { V4L2_STD_NTSC_M, "NTSC-M" },
46 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
47 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
48 { V4L2_STD_NTSC_443, "NTSC-443" },
49 { V4L2_STD_PAL, "PAL" },
50 { V4L2_STD_PAL_BG, "PAL-BG" },
51 { V4L2_STD_PAL_B, "PAL-B" },
52 { V4L2_STD_PAL_B1, "PAL-B1" },
53 { V4L2_STD_PAL_G, "PAL-G" },
54 { V4L2_STD_PAL_H, "PAL-H" },
55 { V4L2_STD_PAL_I, "PAL-I" },
56 { V4L2_STD_PAL_DK, "PAL-DK" },
57 { V4L2_STD_PAL_D, "PAL-D" },
58 { V4L2_STD_PAL_D1, "PAL-D1" },
59 { V4L2_STD_PAL_K, "PAL-K" },
60 { V4L2_STD_PAL_M, "PAL-M" },
61 { V4L2_STD_PAL_N, "PAL-N" },
62 { V4L2_STD_PAL_Nc, "PAL-Nc" },
63 { V4L2_STD_PAL_60, "PAL-60" },
64 { V4L2_STD_SECAM, "SECAM" },
65 { V4L2_STD_SECAM_B, "SECAM-B" },
66 { V4L2_STD_SECAM_G, "SECAM-G" },
67 { V4L2_STD_SECAM_H, "SECAM-H" },
68 { V4L2_STD_SECAM_DK, "SECAM-DK" },
69 { V4L2_STD_SECAM_D, "SECAM-D" },
70 { V4L2_STD_SECAM_K, "SECAM-K" },
71 { V4L2_STD_SECAM_K1, "SECAM-K1" },
72 { V4L2_STD_SECAM_L, "SECAM-L" },
73 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
74 { 0, "Unknown" }
75};
76
77/* video4linux standard ID conversion to standard name
78 */
79const char *v4l2_norm_to_name(v4l2_std_id id)
80{
81 u32 myid = id;
82 int i;
83
84 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
85 64 bit comparations. So, on that architecture, with some gcc
86 variants, compilation fails. Currently, the max value is 30bit wide.
87 */
88 BUG_ON(myid != id);
89
90 for (i = 0; standards[i].std; i++)
91 if (myid == standards[i].std)
92 break;
93 return standards[i].descr;
94}
95EXPORT_SYMBOL(v4l2_norm_to_name);
96
Trent Piepho51f0b8d52009-03-04 01:21:02 -030097/* Returns frame period for the given standard */
98void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
99{
100 if (id & V4L2_STD_525_60) {
101 frameperiod->numerator = 1001;
102 frameperiod->denominator = 30000;
103 } else {
104 frameperiod->numerator = 1;
105 frameperiod->denominator = 25;
106 }
107}
108EXPORT_SYMBOL(v4l2_video_std_frame_period);
109
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300110/* Fill in the fields of a v4l2_standard structure according to the
111 'id' and 'transmission' parameters. Returns negative on error. */
112int v4l2_video_std_construct(struct v4l2_standard *vs,
113 int id, const char *name)
114{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300115 vs->id = id;
116 v4l2_video_std_frame_period(id, &vs->frameperiod);
117 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300118 strlcpy(vs->name, name, sizeof(vs->name));
119 return 0;
120}
121EXPORT_SYMBOL(v4l2_video_std_construct);
122
123/* ----------------------------------------------------------------- */
124/* some arrays for pretty-printing debug messages of enum types */
125
126const char *v4l2_field_names[] = {
127 [V4L2_FIELD_ANY] = "any",
128 [V4L2_FIELD_NONE] = "none",
129 [V4L2_FIELD_TOP] = "top",
130 [V4L2_FIELD_BOTTOM] = "bottom",
131 [V4L2_FIELD_INTERLACED] = "interlaced",
132 [V4L2_FIELD_SEQ_TB] = "seq-tb",
133 [V4L2_FIELD_SEQ_BT] = "seq-bt",
134 [V4L2_FIELD_ALTERNATE] = "alternate",
135 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
136 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
137};
138EXPORT_SYMBOL(v4l2_field_names);
139
140const char *v4l2_type_names[] = {
141 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
142 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
143 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
144 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
145 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
146 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
147 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
148 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300149 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
150 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300151};
152EXPORT_SYMBOL(v4l2_type_names);
153
154static const char *v4l2_memory_names[] = {
155 [V4L2_MEMORY_MMAP] = "mmap",
156 [V4L2_MEMORY_USERPTR] = "userptr",
157 [V4L2_MEMORY_OVERLAY] = "overlay",
158};
159
Hans Verkuild9246242012-10-02 02:47:59 -0300160#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300161
162/* ------------------------------------------------------------------ */
163/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300164
Hans Verkuil59076122012-06-22 06:09:54 -0300165static void v4l_print_querycap(const void *arg, bool write_only)
166{
167 const struct v4l2_capability *p = arg;
168
169 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
170 "capabilities=0x%08x, device_caps=0x%08x\n",
171 p->driver, p->card, p->bus_info,
172 p->version, p->capabilities, p->device_caps);
173}
174
175static void v4l_print_enuminput(const void *arg, bool write_only)
176{
177 const struct v4l2_input *p = arg;
178
179 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
180 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
181 p->index, p->name, p->type, p->audioset, p->tuner,
182 (unsigned long long)p->std, p->status, p->capabilities);
183}
184
185static void v4l_print_enumoutput(const void *arg, bool write_only)
186{
187 const struct v4l2_output *p = arg;
188
189 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
190 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
191 p->index, p->name, p->type, p->audioset, p->modulator,
192 (unsigned long long)p->std, p->capabilities);
193}
194
195static void v4l_print_audio(const void *arg, bool write_only)
196{
197 const struct v4l2_audio *p = arg;
198
199 if (write_only)
200 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
201 else
202 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
203 p->index, p->name, p->capability, p->mode);
204}
205
206static void v4l_print_audioout(const void *arg, bool write_only)
207{
208 const struct v4l2_audioout *p = arg;
209
210 if (write_only)
211 pr_cont("index=%u\n", p->index);
212 else
213 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
214 p->index, p->name, p->capability, p->mode);
215}
216
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300217static void v4l_print_fmtdesc(const void *arg, bool write_only)
218{
219 const struct v4l2_fmtdesc *p = arg;
220
221 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
222 p->index, prt_names(p->type, v4l2_type_names),
223 p->flags, (p->pixelformat & 0xff),
224 (p->pixelformat >> 8) & 0xff,
225 (p->pixelformat >> 16) & 0xff,
226 (p->pixelformat >> 24) & 0xff,
227 p->description);
228}
229
230static void v4l_print_format(const void *arg, bool write_only)
231{
232 const struct v4l2_format *p = arg;
233 const struct v4l2_pix_format *pix;
234 const struct v4l2_pix_format_mplane *mp;
235 const struct v4l2_vbi_format *vbi;
236 const struct v4l2_sliced_vbi_format *sliced;
237 const struct v4l2_window *win;
238 const struct v4l2_clip *clip;
239 unsigned i;
240
241 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
242 switch (p->type) {
243 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
244 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
245 pix = &p->fmt.pix;
246 pr_cont(", width=%u, height=%u, "
247 "pixelformat=%c%c%c%c, field=%s, "
248 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
249 pix->width, pix->height,
250 (pix->pixelformat & 0xff),
251 (pix->pixelformat >> 8) & 0xff,
252 (pix->pixelformat >> 16) & 0xff,
253 (pix->pixelformat >> 24) & 0xff,
254 prt_names(pix->field, v4l2_field_names),
255 pix->bytesperline, pix->sizeimage,
256 pix->colorspace);
257 break;
258 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
259 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
260 mp = &p->fmt.pix_mp;
261 pr_cont(", width=%u, height=%u, "
262 "format=%c%c%c%c, field=%s, "
263 "colorspace=%d, num_planes=%u\n",
264 mp->width, mp->height,
265 (mp->pixelformat & 0xff),
266 (mp->pixelformat >> 8) & 0xff,
267 (mp->pixelformat >> 16) & 0xff,
268 (mp->pixelformat >> 24) & 0xff,
269 prt_names(mp->field, v4l2_field_names),
270 mp->colorspace, mp->num_planes);
271 for (i = 0; i < mp->num_planes; i++)
272 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
273 mp->plane_fmt[i].bytesperline,
274 mp->plane_fmt[i].sizeimage);
275 break;
276 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
277 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
278 win = &p->fmt.win;
279 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
280 "chromakey=0x%08x, bitmap=%p, "
281 "global_alpha=0x%02x\n",
282 win->w.width, win->w.height,
283 win->w.left, win->w.top,
284 prt_names(win->field, v4l2_field_names),
285 win->chromakey, win->bitmap, win->global_alpha);
286 clip = win->clips;
287 for (i = 0; i < win->clipcount; i++) {
288 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
289 i, clip->c.width, clip->c.height,
290 clip->c.left, clip->c.top);
291 clip = clip->next;
292 }
293 break;
294 case V4L2_BUF_TYPE_VBI_CAPTURE:
295 case V4L2_BUF_TYPE_VBI_OUTPUT:
296 vbi = &p->fmt.vbi;
297 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
298 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
299 vbi->sampling_rate, vbi->offset,
300 vbi->samples_per_line,
301 (vbi->sample_format & 0xff),
302 (vbi->sample_format >> 8) & 0xff,
303 (vbi->sample_format >> 16) & 0xff,
304 (vbi->sample_format >> 24) & 0xff,
305 vbi->start[0], vbi->start[1],
306 vbi->count[0], vbi->count[1]);
307 break;
308 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
309 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
310 sliced = &p->fmt.sliced;
311 pr_cont(", service_set=0x%08x, io_size=%d\n",
312 sliced->service_set, sliced->io_size);
313 for (i = 0; i < 24; i++)
314 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
315 sliced->service_lines[0][i],
316 sliced->service_lines[1][i]);
317 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300318 }
319}
320
321static void v4l_print_framebuffer(const void *arg, bool write_only)
322{
323 const struct v4l2_framebuffer *p = arg;
324
325 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
326 "height=%u, pixelformat=%c%c%c%c, "
327 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
328 p->capability, p->flags, p->base,
329 p->fmt.width, p->fmt.height,
330 (p->fmt.pixelformat & 0xff),
331 (p->fmt.pixelformat >> 8) & 0xff,
332 (p->fmt.pixelformat >> 16) & 0xff,
333 (p->fmt.pixelformat >> 24) & 0xff,
334 p->fmt.bytesperline, p->fmt.sizeimage,
335 p->fmt.colorspace);
336}
337
Hans Verkuile325b242012-06-09 12:50:15 -0300338static void v4l_print_buftype(const void *arg, bool write_only)
339{
340 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
341}
342
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300343static void v4l_print_modulator(const void *arg, bool write_only)
344{
345 const struct v4l2_modulator *p = arg;
346
347 if (write_only)
348 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
349 else
350 pr_cont("index=%u, name=%s, capability=0x%x, "
351 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
352 p->index, p->name, p->capability,
353 p->rangelow, p->rangehigh, p->txsubchans);
354}
355
356static void v4l_print_tuner(const void *arg, bool write_only)
357{
358 const struct v4l2_tuner *p = arg;
359
360 if (write_only)
361 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
362 else
363 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
364 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
365 "rxsubchans=0x%x, audmode=%u\n",
366 p->index, p->name, p->type,
367 p->capability, p->rangelow,
368 p->rangehigh, p->signal, p->afc,
369 p->rxsubchans, p->audmode);
370}
371
372static void v4l_print_frequency(const void *arg, bool write_only)
373{
374 const struct v4l2_frequency *p = arg;
375
376 pr_cont("tuner=%u, type=%u, frequency=%u\n",
377 p->tuner, p->type, p->frequency);
378}
379
380static void v4l_print_standard(const void *arg, bool write_only)
381{
382 const struct v4l2_standard *p = arg;
383
384 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
385 "framelines=%u\n", p->index,
386 (unsigned long long)p->id, p->name,
387 p->frameperiod.numerator,
388 p->frameperiod.denominator,
389 p->framelines);
390}
391
392static void v4l_print_std(const void *arg, bool write_only)
393{
394 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
395}
396
397static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
398{
399 const struct v4l2_hw_freq_seek *p = arg;
400
Hans Verkuil5e7883172012-08-01 16:18:41 -0300401 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
402 "rangelow=%u, rangehigh=%u\n",
403 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
404 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300405}
406
Hans Verkuileb3728e2012-06-22 06:23:59 -0300407static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300408{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300409 const struct v4l2_requestbuffers *p = arg;
410
411 pr_cont("count=%d, type=%s, memory=%s\n",
412 p->count,
413 prt_names(p->type, v4l2_type_names),
414 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300415}
416
Hans Verkuileb3728e2012-06-22 06:23:59 -0300417static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300418{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300419 const struct v4l2_buffer *p = arg;
420 const struct v4l2_timecode *tc = &p->timecode;
421 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300422 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300423
Hans Verkuileb3728e2012-06-22 06:23:59 -0300424 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
425 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300426 p->timestamp.tv_sec / 3600,
427 (int)(p->timestamp.tv_sec / 60) % 60,
428 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300429 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300430 p->index,
431 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300432 p->flags, prt_names(p->field, v4l2_field_names),
433 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300434
435 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300436 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300437 for (i = 0; i < p->length; ++i) {
438 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300439 printk(KERN_DEBUG
440 "plane %d: bytesused=%d, data_offset=0x%08x "
441 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300442 i, plane->bytesused, plane->data_offset,
443 plane->m.userptr, plane->length);
444 }
445 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300446 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300447 p->bytesused, p->m.userptr, p->length);
448 }
449
Hans Verkuileb3728e2012-06-22 06:23:59 -0300450 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
451 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300452 tc->hours, tc->minutes, tc->seconds,
453 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
454}
455
Hans Verkuileb3728e2012-06-22 06:23:59 -0300456static void v4l_print_create_buffers(const void *arg, bool write_only)
457{
458 const struct v4l2_create_buffers *p = arg;
459
460 pr_cont("index=%d, count=%d, memory=%s, ",
461 p->index, p->count,
462 prt_names(p->memory, v4l2_memory_names));
463 v4l_print_format(&p->format, write_only);
464}
465
466static void v4l_print_streamparm(const void *arg, bool write_only)
467{
468 const struct v4l2_streamparm *p = arg;
469
470 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
471
472 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
473 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
474 const struct v4l2_captureparm *c = &p->parm.capture;
475
476 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
477 "extendedmode=%d, readbuffers=%d\n",
478 c->capability, c->capturemode,
479 c->timeperframe.numerator, c->timeperframe.denominator,
480 c->extendedmode, c->readbuffers);
481 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
482 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
483 const struct v4l2_outputparm *c = &p->parm.output;
484
485 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
486 "extendedmode=%d, writebuffers=%d\n",
487 c->capability, c->outputmode,
488 c->timeperframe.numerator, c->timeperframe.denominator,
489 c->extendedmode, c->writebuffers);
490 }
491}
492
Hans Verkuilefbceec2012-06-09 12:54:02 -0300493static void v4l_print_queryctrl(const void *arg, bool write_only)
494{
495 const struct v4l2_queryctrl *p = arg;
496
497 pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
498 "step=%d, default=%d, flags=0x%08x\n",
499 p->id, p->type, p->name,
500 p->minimum, p->maximum,
501 p->step, p->default_value, p->flags);
502}
503
504static void v4l_print_querymenu(const void *arg, bool write_only)
505{
506 const struct v4l2_querymenu *p = arg;
507
508 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
509}
510
511static void v4l_print_control(const void *arg, bool write_only)
512{
513 const struct v4l2_control *p = arg;
514
515 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
516}
517
518static void v4l_print_ext_controls(const void *arg, bool write_only)
519{
520 const struct v4l2_ext_controls *p = arg;
521 int i;
522
523 pr_cont("class=0x%x, count=%d, error_idx=%d",
524 p->ctrl_class, p->count, p->error_idx);
525 for (i = 0; i < p->count; i++) {
526 if (p->controls[i].size)
527 pr_cont(", id/val=0x%x/0x%x",
528 p->controls[i].id, p->controls[i].value);
529 else
530 pr_cont(", id/size=0x%x/%u",
531 p->controls[i].id, p->controls[i].size);
532 }
533 pr_cont("\n");
534}
535
Hans Verkuild84f2d942012-06-09 11:57:46 -0300536static void v4l_print_cropcap(const void *arg, bool write_only)
537{
538 const struct v4l2_cropcap *p = arg;
539
540 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
541 "defrect wxh=%dx%d, x,y=%d,%d\n, "
542 "pixelaspect %d/%d\n",
543 prt_names(p->type, v4l2_type_names),
544 p->bounds.width, p->bounds.height,
545 p->bounds.left, p->bounds.top,
546 p->defrect.width, p->defrect.height,
547 p->defrect.left, p->defrect.top,
548 p->pixelaspect.numerator, p->pixelaspect.denominator);
549}
550
551static void v4l_print_crop(const void *arg, bool write_only)
552{
553 const struct v4l2_crop *p = arg;
554
555 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
556 prt_names(p->type, v4l2_type_names),
557 p->c.width, p->c.height,
558 p->c.left, p->c.top);
559}
560
561static void v4l_print_selection(const void *arg, bool write_only)
562{
563 const struct v4l2_selection *p = arg;
564
565 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
566 prt_names(p->type, v4l2_type_names),
567 p->target, p->flags,
568 p->r.width, p->r.height, p->r.left, p->r.top);
569}
570
Hans Verkuild806f2d2012-06-09 10:02:49 -0300571static void v4l_print_jpegcompression(const void *arg, bool write_only)
572{
573 const struct v4l2_jpegcompression *p = arg;
574
575 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
576 "COM_len=%d, jpeg_markers=0x%x\n",
577 p->quality, p->APPn, p->APP_len,
578 p->COM_len, p->jpeg_markers);
579}
580
581static void v4l_print_enc_idx(const void *arg, bool write_only)
582{
583 const struct v4l2_enc_idx *p = arg;
584
585 pr_cont("entries=%d, entries_cap=%d\n",
586 p->entries, p->entries_cap);
587}
588
589static void v4l_print_encoder_cmd(const void *arg, bool write_only)
590{
591 const struct v4l2_encoder_cmd *p = arg;
592
593 pr_cont("cmd=%d, flags=0x%x\n",
594 p->cmd, p->flags);
595}
596
597static void v4l_print_decoder_cmd(const void *arg, bool write_only)
598{
599 const struct v4l2_decoder_cmd *p = arg;
600
601 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
602
603 if (p->cmd == V4L2_DEC_CMD_START)
604 pr_info("speed=%d, format=%u\n",
605 p->start.speed, p->start.format);
606 else if (p->cmd == V4L2_DEC_CMD_STOP)
607 pr_info("pts=%llu\n", p->stop.pts);
608}
609
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300610static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
611{
612 const struct v4l2_dbg_chip_ident *p = arg;
613
614 pr_cont("type=%u, ", p->match.type);
615 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
616 pr_cont("name=%s, ", p->match.name);
617 else
618 pr_cont("addr=%u, ", p->match.addr);
619 pr_cont("chip_ident=%u, revision=0x%x\n",
620 p->ident, p->revision);
621}
622
623static void v4l_print_dbg_register(const void *arg, bool write_only)
624{
625 const struct v4l2_dbg_register *p = arg;
626
627 pr_cont("type=%u, ", p->match.type);
628 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
629 pr_cont("name=%s, ", p->match.name);
630 else
631 pr_cont("addr=%u, ", p->match.addr);
632 pr_cont("reg=0x%llx, val=0x%llx\n",
633 p->reg, p->val);
634}
635
Hans Verkuilefc626b2012-06-09 10:09:07 -0300636static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300637{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300638 const struct v4l2_dv_enum_preset *p = arg;
639
640 pr_cont("index=%u, preset=%u, name=%s, width=%u, height=%u\n",
641 p->index, p->preset, p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300642}
643
Hans Verkuilefc626b2012-06-09 10:09:07 -0300644static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300645{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300646 const struct v4l2_dv_preset *p = arg;
647
648 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300649}
650
Hans Verkuilefc626b2012-06-09 10:09:07 -0300651static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300652{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300653 const struct v4l2_dv_timings *p = arg;
654
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300655 switch (p->type) {
656 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300657 pr_cont("type=bt-656/1120, interlaced=%u, "
658 "pixelclock=%llu, "
659 "width=%u, height=%u, polarities=0x%x, "
660 "hfrontporch=%u, hsync=%u, "
661 "hbackporch=%u, vfrontporch=%u, "
662 "vsync=%u, vbackporch=%u, "
663 "il_vfrontporch=%u, il_vsync=%u, "
664 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300665 p->bt.interlaced, p->bt.pixelclock,
666 p->bt.width, p->bt.height,
667 p->bt.polarities, p->bt.hfrontporch,
668 p->bt.hsync, p->bt.hbackporch,
669 p->bt.vfrontporch, p->bt.vsync,
670 p->bt.vbackporch, p->bt.il_vfrontporch,
671 p->bt.il_vsync, p->bt.il_vbackporch,
672 p->bt.standards, p->bt.flags);
673 break;
674 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300675 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300676 break;
677 }
678}
679
Hans Verkuilefc626b2012-06-09 10:09:07 -0300680static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
681{
682 const struct v4l2_enum_dv_timings *p = arg;
683
684 pr_cont("index=%u, ", p->index);
685 v4l_print_dv_timings(&p->timings, write_only);
686}
687
688static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
689{
690 const struct v4l2_dv_timings_cap *p = arg;
691
692 switch (p->type) {
693 case V4L2_DV_BT_656_1120:
694 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
695 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
696 p->bt.min_width, p->bt.max_width,
697 p->bt.min_height, p->bt.max_height,
698 p->bt.min_pixelclock, p->bt.max_pixelclock,
699 p->bt.standards, p->bt.capabilities);
700 break;
701 default:
702 pr_cont("type=%u\n", p->type);
703 break;
704 }
705}
706
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300707static void v4l_print_frmsizeenum(const void *arg, bool write_only)
708{
709 const struct v4l2_frmsizeenum *p = arg;
710
711 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
712 p->index,
713 (p->pixel_format & 0xff),
714 (p->pixel_format >> 8) & 0xff,
715 (p->pixel_format >> 16) & 0xff,
716 (p->pixel_format >> 24) & 0xff,
717 p->type);
718 switch (p->type) {
719 case V4L2_FRMSIZE_TYPE_DISCRETE:
720 pr_cont(" wxh=%ux%u\n",
721 p->discrete.width, p->discrete.height);
722 break;
723 case V4L2_FRMSIZE_TYPE_STEPWISE:
724 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
725 p->stepwise.min_width, p->stepwise.min_height,
726 p->stepwise.step_width, p->stepwise.step_height,
727 p->stepwise.max_width, p->stepwise.max_height);
728 break;
729 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
730 /* fall through */
731 default:
732 pr_cont("\n");
733 break;
734 }
735}
736
737static void v4l_print_frmivalenum(const void *arg, bool write_only)
738{
739 const struct v4l2_frmivalenum *p = arg;
740
741 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
742 p->index,
743 (p->pixel_format & 0xff),
744 (p->pixel_format >> 8) & 0xff,
745 (p->pixel_format >> 16) & 0xff,
746 (p->pixel_format >> 24) & 0xff,
747 p->width, p->height, p->type);
748 switch (p->type) {
749 case V4L2_FRMIVAL_TYPE_DISCRETE:
750 pr_cont(" fps=%d/%d\n",
751 p->discrete.numerator,
752 p->discrete.denominator);
753 break;
754 case V4L2_FRMIVAL_TYPE_STEPWISE:
755 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
756 p->stepwise.min.numerator,
757 p->stepwise.min.denominator,
758 p->stepwise.max.numerator,
759 p->stepwise.max.denominator,
760 p->stepwise.step.numerator,
761 p->stepwise.step.denominator);
762 break;
763 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
764 /* fall through */
765 default:
766 pr_cont("\n");
767 break;
768 }
769}
770
771static void v4l_print_event(const void *arg, bool write_only)
772{
773 const struct v4l2_event *p = arg;
774 const struct v4l2_event_ctrl *c;
775
776 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
777 "timestamp=%lu.%9.9lu\n",
778 p->type, p->pending, p->sequence, p->id,
779 p->timestamp.tv_sec, p->timestamp.tv_nsec);
780 switch (p->type) {
781 case V4L2_EVENT_VSYNC:
782 printk(KERN_DEBUG "field=%s\n",
783 prt_names(p->u.vsync.field, v4l2_field_names));
784 break;
785 case V4L2_EVENT_CTRL:
786 c = &p->u.ctrl;
787 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
788 c->changes, c->type);
789 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
790 pr_cont("value64=%lld, ", c->value64);
791 else
792 pr_cont("value=%d, ", c->value);
793 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
794 " default_value=%d\n",
795 c->flags, c->minimum, c->maximum,
796 c->step, c->default_value);
797 break;
798 case V4L2_EVENT_FRAME_SYNC:
799 pr_cont("frame_sequence=%u\n",
800 p->u.frame_sync.frame_sequence);
801 break;
802 }
803}
804
805static void v4l_print_event_subscription(const void *arg, bool write_only)
806{
807 const struct v4l2_event_subscription *p = arg;
808
809 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
810 p->type, p->id, p->flags);
811}
812
813static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
814{
815 const struct v4l2_sliced_vbi_cap *p = arg;
816 int i;
817
818 pr_cont("type=%s, service_set=0x%08x\n",
819 prt_names(p->type, v4l2_type_names), p->service_set);
820 for (i = 0; i < 24; i++)
821 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
822 p->service_lines[0][i],
823 p->service_lines[1][i]);
824}
825
Hans Verkuil82b655b2012-07-05 06:37:08 -0300826static void v4l_print_freq_band(const void *arg, bool write_only)
827{
828 const struct v4l2_frequency_band *p = arg;
829
830 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
831 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
832 p->tuner, p->type, p->index,
833 p->capability, p->rangelow,
834 p->rangehigh, p->modulation);
835}
836
Hans Verkuilefc626b2012-06-09 10:09:07 -0300837static void v4l_print_u32(const void *arg, bool write_only)
838{
839 pr_cont("value=%u\n", *(const u32 *)arg);
840}
841
842static void v4l_print_newline(const void *arg, bool write_only)
843{
844 pr_cont("\n");
845}
846
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300847static void v4l_print_default(const void *arg, bool write_only)
848{
849 pr_cont("driver-specific ioctl\n");
850}
851
Hans Verkuilefbceec2012-06-09 12:54:02 -0300852static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300853{
854 __u32 i;
855
856 /* zero the reserved fields */
857 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300858 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300859 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300860
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300861 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
862 when using extended controls.
863 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
864 is it allowed for backwards compatibility.
865 */
866 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
867 return 0;
868 /* Check that all controls are from the same control class. */
869 for (i = 0; i < c->count; i++) {
870 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
871 c->error_idx = i;
872 return 0;
873 }
874 }
875 return 1;
876}
877
Hans Verkuil4b202592012-09-14 07:03:35 -0300878static int check_fmt(struct file *file, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300879{
Hans Verkuil4b202592012-09-14 07:03:35 -0300880 struct video_device *vfd = video_devdata(file);
881 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
882 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
883 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
884 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
885 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
886
Hans Verkuila3998102008-07-21 02:57:38 -0300887 if (ops == NULL)
888 return -EINVAL;
889
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300890 switch (type) {
891 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300892 if (is_vid && is_rx &&
893 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300894 return 0;
895 break;
896 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300897 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300898 return 0;
899 break;
900 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300901 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300902 return 0;
903 break;
904 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300905 if (is_vid && is_tx &&
906 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300907 return 0;
908 break;
909 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300910 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300911 return 0;
912 break;
913 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300914 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300915 return 0;
916 break;
917 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300918 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300919 return 0;
920 break;
921 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300922 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300923 return 0;
924 break;
925 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300926 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300927 return 0;
928 break;
929 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300930 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300931 return 0;
932 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300933 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300934 break;
935 }
936 return -EINVAL;
937}
938
Hans Verkuil59076122012-06-22 06:09:54 -0300939static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
940 struct file *file, void *fh, void *arg)
941{
942 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
943
944 cap->version = LINUX_VERSION_CODE;
945 return ops->vidioc_querycap(file, fh, cap);
946}
947
948static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
949 struct file *file, void *fh, void *arg)
950{
951 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
952}
953
954static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
955 struct file *file, void *fh, void *arg)
956{
957 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
958}
959
Hans Verkuild0547cb2012-06-09 09:27:10 -0300960static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
961 struct file *file, void *fh, void *arg)
962{
963 struct video_device *vfd;
964 u32 *p = arg;
965
966 if (ops->vidioc_g_priority)
967 return ops->vidioc_g_priority(file, fh, arg);
968 vfd = video_devdata(file);
969 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
970 return 0;
971}
972
973static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
974 struct file *file, void *fh, void *arg)
975{
976 struct video_device *vfd;
977 struct v4l2_fh *vfh;
978 u32 *p = arg;
979
980 if (ops->vidioc_s_priority)
981 return ops->vidioc_s_priority(file, fh, *p);
982 vfd = video_devdata(file);
983 vfh = file->private_data;
984 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
985}
986
Hans Verkuil59076122012-06-22 06:09:54 -0300987static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
988 struct file *file, void *fh, void *arg)
989{
990 struct v4l2_input *p = arg;
991
992 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -0300993 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -0300994 * CAP_STD here based on ioctl handler provided by the
995 * driver. If the driver doesn't support these
996 * for a specific input, it must override these flags.
997 */
998 if (ops->vidioc_s_std)
999 p->capabilities |= V4L2_IN_CAP_STD;
1000 if (ops->vidioc_s_dv_preset)
1001 p->capabilities |= V4L2_IN_CAP_PRESETS;
1002 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001003 p->capabilities |= V4L2_IN_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001004
1005 return ops->vidioc_enum_input(file, fh, p);
1006}
1007
1008static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1009 struct file *file, void *fh, void *arg)
1010{
1011 struct v4l2_output *p = arg;
1012
1013 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001014 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001015 * CAP_STD here based on ioctl handler provided by the
1016 * driver. If the driver doesn't support these
1017 * for a specific output, it must override these flags.
1018 */
1019 if (ops->vidioc_s_std)
1020 p->capabilities |= V4L2_OUT_CAP_STD;
1021 if (ops->vidioc_s_dv_preset)
1022 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1023 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001024 p->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001025
1026 return ops->vidioc_enum_output(file, fh, p);
1027}
1028
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001029static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1030 struct file *file, void *fh, void *arg)
1031{
1032 struct v4l2_fmtdesc *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001033 struct video_device *vfd = video_devdata(file);
1034 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1035 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001036
1037 switch (p->type) {
1038 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001039 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001040 break;
1041 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1042 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001043 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001044 break;
1045 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1046 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001047 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001048 break;
1049 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1050 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001051 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001052 break;
1053 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1054 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001055 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001056 break;
1057 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001058 }
1059 return -EINVAL;
1060}
1061
1062static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1063 struct file *file, void *fh, void *arg)
1064{
1065 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001066 struct video_device *vfd = video_devdata(file);
1067 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1068 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1069 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001070
1071 switch (p->type) {
1072 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001073 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001074 break;
1075 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1076 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001077 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001078 break;
1079 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1080 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001081 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001082 break;
1083 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001084 case V4L2_BUF_TYPE_VBI_CAPTURE:
1085 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1086 break;
1087 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1088 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1089 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1090 break;
1091 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001092 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001093 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001094 break;
1095 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1096 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001097 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001098 break;
1099 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1100 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001101 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001102 break;
1103 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001104 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001105 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001106 break;
1107 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001108 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001109 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001110 break;
1111 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001112 }
1113 return -EINVAL;
1114}
1115
1116static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1117 struct file *file, void *fh, void *arg)
1118{
1119 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001120 struct video_device *vfd = video_devdata(file);
1121 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1122 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1123 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001124
1125 switch (p->type) {
1126 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001127 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001128 break;
1129 CLEAR_AFTER_FIELD(p, fmt.pix);
1130 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1131 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001132 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001133 break;
1134 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1135 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1136 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001137 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001138 break;
1139 CLEAR_AFTER_FIELD(p, fmt.win);
1140 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001141 case V4L2_BUF_TYPE_VBI_CAPTURE:
1142 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1143 break;
1144 CLEAR_AFTER_FIELD(p, fmt.vbi);
1145 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1146 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1147 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1148 break;
1149 CLEAR_AFTER_FIELD(p, fmt.sliced);
1150 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001151 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001152 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001153 break;
1154 CLEAR_AFTER_FIELD(p, fmt.pix);
1155 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1156 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001157 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001158 break;
1159 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1160 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1161 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001162 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001163 break;
1164 CLEAR_AFTER_FIELD(p, fmt.win);
1165 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001166 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001167 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001168 break;
1169 CLEAR_AFTER_FIELD(p, fmt.vbi);
1170 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001171 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001172 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001173 break;
1174 CLEAR_AFTER_FIELD(p, fmt.sliced);
1175 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001176 }
1177 return -EINVAL;
1178}
1179
1180static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1181 struct file *file, void *fh, void *arg)
1182{
1183 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001184 struct video_device *vfd = video_devdata(file);
1185 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1186 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1187 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001188
1189 switch (p->type) {
1190 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001191 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001192 break;
1193 CLEAR_AFTER_FIELD(p, fmt.pix);
1194 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1195 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001196 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001197 break;
1198 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1199 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1200 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001201 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001202 break;
1203 CLEAR_AFTER_FIELD(p, fmt.win);
1204 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001205 case V4L2_BUF_TYPE_VBI_CAPTURE:
1206 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1207 break;
1208 CLEAR_AFTER_FIELD(p, fmt.vbi);
1209 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1210 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1211 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1212 break;
1213 CLEAR_AFTER_FIELD(p, fmt.sliced);
1214 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001215 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001216 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001217 break;
1218 CLEAR_AFTER_FIELD(p, fmt.pix);
1219 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1220 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001221 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001222 break;
1223 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1224 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1225 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001226 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001227 break;
1228 CLEAR_AFTER_FIELD(p, fmt.win);
1229 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001230 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001231 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001232 break;
1233 CLEAR_AFTER_FIELD(p, fmt.vbi);
1234 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001235 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001236 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001237 break;
1238 CLEAR_AFTER_FIELD(p, fmt.sliced);
1239 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001240 }
1241 return -EINVAL;
1242}
1243
Hans Verkuile325b242012-06-09 12:50:15 -03001244static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1245 struct file *file, void *fh, void *arg)
1246{
1247 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1248}
1249
1250static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1251 struct file *file, void *fh, void *arg)
1252{
1253 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1254}
1255
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001256static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1257 struct file *file, void *fh, void *arg)
1258{
1259 struct video_device *vfd = video_devdata(file);
1260 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001261 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001262
1263 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1264 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001265 err = ops->vidioc_g_tuner(file, fh, p);
1266 if (!err)
1267 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1268 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001269}
1270
1271static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1272 struct file *file, void *fh, void *arg)
1273{
1274 struct video_device *vfd = video_devdata(file);
1275 struct v4l2_tuner *p = arg;
1276
1277 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1278 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1279 return ops->vidioc_s_tuner(file, fh, p);
1280}
1281
Hans Verkuil82b655b2012-07-05 06:37:08 -03001282static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1283 struct file *file, void *fh, void *arg)
1284{
1285 struct v4l2_modulator *p = arg;
1286 int err;
1287
1288 err = ops->vidioc_g_modulator(file, fh, p);
1289 if (!err)
1290 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1291 return err;
1292}
1293
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001294static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1295 struct file *file, void *fh, void *arg)
1296{
1297 struct video_device *vfd = video_devdata(file);
1298 struct v4l2_frequency *p = arg;
1299
1300 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1301 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1302 return ops->vidioc_g_frequency(file, fh, p);
1303}
1304
1305static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1306 struct file *file, void *fh, void *arg)
1307{
1308 struct video_device *vfd = video_devdata(file);
1309 struct v4l2_frequency *p = arg;
1310 enum v4l2_tuner_type type;
1311
1312 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1313 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1314 if (p->type != type)
1315 return -EINVAL;
1316 return ops->vidioc_s_frequency(file, fh, p);
1317}
1318
1319static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1320 struct file *file, void *fh, void *arg)
1321{
1322 struct video_device *vfd = video_devdata(file);
1323 struct v4l2_standard *p = arg;
1324 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1325 unsigned int index = p->index, i, j = 0;
1326 const char *descr = "";
1327
Hans Verkuila5338192012-09-14 06:45:43 -03001328 /* Return -ENODATA if the tvnorms for the current input
1329 or output is 0, meaning that it doesn't support this API. */
1330 if (id == 0)
1331 return -ENODATA;
1332
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001333 /* Return norm array in a canonical way */
1334 for (i = 0; i <= index && id; i++) {
1335 /* last std value in the standards array is 0, so this
1336 while always ends there since (id & 0) == 0. */
1337 while ((id & standards[j].std) != standards[j].std)
1338 j++;
1339 curr_id = standards[j].std;
1340 descr = standards[j].descr;
1341 j++;
1342 if (curr_id == 0)
1343 break;
1344 if (curr_id != V4L2_STD_PAL &&
1345 curr_id != V4L2_STD_SECAM &&
1346 curr_id != V4L2_STD_NTSC)
1347 id &= ~curr_id;
1348 }
1349 if (i <= index)
1350 return -EINVAL;
1351
1352 v4l2_video_std_construct(p, curr_id, descr);
1353 return 0;
1354}
1355
1356static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1357 struct file *file, void *fh, void *arg)
1358{
1359 struct video_device *vfd = video_devdata(file);
1360 v4l2_std_id *id = arg;
1361
1362 /* Calls the specific handler */
1363 if (ops->vidioc_g_std)
1364 return ops->vidioc_g_std(file, fh, arg);
1365 if (vfd->current_norm) {
1366 *id = vfd->current_norm;
1367 return 0;
1368 }
1369 return -ENOTTY;
1370}
1371
1372static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1373 struct file *file, void *fh, void *arg)
1374{
1375 struct video_device *vfd = video_devdata(file);
1376 v4l2_std_id *id = arg, norm;
1377 int ret;
1378
1379 norm = (*id) & vfd->tvnorms;
1380 if (vfd->tvnorms && !norm) /* Check if std is supported */
1381 return -EINVAL;
1382
1383 /* Calls the specific handler */
1384 ret = ops->vidioc_s_std(file, fh, &norm);
1385
1386 /* Updates standard information */
1387 if (ret >= 0)
1388 vfd->current_norm = norm;
1389 return ret;
1390}
1391
1392static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1393 struct file *file, void *fh, void *arg)
1394{
1395 struct video_device *vfd = video_devdata(file);
1396 v4l2_std_id *p = arg;
1397
1398 /*
1399 * If nothing detected, it should return all supported
1400 * standard.
1401 * Drivers just need to mask the std argument, in order
1402 * to remove the standards that don't apply from the mask.
1403 * This means that tuners, audio and video decoders can join
1404 * their efforts to improve the standards detection.
1405 */
1406 *p = vfd->tvnorms;
1407 return ops->vidioc_querystd(file, fh, arg);
1408}
1409
1410static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1411 struct file *file, void *fh, void *arg)
1412{
1413 struct video_device *vfd = video_devdata(file);
1414 struct v4l2_hw_freq_seek *p = arg;
1415 enum v4l2_tuner_type type;
1416
1417 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1418 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1419 if (p->type != type)
1420 return -EINVAL;
1421 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1422}
1423
Hans Verkuil737c0972012-09-04 10:08:01 -03001424static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1425 struct file *file, void *fh, void *arg)
1426{
1427 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1428}
1429
Hans Verkuileb3728e2012-06-22 06:23:59 -03001430static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1431 struct file *file, void *fh, void *arg)
1432{
1433 struct v4l2_requestbuffers *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001434 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001435
1436 if (ret)
1437 return ret;
1438
Hans Verkuil633c98e2012-09-17 05:02:26 -03001439 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001440
1441 return ops->vidioc_reqbufs(file, fh, p);
1442}
1443
1444static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1445 struct file *file, void *fh, void *arg)
1446{
1447 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001448 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001449
1450 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1451}
1452
1453static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1454 struct file *file, void *fh, void *arg)
1455{
1456 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001457 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001458
1459 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1460}
1461
1462static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1463 struct file *file, void *fh, void *arg)
1464{
1465 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001466 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001467
1468 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1469}
1470
1471static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1472 struct file *file, void *fh, void *arg)
1473{
1474 struct v4l2_create_buffers *create = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001475 int ret = check_fmt(file, create->format.type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001476
1477 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1478}
1479
1480static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1481 struct file *file, void *fh, void *arg)
1482{
1483 struct v4l2_buffer *b = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001484 int ret = check_fmt(file, b->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001485
1486 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1487}
1488
1489static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1490 struct file *file, void *fh, void *arg)
1491{
1492 struct video_device *vfd = video_devdata(file);
1493 struct v4l2_streamparm *p = arg;
1494 v4l2_std_id std;
Hans Verkuil4b202592012-09-14 07:03:35 -03001495 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001496
1497 if (ret)
1498 return ret;
1499 if (ops->vidioc_g_parm)
1500 return ops->vidioc_g_parm(file, fh, p);
1501 std = vfd->current_norm;
1502 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1503 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1504 return -EINVAL;
1505 p->parm.capture.readbuffers = 2;
1506 if (ops->vidioc_g_std)
1507 ret = ops->vidioc_g_std(file, fh, &std);
1508 if (ret == 0)
1509 v4l2_video_std_frame_period(std,
1510 &p->parm.capture.timeperframe);
1511 return ret;
1512}
1513
1514static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1515 struct file *file, void *fh, void *arg)
1516{
1517 struct v4l2_streamparm *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001518 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001519
1520 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1521}
1522
Hans Verkuilefbceec2012-06-09 12:54:02 -03001523static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1524 struct file *file, void *fh, void *arg)
1525{
1526 struct video_device *vfd = video_devdata(file);
1527 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001528 struct v4l2_fh *vfh =
1529 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001530
1531 if (vfh && vfh->ctrl_handler)
1532 return v4l2_queryctrl(vfh->ctrl_handler, p);
1533 if (vfd->ctrl_handler)
1534 return v4l2_queryctrl(vfd->ctrl_handler, p);
1535 if (ops->vidioc_queryctrl)
1536 return ops->vidioc_queryctrl(file, fh, p);
1537 return -ENOTTY;
1538}
1539
1540static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1541 struct file *file, void *fh, void *arg)
1542{
1543 struct video_device *vfd = video_devdata(file);
1544 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001545 struct v4l2_fh *vfh =
1546 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001547
1548 if (vfh && vfh->ctrl_handler)
1549 return v4l2_querymenu(vfh->ctrl_handler, p);
1550 if (vfd->ctrl_handler)
1551 return v4l2_querymenu(vfd->ctrl_handler, p);
1552 if (ops->vidioc_querymenu)
1553 return ops->vidioc_querymenu(file, fh, p);
1554 return -ENOTTY;
1555}
1556
1557static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1558 struct file *file, void *fh, void *arg)
1559{
1560 struct video_device *vfd = video_devdata(file);
1561 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001562 struct v4l2_fh *vfh =
1563 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001564 struct v4l2_ext_controls ctrls;
1565 struct v4l2_ext_control ctrl;
1566
1567 if (vfh && vfh->ctrl_handler)
1568 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1569 if (vfd->ctrl_handler)
1570 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1571 if (ops->vidioc_g_ctrl)
1572 return ops->vidioc_g_ctrl(file, fh, p);
1573 if (ops->vidioc_g_ext_ctrls == NULL)
1574 return -ENOTTY;
1575
1576 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1577 ctrls.count = 1;
1578 ctrls.controls = &ctrl;
1579 ctrl.id = p->id;
1580 ctrl.value = p->value;
1581 if (check_ext_ctrls(&ctrls, 1)) {
1582 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1583
1584 if (ret == 0)
1585 p->value = ctrl.value;
1586 return ret;
1587 }
1588 return -EINVAL;
1589}
1590
1591static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1592 struct file *file, void *fh, void *arg)
1593{
1594 struct video_device *vfd = video_devdata(file);
1595 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001596 struct v4l2_fh *vfh =
1597 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001598 struct v4l2_ext_controls ctrls;
1599 struct v4l2_ext_control ctrl;
1600
1601 if (vfh && vfh->ctrl_handler)
1602 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1603 if (vfd->ctrl_handler)
1604 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1605 if (ops->vidioc_s_ctrl)
1606 return ops->vidioc_s_ctrl(file, fh, p);
1607 if (ops->vidioc_s_ext_ctrls == NULL)
1608 return -ENOTTY;
1609
1610 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1611 ctrls.count = 1;
1612 ctrls.controls = &ctrl;
1613 ctrl.id = p->id;
1614 ctrl.value = p->value;
1615 if (check_ext_ctrls(&ctrls, 1))
1616 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1617 return -EINVAL;
1618}
1619
1620static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1621 struct file *file, void *fh, void *arg)
1622{
1623 struct video_device *vfd = video_devdata(file);
1624 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001625 struct v4l2_fh *vfh =
1626 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001627
1628 p->error_idx = p->count;
1629 if (vfh && vfh->ctrl_handler)
1630 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1631 if (vfd->ctrl_handler)
1632 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1633 if (ops->vidioc_g_ext_ctrls == NULL)
1634 return -ENOTTY;
1635 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1636 -EINVAL;
1637}
1638
1639static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1640 struct file *file, void *fh, void *arg)
1641{
1642 struct video_device *vfd = video_devdata(file);
1643 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001644 struct v4l2_fh *vfh =
1645 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001646
1647 p->error_idx = p->count;
1648 if (vfh && vfh->ctrl_handler)
1649 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1650 if (vfd->ctrl_handler)
1651 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1652 if (ops->vidioc_s_ext_ctrls == NULL)
1653 return -ENOTTY;
1654 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1655 -EINVAL;
1656}
1657
1658static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1659 struct file *file, void *fh, void *arg)
1660{
1661 struct video_device *vfd = video_devdata(file);
1662 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001663 struct v4l2_fh *vfh =
1664 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001665
1666 p->error_idx = p->count;
1667 if (vfh && vfh->ctrl_handler)
1668 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1669 if (vfd->ctrl_handler)
1670 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1671 if (ops->vidioc_try_ext_ctrls == NULL)
1672 return -ENOTTY;
1673 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1674 -EINVAL;
1675}
1676
Hans Verkuild84f2d942012-06-09 11:57:46 -03001677static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1678 struct file *file, void *fh, void *arg)
1679{
1680 struct v4l2_crop *p = arg;
1681 struct v4l2_selection s = {
1682 .type = p->type,
1683 };
1684 int ret;
1685
1686 if (ops->vidioc_g_crop)
1687 return ops->vidioc_g_crop(file, fh, p);
1688 /* simulate capture crop using selection api */
1689
1690 /* crop means compose for output devices */
1691 if (V4L2_TYPE_IS_OUTPUT(p->type))
1692 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1693 else
1694 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1695
1696 ret = ops->vidioc_g_selection(file, fh, &s);
1697
1698 /* copying results to old structure on success */
1699 if (!ret)
1700 p->c = s.r;
1701 return ret;
1702}
1703
1704static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1705 struct file *file, void *fh, void *arg)
1706{
1707 struct v4l2_crop *p = arg;
1708 struct v4l2_selection s = {
1709 .type = p->type,
1710 .r = p->c,
1711 };
1712
1713 if (ops->vidioc_s_crop)
1714 return ops->vidioc_s_crop(file, fh, p);
1715 /* simulate capture crop using selection api */
1716
1717 /* crop means compose for output devices */
1718 if (V4L2_TYPE_IS_OUTPUT(p->type))
1719 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1720 else
1721 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1722
1723 return ops->vidioc_s_selection(file, fh, &s);
1724}
1725
1726static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1727 struct file *file, void *fh, void *arg)
1728{
1729 struct v4l2_cropcap *p = arg;
1730 struct v4l2_selection s = { .type = p->type };
1731 int ret;
1732
1733 if (ops->vidioc_cropcap)
1734 return ops->vidioc_cropcap(file, fh, p);
1735
1736 /* obtaining bounds */
1737 if (V4L2_TYPE_IS_OUTPUT(p->type))
1738 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1739 else
1740 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1741
1742 ret = ops->vidioc_g_selection(file, fh, &s);
1743 if (ret)
1744 return ret;
1745 p->bounds = s.r;
1746
1747 /* obtaining defrect */
1748 if (V4L2_TYPE_IS_OUTPUT(p->type))
1749 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1750 else
1751 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1752
1753 ret = ops->vidioc_g_selection(file, fh, &s);
1754 if (ret)
1755 return ret;
1756 p->defrect = s.r;
1757
1758 /* setting trivial pixelaspect */
1759 p->pixelaspect.numerator = 1;
1760 p->pixelaspect.denominator = 1;
1761 return 0;
1762}
1763
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001764static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1765 struct file *file, void *fh, void *arg)
1766{
1767 struct video_device *vfd = video_devdata(file);
1768 int ret;
1769
1770 if (vfd->v4l2_dev)
1771 pr_info("%s: ================= START STATUS =================\n",
1772 vfd->v4l2_dev->name);
1773 ret = ops->vidioc_log_status(file, fh);
1774 if (vfd->v4l2_dev)
1775 pr_info("%s: ================== END STATUS ==================\n",
1776 vfd->v4l2_dev->name);
1777 return ret;
1778}
1779
1780static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1781 struct file *file, void *fh, void *arg)
1782{
1783#ifdef CONFIG_VIDEO_ADV_DEBUG
1784 struct v4l2_dbg_register *p = arg;
1785
1786 if (!capable(CAP_SYS_ADMIN))
1787 return -EPERM;
1788 return ops->vidioc_g_register(file, fh, p);
1789#else
1790 return -ENOTTY;
1791#endif
1792}
1793
1794static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1795 struct file *file, void *fh, void *arg)
1796{
1797#ifdef CONFIG_VIDEO_ADV_DEBUG
1798 struct v4l2_dbg_register *p = arg;
1799
1800 if (!capable(CAP_SYS_ADMIN))
1801 return -EPERM;
1802 return ops->vidioc_s_register(file, fh, p);
1803#else
1804 return -ENOTTY;
1805#endif
1806}
1807
1808static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1809 struct file *file, void *fh, void *arg)
1810{
1811 struct v4l2_dbg_chip_ident *p = arg;
1812
1813 p->ident = V4L2_IDENT_NONE;
1814 p->revision = 0;
1815 return ops->vidioc_g_chip_ident(file, fh, p);
1816}
1817
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001818static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1819 struct file *file, void *fh, void *arg)
1820{
1821 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1822}
1823
1824static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1825 struct file *file, void *fh, void *arg)
1826{
1827 return ops->vidioc_subscribe_event(fh, arg);
1828}
1829
1830static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1831 struct file *file, void *fh, void *arg)
1832{
1833 return ops->vidioc_unsubscribe_event(fh, arg);
1834}
1835
1836static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1837 struct file *file, void *fh, void *arg)
1838{
1839 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001840 int ret = check_fmt(file, p->type);
1841
1842 if (ret)
1843 return ret;
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001844
1845 /* Clear up to type, everything after type is zeroed already */
1846 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1847
1848 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1849}
1850
Hans Verkuil82b655b2012-07-05 06:37:08 -03001851static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1852 struct file *file, void *fh, void *arg)
1853{
1854 struct video_device *vfd = video_devdata(file);
1855 struct v4l2_frequency_band *p = arg;
1856 enum v4l2_tuner_type type;
1857 int err;
1858
1859 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1860 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1861
1862 if (type != p->type)
1863 return -EINVAL;
1864 if (ops->vidioc_enum_freq_bands)
1865 return ops->vidioc_enum_freq_bands(file, fh, p);
1866 if (ops->vidioc_g_tuner) {
1867 struct v4l2_tuner t = {
1868 .index = p->tuner,
1869 .type = type,
1870 };
1871
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001872 if (p->index)
1873 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001874 err = ops->vidioc_g_tuner(file, fh, &t);
1875 if (err)
1876 return err;
1877 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1878 p->rangelow = t.rangelow;
1879 p->rangehigh = t.rangehigh;
1880 p->modulation = (type == V4L2_TUNER_RADIO) ?
1881 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1882 return 0;
1883 }
1884 if (ops->vidioc_g_modulator) {
1885 struct v4l2_modulator m = {
1886 .index = p->tuner,
1887 };
1888
1889 if (type != V4L2_TUNER_RADIO)
1890 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001891 if (p->index)
1892 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001893 err = ops->vidioc_g_modulator(file, fh, &m);
1894 if (err)
1895 return err;
1896 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1897 p->rangelow = m.rangelow;
1898 p->rangehigh = m.rangehigh;
1899 p->modulation = (type == V4L2_TUNER_RADIO) ?
1900 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1901 return 0;
1902 }
1903 return -ENOTTY;
1904}
1905
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001906struct v4l2_ioctl_info {
1907 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001908 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001909 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001910 union {
1911 u32 offset;
1912 int (*func)(const struct v4l2_ioctl_ops *ops,
1913 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001914 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03001915 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001916};
1917
1918/* This control needs a priority check */
1919#define INFO_FL_PRIO (1 << 0)
1920/* This control can be valid if the filehandle passes a control handler. */
1921#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001922/* This is a standard ioctl, no need for special code */
1923#define INFO_FL_STD (1 << 2)
1924/* This is ioctl has its own function */
1925#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001926/* Queuing ioctl */
1927#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001928/* Zero struct from after the field to the end */
1929#define INFO_FL_CLEAR(v4l2_struct, field) \
1930 ((offsetof(struct v4l2_struct, field) + \
1931 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1932#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001933
Hans Verkuil86748f32012-06-22 06:04:16 -03001934#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1935 [_IOC_NR(_ioctl)] = { \
1936 .ioctl = _ioctl, \
1937 .flags = _flags | INFO_FL_STD, \
1938 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001939 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03001940 .debug = _debug, \
1941 }
1942
1943#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1944 [_IOC_NR(_ioctl)] = { \
1945 .ioctl = _ioctl, \
1946 .flags = _flags | INFO_FL_FUNC, \
1947 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001948 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03001949 .debug = _debug, \
1950 }
1951
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001952static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001953 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001954 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1955 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1956 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001957 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1958 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001959 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1960 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuil737c0972012-09-04 10:08:01 -03001961 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001962 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
1963 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1964 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1965 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001966 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1967 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001968 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1969 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1970 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001971 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001972 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1973 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001974 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1975 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001976 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1977 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001978 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1979 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001980 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1981 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1982 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1983 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1984 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1985 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1986 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03001987 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001988 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1989 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1990 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03001991 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1992 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1993 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1994 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1995 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001996 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1997 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001998 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001999 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03002000 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2001 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03002002 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2003 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002004 IOCTL_INFO_FNC(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002005 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002006 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2007 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil9bc31632012-07-18 09:34:59 -03002008 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002009 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2010 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002011 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2012 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2013 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2014 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2015 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002016 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2017 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2018 IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_IDENT, v4l_dbg_g_chip_ident, v4l_print_dbg_chip_ident, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002019 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002020 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
2021 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
2022 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
2023 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
2024 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2025 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002026 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2027 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2028 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002029 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2030 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002031 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2032 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002033 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002034 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002035};
2036#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2037
2038bool v4l2_is_known_ioctl(unsigned int cmd)
2039{
2040 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2041 return false;
2042 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2043}
2044
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002045struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2046{
2047 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2048 return vdev->lock;
2049 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2050 return NULL;
2051 if (vdev->queue && vdev->queue->lock &&
2052 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2053 return vdev->queue->lock;
2054 return vdev->lock;
2055}
2056
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002057/* Common ioctl debug function. This function can be used by
2058 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002059void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002060{
Hans Verkuil86748f32012-06-22 06:04:16 -03002061 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002062
Hans Verkuil4a085162012-06-22 06:38:06 -03002063 if (prefix)
2064 printk(KERN_DEBUG "%s: ", prefix);
2065
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002066 switch (_IOC_TYPE(cmd)) {
2067 case 'd':
2068 type = "v4l2_int";
2069 break;
2070 case 'V':
2071 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2072 type = "v4l2";
2073 break;
2074 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002075 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002076 return;
2077 default:
2078 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002079 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002080 }
2081
2082 switch (_IOC_DIR(cmd)) {
2083 case _IOC_NONE: dir = "--"; break;
2084 case _IOC_READ: dir = "r-"; break;
2085 case _IOC_WRITE: dir = "-w"; break;
2086 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2087 default: dir = "*ERR*"; break;
2088 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002089 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002090 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2091}
2092EXPORT_SYMBOL(v4l_printk_ioctl);
2093
Hans Verkuil069b7472008-12-30 07:04:34 -03002094static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002095 unsigned int cmd, void *arg)
2096{
2097 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002098 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002099 bool write_only = false;
2100 struct v4l2_ioctl_info default_info;
2101 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002102 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002103 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002104 int use_fh_prio = 0;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002105 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002106 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002107
Hans Verkuil6a717882010-04-06 15:56:08 -03002108 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002109 pr_warn("%s: has no ioctl_ops.\n",
2110 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002111 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002112 }
2113
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002114 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2115 vfh = file->private_data;
2116 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002117 }
2118
2119 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002120 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002121
2122 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2123 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002124 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002125
2126 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2127 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2128 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002129 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002130 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002131 } else {
2132 default_info.ioctl = cmd;
2133 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002134 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002135 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002136 }
2137
Hans Verkuil86748f32012-06-22 06:04:16 -03002138 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002139 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002140 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002141 pr_cont(": ");
2142 info->debug(arg, write_only);
2143 }
2144 if (info->flags & INFO_FL_STD) {
2145 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2146 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002147 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002148
2149 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002150 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002151 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002152 } else if (!ops->vidioc_default) {
2153 ret = -ENOTTY;
2154 } else {
2155 ret = ops->vidioc_default(file, fh,
2156 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2157 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002158 }
2159
Hans Verkuil86748f32012-06-22 06:04:16 -03002160done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002161 if (debug) {
2162 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002163 if (ret < 0)
2164 printk(KERN_DEBUG "%s: error %ld\n",
2165 video_device_node_name(vfd), ret);
2166 return ret;
2167 }
Hans Verkuil4a085162012-06-22 06:38:06 -03002168 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002169 if (ret < 0)
2170 pr_cont(": error %ld\n", ret);
Hans Verkuil80131fe02012-06-22 06:37:38 -03002171 else if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002172 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002173 else if (_IOC_DIR(cmd) == _IOC_NONE)
2174 info->debug(arg, write_only);
2175 else {
2176 pr_cont(": ");
2177 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002178 }
2179 }
2180
2181 return ret;
2182}
2183
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002184static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2185 void * __user *user_ptr, void ***kernel_ptr)
2186{
2187 int ret = 0;
2188
2189 switch (cmd) {
Hans Verkuil96b1a702012-09-19 10:14:41 -03002190 case VIDIOC_PREPARE_BUF:
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002191 case VIDIOC_QUERYBUF:
2192 case VIDIOC_QBUF:
2193 case VIDIOC_DQBUF: {
2194 struct v4l2_buffer *buf = parg;
2195
2196 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2197 if (buf->length > VIDEO_MAX_PLANES) {
2198 ret = -EINVAL;
2199 break;
2200 }
2201 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002202 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002203 *array_size = sizeof(struct v4l2_plane) * buf->length;
2204 ret = 1;
2205 }
2206 break;
2207 }
2208
Hans Verkuiled45ce22012-08-10 06:07:12 -03002209 case VIDIOC_SUBDEV_G_EDID:
2210 case VIDIOC_SUBDEV_S_EDID: {
2211 struct v4l2_subdev_edid *edid = parg;
2212
2213 if (edid->blocks) {
Hans Verkuil1b8b10c2012-10-02 02:47:58 -03002214 if (edid->blocks > 256) {
2215 ret = -EINVAL;
2216 break;
2217 }
Hans Verkuiled45ce22012-08-10 06:07:12 -03002218 *user_ptr = (void __user *)edid->edid;
2219 *kernel_ptr = (void *)&edid->edid;
2220 *array_size = edid->blocks * 128;
2221 ret = 1;
2222 }
2223 break;
2224 }
2225
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002226 case VIDIOC_S_EXT_CTRLS:
2227 case VIDIOC_G_EXT_CTRLS:
2228 case VIDIOC_TRY_EXT_CTRLS: {
2229 struct v4l2_ext_controls *ctrls = parg;
2230
2231 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002232 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2233 ret = -EINVAL;
2234 break;
2235 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002236 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002237 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002238 *array_size = sizeof(struct v4l2_ext_control)
2239 * ctrls->count;
2240 ret = 1;
2241 }
2242 break;
2243 }
2244 }
2245
2246 return ret;
2247}
2248
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002249long
2250video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2251 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002252{
2253 char sbuf[128];
2254 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002255 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002256 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002257 bool has_array_args;
2258 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002259 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002260 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002261
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002262 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002263 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002264 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2265 parg = sbuf;
2266 } else {
2267 /* too big to allocate from stack */
2268 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2269 if (NULL == mbuf)
2270 return -ENOMEM;
2271 parg = mbuf;
2272 }
2273
2274 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002275 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002276 unsigned int n = _IOC_SIZE(cmd);
2277
2278 /*
2279 * In some cases, only a few fields are used as input,
2280 * i.e. when the app sets "index" and then the driver
2281 * fills in the rest of the structure for the thing
2282 * with that index. We only need to copy up the first
2283 * non-input field.
2284 */
2285 if (v4l2_is_known_ioctl(cmd)) {
2286 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2287 if (flags & INFO_FL_CLEAR_MASK)
2288 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2289 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002290
2291 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002292 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002293
2294 /* zero out anything we don't copy from userspace */
2295 if (n < _IOC_SIZE(cmd))
2296 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002297 } else {
2298 /* read-only ioctl */
2299 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002300 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002301 }
2302
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002303 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2304 if (err < 0)
2305 goto out;
2306 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002307
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002308 if (has_array_args) {
2309 /*
2310 * When adding new types of array args, make sure that the
2311 * parent argument to ioctl (which contains the pointer to the
2312 * array) fits into sbuf (so that mbuf will still remain
2313 * unused up to here).
2314 */
2315 mbuf = kmalloc(array_size, GFP_KERNEL);
2316 err = -ENOMEM;
2317 if (NULL == mbuf)
2318 goto out_array_args;
2319 err = -EFAULT;
2320 if (copy_from_user(mbuf, user_ptr, array_size))
2321 goto out_array_args;
2322 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002323 }
2324
2325 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002326 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002327 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002328 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002329
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002330 if (has_array_args) {
2331 *kernel_ptr = user_ptr;
2332 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002333 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002334 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002335 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002336 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2337 results that must be returned. */
2338 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002339 goto out;
2340
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002341out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002342 /* Copy results into user buffer */
2343 switch (_IOC_DIR(cmd)) {
2344 case _IOC_READ:
2345 case (_IOC_WRITE | _IOC_READ):
2346 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2347 err = -EFAULT;
2348 break;
2349 }
2350
2351out:
2352 kfree(mbuf);
2353 return err;
2354}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002355EXPORT_SYMBOL(video_usercopy);
2356
2357long video_ioctl2(struct file *file,
2358 unsigned int cmd, unsigned long arg)
2359{
2360 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2361}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002362EXPORT_SYMBOL(video_ioctl2);