[media] Spec: document CREATE_BUFS behavior if count == 0
[deliverable/linux.git] / drivers / media / video / v4l2-ioctl.c
CommitLineData
35ea11ff
HV
1/*
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 *
d9b01449 11 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
35ea11ff
HV
12 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
35ea11ff
HV
17#include <linux/types.h>
18#include <linux/kernel.h>
ae6db515 19#include <linux/version.h>
35ea11ff 20
35ea11ff
HV
21#include <linux/videodev2.h>
22
35ea11ff
HV
23#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
11bbc1ca 25#include <media/v4l2-ctrls.h>
d3d7c963
SA
26#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
99cd47bc 28#include <media/v4l2-device.h>
80b36e0f 29#include <media/v4l2-chip-ident.h>
35ea11ff 30
25985edc 31/* Zero out the end of the struct pointed to by p. Everything after, but
7ecc0cf9
TP
32 * not including, the specified field is cleared. */
33#define CLEAR_AFTER_FIELD(p, field) \
34 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
35 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
36
35ea11ff
HV
37struct std_descr {
38 v4l2_std_id std;
39 const char *descr;
40};
41
42static const struct std_descr standards[] = {
43 { V4L2_STD_NTSC, "NTSC" },
44 { V4L2_STD_NTSC_M, "NTSC-M" },
45 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
46 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
47 { V4L2_STD_NTSC_443, "NTSC-443" },
48 { V4L2_STD_PAL, "PAL" },
49 { V4L2_STD_PAL_BG, "PAL-BG" },
50 { V4L2_STD_PAL_B, "PAL-B" },
51 { V4L2_STD_PAL_B1, "PAL-B1" },
52 { V4L2_STD_PAL_G, "PAL-G" },
53 { V4L2_STD_PAL_H, "PAL-H" },
54 { V4L2_STD_PAL_I, "PAL-I" },
55 { V4L2_STD_PAL_DK, "PAL-DK" },
56 { V4L2_STD_PAL_D, "PAL-D" },
57 { V4L2_STD_PAL_D1, "PAL-D1" },
58 { V4L2_STD_PAL_K, "PAL-K" },
59 { V4L2_STD_PAL_M, "PAL-M" },
60 { V4L2_STD_PAL_N, "PAL-N" },
61 { V4L2_STD_PAL_Nc, "PAL-Nc" },
62 { V4L2_STD_PAL_60, "PAL-60" },
63 { V4L2_STD_SECAM, "SECAM" },
64 { V4L2_STD_SECAM_B, "SECAM-B" },
65 { V4L2_STD_SECAM_G, "SECAM-G" },
66 { V4L2_STD_SECAM_H, "SECAM-H" },
67 { V4L2_STD_SECAM_DK, "SECAM-DK" },
68 { V4L2_STD_SECAM_D, "SECAM-D" },
69 { V4L2_STD_SECAM_K, "SECAM-K" },
70 { V4L2_STD_SECAM_K1, "SECAM-K1" },
71 { V4L2_STD_SECAM_L, "SECAM-L" },
72 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
73 { 0, "Unknown" }
74};
75
76/* video4linux standard ID conversion to standard name
77 */
78const char *v4l2_norm_to_name(v4l2_std_id id)
79{
80 u32 myid = id;
81 int i;
82
83 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
84 64 bit comparations. So, on that architecture, with some gcc
85 variants, compilation fails. Currently, the max value is 30bit wide.
86 */
87 BUG_ON(myid != id);
88
89 for (i = 0; standards[i].std; i++)
90 if (myid == standards[i].std)
91 break;
92 return standards[i].descr;
93}
94EXPORT_SYMBOL(v4l2_norm_to_name);
95
51f0b8d5
TP
96/* Returns frame period for the given standard */
97void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
98{
99 if (id & V4L2_STD_525_60) {
100 frameperiod->numerator = 1001;
101 frameperiod->denominator = 30000;
102 } else {
103 frameperiod->numerator = 1;
104 frameperiod->denominator = 25;
105 }
106}
107EXPORT_SYMBOL(v4l2_video_std_frame_period);
108
35ea11ff
HV
109/* Fill in the fields of a v4l2_standard structure according to the
110 'id' and 'transmission' parameters. Returns negative on error. */
111int v4l2_video_std_construct(struct v4l2_standard *vs,
112 int id, const char *name)
113{
51f0b8d5
TP
114 vs->id = id;
115 v4l2_video_std_frame_period(id, &vs->frameperiod);
116 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
35ea11ff
HV
117 strlcpy(vs->name, name, sizeof(vs->name));
118 return 0;
119}
120EXPORT_SYMBOL(v4l2_video_std_construct);
121
122/* ----------------------------------------------------------------- */
123/* some arrays for pretty-printing debug messages of enum types */
124
125const char *v4l2_field_names[] = {
126 [V4L2_FIELD_ANY] = "any",
127 [V4L2_FIELD_NONE] = "none",
128 [V4L2_FIELD_TOP] = "top",
129 [V4L2_FIELD_BOTTOM] = "bottom",
130 [V4L2_FIELD_INTERLACED] = "interlaced",
131 [V4L2_FIELD_SEQ_TB] = "seq-tb",
132 [V4L2_FIELD_SEQ_BT] = "seq-bt",
133 [V4L2_FIELD_ALTERNATE] = "alternate",
134 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
135 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
136};
137EXPORT_SYMBOL(v4l2_field_names);
138
139const char *v4l2_type_names[] = {
140 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
141 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
142 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
143 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
144 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
145 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
146 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
147 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
f8f3914c
PO
148 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
149 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
35ea11ff
HV
150};
151EXPORT_SYMBOL(v4l2_type_names);
152
153static const char *v4l2_memory_names[] = {
154 [V4L2_MEMORY_MMAP] = "mmap",
155 [V4L2_MEMORY_USERPTR] = "userptr",
156 [V4L2_MEMORY_OVERLAY] = "overlay",
157};
158
159#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
160 arr[a] : "unknown")
161
162/* ------------------------------------------------------------------ */
163/* debug help functions */
8ab75e3e 164
59076122
HV
165static void v4l_print_querycap(const void *arg, bool write_only)
166{
167 const struct v4l2_capability *p = arg;
168
169 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
170 "capabilities=0x%08x, device_caps=0x%08x\n",
171 p->driver, p->card, p->bus_info,
172 p->version, p->capabilities, p->device_caps);
173}
174
175static void v4l_print_enuminput(const void *arg, bool write_only)
176{
177 const struct v4l2_input *p = arg;
178
179 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
180 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
181 p->index, p->name, p->type, p->audioset, p->tuner,
182 (unsigned long long)p->std, p->status, p->capabilities);
183}
184
185static void v4l_print_enumoutput(const void *arg, bool write_only)
186{
187 const struct v4l2_output *p = arg;
188
189 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
190 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
191 p->index, p->name, p->type, p->audioset, p->modulator,
192 (unsigned long long)p->std, p->capabilities);
193}
194
195static void v4l_print_audio(const void *arg, bool write_only)
196{
197 const struct v4l2_audio *p = arg;
198
199 if (write_only)
200 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
201 else
202 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
203 p->index, p->name, p->capability, p->mode);
204}
205
206static void v4l_print_audioout(const void *arg, bool write_only)
207{
208 const struct v4l2_audioout *p = arg;
209
210 if (write_only)
211 pr_cont("index=%u\n", p->index);
212 else
213 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
214 p->index, p->name, p->capability, p->mode);
215}
216
be6c64e4
HV
217static void v4l_print_fmtdesc(const void *arg, bool write_only)
218{
219 const struct v4l2_fmtdesc *p = arg;
220
221 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%s'\n",
222 p->index, prt_names(p->type, v4l2_type_names),
223 p->flags, (p->pixelformat & 0xff),
224 (p->pixelformat >> 8) & 0xff,
225 (p->pixelformat >> 16) & 0xff,
226 (p->pixelformat >> 24) & 0xff,
227 p->description);
228}
229
230static void v4l_print_format(const void *arg, bool write_only)
231{
232 const struct v4l2_format *p = arg;
233 const struct v4l2_pix_format *pix;
234 const struct v4l2_pix_format_mplane *mp;
235 const struct v4l2_vbi_format *vbi;
236 const struct v4l2_sliced_vbi_format *sliced;
237 const struct v4l2_window *win;
238 const struct v4l2_clip *clip;
239 unsigned i;
240
241 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
242 switch (p->type) {
243 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
244 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
245 pix = &p->fmt.pix;
246 pr_cont(", width=%u, height=%u, "
247 "pixelformat=%c%c%c%c, field=%s, "
248 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
249 pix->width, pix->height,
250 (pix->pixelformat & 0xff),
251 (pix->pixelformat >> 8) & 0xff,
252 (pix->pixelformat >> 16) & 0xff,
253 (pix->pixelformat >> 24) & 0xff,
254 prt_names(pix->field, v4l2_field_names),
255 pix->bytesperline, pix->sizeimage,
256 pix->colorspace);
257 break;
258 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
259 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
260 mp = &p->fmt.pix_mp;
261 pr_cont(", width=%u, height=%u, "
262 "format=%c%c%c%c, field=%s, "
263 "colorspace=%d, num_planes=%u\n",
264 mp->width, mp->height,
265 (mp->pixelformat & 0xff),
266 (mp->pixelformat >> 8) & 0xff,
267 (mp->pixelformat >> 16) & 0xff,
268 (mp->pixelformat >> 24) & 0xff,
269 prt_names(mp->field, v4l2_field_names),
270 mp->colorspace, mp->num_planes);
271 for (i = 0; i < mp->num_planes; i++)
272 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
273 mp->plane_fmt[i].bytesperline,
274 mp->plane_fmt[i].sizeimage);
275 break;
276 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
277 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
278 win = &p->fmt.win;
279 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
280 "chromakey=0x%08x, bitmap=%p, "
281 "global_alpha=0x%02x\n",
282 win->w.width, win->w.height,
283 win->w.left, win->w.top,
284 prt_names(win->field, v4l2_field_names),
285 win->chromakey, win->bitmap, win->global_alpha);
286 clip = win->clips;
287 for (i = 0; i < win->clipcount; i++) {
288 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
289 i, clip->c.width, clip->c.height,
290 clip->c.left, clip->c.top);
291 clip = clip->next;
292 }
293 break;
294 case V4L2_BUF_TYPE_VBI_CAPTURE:
295 case V4L2_BUF_TYPE_VBI_OUTPUT:
296 vbi = &p->fmt.vbi;
297 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
298 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
299 vbi->sampling_rate, vbi->offset,
300 vbi->samples_per_line,
301 (vbi->sample_format & 0xff),
302 (vbi->sample_format >> 8) & 0xff,
303 (vbi->sample_format >> 16) & 0xff,
304 (vbi->sample_format >> 24) & 0xff,
305 vbi->start[0], vbi->start[1],
306 vbi->count[0], vbi->count[1]);
307 break;
308 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
309 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
310 sliced = &p->fmt.sliced;
311 pr_cont(", service_set=0x%08x, io_size=%d\n",
312 sliced->service_set, sliced->io_size);
313 for (i = 0; i < 24; i++)
314 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
315 sliced->service_lines[0][i],
316 sliced->service_lines[1][i]);
317 break;
318 case V4L2_BUF_TYPE_PRIVATE:
319 pr_cont("\n");
320 break;
321 }
322}
323
324static void v4l_print_framebuffer(const void *arg, bool write_only)
325{
326 const struct v4l2_framebuffer *p = arg;
327
328 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
329 "height=%u, pixelformat=%c%c%c%c, "
330 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
331 p->capability, p->flags, p->base,
332 p->fmt.width, p->fmt.height,
333 (p->fmt.pixelformat & 0xff),
334 (p->fmt.pixelformat >> 8) & 0xff,
335 (p->fmt.pixelformat >> 16) & 0xff,
336 (p->fmt.pixelformat >> 24) & 0xff,
337 p->fmt.bytesperline, p->fmt.sizeimage,
338 p->fmt.colorspace);
339}
340
e325b244
HV
341static void v4l_print_buftype(const void *arg, bool write_only)
342{
343 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
344}
345
4b1e2e4d
HV
346static void v4l_print_modulator(const void *arg, bool write_only)
347{
348 const struct v4l2_modulator *p = arg;
349
350 if (write_only)
351 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
352 else
353 pr_cont("index=%u, name=%s, capability=0x%x, "
354 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
355 p->index, p->name, p->capability,
356 p->rangelow, p->rangehigh, p->txsubchans);
357}
358
359static void v4l_print_tuner(const void *arg, bool write_only)
360{
361 const struct v4l2_tuner *p = arg;
362
363 if (write_only)
364 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
365 else
366 pr_cont("index=%u, name=%s, type=%u, capability=0x%x, "
367 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
368 "rxsubchans=0x%x, audmode=%u\n",
369 p->index, p->name, p->type,
370 p->capability, p->rangelow,
371 p->rangehigh, p->signal, p->afc,
372 p->rxsubchans, p->audmode);
373}
374
375static void v4l_print_frequency(const void *arg, bool write_only)
376{
377 const struct v4l2_frequency *p = arg;
378
379 pr_cont("tuner=%u, type=%u, frequency=%u\n",
380 p->tuner, p->type, p->frequency);
381}
382
383static void v4l_print_standard(const void *arg, bool write_only)
384{
385 const struct v4l2_standard *p = arg;
386
387 pr_cont("index=%u, id=0x%Lx, name=%s, fps=%u/%u, "
388 "framelines=%u\n", p->index,
389 (unsigned long long)p->id, p->name,
390 p->frameperiod.numerator,
391 p->frameperiod.denominator,
392 p->framelines);
393}
394
395static void v4l_print_std(const void *arg, bool write_only)
396{
397 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
398}
399
400static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
401{
402 const struct v4l2_hw_freq_seek *p = arg;
403
404 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
405 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
406}
407
eb3728ed 408static void v4l_print_requestbuffers(const void *arg, bool write_only)
59076122 409{
eb3728ed
HV
410 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));
59076122
HV
416}
417
eb3728ed 418static void v4l_print_buffer(const void *arg, bool write_only)
35ea11ff 419{
eb3728ed
HV
420 const struct v4l2_buffer *p = arg;
421 const struct v4l2_timecode *tc = &p->timecode;
422 const struct v4l2_plane *plane;
d14e6d76 423 int i;
35ea11ff 424
eb3728ed
HV
425 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
426 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
35ea11ff
HV
427 p->timestamp.tv_sec / 3600,
428 (int)(p->timestamp.tv_sec / 60) % 60,
429 (int)(p->timestamp.tv_sec % 60),
b045979d 430 (long)p->timestamp.tv_usec,
35ea11ff
HV
431 p->index,
432 prt_names(p->type, v4l2_type_names),
eb3728ed
HV
433 p->flags, prt_names(p->field, v4l2_field_names),
434 p->sequence, prt_names(p->memory, v4l2_memory_names));
d14e6d76
PO
435
436 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
eb3728ed 437 pr_cont("\n");
d14e6d76
PO
438 for (i = 0; i < p->length; ++i) {
439 plane = &p->m.planes[i];
eb3728ed
HV
440 printk(KERN_DEBUG
441 "plane %d: bytesused=%d, data_offset=0x%08x "
442 "offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
443 i, plane->bytesused, plane->data_offset,
444 plane->m.userptr, plane->length);
445 }
446 } else {
eb3728ed 447 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
448 p->bytesused, p->m.userptr, p->length);
449 }
450
eb3728ed
HV
451 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
452 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
35ea11ff
HV
453 tc->hours, tc->minutes, tc->seconds,
454 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
455}
456
eb3728ed
HV
457static 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
efbceecd
HV
494static 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
d84f2d94 537static void v4l_print_cropcap(const void *arg, bool write_only)
eb3728ed 538{
d84f2d94
HV
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);
eb3728ed
HV
550}
551
d84f2d94 552static void v4l_print_crop(const void *arg, bool write_only)
35ea11ff 553{
d84f2d94
HV
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
d806f2df
HV
572static 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
f16f77b0
HV
611static 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
efc626b0 637static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
d84f2d94 638{
efc626b0
HV
639 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);
d84f2d94 643}
35ea11ff 644
efc626b0 645static void v4l_print_dv_preset(const void *arg, bool write_only)
f16f77b0 646{
efc626b0
HV
647 const struct v4l2_dv_preset *p = arg;
648
649 pr_cont("preset=%u\n", p->preset);
f16f77b0
HV
650}
651
efc626b0 652static void v4l_print_dv_timings(const void *arg, bool write_only)
5d7758ee 653{
efc626b0
HV
654 const struct v4l2_dv_timings *p = arg;
655
5d7758ee
HV
656 switch (p->type) {
657 case V4L2_DV_BT_656_1120:
efc626b0
HV
658 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",
5d7758ee
HV
666 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:
efc626b0 676 pr_cont("type=%d\n", p->type);
5d7758ee
HV
677 break;
678 }
679}
680
efc626b0
HV
681static 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
458aa4a6
HV
708static 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
efc626b0
HV
827static void v4l_print_u32(const void *arg, bool write_only)
828{
829 pr_cont("value=%u\n", *(const u32 *)arg);
830}
831
832static void v4l_print_newline(const void *arg, bool write_only)
833{
834 pr_cont("\n");
835}
836
f18d8e07
HV
837static void v4l_print_default(const void *arg, bool write_only)
838{
839 pr_cont("driver-specific ioctl\n");
840}
841
efbceecd 842static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
35ea11ff
HV
843{
844 __u32 i;
845
846 /* zero the reserved fields */
847 c->reserved[0] = c->reserved[1] = 0;
6b5a9492 848 for (i = 0; i < c->count; i++)
35ea11ff 849 c->controls[i].reserved2[0] = 0;
6b5a9492 850
35ea11ff
HV
851 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
852 when using extended controls.
853 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
854 is it allowed for backwards compatibility.
855 */
856 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
857 return 0;
858 /* Check that all controls are from the same control class. */
859 for (i = 0; i < c->count; i++) {
860 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
861 c->error_idx = i;
862 return 0;
863 }
864 }
865 return 1;
866}
867
a399810c 868static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
35ea11ff 869{
a399810c
HV
870 if (ops == NULL)
871 return -EINVAL;
872
35ea11ff
HV
873 switch (type) {
874 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
d14e6d76
PO
875 if (ops->vidioc_g_fmt_vid_cap ||
876 ops->vidioc_g_fmt_vid_cap_mplane)
877 return 0;
878 break;
879 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
880 if (ops->vidioc_g_fmt_vid_cap_mplane)
35ea11ff
HV
881 return 0;
882 break;
883 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1175d613 884 if (ops->vidioc_g_fmt_vid_overlay)
35ea11ff
HV
885 return 0;
886 break;
887 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
d14e6d76
PO
888 if (ops->vidioc_g_fmt_vid_out ||
889 ops->vidioc_g_fmt_vid_out_mplane)
890 return 0;
891 break;
892 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
893 if (ops->vidioc_g_fmt_vid_out_mplane)
35ea11ff
HV
894 return 0;
895 break;
896 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1175d613 897 if (ops->vidioc_g_fmt_vid_out_overlay)
35ea11ff
HV
898 return 0;
899 break;
900 case V4L2_BUF_TYPE_VBI_CAPTURE:
1175d613 901 if (ops->vidioc_g_fmt_vbi_cap)
35ea11ff
HV
902 return 0;
903 break;
904 case V4L2_BUF_TYPE_VBI_OUTPUT:
1175d613 905 if (ops->vidioc_g_fmt_vbi_out)
35ea11ff
HV
906 return 0;
907 break;
908 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1175d613 909 if (ops->vidioc_g_fmt_sliced_vbi_cap)
35ea11ff
HV
910 return 0;
911 break;
912 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1175d613 913 if (ops->vidioc_g_fmt_sliced_vbi_out)
35ea11ff
HV
914 return 0;
915 break;
916 case V4L2_BUF_TYPE_PRIVATE:
1175d613 917 if (ops->vidioc_g_fmt_type_private)
35ea11ff
HV
918 return 0;
919 break;
920 }
921 return -EINVAL;
922}
923
59076122
HV
924static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
925 struct file *file, void *fh, void *arg)
926{
927 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
928
929 cap->version = LINUX_VERSION_CODE;
930 return ops->vidioc_querycap(file, fh, cap);
931}
932
933static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
934 struct file *file, void *fh, void *arg)
935{
936 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
937}
938
939static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
940 struct file *file, void *fh, void *arg)
941{
942 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
943}
944
d0547cba
HV
945static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
946 struct file *file, void *fh, void *arg)
947{
948 struct video_device *vfd;
949 u32 *p = arg;
950
951 if (ops->vidioc_g_priority)
952 return ops->vidioc_g_priority(file, fh, arg);
953 vfd = video_devdata(file);
954 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
955 return 0;
956}
957
958static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
959 struct file *file, void *fh, void *arg)
960{
961 struct video_device *vfd;
962 struct v4l2_fh *vfh;
963 u32 *p = arg;
964
965 if (ops->vidioc_s_priority)
966 return ops->vidioc_s_priority(file, fh, *p);
967 vfd = video_devdata(file);
968 vfh = file->private_data;
969 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
970}
971
59076122
HV
972static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
973 struct file *file, void *fh, void *arg)
974{
975 struct v4l2_input *p = arg;
976
977 /*
978 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
979 * CAP_STD here based on ioctl handler provided by the
980 * driver. If the driver doesn't support these
981 * for a specific input, it must override these flags.
982 */
983 if (ops->vidioc_s_std)
984 p->capabilities |= V4L2_IN_CAP_STD;
985 if (ops->vidioc_s_dv_preset)
986 p->capabilities |= V4L2_IN_CAP_PRESETS;
987 if (ops->vidioc_s_dv_timings)
988 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
989
990 return ops->vidioc_enum_input(file, fh, p);
991}
992
993static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
994 struct file *file, void *fh, void *arg)
995{
996 struct v4l2_output *p = arg;
997
998 /*
999 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1000 * CAP_STD here based on ioctl handler provided by the
1001 * driver. If the driver doesn't support these
1002 * for a specific output, it must override these flags.
1003 */
1004 if (ops->vidioc_s_std)
1005 p->capabilities |= V4L2_OUT_CAP_STD;
1006 if (ops->vidioc_s_dv_preset)
1007 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1008 if (ops->vidioc_s_dv_timings)
1009 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1010
1011 return ops->vidioc_enum_output(file, fh, p);
1012}
1013
be6c64e4
HV
1014static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1015 struct file *file, void *fh, void *arg)
1016{
1017 struct v4l2_fmtdesc *p = arg;
1018
1019 switch (p->type) {
1020 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1021 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1022 break;
1023 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1024 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1025 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1026 break;
1027 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1028 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1029 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1030 break;
1031 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1032 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1033 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1034 break;
1035 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1036 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1037 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1038 break;
1039 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1040 case V4L2_BUF_TYPE_PRIVATE:
1041 if (unlikely(!ops->vidioc_enum_fmt_type_private))
1042 break;
1043 return ops->vidioc_enum_fmt_type_private(file, fh, arg);
1044 }
1045 return -EINVAL;
1046}
1047
1048static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1049 struct file *file, void *fh, void *arg)
1050{
1051 struct v4l2_format *p = arg;
1052
1053 switch (p->type) {
1054 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1055 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1056 break;
1057 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1058 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1059 if (unlikely(!ops->vidioc_g_fmt_vid_cap_mplane))
1060 break;
1061 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1062 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1063 if (unlikely(!ops->vidioc_g_fmt_vid_overlay))
1064 break;
1065 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1066 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1067 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1068 break;
1069 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1070 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1071 if (unlikely(!ops->vidioc_g_fmt_vid_out_mplane))
1072 break;
1073 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1074 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1075 if (unlikely(!ops->vidioc_g_fmt_vid_out_overlay))
1076 break;
1077 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1078 case V4L2_BUF_TYPE_VBI_CAPTURE:
1079 if (unlikely(!ops->vidioc_g_fmt_vbi_cap))
1080 break;
1081 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1082 case V4L2_BUF_TYPE_VBI_OUTPUT:
1083 if (unlikely(!ops->vidioc_g_fmt_vbi_out))
1084 break;
1085 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1086 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1087 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_cap))
1088 break;
1089 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1090 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1091 if (unlikely(!ops->vidioc_g_fmt_sliced_vbi_out))
1092 break;
1093 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1094 case V4L2_BUF_TYPE_PRIVATE:
1095 if (unlikely(!ops->vidioc_g_fmt_type_private))
1096 break;
1097 return ops->vidioc_g_fmt_type_private(file, fh, arg);
1098 }
1099 return -EINVAL;
1100}
1101
1102static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1103 struct file *file, void *fh, void *arg)
1104{
1105 struct v4l2_format *p = arg;
1106
1107 switch (p->type) {
1108 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1109 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1110 break;
1111 CLEAR_AFTER_FIELD(p, fmt.pix);
1112 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1113 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1114 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1115 break;
1116 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1117 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1118 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1119 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1120 break;
1121 CLEAR_AFTER_FIELD(p, fmt.win);
1122 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1123 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1124 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1125 break;
1126 CLEAR_AFTER_FIELD(p, fmt.pix);
1127 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1128 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1129 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1130 break;
1131 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1132 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1133 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1134 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1135 break;
1136 CLEAR_AFTER_FIELD(p, fmt.win);
1137 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1138 case V4L2_BUF_TYPE_VBI_CAPTURE:
1139 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1140 break;
1141 CLEAR_AFTER_FIELD(p, fmt.vbi);
1142 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1143 case V4L2_BUF_TYPE_VBI_OUTPUT:
1144 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1145 break;
1146 CLEAR_AFTER_FIELD(p, fmt.vbi);
1147 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1148 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1149 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1150 break;
1151 CLEAR_AFTER_FIELD(p, fmt.sliced);
1152 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1153 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1154 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1155 break;
1156 CLEAR_AFTER_FIELD(p, fmt.sliced);
1157 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1158 case V4L2_BUF_TYPE_PRIVATE:
1159 if (unlikely(!ops->vidioc_s_fmt_type_private))
1160 break;
1161 return ops->vidioc_s_fmt_type_private(file, fh, arg);
1162 }
1163 return -EINVAL;
1164}
1165
1166static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1167 struct file *file, void *fh, void *arg)
1168{
1169 struct v4l2_format *p = arg;
1170
1171 switch (p->type) {
1172 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1173 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1174 break;
1175 CLEAR_AFTER_FIELD(p, fmt.pix);
1176 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1177 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1178 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1179 break;
1180 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1181 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1182 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1183 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1184 break;
1185 CLEAR_AFTER_FIELD(p, fmt.win);
1186 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1187 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1188 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1189 break;
1190 CLEAR_AFTER_FIELD(p, fmt.pix);
1191 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1192 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1193 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1194 break;
1195 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1196 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1197 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1198 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1199 break;
1200 CLEAR_AFTER_FIELD(p, fmt.win);
1201 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1202 case V4L2_BUF_TYPE_VBI_CAPTURE:
1203 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1204 break;
1205 CLEAR_AFTER_FIELD(p, fmt.vbi);
1206 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1207 case V4L2_BUF_TYPE_VBI_OUTPUT:
1208 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1209 break;
1210 CLEAR_AFTER_FIELD(p, fmt.vbi);
1211 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1212 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1213 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1214 break;
1215 CLEAR_AFTER_FIELD(p, fmt.sliced);
1216 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1217 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1218 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1219 break;
1220 CLEAR_AFTER_FIELD(p, fmt.sliced);
1221 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1222 case V4L2_BUF_TYPE_PRIVATE:
1223 if (unlikely(!ops->vidioc_try_fmt_type_private))
1224 break;
1225 return ops->vidioc_try_fmt_type_private(file, fh, arg);
1226 }
1227 return -EINVAL;
1228}
1229
e325b244
HV
1230static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1231 struct file *file, void *fh, void *arg)
1232{
1233 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1234}
1235
1236static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1237 struct file *file, void *fh, void *arg)
1238{
1239 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1240}
1241
4b1e2e4d
HV
1242static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1243 struct file *file, void *fh, void *arg)
1244{
1245 struct video_device *vfd = video_devdata(file);
1246 struct v4l2_tuner *p = arg;
1247
1248 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1249 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1250 return ops->vidioc_g_tuner(file, fh, p);
1251}
1252
1253static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1254 struct file *file, void *fh, void *arg)
1255{
1256 struct video_device *vfd = video_devdata(file);
1257 struct v4l2_tuner *p = arg;
1258
1259 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1260 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1261 return ops->vidioc_s_tuner(file, fh, p);
1262}
1263
1264static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1265 struct file *file, void *fh, void *arg)
1266{
1267 struct video_device *vfd = video_devdata(file);
1268 struct v4l2_frequency *p = arg;
1269
1270 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1271 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1272 return ops->vidioc_g_frequency(file, fh, p);
1273}
1274
1275static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1276 struct file *file, void *fh, void *arg)
1277{
1278 struct video_device *vfd = video_devdata(file);
1279 struct v4l2_frequency *p = arg;
1280 enum v4l2_tuner_type type;
1281
1282 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1283 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1284 if (p->type != type)
1285 return -EINVAL;
1286 return ops->vidioc_s_frequency(file, fh, p);
1287}
1288
1289static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1290 struct file *file, void *fh, void *arg)
1291{
1292 struct video_device *vfd = video_devdata(file);
1293 struct v4l2_standard *p = arg;
1294 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1295 unsigned int index = p->index, i, j = 0;
1296 const char *descr = "";
1297
1298 /* Return norm array in a canonical way */
1299 for (i = 0; i <= index && id; i++) {
1300 /* last std value in the standards array is 0, so this
1301 while always ends there since (id & 0) == 0. */
1302 while ((id & standards[j].std) != standards[j].std)
1303 j++;
1304 curr_id = standards[j].std;
1305 descr = standards[j].descr;
1306 j++;
1307 if (curr_id == 0)
1308 break;
1309 if (curr_id != V4L2_STD_PAL &&
1310 curr_id != V4L2_STD_SECAM &&
1311 curr_id != V4L2_STD_NTSC)
1312 id &= ~curr_id;
1313 }
1314 if (i <= index)
1315 return -EINVAL;
1316
1317 v4l2_video_std_construct(p, curr_id, descr);
1318 return 0;
1319}
1320
1321static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1322 struct file *file, void *fh, void *arg)
1323{
1324 struct video_device *vfd = video_devdata(file);
1325 v4l2_std_id *id = arg;
1326
1327 /* Calls the specific handler */
1328 if (ops->vidioc_g_std)
1329 return ops->vidioc_g_std(file, fh, arg);
1330 if (vfd->current_norm) {
1331 *id = vfd->current_norm;
1332 return 0;
1333 }
1334 return -ENOTTY;
1335}
1336
1337static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1338 struct file *file, void *fh, void *arg)
1339{
1340 struct video_device *vfd = video_devdata(file);
1341 v4l2_std_id *id = arg, norm;
1342 int ret;
1343
1344 norm = (*id) & vfd->tvnorms;
1345 if (vfd->tvnorms && !norm) /* Check if std is supported */
1346 return -EINVAL;
1347
1348 /* Calls the specific handler */
1349 ret = ops->vidioc_s_std(file, fh, &norm);
1350
1351 /* Updates standard information */
1352 if (ret >= 0)
1353 vfd->current_norm = norm;
1354 return ret;
1355}
1356
1357static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1358 struct file *file, void *fh, void *arg)
1359{
1360 struct video_device *vfd = video_devdata(file);
1361 v4l2_std_id *p = arg;
1362
1363 /*
1364 * If nothing detected, it should return all supported
1365 * standard.
1366 * Drivers just need to mask the std argument, in order
1367 * to remove the standards that don't apply from the mask.
1368 * This means that tuners, audio and video decoders can join
1369 * their efforts to improve the standards detection.
1370 */
1371 *p = vfd->tvnorms;
1372 return ops->vidioc_querystd(file, fh, arg);
1373}
1374
1375static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1376 struct file *file, void *fh, void *arg)
1377{
1378 struct video_device *vfd = video_devdata(file);
1379 struct v4l2_hw_freq_seek *p = arg;
1380 enum v4l2_tuner_type type;
1381
1382 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1383 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1384 if (p->type != type)
1385 return -EINVAL;
1386 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1387}
1388
eb3728ed
HV
1389static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1390 struct file *file, void *fh, void *arg)
1391{
1392 struct v4l2_requestbuffers *p = arg;
1393 int ret = check_fmt(ops, p->type);
1394
1395 if (ret)
1396 return ret;
1397
1398 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1399 CLEAR_AFTER_FIELD(p, memory);
1400
1401 return ops->vidioc_reqbufs(file, fh, p);
1402}
1403
1404static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1405 struct file *file, void *fh, void *arg)
1406{
1407 struct v4l2_buffer *p = arg;
1408 int ret = check_fmt(ops, p->type);
1409
1410 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1411}
1412
1413static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1414 struct file *file, void *fh, void *arg)
1415{
1416 struct v4l2_buffer *p = arg;
1417 int ret = check_fmt(ops, p->type);
1418
1419 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1420}
1421
1422static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1423 struct file *file, void *fh, void *arg)
1424{
1425 struct v4l2_buffer *p = arg;
1426 int ret = check_fmt(ops, p->type);
1427
1428 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1429}
1430
1431static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1432 struct file *file, void *fh, void *arg)
1433{
1434 struct v4l2_create_buffers *create = arg;
1435 int ret = check_fmt(ops, create->format.type);
1436
1437 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1438}
1439
1440static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1441 struct file *file, void *fh, void *arg)
1442{
1443 struct v4l2_buffer *b = arg;
1444 int ret = check_fmt(ops, b->type);
1445
1446 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1447}
1448
1449static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1450 struct file *file, void *fh, void *arg)
1451{
1452 struct video_device *vfd = video_devdata(file);
1453 struct v4l2_streamparm *p = arg;
1454 v4l2_std_id std;
1455 int ret = check_fmt(ops, p->type);
1456
1457 if (ret)
1458 return ret;
1459 if (ops->vidioc_g_parm)
1460 return ops->vidioc_g_parm(file, fh, p);
1461 std = vfd->current_norm;
1462 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1463 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1464 return -EINVAL;
1465 p->parm.capture.readbuffers = 2;
1466 if (ops->vidioc_g_std)
1467 ret = ops->vidioc_g_std(file, fh, &std);
1468 if (ret == 0)
1469 v4l2_video_std_frame_period(std,
1470 &p->parm.capture.timeperframe);
1471 return ret;
1472}
1473
1474static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1475 struct file *file, void *fh, void *arg)
1476{
1477 struct v4l2_streamparm *p = arg;
1478 int ret = check_fmt(ops, p->type);
1479
1480 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1481}
1482
efbceecd
HV
1483static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1484 struct file *file, void *fh, void *arg)
1485{
1486 struct video_device *vfd = video_devdata(file);
1487 struct v4l2_queryctrl *p = arg;
1488 struct v4l2_fh *vfh = fh;
1489
1490 if (vfh && vfh->ctrl_handler)
1491 return v4l2_queryctrl(vfh->ctrl_handler, p);
1492 if (vfd->ctrl_handler)
1493 return v4l2_queryctrl(vfd->ctrl_handler, p);
1494 if (ops->vidioc_queryctrl)
1495 return ops->vidioc_queryctrl(file, fh, p);
1496 return -ENOTTY;
1497}
1498
1499static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1500 struct file *file, void *fh, void *arg)
1501{
1502 struct video_device *vfd = video_devdata(file);
1503 struct v4l2_querymenu *p = arg;
1504 struct v4l2_fh *vfh = fh;
1505
1506 if (vfh && vfh->ctrl_handler)
1507 return v4l2_querymenu(vfh->ctrl_handler, p);
1508 if (vfd->ctrl_handler)
1509 return v4l2_querymenu(vfd->ctrl_handler, p);
1510 if (ops->vidioc_querymenu)
1511 return ops->vidioc_querymenu(file, fh, p);
1512 return -ENOTTY;
1513}
1514
1515static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1516 struct file *file, void *fh, void *arg)
1517{
1518 struct video_device *vfd = video_devdata(file);
1519 struct v4l2_control *p = arg;
1520 struct v4l2_fh *vfh = fh;
1521 struct v4l2_ext_controls ctrls;
1522 struct v4l2_ext_control ctrl;
1523
1524 if (vfh && vfh->ctrl_handler)
1525 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1526 if (vfd->ctrl_handler)
1527 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1528 if (ops->vidioc_g_ctrl)
1529 return ops->vidioc_g_ctrl(file, fh, p);
1530 if (ops->vidioc_g_ext_ctrls == NULL)
1531 return -ENOTTY;
1532
1533 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1534 ctrls.count = 1;
1535 ctrls.controls = &ctrl;
1536 ctrl.id = p->id;
1537 ctrl.value = p->value;
1538 if (check_ext_ctrls(&ctrls, 1)) {
1539 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1540
1541 if (ret == 0)
1542 p->value = ctrl.value;
1543 return ret;
1544 }
1545 return -EINVAL;
1546}
1547
1548static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1549 struct file *file, void *fh, void *arg)
1550{
1551 struct video_device *vfd = video_devdata(file);
1552 struct v4l2_control *p = arg;
1553 struct v4l2_fh *vfh = fh;
1554 struct v4l2_ext_controls ctrls;
1555 struct v4l2_ext_control ctrl;
1556
1557 if (vfh && vfh->ctrl_handler)
1558 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1559 if (vfd->ctrl_handler)
1560 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1561 if (ops->vidioc_s_ctrl)
1562 return ops->vidioc_s_ctrl(file, fh, p);
1563 if (ops->vidioc_s_ext_ctrls == NULL)
1564 return -ENOTTY;
1565
1566 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1567 ctrls.count = 1;
1568 ctrls.controls = &ctrl;
1569 ctrl.id = p->id;
1570 ctrl.value = p->value;
1571 if (check_ext_ctrls(&ctrls, 1))
1572 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1573 return -EINVAL;
1574}
1575
1576static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1577 struct file *file, void *fh, void *arg)
1578{
1579 struct video_device *vfd = video_devdata(file);
1580 struct v4l2_ext_controls *p = arg;
1581 struct v4l2_fh *vfh = fh;
1582
1583 p->error_idx = p->count;
1584 if (vfh && vfh->ctrl_handler)
1585 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1586 if (vfd->ctrl_handler)
1587 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1588 if (ops->vidioc_g_ext_ctrls == NULL)
1589 return -ENOTTY;
1590 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1591 -EINVAL;
1592}
1593
1594static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1595 struct file *file, void *fh, void *arg)
1596{
1597 struct video_device *vfd = video_devdata(file);
1598 struct v4l2_ext_controls *p = arg;
1599 struct v4l2_fh *vfh = fh;
1600
1601 p->error_idx = p->count;
1602 if (vfh && vfh->ctrl_handler)
1603 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1604 if (vfd->ctrl_handler)
1605 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1606 if (ops->vidioc_s_ext_ctrls == NULL)
1607 return -ENOTTY;
1608 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1609 -EINVAL;
1610}
1611
1612static int v4l_try_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;
1617 struct v4l2_fh *vfh = fh;
1618
1619 p->error_idx = p->count;
1620 if (vfh && vfh->ctrl_handler)
1621 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1622 if (vfd->ctrl_handler)
1623 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1624 if (ops->vidioc_try_ext_ctrls == NULL)
1625 return -ENOTTY;
1626 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1627 -EINVAL;
1628}
1629
d84f2d94
HV
1630static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1631 struct file *file, void *fh, void *arg)
1632{
1633 struct v4l2_crop *p = arg;
1634 struct v4l2_selection s = {
1635 .type = p->type,
1636 };
1637 int ret;
1638
1639 if (ops->vidioc_g_crop)
1640 return ops->vidioc_g_crop(file, fh, p);
1641 /* simulate capture crop using selection api */
1642
1643 /* crop means compose for output devices */
1644 if (V4L2_TYPE_IS_OUTPUT(p->type))
1645 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1646 else
1647 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1648
1649 ret = ops->vidioc_g_selection(file, fh, &s);
1650
1651 /* copying results to old structure on success */
1652 if (!ret)
1653 p->c = s.r;
1654 return ret;
1655}
1656
1657static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1658 struct file *file, void *fh, void *arg)
1659{
1660 struct v4l2_crop *p = arg;
1661 struct v4l2_selection s = {
1662 .type = p->type,
1663 .r = p->c,
1664 };
1665
1666 if (ops->vidioc_s_crop)
1667 return ops->vidioc_s_crop(file, fh, p);
1668 /* simulate capture crop using selection api */
1669
1670 /* crop means compose for output devices */
1671 if (V4L2_TYPE_IS_OUTPUT(p->type))
1672 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1673 else
1674 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1675
1676 return ops->vidioc_s_selection(file, fh, &s);
1677}
1678
1679static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1680 struct file *file, void *fh, void *arg)
1681{
1682 struct v4l2_cropcap *p = arg;
1683 struct v4l2_selection s = { .type = p->type };
1684 int ret;
1685
1686 if (ops->vidioc_cropcap)
1687 return ops->vidioc_cropcap(file, fh, p);
1688
1689 /* obtaining bounds */
1690 if (V4L2_TYPE_IS_OUTPUT(p->type))
1691 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1692 else
1693 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1694
1695 ret = ops->vidioc_g_selection(file, fh, &s);
1696 if (ret)
1697 return ret;
1698 p->bounds = s.r;
1699
1700 /* obtaining defrect */
1701 if (V4L2_TYPE_IS_OUTPUT(p->type))
1702 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1703 else
1704 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1705
1706 ret = ops->vidioc_g_selection(file, fh, &s);
1707 if (ret)
1708 return ret;
1709 p->defrect = s.r;
1710
1711 /* setting trivial pixelaspect */
1712 p->pixelaspect.numerator = 1;
1713 p->pixelaspect.denominator = 1;
1714 return 0;
1715}
1716
f16f77b0
HV
1717static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1718 struct file *file, void *fh, void *arg)
1719{
1720 struct video_device *vfd = video_devdata(file);
1721 int ret;
1722
1723 if (vfd->v4l2_dev)
1724 pr_info("%s: ================= START STATUS =================\n",
1725 vfd->v4l2_dev->name);
1726 ret = ops->vidioc_log_status(file, fh);
1727 if (vfd->v4l2_dev)
1728 pr_info("%s: ================== END STATUS ==================\n",
1729 vfd->v4l2_dev->name);
1730 return ret;
1731}
1732
1733static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1734 struct file *file, void *fh, void *arg)
1735{
1736#ifdef CONFIG_VIDEO_ADV_DEBUG
1737 struct v4l2_dbg_register *p = arg;
1738
1739 if (!capable(CAP_SYS_ADMIN))
1740 return -EPERM;
1741 return ops->vidioc_g_register(file, fh, p);
1742#else
1743 return -ENOTTY;
1744#endif
1745}
1746
1747static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1748 struct file *file, void *fh, void *arg)
1749{
1750#ifdef CONFIG_VIDEO_ADV_DEBUG
1751 struct v4l2_dbg_register *p = arg;
1752
1753 if (!capable(CAP_SYS_ADMIN))
1754 return -EPERM;
1755 return ops->vidioc_s_register(file, fh, p);
1756#else
1757 return -ENOTTY;
1758#endif
1759}
1760
1761static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1762 struct file *file, void *fh, void *arg)
1763{
1764 struct v4l2_dbg_chip_ident *p = arg;
1765
1766 p->ident = V4L2_IDENT_NONE;
1767 p->revision = 0;
1768 return ops->vidioc_g_chip_ident(file, fh, p);
1769}
1770
458aa4a6
HV
1771static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1772 struct file *file, void *fh, void *arg)
1773{
1774 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1775}
1776
1777static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1778 struct file *file, void *fh, void *arg)
1779{
1780 return ops->vidioc_subscribe_event(fh, arg);
1781}
1782
1783static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1784 struct file *file, void *fh, void *arg)
1785{
1786 return ops->vidioc_unsubscribe_event(fh, arg);
1787}
1788
1789static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1790 struct file *file, void *fh, void *arg)
1791{
1792 struct v4l2_sliced_vbi_cap *p = arg;
1793
1794 /* Clear up to type, everything after type is zeroed already */
1795 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1796
1797 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1798}
1799
4839e6c2
HV
1800struct v4l2_ioctl_info {
1801 unsigned int ioctl;
27cd2aba 1802 u32 flags;
4839e6c2 1803 const char * const name;
86748f35
HV
1804 union {
1805 u32 offset;
1806 int (*func)(const struct v4l2_ioctl_ops *ops,
1807 struct file *file, void *fh, void *p);
1808 };
1809 void (*debug)(const void *arg, bool write_only);
4839e6c2
HV
1810};
1811
1812/* This control needs a priority check */
1813#define INFO_FL_PRIO (1 << 0)
1814/* This control can be valid if the filehandle passes a control handler. */
1815#define INFO_FL_CTRL (1 << 1)
86748f35
HV
1816/* This is a standard ioctl, no need for special code */
1817#define INFO_FL_STD (1 << 2)
1818/* This is ioctl has its own function */
1819#define INFO_FL_FUNC (1 << 3)
27cd2aba
HV
1820/* Zero struct from after the field to the end */
1821#define INFO_FL_CLEAR(v4l2_struct, field) \
1822 ((offsetof(struct v4l2_struct, field) + \
1823 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1824#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
4839e6c2 1825
86748f35
HV
1826#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1827 [_IOC_NR(_ioctl)] = { \
1828 .ioctl = _ioctl, \
1829 .flags = _flags | INFO_FL_STD, \
1830 .name = #_ioctl, \
1831 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1832 .debug = _debug, \
1833 }
1834
1835#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1836 [_IOC_NR(_ioctl)] = { \
1837 .ioctl = _ioctl, \
1838 .flags = _flags | INFO_FL_FUNC, \
1839 .name = #_ioctl, \
1840 .func = _func, \
1841 .debug = _debug, \
1842 }
1843
4839e6c2 1844static struct v4l2_ioctl_info v4l2_ioctls[] = {
59076122 1845 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
be6c64e4
HV
1846 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1847 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1848 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
eb3728ed
HV
1849 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO),
1850 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_CLEAR(v4l2_buffer, length)),
be6c64e4
HV
1851 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1852 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
e325b244 1853 IOCTL_INFO_STD(VIDIOC_OVERLAY, vidioc_overlay, v4l_print_u32, INFO_FL_PRIO),
eb3728ed
HV
1854 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, 0),
1855 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, 0),
e325b244
HV
1856 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO),
1857 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO),
eb3728ed
HV
1858 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1859 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
4b1e2e4d
HV
1860 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1861 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1862 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
59076122 1863 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
efbceecd
HV
1864 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1865 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
4b1e2e4d
HV
1866 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1867 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
59076122
HV
1868 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1869 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
efbceecd
HV
1870 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1871 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
59076122
HV
1872 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
1873 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
1874 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
1875 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
1876 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
1877 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
1878 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
4b1e2e4d
HV
1879 IOCTL_INFO_STD(VIDIOC_G_MODULATOR, vidioc_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
1880 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
1881 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
1882 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
d84f2d94
HV
1883 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
1884 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
1885 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
1886 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
1887 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
d806f2df
HV
1888 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
1889 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
4b1e2e4d 1890 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
be6c64e4 1891 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
59076122
HV
1892 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
1893 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
d0547cba
HV
1894 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
1895 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
458aa4a6 1896 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)),
f16f77b0 1897 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
efbceecd
HV
1898 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
1899 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
1900 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, 0),
458aa4a6
HV
1901 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
1902 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
d806f2df
HV
1903 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
1904 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1905 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
1906 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
1907 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
f16f77b0
HV
1908 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
1909 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
1910 IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_IDENT, v4l_dbg_g_chip_ident, v4l_print_dbg_chip_ident, 0),
4b1e2e4d 1911 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
efc626b0
HV
1912 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
1913 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
1914 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
1915 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
1916 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
1917 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
458aa4a6
HV
1918 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
1919 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
1920 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
eb3728ed
HV
1921 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO),
1922 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, 0),
efc626b0
HV
1923 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
1924 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
1925 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, 0),
4839e6c2
HV
1926};
1927#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
1928
1929bool v4l2_is_known_ioctl(unsigned int cmd)
1930{
1931 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
1932 return false;
1933 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
1934}
1935
1936/* Common ioctl debug function. This function can be used by
1937 external ioctl messages as well as internal V4L ioctl */
4a085168 1938void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
4839e6c2 1939{
86748f35 1940 const char *dir, *type;
4839e6c2 1941
4a085168
HV
1942 if (prefix)
1943 printk(KERN_DEBUG "%s: ", prefix);
1944
4839e6c2
HV
1945 switch (_IOC_TYPE(cmd)) {
1946 case 'd':
1947 type = "v4l2_int";
1948 break;
1949 case 'V':
1950 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
1951 type = "v4l2";
1952 break;
1953 }
86748f35 1954 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
4839e6c2
HV
1955 return;
1956 default:
1957 type = "unknown";
86748f35 1958 break;
4839e6c2
HV
1959 }
1960
1961 switch (_IOC_DIR(cmd)) {
1962 case _IOC_NONE: dir = "--"; break;
1963 case _IOC_READ: dir = "r-"; break;
1964 case _IOC_WRITE: dir = "-w"; break;
1965 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
1966 default: dir = "*ERR*"; break;
1967 }
86748f35 1968 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
4839e6c2
HV
1969 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
1970}
1971EXPORT_SYMBOL(v4l_printk_ioctl);
1972
069b7479 1973static long __video_do_ioctl(struct file *file,
35ea11ff
HV
1974 unsigned int cmd, void *arg)
1975{
1976 struct video_device *vfd = video_devdata(file);
a399810c 1977 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
86748f35
HV
1978 bool write_only = false;
1979 struct v4l2_ioctl_info default_info;
1980 const struct v4l2_ioctl_info *info;
d5fbf32f 1981 void *fh = file->private_data;
99cd47bc 1982 struct v4l2_fh *vfh = NULL;
b1a873a3 1983 int use_fh_prio = 0;
80131fe0 1984 int debug = vfd->debug;
9190d191 1985 long ret = -ENOTTY;
35ea11ff 1986
6a717883 1987 if (ops == NULL) {
4a085168
HV
1988 pr_warn("%s: has no ioctl_ops.\n",
1989 video_device_node_name(vfd));
9190d191 1990 return ret;
6a717883
HV
1991 }
1992
b1a873a3 1993 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
99cd47bc 1994 vfh = file->private_data;
b1a873a3
HV
1995 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1996 }
99cd47bc 1997
48ea0be0 1998 if (v4l2_is_known_ioctl(cmd)) {
86748f35 1999 info = &v4l2_ioctls[_IOC_NR(cmd)];
48ea0be0
HV
2000
2001 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2002 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
86748f35 2003 goto done;
4b902fec
HV
2004
2005 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2006 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2007 if (ret)
86748f35 2008 goto done;
4b902fec 2009 }
86748f35
HV
2010 } else {
2011 default_info.ioctl = cmd;
2012 default_info.flags = 0;
f18d8e07 2013 default_info.debug = v4l_print_default;
86748f35 2014 info = &default_info;
48ea0be0
HV
2015 }
2016
86748f35 2017 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
80131fe0 2018 if (write_only && debug > V4L2_DEBUG_IOCTL) {
4a085168 2019 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
86748f35
HV
2020 pr_cont(": ");
2021 info->debug(arg, write_only);
2022 }
2023 if (info->flags & INFO_FL_STD) {
2024 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2025 const void *p = vfd->ioctl_ops;
2026 const vidioc_op *vidioc = p + info->offset;
2027
2028 ret = (*vidioc)(file, fh, arg);
86748f35
HV
2029 } else if (info->flags & INFO_FL_FUNC) {
2030 ret = info->func(ops, file, fh, arg);
f18d8e07
HV
2031 } else if (!ops->vidioc_default) {
2032 ret = -ENOTTY;
2033 } else {
2034 ret = ops->vidioc_default(file, fh,
2035 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2036 cmd, arg);
48ea0be0 2037 }
99cd47bc 2038
86748f35 2039done:
80131fe0
HV
2040 if (debug) {
2041 if (write_only && debug > V4L2_DEBUG_IOCTL) {
86748f35
HV
2042 if (ret < 0)
2043 printk(KERN_DEBUG "%s: error %ld\n",
2044 video_device_node_name(vfd), ret);
2045 return ret;
2046 }
4a085168 2047 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
86748f35
HV
2048 if (ret < 0)
2049 pr_cont(": error %ld\n", ret);
80131fe0 2050 else if (debug == V4L2_DEBUG_IOCTL)
86748f35 2051 pr_cont("\n");
86748f35
HV
2052 else if (_IOC_DIR(cmd) == _IOC_NONE)
2053 info->debug(arg, write_only);
2054 else {
2055 pr_cont(": ");
2056 info->debug(arg, write_only);
35ea11ff
HV
2057 }
2058 }
2059
2060 return ret;
2061}
2062
d14e6d76
PO
2063static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2064 void * __user *user_ptr, void ***kernel_ptr)
2065{
2066 int ret = 0;
2067
2068 switch (cmd) {
2069 case VIDIOC_QUERYBUF:
2070 case VIDIOC_QBUF:
2071 case VIDIOC_DQBUF: {
2072 struct v4l2_buffer *buf = parg;
2073
2074 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2075 if (buf->length > VIDEO_MAX_PLANES) {
2076 ret = -EINVAL;
2077 break;
2078 }
2079 *user_ptr = (void __user *)buf->m.planes;
2ef40370 2080 *kernel_ptr = (void *)&buf->m.planes;
d14e6d76
PO
2081 *array_size = sizeof(struct v4l2_plane) * buf->length;
2082 ret = 1;
2083 }
2084 break;
2085 }
2086
2087 case VIDIOC_S_EXT_CTRLS:
2088 case VIDIOC_G_EXT_CTRLS:
2089 case VIDIOC_TRY_EXT_CTRLS: {
2090 struct v4l2_ext_controls *ctrls = parg;
2091
2092 if (ctrls->count != 0) {
6c06108b
DC
2093 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2094 ret = -EINVAL;
2095 break;
2096 }
d14e6d76 2097 *user_ptr = (void __user *)ctrls->controls;
2ef40370 2098 *kernel_ptr = (void *)&ctrls->controls;
d14e6d76
PO
2099 *array_size = sizeof(struct v4l2_ext_control)
2100 * ctrls->count;
2101 ret = 1;
2102 }
2103 break;
2104 }
2105 }
2106
2107 return ret;
2108}
2109
fc0a8079
LP
2110long
2111video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2112 v4l2_kioctl func)
35ea11ff
HV
2113{
2114 char sbuf[128];
2115 void *mbuf = NULL;
1d94aa36 2116 void *parg = (void *)arg;
069b7479 2117 long err = -EINVAL;
d14e6d76
PO
2118 bool has_array_args;
2119 size_t array_size = 0;
35ea11ff 2120 void __user *user_ptr = NULL;
d14e6d76 2121 void **kernel_ptr = NULL;
35ea11ff 2122
35ea11ff 2123 /* Copy arguments into temp kernel buffer */
337f9d20 2124 if (_IOC_DIR(cmd) != _IOC_NONE) {
35ea11ff
HV
2125 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2126 parg = sbuf;
2127 } else {
2128 /* too big to allocate from stack */
2129 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2130 if (NULL == mbuf)
2131 return -ENOMEM;
2132 parg = mbuf;
2133 }
2134
2135 err = -EFAULT;
19c96e4b 2136 if (_IOC_DIR(cmd) & _IOC_WRITE) {
27cd2aba
HV
2137 unsigned int n = _IOC_SIZE(cmd);
2138
2139 /*
2140 * In some cases, only a few fields are used as input,
2141 * i.e. when the app sets "index" and then the driver
2142 * fills in the rest of the structure for the thing
2143 * with that index. We only need to copy up the first
2144 * non-input field.
2145 */
2146 if (v4l2_is_known_ioctl(cmd)) {
2147 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2148 if (flags & INFO_FL_CLEAR_MASK)
2149 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2150 }
19c96e4b
TP
2151
2152 if (copy_from_user(parg, (void __user *)arg, n))
35ea11ff 2153 goto out;
19c96e4b
TP
2154
2155 /* zero out anything we don't copy from userspace */
2156 if (n < _IOC_SIZE(cmd))
2157 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
337f9d20
TP
2158 } else {
2159 /* read-only ioctl */
2160 memset(parg, 0, _IOC_SIZE(cmd));
19c96e4b 2161 }
35ea11ff
HV
2162 }
2163
d14e6d76
PO
2164 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2165 if (err < 0)
2166 goto out;
2167 has_array_args = err;
35ea11ff 2168
d14e6d76
PO
2169 if (has_array_args) {
2170 /*
2171 * When adding new types of array args, make sure that the
2172 * parent argument to ioctl (which contains the pointer to the
2173 * array) fits into sbuf (so that mbuf will still remain
2174 * unused up to here).
2175 */
2176 mbuf = kmalloc(array_size, GFP_KERNEL);
2177 err = -ENOMEM;
2178 if (NULL == mbuf)
2179 goto out_array_args;
2180 err = -EFAULT;
2181 if (copy_from_user(mbuf, user_ptr, array_size))
2182 goto out_array_args;
2183 *kernel_ptr = mbuf;
35ea11ff
HV
2184 }
2185
2186 /* Handles IOCTL */
fc0a8079 2187 err = func(file, cmd, parg);
35ea11ff 2188 if (err == -ENOIOCTLCMD)
02bbb814 2189 err = -ENOTTY;
35ea11ff 2190
d14e6d76
PO
2191 if (has_array_args) {
2192 *kernel_ptr = user_ptr;
2193 if (copy_to_user(user_ptr, mbuf, array_size))
35ea11ff 2194 err = -EFAULT;
d14e6d76 2195 goto out_array_args;
35ea11ff 2196 }
5d7758ee
HV
2197 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2198 results that must be returned. */
2199 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
35ea11ff
HV
2200 goto out;
2201
d14e6d76 2202out_array_args:
35ea11ff
HV
2203 /* Copy results into user buffer */
2204 switch (_IOC_DIR(cmd)) {
2205 case _IOC_READ:
2206 case (_IOC_WRITE | _IOC_READ):
2207 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2208 err = -EFAULT;
2209 break;
2210 }
2211
2212out:
2213 kfree(mbuf);
2214 return err;
2215}
fc0a8079
LP
2216EXPORT_SYMBOL(video_usercopy);
2217
2218long video_ioctl2(struct file *file,
2219 unsigned int cmd, unsigned long arg)
2220{
2221 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2222}
8a522c91 2223EXPORT_SYMBOL(video_ioctl2);
This page took 0.575001 seconds and 5 git commands to generate.