blob: 935fcbc2d50b6cb6db6ca00adabd692b643f8fae [file] [log] [blame]
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001/*
2 * Video capture interface for Linux version 2
3 *
4 * A generic framework to process V4L2 ioctl commands.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
Alan Coxd9b01442008-10-27 15:13:47 -030011 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil35ea11f2008-07-20 08:12:02 -030012 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030017#include <linux/types.h>
18#include <linux/kernel.h>
Mauro Carvalho Chehabae6db512011-06-24 13:14:14 -030019#include <linux/version.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030020
Hans Verkuil35ea11f2008-07-20 08:12:02 -030021#include <linux/videodev2.h>
22
Hans Verkuil35ea11f2008-07-20 08:12:02 -030023#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030025#include <media/v4l2-ctrls.h>
Sakari Ailusd3d7c962010-03-27 11:02:10 -030026#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
Hans Verkuil99cd47bc2011-03-11 19:00:56 -030028#include <media/v4l2-device.h>
Hans Verkuil80b36e02009-02-07 11:00:02 -030029#include <media/v4l2-chip-ident.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030
31#define dbgarg(cmd, fmt, arg...) \
32 do { \
33 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
34 printk(KERN_DEBUG "%s: ", vfd->name); \
35 v4l_printk_ioctl(cmd); \
36 printk(" " fmt, ## arg); \
37 } \
38 } while (0)
39
40#define dbgarg2(fmt, arg...) \
41 do { \
42 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
43 printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
44 } while (0)
45
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -030046#define dbgarg3(fmt, arg...) \
47 do { \
48 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
49 printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
50 } while (0)
51
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030053 * not including, the specified field is cleared. */
54#define CLEAR_AFTER_FIELD(p, field) \
55 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
56 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
57
Hans Verkuil35ea11f2008-07-20 08:12:02 -030058struct std_descr {
59 v4l2_std_id std;
60 const char *descr;
61};
62
63static const struct std_descr standards[] = {
64 { V4L2_STD_NTSC, "NTSC" },
65 { V4L2_STD_NTSC_M, "NTSC-M" },
66 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
67 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
68 { V4L2_STD_NTSC_443, "NTSC-443" },
69 { V4L2_STD_PAL, "PAL" },
70 { V4L2_STD_PAL_BG, "PAL-BG" },
71 { V4L2_STD_PAL_B, "PAL-B" },
72 { V4L2_STD_PAL_B1, "PAL-B1" },
73 { V4L2_STD_PAL_G, "PAL-G" },
74 { V4L2_STD_PAL_H, "PAL-H" },
75 { V4L2_STD_PAL_I, "PAL-I" },
76 { V4L2_STD_PAL_DK, "PAL-DK" },
77 { V4L2_STD_PAL_D, "PAL-D" },
78 { V4L2_STD_PAL_D1, "PAL-D1" },
79 { V4L2_STD_PAL_K, "PAL-K" },
80 { V4L2_STD_PAL_M, "PAL-M" },
81 { V4L2_STD_PAL_N, "PAL-N" },
82 { V4L2_STD_PAL_Nc, "PAL-Nc" },
83 { V4L2_STD_PAL_60, "PAL-60" },
84 { V4L2_STD_SECAM, "SECAM" },
85 { V4L2_STD_SECAM_B, "SECAM-B" },
86 { V4L2_STD_SECAM_G, "SECAM-G" },
87 { V4L2_STD_SECAM_H, "SECAM-H" },
88 { V4L2_STD_SECAM_DK, "SECAM-DK" },
89 { V4L2_STD_SECAM_D, "SECAM-D" },
90 { V4L2_STD_SECAM_K, "SECAM-K" },
91 { V4L2_STD_SECAM_K1, "SECAM-K1" },
92 { V4L2_STD_SECAM_L, "SECAM-L" },
93 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
94 { 0, "Unknown" }
95};
96
97/* video4linux standard ID conversion to standard name
98 */
99const char *v4l2_norm_to_name(v4l2_std_id id)
100{
101 u32 myid = id;
102 int i;
103
104 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
105 64 bit comparations. So, on that architecture, with some gcc
106 variants, compilation fails. Currently, the max value is 30bit wide.
107 */
108 BUG_ON(myid != id);
109
110 for (i = 0; standards[i].std; i++)
111 if (myid == standards[i].std)
112 break;
113 return standards[i].descr;
114}
115EXPORT_SYMBOL(v4l2_norm_to_name);
116
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300117/* Returns frame period for the given standard */
118void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
119{
120 if (id & V4L2_STD_525_60) {
121 frameperiod->numerator = 1001;
122 frameperiod->denominator = 30000;
123 } else {
124 frameperiod->numerator = 1;
125 frameperiod->denominator = 25;
126 }
127}
128EXPORT_SYMBOL(v4l2_video_std_frame_period);
129
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300130/* Fill in the fields of a v4l2_standard structure according to the
131 'id' and 'transmission' parameters. Returns negative on error. */
132int v4l2_video_std_construct(struct v4l2_standard *vs,
133 int id, const char *name)
134{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300135 vs->id = id;
136 v4l2_video_std_frame_period(id, &vs->frameperiod);
137 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300138 strlcpy(vs->name, name, sizeof(vs->name));
139 return 0;
140}
141EXPORT_SYMBOL(v4l2_video_std_construct);
142
143/* ----------------------------------------------------------------- */
144/* some arrays for pretty-printing debug messages of enum types */
145
146const char *v4l2_field_names[] = {
147 [V4L2_FIELD_ANY] = "any",
148 [V4L2_FIELD_NONE] = "none",
149 [V4L2_FIELD_TOP] = "top",
150 [V4L2_FIELD_BOTTOM] = "bottom",
151 [V4L2_FIELD_INTERLACED] = "interlaced",
152 [V4L2_FIELD_SEQ_TB] = "seq-tb",
153 [V4L2_FIELD_SEQ_BT] = "seq-bt",
154 [V4L2_FIELD_ALTERNATE] = "alternate",
155 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
156 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
157};
158EXPORT_SYMBOL(v4l2_field_names);
159
160const char *v4l2_type_names[] = {
161 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
162 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
163 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
164 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
165 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
166 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
167 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
168 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300169 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
170 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300171};
172EXPORT_SYMBOL(v4l2_type_names);
173
174static const char *v4l2_memory_names[] = {
175 [V4L2_MEMORY_MMAP] = "mmap",
176 [V4L2_MEMORY_USERPTR] = "userptr",
177 [V4L2_MEMORY_OVERLAY] = "overlay",
178};
179
180#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
181 arr[a] : "unknown")
182
183/* ------------------------------------------------------------------ */
184/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300185
Hans Verkuil59076122012-06-22 06:09:54 -0300186static void v4l_print_querycap(const void *arg, bool write_only)
187{
188 const struct v4l2_capability *p = arg;
189
190 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
191 "capabilities=0x%08x, device_caps=0x%08x\n",
192 p->driver, p->card, p->bus_info,
193 p->version, p->capabilities, p->device_caps);
194}
195
196static void v4l_print_enuminput(const void *arg, bool write_only)
197{
198 const struct v4l2_input *p = arg;
199
200 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
201 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
202 p->index, p->name, p->type, p->audioset, p->tuner,
203 (unsigned long long)p->std, p->status, p->capabilities);
204}
205
206static void v4l_print_enumoutput(const void *arg, bool write_only)
207{
208 const struct v4l2_output *p = arg;
209
210 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
211 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
212 p->index, p->name, p->type, p->audioset, p->modulator,
213 (unsigned long long)p->std, p->capabilities);
214}
215
216static void v4l_print_audio(const void *arg, bool write_only)
217{
218 const struct v4l2_audio *p = arg;
219
220 if (write_only)
221 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
222 else
223 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
224 p->index, p->name, p->capability, p->mode);
225}
226
227static void v4l_print_audioout(const void *arg, bool write_only)
228{
229 const struct v4l2_audioout *p = arg;
230
231 if (write_only)
232 pr_cont("index=%u\n", p->index);
233 else
234 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
235 p->index, p->name, p->capability, p->mode);
236}
237
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300238static void v4l_print_fmtdesc(const void *arg, bool write_only)
239{
240 const struct v4l2_fmtdesc *p = arg;
241
242 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
243 p->index, prt_names(p->type, v4l2_type_names),
244 p->flags, (p->pixelformat & 0xff),
245 (p->pixelformat >> 8) & 0xff,
246 (p->pixelformat >> 16) & 0xff,
247 (p->pixelformat >> 24) & 0xff,
248 p->description);
249}
250
251static void v4l_print_format(const void *arg, bool write_only)
252{
253 const struct v4l2_format *p = arg;
254 const struct v4l2_pix_format *pix;
255 const struct v4l2_pix_format_mplane *mp;
256 const struct v4l2_vbi_format *vbi;
257 const struct v4l2_sliced_vbi_format *sliced;
258 const struct v4l2_window *win;
259 const struct v4l2_clip *clip;
260 unsigned i;
261
262 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
263 switch (p->type) {
264 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
265 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
266 pix = &p->fmt.pix;
267 pr_cont(", width=%u, height=%u, "
268 "pixelformat=%c%c%c%c, field=%s, "
269 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
270 pix->width, pix->height,
271 (pix->pixelformat & 0xff),
272 (pix->pixelformat >> 8) & 0xff,
273 (pix->pixelformat >> 16) & 0xff,
274 (pix->pixelformat >> 24) & 0xff,
275 prt_names(pix->field, v4l2_field_names),
276 pix->bytesperline, pix->sizeimage,
277 pix->colorspace);
278 break;
279 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
280 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
281 mp = &p->fmt.pix_mp;
282 pr_cont(", width=%u, height=%u, "
283 "format=%c%c%c%c, field=%s, "
284 "colorspace=%d, num_planes=%u\n",
285 mp->width, mp->height,
286 (mp->pixelformat & 0xff),
287 (mp->pixelformat >> 8) & 0xff,
288 (mp->pixelformat >> 16) & 0xff,
289 (mp->pixelformat >> 24) & 0xff,
290 prt_names(mp->field, v4l2_field_names),
291 mp->colorspace, mp->num_planes);
292 for (i = 0; i < mp->num_planes; i++)
293 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
294 mp->plane_fmt[i].bytesperline,
295 mp->plane_fmt[i].sizeimage);
296 break;
297 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
298 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
299 win = &p->fmt.win;
300 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
301 "chromakey=0x%08x, bitmap=%p, "
302 "global_alpha=0x%02x\n",
303 win->w.width, win->w.height,
304 win->w.left, win->w.top,
305 prt_names(win->field, v4l2_field_names),
306 win->chromakey, win->bitmap, win->global_alpha);
307 clip = win->clips;
308 for (i = 0; i < win->clipcount; i++) {
309 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
310 i, clip->c.width, clip->c.height,
311 clip->c.left, clip->c.top);
312 clip = clip->next;
313 }
314 break;
315 case V4L2_BUF_TYPE_VBI_CAPTURE:
316 case V4L2_BUF_TYPE_VBI_OUTPUT:
317 vbi = &p->fmt.vbi;
318 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
319 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
320 vbi->sampling_rate, vbi->offset,
321 vbi->samples_per_line,
322 (vbi->sample_format & 0xff),
323 (vbi->sample_format >> 8) & 0xff,
324 (vbi->sample_format >> 16) & 0xff,
325 (vbi->sample_format >> 24) & 0xff,
326 vbi->start[0], vbi->start[1],
327 vbi->count[0], vbi->count[1]);
328 break;
329 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
330 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
331 sliced = &p->fmt.sliced;
332 pr_cont(", service_set=0x%08x, io_size=%d\n",
333 sliced->service_set, sliced->io_size);
334 for (i = 0; i < 24; i++)
335 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
336 sliced->service_lines[0][i],
337 sliced->service_lines[1][i]);
338 break;
339 case V4L2_BUF_TYPE_PRIVATE:
340 pr_cont("\n");
341 break;
342 }
343}
344
345static void v4l_print_framebuffer(const void *arg, bool write_only)
346{
347 const struct v4l2_framebuffer *p = arg;
348
349 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
350 "height=%u, pixelformat=%c%c%c%c, "
351 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
352 p->capability, p->flags, p->base,
353 p->fmt.width, p->fmt.height,
354 (p->fmt.pixelformat & 0xff),
355 (p->fmt.pixelformat >> 8) & 0xff,
356 (p->fmt.pixelformat >> 16) & 0xff,
357 (p->fmt.pixelformat >> 24) & 0xff,
358 p->fmt.bytesperline, p->fmt.sizeimage,
359 p->fmt.colorspace);
360}
361
Hans Verkuile325b242012-06-09 12:50:15 -0300362static void v4l_print_buftype(const void *arg, bool write_only)
363{
364 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
365}
366
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300367static void v4l_print_modulator(const void *arg, bool write_only)
368{
369 const struct v4l2_modulator *p = arg;
370
371 if (write_only)
372 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
373 else
374 pr_cont("index=%u, name=%s, capability=0x%x, "
375 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
376 p->index, p->name, p->capability,
377 p->rangelow, p->rangehigh, p->txsubchans);
378}
379
380static void v4l_print_tuner(const void *arg, bool write_only)
381{
382 const struct v4l2_tuner *p = arg;
383
384 if (write_only)
385 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
386 else
387 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
388 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
389 "rxsubchans=0x%x, audmode=%u\n",
390 p->index, p->name, p->type,
391 p->capability, p->rangelow,
392 p->rangehigh, p->signal, p->afc,
393 p->rxsubchans, p->audmode);
394}
395
396static void v4l_print_frequency(const void *arg, bool write_only)
397{
398 const struct v4l2_frequency *p = arg;
399
400 pr_cont("tuner=%u, type=%u, frequency=%u\n",
401 p->tuner, p->type, p->frequency);
402}
403
404static void v4l_print_standard(const void *arg, bool write_only)
405{
406 const struct v4l2_standard *p = arg;
407
408 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
409 "framelines=%u\n", p->index,
410 (unsigned long long)p->id, p->name,
411 p->frameperiod.numerator,
412 p->frameperiod.denominator,
413 p->framelines);
414}
415
416static void v4l_print_std(const void *arg, bool write_only)
417{
418 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
419}
420
421static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
422{
423 const struct v4l2_hw_freq_seek *p = arg;
424
425 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
426 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
427}
428
Hans Verkuileb3728e2012-06-22 06:23:59 -0300429static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300430{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300431 const struct v4l2_requestbuffers *p = arg;
432
433 pr_cont("count=%d, type=%s, memory=%s\n",
434 p->count,
435 prt_names(p->type, v4l2_type_names),
436 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300437}
438
Hans Verkuileb3728e2012-06-22 06:23:59 -0300439static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300440{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300441 const struct v4l2_buffer *p = arg;
442 const struct v4l2_timecode *tc = &p->timecode;
443 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300444 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300445
Hans Verkuileb3728e2012-06-22 06:23:59 -0300446 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
447 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300448 p->timestamp.tv_sec / 3600,
449 (int)(p->timestamp.tv_sec / 60) % 60,
450 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300451 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300452 p->index,
453 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300454 p->flags, prt_names(p->field, v4l2_field_names),
455 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300456
457 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300458 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300459 for (i = 0; i < p->length; ++i) {
460 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300461 printk(KERN_DEBUG
462 "plane %d: bytesused=%d, data_offset=0x%08x "
463 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300464 i, plane->bytesused, plane->data_offset,
465 plane->m.userptr, plane->length);
466 }
467 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300468 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300469 p->bytesused, p->m.userptr, p->length);
470 }
471
Hans Verkuileb3728e2012-06-22 06:23:59 -0300472 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
473 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300474 tc->hours, tc->minutes, tc->seconds,
475 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
476}
477
Hans Verkuileb3728e2012-06-22 06:23:59 -0300478static void v4l_print_create_buffers(const void *arg, bool write_only)
479{
480 const struct v4l2_create_buffers *p = arg;
481
482 pr_cont("index=%d, count=%d, memory=%s, ",
483 p->index, p->count,
484 prt_names(p->memory, v4l2_memory_names));
485 v4l_print_format(&p->format, write_only);
486}
487
488static void v4l_print_streamparm(const void *arg, bool write_only)
489{
490 const struct v4l2_streamparm *p = arg;
491
492 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
493
494 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
495 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
496 const struct v4l2_captureparm *c = &p->parm.capture;
497
498 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
499 "extendedmode=%d, readbuffers=%d\n",
500 c->capability, c->capturemode,
501 c->timeperframe.numerator, c->timeperframe.denominator,
502 c->extendedmode, c->readbuffers);
503 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
504 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
505 const struct v4l2_outputparm *c = &p->parm.output;
506
507 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
508 "extendedmode=%d, writebuffers=%d\n",
509 c->capability, c->outputmode,
510 c->timeperframe.numerator, c->timeperframe.denominator,
511 c->extendedmode, c->writebuffers);
512 }
513}
514
Hans Verkuilefbceec2012-06-09 12:54:02 -0300515static void v4l_print_queryctrl(const void *arg, bool write_only)
516{
517 const struct v4l2_queryctrl *p = arg;
518
519 pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
520 "step=%d, default=%d, flags=0x%08x\n",
521 p->id, p->type, p->name,
522 p->minimum, p->maximum,
523 p->step, p->default_value, p->flags);
524}
525
526static void v4l_print_querymenu(const void *arg, bool write_only)
527{
528 const struct v4l2_querymenu *p = arg;
529
530 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
531}
532
533static void v4l_print_control(const void *arg, bool write_only)
534{
535 const struct v4l2_control *p = arg;
536
537 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
538}
539
540static void v4l_print_ext_controls(const void *arg, bool write_only)
541{
542 const struct v4l2_ext_controls *p = arg;
543 int i;
544
545 pr_cont("class=0x%x, count=%d, error_idx=%d",
546 p->ctrl_class, p->count, p->error_idx);
547 for (i = 0; i < p->count; i++) {
548 if (p->controls[i].size)
549 pr_cont(", id/val=0x%x/0x%x",
550 p->controls[i].id, p->controls[i].value);
551 else
552 pr_cont(", id/size=0x%x/%u",
553 p->controls[i].id, p->controls[i].size);
554 }
555 pr_cont("\n");
556}
557
Hans Verkuild84f2d942012-06-09 11:57:46 -0300558static void v4l_print_cropcap(const void *arg, bool write_only)
559{
560 const struct v4l2_cropcap *p = arg;
561
562 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
563 "defrect wxh=%dx%d, x,y=%d,%d\n, "
564 "pixelaspect %d/%d\n",
565 prt_names(p->type, v4l2_type_names),
566 p->bounds.width, p->bounds.height,
567 p->bounds.left, p->bounds.top,
568 p->defrect.width, p->defrect.height,
569 p->defrect.left, p->defrect.top,
570 p->pixelaspect.numerator, p->pixelaspect.denominator);
571}
572
573static void v4l_print_crop(const void *arg, bool write_only)
574{
575 const struct v4l2_crop *p = arg;
576
577 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
578 prt_names(p->type, v4l2_type_names),
579 p->c.width, p->c.height,
580 p->c.left, p->c.top);
581}
582
583static void v4l_print_selection(const void *arg, bool write_only)
584{
585 const struct v4l2_selection *p = arg;
586
587 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
588 prt_names(p->type, v4l2_type_names),
589 p->target, p->flags,
590 p->r.width, p->r.height, p->r.left, p->r.top);
591}
592
Hans Verkuild806f2d2012-06-09 10:02:49 -0300593static void v4l_print_jpegcompression(const void *arg, bool write_only)
594{
595 const struct v4l2_jpegcompression *p = arg;
596
597 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
598 "COM_len=%d, jpeg_markers=0x%x\n",
599 p->quality, p->APPn, p->APP_len,
600 p->COM_len, p->jpeg_markers);
601}
602
603static void v4l_print_enc_idx(const void *arg, bool write_only)
604{
605 const struct v4l2_enc_idx *p = arg;
606
607 pr_cont("entries=%d, entries_cap=%d\n",
608 p->entries, p->entries_cap);
609}
610
611static void v4l_print_encoder_cmd(const void *arg, bool write_only)
612{
613 const struct v4l2_encoder_cmd *p = arg;
614
615 pr_cont("cmd=%d, flags=0x%x\n",
616 p->cmd, p->flags);
617}
618
619static void v4l_print_decoder_cmd(const void *arg, bool write_only)
620{
621 const struct v4l2_decoder_cmd *p = arg;
622
623 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
624
625 if (p->cmd == V4L2_DEC_CMD_START)
626 pr_info("speed=%d, format=%u\n",
627 p->start.speed, p->start.format);
628 else if (p->cmd == V4L2_DEC_CMD_STOP)
629 pr_info("pts=%llu\n", p->stop.pts);
630}
631
Hans Verkuileb3728e2012-06-22 06:23:59 -0300632static void v4l_print_u32(const void *arg, bool write_only)
633{
634 pr_cont("value=%u\n", *(const u32 *)arg);
635}
636
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300637static void dbgtimings(struct video_device *vfd,
638 const struct v4l2_dv_timings *p)
639{
640 switch (p->type) {
641 case V4L2_DV_BT_656_1120:
642 dbgarg2("bt-656/1120:interlaced=%d,"
643 " pixelclock=%lld,"
644 " width=%d, height=%d, polarities=%x,"
645 " hfrontporch=%d, hsync=%d,"
646 " hbackporch=%d, vfrontporch=%d,"
647 " vsync=%d, vbackporch=%d,"
648 " il_vfrontporch=%d, il_vsync=%d,"
649 " il_vbackporch=%d, standards=%x, flags=%x\n",
650 p->bt.interlaced, p->bt.pixelclock,
651 p->bt.width, p->bt.height,
652 p->bt.polarities, p->bt.hfrontporch,
653 p->bt.hsync, p->bt.hbackporch,
654 p->bt.vfrontporch, p->bt.vsync,
655 p->bt.vbackporch, p->bt.il_vfrontporch,
656 p->bt.il_vsync, p->bt.il_vbackporch,
657 p->bt.standards, p->bt.flags);
658 break;
659 default:
660 dbgarg2("Unknown type %d!\n", p->type);
661 break;
662 }
663}
664
Hans Verkuilefbceec2012-06-09 12:54:02 -0300665static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300666{
667 __u32 i;
668
669 /* zero the reserved fields */
670 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300671 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300672 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300673
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300674 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
675 when using extended controls.
676 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
677 is it allowed for backwards compatibility.
678 */
679 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
680 return 0;
681 /* Check that all controls are from the same control class. */
682 for (i = 0; i < c->count; i++) {
683 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
684 c->error_idx = i;
685 return 0;
686 }
687 }
688 return 1;
689}
690
Hans Verkuila3998102008-07-21 02:57:38 -0300691static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300692{
Hans Verkuila3998102008-07-21 02:57:38 -0300693 if (ops == NULL)
694 return -EINVAL;
695
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300696 switch (type) {
697 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300698 if (ops->vidioc_g_fmt_vid_cap ||
699 ops->vidioc_g_fmt_vid_cap_mplane)
700 return 0;
701 break;
702 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
703 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300704 return 0;
705 break;
706 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300707 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300708 return 0;
709 break;
710 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300711 if (ops->vidioc_g_fmt_vid_out ||
712 ops->vidioc_g_fmt_vid_out_mplane)
713 return 0;
714 break;
715 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
716 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300717 return 0;
718 break;
719 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300720 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300721 return 0;
722 break;
723 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300724 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300725 return 0;
726 break;
727 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300728 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300729 return 0;
730 break;
731 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300732 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300733 return 0;
734 break;
735 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300736 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300737 return 0;
738 break;
739 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300740 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300741 return 0;
742 break;
743 }
744 return -EINVAL;
745}
746
Hans Verkuil59076122012-06-22 06:09:54 -0300747static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
748 struct file *file, void *fh, void *arg)
749{
750 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
751
752 cap->version = LINUX_VERSION_CODE;
753 return ops->vidioc_querycap(file, fh, cap);
754}
755
756static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
757 struct file *file, void *fh, void *arg)
758{
759 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
760}
761
762static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
763 struct file *file, void *fh, void *arg)
764{
765 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
766}
767
Hans Verkuild0547cb2012-06-09 09:27:10 -0300768static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
769 struct file *file, void *fh, void *arg)
770{
771 struct video_device *vfd;
772 u32 *p = arg;
773
774 if (ops->vidioc_g_priority)
775 return ops->vidioc_g_priority(file, fh, arg);
776 vfd = video_devdata(file);
777 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
778 return 0;
779}
780
781static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
782 struct file *file, void *fh, void *arg)
783{
784 struct video_device *vfd;
785 struct v4l2_fh *vfh;
786 u32 *p = arg;
787
788 if (ops->vidioc_s_priority)
789 return ops->vidioc_s_priority(file, fh, *p);
790 vfd = video_devdata(file);
791 vfh = file->private_data;
792 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
793}
794
Hans Verkuil59076122012-06-22 06:09:54 -0300795static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
796 struct file *file, void *fh, void *arg)
797{
798 struct v4l2_input *p = arg;
799
800 /*
801 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
802 * CAP_STD here based on ioctl handler provided by the
803 * driver. If the driver doesn't support these
804 * for a specific input, it must override these flags.
805 */
806 if (ops->vidioc_s_std)
807 p->capabilities |= V4L2_IN_CAP_STD;
808 if (ops->vidioc_s_dv_preset)
809 p->capabilities |= V4L2_IN_CAP_PRESETS;
810 if (ops->vidioc_s_dv_timings)
811 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
812
813 return ops->vidioc_enum_input(file, fh, p);
814}
815
816static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
817 struct file *file, void *fh, void *arg)
818{
819 struct v4l2_output *p = arg;
820
821 /*
822 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
823 * CAP_STD here based on ioctl handler provided by the
824 * driver. If the driver doesn't support these
825 * for a specific output, it must override these flags.
826 */
827 if (ops->vidioc_s_std)
828 p->capabilities |= V4L2_OUT_CAP_STD;
829 if (ops->vidioc_s_dv_preset)
830 p->capabilities |= V4L2_OUT_CAP_PRESETS;
831 if (ops->vidioc_s_dv_timings)
832 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
833
834 return ops->vidioc_enum_output(file, fh, p);
835}
836
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300837static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
838 struct file *file, void *fh, void *arg)
839{
840 struct v4l2_fmtdesc *p = arg;
841
842 switch (p->type) {
843 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
844 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
845 break;
846 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
847 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
848 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
849 break;
850 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
851 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
852 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
853 break;
854 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
855 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
856 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
857 break;
858 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
859 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
860 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
861 break;
862 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
863 case V4L2_BUF_TYPE_PRIVATE:
864 if (unlikely(!ops->vidioc_enum_fmt_type_private))
865 break;
866 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
867 }
868 return -EINVAL;
869}
870
871static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
872 struct file *file, void *fh, void *arg)
873{
874 struct v4l2_format *p = arg;
875
876 switch (p->type) {
877 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
878 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
879 break;
880 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
881 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
882 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
883 break;
884 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
885 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
886 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
887 break;
888 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
889 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
890 if (unlikely(!ops->vidioc_g_fmt_vid_out))
891 break;
892 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
893 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
894 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
895 break;
896 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
897 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
898 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
899 break;
900 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
901 case V4L2_BUF_TYPE_VBI_CAPTURE:
902 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
903 break;
904 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
905 case V4L2_BUF_TYPE_VBI_OUTPUT:
906 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
907 break;
908 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
909 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
910 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
911 break;
912 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
913 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
914 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
915 break;
916 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
917 case V4L2_BUF_TYPE_PRIVATE:
918 if (unlikely(!ops->vidioc_g_fmt_type_private))
919 break;
920 return ops->vidioc_g_fmt_type_private(file, fh, arg);
921 }
922 return -EINVAL;
923}
924
925static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
926 struct file *file, void *fh, void *arg)
927{
928 struct v4l2_format *p = arg;
929
930 switch (p->type) {
931 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
932 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
933 break;
934 CLEAR_AFTER_FIELD(p, fmt.pix);
935 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
936 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
937 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
938 break;
939 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
940 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
941 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
942 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
943 break;
944 CLEAR_AFTER_FIELD(p, fmt.win);
945 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
946 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
947 if (unlikely(!ops->vidioc_s_fmt_vid_out))
948 break;
949 CLEAR_AFTER_FIELD(p, fmt.pix);
950 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
951 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
952 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
953 break;
954 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
955 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
956 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
957 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
958 break;
959 CLEAR_AFTER_FIELD(p, fmt.win);
960 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
961 case V4L2_BUF_TYPE_VBI_CAPTURE:
962 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
963 break;
964 CLEAR_AFTER_FIELD(p, fmt.vbi);
965 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
966 case V4L2_BUF_TYPE_VBI_OUTPUT:
967 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
968 break;
969 CLEAR_AFTER_FIELD(p, fmt.vbi);
970 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
971 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
972 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
973 break;
974 CLEAR_AFTER_FIELD(p, fmt.sliced);
975 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
976 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
977 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
978 break;
979 CLEAR_AFTER_FIELD(p, fmt.sliced);
980 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
981 case V4L2_BUF_TYPE_PRIVATE:
982 if (unlikely(!ops->vidioc_s_fmt_type_private))
983 break;
984 return ops->vidioc_s_fmt_type_private(file, fh, arg);
985 }
986 return -EINVAL;
987}
988
989static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
990 struct file *file, void *fh, void *arg)
991{
992 struct v4l2_format *p = arg;
993
994 switch (p->type) {
995 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
996 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
997 break;
998 CLEAR_AFTER_FIELD(p, fmt.pix);
999 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1000 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1001 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1002 break;
1003 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1004 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1005 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1006 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1007 break;
1008 CLEAR_AFTER_FIELD(p, fmt.win);
1009 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1010 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1011 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1012 break;
1013 CLEAR_AFTER_FIELD(p, fmt.pix);
1014 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1015 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1016 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1017 break;
1018 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1019 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1020 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1021 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1022 break;
1023 CLEAR_AFTER_FIELD(p, fmt.win);
1024 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1025 case V4L2_BUF_TYPE_VBI_CAPTURE:
1026 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1027 break;
1028 CLEAR_AFTER_FIELD(p, fmt.vbi);
1029 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1030 case V4L2_BUF_TYPE_VBI_OUTPUT:
1031 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1032 break;
1033 CLEAR_AFTER_FIELD(p, fmt.vbi);
1034 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1035 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1036 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1037 break;
1038 CLEAR_AFTER_FIELD(p, fmt.sliced);
1039 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1040 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1041 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1042 break;
1043 CLEAR_AFTER_FIELD(p, fmt.sliced);
1044 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1045 case V4L2_BUF_TYPE_PRIVATE:
1046 if (unlikely(!ops->vidioc_try_fmt_type_private))
1047 break;
1048 return ops->vidioc_try_fmt_type_private(file, fh, arg);
1049 }
1050 return -EINVAL;
1051}
1052
Hans Verkuile325b242012-06-09 12:50:15 -03001053static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1054 struct file *file, void *fh, void *arg)
1055{
1056 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1057}
1058
1059static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1060 struct file *file, void *fh, void *arg)
1061{
1062 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1063}
1064
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001065static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1066 struct file *file, void *fh, void *arg)
1067{
1068 struct video_device *vfd = video_devdata(file);
1069 struct v4l2_tuner *p = arg;
1070
1071 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1072 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1073 return ops->vidioc_g_tuner(file, fh, p);
1074}
1075
1076static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1077 struct file *file, void *fh, void *arg)
1078{
1079 struct video_device *vfd = video_devdata(file);
1080 struct v4l2_tuner *p = arg;
1081
1082 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1083 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1084 return ops->vidioc_s_tuner(file, fh, p);
1085}
1086
1087static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1088 struct file *file, void *fh, void *arg)
1089{
1090 struct video_device *vfd = video_devdata(file);
1091 struct v4l2_frequency *p = arg;
1092
1093 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1094 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1095 return ops->vidioc_g_frequency(file, fh, p);
1096}
1097
1098static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1099 struct file *file, void *fh, void *arg)
1100{
1101 struct video_device *vfd = video_devdata(file);
1102 struct v4l2_frequency *p = arg;
1103 enum v4l2_tuner_type type;
1104
1105 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1106 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1107 if (p->type != type)
1108 return -EINVAL;
1109 return ops->vidioc_s_frequency(file, fh, p);
1110}
1111
1112static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1113 struct file *file, void *fh, void *arg)
1114{
1115 struct video_device *vfd = video_devdata(file);
1116 struct v4l2_standard *p = arg;
1117 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1118 unsigned int index = p->index, i, j = 0;
1119 const char *descr = "";
1120
1121 /* Return norm array in a canonical way */
1122 for (i = 0; i <= index && id; i++) {
1123 /* last std value in the standards array is 0, so this
1124 while always ends there since (id & 0) == 0. */
1125 while ((id & standards[j].std) != standards[j].std)
1126 j++;
1127 curr_id = standards[j].std;
1128 descr = standards[j].descr;
1129 j++;
1130 if (curr_id == 0)
1131 break;
1132 if (curr_id != V4L2_STD_PAL &&
1133 curr_id != V4L2_STD_SECAM &&
1134 curr_id != V4L2_STD_NTSC)
1135 id &= ~curr_id;
1136 }
1137 if (i <= index)
1138 return -EINVAL;
1139
1140 v4l2_video_std_construct(p, curr_id, descr);
1141 return 0;
1142}
1143
1144static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1145 struct file *file, void *fh, void *arg)
1146{
1147 struct video_device *vfd = video_devdata(file);
1148 v4l2_std_id *id = arg;
1149
1150 /* Calls the specific handler */
1151 if (ops->vidioc_g_std)
1152 return ops->vidioc_g_std(file, fh, arg);
1153 if (vfd->current_norm) {
1154 *id = vfd->current_norm;
1155 return 0;
1156 }
1157 return -ENOTTY;
1158}
1159
1160static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1161 struct file *file, void *fh, void *arg)
1162{
1163 struct video_device *vfd = video_devdata(file);
1164 v4l2_std_id *id = arg, norm;
1165 int ret;
1166
1167 norm = (*id) & vfd->tvnorms;
1168 if (vfd->tvnorms && !norm) /* Check if std is supported */
1169 return -EINVAL;
1170
1171 /* Calls the specific handler */
1172 ret = ops->vidioc_s_std(file, fh, &norm);
1173
1174 /* Updates standard information */
1175 if (ret >= 0)
1176 vfd->current_norm = norm;
1177 return ret;
1178}
1179
1180static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1181 struct file *file, void *fh, void *arg)
1182{
1183 struct video_device *vfd = video_devdata(file);
1184 v4l2_std_id *p = arg;
1185
1186 /*
1187 * If nothing detected, it should return all supported
1188 * standard.
1189 * Drivers just need to mask the std argument, in order
1190 * to remove the standards that don't apply from the mask.
1191 * This means that tuners, audio and video decoders can join
1192 * their efforts to improve the standards detection.
1193 */
1194 *p = vfd->tvnorms;
1195 return ops->vidioc_querystd(file, fh, arg);
1196}
1197
1198static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1199 struct file *file, void *fh, void *arg)
1200{
1201 struct video_device *vfd = video_devdata(file);
1202 struct v4l2_hw_freq_seek *p = arg;
1203 enum v4l2_tuner_type type;
1204
1205 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1206 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1207 if (p->type != type)
1208 return -EINVAL;
1209 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1210}
1211
Hans Verkuileb3728e2012-06-22 06:23:59 -03001212static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1213 struct file *file, void *fh, void *arg)
1214{
1215 struct v4l2_requestbuffers *p = arg;
1216 int ret = check_fmt(ops, p->type);
1217
1218 if (ret)
1219 return ret;
1220
1221 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1222 CLEAR_AFTER_FIELD(p, memory);
1223
1224 return ops->vidioc_reqbufs(file, fh, p);
1225}
1226
1227static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1228 struct file *file, void *fh, void *arg)
1229{
1230 struct v4l2_buffer *p = arg;
1231 int ret = check_fmt(ops, p->type);
1232
1233 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1234}
1235
1236static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1237 struct file *file, void *fh, void *arg)
1238{
1239 struct v4l2_buffer *p = arg;
1240 int ret = check_fmt(ops, p->type);
1241
1242 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1243}
1244
1245static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1246 struct file *file, void *fh, void *arg)
1247{
1248 struct v4l2_buffer *p = arg;
1249 int ret = check_fmt(ops, p->type);
1250
1251 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1252}
1253
1254static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1255 struct file *file, void *fh, void *arg)
1256{
1257 struct v4l2_create_buffers *create = arg;
1258 int ret = check_fmt(ops, create->format.type);
1259
1260 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1261}
1262
1263static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1264 struct file *file, void *fh, void *arg)
1265{
1266 struct v4l2_buffer *b = arg;
1267 int ret = check_fmt(ops, b->type);
1268
1269 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1270}
1271
1272static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1273 struct file *file, void *fh, void *arg)
1274{
1275 struct video_device *vfd = video_devdata(file);
1276 struct v4l2_streamparm *p = arg;
1277 v4l2_std_id std;
1278 int ret = check_fmt(ops, p->type);
1279
1280 if (ret)
1281 return ret;
1282 if (ops->vidioc_g_parm)
1283 return ops->vidioc_g_parm(file, fh, p);
1284 std = vfd->current_norm;
1285 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1286 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1287 return -EINVAL;
1288 p->parm.capture.readbuffers = 2;
1289 if (ops->vidioc_g_std)
1290 ret = ops->vidioc_g_std(file, fh, &std);
1291 if (ret == 0)
1292 v4l2_video_std_frame_period(std,
1293 &p->parm.capture.timeperframe);
1294 return ret;
1295}
1296
1297static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1298 struct file *file, void *fh, void *arg)
1299{
1300 struct v4l2_streamparm *p = arg;
1301 int ret = check_fmt(ops, p->type);
1302
1303 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1304}
1305
Hans Verkuilefbceec2012-06-09 12:54:02 -03001306static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1307 struct file *file, void *fh, void *arg)
1308{
1309 struct video_device *vfd = video_devdata(file);
1310 struct v4l2_queryctrl *p = arg;
1311 struct v4l2_fh *vfh = fh;
1312
1313 if (vfh && vfh->ctrl_handler)
1314 return v4l2_queryctrl(vfh->ctrl_handler, p);
1315 if (vfd->ctrl_handler)
1316 return v4l2_queryctrl(vfd->ctrl_handler, p);
1317 if (ops->vidioc_queryctrl)
1318 return ops->vidioc_queryctrl(file, fh, p);
1319 return -ENOTTY;
1320}
1321
1322static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1323 struct file *file, void *fh, void *arg)
1324{
1325 struct video_device *vfd = video_devdata(file);
1326 struct v4l2_querymenu *p = arg;
1327 struct v4l2_fh *vfh = fh;
1328
1329 if (vfh && vfh->ctrl_handler)
1330 return v4l2_querymenu(vfh->ctrl_handler, p);
1331 if (vfd->ctrl_handler)
1332 return v4l2_querymenu(vfd->ctrl_handler, p);
1333 if (ops->vidioc_querymenu)
1334 return ops->vidioc_querymenu(file, fh, p);
1335 return -ENOTTY;
1336}
1337
1338static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1339 struct file *file, void *fh, void *arg)
1340{
1341 struct video_device *vfd = video_devdata(file);
1342 struct v4l2_control *p = arg;
1343 struct v4l2_fh *vfh = fh;
1344 struct v4l2_ext_controls ctrls;
1345 struct v4l2_ext_control ctrl;
1346
1347 if (vfh && vfh->ctrl_handler)
1348 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1349 if (vfd->ctrl_handler)
1350 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1351 if (ops->vidioc_g_ctrl)
1352 return ops->vidioc_g_ctrl(file, fh, p);
1353 if (ops->vidioc_g_ext_ctrls == NULL)
1354 return -ENOTTY;
1355
1356 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1357 ctrls.count = 1;
1358 ctrls.controls = &ctrl;
1359 ctrl.id = p->id;
1360 ctrl.value = p->value;
1361 if (check_ext_ctrls(&ctrls, 1)) {
1362 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1363
1364 if (ret == 0)
1365 p->value = ctrl.value;
1366 return ret;
1367 }
1368 return -EINVAL;
1369}
1370
1371static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1372 struct file *file, void *fh, void *arg)
1373{
1374 struct video_device *vfd = video_devdata(file);
1375 struct v4l2_control *p = arg;
1376 struct v4l2_fh *vfh = fh;
1377 struct v4l2_ext_controls ctrls;
1378 struct v4l2_ext_control ctrl;
1379
1380 if (vfh && vfh->ctrl_handler)
1381 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1382 if (vfd->ctrl_handler)
1383 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1384 if (ops->vidioc_s_ctrl)
1385 return ops->vidioc_s_ctrl(file, fh, p);
1386 if (ops->vidioc_s_ext_ctrls == NULL)
1387 return -ENOTTY;
1388
1389 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1390 ctrls.count = 1;
1391 ctrls.controls = &ctrl;
1392 ctrl.id = p->id;
1393 ctrl.value = p->value;
1394 if (check_ext_ctrls(&ctrls, 1))
1395 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1396 return -EINVAL;
1397}
1398
1399static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1400 struct file *file, void *fh, void *arg)
1401{
1402 struct video_device *vfd = video_devdata(file);
1403 struct v4l2_ext_controls *p = arg;
1404 struct v4l2_fh *vfh = fh;
1405
1406 p->error_idx = p->count;
1407 if (vfh && vfh->ctrl_handler)
1408 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1409 if (vfd->ctrl_handler)
1410 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1411 if (ops->vidioc_g_ext_ctrls == NULL)
1412 return -ENOTTY;
1413 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1414 -EINVAL;
1415}
1416
1417static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1418 struct file *file, void *fh, void *arg)
1419{
1420 struct video_device *vfd = video_devdata(file);
1421 struct v4l2_ext_controls *p = arg;
1422 struct v4l2_fh *vfh = fh;
1423
1424 p->error_idx = p->count;
1425 if (vfh && vfh->ctrl_handler)
1426 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1427 if (vfd->ctrl_handler)
1428 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1429 if (ops->vidioc_s_ext_ctrls == NULL)
1430 return -ENOTTY;
1431 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1432 -EINVAL;
1433}
1434
1435static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1436 struct file *file, void *fh, void *arg)
1437{
1438 struct video_device *vfd = video_devdata(file);
1439 struct v4l2_ext_controls *p = arg;
1440 struct v4l2_fh *vfh = fh;
1441
1442 p->error_idx = p->count;
1443 if (vfh && vfh->ctrl_handler)
1444 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1445 if (vfd->ctrl_handler)
1446 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1447 if (ops->vidioc_try_ext_ctrls == NULL)
1448 return -ENOTTY;
1449 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1450 -EINVAL;
1451}
1452
Hans Verkuild84f2d942012-06-09 11:57:46 -03001453static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1454 struct file *file, void *fh, void *arg)
1455{
1456 struct v4l2_crop *p = arg;
1457 struct v4l2_selection s = {
1458 .type = p->type,
1459 };
1460 int ret;
1461
1462 if (ops->vidioc_g_crop)
1463 return ops->vidioc_g_crop(file, fh, p);
1464 /* simulate capture crop using selection api */
1465
1466 /* crop means compose for output devices */
1467 if (V4L2_TYPE_IS_OUTPUT(p->type))
1468 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1469 else
1470 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1471
1472 ret = ops->vidioc_g_selection(file, fh, &s);
1473
1474 /* copying results to old structure on success */
1475 if (!ret)
1476 p->c = s.r;
1477 return ret;
1478}
1479
1480static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1481 struct file *file, void *fh, void *arg)
1482{
1483 struct v4l2_crop *p = arg;
1484 struct v4l2_selection s = {
1485 .type = p->type,
1486 .r = p->c,
1487 };
1488
1489 if (ops->vidioc_s_crop)
1490 return ops->vidioc_s_crop(file, fh, p);
1491 /* simulate capture crop using selection api */
1492
1493 /* crop means compose for output devices */
1494 if (V4L2_TYPE_IS_OUTPUT(p->type))
1495 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1496 else
1497 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1498
1499 return ops->vidioc_s_selection(file, fh, &s);
1500}
1501
1502static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1503 struct file *file, void *fh, void *arg)
1504{
1505 struct v4l2_cropcap *p = arg;
1506 struct v4l2_selection s = { .type = p->type };
1507 int ret;
1508
1509 if (ops->vidioc_cropcap)
1510 return ops->vidioc_cropcap(file, fh, p);
1511
1512 /* obtaining bounds */
1513 if (V4L2_TYPE_IS_OUTPUT(p->type))
1514 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1515 else
1516 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1517
1518 ret = ops->vidioc_g_selection(file, fh, &s);
1519 if (ret)
1520 return ret;
1521 p->bounds = s.r;
1522
1523 /* obtaining defrect */
1524 if (V4L2_TYPE_IS_OUTPUT(p->type))
1525 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1526 else
1527 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1528
1529 ret = ops->vidioc_g_selection(file, fh, &s);
1530 if (ret)
1531 return ret;
1532 p->defrect = s.r;
1533
1534 /* setting trivial pixelaspect */
1535 p->pixelaspect.numerator = 1;
1536 p->pixelaspect.denominator = 1;
1537 return 0;
1538}
1539
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001540struct v4l2_ioctl_info {
1541 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001542 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001543 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001544 union {
1545 u32 offset;
1546 int (*func)(const struct v4l2_ioctl_ops *ops,
1547 struct file *file, void *fh, void *p);
1548 };
1549 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001550};
1551
1552/* This control needs a priority check */
1553#define INFO_FL_PRIO (1 << 0)
1554/* This control can be valid if the filehandle passes a control handler. */
1555#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001556/* This is a standard ioctl, no need for special code */
1557#define INFO_FL_STD (1 << 2)
1558/* This is ioctl has its own function */
1559#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001560/* Zero struct from after the field to the end */
1561#define INFO_FL_CLEAR(v4l2_struct, field) \
1562 ((offsetof(struct v4l2_struct, field) + \
1563 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1564#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001565
1566#define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
1567 .ioctl = _ioctl, \
1568 .flags = _flags, \
1569 .name = #_ioctl, \
1570}
1571
Hans Verkuil86748f32012-06-22 06:04:16 -03001572#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1573 [_IOC_NR(_ioctl)] = { \
1574 .ioctl = _ioctl, \
1575 .flags = _flags | INFO_FL_STD, \
1576 .name = #_ioctl, \
1577 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1578 .debug = _debug, \
1579 }
1580
1581#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1582 [_IOC_NR(_ioctl)] = { \
1583 .ioctl = _ioctl, \
1584 .flags = _flags | INFO_FL_FUNC, \
1585 .name = #_ioctl, \
1586 .func = _func, \
1587 .debug = _debug, \
1588 }
1589
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001590static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001591 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001592 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1593 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1594 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001595 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
1596 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001597 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1598 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001599 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001600 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
1601 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
Hans Verkuile325b242012-06-09 12:50:15 -03001602 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
1603 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001604 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1605 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001606 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1607 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1608 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001609 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001610 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1611 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 -03001612 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1613 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001614 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1615 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001616 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1617 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 -03001618 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1619 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1620 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1621 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1622 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1623 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1624 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001625 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1626 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1627 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1628 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03001629 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1630 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1631 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1632 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1633 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001634 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1635 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001636 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001637 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001638 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1639 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001640 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1641 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001642 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001643 IOCTL_INFO(VIDIOC_LOG_STATUS, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001644 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1645 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
1646 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001647 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1648 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001649 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
1650 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1651 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1652 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
1653 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001654 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, 0),
1655 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001656 IOCTL_INFO(VIDIOC_DBG_G_CHIP_IDENT, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001657 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 -03001658 IOCTL_INFO(VIDIOC_ENUM_DV_PRESETS, 0),
1659 IOCTL_INFO(VIDIOC_S_DV_PRESET, INFO_FL_PRIO),
1660 IOCTL_INFO(VIDIOC_G_DV_PRESET, 0),
1661 IOCTL_INFO(VIDIOC_QUERY_DV_PRESET, 0),
1662 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, INFO_FL_PRIO),
1663 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, 0),
1664 IOCTL_INFO(VIDIOC_DQEVENT, 0),
1665 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, 0),
1666 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, 0),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001667 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
1668 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001669 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, 0),
1670 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, 0),
1671 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, 0),
1672};
1673#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1674
1675bool v4l2_is_known_ioctl(unsigned int cmd)
1676{
1677 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
1678 return false;
1679 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
1680}
1681
1682/* Common ioctl debug function. This function can be used by
1683 external ioctl messages as well as internal V4L ioctl */
1684void v4l_printk_ioctl(unsigned int cmd)
1685{
Hans Verkuil86748f32012-06-22 06:04:16 -03001686 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001687
1688 switch (_IOC_TYPE(cmd)) {
1689 case 'd':
1690 type = "v4l2_int";
1691 break;
1692 case 'V':
1693 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
1694 type = "v4l2";
1695 break;
1696 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001697 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001698 return;
1699 default:
1700 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03001701 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001702 }
1703
1704 switch (_IOC_DIR(cmd)) {
1705 case _IOC_NONE: dir = "--"; break;
1706 case _IOC_READ: dir = "r-"; break;
1707 case _IOC_WRITE: dir = "-w"; break;
1708 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
1709 default: dir = "*ERR*"; break;
1710 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001711 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001712 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
1713}
1714EXPORT_SYMBOL(v4l_printk_ioctl);
1715
Hans Verkuil069b7472008-12-30 07:04:34 -03001716static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001717 unsigned int cmd, void *arg)
1718{
1719 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03001720 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03001721 bool write_only = false;
1722 struct v4l2_ioctl_info default_info;
1723 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03001724 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001725 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03001726 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001727 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001728
Hans Verkuil6a717882010-04-06 15:56:08 -03001729 if (ops == NULL) {
1730 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
1731 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03001732 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03001733 }
1734
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001735 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
1736 vfh = file->private_data;
1737 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001738 }
1739
1740 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03001741 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001742
1743 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
1744 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03001745 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001746
1747 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
1748 ret = v4l2_prio_check(vfd->prio, vfh->prio);
1749 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03001750 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03001751 }
Hans Verkuil86748f32012-06-22 06:04:16 -03001752 } else {
1753 default_info.ioctl = cmd;
1754 default_info.flags = 0;
1755 default_info.debug = NULL;
1756 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001757 }
1758
Hans Verkuil86748f32012-06-22 06:04:16 -03001759 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
1760 if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001761 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03001762 pr_cont(": ");
1763 info->debug(arg, write_only);
1764 }
1765 if (info->flags & INFO_FL_STD) {
1766 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
1767 const void *p = vfd->ioctl_ops;
1768 const vidioc_op *vidioc = p + info->offset;
1769
1770 ret = (*vidioc)(file, fh, arg);
1771 goto done;
1772 } else if (info->flags & INFO_FL_FUNC) {
1773 ret = info->func(ops, file, fh, arg);
1774 goto done;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001775 }
1776
Hans Verkuil6a717882010-04-06 15:56:08 -03001777 switch (cmd) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001778 case VIDIOC_G_SLICED_VBI_CAP:
1779 {
1780 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001781
Trent Piepho19c96e42009-03-04 01:21:02 -03001782 /* Clear up to type, everything after type is zerod already */
1783 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1784
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001785 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001786 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001787 if (!ret)
1788 dbgarg2("service_set=%d\n", p->service_set);
1789 break;
1790 }
1791 case VIDIOC_LOG_STATUS:
1792 {
Hans Verkuile2ecb252012-02-02 08:20:53 -03001793 if (vfd->v4l2_dev)
1794 pr_info("%s: ================= START STATUS =================\n",
1795 vfd->v4l2_dev->name);
Hans Verkuila3998102008-07-21 02:57:38 -03001796 ret = ops->vidioc_log_status(file, fh);
Hans Verkuile2ecb252012-02-02 08:20:53 -03001797 if (vfd->v4l2_dev)
1798 pr_info("%s: ================== END STATUS ==================\n",
1799 vfd->v4l2_dev->name);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001800 break;
1801 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001802 case VIDIOC_DBG_G_REGISTER:
1803 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001804#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001805 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001806
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001807 if (!capable(CAP_SYS_ADMIN))
1808 ret = -EPERM;
1809 else
1810 ret = ops->vidioc_g_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001811#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001812 break;
1813 }
1814 case VIDIOC_DBG_S_REGISTER:
1815 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001816#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001817 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001818
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001819 if (!capable(CAP_SYS_ADMIN))
1820 ret = -EPERM;
1821 else
1822 ret = ops->vidioc_s_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001823#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001824 break;
1825 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001826 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001827 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001828 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001829
Hans Verkuil80b36e02009-02-07 11:00:02 -03001830 p->ident = V4L2_IDENT_NONE;
1831 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001832 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001833 if (!ret)
1834 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1835 break;
1836 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001837 case VIDIOC_ENUM_FRAMESIZES:
1838 {
1839 struct v4l2_frmsizeenum *p = arg;
1840
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001841 ret = ops->vidioc_enum_framesizes(file, fh, p);
1842 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03001843 "index=%d, pixelformat=%c%c%c%c, type=%d ",
1844 p->index,
1845 (p->pixel_format & 0xff),
1846 (p->pixel_format >> 8) & 0xff,
1847 (p->pixel_format >> 16) & 0xff,
1848 (p->pixel_format >> 24) & 0xff,
1849 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001850 switch (p->type) {
1851 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001852 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001853 p->discrete.width, p->discrete.height);
1854 break;
1855 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001856 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001857 p->stepwise.min_width, p->stepwise.min_height,
1858 p->stepwise.step_width, p->stepwise.step_height,
1859 p->stepwise.max_width, p->stepwise.max_height);
1860 break;
1861 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001862 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001863 break;
1864 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001865 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001866 }
1867
1868 break;
1869 }
1870 case VIDIOC_ENUM_FRAMEINTERVALS:
1871 {
1872 struct v4l2_frmivalenum *p = arg;
1873
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001874 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1875 dbgarg(cmd,
1876 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1877 p->index, p->pixel_format,
1878 p->width, p->height, p->type);
1879 switch (p->type) {
1880 case V4L2_FRMIVAL_TYPE_DISCRETE:
1881 dbgarg2("fps=%d/%d\n",
1882 p->discrete.numerator,
1883 p->discrete.denominator);
1884 break;
1885 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03001886 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1887 p->stepwise.min.numerator,
1888 p->stepwise.min.denominator,
1889 p->stepwise.max.numerator,
1890 p->stepwise.max.denominator,
1891 p->stepwise.step.numerator,
1892 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001893 break;
1894 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1895 dbgarg2("continuous\n");
1896 break;
1897 default:
1898 dbgarg2("- Unknown type!\n");
1899 }
1900 break;
1901 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001902 case VIDIOC_ENUM_DV_PRESETS:
1903 {
1904 struct v4l2_dv_enum_preset *p = arg;
1905
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001906 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1907 if (!ret)
1908 dbgarg(cmd,
1909 "index=%d, preset=%d, name=%s, width=%d,"
1910 " height=%d ",
1911 p->index, p->preset, p->name, p->width,
1912 p->height);
1913 break;
1914 }
1915 case VIDIOC_S_DV_PRESET:
1916 {
1917 struct v4l2_dv_preset *p = arg;
1918
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001919 dbgarg(cmd, "preset=%d\n", p->preset);
1920 ret = ops->vidioc_s_dv_preset(file, fh, p);
1921 break;
1922 }
1923 case VIDIOC_G_DV_PRESET:
1924 {
1925 struct v4l2_dv_preset *p = arg;
1926
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001927 ret = ops->vidioc_g_dv_preset(file, fh, p);
1928 if (!ret)
1929 dbgarg(cmd, "preset=%d\n", p->preset);
1930 break;
1931 }
1932 case VIDIOC_QUERY_DV_PRESET:
1933 {
1934 struct v4l2_dv_preset *p = arg;
1935
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001936 ret = ops->vidioc_query_dv_preset(file, fh, p);
1937 if (!ret)
1938 dbgarg(cmd, "preset=%d\n", p->preset);
1939 break;
1940 }
1941 case VIDIOC_S_DV_TIMINGS:
1942 {
1943 struct v4l2_dv_timings *p = arg;
1944
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001945 dbgtimings(vfd, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001946 switch (p->type) {
1947 case V4L2_DV_BT_656_1120:
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001948 ret = ops->vidioc_s_dv_timings(file, fh, p);
1949 break;
1950 default:
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001951 ret = -EINVAL;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001952 break;
1953 }
1954 break;
1955 }
1956 case VIDIOC_G_DV_TIMINGS:
1957 {
1958 struct v4l2_dv_timings *p = arg;
1959
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001960 ret = ops->vidioc_g_dv_timings(file, fh, p);
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001961 if (!ret)
1962 dbgtimings(vfd, p);
1963 break;
1964 }
1965 case VIDIOC_ENUM_DV_TIMINGS:
1966 {
1967 struct v4l2_enum_dv_timings *p = arg;
1968
1969 if (!ops->vidioc_enum_dv_timings)
1970 break;
1971
1972 ret = ops->vidioc_enum_dv_timings(file, fh, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001973 if (!ret) {
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001974 dbgarg(cmd, "index=%d: ", p->index);
1975 dbgtimings(vfd, &p->timings);
1976 }
1977 break;
1978 }
1979 case VIDIOC_QUERY_DV_TIMINGS:
1980 {
1981 struct v4l2_dv_timings *p = arg;
1982
1983 if (!ops->vidioc_query_dv_timings)
1984 break;
1985
1986 ret = ops->vidioc_query_dv_timings(file, fh, p);
1987 if (!ret)
1988 dbgtimings(vfd, p);
1989 break;
1990 }
1991 case VIDIOC_DV_TIMINGS_CAP:
1992 {
1993 struct v4l2_dv_timings_cap *p = arg;
1994
1995 if (!ops->vidioc_dv_timings_cap)
1996 break;
1997
1998 ret = ops->vidioc_dv_timings_cap(file, fh, p);
1999 if (ret)
2000 break;
2001 switch (p->type) {
2002 case V4L2_DV_BT_656_1120:
2003 dbgarg(cmd,
2004 "type=%d, width=%u-%u, height=%u-%u, "
2005 "pixelclock=%llu-%llu, standards=%x, capabilities=%x ",
2006 p->type,
2007 p->bt.min_width, p->bt.max_width,
2008 p->bt.min_height, p->bt.max_height,
2009 p->bt.min_pixelclock, p->bt.max_pixelclock,
2010 p->bt.standards, p->bt.capabilities);
2011 break;
2012 default:
2013 dbgarg(cmd, "unknown type ");
2014 break;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002015 }
2016 break;
2017 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002018 case VIDIOC_DQEVENT:
2019 {
2020 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002021
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002022 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2023 if (ret < 0) {
2024 dbgarg(cmd, "no pending events?");
2025 break;
2026 }
2027 dbgarg(cmd,
2028 "pending=%d, type=0x%8.8x, sequence=%d, "
2029 "timestamp=%lu.%9.9lu ",
2030 ev->pending, ev->type, ev->sequence,
2031 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2032 break;
2033 }
2034 case VIDIOC_SUBSCRIBE_EVENT:
2035 {
2036 struct v4l2_event_subscription *sub = arg;
2037
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002038 ret = ops->vidioc_subscribe_event(fh, sub);
2039 if (ret < 0) {
2040 dbgarg(cmd, "failed, ret=%ld", ret);
2041 break;
2042 }
2043 dbgarg(cmd, "type=0x%8.8x", sub->type);
2044 break;
2045 }
2046 case VIDIOC_UNSUBSCRIBE_EVENT:
2047 {
2048 struct v4l2_event_subscription *sub = arg;
2049
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002050 ret = ops->vidioc_unsubscribe_event(fh, sub);
2051 if (ret < 0) {
2052 dbgarg(cmd, "failed, ret=%ld", ret);
2053 break;
2054 }
2055 dbgarg(cmd, "type=0x%8.8x", sub->type);
2056 break;
2057 }
Hans Verkuila3998102008-07-21 02:57:38 -03002058 default:
Hans Verkuila3998102008-07-21 02:57:38 -03002059 if (!ops->vidioc_default)
2060 break;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002061 ret = ops->vidioc_default(file, fh, use_fh_prio ?
2062 v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2063 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002064 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002065 } /* switch */
2066
Hans Verkuil86748f32012-06-22 06:04:16 -03002067done:
2068 if (vfd->debug) {
2069 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2070 if (ret < 0)
2071 printk(KERN_DEBUG "%s: error %ld\n",
2072 video_device_node_name(vfd), ret);
2073 return ret;
2074 }
2075 v4l_print_ioctl(vfd->name, cmd);
2076 if (ret < 0)
2077 pr_cont(": error %ld\n", ret);
2078 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2079 pr_cont("\n");
2080 else if (!info->debug)
2081 return ret;
2082 else if (_IOC_DIR(cmd) == _IOC_NONE)
2083 info->debug(arg, write_only);
2084 else {
2085 pr_cont(": ");
2086 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002087 }
2088 }
2089
2090 return ret;
2091}
2092
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002093static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2094 void * __user *user_ptr, void ***kernel_ptr)
2095{
2096 int ret = 0;
2097
2098 switch (cmd) {
2099 case VIDIOC_QUERYBUF:
2100 case VIDIOC_QBUF:
2101 case VIDIOC_DQBUF: {
2102 struct v4l2_buffer *buf = parg;
2103
2104 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2105 if (buf->length > VIDEO_MAX_PLANES) {
2106 ret = -EINVAL;
2107 break;
2108 }
2109 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002110 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002111 *array_size = sizeof(struct v4l2_plane) * buf->length;
2112 ret = 1;
2113 }
2114 break;
2115 }
2116
2117 case VIDIOC_S_EXT_CTRLS:
2118 case VIDIOC_G_EXT_CTRLS:
2119 case VIDIOC_TRY_EXT_CTRLS: {
2120 struct v4l2_ext_controls *ctrls = parg;
2121
2122 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002123 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2124 ret = -EINVAL;
2125 break;
2126 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002127 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002128 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002129 *array_size = sizeof(struct v4l2_ext_control)
2130 * ctrls->count;
2131 ret = 1;
2132 }
2133 break;
2134 }
2135 }
2136
2137 return ret;
2138}
2139
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002140long
2141video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2142 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002143{
2144 char sbuf[128];
2145 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002146 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002147 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002148 bool has_array_args;
2149 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002150 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002151 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002152
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002153 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002154 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002155 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2156 parg = sbuf;
2157 } else {
2158 /* too big to allocate from stack */
2159 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2160 if (NULL == mbuf)
2161 return -ENOMEM;
2162 parg = mbuf;
2163 }
2164
2165 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002166 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002167 unsigned int n = _IOC_SIZE(cmd);
2168
2169 /*
2170 * In some cases, only a few fields are used as input,
2171 * i.e. when the app sets "index" and then the driver
2172 * fills in the rest of the structure for the thing
2173 * with that index. We only need to copy up the first
2174 * non-input field.
2175 */
2176 if (v4l2_is_known_ioctl(cmd)) {
2177 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2178 if (flags & INFO_FL_CLEAR_MASK)
2179 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2180 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002181
2182 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002183 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002184
2185 /* zero out anything we don't copy from userspace */
2186 if (n < _IOC_SIZE(cmd))
2187 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002188 } else {
2189 /* read-only ioctl */
2190 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002191 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002192 }
2193
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002194 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2195 if (err < 0)
2196 goto out;
2197 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002198
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002199 if (has_array_args) {
2200 /*
2201 * When adding new types of array args, make sure that the
2202 * parent argument to ioctl (which contains the pointer to the
2203 * array) fits into sbuf (so that mbuf will still remain
2204 * unused up to here).
2205 */
2206 mbuf = kmalloc(array_size, GFP_KERNEL);
2207 err = -ENOMEM;
2208 if (NULL == mbuf)
2209 goto out_array_args;
2210 err = -EFAULT;
2211 if (copy_from_user(mbuf, user_ptr, array_size))
2212 goto out_array_args;
2213 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002214 }
2215
2216 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002217 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002218 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002219 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002220
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002221 if (has_array_args) {
2222 *kernel_ptr = user_ptr;
2223 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002224 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002225 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002226 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002227 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2228 results that must be returned. */
2229 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002230 goto out;
2231
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002232out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002233 /* Copy results into user buffer */
2234 switch (_IOC_DIR(cmd)) {
2235 case _IOC_READ:
2236 case (_IOC_WRITE | _IOC_READ):
2237 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2238 err = -EFAULT;
2239 break;
2240 }
2241
2242out:
2243 kfree(mbuf);
2244 return err;
2245}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002246EXPORT_SYMBOL(video_usercopy);
2247
2248long video_ioctl2(struct file *file,
2249 unsigned int cmd, unsigned long arg)
2250{
2251 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2252}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002253EXPORT_SYMBOL(video_ioctl2);