blob: 9ded54b16e88dcb6edf64c25aa4e99b8e192c49c [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 Verkuil35ea11f2008-07-20 08:12:02 -030030
31#define dbgarg(cmd, fmt, arg...) \
32 do { \
33 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
34 printk(KERN_DEBUG "%s: ", vfd->name); \
35 v4l_printk_ioctl(cmd); \
36 printk(" " fmt, ## arg); \
37 } \
38 } while (0)
39
40#define dbgarg2(fmt, arg...) \
41 do { \
42 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
43 printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
44 } while (0)
45
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -030046#define dbgarg3(fmt, arg...) \
47 do { \
48 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
49 printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
50 } while (0)
51
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030053 * not including, the specified field is cleared. */
54#define CLEAR_AFTER_FIELD(p, field) \
55 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
56 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
57
Hans Verkuil35ea11f2008-07-20 08:12:02 -030058struct std_descr {
59 v4l2_std_id std;
60 const char *descr;
61};
62
63static const struct std_descr standards[] = {
64 { V4L2_STD_NTSC, "NTSC" },
65 { V4L2_STD_NTSC_M, "NTSC-M" },
66 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
67 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
68 { V4L2_STD_NTSC_443, "NTSC-443" },
69 { V4L2_STD_PAL, "PAL" },
70 { V4L2_STD_PAL_BG, "PAL-BG" },
71 { V4L2_STD_PAL_B, "PAL-B" },
72 { V4L2_STD_PAL_B1, "PAL-B1" },
73 { V4L2_STD_PAL_G, "PAL-G" },
74 { V4L2_STD_PAL_H, "PAL-H" },
75 { V4L2_STD_PAL_I, "PAL-I" },
76 { V4L2_STD_PAL_DK, "PAL-DK" },
77 { V4L2_STD_PAL_D, "PAL-D" },
78 { V4L2_STD_PAL_D1, "PAL-D1" },
79 { V4L2_STD_PAL_K, "PAL-K" },
80 { V4L2_STD_PAL_M, "PAL-M" },
81 { V4L2_STD_PAL_N, "PAL-N" },
82 { V4L2_STD_PAL_Nc, "PAL-Nc" },
83 { V4L2_STD_PAL_60, "PAL-60" },
84 { V4L2_STD_SECAM, "SECAM" },
85 { V4L2_STD_SECAM_B, "SECAM-B" },
86 { V4L2_STD_SECAM_G, "SECAM-G" },
87 { V4L2_STD_SECAM_H, "SECAM-H" },
88 { V4L2_STD_SECAM_DK, "SECAM-DK" },
89 { V4L2_STD_SECAM_D, "SECAM-D" },
90 { V4L2_STD_SECAM_K, "SECAM-K" },
91 { V4L2_STD_SECAM_K1, "SECAM-K1" },
92 { V4L2_STD_SECAM_L, "SECAM-L" },
93 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
94 { 0, "Unknown" }
95};
96
97/* video4linux standard ID conversion to standard name
98 */
99const char *v4l2_norm_to_name(v4l2_std_id id)
100{
101 u32 myid = id;
102 int i;
103
104 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
105 64 bit comparations. So, on that architecture, with some gcc
106 variants, compilation fails. Currently, the max value is 30bit wide.
107 */
108 BUG_ON(myid != id);
109
110 for (i = 0; standards[i].std; i++)
111 if (myid == standards[i].std)
112 break;
113 return standards[i].descr;
114}
115EXPORT_SYMBOL(v4l2_norm_to_name);
116
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300117/* Returns frame period for the given standard */
118void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
119{
120 if (id & V4L2_STD_525_60) {
121 frameperiod->numerator = 1001;
122 frameperiod->denominator = 30000;
123 } else {
124 frameperiod->numerator = 1;
125 frameperiod->denominator = 25;
126 }
127}
128EXPORT_SYMBOL(v4l2_video_std_frame_period);
129
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300130/* Fill in the fields of a v4l2_standard structure according to the
131 'id' and 'transmission' parameters. Returns negative on error. */
132int v4l2_video_std_construct(struct v4l2_standard *vs,
133 int id, const char *name)
134{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300135 vs->id = id;
136 v4l2_video_std_frame_period(id, &vs->frameperiod);
137 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300138 strlcpy(vs->name, name, sizeof(vs->name));
139 return 0;
140}
141EXPORT_SYMBOL(v4l2_video_std_construct);
142
143/* ----------------------------------------------------------------- */
144/* some arrays for pretty-printing debug messages of enum types */
145
146const char *v4l2_field_names[] = {
147 [V4L2_FIELD_ANY] = "any",
148 [V4L2_FIELD_NONE] = "none",
149 [V4L2_FIELD_TOP] = "top",
150 [V4L2_FIELD_BOTTOM] = "bottom",
151 [V4L2_FIELD_INTERLACED] = "interlaced",
152 [V4L2_FIELD_SEQ_TB] = "seq-tb",
153 [V4L2_FIELD_SEQ_BT] = "seq-bt",
154 [V4L2_FIELD_ALTERNATE] = "alternate",
155 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
156 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
157};
158EXPORT_SYMBOL(v4l2_field_names);
159
160const char *v4l2_type_names[] = {
161 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
162 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
163 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
164 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
165 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
166 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
167 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
168 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300169 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
170 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300171};
172EXPORT_SYMBOL(v4l2_type_names);
173
174static const char *v4l2_memory_names[] = {
175 [V4L2_MEMORY_MMAP] = "mmap",
176 [V4L2_MEMORY_USERPTR] = "userptr",
177 [V4L2_MEMORY_OVERLAY] = "overlay",
178};
179
180#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
181 arr[a] : "unknown")
182
183/* ------------------------------------------------------------------ */
184/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300185
Hans Verkuil59076122012-06-22 06:09:54 -0300186static void v4l_print_querycap(const void *arg, bool write_only)
187{
188 const struct v4l2_capability *p = arg;
189
190 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
191 "capabilities=0x%08x, device_caps=0x%08x\n",
192 p->driver, p->card, p->bus_info,
193 p->version, p->capabilities, p->device_caps);
194}
195
196static void v4l_print_enuminput(const void *arg, bool write_only)
197{
198 const struct v4l2_input *p = arg;
199
200 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
201 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
202 p->index, p->name, p->type, p->audioset, p->tuner,
203 (unsigned long long)p->std, p->status, p->capabilities);
204}
205
206static void v4l_print_enumoutput(const void *arg, bool write_only)
207{
208 const struct v4l2_output *p = arg;
209
210 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
211 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
212 p->index, p->name, p->type, p->audioset, p->modulator,
213 (unsigned long long)p->std, p->capabilities);
214}
215
216static void v4l_print_audio(const void *arg, bool write_only)
217{
218 const struct v4l2_audio *p = arg;
219
220 if (write_only)
221 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
222 else
223 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
224 p->index, p->name, p->capability, p->mode);
225}
226
227static void v4l_print_audioout(const void *arg, bool write_only)
228{
229 const struct v4l2_audioout *p = arg;
230
231 if (write_only)
232 pr_cont("index=%u\n", p->index);
233 else
234 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
235 p->index, p->name, p->capability, p->mode);
236}
237
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300238static void v4l_print_fmtdesc(const void *arg, bool write_only)
239{
240 const struct v4l2_fmtdesc *p = arg;
241
242 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
243 p->index, prt_names(p->type, v4l2_type_names),
244 p->flags, (p->pixelformat & 0xff),
245 (p->pixelformat >> 8) & 0xff,
246 (p->pixelformat >> 16) & 0xff,
247 (p->pixelformat >> 24) & 0xff,
248 p->description);
249}
250
251static void v4l_print_format(const void *arg, bool write_only)
252{
253 const struct v4l2_format *p = arg;
254 const struct v4l2_pix_format *pix;
255 const struct v4l2_pix_format_mplane *mp;
256 const struct v4l2_vbi_format *vbi;
257 const struct v4l2_sliced_vbi_format *sliced;
258 const struct v4l2_window *win;
259 const struct v4l2_clip *clip;
260 unsigned i;
261
262 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
263 switch (p->type) {
264 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
265 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
266 pix = &p->fmt.pix;
267 pr_cont(", width=%u, height=%u, "
268 "pixelformat=%c%c%c%c, field=%s, "
269 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
270 pix->width, pix->height,
271 (pix->pixelformat & 0xff),
272 (pix->pixelformat >> 8) & 0xff,
273 (pix->pixelformat >> 16) & 0xff,
274 (pix->pixelformat >> 24) & 0xff,
275 prt_names(pix->field, v4l2_field_names),
276 pix->bytesperline, pix->sizeimage,
277 pix->colorspace);
278 break;
279 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
280 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
281 mp = &p->fmt.pix_mp;
282 pr_cont(", width=%u, height=%u, "
283 "format=%c%c%c%c, field=%s, "
284 "colorspace=%d, num_planes=%u\n",
285 mp->width, mp->height,
286 (mp->pixelformat & 0xff),
287 (mp->pixelformat >> 8) & 0xff,
288 (mp->pixelformat >> 16) & 0xff,
289 (mp->pixelformat >> 24) & 0xff,
290 prt_names(mp->field, v4l2_field_names),
291 mp->colorspace, mp->num_planes);
292 for (i = 0; i < mp->num_planes; i++)
293 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
294 mp->plane_fmt[i].bytesperline,
295 mp->plane_fmt[i].sizeimage);
296 break;
297 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
298 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
299 win = &p->fmt.win;
300 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
301 "chromakey=0x%08x, bitmap=%p, "
302 "global_alpha=0x%02x\n",
303 win->w.width, win->w.height,
304 win->w.left, win->w.top,
305 prt_names(win->field, v4l2_field_names),
306 win->chromakey, win->bitmap, win->global_alpha);
307 clip = win->clips;
308 for (i = 0; i < win->clipcount; i++) {
309 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
310 i, clip->c.width, clip->c.height,
311 clip->c.left, clip->c.top);
312 clip = clip->next;
313 }
314 break;
315 case V4L2_BUF_TYPE_VBI_CAPTURE:
316 case V4L2_BUF_TYPE_VBI_OUTPUT:
317 vbi = &p->fmt.vbi;
318 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
319 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
320 vbi->sampling_rate, vbi->offset,
321 vbi->samples_per_line,
322 (vbi->sample_format & 0xff),
323 (vbi->sample_format >> 8) & 0xff,
324 (vbi->sample_format >> 16) & 0xff,
325 (vbi->sample_format >> 24) & 0xff,
326 vbi->start[0], vbi->start[1],
327 vbi->count[0], vbi->count[1]);
328 break;
329 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
330 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
331 sliced = &p->fmt.sliced;
332 pr_cont(", service_set=0x%08x, io_size=%d\n",
333 sliced->service_set, sliced->io_size);
334 for (i = 0; i < 24; i++)
335 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
336 sliced->service_lines[0][i],
337 sliced->service_lines[1][i]);
338 break;
339 case V4L2_BUF_TYPE_PRIVATE:
340 pr_cont("\n");
341 break;
342 }
343}
344
345static void v4l_print_framebuffer(const void *arg, bool write_only)
346{
347 const struct v4l2_framebuffer *p = arg;
348
349 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
350 "height=%u, pixelformat=%c%c%c%c, "
351 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
352 p->capability, p->flags, p->base,
353 p->fmt.width, p->fmt.height,
354 (p->fmt.pixelformat & 0xff),
355 (p->fmt.pixelformat >> 8) & 0xff,
356 (p->fmt.pixelformat >> 16) & 0xff,
357 (p->fmt.pixelformat >> 24) & 0xff,
358 p->fmt.bytesperline, p->fmt.sizeimage,
359 p->fmt.colorspace);
360}
361
Hans Verkuile325b242012-06-09 12:50:15 -0300362static void v4l_print_buftype(const void *arg, bool write_only)
363{
364 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
365}
366
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300367static void v4l_print_modulator(const void *arg, bool write_only)
368{
369 const struct v4l2_modulator *p = arg;
370
371 if (write_only)
372 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
373 else
374 pr_cont("index=%u, name=%s, capability=0x%x, "
375 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
376 p->index, p->name, p->capability,
377 p->rangelow, p->rangehigh, p->txsubchans);
378}
379
380static void v4l_print_tuner(const void *arg, bool write_only)
381{
382 const struct v4l2_tuner *p = arg;
383
384 if (write_only)
385 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
386 else
387 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
388 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
389 "rxsubchans=0x%x, audmode=%u\n",
390 p->index, p->name, p->type,
391 p->capability, p->rangelow,
392 p->rangehigh, p->signal, p->afc,
393 p->rxsubchans, p->audmode);
394}
395
396static void v4l_print_frequency(const void *arg, bool write_only)
397{
398 const struct v4l2_frequency *p = arg;
399
400 pr_cont("tuner=%u, type=%u, frequency=%u\n",
401 p->tuner, p->type, p->frequency);
402}
403
404static void v4l_print_standard(const void *arg, bool write_only)
405{
406 const struct v4l2_standard *p = arg;
407
408 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
409 "framelines=%u\n", p->index,
410 (unsigned long long)p->id, p->name,
411 p->frameperiod.numerator,
412 p->frameperiod.denominator,
413 p->framelines);
414}
415
416static void v4l_print_std(const void *arg, bool write_only)
417{
418 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
419}
420
421static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
422{
423 const struct v4l2_hw_freq_seek *p = arg;
424
425 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
426 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
427}
428
Hans Verkuileb3728e2012-06-22 06:23:59 -0300429static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300430{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300431 const struct v4l2_requestbuffers *p = arg;
432
433 pr_cont("count=%d, type=%s, memory=%s\n",
434 p->count,
435 prt_names(p->type, v4l2_type_names),
436 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300437}
438
Hans Verkuileb3728e2012-06-22 06:23:59 -0300439static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300440{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300441 const struct v4l2_buffer *p = arg;
442 const struct v4l2_timecode *tc = &p->timecode;
443 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300444 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300445
Hans Verkuileb3728e2012-06-22 06:23:59 -0300446 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
447 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300448 p->timestamp.tv_sec / 3600,
449 (int)(p->timestamp.tv_sec / 60) % 60,
450 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300451 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300452 p->index,
453 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300454 p->flags, prt_names(p->field, v4l2_field_names),
455 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300456
457 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300458 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300459 for (i = 0; i < p->length; ++i) {
460 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300461 printk(KERN_DEBUG
462 "plane %d: bytesused=%d, data_offset=0x%08x "
463 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300464 i, plane->bytesused, plane->data_offset,
465 plane->m.userptr, plane->length);
466 }
467 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300468 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300469 p->bytesused, p->m.userptr, p->length);
470 }
471
Hans Verkuileb3728e2012-06-22 06:23:59 -0300472 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
473 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300474 tc->hours, tc->minutes, tc->seconds,
475 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
476}
477
Hans Verkuileb3728e2012-06-22 06:23:59 -0300478static void v4l_print_create_buffers(const void *arg, bool write_only)
479{
480 const struct v4l2_create_buffers *p = arg;
481
482 pr_cont("index=%d, count=%d, memory=%s, ",
483 p->index, p->count,
484 prt_names(p->memory, v4l2_memory_names));
485 v4l_print_format(&p->format, write_only);
486}
487
488static void v4l_print_streamparm(const void *arg, bool write_only)
489{
490 const struct v4l2_streamparm *p = arg;
491
492 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
493
494 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
495 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
496 const struct v4l2_captureparm *c = &p->parm.capture;
497
498 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
499 "extendedmode=%d, readbuffers=%d\n",
500 c->capability, c->capturemode,
501 c->timeperframe.numerator, c->timeperframe.denominator,
502 c->extendedmode, c->readbuffers);
503 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
504 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
505 const struct v4l2_outputparm *c = &p->parm.output;
506
507 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
508 "extendedmode=%d, writebuffers=%d\n",
509 c->capability, c->outputmode,
510 c->timeperframe.numerator, c->timeperframe.denominator,
511 c->extendedmode, c->writebuffers);
512 }
513}
514
Hans Verkuilefbceec2012-06-09 12:54:02 -0300515static void v4l_print_queryctrl(const void *arg, bool write_only)
516{
517 const struct v4l2_queryctrl *p = arg;
518
519 pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
520 "step=%d, default=%d, flags=0x%08x\n",
521 p->id, p->type, p->name,
522 p->minimum, p->maximum,
523 p->step, p->default_value, p->flags);
524}
525
526static void v4l_print_querymenu(const void *arg, bool write_only)
527{
528 const struct v4l2_querymenu *p = arg;
529
530 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
531}
532
533static void v4l_print_control(const void *arg, bool write_only)
534{
535 const struct v4l2_control *p = arg;
536
537 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
538}
539
540static void v4l_print_ext_controls(const void *arg, bool write_only)
541{
542 const struct v4l2_ext_controls *p = arg;
543 int i;
544
545 pr_cont("class=0x%x, count=%d, error_idx=%d",
546 p->ctrl_class, p->count, p->error_idx);
547 for (i = 0; i < p->count; i++) {
548 if (p->controls[i].size)
549 pr_cont(", id/val=0x%x/0x%x",
550 p->controls[i].id, p->controls[i].value);
551 else
552 pr_cont(", id/size=0x%x/%u",
553 p->controls[i].id, p->controls[i].size);
554 }
555 pr_cont("\n");
556}
557
Hans Verkuild84f2d942012-06-09 11:57:46 -0300558static void v4l_print_cropcap(const void *arg, bool write_only)
559{
560 const struct v4l2_cropcap *p = arg;
561
562 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
563 "defrect wxh=%dx%d, x,y=%d,%d\n, "
564 "pixelaspect %d/%d\n",
565 prt_names(p->type, v4l2_type_names),
566 p->bounds.width, p->bounds.height,
567 p->bounds.left, p->bounds.top,
568 p->defrect.width, p->defrect.height,
569 p->defrect.left, p->defrect.top,
570 p->pixelaspect.numerator, p->pixelaspect.denominator);
571}
572
573static void v4l_print_crop(const void *arg, bool write_only)
574{
575 const struct v4l2_crop *p = arg;
576
577 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
578 prt_names(p->type, v4l2_type_names),
579 p->c.width, p->c.height,
580 p->c.left, p->c.top);
581}
582
583static void v4l_print_selection(const void *arg, bool write_only)
584{
585 const struct v4l2_selection *p = arg;
586
587 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
588 prt_names(p->type, v4l2_type_names),
589 p->target, p->flags,
590 p->r.width, p->r.height, p->r.left, p->r.top);
591}
592
Hans Verkuild806f2d2012-06-09 10:02:49 -0300593static void v4l_print_jpegcompression(const void *arg, bool write_only)
594{
595 const struct v4l2_jpegcompression *p = arg;
596
597 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
598 "COM_len=%d, jpeg_markers=0x%x\n",
599 p->quality, p->APPn, p->APP_len,
600 p->COM_len, p->jpeg_markers);
601}
602
603static void v4l_print_enc_idx(const void *arg, bool write_only)
604{
605 const struct v4l2_enc_idx *p = arg;
606
607 pr_cont("entries=%d, entries_cap=%d\n",
608 p->entries, p->entries_cap);
609}
610
611static void v4l_print_encoder_cmd(const void *arg, bool write_only)
612{
613 const struct v4l2_encoder_cmd *p = arg;
614
615 pr_cont("cmd=%d, flags=0x%x\n",
616 p->cmd, p->flags);
617}
618
619static void v4l_print_decoder_cmd(const void *arg, bool write_only)
620{
621 const struct v4l2_decoder_cmd *p = arg;
622
623 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
624
625 if (p->cmd == V4L2_DEC_CMD_START)
626 pr_info("speed=%d, format=%u\n",
627 p->start.speed, p->start.format);
628 else if (p->cmd == V4L2_DEC_CMD_STOP)
629 pr_info("pts=%llu\n", p->stop.pts);
630}
631
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300632static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
633{
634 const struct v4l2_dbg_chip_ident *p = arg;
635
636 pr_cont("type=%u, ", p->match.type);
637 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
638 pr_cont("name=%s, ", p->match.name);
639 else
640 pr_cont("addr=%u, ", p->match.addr);
641 pr_cont("chip_ident=%u, revision=0x%x\n",
642 p->ident, p->revision);
643}
644
645static void v4l_print_dbg_register(const void *arg, bool write_only)
646{
647 const struct v4l2_dbg_register *p = arg;
648
649 pr_cont("type=%u, ", p->match.type);
650 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
651 pr_cont("name=%s, ", p->match.name);
652 else
653 pr_cont("addr=%u, ", p->match.addr);
654 pr_cont("reg=0x%llx, val=0x%llx\n",
655 p->reg, p->val);
656}
657
Hans Verkuilefc626b2012-06-09 10:09:07 -0300658static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300659{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300660 const struct v4l2_dv_enum_preset *p = arg;
661
662 pr_cont("index=%u, preset=%u, name=%s, width=%u, height=%u\n",
663 p->index, p->preset, p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300664}
665
Hans Verkuilefc626b2012-06-09 10:09:07 -0300666static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300667{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300668 const struct v4l2_dv_preset *p = arg;
669
670 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300671}
672
Hans Verkuilefc626b2012-06-09 10:09:07 -0300673static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300674{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300675 const struct v4l2_dv_timings *p = arg;
676
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300677 switch (p->type) {
678 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300679 pr_cont("type=bt-656/1120, interlaced=%u, "
680 "pixelclock=%llu, "
681 "width=%u, height=%u, polarities=0x%x, "
682 "hfrontporch=%u, hsync=%u, "
683 "hbackporch=%u, vfrontporch=%u, "
684 "vsync=%u, vbackporch=%u, "
685 "il_vfrontporch=%u, il_vsync=%u, "
686 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300687 p->bt.interlaced, p->bt.pixelclock,
688 p->bt.width, p->bt.height,
689 p->bt.polarities, p->bt.hfrontporch,
690 p->bt.hsync, p->bt.hbackporch,
691 p->bt.vfrontporch, p->bt.vsync,
692 p->bt.vbackporch, p->bt.il_vfrontporch,
693 p->bt.il_vsync, p->bt.il_vbackporch,
694 p->bt.standards, p->bt.flags);
695 break;
696 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300697 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300698 break;
699 }
700}
701
Hans Verkuilefc626b2012-06-09 10:09:07 -0300702static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
703{
704 const struct v4l2_enum_dv_timings *p = arg;
705
706 pr_cont("index=%u, ", p->index);
707 v4l_print_dv_timings(&p->timings, write_only);
708}
709
710static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
711{
712 const struct v4l2_dv_timings_cap *p = arg;
713
714 switch (p->type) {
715 case V4L2_DV_BT_656_1120:
716 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
717 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
718 p->bt.min_width, p->bt.max_width,
719 p->bt.min_height, p->bt.max_height,
720 p->bt.min_pixelclock, p->bt.max_pixelclock,
721 p->bt.standards, p->bt.capabilities);
722 break;
723 default:
724 pr_cont("type=%u\n", p->type);
725 break;
726 }
727}
728
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300729static void v4l_print_frmsizeenum(const void *arg, bool write_only)
730{
731 const struct v4l2_frmsizeenum *p = arg;
732
733 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
734 p->index,
735 (p->pixel_format & 0xff),
736 (p->pixel_format >> 8) & 0xff,
737 (p->pixel_format >> 16) & 0xff,
738 (p->pixel_format >> 24) & 0xff,
739 p->type);
740 switch (p->type) {
741 case V4L2_FRMSIZE_TYPE_DISCRETE:
742 pr_cont(" wxh=%ux%u\n",
743 p->discrete.width, p->discrete.height);
744 break;
745 case V4L2_FRMSIZE_TYPE_STEPWISE:
746 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
747 p->stepwise.min_width, p->stepwise.min_height,
748 p->stepwise.step_width, p->stepwise.step_height,
749 p->stepwise.max_width, p->stepwise.max_height);
750 break;
751 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
752 /* fall through */
753 default:
754 pr_cont("\n");
755 break;
756 }
757}
758
759static void v4l_print_frmivalenum(const void *arg, bool write_only)
760{
761 const struct v4l2_frmivalenum *p = arg;
762
763 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
764 p->index,
765 (p->pixel_format & 0xff),
766 (p->pixel_format >> 8) & 0xff,
767 (p->pixel_format >> 16) & 0xff,
768 (p->pixel_format >> 24) & 0xff,
769 p->width, p->height, p->type);
770 switch (p->type) {
771 case V4L2_FRMIVAL_TYPE_DISCRETE:
772 pr_cont(" fps=%d/%d\n",
773 p->discrete.numerator,
774 p->discrete.denominator);
775 break;
776 case V4L2_FRMIVAL_TYPE_STEPWISE:
777 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
778 p->stepwise.min.numerator,
779 p->stepwise.min.denominator,
780 p->stepwise.max.numerator,
781 p->stepwise.max.denominator,
782 p->stepwise.step.numerator,
783 p->stepwise.step.denominator);
784 break;
785 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
786 /* fall through */
787 default:
788 pr_cont("\n");
789 break;
790 }
791}
792
793static void v4l_print_event(const void *arg, bool write_only)
794{
795 const struct v4l2_event *p = arg;
796 const struct v4l2_event_ctrl *c;
797
798 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
799 "timestamp=%lu.%9.9lu\n",
800 p->type, p->pending, p->sequence, p->id,
801 p->timestamp.tv_sec, p->timestamp.tv_nsec);
802 switch (p->type) {
803 case V4L2_EVENT_VSYNC:
804 printk(KERN_DEBUG "field=%s\n",
805 prt_names(p->u.vsync.field, v4l2_field_names));
806 break;
807 case V4L2_EVENT_CTRL:
808 c = &p->u.ctrl;
809 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
810 c->changes, c->type);
811 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
812 pr_cont("value64=%lld, ", c->value64);
813 else
814 pr_cont("value=%d, ", c->value);
815 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
816 " default_value=%d\n",
817 c->flags, c->minimum, c->maximum,
818 c->step, c->default_value);
819 break;
820 case V4L2_EVENT_FRAME_SYNC:
821 pr_cont("frame_sequence=%u\n",
822 p->u.frame_sync.frame_sequence);
823 break;
824 }
825}
826
827static void v4l_print_event_subscription(const void *arg, bool write_only)
828{
829 const struct v4l2_event_subscription *p = arg;
830
831 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
832 p->type, p->id, p->flags);
833}
834
835static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
836{
837 const struct v4l2_sliced_vbi_cap *p = arg;
838 int i;
839
840 pr_cont("type=%s, service_set=0x%08x\n",
841 prt_names(p->type, v4l2_type_names), p->service_set);
842 for (i = 0; i < 24; i++)
843 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
844 p->service_lines[0][i],
845 p->service_lines[1][i]);
846}
847
Hans Verkuilefc626b2012-06-09 10:09:07 -0300848static void v4l_print_u32(const void *arg, bool write_only)
849{
850 pr_cont("value=%u\n", *(const u32 *)arg);
851}
852
853static void v4l_print_newline(const void *arg, bool write_only)
854{
855 pr_cont("\n");
856}
857
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300858static void v4l_print_default(const void *arg, bool write_only)
859{
860 pr_cont("driver-specific ioctl\n");
861}
862
Hans Verkuilefbceec2012-06-09 12:54:02 -0300863static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300864{
865 __u32 i;
866
867 /* zero the reserved fields */
868 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300869 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300870 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300871
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300872 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
873 when using extended controls.
874 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
875 is it allowed for backwards compatibility.
876 */
877 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
878 return 0;
879 /* Check that all controls are from the same control class. */
880 for (i = 0; i < c->count; i++) {
881 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
882 c->error_idx = i;
883 return 0;
884 }
885 }
886 return 1;
887}
888
Hans Verkuila3998102008-07-21 02:57:38 -0300889static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300890{
Hans Verkuila3998102008-07-21 02:57:38 -0300891 if (ops == NULL)
892 return -EINVAL;
893
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300894 switch (type) {
895 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300896 if (ops->vidioc_g_fmt_vid_cap ||
897 ops->vidioc_g_fmt_vid_cap_mplane)
898 return 0;
899 break;
900 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
901 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300902 return 0;
903 break;
904 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300905 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300906 return 0;
907 break;
908 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300909 if (ops->vidioc_g_fmt_vid_out ||
910 ops->vidioc_g_fmt_vid_out_mplane)
911 return 0;
912 break;
913 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
914 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300915 return 0;
916 break;
917 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300918 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300919 return 0;
920 break;
921 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300922 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300923 return 0;
924 break;
925 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300926 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300927 return 0;
928 break;
929 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300930 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300931 return 0;
932 break;
933 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300934 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300935 return 0;
936 break;
937 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300938 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300939 return 0;
940 break;
941 }
942 return -EINVAL;
943}
944
Hans Verkuil59076122012-06-22 06:09:54 -0300945static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
946 struct file *file, void *fh, void *arg)
947{
948 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
949
950 cap->version = LINUX_VERSION_CODE;
951 return ops->vidioc_querycap(file, fh, cap);
952}
953
954static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
955 struct file *file, void *fh, void *arg)
956{
957 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
958}
959
960static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
961 struct file *file, void *fh, void *arg)
962{
963 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
964}
965
Hans Verkuild0547cb2012-06-09 09:27:10 -0300966static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
967 struct file *file, void *fh, void *arg)
968{
969 struct video_device *vfd;
970 u32 *p = arg;
971
972 if (ops->vidioc_g_priority)
973 return ops->vidioc_g_priority(file, fh, arg);
974 vfd = video_devdata(file);
975 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
976 return 0;
977}
978
979static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
980 struct file *file, void *fh, void *arg)
981{
982 struct video_device *vfd;
983 struct v4l2_fh *vfh;
984 u32 *p = arg;
985
986 if (ops->vidioc_s_priority)
987 return ops->vidioc_s_priority(file, fh, *p);
988 vfd = video_devdata(file);
989 vfh = file->private_data;
990 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
991}
992
Hans Verkuil59076122012-06-22 06:09:54 -0300993static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
994 struct file *file, void *fh, void *arg)
995{
996 struct v4l2_input *p = arg;
997
998 /*
999 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1000 * CAP_STD here based on ioctl handler provided by the
1001 * driver. If the driver doesn't support these
1002 * for a specific input, it must override these flags.
1003 */
1004 if (ops->vidioc_s_std)
1005 p->capabilities |= V4L2_IN_CAP_STD;
1006 if (ops->vidioc_s_dv_preset)
1007 p->capabilities |= V4L2_IN_CAP_PRESETS;
1008 if (ops->vidioc_s_dv_timings)
1009 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
1010
1011 return ops->vidioc_enum_input(file, fh, p);
1012}
1013
1014static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1015 struct file *file, void *fh, void *arg)
1016{
1017 struct v4l2_output *p = arg;
1018
1019 /*
1020 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1021 * CAP_STD here based on ioctl handler provided by the
1022 * driver. If the driver doesn't support these
1023 * for a specific output, it must override these flags.
1024 */
1025 if (ops->vidioc_s_std)
1026 p->capabilities |= V4L2_OUT_CAP_STD;
1027 if (ops->vidioc_s_dv_preset)
1028 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1029 if (ops->vidioc_s_dv_timings)
1030 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1031
1032 return ops->vidioc_enum_output(file, fh, p);
1033}
1034
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001035static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1036 struct file *file, void *fh, void *arg)
1037{
1038 struct v4l2_fmtdesc *p = arg;
1039
1040 switch (p->type) {
1041 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1042 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1043 break;
1044 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1045 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1046 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1047 break;
1048 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1049 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1050 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1051 break;
1052 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1053 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1054 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1055 break;
1056 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1057 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1058 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1059 break;
1060 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1061 case V4L2_BUF_TYPE_PRIVATE:
1062 if (unlikely(!ops->vidioc_enum_fmt_type_private))
1063 break;
1064 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
1065 }
1066 return -EINVAL;
1067}
1068
1069static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1070 struct file *file, void *fh, void *arg)
1071{
1072 struct v4l2_format *p = arg;
1073
1074 switch (p->type) {
1075 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1076 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1077 break;
1078 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1079 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1080 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
1081 break;
1082 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1083 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1084 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
1085 break;
1086 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1087 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1088 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1089 break;
1090 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1091 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1092 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
1093 break;
1094 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1095 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1096 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
1097 break;
1098 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1099 case V4L2_BUF_TYPE_VBI_CAPTURE:
1100 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
1101 break;
1102 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1103 case V4L2_BUF_TYPE_VBI_OUTPUT:
1104 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
1105 break;
1106 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1107 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1108 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
1109 break;
1110 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1111 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1112 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
1113 break;
1114 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1115 case V4L2_BUF_TYPE_PRIVATE:
1116 if (unlikely(!ops->vidioc_g_fmt_type_private))
1117 break;
1118 return ops->vidioc_g_fmt_type_private(file, fh, arg);
1119 }
1120 return -EINVAL;
1121}
1122
1123static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1124 struct file *file, void *fh, void *arg)
1125{
1126 struct v4l2_format *p = arg;
1127
1128 switch (p->type) {
1129 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1130 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1131 break;
1132 CLEAR_AFTER_FIELD(p, fmt.pix);
1133 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1134 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1135 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1136 break;
1137 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1138 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1139 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1140 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1141 break;
1142 CLEAR_AFTER_FIELD(p, fmt.win);
1143 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1144 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1145 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1146 break;
1147 CLEAR_AFTER_FIELD(p, fmt.pix);
1148 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1149 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1150 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1151 break;
1152 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1153 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1154 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1155 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1156 break;
1157 CLEAR_AFTER_FIELD(p, fmt.win);
1158 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1159 case V4L2_BUF_TYPE_VBI_CAPTURE:
1160 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1161 break;
1162 CLEAR_AFTER_FIELD(p, fmt.vbi);
1163 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1164 case V4L2_BUF_TYPE_VBI_OUTPUT:
1165 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1166 break;
1167 CLEAR_AFTER_FIELD(p, fmt.vbi);
1168 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1169 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1170 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1171 break;
1172 CLEAR_AFTER_FIELD(p, fmt.sliced);
1173 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1174 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1175 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1176 break;
1177 CLEAR_AFTER_FIELD(p, fmt.sliced);
1178 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1179 case V4L2_BUF_TYPE_PRIVATE:
1180 if (unlikely(!ops->vidioc_s_fmt_type_private))
1181 break;
1182 return ops->vidioc_s_fmt_type_private(file, fh, arg);
1183 }
1184 return -EINVAL;
1185}
1186
1187static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1188 struct file *file, void *fh, void *arg)
1189{
1190 struct v4l2_format *p = arg;
1191
1192 switch (p->type) {
1193 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1194 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1195 break;
1196 CLEAR_AFTER_FIELD(p, fmt.pix);
1197 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1198 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1199 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1200 break;
1201 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1202 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1203 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1204 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1205 break;
1206 CLEAR_AFTER_FIELD(p, fmt.win);
1207 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1209 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1210 break;
1211 CLEAR_AFTER_FIELD(p, fmt.pix);
1212 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1213 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1214 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1215 break;
1216 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1217 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1218 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1219 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1220 break;
1221 CLEAR_AFTER_FIELD(p, fmt.win);
1222 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1223 case V4L2_BUF_TYPE_VBI_CAPTURE:
1224 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1225 break;
1226 CLEAR_AFTER_FIELD(p, fmt.vbi);
1227 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1228 case V4L2_BUF_TYPE_VBI_OUTPUT:
1229 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1230 break;
1231 CLEAR_AFTER_FIELD(p, fmt.vbi);
1232 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1233 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1234 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1235 break;
1236 CLEAR_AFTER_FIELD(p, fmt.sliced);
1237 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1238 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1239 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1240 break;
1241 CLEAR_AFTER_FIELD(p, fmt.sliced);
1242 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1243 case V4L2_BUF_TYPE_PRIVATE:
1244 if (unlikely(!ops->vidioc_try_fmt_type_private))
1245 break;
1246 return ops->vidioc_try_fmt_type_private(file, fh, arg);
1247 }
1248 return -EINVAL;
1249}
1250
Hans Verkuile325b242012-06-09 12:50:15 -03001251static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1252 struct file *file, void *fh, void *arg)
1253{
1254 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1255}
1256
1257static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1258 struct file *file, void *fh, void *arg)
1259{
1260 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1261}
1262
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001263static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1264 struct file *file, void *fh, void *arg)
1265{
1266 struct video_device *vfd = video_devdata(file);
1267 struct v4l2_tuner *p = arg;
1268
1269 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1270 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1271 return ops->vidioc_g_tuner(file, fh, p);
1272}
1273
1274static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1275 struct file *file, void *fh, void *arg)
1276{
1277 struct video_device *vfd = video_devdata(file);
1278 struct v4l2_tuner *p = arg;
1279
1280 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1281 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1282 return ops->vidioc_s_tuner(file, fh, p);
1283}
1284
1285static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1286 struct file *file, void *fh, void *arg)
1287{
1288 struct video_device *vfd = video_devdata(file);
1289 struct v4l2_frequency *p = arg;
1290
1291 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1292 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1293 return ops->vidioc_g_frequency(file, fh, p);
1294}
1295
1296static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1297 struct file *file, void *fh, void *arg)
1298{
1299 struct video_device *vfd = video_devdata(file);
1300 struct v4l2_frequency *p = arg;
1301 enum v4l2_tuner_type type;
1302
1303 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1304 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1305 if (p->type != type)
1306 return -EINVAL;
1307 return ops->vidioc_s_frequency(file, fh, p);
1308}
1309
1310static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1311 struct file *file, void *fh, void *arg)
1312{
1313 struct video_device *vfd = video_devdata(file);
1314 struct v4l2_standard *p = arg;
1315 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1316 unsigned int index = p->index, i, j = 0;
1317 const char *descr = "";
1318
1319 /* Return norm array in a canonical way */
1320 for (i = 0; i <= index && id; i++) {
1321 /* last std value in the standards array is 0, so this
1322 while always ends there since (id & 0) == 0. */
1323 while ((id & standards[j].std) != standards[j].std)
1324 j++;
1325 curr_id = standards[j].std;
1326 descr = standards[j].descr;
1327 j++;
1328 if (curr_id == 0)
1329 break;
1330 if (curr_id != V4L2_STD_PAL &&
1331 curr_id != V4L2_STD_SECAM &&
1332 curr_id != V4L2_STD_NTSC)
1333 id &= ~curr_id;
1334 }
1335 if (i <= index)
1336 return -EINVAL;
1337
1338 v4l2_video_std_construct(p, curr_id, descr);
1339 return 0;
1340}
1341
1342static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1343 struct file *file, void *fh, void *arg)
1344{
1345 struct video_device *vfd = video_devdata(file);
1346 v4l2_std_id *id = arg;
1347
1348 /* Calls the specific handler */
1349 if (ops->vidioc_g_std)
1350 return ops->vidioc_g_std(file, fh, arg);
1351 if (vfd->current_norm) {
1352 *id = vfd->current_norm;
1353 return 0;
1354 }
1355 return -ENOTTY;
1356}
1357
1358static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1359 struct file *file, void *fh, void *arg)
1360{
1361 struct video_device *vfd = video_devdata(file);
1362 v4l2_std_id *id = arg, norm;
1363 int ret;
1364
1365 norm = (*id) & vfd->tvnorms;
1366 if (vfd->tvnorms && !norm) /* Check if std is supported */
1367 return -EINVAL;
1368
1369 /* Calls the specific handler */
1370 ret = ops->vidioc_s_std(file, fh, &norm);
1371
1372 /* Updates standard information */
1373 if (ret >= 0)
1374 vfd->current_norm = norm;
1375 return ret;
1376}
1377
1378static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1379 struct file *file, void *fh, void *arg)
1380{
1381 struct video_device *vfd = video_devdata(file);
1382 v4l2_std_id *p = arg;
1383
1384 /*
1385 * If nothing detected, it should return all supported
1386 * standard.
1387 * Drivers just need to mask the std argument, in order
1388 * to remove the standards that don't apply from the mask.
1389 * This means that tuners, audio and video decoders can join
1390 * their efforts to improve the standards detection.
1391 */
1392 *p = vfd->tvnorms;
1393 return ops->vidioc_querystd(file, fh, arg);
1394}
1395
1396static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1397 struct file *file, void *fh, void *arg)
1398{
1399 struct video_device *vfd = video_devdata(file);
1400 struct v4l2_hw_freq_seek *p = arg;
1401 enum v4l2_tuner_type type;
1402
1403 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1404 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1405 if (p->type != type)
1406 return -EINVAL;
1407 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1408}
1409
Hans Verkuileb3728e2012-06-22 06:23:59 -03001410static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1411 struct file *file, void *fh, void *arg)
1412{
1413 struct v4l2_requestbuffers *p = arg;
1414 int ret = check_fmt(ops, p->type);
1415
1416 if (ret)
1417 return ret;
1418
1419 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1420 CLEAR_AFTER_FIELD(p, memory);
1421
1422 return ops->vidioc_reqbufs(file, fh, p);
1423}
1424
1425static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1426 struct file *file, void *fh, void *arg)
1427{
1428 struct v4l2_buffer *p = arg;
1429 int ret = check_fmt(ops, p->type);
1430
1431 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1432}
1433
1434static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1435 struct file *file, void *fh, void *arg)
1436{
1437 struct v4l2_buffer *p = arg;
1438 int ret = check_fmt(ops, p->type);
1439
1440 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1441}
1442
1443static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1444 struct file *file, void *fh, void *arg)
1445{
1446 struct v4l2_buffer *p = arg;
1447 int ret = check_fmt(ops, p->type);
1448
1449 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1450}
1451
1452static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1453 struct file *file, void *fh, void *arg)
1454{
1455 struct v4l2_create_buffers *create = arg;
1456 int ret = check_fmt(ops, create->format.type);
1457
1458 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1459}
1460
1461static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1462 struct file *file, void *fh, void *arg)
1463{
1464 struct v4l2_buffer *b = arg;
1465 int ret = check_fmt(ops, b->type);
1466
1467 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1468}
1469
1470static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1471 struct file *file, void *fh, void *arg)
1472{
1473 struct video_device *vfd = video_devdata(file);
1474 struct v4l2_streamparm *p = arg;
1475 v4l2_std_id std;
1476 int ret = check_fmt(ops, p->type);
1477
1478 if (ret)
1479 return ret;
1480 if (ops->vidioc_g_parm)
1481 return ops->vidioc_g_parm(file, fh, p);
1482 std = vfd->current_norm;
1483 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1484 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1485 return -EINVAL;
1486 p->parm.capture.readbuffers = 2;
1487 if (ops->vidioc_g_std)
1488 ret = ops->vidioc_g_std(file, fh, &std);
1489 if (ret == 0)
1490 v4l2_video_std_frame_period(std,
1491 &p->parm.capture.timeperframe);
1492 return ret;
1493}
1494
1495static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1496 struct file *file, void *fh, void *arg)
1497{
1498 struct v4l2_streamparm *p = arg;
1499 int ret = check_fmt(ops, p->type);
1500
1501 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1502}
1503
Hans Verkuilefbceec2012-06-09 12:54:02 -03001504static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1505 struct file *file, void *fh, void *arg)
1506{
1507 struct video_device *vfd = video_devdata(file);
1508 struct v4l2_queryctrl *p = arg;
1509 struct v4l2_fh *vfh = fh;
1510
1511 if (vfh && vfh->ctrl_handler)
1512 return v4l2_queryctrl(vfh->ctrl_handler, p);
1513 if (vfd->ctrl_handler)
1514 return v4l2_queryctrl(vfd->ctrl_handler, p);
1515 if (ops->vidioc_queryctrl)
1516 return ops->vidioc_queryctrl(file, fh, p);
1517 return -ENOTTY;
1518}
1519
1520static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1521 struct file *file, void *fh, void *arg)
1522{
1523 struct video_device *vfd = video_devdata(file);
1524 struct v4l2_querymenu *p = arg;
1525 struct v4l2_fh *vfh = fh;
1526
1527 if (vfh && vfh->ctrl_handler)
1528 return v4l2_querymenu(vfh->ctrl_handler, p);
1529 if (vfd->ctrl_handler)
1530 return v4l2_querymenu(vfd->ctrl_handler, p);
1531 if (ops->vidioc_querymenu)
1532 return ops->vidioc_querymenu(file, fh, p);
1533 return -ENOTTY;
1534}
1535
1536static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1537 struct file *file, void *fh, void *arg)
1538{
1539 struct video_device *vfd = video_devdata(file);
1540 struct v4l2_control *p = arg;
1541 struct v4l2_fh *vfh = fh;
1542 struct v4l2_ext_controls ctrls;
1543 struct v4l2_ext_control ctrl;
1544
1545 if (vfh && vfh->ctrl_handler)
1546 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1547 if (vfd->ctrl_handler)
1548 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1549 if (ops->vidioc_g_ctrl)
1550 return ops->vidioc_g_ctrl(file, fh, p);
1551 if (ops->vidioc_g_ext_ctrls == NULL)
1552 return -ENOTTY;
1553
1554 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1555 ctrls.count = 1;
1556 ctrls.controls = &ctrl;
1557 ctrl.id = p->id;
1558 ctrl.value = p->value;
1559 if (check_ext_ctrls(&ctrls, 1)) {
1560 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1561
1562 if (ret == 0)
1563 p->value = ctrl.value;
1564 return ret;
1565 }
1566 return -EINVAL;
1567}
1568
1569static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1570 struct file *file, void *fh, void *arg)
1571{
1572 struct video_device *vfd = video_devdata(file);
1573 struct v4l2_control *p = arg;
1574 struct v4l2_fh *vfh = fh;
1575 struct v4l2_ext_controls ctrls;
1576 struct v4l2_ext_control ctrl;
1577
1578 if (vfh && vfh->ctrl_handler)
1579 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1580 if (vfd->ctrl_handler)
1581 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1582 if (ops->vidioc_s_ctrl)
1583 return ops->vidioc_s_ctrl(file, fh, p);
1584 if (ops->vidioc_s_ext_ctrls == NULL)
1585 return -ENOTTY;
1586
1587 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1588 ctrls.count = 1;
1589 ctrls.controls = &ctrl;
1590 ctrl.id = p->id;
1591 ctrl.value = p->value;
1592 if (check_ext_ctrls(&ctrls, 1))
1593 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1594 return -EINVAL;
1595}
1596
1597static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1598 struct file *file, void *fh, void *arg)
1599{
1600 struct video_device *vfd = video_devdata(file);
1601 struct v4l2_ext_controls *p = arg;
1602 struct v4l2_fh *vfh = fh;
1603
1604 p->error_idx = p->count;
1605 if (vfh && vfh->ctrl_handler)
1606 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1607 if (vfd->ctrl_handler)
1608 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1609 if (ops->vidioc_g_ext_ctrls == NULL)
1610 return -ENOTTY;
1611 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1612 -EINVAL;
1613}
1614
1615static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1616 struct file *file, void *fh, void *arg)
1617{
1618 struct video_device *vfd = video_devdata(file);
1619 struct v4l2_ext_controls *p = arg;
1620 struct v4l2_fh *vfh = fh;
1621
1622 p->error_idx = p->count;
1623 if (vfh && vfh->ctrl_handler)
1624 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1625 if (vfd->ctrl_handler)
1626 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1627 if (ops->vidioc_s_ext_ctrls == NULL)
1628 return -ENOTTY;
1629 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1630 -EINVAL;
1631}
1632
1633static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1634 struct file *file, void *fh, void *arg)
1635{
1636 struct video_device *vfd = video_devdata(file);
1637 struct v4l2_ext_controls *p = arg;
1638 struct v4l2_fh *vfh = fh;
1639
1640 p->error_idx = p->count;
1641 if (vfh && vfh->ctrl_handler)
1642 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1643 if (vfd->ctrl_handler)
1644 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1645 if (ops->vidioc_try_ext_ctrls == NULL)
1646 return -ENOTTY;
1647 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1648 -EINVAL;
1649}
1650
Hans Verkuild84f2d942012-06-09 11:57:46 -03001651static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1652 struct file *file, void *fh, void *arg)
1653{
1654 struct v4l2_crop *p = arg;
1655 struct v4l2_selection s = {
1656 .type = p->type,
1657 };
1658 int ret;
1659
1660 if (ops->vidioc_g_crop)
1661 return ops->vidioc_g_crop(file, fh, p);
1662 /* simulate capture crop using selection api */
1663
1664 /* crop means compose for output devices */
1665 if (V4L2_TYPE_IS_OUTPUT(p->type))
1666 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1667 else
1668 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1669
1670 ret = ops->vidioc_g_selection(file, fh, &s);
1671
1672 /* copying results to old structure on success */
1673 if (!ret)
1674 p->c = s.r;
1675 return ret;
1676}
1677
1678static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1679 struct file *file, void *fh, void *arg)
1680{
1681 struct v4l2_crop *p = arg;
1682 struct v4l2_selection s = {
1683 .type = p->type,
1684 .r = p->c,
1685 };
1686
1687 if (ops->vidioc_s_crop)
1688 return ops->vidioc_s_crop(file, fh, p);
1689 /* simulate capture crop using selection api */
1690
1691 /* crop means compose for output devices */
1692 if (V4L2_TYPE_IS_OUTPUT(p->type))
1693 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1694 else
1695 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1696
1697 return ops->vidioc_s_selection(file, fh, &s);
1698}
1699
1700static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1701 struct file *file, void *fh, void *arg)
1702{
1703 struct v4l2_cropcap *p = arg;
1704 struct v4l2_selection s = { .type = p->type };
1705 int ret;
1706
1707 if (ops->vidioc_cropcap)
1708 return ops->vidioc_cropcap(file, fh, p);
1709
1710 /* obtaining bounds */
1711 if (V4L2_TYPE_IS_OUTPUT(p->type))
1712 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1713 else
1714 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1715
1716 ret = ops->vidioc_g_selection(file, fh, &s);
1717 if (ret)
1718 return ret;
1719 p->bounds = s.r;
1720
1721 /* obtaining defrect */
1722 if (V4L2_TYPE_IS_OUTPUT(p->type))
1723 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1724 else
1725 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1726
1727 ret = ops->vidioc_g_selection(file, fh, &s);
1728 if (ret)
1729 return ret;
1730 p->defrect = s.r;
1731
1732 /* setting trivial pixelaspect */
1733 p->pixelaspect.numerator = 1;
1734 p->pixelaspect.denominator = 1;
1735 return 0;
1736}
1737
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001738static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1739 struct file *file, void *fh, void *arg)
1740{
1741 struct video_device *vfd = video_devdata(file);
1742 int ret;
1743
1744 if (vfd->v4l2_dev)
1745 pr_info("%s: ================= START STATUS =================\n",
1746 vfd->v4l2_dev->name);
1747 ret = ops->vidioc_log_status(file, fh);
1748 if (vfd->v4l2_dev)
1749 pr_info("%s: ================== END STATUS ==================\n",
1750 vfd->v4l2_dev->name);
1751 return ret;
1752}
1753
1754static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1755 struct file *file, void *fh, void *arg)
1756{
1757#ifdef CONFIG_VIDEO_ADV_DEBUG
1758 struct v4l2_dbg_register *p = arg;
1759
1760 if (!capable(CAP_SYS_ADMIN))
1761 return -EPERM;
1762 return ops->vidioc_g_register(file, fh, p);
1763#else
1764 return -ENOTTY;
1765#endif
1766}
1767
1768static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1769 struct file *file, void *fh, void *arg)
1770{
1771#ifdef CONFIG_VIDEO_ADV_DEBUG
1772 struct v4l2_dbg_register *p = arg;
1773
1774 if (!capable(CAP_SYS_ADMIN))
1775 return -EPERM;
1776 return ops->vidioc_s_register(file, fh, p);
1777#else
1778 return -ENOTTY;
1779#endif
1780}
1781
1782static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1783 struct file *file, void *fh, void *arg)
1784{
1785 struct v4l2_dbg_chip_ident *p = arg;
1786
1787 p->ident = V4L2_IDENT_NONE;
1788 p->revision = 0;
1789 return ops->vidioc_g_chip_ident(file, fh, p);
1790}
1791
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001792static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1793 struct file *file, void *fh, void *arg)
1794{
1795 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1796}
1797
1798static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1799 struct file *file, void *fh, void *arg)
1800{
1801 return ops->vidioc_subscribe_event(fh, arg);
1802}
1803
1804static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1805 struct file *file, void *fh, void *arg)
1806{
1807 return ops->vidioc_unsubscribe_event(fh, arg);
1808}
1809
1810static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1811 struct file *file, void *fh, void *arg)
1812{
1813 struct v4l2_sliced_vbi_cap *p = arg;
1814
1815 /* Clear up to type, everything after type is zeroed already */
1816 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1817
1818 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1819}
1820
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001821struct v4l2_ioctl_info {
1822 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001823 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001824 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001825 union {
1826 u32 offset;
1827 int (*func)(const struct v4l2_ioctl_ops *ops,
1828 struct file *file, void *fh, void *p);
1829 };
1830 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001831};
1832
1833/* This control needs a priority check */
1834#define INFO_FL_PRIO (1 << 0)
1835/* This control can be valid if the filehandle passes a control handler. */
1836#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001837/* This is a standard ioctl, no need for special code */
1838#define INFO_FL_STD (1 << 2)
1839/* This is ioctl has its own function */
1840#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001841/* Zero struct from after the field to the end */
1842#define INFO_FL_CLEAR(v4l2_struct, field) \
1843 ((offsetof(struct v4l2_struct, field) + \
1844 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1845#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001846
Hans Verkuil86748f32012-06-22 06:04:16 -03001847#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1848 [_IOC_NR(_ioctl)] = { \
1849 .ioctl = _ioctl, \
1850 .flags = _flags | INFO_FL_STD, \
1851 .name = #_ioctl, \
1852 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1853 .debug = _debug, \
1854 }
1855
1856#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1857 [_IOC_NR(_ioctl)] = { \
1858 .ioctl = _ioctl, \
1859 .flags = _flags | INFO_FL_FUNC, \
1860 .name = #_ioctl, \
1861 .func = _func, \
1862 .debug = _debug, \
1863 }
1864
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001865static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001866 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001867 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1868 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1869 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001870 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
1871 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001872 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1873 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001874 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001875 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
1876 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
Hans Verkuile325b242012-06-09 12:50:15 -03001877 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
1878 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001879 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1880 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001881 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1882 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1883 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001884 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001885 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1886 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 -03001887 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1888 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001889 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1890 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001891 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1892 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 -03001893 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1894 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1895 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1896 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1897 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1898 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1899 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001900 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1901 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1902 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1903 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03001904 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1905 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1906 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1907 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1908 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001909 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1910 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001911 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001912 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001913 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1914 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001915 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1916 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001917 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 -03001918 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001919 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1920 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
1921 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001922 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1923 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 -03001924 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
1925 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1926 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1927 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
1928 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001929 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
1930 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
1931 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 -03001932 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 -03001933 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
1934 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
1935 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
1936 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
1937 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
1938 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001939 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
1940 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
1941 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001942 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
1943 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
Hans Verkuilefc626b2012-06-09 10:09:07 -03001944 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
1945 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
1946 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001947};
1948#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1949
1950bool v4l2_is_known_ioctl(unsigned int cmd)
1951{
1952 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
1953 return false;
1954 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
1955}
1956
1957/* Common ioctl debug function. This function can be used by
1958 external ioctl messages as well as internal V4L ioctl */
1959void v4l_printk_ioctl(unsigned int cmd)
1960{
Hans Verkuil86748f32012-06-22 06:04:16 -03001961 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001962
1963 switch (_IOC_TYPE(cmd)) {
1964 case 'd':
1965 type = "v4l2_int";
1966 break;
1967 case 'V':
1968 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
1969 type = "v4l2";
1970 break;
1971 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001972 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001973 return;
1974 default:
1975 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03001976 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001977 }
1978
1979 switch (_IOC_DIR(cmd)) {
1980 case _IOC_NONE: dir = "--"; break;
1981 case _IOC_READ: dir = "r-"; break;
1982 case _IOC_WRITE: dir = "-w"; break;
1983 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
1984 default: dir = "*ERR*"; break;
1985 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001986 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001987 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
1988}
1989EXPORT_SYMBOL(v4l_printk_ioctl);
1990
Hans Verkuil069b7472008-12-30 07:04:34 -03001991static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001992 unsigned int cmd, void *arg)
1993{
1994 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03001995 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03001996 bool write_only = false;
1997 struct v4l2_ioctl_info default_info;
1998 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03001999 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002000 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002001 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002002 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002003
Hans Verkuil6a717882010-04-06 15:56:08 -03002004 if (ops == NULL) {
2005 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
2006 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002007 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002008 }
2009
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002010 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2011 vfh = file->private_data;
2012 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002013 }
2014
2015 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002016 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002017
2018 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2019 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002020 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002021
2022 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2023 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2024 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002025 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002026 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002027 } else {
2028 default_info.ioctl = cmd;
2029 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002030 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002031 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002032 }
2033
Hans Verkuil86748f32012-06-22 06:04:16 -03002034 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002035 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002036 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002037 pr_cont(": ");
2038 info->debug(arg, write_only);
2039 }
2040 if (info->flags & INFO_FL_STD) {
2041 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2042 const void *p = vfd->ioctl_ops;
2043 const vidioc_op *vidioc = p + info->offset;
2044
2045 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002046 } else if (info->flags & INFO_FL_FUNC) {
2047 ret = info->func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002048 } else if (!ops->vidioc_default) {
2049 ret = -ENOTTY;
2050 } else {
2051 ret = ops->vidioc_default(file, fh,
2052 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2053 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002054 }
2055
Hans Verkuil86748f32012-06-22 06:04:16 -03002056done:
2057 if (vfd->debug) {
2058 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2059 if (ret < 0)
2060 printk(KERN_DEBUG "%s: error %ld\n",
2061 video_device_node_name(vfd), ret);
2062 return ret;
2063 }
2064 v4l_print_ioctl(vfd->name, cmd);
2065 if (ret < 0)
2066 pr_cont(": error %ld\n", ret);
2067 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2068 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002069 else if (_IOC_DIR(cmd) == _IOC_NONE)
2070 info->debug(arg, write_only);
2071 else {
2072 pr_cont(": ");
2073 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002074 }
2075 }
2076
2077 return ret;
2078}
2079
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002080static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2081 void * __user *user_ptr, void ***kernel_ptr)
2082{
2083 int ret = 0;
2084
2085 switch (cmd) {
2086 case VIDIOC_QUERYBUF:
2087 case VIDIOC_QBUF:
2088 case VIDIOC_DQBUF: {
2089 struct v4l2_buffer *buf = parg;
2090
2091 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2092 if (buf->length > VIDEO_MAX_PLANES) {
2093 ret = -EINVAL;
2094 break;
2095 }
2096 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002097 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002098 *array_size = sizeof(struct v4l2_plane) * buf->length;
2099 ret = 1;
2100 }
2101 break;
2102 }
2103
2104 case VIDIOC_S_EXT_CTRLS:
2105 case VIDIOC_G_EXT_CTRLS:
2106 case VIDIOC_TRY_EXT_CTRLS: {
2107 struct v4l2_ext_controls *ctrls = parg;
2108
2109 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002110 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2111 ret = -EINVAL;
2112 break;
2113 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002114 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002115 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002116 *array_size = sizeof(struct v4l2_ext_control)
2117 * ctrls->count;
2118 ret = 1;
2119 }
2120 break;
2121 }
2122 }
2123
2124 return ret;
2125}
2126
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002127long
2128video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2129 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002130{
2131 char sbuf[128];
2132 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002133 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002134 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002135 bool has_array_args;
2136 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002137 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002138 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002139
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002140 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002141 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002142 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2143 parg = sbuf;
2144 } else {
2145 /* too big to allocate from stack */
2146 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2147 if (NULL == mbuf)
2148 return -ENOMEM;
2149 parg = mbuf;
2150 }
2151
2152 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002153 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002154 unsigned int n = _IOC_SIZE(cmd);
2155
2156 /*
2157 * In some cases, only a few fields are used as input,
2158 * i.e. when the app sets "index" and then the driver
2159 * fills in the rest of the structure for the thing
2160 * with that index. We only need to copy up the first
2161 * non-input field.
2162 */
2163 if (v4l2_is_known_ioctl(cmd)) {
2164 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2165 if (flags & INFO_FL_CLEAR_MASK)
2166 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2167 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002168
2169 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002170 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002171
2172 /* zero out anything we don't copy from userspace */
2173 if (n < _IOC_SIZE(cmd))
2174 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002175 } else {
2176 /* read-only ioctl */
2177 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002178 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002179 }
2180
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002181 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2182 if (err < 0)
2183 goto out;
2184 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002185
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002186 if (has_array_args) {
2187 /*
2188 * When adding new types of array args, make sure that the
2189 * parent argument to ioctl (which contains the pointer to the
2190 * array) fits into sbuf (so that mbuf will still remain
2191 * unused up to here).
2192 */
2193 mbuf = kmalloc(array_size, GFP_KERNEL);
2194 err = -ENOMEM;
2195 if (NULL == mbuf)
2196 goto out_array_args;
2197 err = -EFAULT;
2198 if (copy_from_user(mbuf, user_ptr, array_size))
2199 goto out_array_args;
2200 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002201 }
2202
2203 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002204 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002205 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002206 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002207
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002208 if (has_array_args) {
2209 *kernel_ptr = user_ptr;
2210 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002211 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002212 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002213 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002214 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2215 results that must be returned. */
2216 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002217 goto out;
2218
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002219out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002220 /* Copy results into user buffer */
2221 switch (_IOC_DIR(cmd)) {
2222 case _IOC_READ:
2223 case (_IOC_WRITE | _IOC_READ):
2224 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2225 err = -EFAULT;
2226 break;
2227 }
2228
2229out:
2230 kfree(mbuf);
2231 return err;
2232}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002233EXPORT_SYMBOL(video_usercopy);
2234
2235long video_ioctl2(struct file *file,
2236 unsigned int cmd, unsigned long arg)
2237{
2238 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2239}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002240EXPORT_SYMBOL(video_ioctl2);