blob: 798ee42d4e1aea90799ed99c8f089d24e9350679 [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 Verkuileb3728e2012-06-22 06:23:59 -0300558static void v4l_print_u32(const void *arg, bool write_only)
559{
560 pr_cont("value=%u\n", *(const u32 *)arg);
561}
562
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300563static inline void dbgrect(struct video_device *vfd, char *s,
564 struct v4l2_rect *r)
565{
566 dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
567 r->width, r->height);
568};
569
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300570static void dbgtimings(struct video_device *vfd,
571 const struct v4l2_dv_timings *p)
572{
573 switch (p->type) {
574 case V4L2_DV_BT_656_1120:
575 dbgarg2("bt-656/1120:interlaced=%d,"
576 " pixelclock=%lld,"
577 " width=%d, height=%d, polarities=%x,"
578 " hfrontporch=%d, hsync=%d,"
579 " hbackporch=%d, vfrontporch=%d,"
580 " vsync=%d, vbackporch=%d,"
581 " il_vfrontporch=%d, il_vsync=%d,"
582 " il_vbackporch=%d, standards=%x, flags=%x\n",
583 p->bt.interlaced, p->bt.pixelclock,
584 p->bt.width, p->bt.height,
585 p->bt.polarities, p->bt.hfrontporch,
586 p->bt.hsync, p->bt.hbackporch,
587 p->bt.vfrontporch, p->bt.vsync,
588 p->bt.vbackporch, p->bt.il_vfrontporch,
589 p->bt.il_vsync, p->bt.il_vbackporch,
590 p->bt.standards, p->bt.flags);
591 break;
592 default:
593 dbgarg2("Unknown type %d!\n", p->type);
594 break;
595 }
596}
597
Hans Verkuilefbceec2012-06-09 12:54:02 -0300598static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300599{
600 __u32 i;
601
602 /* zero the reserved fields */
603 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300604 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300605 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300606
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300607 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
608 when using extended controls.
609 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
610 is it allowed for backwards compatibility.
611 */
612 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
613 return 0;
614 /* Check that all controls are from the same control class. */
615 for (i = 0; i < c->count; i++) {
616 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
617 c->error_idx = i;
618 return 0;
619 }
620 }
621 return 1;
622}
623
Hans Verkuila3998102008-07-21 02:57:38 -0300624static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300625{
Hans Verkuila3998102008-07-21 02:57:38 -0300626 if (ops == NULL)
627 return -EINVAL;
628
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300629 switch (type) {
630 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300631 if (ops->vidioc_g_fmt_vid_cap ||
632 ops->vidioc_g_fmt_vid_cap_mplane)
633 return 0;
634 break;
635 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
636 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300637 return 0;
638 break;
639 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300640 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300641 return 0;
642 break;
643 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300644 if (ops->vidioc_g_fmt_vid_out ||
645 ops->vidioc_g_fmt_vid_out_mplane)
646 return 0;
647 break;
648 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
649 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300650 return 0;
651 break;
652 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300653 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300654 return 0;
655 break;
656 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300657 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300658 return 0;
659 break;
660 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300661 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300662 return 0;
663 break;
664 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300665 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300666 return 0;
667 break;
668 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300669 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300670 return 0;
671 break;
672 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300673 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300674 return 0;
675 break;
676 }
677 return -EINVAL;
678}
679
Hans Verkuil59076122012-06-22 06:09:54 -0300680static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
681 struct file *file, void *fh, void *arg)
682{
683 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
684
685 cap->version = LINUX_VERSION_CODE;
686 return ops->vidioc_querycap(file, fh, cap);
687}
688
689static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
690 struct file *file, void *fh, void *arg)
691{
692 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
693}
694
695static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
696 struct file *file, void *fh, void *arg)
697{
698 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
699}
700
Hans Verkuild0547cb2012-06-09 09:27:10 -0300701static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
702 struct file *file, void *fh, void *arg)
703{
704 struct video_device *vfd;
705 u32 *p = arg;
706
707 if (ops->vidioc_g_priority)
708 return ops->vidioc_g_priority(file, fh, arg);
709 vfd = video_devdata(file);
710 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
711 return 0;
712}
713
714static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
715 struct file *file, void *fh, void *arg)
716{
717 struct video_device *vfd;
718 struct v4l2_fh *vfh;
719 u32 *p = arg;
720
721 if (ops->vidioc_s_priority)
722 return ops->vidioc_s_priority(file, fh, *p);
723 vfd = video_devdata(file);
724 vfh = file->private_data;
725 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
726}
727
Hans Verkuil59076122012-06-22 06:09:54 -0300728static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
729 struct file *file, void *fh, void *arg)
730{
731 struct v4l2_input *p = arg;
732
733 /*
734 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
735 * CAP_STD here based on ioctl handler provided by the
736 * driver. If the driver doesn't support these
737 * for a specific input, it must override these flags.
738 */
739 if (ops->vidioc_s_std)
740 p->capabilities |= V4L2_IN_CAP_STD;
741 if (ops->vidioc_s_dv_preset)
742 p->capabilities |= V4L2_IN_CAP_PRESETS;
743 if (ops->vidioc_s_dv_timings)
744 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
745
746 return ops->vidioc_enum_input(file, fh, p);
747}
748
749static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
750 struct file *file, void *fh, void *arg)
751{
752 struct v4l2_output *p = arg;
753
754 /*
755 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
756 * CAP_STD here based on ioctl handler provided by the
757 * driver. If the driver doesn't support these
758 * for a specific output, it must override these flags.
759 */
760 if (ops->vidioc_s_std)
761 p->capabilities |= V4L2_OUT_CAP_STD;
762 if (ops->vidioc_s_dv_preset)
763 p->capabilities |= V4L2_OUT_CAP_PRESETS;
764 if (ops->vidioc_s_dv_timings)
765 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
766
767 return ops->vidioc_enum_output(file, fh, p);
768}
769
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300770static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
771 struct file *file, void *fh, void *arg)
772{
773 struct v4l2_fmtdesc *p = arg;
774
775 switch (p->type) {
776 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
777 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
778 break;
779 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
780 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
781 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
782 break;
783 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
784 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
785 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
786 break;
787 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
788 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
789 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
790 break;
791 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
792 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
793 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
794 break;
795 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
796 case V4L2_BUF_TYPE_PRIVATE:
797 if (unlikely(!ops->vidioc_enum_fmt_type_private))
798 break;
799 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
800 }
801 return -EINVAL;
802}
803
804static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
805 struct file *file, void *fh, void *arg)
806{
807 struct v4l2_format *p = arg;
808
809 switch (p->type) {
810 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
811 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
812 break;
813 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
814 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
815 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
816 break;
817 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
818 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
819 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
820 break;
821 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
822 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
823 if (unlikely(!ops->vidioc_g_fmt_vid_out))
824 break;
825 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
826 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
827 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
828 break;
829 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
830 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
831 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
832 break;
833 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
834 case V4L2_BUF_TYPE_VBI_CAPTURE:
835 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
836 break;
837 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
838 case V4L2_BUF_TYPE_VBI_OUTPUT:
839 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
840 break;
841 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
842 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
843 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
844 break;
845 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
846 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
847 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
848 break;
849 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
850 case V4L2_BUF_TYPE_PRIVATE:
851 if (unlikely(!ops->vidioc_g_fmt_type_private))
852 break;
853 return ops->vidioc_g_fmt_type_private(file, fh, arg);
854 }
855 return -EINVAL;
856}
857
858static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
859 struct file *file, void *fh, void *arg)
860{
861 struct v4l2_format *p = arg;
862
863 switch (p->type) {
864 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
865 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
866 break;
867 CLEAR_AFTER_FIELD(p, fmt.pix);
868 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
869 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
870 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
871 break;
872 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
873 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
874 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
875 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
876 break;
877 CLEAR_AFTER_FIELD(p, fmt.win);
878 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
879 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
880 if (unlikely(!ops->vidioc_s_fmt_vid_out))
881 break;
882 CLEAR_AFTER_FIELD(p, fmt.pix);
883 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
884 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
885 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
886 break;
887 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
888 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
889 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
890 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
891 break;
892 CLEAR_AFTER_FIELD(p, fmt.win);
893 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
894 case V4L2_BUF_TYPE_VBI_CAPTURE:
895 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
896 break;
897 CLEAR_AFTER_FIELD(p, fmt.vbi);
898 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
899 case V4L2_BUF_TYPE_VBI_OUTPUT:
900 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
901 break;
902 CLEAR_AFTER_FIELD(p, fmt.vbi);
903 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
904 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
905 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
906 break;
907 CLEAR_AFTER_FIELD(p, fmt.sliced);
908 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
909 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
910 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
911 break;
912 CLEAR_AFTER_FIELD(p, fmt.sliced);
913 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
914 case V4L2_BUF_TYPE_PRIVATE:
915 if (unlikely(!ops->vidioc_s_fmt_type_private))
916 break;
917 return ops->vidioc_s_fmt_type_private(file, fh, arg);
918 }
919 return -EINVAL;
920}
921
922static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
923 struct file *file, void *fh, void *arg)
924{
925 struct v4l2_format *p = arg;
926
927 switch (p->type) {
928 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
929 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
930 break;
931 CLEAR_AFTER_FIELD(p, fmt.pix);
932 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
933 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
934 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
935 break;
936 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
937 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
938 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
939 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
940 break;
941 CLEAR_AFTER_FIELD(p, fmt.win);
942 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
943 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
944 if (unlikely(!ops->vidioc_try_fmt_vid_out))
945 break;
946 CLEAR_AFTER_FIELD(p, fmt.pix);
947 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
948 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
949 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
950 break;
951 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
952 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
953 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
954 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
955 break;
956 CLEAR_AFTER_FIELD(p, fmt.win);
957 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
958 case V4L2_BUF_TYPE_VBI_CAPTURE:
959 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
960 break;
961 CLEAR_AFTER_FIELD(p, fmt.vbi);
962 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
963 case V4L2_BUF_TYPE_VBI_OUTPUT:
964 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
965 break;
966 CLEAR_AFTER_FIELD(p, fmt.vbi);
967 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
968 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
969 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
970 break;
971 CLEAR_AFTER_FIELD(p, fmt.sliced);
972 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
973 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
974 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
975 break;
976 CLEAR_AFTER_FIELD(p, fmt.sliced);
977 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
978 case V4L2_BUF_TYPE_PRIVATE:
979 if (unlikely(!ops->vidioc_try_fmt_type_private))
980 break;
981 return ops->vidioc_try_fmt_type_private(file, fh, arg);
982 }
983 return -EINVAL;
984}
985
Hans Verkuile325b242012-06-09 12:50:15 -0300986static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
987 struct file *file, void *fh, void *arg)
988{
989 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
990}
991
992static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
993 struct file *file, void *fh, void *arg)
994{
995 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
996}
997
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300998static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
999 struct file *file, void *fh, void *arg)
1000{
1001 struct video_device *vfd = video_devdata(file);
1002 struct v4l2_tuner *p = arg;
1003
1004 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1005 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1006 return ops->vidioc_g_tuner(file, fh, p);
1007}
1008
1009static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1010 struct file *file, void *fh, void *arg)
1011{
1012 struct video_device *vfd = video_devdata(file);
1013 struct v4l2_tuner *p = arg;
1014
1015 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1016 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1017 return ops->vidioc_s_tuner(file, fh, p);
1018}
1019
1020static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1021 struct file *file, void *fh, void *arg)
1022{
1023 struct video_device *vfd = video_devdata(file);
1024 struct v4l2_frequency *p = arg;
1025
1026 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1027 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1028 return ops->vidioc_g_frequency(file, fh, p);
1029}
1030
1031static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1032 struct file *file, void *fh, void *arg)
1033{
1034 struct video_device *vfd = video_devdata(file);
1035 struct v4l2_frequency *p = arg;
1036 enum v4l2_tuner_type type;
1037
1038 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1039 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1040 if (p->type != type)
1041 return -EINVAL;
1042 return ops->vidioc_s_frequency(file, fh, p);
1043}
1044
1045static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1046 struct file *file, void *fh, void *arg)
1047{
1048 struct video_device *vfd = video_devdata(file);
1049 struct v4l2_standard *p = arg;
1050 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1051 unsigned int index = p->index, i, j = 0;
1052 const char *descr = "";
1053
1054 /* Return norm array in a canonical way */
1055 for (i = 0; i <= index && id; i++) {
1056 /* last std value in the standards array is 0, so this
1057 while always ends there since (id & 0) == 0. */
1058 while ((id & standards[j].std) != standards[j].std)
1059 j++;
1060 curr_id = standards[j].std;
1061 descr = standards[j].descr;
1062 j++;
1063 if (curr_id == 0)
1064 break;
1065 if (curr_id != V4L2_STD_PAL &&
1066 curr_id != V4L2_STD_SECAM &&
1067 curr_id != V4L2_STD_NTSC)
1068 id &= ~curr_id;
1069 }
1070 if (i <= index)
1071 return -EINVAL;
1072
1073 v4l2_video_std_construct(p, curr_id, descr);
1074 return 0;
1075}
1076
1077static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1078 struct file *file, void *fh, void *arg)
1079{
1080 struct video_device *vfd = video_devdata(file);
1081 v4l2_std_id *id = arg;
1082
1083 /* Calls the specific handler */
1084 if (ops->vidioc_g_std)
1085 return ops->vidioc_g_std(file, fh, arg);
1086 if (vfd->current_norm) {
1087 *id = vfd->current_norm;
1088 return 0;
1089 }
1090 return -ENOTTY;
1091}
1092
1093static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1094 struct file *file, void *fh, void *arg)
1095{
1096 struct video_device *vfd = video_devdata(file);
1097 v4l2_std_id *id = arg, norm;
1098 int ret;
1099
1100 norm = (*id) & vfd->tvnorms;
1101 if (vfd->tvnorms && !norm) /* Check if std is supported */
1102 return -EINVAL;
1103
1104 /* Calls the specific handler */
1105 ret = ops->vidioc_s_std(file, fh, &norm);
1106
1107 /* Updates standard information */
1108 if (ret >= 0)
1109 vfd->current_norm = norm;
1110 return ret;
1111}
1112
1113static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1114 struct file *file, void *fh, void *arg)
1115{
1116 struct video_device *vfd = video_devdata(file);
1117 v4l2_std_id *p = arg;
1118
1119 /*
1120 * If nothing detected, it should return all supported
1121 * standard.
1122 * Drivers just need to mask the std argument, in order
1123 * to remove the standards that don't apply from the mask.
1124 * This means that tuners, audio and video decoders can join
1125 * their efforts to improve the standards detection.
1126 */
1127 *p = vfd->tvnorms;
1128 return ops->vidioc_querystd(file, fh, arg);
1129}
1130
1131static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1132 struct file *file, void *fh, void *arg)
1133{
1134 struct video_device *vfd = video_devdata(file);
1135 struct v4l2_hw_freq_seek *p = arg;
1136 enum v4l2_tuner_type type;
1137
1138 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1139 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1140 if (p->type != type)
1141 return -EINVAL;
1142 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1143}
1144
Hans Verkuileb3728e2012-06-22 06:23:59 -03001145static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1146 struct file *file, void *fh, void *arg)
1147{
1148 struct v4l2_requestbuffers *p = arg;
1149 int ret = check_fmt(ops, p->type);
1150
1151 if (ret)
1152 return ret;
1153
1154 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1155 CLEAR_AFTER_FIELD(p, memory);
1156
1157 return ops->vidioc_reqbufs(file, fh, p);
1158}
1159
1160static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1161 struct file *file, void *fh, void *arg)
1162{
1163 struct v4l2_buffer *p = arg;
1164 int ret = check_fmt(ops, p->type);
1165
1166 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1167}
1168
1169static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1170 struct file *file, void *fh, void *arg)
1171{
1172 struct v4l2_buffer *p = arg;
1173 int ret = check_fmt(ops, p->type);
1174
1175 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1176}
1177
1178static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1179 struct file *file, void *fh, void *arg)
1180{
1181 struct v4l2_buffer *p = arg;
1182 int ret = check_fmt(ops, p->type);
1183
1184 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1185}
1186
1187static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1188 struct file *file, void *fh, void *arg)
1189{
1190 struct v4l2_create_buffers *create = arg;
1191 int ret = check_fmt(ops, create->format.type);
1192
1193 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1194}
1195
1196static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1197 struct file *file, void *fh, void *arg)
1198{
1199 struct v4l2_buffer *b = arg;
1200 int ret = check_fmt(ops, b->type);
1201
1202 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1203}
1204
1205static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1206 struct file *file, void *fh, void *arg)
1207{
1208 struct video_device *vfd = video_devdata(file);
1209 struct v4l2_streamparm *p = arg;
1210 v4l2_std_id std;
1211 int ret = check_fmt(ops, p->type);
1212
1213 if (ret)
1214 return ret;
1215 if (ops->vidioc_g_parm)
1216 return ops->vidioc_g_parm(file, fh, p);
1217 std = vfd->current_norm;
1218 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1219 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1220 return -EINVAL;
1221 p->parm.capture.readbuffers = 2;
1222 if (ops->vidioc_g_std)
1223 ret = ops->vidioc_g_std(file, fh, &std);
1224 if (ret == 0)
1225 v4l2_video_std_frame_period(std,
1226 &p->parm.capture.timeperframe);
1227 return ret;
1228}
1229
1230static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1231 struct file *file, void *fh, void *arg)
1232{
1233 struct v4l2_streamparm *p = arg;
1234 int ret = check_fmt(ops, p->type);
1235
1236 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1237}
1238
Hans Verkuilefbceec2012-06-09 12:54:02 -03001239static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1240 struct file *file, void *fh, void *arg)
1241{
1242 struct video_device *vfd = video_devdata(file);
1243 struct v4l2_queryctrl *p = arg;
1244 struct v4l2_fh *vfh = fh;
1245
1246 if (vfh && vfh->ctrl_handler)
1247 return v4l2_queryctrl(vfh->ctrl_handler, p);
1248 if (vfd->ctrl_handler)
1249 return v4l2_queryctrl(vfd->ctrl_handler, p);
1250 if (ops->vidioc_queryctrl)
1251 return ops->vidioc_queryctrl(file, fh, p);
1252 return -ENOTTY;
1253}
1254
1255static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1256 struct file *file, void *fh, void *arg)
1257{
1258 struct video_device *vfd = video_devdata(file);
1259 struct v4l2_querymenu *p = arg;
1260 struct v4l2_fh *vfh = fh;
1261
1262 if (vfh && vfh->ctrl_handler)
1263 return v4l2_querymenu(vfh->ctrl_handler, p);
1264 if (vfd->ctrl_handler)
1265 return v4l2_querymenu(vfd->ctrl_handler, p);
1266 if (ops->vidioc_querymenu)
1267 return ops->vidioc_querymenu(file, fh, p);
1268 return -ENOTTY;
1269}
1270
1271static int v4l_g_ctrl(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_control *p = arg;
1276 struct v4l2_fh *vfh = fh;
1277 struct v4l2_ext_controls ctrls;
1278 struct v4l2_ext_control ctrl;
1279
1280 if (vfh && vfh->ctrl_handler)
1281 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1282 if (vfd->ctrl_handler)
1283 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1284 if (ops->vidioc_g_ctrl)
1285 return ops->vidioc_g_ctrl(file, fh, p);
1286 if (ops->vidioc_g_ext_ctrls == NULL)
1287 return -ENOTTY;
1288
1289 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1290 ctrls.count = 1;
1291 ctrls.controls = &ctrl;
1292 ctrl.id = p->id;
1293 ctrl.value = p->value;
1294 if (check_ext_ctrls(&ctrls, 1)) {
1295 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1296
1297 if (ret == 0)
1298 p->value = ctrl.value;
1299 return ret;
1300 }
1301 return -EINVAL;
1302}
1303
1304static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1305 struct file *file, void *fh, void *arg)
1306{
1307 struct video_device *vfd = video_devdata(file);
1308 struct v4l2_control *p = arg;
1309 struct v4l2_fh *vfh = fh;
1310 struct v4l2_ext_controls ctrls;
1311 struct v4l2_ext_control ctrl;
1312
1313 if (vfh && vfh->ctrl_handler)
1314 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1315 if (vfd->ctrl_handler)
1316 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1317 if (ops->vidioc_s_ctrl)
1318 return ops->vidioc_s_ctrl(file, fh, p);
1319 if (ops->vidioc_s_ext_ctrls == NULL)
1320 return -ENOTTY;
1321
1322 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1323 ctrls.count = 1;
1324 ctrls.controls = &ctrl;
1325 ctrl.id = p->id;
1326 ctrl.value = p->value;
1327 if (check_ext_ctrls(&ctrls, 1))
1328 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1329 return -EINVAL;
1330}
1331
1332static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1333 struct file *file, void *fh, void *arg)
1334{
1335 struct video_device *vfd = video_devdata(file);
1336 struct v4l2_ext_controls *p = arg;
1337 struct v4l2_fh *vfh = fh;
1338
1339 p->error_idx = p->count;
1340 if (vfh && vfh->ctrl_handler)
1341 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1342 if (vfd->ctrl_handler)
1343 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1344 if (ops->vidioc_g_ext_ctrls == NULL)
1345 return -ENOTTY;
1346 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1347 -EINVAL;
1348}
1349
1350static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1351 struct file *file, void *fh, void *arg)
1352{
1353 struct video_device *vfd = video_devdata(file);
1354 struct v4l2_ext_controls *p = arg;
1355 struct v4l2_fh *vfh = fh;
1356
1357 p->error_idx = p->count;
1358 if (vfh && vfh->ctrl_handler)
1359 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1360 if (vfd->ctrl_handler)
1361 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1362 if (ops->vidioc_s_ext_ctrls == NULL)
1363 return -ENOTTY;
1364 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1365 -EINVAL;
1366}
1367
1368static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1369 struct file *file, void *fh, void *arg)
1370{
1371 struct video_device *vfd = video_devdata(file);
1372 struct v4l2_ext_controls *p = arg;
1373 struct v4l2_fh *vfh = fh;
1374
1375 p->error_idx = p->count;
1376 if (vfh && vfh->ctrl_handler)
1377 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1378 if (vfd->ctrl_handler)
1379 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1380 if (ops->vidioc_try_ext_ctrls == NULL)
1381 return -ENOTTY;
1382 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1383 -EINVAL;
1384}
1385
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001386struct v4l2_ioctl_info {
1387 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001388 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001389 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001390 union {
1391 u32 offset;
1392 int (*func)(const struct v4l2_ioctl_ops *ops,
1393 struct file *file, void *fh, void *p);
1394 };
1395 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001396};
1397
1398/* This control needs a priority check */
1399#define INFO_FL_PRIO (1 << 0)
1400/* This control can be valid if the filehandle passes a control handler. */
1401#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001402/* This is a standard ioctl, no need for special code */
1403#define INFO_FL_STD (1 << 2)
1404/* This is ioctl has its own function */
1405#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001406/* Zero struct from after the field to the end */
1407#define INFO_FL_CLEAR(v4l2_struct, field) \
1408 ((offsetof(struct v4l2_struct, field) + \
1409 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1410#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001411
1412#define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
1413 .ioctl = _ioctl, \
1414 .flags = _flags, \
1415 .name = #_ioctl, \
1416}
1417
Hans Verkuil86748f32012-06-22 06:04:16 -03001418#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1419 [_IOC_NR(_ioctl)] = { \
1420 .ioctl = _ioctl, \
1421 .flags = _flags | INFO_FL_STD, \
1422 .name = #_ioctl, \
1423 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1424 .debug = _debug, \
1425 }
1426
1427#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1428 [_IOC_NR(_ioctl)] = { \
1429 .ioctl = _ioctl, \
1430 .flags = _flags | INFO_FL_FUNC, \
1431 .name = #_ioctl, \
1432 .func = _func, \
1433 .debug = _debug, \
1434 }
1435
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001436static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001437 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001438 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1439 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1440 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001441 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
1442 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001443 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1444 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001445 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001446 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
1447 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
Hans Verkuile325b242012-06-09 12:50:15 -03001448 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
1449 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001450 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1451 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001452 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1453 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1454 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001455 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001456 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1457 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 -03001458 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1459 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001460 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1461 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001462 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1463 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 -03001464 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1465 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1466 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1467 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1468 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1469 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1470 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001471 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1472 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1473 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1474 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001475 IOCTL_INFO(VIDIOC_CROPCAP, INFO_FL_CLEAR(v4l2_cropcap, type)),
1476 IOCTL_INFO(VIDIOC_G_CROP, INFO_FL_CLEAR(v4l2_crop, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001477 IOCTL_INFO(VIDIOC_S_CROP, INFO_FL_PRIO),
1478 IOCTL_INFO(VIDIOC_G_SELECTION, 0),
1479 IOCTL_INFO(VIDIOC_S_SELECTION, INFO_FL_PRIO),
1480 IOCTL_INFO(VIDIOC_G_JPEGCOMP, 0),
1481 IOCTL_INFO(VIDIOC_S_JPEGCOMP, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001482 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001483 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001484 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1485 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001486 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1487 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001488 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001489 IOCTL_INFO(VIDIOC_LOG_STATUS, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001490 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1491 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
1492 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001493 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1494 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001495 IOCTL_INFO(VIDIOC_G_ENC_INDEX, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001496 IOCTL_INFO(VIDIOC_ENCODER_CMD, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1497 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001498 IOCTL_INFO(VIDIOC_DECODER_CMD, INFO_FL_PRIO),
1499 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001500 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, 0),
1501 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001502 IOCTL_INFO(VIDIOC_DBG_G_CHIP_IDENT, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001503 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001504 IOCTL_INFO(VIDIOC_ENUM_DV_PRESETS, 0),
1505 IOCTL_INFO(VIDIOC_S_DV_PRESET, INFO_FL_PRIO),
1506 IOCTL_INFO(VIDIOC_G_DV_PRESET, 0),
1507 IOCTL_INFO(VIDIOC_QUERY_DV_PRESET, 0),
1508 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, INFO_FL_PRIO),
1509 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, 0),
1510 IOCTL_INFO(VIDIOC_DQEVENT, 0),
1511 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, 0),
1512 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, 0),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001513 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
1514 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001515 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, 0),
1516 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, 0),
1517 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, 0),
1518};
1519#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1520
1521bool v4l2_is_known_ioctl(unsigned int cmd)
1522{
1523 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
1524 return false;
1525 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
1526}
1527
1528/* Common ioctl debug function. This function can be used by
1529 external ioctl messages as well as internal V4L ioctl */
1530void v4l_printk_ioctl(unsigned int cmd)
1531{
Hans Verkuil86748f32012-06-22 06:04:16 -03001532 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001533
1534 switch (_IOC_TYPE(cmd)) {
1535 case 'd':
1536 type = "v4l2_int";
1537 break;
1538 case 'V':
1539 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
1540 type = "v4l2";
1541 break;
1542 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001543 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001544 return;
1545 default:
1546 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03001547 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001548 }
1549
1550 switch (_IOC_DIR(cmd)) {
1551 case _IOC_NONE: dir = "--"; break;
1552 case _IOC_READ: dir = "r-"; break;
1553 case _IOC_WRITE: dir = "-w"; break;
1554 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
1555 default: dir = "*ERR*"; break;
1556 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001557 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001558 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
1559}
1560EXPORT_SYMBOL(v4l_printk_ioctl);
1561
Hans Verkuil069b7472008-12-30 07:04:34 -03001562static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001563 unsigned int cmd, void *arg)
1564{
1565 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03001566 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03001567 bool write_only = false;
1568 struct v4l2_ioctl_info default_info;
1569 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03001570 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001571 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03001572 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001573 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001574
Hans Verkuil6a717882010-04-06 15:56:08 -03001575 if (ops == NULL) {
1576 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
1577 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001578 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03001579 }
1580
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001581 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1582 vfh = file->private_data;
1583 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001584 }
1585
1586 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03001587 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001588
1589 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
1590 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03001591 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001592
1593 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
1594 ret = v4l2_prio_check(vfd->prio, vfh->prio);
1595 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03001596 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001597 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001598 } else {
1599 default_info.ioctl = cmd;
1600 default_info.flags = 0;
1601 default_info.debug = NULL;
1602 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001603 }
1604
Hans Verkuil86748f32012-06-22 06:04:16 -03001605 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
1606 if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001607 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03001608 pr_cont(": ");
1609 info->debug(arg, write_only);
1610 }
1611 if (info->flags & INFO_FL_STD) {
1612 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
1613 const void *p = vfd->ioctl_ops;
1614 const vidioc_op *vidioc = p + info->offset;
1615
1616 ret = (*vidioc)(file, fh, arg);
1617 goto done;
1618 } else if (info->flags & INFO_FL_FUNC) {
1619 ret = info->func(ops, file, fh, arg);
1620 goto done;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001621 }
1622
Hans Verkuil6a717882010-04-06 15:56:08 -03001623 switch (cmd) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001624 case VIDIOC_G_CROP:
1625 {
1626 struct v4l2_crop *p = arg;
1627
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001628 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001629
1630 if (ops->vidioc_g_crop) {
1631 ret = ops->vidioc_g_crop(file, fh, p);
1632 } else {
1633 /* simulate capture crop using selection api */
1634 struct v4l2_selection s = {
1635 .type = p->type,
1636 };
1637
1638 /* crop means compose for output devices */
1639 if (V4L2_TYPE_IS_OUTPUT(p->type))
1640 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1641 else
1642 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1643
1644 ret = ops->vidioc_g_selection(file, fh, &s);
1645
1646 /* copying results to old structure on success */
1647 if (!ret)
1648 p->c = s.r;
1649 }
1650
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001651 if (!ret)
1652 dbgrect(vfd, "", &p->c);
1653 break;
1654 }
1655 case VIDIOC_S_CROP:
1656 {
1657 struct v4l2_crop *p = arg;
1658
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001659 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1660 dbgrect(vfd, "", &p->c);
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001661
1662 if (ops->vidioc_s_crop) {
1663 ret = ops->vidioc_s_crop(file, fh, p);
1664 } else {
1665 /* simulate capture crop using selection api */
1666 struct v4l2_selection s = {
1667 .type = p->type,
1668 .r = p->c,
1669 };
1670
1671 /* crop means compose for output devices */
1672 if (V4L2_TYPE_IS_OUTPUT(p->type))
1673 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1674 else
1675 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1676
1677 ret = ops->vidioc_s_selection(file, fh, &s);
1678 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001679 break;
1680 }
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001681 case VIDIOC_G_SELECTION:
1682 {
1683 struct v4l2_selection *p = arg;
1684
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001685 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1686
1687 ret = ops->vidioc_g_selection(file, fh, p);
1688 if (!ret)
1689 dbgrect(vfd, "", &p->r);
1690 break;
1691 }
1692 case VIDIOC_S_SELECTION:
1693 {
1694 struct v4l2_selection *p = arg;
1695
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001696
1697 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1698 dbgrect(vfd, "", &p->r);
1699
1700 ret = ops->vidioc_s_selection(file, fh, p);
1701 break;
1702 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001703 case VIDIOC_CROPCAP:
1704 {
1705 struct v4l2_cropcap *p = arg;
1706
1707 /*FIXME: Should also show v4l2_fract pixelaspect */
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001708 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001709 if (ops->vidioc_cropcap) {
1710 ret = ops->vidioc_cropcap(file, fh, p);
1711 } else {
1712 struct v4l2_selection s = { .type = p->type };
1713
1714 /* obtaining bounds */
1715 if (V4L2_TYPE_IS_OUTPUT(p->type))
1716 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1717 else
1718 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1719
1720 ret = ops->vidioc_g_selection(file, fh, &s);
1721 if (ret)
1722 break;
1723 p->bounds = s.r;
1724
1725 /* obtaining defrect */
1726 if (V4L2_TYPE_IS_OUTPUT(p->type))
1727 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1728 else
1729 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1730
1731 ret = ops->vidioc_g_selection(file, fh, &s);
1732 if (ret)
1733 break;
1734 p->defrect = s.r;
1735
1736 /* setting trivial pixelaspect */
1737 p->pixelaspect.numerator = 1;
1738 p->pixelaspect.denominator = 1;
1739 }
1740
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001741 if (!ret) {
1742 dbgrect(vfd, "bounds ", &p->bounds);
1743 dbgrect(vfd, "defrect ", &p->defrect);
1744 }
1745 break;
1746 }
1747 case VIDIOC_G_JPEGCOMP:
1748 {
1749 struct v4l2_jpegcompression *p = arg;
1750
Hans Verkuila3998102008-07-21 02:57:38 -03001751 ret = ops->vidioc_g_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001752 if (!ret)
1753 dbgarg(cmd, "quality=%d, APPn=%d, "
1754 "APP_len=%d, COM_len=%d, "
1755 "jpeg_markers=%d\n",
1756 p->quality, p->APPn, p->APP_len,
1757 p->COM_len, p->jpeg_markers);
1758 break;
1759 }
1760 case VIDIOC_S_JPEGCOMP:
1761 {
1762 struct v4l2_jpegcompression *p = arg;
1763
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001764 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1765 "COM_len=%d, jpeg_markers=%d\n",
1766 p->quality, p->APPn, p->APP_len,
1767 p->COM_len, p->jpeg_markers);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001768 ret = ops->vidioc_s_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001769 break;
1770 }
1771 case VIDIOC_G_ENC_INDEX:
1772 {
1773 struct v4l2_enc_idx *p = arg;
1774
Hans Verkuila3998102008-07-21 02:57:38 -03001775 ret = ops->vidioc_g_enc_index(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001776 if (!ret)
1777 dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1778 p->entries, p->entries_cap);
1779 break;
1780 }
1781 case VIDIOC_ENCODER_CMD:
1782 {
1783 struct v4l2_encoder_cmd *p = arg;
1784
Hans Verkuila3998102008-07-21 02:57:38 -03001785 ret = ops->vidioc_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001786 if (!ret)
1787 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1788 break;
1789 }
1790 case VIDIOC_TRY_ENCODER_CMD:
1791 {
1792 struct v4l2_encoder_cmd *p = arg;
1793
Hans Verkuila3998102008-07-21 02:57:38 -03001794 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001795 if (!ret)
1796 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1797 break;
1798 }
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001799 case VIDIOC_DECODER_CMD:
1800 {
1801 struct v4l2_decoder_cmd *p = arg;
1802
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001803 ret = ops->vidioc_decoder_cmd(file, fh, p);
1804 if (!ret)
1805 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1806 break;
1807 }
1808 case VIDIOC_TRY_DECODER_CMD:
1809 {
1810 struct v4l2_decoder_cmd *p = arg;
1811
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001812 ret = ops->vidioc_try_decoder_cmd(file, fh, p);
1813 if (!ret)
1814 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1815 break;
1816 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001817 case VIDIOC_G_SLICED_VBI_CAP:
1818 {
1819 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001820
Trent Piepho19c96e42009-03-04 01:21:02 -03001821 /* Clear up to type, everything after type is zerod already */
1822 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1823
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001824 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001825 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001826 if (!ret)
1827 dbgarg2("service_set=%d\n", p->service_set);
1828 break;
1829 }
1830 case VIDIOC_LOG_STATUS:
1831 {
Hans Verkuile2ecb252012-02-02 08:20:53 -03001832 if (vfd->v4l2_dev)
1833 pr_info("%s: ================= START STATUS =================\n",
1834 vfd->v4l2_dev->name);
Hans Verkuila3998102008-07-21 02:57:38 -03001835 ret = ops->vidioc_log_status(file, fh);
Hans Verkuile2ecb252012-02-02 08:20:53 -03001836 if (vfd->v4l2_dev)
1837 pr_info("%s: ================== END STATUS ==================\n",
1838 vfd->v4l2_dev->name);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001839 break;
1840 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001841 case VIDIOC_DBG_G_REGISTER:
1842 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001843#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001844 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001845
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001846 if (!capable(CAP_SYS_ADMIN))
1847 ret = -EPERM;
1848 else
1849 ret = ops->vidioc_g_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001850#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001851 break;
1852 }
1853 case VIDIOC_DBG_S_REGISTER:
1854 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001855#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001856 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001857
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001858 if (!capable(CAP_SYS_ADMIN))
1859 ret = -EPERM;
1860 else
1861 ret = ops->vidioc_s_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001862#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001863 break;
1864 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001865 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001866 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001867 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001868
Hans Verkuil80b36e02009-02-07 11:00:02 -03001869 p->ident = V4L2_IDENT_NONE;
1870 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001871 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001872 if (!ret)
1873 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1874 break;
1875 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001876 case VIDIOC_ENUM_FRAMESIZES:
1877 {
1878 struct v4l2_frmsizeenum *p = arg;
1879
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001880 ret = ops->vidioc_enum_framesizes(file, fh, p);
1881 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03001882 "index=%d, pixelformat=%c%c%c%c, type=%d ",
1883 p->index,
1884 (p->pixel_format & 0xff),
1885 (p->pixel_format >> 8) & 0xff,
1886 (p->pixel_format >> 16) & 0xff,
1887 (p->pixel_format >> 24) & 0xff,
1888 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001889 switch (p->type) {
1890 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001891 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001892 p->discrete.width, p->discrete.height);
1893 break;
1894 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001895 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001896 p->stepwise.min_width, p->stepwise.min_height,
1897 p->stepwise.step_width, p->stepwise.step_height,
1898 p->stepwise.max_width, p->stepwise.max_height);
1899 break;
1900 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001901 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001902 break;
1903 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001904 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001905 }
1906
1907 break;
1908 }
1909 case VIDIOC_ENUM_FRAMEINTERVALS:
1910 {
1911 struct v4l2_frmivalenum *p = arg;
1912
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001913 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1914 dbgarg(cmd,
1915 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1916 p->index, p->pixel_format,
1917 p->width, p->height, p->type);
1918 switch (p->type) {
1919 case V4L2_FRMIVAL_TYPE_DISCRETE:
1920 dbgarg2("fps=%d/%d\n",
1921 p->discrete.numerator,
1922 p->discrete.denominator);
1923 break;
1924 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03001925 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1926 p->stepwise.min.numerator,
1927 p->stepwise.min.denominator,
1928 p->stepwise.max.numerator,
1929 p->stepwise.max.denominator,
1930 p->stepwise.step.numerator,
1931 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001932 break;
1933 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1934 dbgarg2("continuous\n");
1935 break;
1936 default:
1937 dbgarg2("- Unknown type!\n");
1938 }
1939 break;
1940 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001941 case VIDIOC_ENUM_DV_PRESETS:
1942 {
1943 struct v4l2_dv_enum_preset *p = arg;
1944
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001945 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1946 if (!ret)
1947 dbgarg(cmd,
1948 "index=%d, preset=%d, name=%s, width=%d,"
1949 " height=%d ",
1950 p->index, p->preset, p->name, p->width,
1951 p->height);
1952 break;
1953 }
1954 case VIDIOC_S_DV_PRESET:
1955 {
1956 struct v4l2_dv_preset *p = arg;
1957
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001958 dbgarg(cmd, "preset=%d\n", p->preset);
1959 ret = ops->vidioc_s_dv_preset(file, fh, p);
1960 break;
1961 }
1962 case VIDIOC_G_DV_PRESET:
1963 {
1964 struct v4l2_dv_preset *p = arg;
1965
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001966 ret = ops->vidioc_g_dv_preset(file, fh, p);
1967 if (!ret)
1968 dbgarg(cmd, "preset=%d\n", p->preset);
1969 break;
1970 }
1971 case VIDIOC_QUERY_DV_PRESET:
1972 {
1973 struct v4l2_dv_preset *p = arg;
1974
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001975 ret = ops->vidioc_query_dv_preset(file, fh, p);
1976 if (!ret)
1977 dbgarg(cmd, "preset=%d\n", p->preset);
1978 break;
1979 }
1980 case VIDIOC_S_DV_TIMINGS:
1981 {
1982 struct v4l2_dv_timings *p = arg;
1983
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001984 dbgtimings(vfd, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001985 switch (p->type) {
1986 case V4L2_DV_BT_656_1120:
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001987 ret = ops->vidioc_s_dv_timings(file, fh, p);
1988 break;
1989 default:
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001990 ret = -EINVAL;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001991 break;
1992 }
1993 break;
1994 }
1995 case VIDIOC_G_DV_TIMINGS:
1996 {
1997 struct v4l2_dv_timings *p = arg;
1998
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001999 ret = ops->vidioc_g_dv_timings(file, fh, p);
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002000 if (!ret)
2001 dbgtimings(vfd, p);
2002 break;
2003 }
2004 case VIDIOC_ENUM_DV_TIMINGS:
2005 {
2006 struct v4l2_enum_dv_timings *p = arg;
2007
2008 if (!ops->vidioc_enum_dv_timings)
2009 break;
2010
2011 ret = ops->vidioc_enum_dv_timings(file, fh, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002012 if (!ret) {
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002013 dbgarg(cmd, "index=%d: ", p->index);
2014 dbgtimings(vfd, &p->timings);
2015 }
2016 break;
2017 }
2018 case VIDIOC_QUERY_DV_TIMINGS:
2019 {
2020 struct v4l2_dv_timings *p = arg;
2021
2022 if (!ops->vidioc_query_dv_timings)
2023 break;
2024
2025 ret = ops->vidioc_query_dv_timings(file, fh, p);
2026 if (!ret)
2027 dbgtimings(vfd, p);
2028 break;
2029 }
2030 case VIDIOC_DV_TIMINGS_CAP:
2031 {
2032 struct v4l2_dv_timings_cap *p = arg;
2033
2034 if (!ops->vidioc_dv_timings_cap)
2035 break;
2036
2037 ret = ops->vidioc_dv_timings_cap(file, fh, p);
2038 if (ret)
2039 break;
2040 switch (p->type) {
2041 case V4L2_DV_BT_656_1120:
2042 dbgarg(cmd,
2043 "type=%d, width=%u-%u, height=%u-%u, "
2044 "pixelclock=%llu-%llu, standards=%x, capabilities=%x ",
2045 p->type,
2046 p->bt.min_width, p->bt.max_width,
2047 p->bt.min_height, p->bt.max_height,
2048 p->bt.min_pixelclock, p->bt.max_pixelclock,
2049 p->bt.standards, p->bt.capabilities);
2050 break;
2051 default:
2052 dbgarg(cmd, "unknown type ");
2053 break;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002054 }
2055 break;
2056 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002057 case VIDIOC_DQEVENT:
2058 {
2059 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002060
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002061 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2062 if (ret < 0) {
2063 dbgarg(cmd, "no pending events?");
2064 break;
2065 }
2066 dbgarg(cmd,
2067 "pending=%d, type=0x%8.8x, sequence=%d, "
2068 "timestamp=%lu.%9.9lu ",
2069 ev->pending, ev->type, ev->sequence,
2070 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2071 break;
2072 }
2073 case VIDIOC_SUBSCRIBE_EVENT:
2074 {
2075 struct v4l2_event_subscription *sub = arg;
2076
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002077 ret = ops->vidioc_subscribe_event(fh, sub);
2078 if (ret < 0) {
2079 dbgarg(cmd, "failed, ret=%ld", ret);
2080 break;
2081 }
2082 dbgarg(cmd, "type=0x%8.8x", sub->type);
2083 break;
2084 }
2085 case VIDIOC_UNSUBSCRIBE_EVENT:
2086 {
2087 struct v4l2_event_subscription *sub = arg;
2088
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002089 ret = ops->vidioc_unsubscribe_event(fh, sub);
2090 if (ret < 0) {
2091 dbgarg(cmd, "failed, ret=%ld", ret);
2092 break;
2093 }
2094 dbgarg(cmd, "type=0x%8.8x", sub->type);
2095 break;
2096 }
Hans Verkuila3998102008-07-21 02:57:38 -03002097 default:
Hans Verkuila3998102008-07-21 02:57:38 -03002098 if (!ops->vidioc_default)
2099 break;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002100 ret = ops->vidioc_default(file, fh, use_fh_prio ?
2101 v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2102 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002103 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002104 } /* switch */
2105
Hans Verkuil86748f32012-06-22 06:04:16 -03002106done:
2107 if (vfd->debug) {
2108 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2109 if (ret < 0)
2110 printk(KERN_DEBUG "%s: error %ld\n",
2111 video_device_node_name(vfd), ret);
2112 return ret;
2113 }
2114 v4l_print_ioctl(vfd->name, cmd);
2115 if (ret < 0)
2116 pr_cont(": error %ld\n", ret);
2117 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2118 pr_cont("\n");
2119 else if (!info->debug)
2120 return ret;
2121 else if (_IOC_DIR(cmd) == _IOC_NONE)
2122 info->debug(arg, write_only);
2123 else {
2124 pr_cont(": ");
2125 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002126 }
2127 }
2128
2129 return ret;
2130}
2131
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002132static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2133 void * __user *user_ptr, void ***kernel_ptr)
2134{
2135 int ret = 0;
2136
2137 switch (cmd) {
2138 case VIDIOC_QUERYBUF:
2139 case VIDIOC_QBUF:
2140 case VIDIOC_DQBUF: {
2141 struct v4l2_buffer *buf = parg;
2142
2143 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2144 if (buf->length > VIDEO_MAX_PLANES) {
2145 ret = -EINVAL;
2146 break;
2147 }
2148 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002149 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002150 *array_size = sizeof(struct v4l2_plane) * buf->length;
2151 ret = 1;
2152 }
2153 break;
2154 }
2155
2156 case VIDIOC_S_EXT_CTRLS:
2157 case VIDIOC_G_EXT_CTRLS:
2158 case VIDIOC_TRY_EXT_CTRLS: {
2159 struct v4l2_ext_controls *ctrls = parg;
2160
2161 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002162 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2163 ret = -EINVAL;
2164 break;
2165 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002166 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002167 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002168 *array_size = sizeof(struct v4l2_ext_control)
2169 * ctrls->count;
2170 ret = 1;
2171 }
2172 break;
2173 }
2174 }
2175
2176 return ret;
2177}
2178
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002179long
2180video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2181 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002182{
2183 char sbuf[128];
2184 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002185 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002186 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002187 bool has_array_args;
2188 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002189 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002190 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002191
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002192 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002193 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002194 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2195 parg = sbuf;
2196 } else {
2197 /* too big to allocate from stack */
2198 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2199 if (NULL == mbuf)
2200 return -ENOMEM;
2201 parg = mbuf;
2202 }
2203
2204 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002205 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002206 unsigned int n = _IOC_SIZE(cmd);
2207
2208 /*
2209 * In some cases, only a few fields are used as input,
2210 * i.e. when the app sets "index" and then the driver
2211 * fills in the rest of the structure for the thing
2212 * with that index. We only need to copy up the first
2213 * non-input field.
2214 */
2215 if (v4l2_is_known_ioctl(cmd)) {
2216 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2217 if (flags & INFO_FL_CLEAR_MASK)
2218 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2219 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002220
2221 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002222 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002223
2224 /* zero out anything we don't copy from userspace */
2225 if (n < _IOC_SIZE(cmd))
2226 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002227 } else {
2228 /* read-only ioctl */
2229 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002230 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002231 }
2232
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002233 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2234 if (err < 0)
2235 goto out;
2236 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002237
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002238 if (has_array_args) {
2239 /*
2240 * When adding new types of array args, make sure that the
2241 * parent argument to ioctl (which contains the pointer to the
2242 * array) fits into sbuf (so that mbuf will still remain
2243 * unused up to here).
2244 */
2245 mbuf = kmalloc(array_size, GFP_KERNEL);
2246 err = -ENOMEM;
2247 if (NULL == mbuf)
2248 goto out_array_args;
2249 err = -EFAULT;
2250 if (copy_from_user(mbuf, user_ptr, array_size))
2251 goto out_array_args;
2252 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002253 }
2254
2255 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002256 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002257 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002258 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002259
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002260 if (has_array_args) {
2261 *kernel_ptr = user_ptr;
2262 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002263 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002264 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002265 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002266 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2267 results that must be returned. */
2268 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002269 goto out;
2270
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002271out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002272 /* Copy results into user buffer */
2273 switch (_IOC_DIR(cmd)) {
2274 case _IOC_READ:
2275 case (_IOC_WRITE | _IOC_READ):
2276 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2277 err = -EFAULT;
2278 break;
2279 }
2280
2281out:
2282 kfree(mbuf);
2283 return err;
2284}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002285EXPORT_SYMBOL(video_usercopy);
2286
2287long video_ioctl2(struct file *file,
2288 unsigned int cmd, unsigned long arg)
2289{
2290 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2291}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002292EXPORT_SYMBOL(video_ioctl2);