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