blob: 1f75a6c127ee691b62f8655b50c167240fe7e378 [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
515static void v4l_print_u32(const void *arg, bool write_only)
516{
517 pr_cont("value=%u\n", *(const u32 *)arg);
518}
519
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300520static inline void dbgrect(struct video_device *vfd, char *s,
521 struct v4l2_rect *r)
522{
523 dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
524 r->width, r->height);
525};
526
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300527static void dbgtimings(struct video_device *vfd,
528 const struct v4l2_dv_timings *p)
529{
530 switch (p->type) {
531 case V4L2_DV_BT_656_1120:
532 dbgarg2("bt-656/1120:interlaced=%d,"
533 " pixelclock=%lld,"
534 " width=%d, height=%d, polarities=%x,"
535 " hfrontporch=%d, hsync=%d,"
536 " hbackporch=%d, vfrontporch=%d,"
537 " vsync=%d, vbackporch=%d,"
538 " il_vfrontporch=%d, il_vsync=%d,"
539 " il_vbackporch=%d, standards=%x, flags=%x\n",
540 p->bt.interlaced, p->bt.pixelclock,
541 p->bt.width, p->bt.height,
542 p->bt.polarities, p->bt.hfrontporch,
543 p->bt.hsync, p->bt.hbackporch,
544 p->bt.vfrontporch, p->bt.vsync,
545 p->bt.vbackporch, p->bt.il_vfrontporch,
546 p->bt.il_vsync, p->bt.il_vbackporch,
547 p->bt.standards, p->bt.flags);
548 break;
549 default:
550 dbgarg2("Unknown type %d!\n", p->type);
551 break;
552 }
553}
554
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300555static inline void v4l_print_ext_ctrls(unsigned int cmd,
556 struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
557{
558 __u32 i;
559
560 if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
561 return;
562 dbgarg(cmd, "");
563 printk(KERN_CONT "class=0x%x", c->ctrl_class);
564 for (i = 0; i < c->count; i++) {
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300565 if (show_vals && !c->controls[i].size)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300566 printk(KERN_CONT " id/val=0x%x/0x%x",
567 c->controls[i].id, c->controls[i].value);
568 else
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300569 printk(KERN_CONT " id=0x%x,size=%u",
570 c->controls[i].id, c->controls[i].size);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300571 }
572 printk(KERN_CONT "\n");
573};
574
575static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
576{
577 __u32 i;
578
579 /* zero the reserved fields */
580 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300581 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300582 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300583
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300584 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
585 when using extended controls.
586 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
587 is it allowed for backwards compatibility.
588 */
589 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
590 return 0;
591 /* Check that all controls are from the same control class. */
592 for (i = 0; i < c->count; i++) {
593 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
594 c->error_idx = i;
595 return 0;
596 }
597 }
598 return 1;
599}
600
Hans Verkuila3998102008-07-21 02:57:38 -0300601static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300602{
Hans Verkuila3998102008-07-21 02:57:38 -0300603 if (ops == NULL)
604 return -EINVAL;
605
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300606 switch (type) {
607 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300608 if (ops->vidioc_g_fmt_vid_cap ||
609 ops->vidioc_g_fmt_vid_cap_mplane)
610 return 0;
611 break;
612 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
613 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300614 return 0;
615 break;
616 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300617 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300618 return 0;
619 break;
620 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300621 if (ops->vidioc_g_fmt_vid_out ||
622 ops->vidioc_g_fmt_vid_out_mplane)
623 return 0;
624 break;
625 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
626 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300627 return 0;
628 break;
629 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300630 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300631 return 0;
632 break;
633 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300634 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300635 return 0;
636 break;
637 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300638 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300639 return 0;
640 break;
641 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300642 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300643 return 0;
644 break;
645 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300646 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300647 return 0;
648 break;
649 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300650 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300651 return 0;
652 break;
653 }
654 return -EINVAL;
655}
656
Hans Verkuil59076122012-06-22 06:09:54 -0300657static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
658 struct file *file, void *fh, void *arg)
659{
660 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
661
662 cap->version = LINUX_VERSION_CODE;
663 return ops->vidioc_querycap(file, fh, cap);
664}
665
666static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
667 struct file *file, void *fh, void *arg)
668{
669 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
670}
671
672static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
673 struct file *file, void *fh, void *arg)
674{
675 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
676}
677
Hans Verkuild0547cb2012-06-09 09:27:10 -0300678static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
679 struct file *file, void *fh, void *arg)
680{
681 struct video_device *vfd;
682 u32 *p = arg;
683
684 if (ops->vidioc_g_priority)
685 return ops->vidioc_g_priority(file, fh, arg);
686 vfd = video_devdata(file);
687 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
688 return 0;
689}
690
691static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
692 struct file *file, void *fh, void *arg)
693{
694 struct video_device *vfd;
695 struct v4l2_fh *vfh;
696 u32 *p = arg;
697
698 if (ops->vidioc_s_priority)
699 return ops->vidioc_s_priority(file, fh, *p);
700 vfd = video_devdata(file);
701 vfh = file->private_data;
702 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
703}
704
Hans Verkuil59076122012-06-22 06:09:54 -0300705static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
706 struct file *file, void *fh, void *arg)
707{
708 struct v4l2_input *p = arg;
709
710 /*
711 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
712 * CAP_STD here based on ioctl handler provided by the
713 * driver. If the driver doesn't support these
714 * for a specific input, it must override these flags.
715 */
716 if (ops->vidioc_s_std)
717 p->capabilities |= V4L2_IN_CAP_STD;
718 if (ops->vidioc_s_dv_preset)
719 p->capabilities |= V4L2_IN_CAP_PRESETS;
720 if (ops->vidioc_s_dv_timings)
721 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
722
723 return ops->vidioc_enum_input(file, fh, p);
724}
725
726static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
727 struct file *file, void *fh, void *arg)
728{
729 struct v4l2_output *p = arg;
730
731 /*
732 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
733 * CAP_STD here based on ioctl handler provided by the
734 * driver. If the driver doesn't support these
735 * for a specific output, it must override these flags.
736 */
737 if (ops->vidioc_s_std)
738 p->capabilities |= V4L2_OUT_CAP_STD;
739 if (ops->vidioc_s_dv_preset)
740 p->capabilities |= V4L2_OUT_CAP_PRESETS;
741 if (ops->vidioc_s_dv_timings)
742 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
743
744 return ops->vidioc_enum_output(file, fh, p);
745}
746
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300747static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
748 struct file *file, void *fh, void *arg)
749{
750 struct v4l2_fmtdesc *p = arg;
751
752 switch (p->type) {
753 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
754 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
755 break;
756 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
757 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
758 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
759 break;
760 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
761 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
762 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
763 break;
764 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
765 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
766 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
767 break;
768 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
769 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
770 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
771 break;
772 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
773 case V4L2_BUF_TYPE_PRIVATE:
774 if (unlikely(!ops->vidioc_enum_fmt_type_private))
775 break;
776 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
777 }
778 return -EINVAL;
779}
780
781static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
782 struct file *file, void *fh, void *arg)
783{
784 struct v4l2_format *p = arg;
785
786 switch (p->type) {
787 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
789 break;
790 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
791 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
792 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
793 break;
794 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
795 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
796 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
797 break;
798 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
799 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
800 if (unlikely(!ops->vidioc_g_fmt_vid_out))
801 break;
802 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
803 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
804 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
805 break;
806 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
807 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
808 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
809 break;
810 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
811 case V4L2_BUF_TYPE_VBI_CAPTURE:
812 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
813 break;
814 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
815 case V4L2_BUF_TYPE_VBI_OUTPUT:
816 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
817 break;
818 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
819 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
820 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
821 break;
822 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
823 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
824 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
825 break;
826 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
827 case V4L2_BUF_TYPE_PRIVATE:
828 if (unlikely(!ops->vidioc_g_fmt_type_private))
829 break;
830 return ops->vidioc_g_fmt_type_private(file, fh, arg);
831 }
832 return -EINVAL;
833}
834
835static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
836 struct file *file, void *fh, void *arg)
837{
838 struct v4l2_format *p = arg;
839
840 switch (p->type) {
841 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
842 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
843 break;
844 CLEAR_AFTER_FIELD(p, fmt.pix);
845 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
846 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
847 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
848 break;
849 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
850 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
851 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
852 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
853 break;
854 CLEAR_AFTER_FIELD(p, fmt.win);
855 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
856 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
857 if (unlikely(!ops->vidioc_s_fmt_vid_out))
858 break;
859 CLEAR_AFTER_FIELD(p, fmt.pix);
860 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
861 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
862 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
863 break;
864 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
865 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
866 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
867 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
868 break;
869 CLEAR_AFTER_FIELD(p, fmt.win);
870 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
871 case V4L2_BUF_TYPE_VBI_CAPTURE:
872 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
873 break;
874 CLEAR_AFTER_FIELD(p, fmt.vbi);
875 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
876 case V4L2_BUF_TYPE_VBI_OUTPUT:
877 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
878 break;
879 CLEAR_AFTER_FIELD(p, fmt.vbi);
880 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
881 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
882 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
883 break;
884 CLEAR_AFTER_FIELD(p, fmt.sliced);
885 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
886 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
887 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
888 break;
889 CLEAR_AFTER_FIELD(p, fmt.sliced);
890 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
891 case V4L2_BUF_TYPE_PRIVATE:
892 if (unlikely(!ops->vidioc_s_fmt_type_private))
893 break;
894 return ops->vidioc_s_fmt_type_private(file, fh, arg);
895 }
896 return -EINVAL;
897}
898
899static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
900 struct file *file, void *fh, void *arg)
901{
902 struct v4l2_format *p = arg;
903
904 switch (p->type) {
905 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
906 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
907 break;
908 CLEAR_AFTER_FIELD(p, fmt.pix);
909 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
910 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
911 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
912 break;
913 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
914 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
915 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
916 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
917 break;
918 CLEAR_AFTER_FIELD(p, fmt.win);
919 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
920 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
921 if (unlikely(!ops->vidioc_try_fmt_vid_out))
922 break;
923 CLEAR_AFTER_FIELD(p, fmt.pix);
924 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
925 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
926 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
927 break;
928 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
929 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
930 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
931 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
932 break;
933 CLEAR_AFTER_FIELD(p, fmt.win);
934 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
935 case V4L2_BUF_TYPE_VBI_CAPTURE:
936 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
937 break;
938 CLEAR_AFTER_FIELD(p, fmt.vbi);
939 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
940 case V4L2_BUF_TYPE_VBI_OUTPUT:
941 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
942 break;
943 CLEAR_AFTER_FIELD(p, fmt.vbi);
944 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
945 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
946 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
947 break;
948 CLEAR_AFTER_FIELD(p, fmt.sliced);
949 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
950 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
951 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
952 break;
953 CLEAR_AFTER_FIELD(p, fmt.sliced);
954 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
955 case V4L2_BUF_TYPE_PRIVATE:
956 if (unlikely(!ops->vidioc_try_fmt_type_private))
957 break;
958 return ops->vidioc_try_fmt_type_private(file, fh, arg);
959 }
960 return -EINVAL;
961}
962
Hans Verkuile325b242012-06-09 12:50:15 -0300963static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
964 struct file *file, void *fh, void *arg)
965{
966 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
967}
968
969static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
970 struct file *file, void *fh, void *arg)
971{
972 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
973}
974
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300975static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
976 struct file *file, void *fh, void *arg)
977{
978 struct video_device *vfd = video_devdata(file);
979 struct v4l2_tuner *p = arg;
980
981 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
982 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
983 return ops->vidioc_g_tuner(file, fh, p);
984}
985
986static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
987 struct file *file, void *fh, void *arg)
988{
989 struct video_device *vfd = video_devdata(file);
990 struct v4l2_tuner *p = arg;
991
992 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
993 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
994 return ops->vidioc_s_tuner(file, fh, p);
995}
996
997static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
998 struct file *file, void *fh, void *arg)
999{
1000 struct video_device *vfd = video_devdata(file);
1001 struct v4l2_frequency *p = arg;
1002
1003 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1004 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1005 return ops->vidioc_g_frequency(file, fh, p);
1006}
1007
1008static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1009 struct file *file, void *fh, void *arg)
1010{
1011 struct video_device *vfd = video_devdata(file);
1012 struct v4l2_frequency *p = arg;
1013 enum v4l2_tuner_type type;
1014
1015 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1016 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1017 if (p->type != type)
1018 return -EINVAL;
1019 return ops->vidioc_s_frequency(file, fh, p);
1020}
1021
1022static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1023 struct file *file, void *fh, void *arg)
1024{
1025 struct video_device *vfd = video_devdata(file);
1026 struct v4l2_standard *p = arg;
1027 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1028 unsigned int index = p->index, i, j = 0;
1029 const char *descr = "";
1030
1031 /* Return norm array in a canonical way */
1032 for (i = 0; i <= index && id; i++) {
1033 /* last std value in the standards array is 0, so this
1034 while always ends there since (id & 0) == 0. */
1035 while ((id & standards[j].std) != standards[j].std)
1036 j++;
1037 curr_id = standards[j].std;
1038 descr = standards[j].descr;
1039 j++;
1040 if (curr_id == 0)
1041 break;
1042 if (curr_id != V4L2_STD_PAL &&
1043 curr_id != V4L2_STD_SECAM &&
1044 curr_id != V4L2_STD_NTSC)
1045 id &= ~curr_id;
1046 }
1047 if (i <= index)
1048 return -EINVAL;
1049
1050 v4l2_video_std_construct(p, curr_id, descr);
1051 return 0;
1052}
1053
1054static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1055 struct file *file, void *fh, void *arg)
1056{
1057 struct video_device *vfd = video_devdata(file);
1058 v4l2_std_id *id = arg;
1059
1060 /* Calls the specific handler */
1061 if (ops->vidioc_g_std)
1062 return ops->vidioc_g_std(file, fh, arg);
1063 if (vfd->current_norm) {
1064 *id = vfd->current_norm;
1065 return 0;
1066 }
1067 return -ENOTTY;
1068}
1069
1070static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1071 struct file *file, void *fh, void *arg)
1072{
1073 struct video_device *vfd = video_devdata(file);
1074 v4l2_std_id *id = arg, norm;
1075 int ret;
1076
1077 norm = (*id) & vfd->tvnorms;
1078 if (vfd->tvnorms && !norm) /* Check if std is supported */
1079 return -EINVAL;
1080
1081 /* Calls the specific handler */
1082 ret = ops->vidioc_s_std(file, fh, &norm);
1083
1084 /* Updates standard information */
1085 if (ret >= 0)
1086 vfd->current_norm = norm;
1087 return ret;
1088}
1089
1090static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1091 struct file *file, void *fh, void *arg)
1092{
1093 struct video_device *vfd = video_devdata(file);
1094 v4l2_std_id *p = arg;
1095
1096 /*
1097 * If nothing detected, it should return all supported
1098 * standard.
1099 * Drivers just need to mask the std argument, in order
1100 * to remove the standards that don't apply from the mask.
1101 * This means that tuners, audio and video decoders can join
1102 * their efforts to improve the standards detection.
1103 */
1104 *p = vfd->tvnorms;
1105 return ops->vidioc_querystd(file, fh, arg);
1106}
1107
1108static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1109 struct file *file, void *fh, void *arg)
1110{
1111 struct video_device *vfd = video_devdata(file);
1112 struct v4l2_hw_freq_seek *p = arg;
1113 enum v4l2_tuner_type type;
1114
1115 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1116 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1117 if (p->type != type)
1118 return -EINVAL;
1119 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1120}
1121
Hans Verkuileb3728e2012-06-22 06:23:59 -03001122static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1123 struct file *file, void *fh, void *arg)
1124{
1125 struct v4l2_requestbuffers *p = arg;
1126 int ret = check_fmt(ops, p->type);
1127
1128 if (ret)
1129 return ret;
1130
1131 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1132 CLEAR_AFTER_FIELD(p, memory);
1133
1134 return ops->vidioc_reqbufs(file, fh, p);
1135}
1136
1137static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1138 struct file *file, void *fh, void *arg)
1139{
1140 struct v4l2_buffer *p = arg;
1141 int ret = check_fmt(ops, p->type);
1142
1143 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1144}
1145
1146static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1147 struct file *file, void *fh, void *arg)
1148{
1149 struct v4l2_buffer *p = arg;
1150 int ret = check_fmt(ops, p->type);
1151
1152 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1153}
1154
1155static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1156 struct file *file, void *fh, void *arg)
1157{
1158 struct v4l2_buffer *p = arg;
1159 int ret = check_fmt(ops, p->type);
1160
1161 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1162}
1163
1164static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1165 struct file *file, void *fh, void *arg)
1166{
1167 struct v4l2_create_buffers *create = arg;
1168 int ret = check_fmt(ops, create->format.type);
1169
1170 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1171}
1172
1173static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1174 struct file *file, void *fh, void *arg)
1175{
1176 struct v4l2_buffer *b = arg;
1177 int ret = check_fmt(ops, b->type);
1178
1179 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1180}
1181
1182static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1183 struct file *file, void *fh, void *arg)
1184{
1185 struct video_device *vfd = video_devdata(file);
1186 struct v4l2_streamparm *p = arg;
1187 v4l2_std_id std;
1188 int ret = check_fmt(ops, p->type);
1189
1190 if (ret)
1191 return ret;
1192 if (ops->vidioc_g_parm)
1193 return ops->vidioc_g_parm(file, fh, p);
1194 std = vfd->current_norm;
1195 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1196 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1197 return -EINVAL;
1198 p->parm.capture.readbuffers = 2;
1199 if (ops->vidioc_g_std)
1200 ret = ops->vidioc_g_std(file, fh, &std);
1201 if (ret == 0)
1202 v4l2_video_std_frame_period(std,
1203 &p->parm.capture.timeperframe);
1204 return ret;
1205}
1206
1207static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1208 struct file *file, void *fh, void *arg)
1209{
1210 struct v4l2_streamparm *p = arg;
1211 int ret = check_fmt(ops, p->type);
1212
1213 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1214}
1215
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001216struct v4l2_ioctl_info {
1217 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001218 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001219 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001220 union {
1221 u32 offset;
1222 int (*func)(const struct v4l2_ioctl_ops *ops,
1223 struct file *file, void *fh, void *p);
1224 };
1225 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001226};
1227
1228/* This control needs a priority check */
1229#define INFO_FL_PRIO (1 << 0)
1230/* This control can be valid if the filehandle passes a control handler. */
1231#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001232/* This is a standard ioctl, no need for special code */
1233#define INFO_FL_STD (1 << 2)
1234/* This is ioctl has its own function */
1235#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001236/* Zero struct from after the field to the end */
1237#define INFO_FL_CLEAR(v4l2_struct, field) \
1238 ((offsetof(struct v4l2_struct, field) + \
1239 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1240#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001241
1242#define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
1243 .ioctl = _ioctl, \
1244 .flags = _flags, \
1245 .name = #_ioctl, \
1246}
1247
Hans Verkuil86748f32012-06-22 06:04:16 -03001248#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1249 [_IOC_NR(_ioctl)] = { \
1250 .ioctl = _ioctl, \
1251 .flags = _flags | INFO_FL_STD, \
1252 .name = #_ioctl, \
1253 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1254 .debug = _debug, \
1255 }
1256
1257#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1258 [_IOC_NR(_ioctl)] = { \
1259 .ioctl = _ioctl, \
1260 .flags = _flags | INFO_FL_FUNC, \
1261 .name = #_ioctl, \
1262 .func = _func, \
1263 .debug = _debug, \
1264 }
1265
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001266static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001267 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001268 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1269 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1270 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001271 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
1272 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001273 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1274 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001275 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001276 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
1277 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
Hans Verkuile325b242012-06-09 12:50:15 -03001278 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
1279 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001280 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1281 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001282 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1283 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1284 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001285 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001286 IOCTL_INFO(VIDIOC_G_CTRL, INFO_FL_CTRL),
1287 IOCTL_INFO(VIDIOC_S_CTRL, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001288 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1289 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001290 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1291 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001292 IOCTL_INFO(VIDIOC_QUERYCTRL, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1293 IOCTL_INFO(VIDIOC_QUERYMENU, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001294 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1295 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1296 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1297 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1298 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1299 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1300 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001301 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1302 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1303 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1304 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001305 IOCTL_INFO(VIDIOC_CROPCAP, INFO_FL_CLEAR(v4l2_cropcap, type)),
1306 IOCTL_INFO(VIDIOC_G_CROP, INFO_FL_CLEAR(v4l2_crop, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001307 IOCTL_INFO(VIDIOC_S_CROP, INFO_FL_PRIO),
1308 IOCTL_INFO(VIDIOC_G_SELECTION, 0),
1309 IOCTL_INFO(VIDIOC_S_SELECTION, INFO_FL_PRIO),
1310 IOCTL_INFO(VIDIOC_G_JPEGCOMP, 0),
1311 IOCTL_INFO(VIDIOC_S_JPEGCOMP, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001312 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001313 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001314 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1315 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001316 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1317 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001318 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001319 IOCTL_INFO(VIDIOC_LOG_STATUS, 0),
1320 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, INFO_FL_CTRL),
1321 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, INFO_FL_PRIO | INFO_FL_CTRL),
1322 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001323 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1324 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001325 IOCTL_INFO(VIDIOC_G_ENC_INDEX, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001326 IOCTL_INFO(VIDIOC_ENCODER_CMD, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1327 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001328 IOCTL_INFO(VIDIOC_DECODER_CMD, INFO_FL_PRIO),
1329 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001330 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, 0),
1331 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001332 IOCTL_INFO(VIDIOC_DBG_G_CHIP_IDENT, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001333 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 -03001334 IOCTL_INFO(VIDIOC_ENUM_DV_PRESETS, 0),
1335 IOCTL_INFO(VIDIOC_S_DV_PRESET, INFO_FL_PRIO),
1336 IOCTL_INFO(VIDIOC_G_DV_PRESET, 0),
1337 IOCTL_INFO(VIDIOC_QUERY_DV_PRESET, 0),
1338 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, INFO_FL_PRIO),
1339 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, 0),
1340 IOCTL_INFO(VIDIOC_DQEVENT, 0),
1341 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, 0),
1342 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, 0),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001343 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
1344 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001345 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, 0),
1346 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, 0),
1347 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, 0),
1348};
1349#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1350
1351bool v4l2_is_known_ioctl(unsigned int cmd)
1352{
1353 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
1354 return false;
1355 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
1356}
1357
1358/* Common ioctl debug function. This function can be used by
1359 external ioctl messages as well as internal V4L ioctl */
1360void v4l_printk_ioctl(unsigned int cmd)
1361{
Hans Verkuil86748f32012-06-22 06:04:16 -03001362 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001363
1364 switch (_IOC_TYPE(cmd)) {
1365 case 'd':
1366 type = "v4l2_int";
1367 break;
1368 case 'V':
1369 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
1370 type = "v4l2";
1371 break;
1372 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001373 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001374 return;
1375 default:
1376 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03001377 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001378 }
1379
1380 switch (_IOC_DIR(cmd)) {
1381 case _IOC_NONE: dir = "--"; break;
1382 case _IOC_READ: dir = "r-"; break;
1383 case _IOC_WRITE: dir = "-w"; break;
1384 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
1385 default: dir = "*ERR*"; break;
1386 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001387 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001388 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
1389}
1390EXPORT_SYMBOL(v4l_printk_ioctl);
1391
Hans Verkuil069b7472008-12-30 07:04:34 -03001392static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001393 unsigned int cmd, void *arg)
1394{
1395 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03001396 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03001397 bool write_only = false;
1398 struct v4l2_ioctl_info default_info;
1399 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03001400 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001401 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03001402 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001403 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001404
Hans Verkuil6a717882010-04-06 15:56:08 -03001405 if (ops == NULL) {
1406 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
1407 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001408 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03001409 }
1410
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001411 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1412 vfh = file->private_data;
1413 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001414 }
1415
1416 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03001417 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001418
1419 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
1420 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03001421 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001422
1423 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
1424 ret = v4l2_prio_check(vfd->prio, vfh->prio);
1425 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03001426 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001427 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001428 } else {
1429 default_info.ioctl = cmd;
1430 default_info.flags = 0;
1431 default_info.debug = NULL;
1432 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001433 }
1434
Hans Verkuil86748f32012-06-22 06:04:16 -03001435 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
1436 if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001437 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03001438 pr_cont(": ");
1439 info->debug(arg, write_only);
1440 }
1441 if (info->flags & INFO_FL_STD) {
1442 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
1443 const void *p = vfd->ioctl_ops;
1444 const vidioc_op *vidioc = p + info->offset;
1445
1446 ret = (*vidioc)(file, fh, arg);
1447 goto done;
1448 } else if (info->flags & INFO_FL_FUNC) {
1449 ret = info->func(ops, file, fh, arg);
1450 goto done;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001451 }
1452
Hans Verkuil6a717882010-04-06 15:56:08 -03001453 switch (cmd) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001454 /* --- controls ---------------------------------------------- */
1455 case VIDIOC_QUERYCTRL:
1456 {
1457 struct v4l2_queryctrl *p = arg;
1458
Hans Verkuil2d28b682011-03-12 08:54:43 -03001459 if (vfh && vfh->ctrl_handler)
1460 ret = v4l2_queryctrl(vfh->ctrl_handler, p);
1461 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001462 ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1463 else if (ops->vidioc_queryctrl)
1464 ret = ops->vidioc_queryctrl(file, fh, p);
1465 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001466 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001467 if (!ret)
1468 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1469 "step=%d, default=%d, flags=0x%08x\n",
1470 p->id, p->type, p->name,
1471 p->minimum, p->maximum,
1472 p->step, p->default_value, p->flags);
1473 else
1474 dbgarg(cmd, "id=0x%x\n", p->id);
1475 break;
1476 }
1477 case VIDIOC_G_CTRL:
1478 {
1479 struct v4l2_control *p = arg;
1480
Hans Verkuil2d28b682011-03-12 08:54:43 -03001481 if (vfh && vfh->ctrl_handler)
1482 ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
1483 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001484 ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1485 else if (ops->vidioc_g_ctrl)
Hans Verkuila3998102008-07-21 02:57:38 -03001486 ret = ops->vidioc_g_ctrl(file, fh, p);
1487 else if (ops->vidioc_g_ext_ctrls) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001488 struct v4l2_ext_controls ctrls;
1489 struct v4l2_ext_control ctrl;
1490
1491 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1492 ctrls.count = 1;
1493 ctrls.controls = &ctrl;
1494 ctrl.id = p->id;
1495 ctrl.value = p->value;
1496 if (check_ext_ctrls(&ctrls, 1)) {
Hans Verkuila3998102008-07-21 02:57:38 -03001497 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001498 if (ret == 0)
1499 p->value = ctrl.value;
1500 }
1501 } else
1502 break;
1503 if (!ret)
1504 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1505 else
1506 dbgarg(cmd, "id=0x%x\n", p->id);
1507 break;
1508 }
1509 case VIDIOC_S_CTRL:
1510 {
1511 struct v4l2_control *p = arg;
1512 struct v4l2_ext_controls ctrls;
1513 struct v4l2_ext_control ctrl;
1514
Hans Verkuil2d28b682011-03-12 08:54:43 -03001515 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001516 !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001517 break;
1518
1519 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1520
Hans Verkuil2d28b682011-03-12 08:54:43 -03001521 if (vfh && vfh->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001522 ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001523 break;
1524 }
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001525 if (vfd->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001526 ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001527 break;
1528 }
Hans Verkuila3998102008-07-21 02:57:38 -03001529 if (ops->vidioc_s_ctrl) {
1530 ret = ops->vidioc_s_ctrl(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001531 break;
1532 }
Hans Verkuila3998102008-07-21 02:57:38 -03001533 if (!ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001534 break;
1535
1536 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1537 ctrls.count = 1;
1538 ctrls.controls = &ctrl;
1539 ctrl.id = p->id;
1540 ctrl.value = p->value;
1541 if (check_ext_ctrls(&ctrls, 1))
Hans Verkuila3998102008-07-21 02:57:38 -03001542 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001543 else
1544 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001545 break;
1546 }
1547 case VIDIOC_G_EXT_CTRLS:
1548 {
1549 struct v4l2_ext_controls *p = arg;
1550
1551 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001552 if (vfh && vfh->ctrl_handler)
1553 ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1554 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001555 ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001556 else if (ops->vidioc_g_ext_ctrls)
1557 ret = check_ext_ctrls(p, 0) ?
1558 ops->vidioc_g_ext_ctrls(file, fh, p) :
1559 -EINVAL;
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001560 else
1561 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001562 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1563 break;
1564 }
1565 case VIDIOC_S_EXT_CTRLS:
1566 {
1567 struct v4l2_ext_controls *p = arg;
1568
1569 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001570 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1571 !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001572 break;
1573 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001574 if (vfh && vfh->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001575 ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001576 else if (vfd->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001577 ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001578 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001579 ret = ops->vidioc_s_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001580 else
1581 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001582 break;
1583 }
1584 case VIDIOC_TRY_EXT_CTRLS:
1585 {
1586 struct v4l2_ext_controls *p = arg;
1587
1588 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001589 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1590 !ops->vidioc_try_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001591 break;
1592 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001593 if (vfh && vfh->ctrl_handler)
1594 ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1595 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001596 ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1597 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001598 ret = ops->vidioc_try_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001599 else
1600 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001601 break;
1602 }
1603 case VIDIOC_QUERYMENU:
1604 {
1605 struct v4l2_querymenu *p = arg;
1606
Hans Verkuil2d28b682011-03-12 08:54:43 -03001607 if (vfh && vfh->ctrl_handler)
1608 ret = v4l2_querymenu(vfh->ctrl_handler, p);
1609 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001610 ret = v4l2_querymenu(vfd->ctrl_handler, p);
1611 else if (ops->vidioc_querymenu)
1612 ret = ops->vidioc_querymenu(file, fh, p);
1613 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001614 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001615 if (!ret)
1616 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1617 p->id, p->index, p->name);
1618 else
1619 dbgarg(cmd, "id=0x%x, index=%d\n",
1620 p->id, p->index);
1621 break;
1622 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001623 case VIDIOC_G_CROP:
1624 {
1625 struct v4l2_crop *p = arg;
1626
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001627 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001628
1629 if (ops->vidioc_g_crop) {
1630 ret = ops->vidioc_g_crop(file, fh, p);
1631 } else {
1632 /* simulate capture crop using selection api */
1633 struct v4l2_selection s = {
1634 .type = p->type,
1635 };
1636
1637 /* crop means compose for output devices */
1638 if (V4L2_TYPE_IS_OUTPUT(p->type))
1639 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1640 else
1641 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1642
1643 ret = ops->vidioc_g_selection(file, fh, &s);
1644
1645 /* copying results to old structure on success */
1646 if (!ret)
1647 p->c = s.r;
1648 }
1649
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001650 if (!ret)
1651 dbgrect(vfd, "", &p->c);
1652 break;
1653 }
1654 case VIDIOC_S_CROP:
1655 {
1656 struct v4l2_crop *p = arg;
1657
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001658 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1659 dbgrect(vfd, "", &p->c);
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001660
1661 if (ops->vidioc_s_crop) {
1662 ret = ops->vidioc_s_crop(file, fh, p);
1663 } else {
1664 /* simulate capture crop using selection api */
1665 struct v4l2_selection s = {
1666 .type = p->type,
1667 .r = p->c,
1668 };
1669
1670 /* crop means compose for output devices */
1671 if (V4L2_TYPE_IS_OUTPUT(p->type))
1672 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1673 else
1674 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1675
1676 ret = ops->vidioc_s_selection(file, fh, &s);
1677 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001678 break;
1679 }
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001680 case VIDIOC_G_SELECTION:
1681 {
1682 struct v4l2_selection *p = arg;
1683
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001684 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1685
1686 ret = ops->vidioc_g_selection(file, fh, p);
1687 if (!ret)
1688 dbgrect(vfd, "", &p->r);
1689 break;
1690 }
1691 case VIDIOC_S_SELECTION:
1692 {
1693 struct v4l2_selection *p = arg;
1694
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001695
1696 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1697 dbgrect(vfd, "", &p->r);
1698
1699 ret = ops->vidioc_s_selection(file, fh, p);
1700 break;
1701 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001702 case VIDIOC_CROPCAP:
1703 {
1704 struct v4l2_cropcap *p = arg;
1705
1706 /*FIXME: Should also show v4l2_fract pixelaspect */
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001707 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001708 if (ops->vidioc_cropcap) {
1709 ret = ops->vidioc_cropcap(file, fh, p);
1710 } else {
1711 struct v4l2_selection s = { .type = p->type };
1712
1713 /* obtaining bounds */
1714 if (V4L2_TYPE_IS_OUTPUT(p->type))
1715 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1716 else
1717 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1718
1719 ret = ops->vidioc_g_selection(file, fh, &s);
1720 if (ret)
1721 break;
1722 p->bounds = s.r;
1723
1724 /* obtaining defrect */
1725 if (V4L2_TYPE_IS_OUTPUT(p->type))
1726 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1727 else
1728 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1729
1730 ret = ops->vidioc_g_selection(file, fh, &s);
1731 if (ret)
1732 break;
1733 p->defrect = s.r;
1734
1735 /* setting trivial pixelaspect */
1736 p->pixelaspect.numerator = 1;
1737 p->pixelaspect.denominator = 1;
1738 }
1739
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001740 if (!ret) {
1741 dbgrect(vfd, "bounds ", &p->bounds);
1742 dbgrect(vfd, "defrect ", &p->defrect);
1743 }
1744 break;
1745 }
1746 case VIDIOC_G_JPEGCOMP:
1747 {
1748 struct v4l2_jpegcompression *p = arg;
1749
Hans Verkuila3998102008-07-21 02:57:38 -03001750 ret = ops->vidioc_g_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001751 if (!ret)
1752 dbgarg(cmd, "quality=%d, APPn=%d, "
1753 "APP_len=%d, COM_len=%d, "
1754 "jpeg_markers=%d\n",
1755 p->quality, p->APPn, p->APP_len,
1756 p->COM_len, p->jpeg_markers);
1757 break;
1758 }
1759 case VIDIOC_S_JPEGCOMP:
1760 {
1761 struct v4l2_jpegcompression *p = arg;
1762
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001763 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1764 "COM_len=%d, jpeg_markers=%d\n",
1765 p->quality, p->APPn, p->APP_len,
1766 p->COM_len, p->jpeg_markers);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001767 ret = ops->vidioc_s_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001768 break;
1769 }
1770 case VIDIOC_G_ENC_INDEX:
1771 {
1772 struct v4l2_enc_idx *p = arg;
1773
Hans Verkuila3998102008-07-21 02:57:38 -03001774 ret = ops->vidioc_g_enc_index(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001775 if (!ret)
1776 dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1777 p->entries, p->entries_cap);
1778 break;
1779 }
1780 case VIDIOC_ENCODER_CMD:
1781 {
1782 struct v4l2_encoder_cmd *p = arg;
1783
Hans Verkuila3998102008-07-21 02:57:38 -03001784 ret = ops->vidioc_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001785 if (!ret)
1786 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1787 break;
1788 }
1789 case VIDIOC_TRY_ENCODER_CMD:
1790 {
1791 struct v4l2_encoder_cmd *p = arg;
1792
Hans Verkuila3998102008-07-21 02:57:38 -03001793 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001794 if (!ret)
1795 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1796 break;
1797 }
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001798 case VIDIOC_DECODER_CMD:
1799 {
1800 struct v4l2_decoder_cmd *p = arg;
1801
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001802 ret = ops->vidioc_decoder_cmd(file, fh, p);
1803 if (!ret)
1804 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1805 break;
1806 }
1807 case VIDIOC_TRY_DECODER_CMD:
1808 {
1809 struct v4l2_decoder_cmd *p = arg;
1810
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001811 ret = ops->vidioc_try_decoder_cmd(file, fh, p);
1812 if (!ret)
1813 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1814 break;
1815 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001816 case VIDIOC_G_SLICED_VBI_CAP:
1817 {
1818 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001819
Trent Piepho19c96e42009-03-04 01:21:02 -03001820 /* Clear up to type, everything after type is zerod already */
1821 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1822
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001823 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001824 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001825 if (!ret)
1826 dbgarg2("service_set=%d\n", p->service_set);
1827 break;
1828 }
1829 case VIDIOC_LOG_STATUS:
1830 {
Hans Verkuile2ecb252012-02-02 08:20:53 -03001831 if (vfd->v4l2_dev)
1832 pr_info("%s: ================= START STATUS =================\n",
1833 vfd->v4l2_dev->name);
Hans Verkuila3998102008-07-21 02:57:38 -03001834 ret = ops->vidioc_log_status(file, fh);
Hans Verkuile2ecb252012-02-02 08:20:53 -03001835 if (vfd->v4l2_dev)
1836 pr_info("%s: ================== END STATUS ==================\n",
1837 vfd->v4l2_dev->name);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001838 break;
1839 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001840 case VIDIOC_DBG_G_REGISTER:
1841 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001842#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001843 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001844
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001845 if (!capable(CAP_SYS_ADMIN))
1846 ret = -EPERM;
1847 else
1848 ret = ops->vidioc_g_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001849#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001850 break;
1851 }
1852 case VIDIOC_DBG_S_REGISTER:
1853 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001854#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001855 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001856
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001857 if (!capable(CAP_SYS_ADMIN))
1858 ret = -EPERM;
1859 else
1860 ret = ops->vidioc_s_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001861#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001862 break;
1863 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001864 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001865 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001866 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001867
Hans Verkuil80b36e02009-02-07 11:00:02 -03001868 p->ident = V4L2_IDENT_NONE;
1869 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001870 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001871 if (!ret)
1872 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1873 break;
1874 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001875 case VIDIOC_ENUM_FRAMESIZES:
1876 {
1877 struct v4l2_frmsizeenum *p = arg;
1878
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001879 ret = ops->vidioc_enum_framesizes(file, fh, p);
1880 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03001881 "index=%d, pixelformat=%c%c%c%c, type=%d ",
1882 p->index,
1883 (p->pixel_format & 0xff),
1884 (p->pixel_format >> 8) & 0xff,
1885 (p->pixel_format >> 16) & 0xff,
1886 (p->pixel_format >> 24) & 0xff,
1887 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001888 switch (p->type) {
1889 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001890 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001891 p->discrete.width, p->discrete.height);
1892 break;
1893 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001894 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001895 p->stepwise.min_width, p->stepwise.min_height,
1896 p->stepwise.step_width, p->stepwise.step_height,
1897 p->stepwise.max_width, p->stepwise.max_height);
1898 break;
1899 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001900 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001901 break;
1902 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001903 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001904 }
1905
1906 break;
1907 }
1908 case VIDIOC_ENUM_FRAMEINTERVALS:
1909 {
1910 struct v4l2_frmivalenum *p = arg;
1911
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001912 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1913 dbgarg(cmd,
1914 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1915 p->index, p->pixel_format,
1916 p->width, p->height, p->type);
1917 switch (p->type) {
1918 case V4L2_FRMIVAL_TYPE_DISCRETE:
1919 dbgarg2("fps=%d/%d\n",
1920 p->discrete.numerator,
1921 p->discrete.denominator);
1922 break;
1923 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03001924 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1925 p->stepwise.min.numerator,
1926 p->stepwise.min.denominator,
1927 p->stepwise.max.numerator,
1928 p->stepwise.max.denominator,
1929 p->stepwise.step.numerator,
1930 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001931 break;
1932 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1933 dbgarg2("continuous\n");
1934 break;
1935 default:
1936 dbgarg2("- Unknown type!\n");
1937 }
1938 break;
1939 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001940 case VIDIOC_ENUM_DV_PRESETS:
1941 {
1942 struct v4l2_dv_enum_preset *p = arg;
1943
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001944 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1945 if (!ret)
1946 dbgarg(cmd,
1947 "index=%d, preset=%d, name=%s, width=%d,"
1948 " height=%d ",
1949 p->index, p->preset, p->name, p->width,
1950 p->height);
1951 break;
1952 }
1953 case VIDIOC_S_DV_PRESET:
1954 {
1955 struct v4l2_dv_preset *p = arg;
1956
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001957 dbgarg(cmd, "preset=%d\n", p->preset);
1958 ret = ops->vidioc_s_dv_preset(file, fh, p);
1959 break;
1960 }
1961 case VIDIOC_G_DV_PRESET:
1962 {
1963 struct v4l2_dv_preset *p = arg;
1964
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001965 ret = ops->vidioc_g_dv_preset(file, fh, p);
1966 if (!ret)
1967 dbgarg(cmd, "preset=%d\n", p->preset);
1968 break;
1969 }
1970 case VIDIOC_QUERY_DV_PRESET:
1971 {
1972 struct v4l2_dv_preset *p = arg;
1973
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001974 ret = ops->vidioc_query_dv_preset(file, fh, p);
1975 if (!ret)
1976 dbgarg(cmd, "preset=%d\n", p->preset);
1977 break;
1978 }
1979 case VIDIOC_S_DV_TIMINGS:
1980 {
1981 struct v4l2_dv_timings *p = arg;
1982
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001983 dbgtimings(vfd, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001984 switch (p->type) {
1985 case V4L2_DV_BT_656_1120:
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001986 ret = ops->vidioc_s_dv_timings(file, fh, p);
1987 break;
1988 default:
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001989 ret = -EINVAL;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001990 break;
1991 }
1992 break;
1993 }
1994 case VIDIOC_G_DV_TIMINGS:
1995 {
1996 struct v4l2_dv_timings *p = arg;
1997
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001998 ret = ops->vidioc_g_dv_timings(file, fh, p);
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001999 if (!ret)
2000 dbgtimings(vfd, p);
2001 break;
2002 }
2003 case VIDIOC_ENUM_DV_TIMINGS:
2004 {
2005 struct v4l2_enum_dv_timings *p = arg;
2006
2007 if (!ops->vidioc_enum_dv_timings)
2008 break;
2009
2010 ret = ops->vidioc_enum_dv_timings(file, fh, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002011 if (!ret) {
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002012 dbgarg(cmd, "index=%d: ", p->index);
2013 dbgtimings(vfd, &p->timings);
2014 }
2015 break;
2016 }
2017 case VIDIOC_QUERY_DV_TIMINGS:
2018 {
2019 struct v4l2_dv_timings *p = arg;
2020
2021 if (!ops->vidioc_query_dv_timings)
2022 break;
2023
2024 ret = ops->vidioc_query_dv_timings(file, fh, p);
2025 if (!ret)
2026 dbgtimings(vfd, p);
2027 break;
2028 }
2029 case VIDIOC_DV_TIMINGS_CAP:
2030 {
2031 struct v4l2_dv_timings_cap *p = arg;
2032
2033 if (!ops->vidioc_dv_timings_cap)
2034 break;
2035
2036 ret = ops->vidioc_dv_timings_cap(file, fh, p);
2037 if (ret)
2038 break;
2039 switch (p->type) {
2040 case V4L2_DV_BT_656_1120:
2041 dbgarg(cmd,
2042 "type=%d, width=%u-%u, height=%u-%u, "
2043 "pixelclock=%llu-%llu, standards=%x, capabilities=%x ",
2044 p->type,
2045 p->bt.min_width, p->bt.max_width,
2046 p->bt.min_height, p->bt.max_height,
2047 p->bt.min_pixelclock, p->bt.max_pixelclock,
2048 p->bt.standards, p->bt.capabilities);
2049 break;
2050 default:
2051 dbgarg(cmd, "unknown type ");
2052 break;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002053 }
2054 break;
2055 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002056 case VIDIOC_DQEVENT:
2057 {
2058 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002059
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002060 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2061 if (ret < 0) {
2062 dbgarg(cmd, "no pending events?");
2063 break;
2064 }
2065 dbgarg(cmd,
2066 "pending=%d, type=0x%8.8x, sequence=%d, "
2067 "timestamp=%lu.%9.9lu ",
2068 ev->pending, ev->type, ev->sequence,
2069 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2070 break;
2071 }
2072 case VIDIOC_SUBSCRIBE_EVENT:
2073 {
2074 struct v4l2_event_subscription *sub = arg;
2075
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002076 ret = ops->vidioc_subscribe_event(fh, sub);
2077 if (ret < 0) {
2078 dbgarg(cmd, "failed, ret=%ld", ret);
2079 break;
2080 }
2081 dbgarg(cmd, "type=0x%8.8x", sub->type);
2082 break;
2083 }
2084 case VIDIOC_UNSUBSCRIBE_EVENT:
2085 {
2086 struct v4l2_event_subscription *sub = arg;
2087
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002088 ret = ops->vidioc_unsubscribe_event(fh, sub);
2089 if (ret < 0) {
2090 dbgarg(cmd, "failed, ret=%ld", ret);
2091 break;
2092 }
2093 dbgarg(cmd, "type=0x%8.8x", sub->type);
2094 break;
2095 }
Hans Verkuila3998102008-07-21 02:57:38 -03002096 default:
Hans Verkuila3998102008-07-21 02:57:38 -03002097 if (!ops->vidioc_default)
2098 break;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002099 ret = ops->vidioc_default(file, fh, use_fh_prio ?
2100 v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2101 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002102 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002103 } /* switch */
2104
Hans Verkuil86748f32012-06-22 06:04:16 -03002105done:
2106 if (vfd->debug) {
2107 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2108 if (ret < 0)
2109 printk(KERN_DEBUG "%s: error %ld\n",
2110 video_device_node_name(vfd), ret);
2111 return ret;
2112 }
2113 v4l_print_ioctl(vfd->name, cmd);
2114 if (ret < 0)
2115 pr_cont(": error %ld\n", ret);
2116 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2117 pr_cont("\n");
2118 else if (!info->debug)
2119 return ret;
2120 else if (_IOC_DIR(cmd) == _IOC_NONE)
2121 info->debug(arg, write_only);
2122 else {
2123 pr_cont(": ");
2124 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002125 }
2126 }
2127
2128 return ret;
2129}
2130
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002131static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2132 void * __user *user_ptr, void ***kernel_ptr)
2133{
2134 int ret = 0;
2135
2136 switch (cmd) {
2137 case VIDIOC_QUERYBUF:
2138 case VIDIOC_QBUF:
2139 case VIDIOC_DQBUF: {
2140 struct v4l2_buffer *buf = parg;
2141
2142 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2143 if (buf->length > VIDEO_MAX_PLANES) {
2144 ret = -EINVAL;
2145 break;
2146 }
2147 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002148 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002149 *array_size = sizeof(struct v4l2_plane) * buf->length;
2150 ret = 1;
2151 }
2152 break;
2153 }
2154
2155 case VIDIOC_S_EXT_CTRLS:
2156 case VIDIOC_G_EXT_CTRLS:
2157 case VIDIOC_TRY_EXT_CTRLS: {
2158 struct v4l2_ext_controls *ctrls = parg;
2159
2160 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002161 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2162 ret = -EINVAL;
2163 break;
2164 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002165 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002166 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002167 *array_size = sizeof(struct v4l2_ext_control)
2168 * ctrls->count;
2169 ret = 1;
2170 }
2171 break;
2172 }
2173 }
2174
2175 return ret;
2176}
2177
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002178long
2179video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2180 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002181{
2182 char sbuf[128];
2183 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002184 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002185 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002186 bool has_array_args;
2187 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002188 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002189 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002190
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002191 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002192 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002193 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2194 parg = sbuf;
2195 } else {
2196 /* too big to allocate from stack */
2197 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2198 if (NULL == mbuf)
2199 return -ENOMEM;
2200 parg = mbuf;
2201 }
2202
2203 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002204 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002205 unsigned int n = _IOC_SIZE(cmd);
2206
2207 /*
2208 * In some cases, only a few fields are used as input,
2209 * i.e. when the app sets "index" and then the driver
2210 * fills in the rest of the structure for the thing
2211 * with that index. We only need to copy up the first
2212 * non-input field.
2213 */
2214 if (v4l2_is_known_ioctl(cmd)) {
2215 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2216 if (flags & INFO_FL_CLEAR_MASK)
2217 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2218 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002219
2220 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002221 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002222
2223 /* zero out anything we don't copy from userspace */
2224 if (n < _IOC_SIZE(cmd))
2225 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002226 } else {
2227 /* read-only ioctl */
2228 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002229 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002230 }
2231
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002232 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2233 if (err < 0)
2234 goto out;
2235 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002236
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002237 if (has_array_args) {
2238 /*
2239 * When adding new types of array args, make sure that the
2240 * parent argument to ioctl (which contains the pointer to the
2241 * array) fits into sbuf (so that mbuf will still remain
2242 * unused up to here).
2243 */
2244 mbuf = kmalloc(array_size, GFP_KERNEL);
2245 err = -ENOMEM;
2246 if (NULL == mbuf)
2247 goto out_array_args;
2248 err = -EFAULT;
2249 if (copy_from_user(mbuf, user_ptr, array_size))
2250 goto out_array_args;
2251 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002252 }
2253
2254 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002255 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002256 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002257 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002258
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002259 if (has_array_args) {
2260 *kernel_ptr = user_ptr;
2261 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002262 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002263 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002264 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002265 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2266 results that must be returned. */
2267 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002268 goto out;
2269
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002270out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002271 /* Copy results into user buffer */
2272 switch (_IOC_DIR(cmd)) {
2273 case _IOC_READ:
2274 case (_IOC_WRITE | _IOC_READ):
2275 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2276 err = -EFAULT;
2277 break;
2278 }
2279
2280out:
2281 kfree(mbuf);
2282 return err;
2283}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002284EXPORT_SYMBOL(video_usercopy);
2285
2286long video_ioctl2(struct file *file,
2287 unsigned int cmd, unsigned long arg)
2288{
2289 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2290}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002291EXPORT_SYMBOL(video_ioctl2);