blob: 7336363984c146e430e755879f6420ef30470810 [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 Verkuil5a5adf62012-06-22 07:29:35 -030030#include <media/videobuf2-core.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030031
Lucas De Marchi25985ed2011-03-30 22:57:33 -030032/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030033 * not including, the specified field is cleared. */
34#define CLEAR_AFTER_FIELD(p, field) \
35 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
36 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
37
Hans Verkuil35ea11f2008-07-20 08:12:02 -030038struct std_descr {
39 v4l2_std_id std;
40 const char *descr;
41};
42
43static const struct std_descr standards[] = {
44 { V4L2_STD_NTSC, "NTSC" },
45 { V4L2_STD_NTSC_M, "NTSC-M" },
46 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
47 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
48 { V4L2_STD_NTSC_443, "NTSC-443" },
49 { V4L2_STD_PAL, "PAL" },
50 { V4L2_STD_PAL_BG, "PAL-BG" },
51 { V4L2_STD_PAL_B, "PAL-B" },
52 { V4L2_STD_PAL_B1, "PAL-B1" },
53 { V4L2_STD_PAL_G, "PAL-G" },
54 { V4L2_STD_PAL_H, "PAL-H" },
55 { V4L2_STD_PAL_I, "PAL-I" },
56 { V4L2_STD_PAL_DK, "PAL-DK" },
57 { V4L2_STD_PAL_D, "PAL-D" },
58 { V4L2_STD_PAL_D1, "PAL-D1" },
59 { V4L2_STD_PAL_K, "PAL-K" },
60 { V4L2_STD_PAL_M, "PAL-M" },
61 { V4L2_STD_PAL_N, "PAL-N" },
62 { V4L2_STD_PAL_Nc, "PAL-Nc" },
63 { V4L2_STD_PAL_60, "PAL-60" },
64 { V4L2_STD_SECAM, "SECAM" },
65 { V4L2_STD_SECAM_B, "SECAM-B" },
66 { V4L2_STD_SECAM_G, "SECAM-G" },
67 { V4L2_STD_SECAM_H, "SECAM-H" },
68 { V4L2_STD_SECAM_DK, "SECAM-DK" },
69 { V4L2_STD_SECAM_D, "SECAM-D" },
70 { V4L2_STD_SECAM_K, "SECAM-K" },
71 { V4L2_STD_SECAM_K1, "SECAM-K1" },
72 { V4L2_STD_SECAM_L, "SECAM-L" },
73 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
74 { 0, "Unknown" }
75};
76
77/* video4linux standard ID conversion to standard name
78 */
79const char *v4l2_norm_to_name(v4l2_std_id id)
80{
81 u32 myid = id;
82 int i;
83
84 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
85 64 bit comparations. So, on that architecture, with some gcc
86 variants, compilation fails. Currently, the max value is 30bit wide.
87 */
88 BUG_ON(myid != id);
89
90 for (i = 0; standards[i].std; i++)
91 if (myid == standards[i].std)
92 break;
93 return standards[i].descr;
94}
95EXPORT_SYMBOL(v4l2_norm_to_name);
96
Trent Piepho51f0b8d52009-03-04 01:21:02 -030097/* Returns frame period for the given standard */
98void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
99{
100 if (id & V4L2_STD_525_60) {
101 frameperiod->numerator = 1001;
102 frameperiod->denominator = 30000;
103 } else {
104 frameperiod->numerator = 1;
105 frameperiod->denominator = 25;
106 }
107}
108EXPORT_SYMBOL(v4l2_video_std_frame_period);
109
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300110/* Fill in the fields of a v4l2_standard structure according to the
111 'id' and 'transmission' parameters. Returns negative on error. */
112int v4l2_video_std_construct(struct v4l2_standard *vs,
113 int id, const char *name)
114{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300115 vs->id = id;
116 v4l2_video_std_frame_period(id, &vs->frameperiod);
117 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300118 strlcpy(vs->name, name, sizeof(vs->name));
119 return 0;
120}
121EXPORT_SYMBOL(v4l2_video_std_construct);
122
123/* ----------------------------------------------------------------- */
124/* some arrays for pretty-printing debug messages of enum types */
125
126const char *v4l2_field_names[] = {
127 [V4L2_FIELD_ANY] = "any",
128 [V4L2_FIELD_NONE] = "none",
129 [V4L2_FIELD_TOP] = "top",
130 [V4L2_FIELD_BOTTOM] = "bottom",
131 [V4L2_FIELD_INTERLACED] = "interlaced",
132 [V4L2_FIELD_SEQ_TB] = "seq-tb",
133 [V4L2_FIELD_SEQ_BT] = "seq-bt",
134 [V4L2_FIELD_ALTERNATE] = "alternate",
135 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
136 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
137};
138EXPORT_SYMBOL(v4l2_field_names);
139
140const char *v4l2_type_names[] = {
141 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
142 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
143 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
144 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
145 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
146 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
147 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
148 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300149 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
150 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300151};
152EXPORT_SYMBOL(v4l2_type_names);
153
154static const char *v4l2_memory_names[] = {
155 [V4L2_MEMORY_MMAP] = "mmap",
156 [V4L2_MEMORY_USERPTR] = "userptr",
157 [V4L2_MEMORY_OVERLAY] = "overlay",
158};
159
160#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
161 arr[a] : "unknown")
162
163/* ------------------------------------------------------------------ */
164/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300165
Hans Verkuil59076122012-06-22 06:09:54 -0300166static void v4l_print_querycap(const void *arg, bool write_only)
167{
168 const struct v4l2_capability *p = arg;
169
170 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
171 "capabilities=0x%08x, device_caps=0x%08x\n",
172 p->driver, p->card, p->bus_info,
173 p->version, p->capabilities, p->device_caps);
174}
175
176static void v4l_print_enuminput(const void *arg, bool write_only)
177{
178 const struct v4l2_input *p = arg;
179
180 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
181 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
182 p->index, p->name, p->type, p->audioset, p->tuner,
183 (unsigned long long)p->std, p->status, p->capabilities);
184}
185
186static void v4l_print_enumoutput(const void *arg, bool write_only)
187{
188 const struct v4l2_output *p = arg;
189
190 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
191 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
192 p->index, p->name, p->type, p->audioset, p->modulator,
193 (unsigned long long)p->std, p->capabilities);
194}
195
196static void v4l_print_audio(const void *arg, bool write_only)
197{
198 const struct v4l2_audio *p = arg;
199
200 if (write_only)
201 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
202 else
203 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
204 p->index, p->name, p->capability, p->mode);
205}
206
207static void v4l_print_audioout(const void *arg, bool write_only)
208{
209 const struct v4l2_audioout *p = arg;
210
211 if (write_only)
212 pr_cont("index=%u\n", p->index);
213 else
214 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
215 p->index, p->name, p->capability, p->mode);
216}
217
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300218static void v4l_print_fmtdesc(const void *arg, bool write_only)
219{
220 const struct v4l2_fmtdesc *p = arg;
221
222 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
223 p->index, prt_names(p->type, v4l2_type_names),
224 p->flags, (p->pixelformat & 0xff),
225 (p->pixelformat >> 8) & 0xff,
226 (p->pixelformat >> 16) & 0xff,
227 (p->pixelformat >> 24) & 0xff,
228 p->description);
229}
230
231static void v4l_print_format(const void *arg, bool write_only)
232{
233 const struct v4l2_format *p = arg;
234 const struct v4l2_pix_format *pix;
235 const struct v4l2_pix_format_mplane *mp;
236 const struct v4l2_vbi_format *vbi;
237 const struct v4l2_sliced_vbi_format *sliced;
238 const struct v4l2_window *win;
239 const struct v4l2_clip *clip;
240 unsigned i;
241
242 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
243 switch (p->type) {
244 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
245 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
246 pix = &p->fmt.pix;
247 pr_cont(", width=%u, height=%u, "
248 "pixelformat=%c%c%c%c, field=%s, "
249 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
250 pix->width, pix->height,
251 (pix->pixelformat & 0xff),
252 (pix->pixelformat >> 8) & 0xff,
253 (pix->pixelformat >> 16) & 0xff,
254 (pix->pixelformat >> 24) & 0xff,
255 prt_names(pix->field, v4l2_field_names),
256 pix->bytesperline, pix->sizeimage,
257 pix->colorspace);
258 break;
259 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
260 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
261 mp = &p->fmt.pix_mp;
262 pr_cont(", width=%u, height=%u, "
263 "format=%c%c%c%c, field=%s, "
264 "colorspace=%d, num_planes=%u\n",
265 mp->width, mp->height,
266 (mp->pixelformat & 0xff),
267 (mp->pixelformat >> 8) & 0xff,
268 (mp->pixelformat >> 16) & 0xff,
269 (mp->pixelformat >> 24) & 0xff,
270 prt_names(mp->field, v4l2_field_names),
271 mp->colorspace, mp->num_planes);
272 for (i = 0; i < mp->num_planes; i++)
273 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
274 mp->plane_fmt[i].bytesperline,
275 mp->plane_fmt[i].sizeimage);
276 break;
277 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
278 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
279 win = &p->fmt.win;
280 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
281 "chromakey=0x%08x, bitmap=%p, "
282 "global_alpha=0x%02x\n",
283 win->w.width, win->w.height,
284 win->w.left, win->w.top,
285 prt_names(win->field, v4l2_field_names),
286 win->chromakey, win->bitmap, win->global_alpha);
287 clip = win->clips;
288 for (i = 0; i < win->clipcount; i++) {
289 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
290 i, clip->c.width, clip->c.height,
291 clip->c.left, clip->c.top);
292 clip = clip->next;
293 }
294 break;
295 case V4L2_BUF_TYPE_VBI_CAPTURE:
296 case V4L2_BUF_TYPE_VBI_OUTPUT:
297 vbi = &p->fmt.vbi;
298 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
299 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
300 vbi->sampling_rate, vbi->offset,
301 vbi->samples_per_line,
302 (vbi->sample_format & 0xff),
303 (vbi->sample_format >> 8) & 0xff,
304 (vbi->sample_format >> 16) & 0xff,
305 (vbi->sample_format >> 24) & 0xff,
306 vbi->start[0], vbi->start[1],
307 vbi->count[0], vbi->count[1]);
308 break;
309 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
310 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
311 sliced = &p->fmt.sliced;
312 pr_cont(", service_set=0x%08x, io_size=%d\n",
313 sliced->service_set, sliced->io_size);
314 for (i = 0; i < 24; i++)
315 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
316 sliced->service_lines[0][i],
317 sliced->service_lines[1][i]);
318 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300319 }
320}
321
322static void v4l_print_framebuffer(const void *arg, bool write_only)
323{
324 const struct v4l2_framebuffer *p = arg;
325
326 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
327 "height=%u, pixelformat=%c%c%c%c, "
328 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
329 p->capability, p->flags, p->base,
330 p->fmt.width, p->fmt.height,
331 (p->fmt.pixelformat & 0xff),
332 (p->fmt.pixelformat >> 8) & 0xff,
333 (p->fmt.pixelformat >> 16) & 0xff,
334 (p->fmt.pixelformat >> 24) & 0xff,
335 p->fmt.bytesperline, p->fmt.sizeimage,
336 p->fmt.colorspace);
337}
338
Hans Verkuile325b242012-06-09 12:50:15 -0300339static void v4l_print_buftype(const void *arg, bool write_only)
340{
341 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
342}
343
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300344static void v4l_print_modulator(const void *arg, bool write_only)
345{
346 const struct v4l2_modulator *p = arg;
347
348 if (write_only)
349 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
350 else
351 pr_cont("index=%u, name=%s, capability=0x%x, "
352 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
353 p->index, p->name, p->capability,
354 p->rangelow, p->rangehigh, p->txsubchans);
355}
356
357static void v4l_print_tuner(const void *arg, bool write_only)
358{
359 const struct v4l2_tuner *p = arg;
360
361 if (write_only)
362 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
363 else
364 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
365 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
366 "rxsubchans=0x%x, audmode=%u\n",
367 p->index, p->name, p->type,
368 p->capability, p->rangelow,
369 p->rangehigh, p->signal, p->afc,
370 p->rxsubchans, p->audmode);
371}
372
373static void v4l_print_frequency(const void *arg, bool write_only)
374{
375 const struct v4l2_frequency *p = arg;
376
377 pr_cont("tuner=%u, type=%u, frequency=%u\n",
378 p->tuner, p->type, p->frequency);
379}
380
381static void v4l_print_standard(const void *arg, bool write_only)
382{
383 const struct v4l2_standard *p = arg;
384
385 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
386 "framelines=%u\n", p->index,
387 (unsigned long long)p->id, p->name,
388 p->frameperiod.numerator,
389 p->frameperiod.denominator,
390 p->framelines);
391}
392
393static void v4l_print_std(const void *arg, bool write_only)
394{
395 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
396}
397
398static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
399{
400 const struct v4l2_hw_freq_seek *p = arg;
401
Hans Verkuil5e7883172012-08-01 16:18:41 -0300402 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
403 "rangelow=%u, rangehigh=%u\n",
404 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
405 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300406}
407
Hans Verkuileb3728e2012-06-22 06:23:59 -0300408static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300409{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300410 const struct v4l2_requestbuffers *p = arg;
411
412 pr_cont("count=%d, type=%s, memory=%s\n",
413 p->count,
414 prt_names(p->type, v4l2_type_names),
415 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300416}
417
Hans Verkuileb3728e2012-06-22 06:23:59 -0300418static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300419{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300420 const struct v4l2_buffer *p = arg;
421 const struct v4l2_timecode *tc = &p->timecode;
422 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300423 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300424
Hans Verkuileb3728e2012-06-22 06:23:59 -0300425 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
426 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300427 p->timestamp.tv_sec / 3600,
428 (int)(p->timestamp.tv_sec / 60) % 60,
429 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300430 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300431 p->index,
432 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300433 p->flags, prt_names(p->field, v4l2_field_names),
434 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300435
436 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300437 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300438 for (i = 0; i < p->length; ++i) {
439 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300440 printk(KERN_DEBUG
441 "plane %d: bytesused=%d, data_offset=0x%08x "
442 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300443 i, plane->bytesused, plane->data_offset,
444 plane->m.userptr, plane->length);
445 }
446 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300447 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300448 p->bytesused, p->m.userptr, p->length);
449 }
450
Hans Verkuileb3728e2012-06-22 06:23:59 -0300451 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
452 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300453 tc->hours, tc->minutes, tc->seconds,
454 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
455}
456
Hans Verkuileb3728e2012-06-22 06:23:59 -0300457static void v4l_print_create_buffers(const void *arg, bool write_only)
458{
459 const struct v4l2_create_buffers *p = arg;
460
461 pr_cont("index=%d, count=%d, memory=%s, ",
462 p->index, p->count,
463 prt_names(p->memory, v4l2_memory_names));
464 v4l_print_format(&p->format, write_only);
465}
466
467static void v4l_print_streamparm(const void *arg, bool write_only)
468{
469 const struct v4l2_streamparm *p = arg;
470
471 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
472
473 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
474 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
475 const struct v4l2_captureparm *c = &p->parm.capture;
476
477 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
478 "extendedmode=%d, readbuffers=%d\n",
479 c->capability, c->capturemode,
480 c->timeperframe.numerator, c->timeperframe.denominator,
481 c->extendedmode, c->readbuffers);
482 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
483 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
484 const struct v4l2_outputparm *c = &p->parm.output;
485
486 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
487 "extendedmode=%d, writebuffers=%d\n",
488 c->capability, c->outputmode,
489 c->timeperframe.numerator, c->timeperframe.denominator,
490 c->extendedmode, c->writebuffers);
491 }
492}
493
Hans Verkuilefbceec2012-06-09 12:54:02 -0300494static void v4l_print_queryctrl(const void *arg, bool write_only)
495{
496 const struct v4l2_queryctrl *p = arg;
497
498 pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
499 "step=%d, default=%d, flags=0x%08x\n",
500 p->id, p->type, p->name,
501 p->minimum, p->maximum,
502 p->step, p->default_value, p->flags);
503}
504
505static void v4l_print_querymenu(const void *arg, bool write_only)
506{
507 const struct v4l2_querymenu *p = arg;
508
509 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
510}
511
512static void v4l_print_control(const void *arg, bool write_only)
513{
514 const struct v4l2_control *p = arg;
515
516 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
517}
518
519static void v4l_print_ext_controls(const void *arg, bool write_only)
520{
521 const struct v4l2_ext_controls *p = arg;
522 int i;
523
524 pr_cont("class=0x%x, count=%d, error_idx=%d",
525 p->ctrl_class, p->count, p->error_idx);
526 for (i = 0; i < p->count; i++) {
527 if (p->controls[i].size)
528 pr_cont(", id/val=0x%x/0x%x",
529 p->controls[i].id, p->controls[i].value);
530 else
531 pr_cont(", id/size=0x%x/%u",
532 p->controls[i].id, p->controls[i].size);
533 }
534 pr_cont("\n");
535}
536
Hans Verkuild84f2d942012-06-09 11:57:46 -0300537static void v4l_print_cropcap(const void *arg, bool write_only)
538{
539 const struct v4l2_cropcap *p = arg;
540
541 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
542 "defrect wxh=%dx%d, x,y=%d,%d\n, "
543 "pixelaspect %d/%d\n",
544 prt_names(p->type, v4l2_type_names),
545 p->bounds.width, p->bounds.height,
546 p->bounds.left, p->bounds.top,
547 p->defrect.width, p->defrect.height,
548 p->defrect.left, p->defrect.top,
549 p->pixelaspect.numerator, p->pixelaspect.denominator);
550}
551
552static void v4l_print_crop(const void *arg, bool write_only)
553{
554 const struct v4l2_crop *p = arg;
555
556 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
557 prt_names(p->type, v4l2_type_names),
558 p->c.width, p->c.height,
559 p->c.left, p->c.top);
560}
561
562static void v4l_print_selection(const void *arg, bool write_only)
563{
564 const struct v4l2_selection *p = arg;
565
566 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
567 prt_names(p->type, v4l2_type_names),
568 p->target, p->flags,
569 p->r.width, p->r.height, p->r.left, p->r.top);
570}
571
Hans Verkuild806f2d2012-06-09 10:02:49 -0300572static void v4l_print_jpegcompression(const void *arg, bool write_only)
573{
574 const struct v4l2_jpegcompression *p = arg;
575
576 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
577 "COM_len=%d, jpeg_markers=0x%x\n",
578 p->quality, p->APPn, p->APP_len,
579 p->COM_len, p->jpeg_markers);
580}
581
582static void v4l_print_enc_idx(const void *arg, bool write_only)
583{
584 const struct v4l2_enc_idx *p = arg;
585
586 pr_cont("entries=%d, entries_cap=%d\n",
587 p->entries, p->entries_cap);
588}
589
590static void v4l_print_encoder_cmd(const void *arg, bool write_only)
591{
592 const struct v4l2_encoder_cmd *p = arg;
593
594 pr_cont("cmd=%d, flags=0x%x\n",
595 p->cmd, p->flags);
596}
597
598static void v4l_print_decoder_cmd(const void *arg, bool write_only)
599{
600 const struct v4l2_decoder_cmd *p = arg;
601
602 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
603
604 if (p->cmd == V4L2_DEC_CMD_START)
605 pr_info("speed=%d, format=%u\n",
606 p->start.speed, p->start.format);
607 else if (p->cmd == V4L2_DEC_CMD_STOP)
608 pr_info("pts=%llu\n", p->stop.pts);
609}
610
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300611static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
612{
613 const struct v4l2_dbg_chip_ident *p = arg;
614
615 pr_cont("type=%u, ", p->match.type);
616 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
617 pr_cont("name=%s, ", p->match.name);
618 else
619 pr_cont("addr=%u, ", p->match.addr);
620 pr_cont("chip_ident=%u, revision=0x%x\n",
621 p->ident, p->revision);
622}
623
624static void v4l_print_dbg_register(const void *arg, bool write_only)
625{
626 const struct v4l2_dbg_register *p = arg;
627
628 pr_cont("type=%u, ", p->match.type);
629 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
630 pr_cont("name=%s, ", p->match.name);
631 else
632 pr_cont("addr=%u, ", p->match.addr);
633 pr_cont("reg=0x%llx, val=0x%llx\n",
634 p->reg, p->val);
635}
636
Hans Verkuilefc626b2012-06-09 10:09:07 -0300637static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300638{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300639 const struct v4l2_dv_enum_preset *p = arg;
640
641 pr_cont("index=%u, preset=%u, name=%s, width=%u, height=%u\n",
642 p->index, p->preset, p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300643}
644
Hans Verkuilefc626b2012-06-09 10:09:07 -0300645static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300646{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300647 const struct v4l2_dv_preset *p = arg;
648
649 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300650}
651
Hans Verkuilefc626b2012-06-09 10:09:07 -0300652static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300653{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300654 const struct v4l2_dv_timings *p = arg;
655
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300656 switch (p->type) {
657 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300658 pr_cont("type=bt-656/1120, interlaced=%u, "
659 "pixelclock=%llu, "
660 "width=%u, height=%u, polarities=0x%x, "
661 "hfrontporch=%u, hsync=%u, "
662 "hbackporch=%u, vfrontporch=%u, "
663 "vsync=%u, vbackporch=%u, "
664 "il_vfrontporch=%u, il_vsync=%u, "
665 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300666 p->bt.interlaced, p->bt.pixelclock,
667 p->bt.width, p->bt.height,
668 p->bt.polarities, p->bt.hfrontporch,
669 p->bt.hsync, p->bt.hbackporch,
670 p->bt.vfrontporch, p->bt.vsync,
671 p->bt.vbackporch, p->bt.il_vfrontporch,
672 p->bt.il_vsync, p->bt.il_vbackporch,
673 p->bt.standards, p->bt.flags);
674 break;
675 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300676 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300677 break;
678 }
679}
680
Hans Verkuilefc626b2012-06-09 10:09:07 -0300681static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
682{
683 const struct v4l2_enum_dv_timings *p = arg;
684
685 pr_cont("index=%u, ", p->index);
686 v4l_print_dv_timings(&p->timings, write_only);
687}
688
689static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
690{
691 const struct v4l2_dv_timings_cap *p = arg;
692
693 switch (p->type) {
694 case V4L2_DV_BT_656_1120:
695 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
696 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
697 p->bt.min_width, p->bt.max_width,
698 p->bt.min_height, p->bt.max_height,
699 p->bt.min_pixelclock, p->bt.max_pixelclock,
700 p->bt.standards, p->bt.capabilities);
701 break;
702 default:
703 pr_cont("type=%u\n", p->type);
704 break;
705 }
706}
707
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300708static void v4l_print_frmsizeenum(const void *arg, bool write_only)
709{
710 const struct v4l2_frmsizeenum *p = arg;
711
712 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
713 p->index,
714 (p->pixel_format & 0xff),
715 (p->pixel_format >> 8) & 0xff,
716 (p->pixel_format >> 16) & 0xff,
717 (p->pixel_format >> 24) & 0xff,
718 p->type);
719 switch (p->type) {
720 case V4L2_FRMSIZE_TYPE_DISCRETE:
721 pr_cont(" wxh=%ux%u\n",
722 p->discrete.width, p->discrete.height);
723 break;
724 case V4L2_FRMSIZE_TYPE_STEPWISE:
725 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
726 p->stepwise.min_width, p->stepwise.min_height,
727 p->stepwise.step_width, p->stepwise.step_height,
728 p->stepwise.max_width, p->stepwise.max_height);
729 break;
730 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
731 /* fall through */
732 default:
733 pr_cont("\n");
734 break;
735 }
736}
737
738static void v4l_print_frmivalenum(const void *arg, bool write_only)
739{
740 const struct v4l2_frmivalenum *p = arg;
741
742 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
743 p->index,
744 (p->pixel_format & 0xff),
745 (p->pixel_format >> 8) & 0xff,
746 (p->pixel_format >> 16) & 0xff,
747 (p->pixel_format >> 24) & 0xff,
748 p->width, p->height, p->type);
749 switch (p->type) {
750 case V4L2_FRMIVAL_TYPE_DISCRETE:
751 pr_cont(" fps=%d/%d\n",
752 p->discrete.numerator,
753 p->discrete.denominator);
754 break;
755 case V4L2_FRMIVAL_TYPE_STEPWISE:
756 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
757 p->stepwise.min.numerator,
758 p->stepwise.min.denominator,
759 p->stepwise.max.numerator,
760 p->stepwise.max.denominator,
761 p->stepwise.step.numerator,
762 p->stepwise.step.denominator);
763 break;
764 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
765 /* fall through */
766 default:
767 pr_cont("\n");
768 break;
769 }
770}
771
772static void v4l_print_event(const void *arg, bool write_only)
773{
774 const struct v4l2_event *p = arg;
775 const struct v4l2_event_ctrl *c;
776
777 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
778 "timestamp=%lu.%9.9lu\n",
779 p->type, p->pending, p->sequence, p->id,
780 p->timestamp.tv_sec, p->timestamp.tv_nsec);
781 switch (p->type) {
782 case V4L2_EVENT_VSYNC:
783 printk(KERN_DEBUG "field=%s\n",
784 prt_names(p->u.vsync.field, v4l2_field_names));
785 break;
786 case V4L2_EVENT_CTRL:
787 c = &p->u.ctrl;
788 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
789 c->changes, c->type);
790 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
791 pr_cont("value64=%lld, ", c->value64);
792 else
793 pr_cont("value=%d, ", c->value);
794 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
795 " default_value=%d\n",
796 c->flags, c->minimum, c->maximum,
797 c->step, c->default_value);
798 break;
799 case V4L2_EVENT_FRAME_SYNC:
800 pr_cont("frame_sequence=%u\n",
801 p->u.frame_sync.frame_sequence);
802 break;
803 }
804}
805
806static void v4l_print_event_subscription(const void *arg, bool write_only)
807{
808 const struct v4l2_event_subscription *p = arg;
809
810 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
811 p->type, p->id, p->flags);
812}
813
814static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
815{
816 const struct v4l2_sliced_vbi_cap *p = arg;
817 int i;
818
819 pr_cont("type=%s, service_set=0x%08x\n",
820 prt_names(p->type, v4l2_type_names), p->service_set);
821 for (i = 0; i < 24; i++)
822 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
823 p->service_lines[0][i],
824 p->service_lines[1][i]);
825}
826
Hans Verkuil82b655b2012-07-05 06:37:08 -0300827static void v4l_print_freq_band(const void *arg, bool write_only)
828{
829 const struct v4l2_frequency_band *p = arg;
830
831 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
832 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
833 p->tuner, p->type, p->index,
834 p->capability, p->rangelow,
835 p->rangehigh, p->modulation);
836}
837
Hans Verkuilefc626b2012-06-09 10:09:07 -0300838static void v4l_print_u32(const void *arg, bool write_only)
839{
840 pr_cont("value=%u\n", *(const u32 *)arg);
841}
842
843static void v4l_print_newline(const void *arg, bool write_only)
844{
845 pr_cont("\n");
846}
847
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300848static void v4l_print_default(const void *arg, bool write_only)
849{
850 pr_cont("driver-specific ioctl\n");
851}
852
Hans Verkuilefbceec2012-06-09 12:54:02 -0300853static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300854{
855 __u32 i;
856
857 /* zero the reserved fields */
858 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300859 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300860 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300861
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300862 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
863 when using extended controls.
864 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
865 is it allowed for backwards compatibility.
866 */
867 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
868 return 0;
869 /* Check that all controls are from the same control class. */
870 for (i = 0; i < c->count; i++) {
871 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
872 c->error_idx = i;
873 return 0;
874 }
875 }
876 return 1;
877}
878
Hans Verkuila3998102008-07-21 02:57:38 -0300879static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300880{
Hans Verkuila3998102008-07-21 02:57:38 -0300881 if (ops == NULL)
882 return -EINVAL;
883
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300884 switch (type) {
885 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300886 if (ops->vidioc_g_fmt_vid_cap ||
887 ops->vidioc_g_fmt_vid_cap_mplane)
888 return 0;
889 break;
890 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
891 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300892 return 0;
893 break;
894 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300895 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300896 return 0;
897 break;
898 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300899 if (ops->vidioc_g_fmt_vid_out ||
900 ops->vidioc_g_fmt_vid_out_mplane)
901 return 0;
902 break;
903 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
904 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300905 return 0;
906 break;
907 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300908 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300909 return 0;
910 break;
911 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300912 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300913 return 0;
914 break;
915 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300916 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300917 return 0;
918 break;
919 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300920 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300921 return 0;
922 break;
923 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300924 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300925 return 0;
926 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300927 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300928 break;
929 }
930 return -EINVAL;
931}
932
Hans Verkuil59076122012-06-22 06:09:54 -0300933static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
934 struct file *file, void *fh, void *arg)
935{
936 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
937
938 cap->version = LINUX_VERSION_CODE;
939 return ops->vidioc_querycap(file, fh, cap);
940}
941
942static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
943 struct file *file, void *fh, void *arg)
944{
945 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
946}
947
948static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
949 struct file *file, void *fh, void *arg)
950{
951 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
952}
953
Hans Verkuild0547cb2012-06-09 09:27:10 -0300954static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
955 struct file *file, void *fh, void *arg)
956{
957 struct video_device *vfd;
958 u32 *p = arg;
959
960 if (ops->vidioc_g_priority)
961 return ops->vidioc_g_priority(file, fh, arg);
962 vfd = video_devdata(file);
963 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
964 return 0;
965}
966
967static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
968 struct file *file, void *fh, void *arg)
969{
970 struct video_device *vfd;
971 struct v4l2_fh *vfh;
972 u32 *p = arg;
973
974 if (ops->vidioc_s_priority)
975 return ops->vidioc_s_priority(file, fh, *p);
976 vfd = video_devdata(file);
977 vfh = file->private_data;
978 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
979}
980
Hans Verkuil59076122012-06-22 06:09:54 -0300981static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
982 struct file *file, void *fh, void *arg)
983{
984 struct v4l2_input *p = arg;
985
986 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -0300987 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -0300988 * CAP_STD here based on ioctl handler provided by the
989 * driver. If the driver doesn't support these
990 * for a specific input, it must override these flags.
991 */
992 if (ops->vidioc_s_std)
993 p->capabilities |= V4L2_IN_CAP_STD;
994 if (ops->vidioc_s_dv_preset)
995 p->capabilities |= V4L2_IN_CAP_PRESETS;
996 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c92012-09-03 10:38:41 -0300997 p->capabilities |= V4L2_IN_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -0300998
999 return ops->vidioc_enum_input(file, fh, p);
1000}
1001
1002static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1003 struct file *file, void *fh, void *arg)
1004{
1005 struct v4l2_output *p = arg;
1006
1007 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001008 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001009 * CAP_STD here based on ioctl handler provided by the
1010 * driver. If the driver doesn't support these
1011 * for a specific output, it must override these flags.
1012 */
1013 if (ops->vidioc_s_std)
1014 p->capabilities |= V4L2_OUT_CAP_STD;
1015 if (ops->vidioc_s_dv_preset)
1016 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1017 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001018 p->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001019
1020 return ops->vidioc_enum_output(file, fh, p);
1021}
1022
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001023static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1024 struct file *file, void *fh, void *arg)
1025{
1026 struct v4l2_fmtdesc *p = arg;
1027
1028 switch (p->type) {
1029 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1030 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1031 break;
1032 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1033 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1034 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1035 break;
1036 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1037 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1038 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1039 break;
1040 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1041 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1042 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1043 break;
1044 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1045 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1046 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1047 break;
1048 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001049 }
1050 return -EINVAL;
1051}
1052
1053static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1054 struct file *file, void *fh, void *arg)
1055{
1056 struct v4l2_format *p = arg;
1057
1058 switch (p->type) {
1059 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1060 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1061 break;
1062 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1063 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1064 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
1065 break;
1066 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1067 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1068 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
1069 break;
1070 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1071 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1072 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1073 break;
1074 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1075 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1076 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
1077 break;
1078 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1079 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1080 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
1081 break;
1082 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1083 case V4L2_BUF_TYPE_VBI_CAPTURE:
1084 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
1085 break;
1086 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1087 case V4L2_BUF_TYPE_VBI_OUTPUT:
1088 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
1089 break;
1090 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1091 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1092 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
1093 break;
1094 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1095 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1096 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
1097 break;
1098 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001099 }
1100 return -EINVAL;
1101}
1102
1103static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1104 struct file *file, void *fh, void *arg)
1105{
1106 struct v4l2_format *p = arg;
1107
1108 switch (p->type) {
1109 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1110 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1111 break;
1112 CLEAR_AFTER_FIELD(p, fmt.pix);
1113 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1114 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1115 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1116 break;
1117 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1118 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1119 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1120 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1121 break;
1122 CLEAR_AFTER_FIELD(p, fmt.win);
1123 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1124 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1125 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1126 break;
1127 CLEAR_AFTER_FIELD(p, fmt.pix);
1128 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1129 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1130 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1131 break;
1132 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1133 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1134 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1135 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1136 break;
1137 CLEAR_AFTER_FIELD(p, fmt.win);
1138 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1139 case V4L2_BUF_TYPE_VBI_CAPTURE:
1140 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1141 break;
1142 CLEAR_AFTER_FIELD(p, fmt.vbi);
1143 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1144 case V4L2_BUF_TYPE_VBI_OUTPUT:
1145 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1146 break;
1147 CLEAR_AFTER_FIELD(p, fmt.vbi);
1148 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1149 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1150 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1151 break;
1152 CLEAR_AFTER_FIELD(p, fmt.sliced);
1153 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1154 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1155 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1156 break;
1157 CLEAR_AFTER_FIELD(p, fmt.sliced);
1158 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001159 }
1160 return -EINVAL;
1161}
1162
1163static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1164 struct file *file, void *fh, void *arg)
1165{
1166 struct v4l2_format *p = arg;
1167
1168 switch (p->type) {
1169 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1170 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1171 break;
1172 CLEAR_AFTER_FIELD(p, fmt.pix);
1173 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1174 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1175 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1176 break;
1177 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1178 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1179 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1180 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1181 break;
1182 CLEAR_AFTER_FIELD(p, fmt.win);
1183 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1184 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1185 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1186 break;
1187 CLEAR_AFTER_FIELD(p, fmt.pix);
1188 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1189 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1190 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1191 break;
1192 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1193 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1194 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1195 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1196 break;
1197 CLEAR_AFTER_FIELD(p, fmt.win);
1198 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1199 case V4L2_BUF_TYPE_VBI_CAPTURE:
1200 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1201 break;
1202 CLEAR_AFTER_FIELD(p, fmt.vbi);
1203 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1204 case V4L2_BUF_TYPE_VBI_OUTPUT:
1205 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1206 break;
1207 CLEAR_AFTER_FIELD(p, fmt.vbi);
1208 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1209 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1210 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1211 break;
1212 CLEAR_AFTER_FIELD(p, fmt.sliced);
1213 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1214 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1215 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1216 break;
1217 CLEAR_AFTER_FIELD(p, fmt.sliced);
1218 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001219 }
1220 return -EINVAL;
1221}
1222
Hans Verkuile325b242012-06-09 12:50:15 -03001223static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1224 struct file *file, void *fh, void *arg)
1225{
1226 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1227}
1228
1229static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1230 struct file *file, void *fh, void *arg)
1231{
1232 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1233}
1234
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001235static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1236 struct file *file, void *fh, void *arg)
1237{
1238 struct video_device *vfd = video_devdata(file);
1239 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001240 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001241
1242 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1243 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001244 err = ops->vidioc_g_tuner(file, fh, p);
1245 if (!err)
1246 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1247 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001248}
1249
1250static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1251 struct file *file, void *fh, void *arg)
1252{
1253 struct video_device *vfd = video_devdata(file);
1254 struct v4l2_tuner *p = arg;
1255
1256 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1257 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1258 return ops->vidioc_s_tuner(file, fh, p);
1259}
1260
Hans Verkuil82b655b2012-07-05 06:37:08 -03001261static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1262 struct file *file, void *fh, void *arg)
1263{
1264 struct v4l2_modulator *p = arg;
1265 int err;
1266
1267 err = ops->vidioc_g_modulator(file, fh, p);
1268 if (!err)
1269 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1270 return err;
1271}
1272
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001273static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1274 struct file *file, void *fh, void *arg)
1275{
1276 struct video_device *vfd = video_devdata(file);
1277 struct v4l2_frequency *p = arg;
1278
1279 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1280 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1281 return ops->vidioc_g_frequency(file, fh, p);
1282}
1283
1284static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1285 struct file *file, void *fh, void *arg)
1286{
1287 struct video_device *vfd = video_devdata(file);
1288 struct v4l2_frequency *p = arg;
1289 enum v4l2_tuner_type type;
1290
1291 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1292 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1293 if (p->type != type)
1294 return -EINVAL;
1295 return ops->vidioc_s_frequency(file, fh, p);
1296}
1297
1298static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1299 struct file *file, void *fh, void *arg)
1300{
1301 struct video_device *vfd = video_devdata(file);
1302 struct v4l2_standard *p = arg;
1303 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1304 unsigned int index = p->index, i, j = 0;
1305 const char *descr = "";
1306
Hans Verkuila5338192012-09-14 06:45:43 -03001307 /* Return -ENODATA if the tvnorms for the current input
1308 or output is 0, meaning that it doesn't support this API. */
1309 if (id == 0)
1310 return -ENODATA;
1311
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001312 /* Return norm array in a canonical way */
1313 for (i = 0; i <= index && id; i++) {
1314 /* last std value in the standards array is 0, so this
1315 while always ends there since (id & 0) == 0. */
1316 while ((id & standards[j].std) != standards[j].std)
1317 j++;
1318 curr_id = standards[j].std;
1319 descr = standards[j].descr;
1320 j++;
1321 if (curr_id == 0)
1322 break;
1323 if (curr_id != V4L2_STD_PAL &&
1324 curr_id != V4L2_STD_SECAM &&
1325 curr_id != V4L2_STD_NTSC)
1326 id &= ~curr_id;
1327 }
1328 if (i <= index)
1329 return -EINVAL;
1330
1331 v4l2_video_std_construct(p, curr_id, descr);
1332 return 0;
1333}
1334
1335static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1336 struct file *file, void *fh, void *arg)
1337{
1338 struct video_device *vfd = video_devdata(file);
1339 v4l2_std_id *id = arg;
1340
1341 /* Calls the specific handler */
1342 if (ops->vidioc_g_std)
1343 return ops->vidioc_g_std(file, fh, arg);
1344 if (vfd->current_norm) {
1345 *id = vfd->current_norm;
1346 return 0;
1347 }
1348 return -ENOTTY;
1349}
1350
1351static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1352 struct file *file, void *fh, void *arg)
1353{
1354 struct video_device *vfd = video_devdata(file);
1355 v4l2_std_id *id = arg, norm;
1356 int ret;
1357
1358 norm = (*id) & vfd->tvnorms;
1359 if (vfd->tvnorms && !norm) /* Check if std is supported */
1360 return -EINVAL;
1361
1362 /* Calls the specific handler */
1363 ret = ops->vidioc_s_std(file, fh, &norm);
1364
1365 /* Updates standard information */
1366 if (ret >= 0)
1367 vfd->current_norm = norm;
1368 return ret;
1369}
1370
1371static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1372 struct file *file, void *fh, void *arg)
1373{
1374 struct video_device *vfd = video_devdata(file);
1375 v4l2_std_id *p = arg;
1376
1377 /*
1378 * If nothing detected, it should return all supported
1379 * standard.
1380 * Drivers just need to mask the std argument, in order
1381 * to remove the standards that don't apply from the mask.
1382 * This means that tuners, audio and video decoders can join
1383 * their efforts to improve the standards detection.
1384 */
1385 *p = vfd->tvnorms;
1386 return ops->vidioc_querystd(file, fh, arg);
1387}
1388
1389static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1390 struct file *file, void *fh, void *arg)
1391{
1392 struct video_device *vfd = video_devdata(file);
1393 struct v4l2_hw_freq_seek *p = arg;
1394 enum v4l2_tuner_type type;
1395
1396 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1397 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1398 if (p->type != type)
1399 return -EINVAL;
1400 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1401}
1402
Hans Verkuileb3728e2012-06-22 06:23:59 -03001403static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1404 struct file *file, void *fh, void *arg)
1405{
1406 struct v4l2_requestbuffers *p = arg;
1407 int ret = check_fmt(ops, p->type);
1408
1409 if (ret)
1410 return ret;
1411
Hans Verkuil633c98e2012-09-17 05:02:26 -03001412 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001413
1414 return ops->vidioc_reqbufs(file, fh, p);
1415}
1416
1417static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1418 struct file *file, void *fh, void *arg)
1419{
1420 struct v4l2_buffer *p = arg;
1421 int ret = check_fmt(ops, p->type);
1422
1423 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1424}
1425
1426static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1427 struct file *file, void *fh, void *arg)
1428{
1429 struct v4l2_buffer *p = arg;
1430 int ret = check_fmt(ops, p->type);
1431
1432 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1433}
1434
1435static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1436 struct file *file, void *fh, void *arg)
1437{
1438 struct v4l2_buffer *p = arg;
1439 int ret = check_fmt(ops, p->type);
1440
1441 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1442}
1443
1444static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1445 struct file *file, void *fh, void *arg)
1446{
1447 struct v4l2_create_buffers *create = arg;
1448 int ret = check_fmt(ops, create->format.type);
1449
1450 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1451}
1452
1453static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1454 struct file *file, void *fh, void *arg)
1455{
1456 struct v4l2_buffer *b = arg;
1457 int ret = check_fmt(ops, b->type);
1458
1459 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1460}
1461
1462static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1463 struct file *file, void *fh, void *arg)
1464{
1465 struct video_device *vfd = video_devdata(file);
1466 struct v4l2_streamparm *p = arg;
1467 v4l2_std_id std;
1468 int ret = check_fmt(ops, p->type);
1469
1470 if (ret)
1471 return ret;
1472 if (ops->vidioc_g_parm)
1473 return ops->vidioc_g_parm(file, fh, p);
1474 std = vfd->current_norm;
1475 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1476 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1477 return -EINVAL;
1478 p->parm.capture.readbuffers = 2;
1479 if (ops->vidioc_g_std)
1480 ret = ops->vidioc_g_std(file, fh, &std);
1481 if (ret == 0)
1482 v4l2_video_std_frame_period(std,
1483 &p->parm.capture.timeperframe);
1484 return ret;
1485}
1486
1487static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1488 struct file *file, void *fh, void *arg)
1489{
1490 struct v4l2_streamparm *p = arg;
1491 int ret = check_fmt(ops, p->type);
1492
1493 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1494}
1495
Hans Verkuilefbceec2012-06-09 12:54:02 -03001496static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1497 struct file *file, void *fh, void *arg)
1498{
1499 struct video_device *vfd = video_devdata(file);
1500 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001501 struct v4l2_fh *vfh =
1502 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001503
1504 if (vfh && vfh->ctrl_handler)
1505 return v4l2_queryctrl(vfh->ctrl_handler, p);
1506 if (vfd->ctrl_handler)
1507 return v4l2_queryctrl(vfd->ctrl_handler, p);
1508 if (ops->vidioc_queryctrl)
1509 return ops->vidioc_queryctrl(file, fh, p);
1510 return -ENOTTY;
1511}
1512
1513static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1514 struct file *file, void *fh, void *arg)
1515{
1516 struct video_device *vfd = video_devdata(file);
1517 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001518 struct v4l2_fh *vfh =
1519 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001520
1521 if (vfh && vfh->ctrl_handler)
1522 return v4l2_querymenu(vfh->ctrl_handler, p);
1523 if (vfd->ctrl_handler)
1524 return v4l2_querymenu(vfd->ctrl_handler, p);
1525 if (ops->vidioc_querymenu)
1526 return ops->vidioc_querymenu(file, fh, p);
1527 return -ENOTTY;
1528}
1529
1530static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1531 struct file *file, void *fh, void *arg)
1532{
1533 struct video_device *vfd = video_devdata(file);
1534 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001535 struct v4l2_fh *vfh =
1536 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001537 struct v4l2_ext_controls ctrls;
1538 struct v4l2_ext_control ctrl;
1539
1540 if (vfh && vfh->ctrl_handler)
1541 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1542 if (vfd->ctrl_handler)
1543 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1544 if (ops->vidioc_g_ctrl)
1545 return ops->vidioc_g_ctrl(file, fh, p);
1546 if (ops->vidioc_g_ext_ctrls == NULL)
1547 return -ENOTTY;
1548
1549 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1550 ctrls.count = 1;
1551 ctrls.controls = &ctrl;
1552 ctrl.id = p->id;
1553 ctrl.value = p->value;
1554 if (check_ext_ctrls(&ctrls, 1)) {
1555 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1556
1557 if (ret == 0)
1558 p->value = ctrl.value;
1559 return ret;
1560 }
1561 return -EINVAL;
1562}
1563
1564static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1565 struct file *file, void *fh, void *arg)
1566{
1567 struct video_device *vfd = video_devdata(file);
1568 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001569 struct v4l2_fh *vfh =
1570 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001571 struct v4l2_ext_controls ctrls;
1572 struct v4l2_ext_control ctrl;
1573
1574 if (vfh && vfh->ctrl_handler)
1575 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1576 if (vfd->ctrl_handler)
1577 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1578 if (ops->vidioc_s_ctrl)
1579 return ops->vidioc_s_ctrl(file, fh, p);
1580 if (ops->vidioc_s_ext_ctrls == NULL)
1581 return -ENOTTY;
1582
1583 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1584 ctrls.count = 1;
1585 ctrls.controls = &ctrl;
1586 ctrl.id = p->id;
1587 ctrl.value = p->value;
1588 if (check_ext_ctrls(&ctrls, 1))
1589 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1590 return -EINVAL;
1591}
1592
1593static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1594 struct file *file, void *fh, void *arg)
1595{
1596 struct video_device *vfd = video_devdata(file);
1597 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001598 struct v4l2_fh *vfh =
1599 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001600
1601 p->error_idx = p->count;
1602 if (vfh && vfh->ctrl_handler)
1603 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1604 if (vfd->ctrl_handler)
1605 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1606 if (ops->vidioc_g_ext_ctrls == NULL)
1607 return -ENOTTY;
1608 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1609 -EINVAL;
1610}
1611
1612static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1613 struct file *file, void *fh, void *arg)
1614{
1615 struct video_device *vfd = video_devdata(file);
1616 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001617 struct v4l2_fh *vfh =
1618 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001619
1620 p->error_idx = p->count;
1621 if (vfh && vfh->ctrl_handler)
1622 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1623 if (vfd->ctrl_handler)
1624 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1625 if (ops->vidioc_s_ext_ctrls == NULL)
1626 return -ENOTTY;
1627 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1628 -EINVAL;
1629}
1630
1631static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1632 struct file *file, void *fh, void *arg)
1633{
1634 struct video_device *vfd = video_devdata(file);
1635 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001636 struct v4l2_fh *vfh =
1637 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001638
1639 p->error_idx = p->count;
1640 if (vfh && vfh->ctrl_handler)
1641 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1642 if (vfd->ctrl_handler)
1643 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1644 if (ops->vidioc_try_ext_ctrls == NULL)
1645 return -ENOTTY;
1646 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1647 -EINVAL;
1648}
1649
Hans Verkuild84f2d942012-06-09 11:57:46 -03001650static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1651 struct file *file, void *fh, void *arg)
1652{
1653 struct v4l2_crop *p = arg;
1654 struct v4l2_selection s = {
1655 .type = p->type,
1656 };
1657 int ret;
1658
1659 if (ops->vidioc_g_crop)
1660 return ops->vidioc_g_crop(file, fh, p);
1661 /* simulate capture crop using selection api */
1662
1663 /* crop means compose for output devices */
1664 if (V4L2_TYPE_IS_OUTPUT(p->type))
1665 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1666 else
1667 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1668
1669 ret = ops->vidioc_g_selection(file, fh, &s);
1670
1671 /* copying results to old structure on success */
1672 if (!ret)
1673 p->c = s.r;
1674 return ret;
1675}
1676
1677static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1678 struct file *file, void *fh, void *arg)
1679{
1680 struct v4l2_crop *p = arg;
1681 struct v4l2_selection s = {
1682 .type = p->type,
1683 .r = p->c,
1684 };
1685
1686 if (ops->vidioc_s_crop)
1687 return ops->vidioc_s_crop(file, fh, p);
1688 /* simulate capture crop using selection api */
1689
1690 /* crop means compose for output devices */
1691 if (V4L2_TYPE_IS_OUTPUT(p->type))
1692 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1693 else
1694 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1695
1696 return ops->vidioc_s_selection(file, fh, &s);
1697}
1698
1699static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1700 struct file *file, void *fh, void *arg)
1701{
1702 struct v4l2_cropcap *p = arg;
1703 struct v4l2_selection s = { .type = p->type };
1704 int ret;
1705
1706 if (ops->vidioc_cropcap)
1707 return ops->vidioc_cropcap(file, fh, p);
1708
1709 /* obtaining bounds */
1710 if (V4L2_TYPE_IS_OUTPUT(p->type))
1711 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1712 else
1713 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1714
1715 ret = ops->vidioc_g_selection(file, fh, &s);
1716 if (ret)
1717 return ret;
1718 p->bounds = s.r;
1719
1720 /* obtaining defrect */
1721 if (V4L2_TYPE_IS_OUTPUT(p->type))
1722 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1723 else
1724 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1725
1726 ret = ops->vidioc_g_selection(file, fh, &s);
1727 if (ret)
1728 return ret;
1729 p->defrect = s.r;
1730
1731 /* setting trivial pixelaspect */
1732 p->pixelaspect.numerator = 1;
1733 p->pixelaspect.denominator = 1;
1734 return 0;
1735}
1736
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001737static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1738 struct file *file, void *fh, void *arg)
1739{
1740 struct video_device *vfd = video_devdata(file);
1741 int ret;
1742
1743 if (vfd->v4l2_dev)
1744 pr_info("%s: ================= START STATUS =================\n",
1745 vfd->v4l2_dev->name);
1746 ret = ops->vidioc_log_status(file, fh);
1747 if (vfd->v4l2_dev)
1748 pr_info("%s: ================== END STATUS ==================\n",
1749 vfd->v4l2_dev->name);
1750 return ret;
1751}
1752
1753static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1754 struct file *file, void *fh, void *arg)
1755{
1756#ifdef CONFIG_VIDEO_ADV_DEBUG
1757 struct v4l2_dbg_register *p = arg;
1758
1759 if (!capable(CAP_SYS_ADMIN))
1760 return -EPERM;
1761 return ops->vidioc_g_register(file, fh, p);
1762#else
1763 return -ENOTTY;
1764#endif
1765}
1766
1767static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1768 struct file *file, void *fh, void *arg)
1769{
1770#ifdef CONFIG_VIDEO_ADV_DEBUG
1771 struct v4l2_dbg_register *p = arg;
1772
1773 if (!capable(CAP_SYS_ADMIN))
1774 return -EPERM;
1775 return ops->vidioc_s_register(file, fh, p);
1776#else
1777 return -ENOTTY;
1778#endif
1779}
1780
1781static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1782 struct file *file, void *fh, void *arg)
1783{
1784 struct v4l2_dbg_chip_ident *p = arg;
1785
1786 p->ident = V4L2_IDENT_NONE;
1787 p->revision = 0;
1788 return ops->vidioc_g_chip_ident(file, fh, p);
1789}
1790
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001791static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1792 struct file *file, void *fh, void *arg)
1793{
1794 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1795}
1796
1797static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1798 struct file *file, void *fh, void *arg)
1799{
1800 return ops->vidioc_subscribe_event(fh, arg);
1801}
1802
1803static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1804 struct file *file, void *fh, void *arg)
1805{
1806 return ops->vidioc_unsubscribe_event(fh, arg);
1807}
1808
1809static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1810 struct file *file, void *fh, void *arg)
1811{
1812 struct v4l2_sliced_vbi_cap *p = arg;
1813
1814 /* Clear up to type, everything after type is zeroed already */
1815 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1816
1817 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1818}
1819
Hans Verkuil82b655b2012-07-05 06:37:08 -03001820static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1821 struct file *file, void *fh, void *arg)
1822{
1823 struct video_device *vfd = video_devdata(file);
1824 struct v4l2_frequency_band *p = arg;
1825 enum v4l2_tuner_type type;
1826 int err;
1827
1828 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1829 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1830
1831 if (type != p->type)
1832 return -EINVAL;
1833 if (ops->vidioc_enum_freq_bands)
1834 return ops->vidioc_enum_freq_bands(file, fh, p);
1835 if (ops->vidioc_g_tuner) {
1836 struct v4l2_tuner t = {
1837 .index = p->tuner,
1838 .type = type,
1839 };
1840
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001841 if (p->index)
1842 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001843 err = ops->vidioc_g_tuner(file, fh, &t);
1844 if (err)
1845 return err;
1846 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1847 p->rangelow = t.rangelow;
1848 p->rangehigh = t.rangehigh;
1849 p->modulation = (type == V4L2_TUNER_RADIO) ?
1850 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1851 return 0;
1852 }
1853 if (ops->vidioc_g_modulator) {
1854 struct v4l2_modulator m = {
1855 .index = p->tuner,
1856 };
1857
1858 if (type != V4L2_TUNER_RADIO)
1859 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001860 if (p->index)
1861 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001862 err = ops->vidioc_g_modulator(file, fh, &m);
1863 if (err)
1864 return err;
1865 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1866 p->rangelow = m.rangelow;
1867 p->rangehigh = m.rangehigh;
1868 p->modulation = (type == V4L2_TUNER_RADIO) ?
1869 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1870 return 0;
1871 }
1872 return -ENOTTY;
1873}
1874
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001875struct v4l2_ioctl_info {
1876 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001877 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001878 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001879 union {
1880 u32 offset;
1881 int (*func)(const struct v4l2_ioctl_ops *ops,
1882 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001883 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03001884 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001885};
1886
1887/* This control needs a priority check */
1888#define INFO_FL_PRIO (1 << 0)
1889/* This control can be valid if the filehandle passes a control handler. */
1890#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001891/* This is a standard ioctl, no need for special code */
1892#define INFO_FL_STD (1 << 2)
1893/* This is ioctl has its own function */
1894#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001895/* Queuing ioctl */
1896#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001897/* Zero struct from after the field to the end */
1898#define INFO_FL_CLEAR(v4l2_struct, field) \
1899 ((offsetof(struct v4l2_struct, field) + \
1900 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1901#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001902
Hans Verkuil86748f32012-06-22 06:04:16 -03001903#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1904 [_IOC_NR(_ioctl)] = { \
1905 .ioctl = _ioctl, \
1906 .flags = _flags | INFO_FL_STD, \
1907 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001908 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03001909 .debug = _debug, \
1910 }
1911
1912#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1913 [_IOC_NR(_ioctl)] = { \
1914 .ioctl = _ioctl, \
1915 .flags = _flags | INFO_FL_FUNC, \
1916 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001917 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03001918 .debug = _debug, \
1919 }
1920
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001921static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001922 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001923 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1924 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1925 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001926 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1927 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001928 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1929 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001930 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001931 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
1932 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1933 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1934 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001935 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1936 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001937 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1938 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1939 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001940 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001941 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1942 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 -03001943 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1944 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001945 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1946 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001947 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1948 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 -03001949 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1950 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1951 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1952 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1953 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1954 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1955 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03001956 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001957 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1958 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1959 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03001960 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1961 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1962 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1963 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1964 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001965 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1966 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001967 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001968 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001969 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1970 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001971 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1972 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001973 IOCTL_INFO_FNC(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001974 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001975 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1976 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil9bc31632012-07-18 09:34:59 -03001977 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001978 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1979 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001980 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
1981 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1982 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1983 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
1984 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001985 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
1986 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
1987 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 -03001988 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
Hans Verkuilefc626b2012-06-09 10:09:07 -03001989 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
1990 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
1991 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
1992 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
1993 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
1994 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001995 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
1996 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
1997 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001998 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1999 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002000 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2001 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002002 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002003 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002004};
2005#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2006
2007bool v4l2_is_known_ioctl(unsigned int cmd)
2008{
2009 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2010 return false;
2011 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2012}
2013
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002014struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2015{
2016 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2017 return vdev->lock;
2018 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2019 return NULL;
2020 if (vdev->queue && vdev->queue->lock &&
2021 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2022 return vdev->queue->lock;
2023 return vdev->lock;
2024}
2025
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002026/* Common ioctl debug function. This function can be used by
2027 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002028void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002029{
Hans Verkuil86748f32012-06-22 06:04:16 -03002030 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002031
Hans Verkuil4a085162012-06-22 06:38:06 -03002032 if (prefix)
2033 printk(KERN_DEBUG "%s: ", prefix);
2034
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002035 switch (_IOC_TYPE(cmd)) {
2036 case 'd':
2037 type = "v4l2_int";
2038 break;
2039 case 'V':
2040 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2041 type = "v4l2";
2042 break;
2043 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002044 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002045 return;
2046 default:
2047 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002048 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002049 }
2050
2051 switch (_IOC_DIR(cmd)) {
2052 case _IOC_NONE: dir = "--"; break;
2053 case _IOC_READ: dir = "r-"; break;
2054 case _IOC_WRITE: dir = "-w"; break;
2055 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2056 default: dir = "*ERR*"; break;
2057 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002058 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002059 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2060}
2061EXPORT_SYMBOL(v4l_printk_ioctl);
2062
Hans Verkuil069b7472008-12-30 07:04:34 -03002063static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002064 unsigned int cmd, void *arg)
2065{
2066 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002067 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002068 bool write_only = false;
2069 struct v4l2_ioctl_info default_info;
2070 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002071 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002072 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002073 int use_fh_prio = 0;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002074 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002075 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002076
Hans Verkuil6a717882010-04-06 15:56:08 -03002077 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002078 pr_warn("%s: has no ioctl_ops.\n",
2079 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002080 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002081 }
2082
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002083 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2084 vfh = file->private_data;
2085 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002086 }
2087
2088 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002089 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002090
2091 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2092 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002093 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002094
2095 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2096 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2097 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002098 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002099 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002100 } else {
2101 default_info.ioctl = cmd;
2102 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002103 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002104 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002105 }
2106
Hans Verkuil86748f32012-06-22 06:04:16 -03002107 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002108 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002109 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002110 pr_cont(": ");
2111 info->debug(arg, write_only);
2112 }
2113 if (info->flags & INFO_FL_STD) {
2114 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2115 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002116 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002117
2118 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002119 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002120 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002121 } else if (!ops->vidioc_default) {
2122 ret = -ENOTTY;
2123 } else {
2124 ret = ops->vidioc_default(file, fh,
2125 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2126 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002127 }
2128
Hans Verkuil86748f32012-06-22 06:04:16 -03002129done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002130 if (debug) {
2131 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002132 if (ret < 0)
2133 printk(KERN_DEBUG "%s: error %ld\n",
2134 video_device_node_name(vfd), ret);
2135 return ret;
2136 }
Hans Verkuil4a085162012-06-22 06:38:06 -03002137 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002138 if (ret < 0)
2139 pr_cont(": error %ld\n", ret);
Hans Verkuil80131fe02012-06-22 06:37:38 -03002140 else if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002141 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002142 else if (_IOC_DIR(cmd) == _IOC_NONE)
2143 info->debug(arg, write_only);
2144 else {
2145 pr_cont(": ");
2146 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002147 }
2148 }
2149
2150 return ret;
2151}
2152
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002153static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2154 void * __user *user_ptr, void ***kernel_ptr)
2155{
2156 int ret = 0;
2157
2158 switch (cmd) {
2159 case VIDIOC_QUERYBUF:
2160 case VIDIOC_QBUF:
2161 case VIDIOC_DQBUF: {
2162 struct v4l2_buffer *buf = parg;
2163
2164 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2165 if (buf->length > VIDEO_MAX_PLANES) {
2166 ret = -EINVAL;
2167 break;
2168 }
2169 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002170 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002171 *array_size = sizeof(struct v4l2_plane) * buf->length;
2172 ret = 1;
2173 }
2174 break;
2175 }
2176
Hans Verkuiled45ce22012-08-10 06:07:12 -03002177 case VIDIOC_SUBDEV_G_EDID:
2178 case VIDIOC_SUBDEV_S_EDID: {
2179 struct v4l2_subdev_edid *edid = parg;
2180
2181 if (edid->blocks) {
2182 *user_ptr = (void __user *)edid->edid;
2183 *kernel_ptr = (void *)&edid->edid;
2184 *array_size = edid->blocks * 128;
2185 ret = 1;
2186 }
2187 break;
2188 }
2189
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002190 case VIDIOC_S_EXT_CTRLS:
2191 case VIDIOC_G_EXT_CTRLS:
2192 case VIDIOC_TRY_EXT_CTRLS: {
2193 struct v4l2_ext_controls *ctrls = parg;
2194
2195 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002196 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2197 ret = -EINVAL;
2198 break;
2199 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002200 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002201 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002202 *array_size = sizeof(struct v4l2_ext_control)
2203 * ctrls->count;
2204 ret = 1;
2205 }
2206 break;
2207 }
2208 }
2209
2210 return ret;
2211}
2212
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002213long
2214video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2215 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002216{
2217 char sbuf[128];
2218 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002219 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002220 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002221 bool has_array_args;
2222 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002223 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002224 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002225
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002226 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002227 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002228 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2229 parg = sbuf;
2230 } else {
2231 /* too big to allocate from stack */
2232 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2233 if (NULL == mbuf)
2234 return -ENOMEM;
2235 parg = mbuf;
2236 }
2237
2238 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002239 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002240 unsigned int n = _IOC_SIZE(cmd);
2241
2242 /*
2243 * In some cases, only a few fields are used as input,
2244 * i.e. when the app sets "index" and then the driver
2245 * fills in the rest of the structure for the thing
2246 * with that index. We only need to copy up the first
2247 * non-input field.
2248 */
2249 if (v4l2_is_known_ioctl(cmd)) {
2250 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2251 if (flags & INFO_FL_CLEAR_MASK)
2252 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2253 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002254
2255 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002256 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002257
2258 /* zero out anything we don't copy from userspace */
2259 if (n < _IOC_SIZE(cmd))
2260 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002261 } else {
2262 /* read-only ioctl */
2263 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002264 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002265 }
2266
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002267 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2268 if (err < 0)
2269 goto out;
2270 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002271
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002272 if (has_array_args) {
2273 /*
2274 * When adding new types of array args, make sure that the
2275 * parent argument to ioctl (which contains the pointer to the
2276 * array) fits into sbuf (so that mbuf will still remain
2277 * unused up to here).
2278 */
2279 mbuf = kmalloc(array_size, GFP_KERNEL);
2280 err = -ENOMEM;
2281 if (NULL == mbuf)
2282 goto out_array_args;
2283 err = -EFAULT;
2284 if (copy_from_user(mbuf, user_ptr, array_size))
2285 goto out_array_args;
2286 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002287 }
2288
2289 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002290 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002291 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002292 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002293
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002294 if (has_array_args) {
2295 *kernel_ptr = user_ptr;
2296 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002297 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002298 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002299 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002300 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2301 results that must be returned. */
2302 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002303 goto out;
2304
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002305out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002306 /* Copy results into user buffer */
2307 switch (_IOC_DIR(cmd)) {
2308 case _IOC_READ:
2309 case (_IOC_WRITE | _IOC_READ):
2310 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2311 err = -EFAULT;
2312 break;
2313 }
2314
2315out:
2316 kfree(mbuf);
2317 return err;
2318}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002319EXPORT_SYMBOL(video_usercopy);
2320
2321long video_ioctl2(struct file *file,
2322 unsigned int cmd, unsigned long arg)
2323{
2324 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2325}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002326EXPORT_SYMBOL(video_ioctl2);