blob: 54f4ac612e8e60bfeec18c16667890051de28653 [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;
319 case V4L2_BUF_TYPE_PRIVATE:
320 pr_cont("\n");
321 break;
322 }
323}
324
325static void v4l_print_framebuffer(const void *arg, bool write_only)
326{
327 const struct v4l2_framebuffer *p = arg;
328
329 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
330 "height=%u, pixelformat=%c%c%c%c, "
331 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
332 p->capability, p->flags, p->base,
333 p->fmt.width, p->fmt.height,
334 (p->fmt.pixelformat & 0xff),
335 (p->fmt.pixelformat >> 8) & 0xff,
336 (p->fmt.pixelformat >> 16) & 0xff,
337 (p->fmt.pixelformat >> 24) & 0xff,
338 p->fmt.bytesperline, p->fmt.sizeimage,
339 p->fmt.colorspace);
340}
341
Hans Verkuile325b242012-06-09 12:50:15 -0300342static void v4l_print_buftype(const void *arg, bool write_only)
343{
344 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
345}
346
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300347static void v4l_print_modulator(const void *arg, bool write_only)
348{
349 const struct v4l2_modulator *p = arg;
350
351 if (write_only)
352 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
353 else
354 pr_cont("index=%u, name=%s, capability=0x%x, "
355 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
356 p->index, p->name, p->capability,
357 p->rangelow, p->rangehigh, p->txsubchans);
358}
359
360static void v4l_print_tuner(const void *arg, bool write_only)
361{
362 const struct v4l2_tuner *p = arg;
363
364 if (write_only)
365 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
366 else
367 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
368 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
369 "rxsubchans=0x%x, audmode=%u\n",
370 p->index, p->name, p->type,
371 p->capability, p->rangelow,
372 p->rangehigh, p->signal, p->afc,
373 p->rxsubchans, p->audmode);
374}
375
376static void v4l_print_frequency(const void *arg, bool write_only)
377{
378 const struct v4l2_frequency *p = arg;
379
380 pr_cont("tuner=%u, type=%u, frequency=%u\n",
381 p->tuner, p->type, p->frequency);
382}
383
384static void v4l_print_standard(const void *arg, bool write_only)
385{
386 const struct v4l2_standard *p = arg;
387
388 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
389 "framelines=%u\n", p->index,
390 (unsigned long long)p->id, p->name,
391 p->frameperiod.numerator,
392 p->frameperiod.denominator,
393 p->framelines);
394}
395
396static void v4l_print_std(const void *arg, bool write_only)
397{
398 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
399}
400
401static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
402{
403 const struct v4l2_hw_freq_seek *p = arg;
404
405 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
406 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
407}
408
Hans Verkuileb3728e2012-06-22 06:23:59 -0300409static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300410{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300411 const struct v4l2_requestbuffers *p = arg;
412
413 pr_cont("count=%d, type=%s, memory=%s\n",
414 p->count,
415 prt_names(p->type, v4l2_type_names),
416 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300417}
418
Hans Verkuileb3728e2012-06-22 06:23:59 -0300419static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300420{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300421 const struct v4l2_buffer *p = arg;
422 const struct v4l2_timecode *tc = &p->timecode;
423 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300424 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300425
Hans Verkuileb3728e2012-06-22 06:23:59 -0300426 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
427 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300428 p->timestamp.tv_sec / 3600,
429 (int)(p->timestamp.tv_sec / 60) % 60,
430 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300431 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300432 p->index,
433 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300434 p->flags, prt_names(p->field, v4l2_field_names),
435 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300436
437 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300438 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300439 for (i = 0; i < p->length; ++i) {
440 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300441 printk(KERN_DEBUG
442 "plane %d: bytesused=%d, data_offset=0x%08x "
443 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300444 i, plane->bytesused, plane->data_offset,
445 plane->m.userptr, plane->length);
446 }
447 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300448 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300449 p->bytesused, p->m.userptr, p->length);
450 }
451
Hans Verkuileb3728e2012-06-22 06:23:59 -0300452 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
453 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300454 tc->hours, tc->minutes, tc->seconds,
455 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
456}
457
Hans Verkuileb3728e2012-06-22 06:23:59 -0300458static void v4l_print_create_buffers(const void *arg, bool write_only)
459{
460 const struct v4l2_create_buffers *p = arg;
461
462 pr_cont("index=%d, count=%d, memory=%s, ",
463 p->index, p->count,
464 prt_names(p->memory, v4l2_memory_names));
465 v4l_print_format(&p->format, write_only);
466}
467
468static void v4l_print_streamparm(const void *arg, bool write_only)
469{
470 const struct v4l2_streamparm *p = arg;
471
472 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
473
474 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
475 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
476 const struct v4l2_captureparm *c = &p->parm.capture;
477
478 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
479 "extendedmode=%d, readbuffers=%d\n",
480 c->capability, c->capturemode,
481 c->timeperframe.numerator, c->timeperframe.denominator,
482 c->extendedmode, c->readbuffers);
483 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
484 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
485 const struct v4l2_outputparm *c = &p->parm.output;
486
487 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
488 "extendedmode=%d, writebuffers=%d\n",
489 c->capability, c->outputmode,
490 c->timeperframe.numerator, c->timeperframe.denominator,
491 c->extendedmode, c->writebuffers);
492 }
493}
494
Hans Verkuilefbceec2012-06-09 12:54:02 -0300495static void v4l_print_queryctrl(const void *arg, bool write_only)
496{
497 const struct v4l2_queryctrl *p = arg;
498
499 pr_cont("id=0x%x, type=%d, name=%s, min/max=%d/%d, "
500 "step=%d, default=%d, flags=0x%08x\n",
501 p->id, p->type, p->name,
502 p->minimum, p->maximum,
503 p->step, p->default_value, p->flags);
504}
505
506static void v4l_print_querymenu(const void *arg, bool write_only)
507{
508 const struct v4l2_querymenu *p = arg;
509
510 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
511}
512
513static void v4l_print_control(const void *arg, bool write_only)
514{
515 const struct v4l2_control *p = arg;
516
517 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
518}
519
520static void v4l_print_ext_controls(const void *arg, bool write_only)
521{
522 const struct v4l2_ext_controls *p = arg;
523 int i;
524
525 pr_cont("class=0x%x, count=%d, error_idx=%d",
526 p->ctrl_class, p->count, p->error_idx);
527 for (i = 0; i < p->count; i++) {
528 if (p->controls[i].size)
529 pr_cont(", id/val=0x%x/0x%x",
530 p->controls[i].id, p->controls[i].value);
531 else
532 pr_cont(", id/size=0x%x/%u",
533 p->controls[i].id, p->controls[i].size);
534 }
535 pr_cont("\n");
536}
537
Hans Verkuild84f2d942012-06-09 11:57:46 -0300538static void v4l_print_cropcap(const void *arg, bool write_only)
539{
540 const struct v4l2_cropcap *p = arg;
541
542 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
543 "defrect wxh=%dx%d, x,y=%d,%d\n, "
544 "pixelaspect %d/%d\n",
545 prt_names(p->type, v4l2_type_names),
546 p->bounds.width, p->bounds.height,
547 p->bounds.left, p->bounds.top,
548 p->defrect.width, p->defrect.height,
549 p->defrect.left, p->defrect.top,
550 p->pixelaspect.numerator, p->pixelaspect.denominator);
551}
552
553static void v4l_print_crop(const void *arg, bool write_only)
554{
555 const struct v4l2_crop *p = arg;
556
557 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
558 prt_names(p->type, v4l2_type_names),
559 p->c.width, p->c.height,
560 p->c.left, p->c.top);
561}
562
563static void v4l_print_selection(const void *arg, bool write_only)
564{
565 const struct v4l2_selection *p = arg;
566
567 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
568 prt_names(p->type, v4l2_type_names),
569 p->target, p->flags,
570 p->r.width, p->r.height, p->r.left, p->r.top);
571}
572
Hans Verkuild806f2d2012-06-09 10:02:49 -0300573static void v4l_print_jpegcompression(const void *arg, bool write_only)
574{
575 const struct v4l2_jpegcompression *p = arg;
576
577 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
578 "COM_len=%d, jpeg_markers=0x%x\n",
579 p->quality, p->APPn, p->APP_len,
580 p->COM_len, p->jpeg_markers);
581}
582
583static void v4l_print_enc_idx(const void *arg, bool write_only)
584{
585 const struct v4l2_enc_idx *p = arg;
586
587 pr_cont("entries=%d, entries_cap=%d\n",
588 p->entries, p->entries_cap);
589}
590
591static void v4l_print_encoder_cmd(const void *arg, bool write_only)
592{
593 const struct v4l2_encoder_cmd *p = arg;
594
595 pr_cont("cmd=%d, flags=0x%x\n",
596 p->cmd, p->flags);
597}
598
599static void v4l_print_decoder_cmd(const void *arg, bool write_only)
600{
601 const struct v4l2_decoder_cmd *p = arg;
602
603 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
604
605 if (p->cmd == V4L2_DEC_CMD_START)
606 pr_info("speed=%d, format=%u\n",
607 p->start.speed, p->start.format);
608 else if (p->cmd == V4L2_DEC_CMD_STOP)
609 pr_info("pts=%llu\n", p->stop.pts);
610}
611
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300612static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
613{
614 const struct v4l2_dbg_chip_ident *p = arg;
615
616 pr_cont("type=%u, ", p->match.type);
617 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
618 pr_cont("name=%s, ", p->match.name);
619 else
620 pr_cont("addr=%u, ", p->match.addr);
621 pr_cont("chip_ident=%u, revision=0x%x\n",
622 p->ident, p->revision);
623}
624
625static void v4l_print_dbg_register(const void *arg, bool write_only)
626{
627 const struct v4l2_dbg_register *p = arg;
628
629 pr_cont("type=%u, ", p->match.type);
630 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
631 pr_cont("name=%s, ", p->match.name);
632 else
633 pr_cont("addr=%u, ", p->match.addr);
634 pr_cont("reg=0x%llx, val=0x%llx\n",
635 p->reg, p->val);
636}
637
Hans Verkuilefc626b2012-06-09 10:09:07 -0300638static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300639{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300640 const struct v4l2_dv_enum_preset *p = arg;
641
642 pr_cont("index=%u, preset=%u, name=%s, width=%u, height=%u\n",
643 p->index, p->preset, p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300644}
645
Hans Verkuilefc626b2012-06-09 10:09:07 -0300646static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300647{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300648 const struct v4l2_dv_preset *p = arg;
649
650 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300651}
652
Hans Verkuilefc626b2012-06-09 10:09:07 -0300653static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300654{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300655 const struct v4l2_dv_timings *p = arg;
656
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300657 switch (p->type) {
658 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300659 pr_cont("type=bt-656/1120, interlaced=%u, "
660 "pixelclock=%llu, "
661 "width=%u, height=%u, polarities=0x%x, "
662 "hfrontporch=%u, hsync=%u, "
663 "hbackporch=%u, vfrontporch=%u, "
664 "vsync=%u, vbackporch=%u, "
665 "il_vfrontporch=%u, il_vsync=%u, "
666 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300667 p->bt.interlaced, p->bt.pixelclock,
668 p->bt.width, p->bt.height,
669 p->bt.polarities, p->bt.hfrontporch,
670 p->bt.hsync, p->bt.hbackporch,
671 p->bt.vfrontporch, p->bt.vsync,
672 p->bt.vbackporch, p->bt.il_vfrontporch,
673 p->bt.il_vsync, p->bt.il_vbackporch,
674 p->bt.standards, p->bt.flags);
675 break;
676 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300677 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300678 break;
679 }
680}
681
Hans Verkuilefc626b2012-06-09 10:09:07 -0300682static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
683{
684 const struct v4l2_enum_dv_timings *p = arg;
685
686 pr_cont("index=%u, ", p->index);
687 v4l_print_dv_timings(&p->timings, write_only);
688}
689
690static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
691{
692 const struct v4l2_dv_timings_cap *p = arg;
693
694 switch (p->type) {
695 case V4L2_DV_BT_656_1120:
696 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
697 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
698 p->bt.min_width, p->bt.max_width,
699 p->bt.min_height, p->bt.max_height,
700 p->bt.min_pixelclock, p->bt.max_pixelclock,
701 p->bt.standards, p->bt.capabilities);
702 break;
703 default:
704 pr_cont("type=%u\n", p->type);
705 break;
706 }
707}
708
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300709static void v4l_print_frmsizeenum(const void *arg, bool write_only)
710{
711 const struct v4l2_frmsizeenum *p = arg;
712
713 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
714 p->index,
715 (p->pixel_format & 0xff),
716 (p->pixel_format >> 8) & 0xff,
717 (p->pixel_format >> 16) & 0xff,
718 (p->pixel_format >> 24) & 0xff,
719 p->type);
720 switch (p->type) {
721 case V4L2_FRMSIZE_TYPE_DISCRETE:
722 pr_cont(" wxh=%ux%u\n",
723 p->discrete.width, p->discrete.height);
724 break;
725 case V4L2_FRMSIZE_TYPE_STEPWISE:
726 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
727 p->stepwise.min_width, p->stepwise.min_height,
728 p->stepwise.step_width, p->stepwise.step_height,
729 p->stepwise.max_width, p->stepwise.max_height);
730 break;
731 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
732 /* fall through */
733 default:
734 pr_cont("\n");
735 break;
736 }
737}
738
739static void v4l_print_frmivalenum(const void *arg, bool write_only)
740{
741 const struct v4l2_frmivalenum *p = arg;
742
743 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
744 p->index,
745 (p->pixel_format & 0xff),
746 (p->pixel_format >> 8) & 0xff,
747 (p->pixel_format >> 16) & 0xff,
748 (p->pixel_format >> 24) & 0xff,
749 p->width, p->height, p->type);
750 switch (p->type) {
751 case V4L2_FRMIVAL_TYPE_DISCRETE:
752 pr_cont(" fps=%d/%d\n",
753 p->discrete.numerator,
754 p->discrete.denominator);
755 break;
756 case V4L2_FRMIVAL_TYPE_STEPWISE:
757 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
758 p->stepwise.min.numerator,
759 p->stepwise.min.denominator,
760 p->stepwise.max.numerator,
761 p->stepwise.max.denominator,
762 p->stepwise.step.numerator,
763 p->stepwise.step.denominator);
764 break;
765 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
766 /* fall through */
767 default:
768 pr_cont("\n");
769 break;
770 }
771}
772
773static void v4l_print_event(const void *arg, bool write_only)
774{
775 const struct v4l2_event *p = arg;
776 const struct v4l2_event_ctrl *c;
777
778 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
779 "timestamp=%lu.%9.9lu\n",
780 p->type, p->pending, p->sequence, p->id,
781 p->timestamp.tv_sec, p->timestamp.tv_nsec);
782 switch (p->type) {
783 case V4L2_EVENT_VSYNC:
784 printk(KERN_DEBUG "field=%s\n",
785 prt_names(p->u.vsync.field, v4l2_field_names));
786 break;
787 case V4L2_EVENT_CTRL:
788 c = &p->u.ctrl;
789 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
790 c->changes, c->type);
791 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
792 pr_cont("value64=%lld, ", c->value64);
793 else
794 pr_cont("value=%d, ", c->value);
795 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
796 " default_value=%d\n",
797 c->flags, c->minimum, c->maximum,
798 c->step, c->default_value);
799 break;
800 case V4L2_EVENT_FRAME_SYNC:
801 pr_cont("frame_sequence=%u\n",
802 p->u.frame_sync.frame_sequence);
803 break;
804 }
805}
806
807static void v4l_print_event_subscription(const void *arg, bool write_only)
808{
809 const struct v4l2_event_subscription *p = arg;
810
811 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
812 p->type, p->id, p->flags);
813}
814
815static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
816{
817 const struct v4l2_sliced_vbi_cap *p = arg;
818 int i;
819
820 pr_cont("type=%s, service_set=0x%08x\n",
821 prt_names(p->type, v4l2_type_names), p->service_set);
822 for (i = 0; i < 24; i++)
823 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
824 p->service_lines[0][i],
825 p->service_lines[1][i]);
826}
827
Hans Verkuil82b655b2012-07-05 06:37:08 -0300828static void v4l_print_freq_band(const void *arg, bool write_only)
829{
830 const struct v4l2_frequency_band *p = arg;
831
832 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
833 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
834 p->tuner, p->type, p->index,
835 p->capability, p->rangelow,
836 p->rangehigh, p->modulation);
837}
838
Hans Verkuilefc626b2012-06-09 10:09:07 -0300839static void v4l_print_u32(const void *arg, bool write_only)
840{
841 pr_cont("value=%u\n", *(const u32 *)arg);
842}
843
844static void v4l_print_newline(const void *arg, bool write_only)
845{
846 pr_cont("\n");
847}
848
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300849static void v4l_print_default(const void *arg, bool write_only)
850{
851 pr_cont("driver-specific ioctl\n");
852}
853
Hans Verkuilefbceec2012-06-09 12:54:02 -0300854static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300855{
856 __u32 i;
857
858 /* zero the reserved fields */
859 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300860 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300861 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300862
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300863 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
864 when using extended controls.
865 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
866 is it allowed for backwards compatibility.
867 */
868 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
869 return 0;
870 /* Check that all controls are from the same control class. */
871 for (i = 0; i < c->count; i++) {
872 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
873 c->error_idx = i;
874 return 0;
875 }
876 }
877 return 1;
878}
879
Hans Verkuila3998102008-07-21 02:57:38 -0300880static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300881{
Hans Verkuila3998102008-07-21 02:57:38 -0300882 if (ops == NULL)
883 return -EINVAL;
884
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300885 switch (type) {
886 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300887 if (ops->vidioc_g_fmt_vid_cap ||
888 ops->vidioc_g_fmt_vid_cap_mplane)
889 return 0;
890 break;
891 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
892 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300893 return 0;
894 break;
895 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300896 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300897 return 0;
898 break;
899 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300900 if (ops->vidioc_g_fmt_vid_out ||
901 ops->vidioc_g_fmt_vid_out_mplane)
902 return 0;
903 break;
904 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
905 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300906 return 0;
907 break;
908 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300909 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300910 return 0;
911 break;
912 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300913 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300914 return 0;
915 break;
916 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300917 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300918 return 0;
919 break;
920 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300921 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300922 return 0;
923 break;
924 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300925 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300926 return 0;
927 break;
928 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300929 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300930 return 0;
931 break;
932 }
933 return -EINVAL;
934}
935
Hans Verkuil59076122012-06-22 06:09:54 -0300936static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
937 struct file *file, void *fh, void *arg)
938{
939 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
940
941 cap->version = LINUX_VERSION_CODE;
942 return ops->vidioc_querycap(file, fh, cap);
943}
944
945static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
946 struct file *file, void *fh, void *arg)
947{
948 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
949}
950
951static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
952 struct file *file, void *fh, void *arg)
953{
954 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
955}
956
Hans Verkuild0547cb2012-06-09 09:27:10 -0300957static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
958 struct file *file, void *fh, void *arg)
959{
960 struct video_device *vfd;
961 u32 *p = arg;
962
963 if (ops->vidioc_g_priority)
964 return ops->vidioc_g_priority(file, fh, arg);
965 vfd = video_devdata(file);
966 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
967 return 0;
968}
969
970static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
971 struct file *file, void *fh, void *arg)
972{
973 struct video_device *vfd;
974 struct v4l2_fh *vfh;
975 u32 *p = arg;
976
977 if (ops->vidioc_s_priority)
978 return ops->vidioc_s_priority(file, fh, *p);
979 vfd = video_devdata(file);
980 vfh = file->private_data;
981 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
982}
983
Hans Verkuil59076122012-06-22 06:09:54 -0300984static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
985 struct file *file, void *fh, void *arg)
986{
987 struct v4l2_input *p = arg;
988
989 /*
990 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
991 * CAP_STD here based on ioctl handler provided by the
992 * driver. If the driver doesn't support these
993 * for a specific input, it must override these flags.
994 */
995 if (ops->vidioc_s_std)
996 p->capabilities |= V4L2_IN_CAP_STD;
997 if (ops->vidioc_s_dv_preset)
998 p->capabilities |= V4L2_IN_CAP_PRESETS;
999 if (ops->vidioc_s_dv_timings)
1000 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
1001
1002 return ops->vidioc_enum_input(file, fh, p);
1003}
1004
1005static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1006 struct file *file, void *fh, void *arg)
1007{
1008 struct v4l2_output *p = arg;
1009
1010 /*
1011 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1012 * CAP_STD here based on ioctl handler provided by the
1013 * driver. If the driver doesn't support these
1014 * for a specific output, it must override these flags.
1015 */
1016 if (ops->vidioc_s_std)
1017 p->capabilities |= V4L2_OUT_CAP_STD;
1018 if (ops->vidioc_s_dv_preset)
1019 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1020 if (ops->vidioc_s_dv_timings)
1021 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1022
1023 return ops->vidioc_enum_output(file, fh, p);
1024}
1025
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001026static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1027 struct file *file, void *fh, void *arg)
1028{
1029 struct v4l2_fmtdesc *p = arg;
1030
1031 switch (p->type) {
1032 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1033 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1034 break;
1035 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1036 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1037 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1038 break;
1039 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1040 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1041 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1042 break;
1043 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1044 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1045 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1046 break;
1047 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1048 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1049 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1050 break;
1051 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1052 case V4L2_BUF_TYPE_PRIVATE:
1053 if (unlikely(!ops->vidioc_enum_fmt_type_private))
1054 break;
1055 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
1056 }
1057 return -EINVAL;
1058}
1059
1060static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1061 struct file *file, void *fh, void *arg)
1062{
1063 struct v4l2_format *p = arg;
1064
1065 switch (p->type) {
1066 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1067 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1068 break;
1069 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1070 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1071 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
1072 break;
1073 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1074 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1075 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
1076 break;
1077 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1078 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1079 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1080 break;
1081 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1082 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1083 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
1084 break;
1085 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1086 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1087 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
1088 break;
1089 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1090 case V4L2_BUF_TYPE_VBI_CAPTURE:
1091 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
1092 break;
1093 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1094 case V4L2_BUF_TYPE_VBI_OUTPUT:
1095 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
1096 break;
1097 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1098 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1099 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
1100 break;
1101 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1102 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1103 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
1104 break;
1105 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1106 case V4L2_BUF_TYPE_PRIVATE:
1107 if (unlikely(!ops->vidioc_g_fmt_type_private))
1108 break;
1109 return ops->vidioc_g_fmt_type_private(file, fh, arg);
1110 }
1111 return -EINVAL;
1112}
1113
1114static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1115 struct file *file, void *fh, void *arg)
1116{
1117 struct v4l2_format *p = arg;
1118
1119 switch (p->type) {
1120 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1121 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1122 break;
1123 CLEAR_AFTER_FIELD(p, fmt.pix);
1124 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1125 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1126 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1127 break;
1128 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1129 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1130 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1131 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1132 break;
1133 CLEAR_AFTER_FIELD(p, fmt.win);
1134 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1135 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1136 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1137 break;
1138 CLEAR_AFTER_FIELD(p, fmt.pix);
1139 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1140 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1141 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1142 break;
1143 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1144 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1145 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1146 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1147 break;
1148 CLEAR_AFTER_FIELD(p, fmt.win);
1149 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1150 case V4L2_BUF_TYPE_VBI_CAPTURE:
1151 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1152 break;
1153 CLEAR_AFTER_FIELD(p, fmt.vbi);
1154 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1155 case V4L2_BUF_TYPE_VBI_OUTPUT:
1156 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1157 break;
1158 CLEAR_AFTER_FIELD(p, fmt.vbi);
1159 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1160 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1161 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1162 break;
1163 CLEAR_AFTER_FIELD(p, fmt.sliced);
1164 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1165 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1166 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1167 break;
1168 CLEAR_AFTER_FIELD(p, fmt.sliced);
1169 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1170 case V4L2_BUF_TYPE_PRIVATE:
1171 if (unlikely(!ops->vidioc_s_fmt_type_private))
1172 break;
1173 return ops->vidioc_s_fmt_type_private(file, fh, arg);
1174 }
1175 return -EINVAL;
1176}
1177
1178static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1179 struct file *file, void *fh, void *arg)
1180{
1181 struct v4l2_format *p = arg;
1182
1183 switch (p->type) {
1184 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1185 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1186 break;
1187 CLEAR_AFTER_FIELD(p, fmt.pix);
1188 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1189 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1190 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1191 break;
1192 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1193 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1194 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1195 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1196 break;
1197 CLEAR_AFTER_FIELD(p, fmt.win);
1198 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1199 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1200 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1201 break;
1202 CLEAR_AFTER_FIELD(p, fmt.pix);
1203 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1204 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1205 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1206 break;
1207 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1208 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1209 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1210 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1211 break;
1212 CLEAR_AFTER_FIELD(p, fmt.win);
1213 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1214 case V4L2_BUF_TYPE_VBI_CAPTURE:
1215 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1216 break;
1217 CLEAR_AFTER_FIELD(p, fmt.vbi);
1218 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1219 case V4L2_BUF_TYPE_VBI_OUTPUT:
1220 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1221 break;
1222 CLEAR_AFTER_FIELD(p, fmt.vbi);
1223 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1224 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1225 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1226 break;
1227 CLEAR_AFTER_FIELD(p, fmt.sliced);
1228 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1229 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1230 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1231 break;
1232 CLEAR_AFTER_FIELD(p, fmt.sliced);
1233 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1234 case V4L2_BUF_TYPE_PRIVATE:
1235 if (unlikely(!ops->vidioc_try_fmt_type_private))
1236 break;
1237 return ops->vidioc_try_fmt_type_private(file, fh, arg);
1238 }
1239 return -EINVAL;
1240}
1241
Hans Verkuile325b242012-06-09 12:50:15 -03001242static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1243 struct file *file, void *fh, void *arg)
1244{
1245 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1246}
1247
1248static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1249 struct file *file, void *fh, void *arg)
1250{
1251 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1252}
1253
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001254static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1255 struct file *file, void *fh, void *arg)
1256{
1257 struct video_device *vfd = video_devdata(file);
1258 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001259 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001260
1261 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1262 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001263 err = ops->vidioc_g_tuner(file, fh, p);
1264 if (!err)
1265 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1266 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001267}
1268
1269static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1270 struct file *file, void *fh, void *arg)
1271{
1272 struct video_device *vfd = video_devdata(file);
1273 struct v4l2_tuner *p = arg;
1274
1275 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1276 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1277 return ops->vidioc_s_tuner(file, fh, p);
1278}
1279
Hans Verkuil82b655b2012-07-05 06:37:08 -03001280static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1281 struct file *file, void *fh, void *arg)
1282{
1283 struct v4l2_modulator *p = arg;
1284 int err;
1285
1286 err = ops->vidioc_g_modulator(file, fh, p);
1287 if (!err)
1288 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1289 return err;
1290}
1291
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001292static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1293 struct file *file, void *fh, void *arg)
1294{
1295 struct video_device *vfd = video_devdata(file);
1296 struct v4l2_frequency *p = arg;
1297
1298 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1299 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1300 return ops->vidioc_g_frequency(file, fh, p);
1301}
1302
1303static int v4l_s_frequency(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_frequency *p = arg;
1308 enum v4l2_tuner_type type;
1309
1310 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1311 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1312 if (p->type != type)
1313 return -EINVAL;
1314 return ops->vidioc_s_frequency(file, fh, p);
1315}
1316
1317static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1318 struct file *file, void *fh, void *arg)
1319{
1320 struct video_device *vfd = video_devdata(file);
1321 struct v4l2_standard *p = arg;
1322 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1323 unsigned int index = p->index, i, j = 0;
1324 const char *descr = "";
1325
1326 /* Return norm array in a canonical way */
1327 for (i = 0; i <= index && id; i++) {
1328 /* last std value in the standards array is 0, so this
1329 while always ends there since (id & 0) == 0. */
1330 while ((id & standards[j].std) != standards[j].std)
1331 j++;
1332 curr_id = standards[j].std;
1333 descr = standards[j].descr;
1334 j++;
1335 if (curr_id == 0)
1336 break;
1337 if (curr_id != V4L2_STD_PAL &&
1338 curr_id != V4L2_STD_SECAM &&
1339 curr_id != V4L2_STD_NTSC)
1340 id &= ~curr_id;
1341 }
1342 if (i <= index)
1343 return -EINVAL;
1344
1345 v4l2_video_std_construct(p, curr_id, descr);
1346 return 0;
1347}
1348
1349static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1350 struct file *file, void *fh, void *arg)
1351{
1352 struct video_device *vfd = video_devdata(file);
1353 v4l2_std_id *id = arg;
1354
1355 /* Calls the specific handler */
1356 if (ops->vidioc_g_std)
1357 return ops->vidioc_g_std(file, fh, arg);
1358 if (vfd->current_norm) {
1359 *id = vfd->current_norm;
1360 return 0;
1361 }
1362 return -ENOTTY;
1363}
1364
1365static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1366 struct file *file, void *fh, void *arg)
1367{
1368 struct video_device *vfd = video_devdata(file);
1369 v4l2_std_id *id = arg, norm;
1370 int ret;
1371
1372 norm = (*id) & vfd->tvnorms;
1373 if (vfd->tvnorms && !norm) /* Check if std is supported */
1374 return -EINVAL;
1375
1376 /* Calls the specific handler */
1377 ret = ops->vidioc_s_std(file, fh, &norm);
1378
1379 /* Updates standard information */
1380 if (ret >= 0)
1381 vfd->current_norm = norm;
1382 return ret;
1383}
1384
1385static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1386 struct file *file, void *fh, void *arg)
1387{
1388 struct video_device *vfd = video_devdata(file);
1389 v4l2_std_id *p = arg;
1390
1391 /*
1392 * If nothing detected, it should return all supported
1393 * standard.
1394 * Drivers just need to mask the std argument, in order
1395 * to remove the standards that don't apply from the mask.
1396 * This means that tuners, audio and video decoders can join
1397 * their efforts to improve the standards detection.
1398 */
1399 *p = vfd->tvnorms;
1400 return ops->vidioc_querystd(file, fh, arg);
1401}
1402
1403static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1404 struct file *file, void *fh, void *arg)
1405{
1406 struct video_device *vfd = video_devdata(file);
1407 struct v4l2_hw_freq_seek *p = arg;
1408 enum v4l2_tuner_type type;
1409
1410 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1411 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1412 if (p->type != type)
1413 return -EINVAL;
1414 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1415}
1416
Hans Verkuileb3728e2012-06-22 06:23:59 -03001417static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1418 struct file *file, void *fh, void *arg)
1419{
1420 struct v4l2_requestbuffers *p = arg;
1421 int ret = check_fmt(ops, p->type);
1422
1423 if (ret)
1424 return ret;
1425
1426 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1427 CLEAR_AFTER_FIELD(p, memory);
1428
1429 return ops->vidioc_reqbufs(file, fh, p);
1430}
1431
1432static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1433 struct file *file, void *fh, void *arg)
1434{
1435 struct v4l2_buffer *p = arg;
1436 int ret = check_fmt(ops, p->type);
1437
1438 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1439}
1440
1441static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1442 struct file *file, void *fh, void *arg)
1443{
1444 struct v4l2_buffer *p = arg;
1445 int ret = check_fmt(ops, p->type);
1446
1447 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1448}
1449
1450static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1451 struct file *file, void *fh, void *arg)
1452{
1453 struct v4l2_buffer *p = arg;
1454 int ret = check_fmt(ops, p->type);
1455
1456 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1457}
1458
1459static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1460 struct file *file, void *fh, void *arg)
1461{
1462 struct v4l2_create_buffers *create = arg;
1463 int ret = check_fmt(ops, create->format.type);
1464
1465 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1466}
1467
1468static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1469 struct file *file, void *fh, void *arg)
1470{
1471 struct v4l2_buffer *b = arg;
1472 int ret = check_fmt(ops, b->type);
1473
1474 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1475}
1476
1477static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1478 struct file *file, void *fh, void *arg)
1479{
1480 struct video_device *vfd = video_devdata(file);
1481 struct v4l2_streamparm *p = arg;
1482 v4l2_std_id std;
1483 int ret = check_fmt(ops, p->type);
1484
1485 if (ret)
1486 return ret;
1487 if (ops->vidioc_g_parm)
1488 return ops->vidioc_g_parm(file, fh, p);
1489 std = vfd->current_norm;
1490 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1491 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1492 return -EINVAL;
1493 p->parm.capture.readbuffers = 2;
1494 if (ops->vidioc_g_std)
1495 ret = ops->vidioc_g_std(file, fh, &std);
1496 if (ret == 0)
1497 v4l2_video_std_frame_period(std,
1498 &p->parm.capture.timeperframe);
1499 return ret;
1500}
1501
1502static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1503 struct file *file, void *fh, void *arg)
1504{
1505 struct v4l2_streamparm *p = arg;
1506 int ret = check_fmt(ops, p->type);
1507
1508 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1509}
1510
Hans Verkuilefbceec2012-06-09 12:54:02 -03001511static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1512 struct file *file, void *fh, void *arg)
1513{
1514 struct video_device *vfd = video_devdata(file);
1515 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001516 struct v4l2_fh *vfh =
1517 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001518
1519 if (vfh && vfh->ctrl_handler)
1520 return v4l2_queryctrl(vfh->ctrl_handler, p);
1521 if (vfd->ctrl_handler)
1522 return v4l2_queryctrl(vfd->ctrl_handler, p);
1523 if (ops->vidioc_queryctrl)
1524 return ops->vidioc_queryctrl(file, fh, p);
1525 return -ENOTTY;
1526}
1527
1528static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1529 struct file *file, void *fh, void *arg)
1530{
1531 struct video_device *vfd = video_devdata(file);
1532 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001533 struct v4l2_fh *vfh =
1534 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001535
1536 if (vfh && vfh->ctrl_handler)
1537 return v4l2_querymenu(vfh->ctrl_handler, p);
1538 if (vfd->ctrl_handler)
1539 return v4l2_querymenu(vfd->ctrl_handler, p);
1540 if (ops->vidioc_querymenu)
1541 return ops->vidioc_querymenu(file, fh, p);
1542 return -ENOTTY;
1543}
1544
1545static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1546 struct file *file, void *fh, void *arg)
1547{
1548 struct video_device *vfd = video_devdata(file);
1549 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001550 struct v4l2_fh *vfh =
1551 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001552 struct v4l2_ext_controls ctrls;
1553 struct v4l2_ext_control ctrl;
1554
1555 if (vfh && vfh->ctrl_handler)
1556 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1557 if (vfd->ctrl_handler)
1558 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1559 if (ops->vidioc_g_ctrl)
1560 return ops->vidioc_g_ctrl(file, fh, p);
1561 if (ops->vidioc_g_ext_ctrls == NULL)
1562 return -ENOTTY;
1563
1564 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1565 ctrls.count = 1;
1566 ctrls.controls = &ctrl;
1567 ctrl.id = p->id;
1568 ctrl.value = p->value;
1569 if (check_ext_ctrls(&ctrls, 1)) {
1570 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1571
1572 if (ret == 0)
1573 p->value = ctrl.value;
1574 return ret;
1575 }
1576 return -EINVAL;
1577}
1578
1579static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1580 struct file *file, void *fh, void *arg)
1581{
1582 struct video_device *vfd = video_devdata(file);
1583 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001584 struct v4l2_fh *vfh =
1585 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001586 struct v4l2_ext_controls ctrls;
1587 struct v4l2_ext_control ctrl;
1588
1589 if (vfh && vfh->ctrl_handler)
1590 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1591 if (vfd->ctrl_handler)
1592 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1593 if (ops->vidioc_s_ctrl)
1594 return ops->vidioc_s_ctrl(file, fh, p);
1595 if (ops->vidioc_s_ext_ctrls == NULL)
1596 return -ENOTTY;
1597
1598 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1599 ctrls.count = 1;
1600 ctrls.controls = &ctrl;
1601 ctrl.id = p->id;
1602 ctrl.value = p->value;
1603 if (check_ext_ctrls(&ctrls, 1))
1604 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1605 return -EINVAL;
1606}
1607
1608static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1609 struct file *file, void *fh, void *arg)
1610{
1611 struct video_device *vfd = video_devdata(file);
1612 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001613 struct v4l2_fh *vfh =
1614 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001615
1616 p->error_idx = p->count;
1617 if (vfh && vfh->ctrl_handler)
1618 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1619 if (vfd->ctrl_handler)
1620 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1621 if (ops->vidioc_g_ext_ctrls == NULL)
1622 return -ENOTTY;
1623 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1624 -EINVAL;
1625}
1626
1627static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1628 struct file *file, void *fh, void *arg)
1629{
1630 struct video_device *vfd = video_devdata(file);
1631 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001632 struct v4l2_fh *vfh =
1633 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001634
1635 p->error_idx = p->count;
1636 if (vfh && vfh->ctrl_handler)
1637 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1638 if (vfd->ctrl_handler)
1639 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1640 if (ops->vidioc_s_ext_ctrls == NULL)
1641 return -ENOTTY;
1642 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1643 -EINVAL;
1644}
1645
1646static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1647 struct file *file, void *fh, void *arg)
1648{
1649 struct video_device *vfd = video_devdata(file);
1650 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001651 struct v4l2_fh *vfh =
1652 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001653
1654 p->error_idx = p->count;
1655 if (vfh && vfh->ctrl_handler)
1656 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1657 if (vfd->ctrl_handler)
1658 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1659 if (ops->vidioc_try_ext_ctrls == NULL)
1660 return -ENOTTY;
1661 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1662 -EINVAL;
1663}
1664
Hans Verkuild84f2d942012-06-09 11:57:46 -03001665static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1666 struct file *file, void *fh, void *arg)
1667{
1668 struct v4l2_crop *p = arg;
1669 struct v4l2_selection s = {
1670 .type = p->type,
1671 };
1672 int ret;
1673
1674 if (ops->vidioc_g_crop)
1675 return ops->vidioc_g_crop(file, fh, p);
1676 /* simulate capture crop using selection api */
1677
1678 /* crop means compose for output devices */
1679 if (V4L2_TYPE_IS_OUTPUT(p->type))
1680 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1681 else
1682 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1683
1684 ret = ops->vidioc_g_selection(file, fh, &s);
1685
1686 /* copying results to old structure on success */
1687 if (!ret)
1688 p->c = s.r;
1689 return ret;
1690}
1691
1692static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1693 struct file *file, void *fh, void *arg)
1694{
1695 struct v4l2_crop *p = arg;
1696 struct v4l2_selection s = {
1697 .type = p->type,
1698 .r = p->c,
1699 };
1700
1701 if (ops->vidioc_s_crop)
1702 return ops->vidioc_s_crop(file, fh, p);
1703 /* simulate capture crop using selection api */
1704
1705 /* crop means compose for output devices */
1706 if (V4L2_TYPE_IS_OUTPUT(p->type))
1707 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1708 else
1709 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1710
1711 return ops->vidioc_s_selection(file, fh, &s);
1712}
1713
1714static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1715 struct file *file, void *fh, void *arg)
1716{
1717 struct v4l2_cropcap *p = arg;
1718 struct v4l2_selection s = { .type = p->type };
1719 int ret;
1720
1721 if (ops->vidioc_cropcap)
1722 return ops->vidioc_cropcap(file, fh, p);
1723
1724 /* obtaining bounds */
1725 if (V4L2_TYPE_IS_OUTPUT(p->type))
1726 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1727 else
1728 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1729
1730 ret = ops->vidioc_g_selection(file, fh, &s);
1731 if (ret)
1732 return ret;
1733 p->bounds = s.r;
1734
1735 /* obtaining defrect */
1736 if (V4L2_TYPE_IS_OUTPUT(p->type))
1737 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1738 else
1739 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1740
1741 ret = ops->vidioc_g_selection(file, fh, &s);
1742 if (ret)
1743 return ret;
1744 p->defrect = s.r;
1745
1746 /* setting trivial pixelaspect */
1747 p->pixelaspect.numerator = 1;
1748 p->pixelaspect.denominator = 1;
1749 return 0;
1750}
1751
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001752static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1753 struct file *file, void *fh, void *arg)
1754{
1755 struct video_device *vfd = video_devdata(file);
1756 int ret;
1757
1758 if (vfd->v4l2_dev)
1759 pr_info("%s: ================= START STATUS =================\n",
1760 vfd->v4l2_dev->name);
1761 ret = ops->vidioc_log_status(file, fh);
1762 if (vfd->v4l2_dev)
1763 pr_info("%s: ================== END STATUS ==================\n",
1764 vfd->v4l2_dev->name);
1765 return ret;
1766}
1767
1768static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1769 struct file *file, void *fh, void *arg)
1770{
1771#ifdef CONFIG_VIDEO_ADV_DEBUG
1772 struct v4l2_dbg_register *p = arg;
1773
1774 if (!capable(CAP_SYS_ADMIN))
1775 return -EPERM;
1776 return ops->vidioc_g_register(file, fh, p);
1777#else
1778 return -ENOTTY;
1779#endif
1780}
1781
1782static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1783 struct file *file, void *fh, void *arg)
1784{
1785#ifdef CONFIG_VIDEO_ADV_DEBUG
1786 struct v4l2_dbg_register *p = arg;
1787
1788 if (!capable(CAP_SYS_ADMIN))
1789 return -EPERM;
1790 return ops->vidioc_s_register(file, fh, p);
1791#else
1792 return -ENOTTY;
1793#endif
1794}
1795
1796static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1797 struct file *file, void *fh, void *arg)
1798{
1799 struct v4l2_dbg_chip_ident *p = arg;
1800
1801 p->ident = V4L2_IDENT_NONE;
1802 p->revision = 0;
1803 return ops->vidioc_g_chip_ident(file, fh, p);
1804}
1805
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001806static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1807 struct file *file, void *fh, void *arg)
1808{
1809 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1810}
1811
1812static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1813 struct file *file, void *fh, void *arg)
1814{
1815 return ops->vidioc_subscribe_event(fh, arg);
1816}
1817
1818static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1819 struct file *file, void *fh, void *arg)
1820{
1821 return ops->vidioc_unsubscribe_event(fh, arg);
1822}
1823
1824static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1825 struct file *file, void *fh, void *arg)
1826{
1827 struct v4l2_sliced_vbi_cap *p = arg;
1828
1829 /* Clear up to type, everything after type is zeroed already */
1830 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1831
1832 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1833}
1834
Hans Verkuil82b655b2012-07-05 06:37:08 -03001835static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1836 struct file *file, void *fh, void *arg)
1837{
1838 struct video_device *vfd = video_devdata(file);
1839 struct v4l2_frequency_band *p = arg;
1840 enum v4l2_tuner_type type;
1841 int err;
1842
1843 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1844 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1845
1846 if (type != p->type)
1847 return -EINVAL;
1848 if (ops->vidioc_enum_freq_bands)
1849 return ops->vidioc_enum_freq_bands(file, fh, p);
1850 if (ops->vidioc_g_tuner) {
1851 struct v4l2_tuner t = {
1852 .index = p->tuner,
1853 .type = type,
1854 };
1855
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001856 if (p->index)
1857 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001858 err = ops->vidioc_g_tuner(file, fh, &t);
1859 if (err)
1860 return err;
1861 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1862 p->rangelow = t.rangelow;
1863 p->rangehigh = t.rangehigh;
1864 p->modulation = (type == V4L2_TUNER_RADIO) ?
1865 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1866 return 0;
1867 }
1868 if (ops->vidioc_g_modulator) {
1869 struct v4l2_modulator m = {
1870 .index = p->tuner,
1871 };
1872
1873 if (type != V4L2_TUNER_RADIO)
1874 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001875 if (p->index)
1876 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001877 err = ops->vidioc_g_modulator(file, fh, &m);
1878 if (err)
1879 return err;
1880 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1881 p->rangelow = m.rangelow;
1882 p->rangehigh = m.rangehigh;
1883 p->modulation = (type == V4L2_TUNER_RADIO) ?
1884 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1885 return 0;
1886 }
1887 return -ENOTTY;
1888}
1889
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001890struct v4l2_ioctl_info {
1891 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001892 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001893 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001894 union {
1895 u32 offset;
1896 int (*func)(const struct v4l2_ioctl_ops *ops,
1897 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001898 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03001899 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001900};
1901
1902/* This control needs a priority check */
1903#define INFO_FL_PRIO (1 << 0)
1904/* This control can be valid if the filehandle passes a control handler. */
1905#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001906/* This is a standard ioctl, no need for special code */
1907#define INFO_FL_STD (1 << 2)
1908/* This is ioctl has its own function */
1909#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001910/* Queuing ioctl */
1911#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001912/* Zero struct from after the field to the end */
1913#define INFO_FL_CLEAR(v4l2_struct, field) \
1914 ((offsetof(struct v4l2_struct, field) + \
1915 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1916#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001917
Hans Verkuil86748f32012-06-22 06:04:16 -03001918#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1919 [_IOC_NR(_ioctl)] = { \
1920 .ioctl = _ioctl, \
1921 .flags = _flags | INFO_FL_STD, \
1922 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001923 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03001924 .debug = _debug, \
1925 }
1926
1927#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1928 [_IOC_NR(_ioctl)] = { \
1929 .ioctl = _ioctl, \
1930 .flags = _flags | INFO_FL_FUNC, \
1931 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001932 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03001933 .debug = _debug, \
1934 }
1935
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001936static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001937 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001938 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1939 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1940 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001941 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1942 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 -03001943 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1944 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuile325b242012-06-09 12:50:15 -03001945 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001946 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
1947 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1948 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1949 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001950 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1951 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001952 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1953 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1954 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001955 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001956 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1957 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 -03001958 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1959 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001960 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1961 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001962 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1963 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 -03001964 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1965 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1966 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1967 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1968 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1969 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1970 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03001971 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 -03001972 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1973 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1974 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03001975 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1976 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1977 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1978 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1979 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03001980 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1981 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001982 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001983 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03001984 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1985 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03001986 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1987 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001988 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 -03001989 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001990 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1991 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 -03001992 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 -03001993 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1994 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 -03001995 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
1996 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1997 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1998 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
1999 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002000 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2001 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2002 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 -03002003 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 -03002004 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
2005 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
2006 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
2007 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
2008 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2009 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002010 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2011 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2012 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002013 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2014 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002015 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2016 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002017 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 -03002018 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002019};
2020#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2021
2022bool v4l2_is_known_ioctl(unsigned int cmd)
2023{
2024 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2025 return false;
2026 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2027}
2028
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002029struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2030{
2031 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2032 return vdev->lock;
2033 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2034 return NULL;
2035 if (vdev->queue && vdev->queue->lock &&
2036 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2037 return vdev->queue->lock;
2038 return vdev->lock;
2039}
2040
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002041/* Common ioctl debug function. This function can be used by
2042 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002043void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002044{
Hans Verkuil86748f32012-06-22 06:04:16 -03002045 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002046
Hans Verkuil4a085162012-06-22 06:38:06 -03002047 if (prefix)
2048 printk(KERN_DEBUG "%s: ", prefix);
2049
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002050 switch (_IOC_TYPE(cmd)) {
2051 case 'd':
2052 type = "v4l2_int";
2053 break;
2054 case 'V':
2055 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2056 type = "v4l2";
2057 break;
2058 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002059 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002060 return;
2061 default:
2062 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002063 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002064 }
2065
2066 switch (_IOC_DIR(cmd)) {
2067 case _IOC_NONE: dir = "--"; break;
2068 case _IOC_READ: dir = "r-"; break;
2069 case _IOC_WRITE: dir = "-w"; break;
2070 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2071 default: dir = "*ERR*"; break;
2072 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002073 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002074 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2075}
2076EXPORT_SYMBOL(v4l_printk_ioctl);
2077
Hans Verkuil069b7472008-12-30 07:04:34 -03002078static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002079 unsigned int cmd, void *arg)
2080{
2081 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002082 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002083 bool write_only = false;
2084 struct v4l2_ioctl_info default_info;
2085 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002086 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002087 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002088 int use_fh_prio = 0;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002089 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002090 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002091
Hans Verkuil6a717882010-04-06 15:56:08 -03002092 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002093 pr_warn("%s: has no ioctl_ops.\n",
2094 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002095 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002096 }
2097
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002098 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2099 vfh = file->private_data;
2100 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002101 }
2102
2103 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002104 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002105
2106 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2107 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002108 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002109
2110 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2111 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2112 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002113 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002114 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002115 } else {
2116 default_info.ioctl = cmd;
2117 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002118 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002119 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002120 }
2121
Hans Verkuil86748f32012-06-22 06:04:16 -03002122 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002123 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002124 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002125 pr_cont(": ");
2126 info->debug(arg, write_only);
2127 }
2128 if (info->flags & INFO_FL_STD) {
2129 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2130 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002131 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002132
2133 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002134 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002135 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002136 } else if (!ops->vidioc_default) {
2137 ret = -ENOTTY;
2138 } else {
2139 ret = ops->vidioc_default(file, fh,
2140 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2141 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002142 }
2143
Hans Verkuil86748f32012-06-22 06:04:16 -03002144done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002145 if (debug) {
2146 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002147 if (ret < 0)
2148 printk(KERN_DEBUG "%s: error %ld\n",
2149 video_device_node_name(vfd), ret);
2150 return ret;
2151 }
Hans Verkuil4a085162012-06-22 06:38:06 -03002152 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002153 if (ret < 0)
2154 pr_cont(": error %ld\n", ret);
Hans Verkuil80131fe02012-06-22 06:37:38 -03002155 else if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002156 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002157 else if (_IOC_DIR(cmd) == _IOC_NONE)
2158 info->debug(arg, write_only);
2159 else {
2160 pr_cont(": ");
2161 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002162 }
2163 }
2164
2165 return ret;
2166}
2167
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002168static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2169 void * __user *user_ptr, void ***kernel_ptr)
2170{
2171 int ret = 0;
2172
2173 switch (cmd) {
2174 case VIDIOC_QUERYBUF:
2175 case VIDIOC_QBUF:
2176 case VIDIOC_DQBUF: {
2177 struct v4l2_buffer *buf = parg;
2178
2179 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2180 if (buf->length > VIDEO_MAX_PLANES) {
2181 ret = -EINVAL;
2182 break;
2183 }
2184 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002185 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002186 *array_size = sizeof(struct v4l2_plane) * buf->length;
2187 ret = 1;
2188 }
2189 break;
2190 }
2191
2192 case VIDIOC_S_EXT_CTRLS:
2193 case VIDIOC_G_EXT_CTRLS:
2194 case VIDIOC_TRY_EXT_CTRLS: {
2195 struct v4l2_ext_controls *ctrls = parg;
2196
2197 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002198 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2199 ret = -EINVAL;
2200 break;
2201 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002202 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002203 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002204 *array_size = sizeof(struct v4l2_ext_control)
2205 * ctrls->count;
2206 ret = 1;
2207 }
2208 break;
2209 }
2210 }
2211
2212 return ret;
2213}
2214
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002215long
2216video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2217 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002218{
2219 char sbuf[128];
2220 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002221 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002222 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002223 bool has_array_args;
2224 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002225 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002226 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002227
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002228 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002229 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002230 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2231 parg = sbuf;
2232 } else {
2233 /* too big to allocate from stack */
2234 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2235 if (NULL == mbuf)
2236 return -ENOMEM;
2237 parg = mbuf;
2238 }
2239
2240 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002241 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002242 unsigned int n = _IOC_SIZE(cmd);
2243
2244 /*
2245 * In some cases, only a few fields are used as input,
2246 * i.e. when the app sets "index" and then the driver
2247 * fills in the rest of the structure for the thing
2248 * with that index. We only need to copy up the first
2249 * non-input field.
2250 */
2251 if (v4l2_is_known_ioctl(cmd)) {
2252 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2253 if (flags & INFO_FL_CLEAR_MASK)
2254 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2255 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002256
2257 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002258 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002259
2260 /* zero out anything we don't copy from userspace */
2261 if (n < _IOC_SIZE(cmd))
2262 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002263 } else {
2264 /* read-only ioctl */
2265 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002266 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002267 }
2268
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002269 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2270 if (err < 0)
2271 goto out;
2272 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002273
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002274 if (has_array_args) {
2275 /*
2276 * When adding new types of array args, make sure that the
2277 * parent argument to ioctl (which contains the pointer to the
2278 * array) fits into sbuf (so that mbuf will still remain
2279 * unused up to here).
2280 */
2281 mbuf = kmalloc(array_size, GFP_KERNEL);
2282 err = -ENOMEM;
2283 if (NULL == mbuf)
2284 goto out_array_args;
2285 err = -EFAULT;
2286 if (copy_from_user(mbuf, user_ptr, array_size))
2287 goto out_array_args;
2288 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002289 }
2290
2291 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002292 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002293 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002294 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002295
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002296 if (has_array_args) {
2297 *kernel_ptr = user_ptr;
2298 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002299 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002300 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002301 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002302 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2303 results that must be returned. */
2304 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002305 goto out;
2306
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002307out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002308 /* Copy results into user buffer */
2309 switch (_IOC_DIR(cmd)) {
2310 case _IOC_READ:
2311 case (_IOC_WRITE | _IOC_READ):
2312 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2313 err = -EFAULT;
2314 break;
2315 }
2316
2317out:
2318 kfree(mbuf);
2319 return err;
2320}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002321EXPORT_SYMBOL(video_usercopy);
2322
2323long video_ioctl2(struct file *file,
2324 unsigned int cmd, unsigned long arg)
2325{
2326 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2327}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002328EXPORT_SYMBOL(video_ioctl2);