Merge branches 'amba', 'fixes', 'misc', 'mmci', 'unstable/omap-dma' and 'unstable...
[deliverable/linux.git] / drivers / media / v4l2-core / v4l2-ioctl.c
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 *
11 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
12 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/version.h>
20
21 #include <linux/videodev2.h>
22
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-device.h>
29 #include <media/videobuf2-core.h>
30
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/v4l2.h>
33
34 /* Zero out the end of the struct pointed to by p. Everything after, but
35 * not including, the specified field is cleared. */
36 #define CLEAR_AFTER_FIELD(p, field) \
37 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
38 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
39
40 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
41
42 struct std_descr {
43 v4l2_std_id std;
44 const char *descr;
45 };
46
47 static const struct std_descr standards[] = {
48 { V4L2_STD_NTSC, "NTSC" },
49 { V4L2_STD_NTSC_M, "NTSC-M" },
50 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
51 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
52 { V4L2_STD_NTSC_443, "NTSC-443" },
53 { V4L2_STD_PAL, "PAL" },
54 { V4L2_STD_PAL_BG, "PAL-BG" },
55 { V4L2_STD_PAL_B, "PAL-B" },
56 { V4L2_STD_PAL_B1, "PAL-B1" },
57 { V4L2_STD_PAL_G, "PAL-G" },
58 { V4L2_STD_PAL_H, "PAL-H" },
59 { V4L2_STD_PAL_I, "PAL-I" },
60 { V4L2_STD_PAL_DK, "PAL-DK" },
61 { V4L2_STD_PAL_D, "PAL-D" },
62 { V4L2_STD_PAL_D1, "PAL-D1" },
63 { V4L2_STD_PAL_K, "PAL-K" },
64 { V4L2_STD_PAL_M, "PAL-M" },
65 { V4L2_STD_PAL_N, "PAL-N" },
66 { V4L2_STD_PAL_Nc, "PAL-Nc" },
67 { V4L2_STD_PAL_60, "PAL-60" },
68 { V4L2_STD_SECAM, "SECAM" },
69 { V4L2_STD_SECAM_B, "SECAM-B" },
70 { V4L2_STD_SECAM_G, "SECAM-G" },
71 { V4L2_STD_SECAM_H, "SECAM-H" },
72 { V4L2_STD_SECAM_DK, "SECAM-DK" },
73 { V4L2_STD_SECAM_D, "SECAM-D" },
74 { V4L2_STD_SECAM_K, "SECAM-K" },
75 { V4L2_STD_SECAM_K1, "SECAM-K1" },
76 { V4L2_STD_SECAM_L, "SECAM-L" },
77 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
78 { 0, "Unknown" }
79 };
80
81 /* video4linux standard ID conversion to standard name
82 */
83 const char *v4l2_norm_to_name(v4l2_std_id id)
84 {
85 u32 myid = id;
86 int i;
87
88 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
89 64 bit comparations. So, on that architecture, with some gcc
90 variants, compilation fails. Currently, the max value is 30bit wide.
91 */
92 BUG_ON(myid != id);
93
94 for (i = 0; standards[i].std; i++)
95 if (myid == standards[i].std)
96 break;
97 return standards[i].descr;
98 }
99 EXPORT_SYMBOL(v4l2_norm_to_name);
100
101 /* Returns frame period for the given standard */
102 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
103 {
104 if (id & V4L2_STD_525_60) {
105 frameperiod->numerator = 1001;
106 frameperiod->denominator = 30000;
107 } else {
108 frameperiod->numerator = 1;
109 frameperiod->denominator = 25;
110 }
111 }
112 EXPORT_SYMBOL(v4l2_video_std_frame_period);
113
114 /* Fill in the fields of a v4l2_standard structure according to the
115 'id' and 'transmission' parameters. Returns negative on error. */
116 int v4l2_video_std_construct(struct v4l2_standard *vs,
117 int id, const char *name)
118 {
119 vs->id = id;
120 v4l2_video_std_frame_period(id, &vs->frameperiod);
121 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
122 strlcpy(vs->name, name, sizeof(vs->name));
123 return 0;
124 }
125 EXPORT_SYMBOL(v4l2_video_std_construct);
126
127 /* ----------------------------------------------------------------- */
128 /* some arrays for pretty-printing debug messages of enum types */
129
130 const char *v4l2_field_names[] = {
131 [V4L2_FIELD_ANY] = "any",
132 [V4L2_FIELD_NONE] = "none",
133 [V4L2_FIELD_TOP] = "top",
134 [V4L2_FIELD_BOTTOM] = "bottom",
135 [V4L2_FIELD_INTERLACED] = "interlaced",
136 [V4L2_FIELD_SEQ_TB] = "seq-tb",
137 [V4L2_FIELD_SEQ_BT] = "seq-bt",
138 [V4L2_FIELD_ALTERNATE] = "alternate",
139 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
140 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
141 };
142 EXPORT_SYMBOL(v4l2_field_names);
143
144 const char *v4l2_type_names[] = {
145 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
146 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
147 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
148 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
149 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
150 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
151 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
152 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
153 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
154 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
155 };
156 EXPORT_SYMBOL(v4l2_type_names);
157
158 static const char *v4l2_memory_names[] = {
159 [V4L2_MEMORY_MMAP] = "mmap",
160 [V4L2_MEMORY_USERPTR] = "userptr",
161 [V4L2_MEMORY_OVERLAY] = "overlay",
162 [V4L2_MEMORY_DMABUF] = "dmabuf",
163 };
164
165 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
166
167 /* ------------------------------------------------------------------ */
168 /* debug help functions */
169
170 static void v4l_print_querycap(const void *arg, bool write_only)
171 {
172 const struct v4l2_capability *p = arg;
173
174 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
175 "capabilities=0x%08x, device_caps=0x%08x\n",
176 (int)sizeof(p->driver), p->driver,
177 (int)sizeof(p->card), p->card,
178 (int)sizeof(p->bus_info), p->bus_info,
179 p->version, p->capabilities, p->device_caps);
180 }
181
182 static void v4l_print_enuminput(const void *arg, bool write_only)
183 {
184 const struct v4l2_input *p = arg;
185
186 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
187 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
188 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
189 p->tuner, (unsigned long long)p->std, p->status,
190 p->capabilities);
191 }
192
193 static void v4l_print_enumoutput(const void *arg, bool write_only)
194 {
195 const struct v4l2_output *p = arg;
196
197 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
198 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
199 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
200 p->modulator, (unsigned long long)p->std, p->capabilities);
201 }
202
203 static void v4l_print_audio(const void *arg, bool write_only)
204 {
205 const struct v4l2_audio *p = arg;
206
207 if (write_only)
208 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
209 else
210 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
211 p->index, (int)sizeof(p->name), p->name,
212 p->capability, p->mode);
213 }
214
215 static void v4l_print_audioout(const void *arg, bool write_only)
216 {
217 const struct v4l2_audioout *p = arg;
218
219 if (write_only)
220 pr_cont("index=%u\n", p->index);
221 else
222 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
223 p->index, (int)sizeof(p->name), p->name,
224 p->capability, p->mode);
225 }
226
227 static void v4l_print_fmtdesc(const void *arg, bool write_only)
228 {
229 const struct v4l2_fmtdesc *p = arg;
230
231 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
232 p->index, prt_names(p->type, v4l2_type_names),
233 p->flags, (p->pixelformat & 0xff),
234 (p->pixelformat >> 8) & 0xff,
235 (p->pixelformat >> 16) & 0xff,
236 (p->pixelformat >> 24) & 0xff,
237 (int)sizeof(p->description), p->description);
238 }
239
240 static void v4l_print_format(const void *arg, bool write_only)
241 {
242 const struct v4l2_format *p = arg;
243 const struct v4l2_pix_format *pix;
244 const struct v4l2_pix_format_mplane *mp;
245 const struct v4l2_vbi_format *vbi;
246 const struct v4l2_sliced_vbi_format *sliced;
247 const struct v4l2_window *win;
248 unsigned i;
249
250 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
251 switch (p->type) {
252 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
253 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
254 pix = &p->fmt.pix;
255 pr_cont(", width=%u, height=%u, "
256 "pixelformat=%c%c%c%c, field=%s, "
257 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
258 pix->width, pix->height,
259 (pix->pixelformat & 0xff),
260 (pix->pixelformat >> 8) & 0xff,
261 (pix->pixelformat >> 16) & 0xff,
262 (pix->pixelformat >> 24) & 0xff,
263 prt_names(pix->field, v4l2_field_names),
264 pix->bytesperline, pix->sizeimage,
265 pix->colorspace);
266 break;
267 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
268 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
269 mp = &p->fmt.pix_mp;
270 pr_cont(", width=%u, height=%u, "
271 "format=%c%c%c%c, field=%s, "
272 "colorspace=%d, num_planes=%u\n",
273 mp->width, mp->height,
274 (mp->pixelformat & 0xff),
275 (mp->pixelformat >> 8) & 0xff,
276 (mp->pixelformat >> 16) & 0xff,
277 (mp->pixelformat >> 24) & 0xff,
278 prt_names(mp->field, v4l2_field_names),
279 mp->colorspace, mp->num_planes);
280 for (i = 0; i < mp->num_planes; i++)
281 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
282 mp->plane_fmt[i].bytesperline,
283 mp->plane_fmt[i].sizeimage);
284 break;
285 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
286 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
287 win = &p->fmt.win;
288 /* Note: we can't print the clip list here since the clips
289 * pointer is a userspace pointer, not a kernelspace
290 * pointer. */
291 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
292 win->w.width, win->w.height, win->w.left, win->w.top,
293 prt_names(win->field, v4l2_field_names),
294 win->chromakey, win->clipcount, win->clips,
295 win->bitmap, win->global_alpha);
296 break;
297 case V4L2_BUF_TYPE_VBI_CAPTURE:
298 case V4L2_BUF_TYPE_VBI_OUTPUT:
299 vbi = &p->fmt.vbi;
300 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
301 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
302 vbi->sampling_rate, vbi->offset,
303 vbi->samples_per_line,
304 (vbi->sample_format & 0xff),
305 (vbi->sample_format >> 8) & 0xff,
306 (vbi->sample_format >> 16) & 0xff,
307 (vbi->sample_format >> 24) & 0xff,
308 vbi->start[0], vbi->start[1],
309 vbi->count[0], vbi->count[1]);
310 break;
311 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
312 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
313 sliced = &p->fmt.sliced;
314 pr_cont(", service_set=0x%08x, io_size=%d\n",
315 sliced->service_set, sliced->io_size);
316 for (i = 0; i < 24; i++)
317 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
318 sliced->service_lines[0][i],
319 sliced->service_lines[1][i]);
320 break;
321 }
322 }
323
324 static 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
341 static 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
346 static 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\n", 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, (int)sizeof(p->name), p->name, p->capability,
356 p->rangelow, p->rangehigh, p->txsubchans);
357 }
358
359 static 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, (int)sizeof(p->name), p->name, p->type,
370 p->capability, p->rangelow,
371 p->rangehigh, p->signal, p->afc,
372 p->rxsubchans, p->audmode);
373 }
374
375 static 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
383 static 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, (int)sizeof(p->name), p->name,
390 p->frameperiod.numerator,
391 p->frameperiod.denominator,
392 p->framelines);
393 }
394
395 static 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
400 static 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, "
405 "rangelow=%u, rangehigh=%u\n",
406 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
407 p->rangelow, p->rangehigh);
408 }
409
410 static void v4l_print_requestbuffers(const void *arg, bool write_only)
411 {
412 const struct v4l2_requestbuffers *p = arg;
413
414 pr_cont("count=%d, type=%s, memory=%s\n",
415 p->count,
416 prt_names(p->type, v4l2_type_names),
417 prt_names(p->memory, v4l2_memory_names));
418 }
419
420 static void v4l_print_buffer(const void *arg, bool write_only)
421 {
422 const struct v4l2_buffer *p = arg;
423 const struct v4l2_timecode *tc = &p->timecode;
424 const struct v4l2_plane *plane;
425 int i;
426
427 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
428 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
429 p->timestamp.tv_sec / 3600,
430 (int)(p->timestamp.tv_sec / 60) % 60,
431 (int)(p->timestamp.tv_sec % 60),
432 (long)p->timestamp.tv_usec,
433 p->index,
434 prt_names(p->type, v4l2_type_names),
435 p->flags, prt_names(p->field, v4l2_field_names),
436 p->sequence, prt_names(p->memory, v4l2_memory_names));
437
438 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
439 pr_cont("\n");
440 for (i = 0; i < p->length; ++i) {
441 plane = &p->m.planes[i];
442 printk(KERN_DEBUG
443 "plane %d: bytesused=%d, data_offset=0x%08x, "
444 "offset/userptr=0x%lx, length=%d\n",
445 i, plane->bytesused, plane->data_offset,
446 plane->m.userptr, plane->length);
447 }
448 } else {
449 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
450 p->bytesused, p->m.userptr, p->length);
451 }
452
453 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
454 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
455 tc->hours, tc->minutes, tc->seconds,
456 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
457 }
458
459 static void v4l_print_exportbuffer(const void *arg, bool write_only)
460 {
461 const struct v4l2_exportbuffer *p = arg;
462
463 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
464 p->fd, prt_names(p->type, v4l2_type_names),
465 p->index, p->plane, p->flags);
466 }
467
468 static void v4l_print_create_buffers(const void *arg, bool write_only)
469 {
470 const struct v4l2_create_buffers *p = arg;
471
472 pr_cont("index=%d, count=%d, memory=%s, ",
473 p->index, p->count,
474 prt_names(p->memory, v4l2_memory_names));
475 v4l_print_format(&p->format, write_only);
476 }
477
478 static void v4l_print_streamparm(const void *arg, bool write_only)
479 {
480 const struct v4l2_streamparm *p = arg;
481
482 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
483
484 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
485 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
486 const struct v4l2_captureparm *c = &p->parm.capture;
487
488 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
489 "extendedmode=%d, readbuffers=%d\n",
490 c->capability, c->capturemode,
491 c->timeperframe.numerator, c->timeperframe.denominator,
492 c->extendedmode, c->readbuffers);
493 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
494 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
495 const struct v4l2_outputparm *c = &p->parm.output;
496
497 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
498 "extendedmode=%d, writebuffers=%d\n",
499 c->capability, c->outputmode,
500 c->timeperframe.numerator, c->timeperframe.denominator,
501 c->extendedmode, c->writebuffers);
502 } else {
503 pr_cont("\n");
504 }
505 }
506
507 static void v4l_print_queryctrl(const void *arg, bool write_only)
508 {
509 const struct v4l2_queryctrl *p = arg;
510
511 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
512 "step=%d, default=%d, flags=0x%08x\n",
513 p->id, p->type, (int)sizeof(p->name), p->name,
514 p->minimum, p->maximum,
515 p->step, p->default_value, p->flags);
516 }
517
518 static void v4l_print_querymenu(const void *arg, bool write_only)
519 {
520 const struct v4l2_querymenu *p = arg;
521
522 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
523 }
524
525 static void v4l_print_control(const void *arg, bool write_only)
526 {
527 const struct v4l2_control *p = arg;
528
529 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
530 }
531
532 static void v4l_print_ext_controls(const void *arg, bool write_only)
533 {
534 const struct v4l2_ext_controls *p = arg;
535 int i;
536
537 pr_cont("class=0x%x, count=%d, error_idx=%d",
538 p->ctrl_class, p->count, p->error_idx);
539 for (i = 0; i < p->count; i++) {
540 if (p->controls[i].size)
541 pr_cont(", id/val=0x%x/0x%x",
542 p->controls[i].id, p->controls[i].value);
543 else
544 pr_cont(", id/size=0x%x/%u",
545 p->controls[i].id, p->controls[i].size);
546 }
547 pr_cont("\n");
548 }
549
550 static void v4l_print_cropcap(const void *arg, bool write_only)
551 {
552 const struct v4l2_cropcap *p = arg;
553
554 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
555 "defrect wxh=%dx%d, x,y=%d,%d\n, "
556 "pixelaspect %d/%d\n",
557 prt_names(p->type, v4l2_type_names),
558 p->bounds.width, p->bounds.height,
559 p->bounds.left, p->bounds.top,
560 p->defrect.width, p->defrect.height,
561 p->defrect.left, p->defrect.top,
562 p->pixelaspect.numerator, p->pixelaspect.denominator);
563 }
564
565 static void v4l_print_crop(const void *arg, bool write_only)
566 {
567 const struct v4l2_crop *p = arg;
568
569 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
570 prt_names(p->type, v4l2_type_names),
571 p->c.width, p->c.height,
572 p->c.left, p->c.top);
573 }
574
575 static void v4l_print_selection(const void *arg, bool write_only)
576 {
577 const struct v4l2_selection *p = arg;
578
579 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
580 prt_names(p->type, v4l2_type_names),
581 p->target, p->flags,
582 p->r.width, p->r.height, p->r.left, p->r.top);
583 }
584
585 static void v4l_print_jpegcompression(const void *arg, bool write_only)
586 {
587 const struct v4l2_jpegcompression *p = arg;
588
589 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
590 "COM_len=%d, jpeg_markers=0x%x\n",
591 p->quality, p->APPn, p->APP_len,
592 p->COM_len, p->jpeg_markers);
593 }
594
595 static void v4l_print_enc_idx(const void *arg, bool write_only)
596 {
597 const struct v4l2_enc_idx *p = arg;
598
599 pr_cont("entries=%d, entries_cap=%d\n",
600 p->entries, p->entries_cap);
601 }
602
603 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
604 {
605 const struct v4l2_encoder_cmd *p = arg;
606
607 pr_cont("cmd=%d, flags=0x%x\n",
608 p->cmd, p->flags);
609 }
610
611 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
612 {
613 const struct v4l2_decoder_cmd *p = arg;
614
615 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
616
617 if (p->cmd == V4L2_DEC_CMD_START)
618 pr_info("speed=%d, format=%u\n",
619 p->start.speed, p->start.format);
620 else if (p->cmd == V4L2_DEC_CMD_STOP)
621 pr_info("pts=%llu\n", p->stop.pts);
622 }
623
624 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
625 {
626 const struct v4l2_dbg_chip_info *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, ",
631 (int)sizeof(p->match.name), p->match.name);
632 else
633 pr_cont("addr=%u, ", p->match.addr);
634 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
635 }
636
637 static void v4l_print_dbg_register(const void *arg, bool write_only)
638 {
639 const struct v4l2_dbg_register *p = arg;
640
641 pr_cont("type=%u, ", p->match.type);
642 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
643 pr_cont("name=%.*s, ",
644 (int)sizeof(p->match.name), p->match.name);
645 else
646 pr_cont("addr=%u, ", p->match.addr);
647 pr_cont("reg=0x%llx, val=0x%llx\n",
648 p->reg, p->val);
649 }
650
651 static void v4l_print_dv_timings(const void *arg, bool write_only)
652 {
653 const struct v4l2_dv_timings *p = arg;
654
655 switch (p->type) {
656 case V4L2_DV_BT_656_1120:
657 pr_cont("type=bt-656/1120, interlaced=%u, "
658 "pixelclock=%llu, "
659 "width=%u, height=%u, polarities=0x%x, "
660 "hfrontporch=%u, hsync=%u, "
661 "hbackporch=%u, vfrontporch=%u, "
662 "vsync=%u, vbackporch=%u, "
663 "il_vfrontporch=%u, il_vsync=%u, "
664 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
665 p->bt.interlaced, p->bt.pixelclock,
666 p->bt.width, p->bt.height,
667 p->bt.polarities, p->bt.hfrontporch,
668 p->bt.hsync, p->bt.hbackporch,
669 p->bt.vfrontporch, p->bt.vsync,
670 p->bt.vbackporch, p->bt.il_vfrontporch,
671 p->bt.il_vsync, p->bt.il_vbackporch,
672 p->bt.standards, p->bt.flags);
673 break;
674 default:
675 pr_cont("type=%d\n", p->type);
676 break;
677 }
678 }
679
680 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
681 {
682 const struct v4l2_enum_dv_timings *p = arg;
683
684 pr_cont("index=%u, ", p->index);
685 v4l_print_dv_timings(&p->timings, write_only);
686 }
687
688 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
689 {
690 const struct v4l2_dv_timings_cap *p = arg;
691
692 switch (p->type) {
693 case V4L2_DV_BT_656_1120:
694 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
695 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
696 p->bt.min_width, p->bt.max_width,
697 p->bt.min_height, p->bt.max_height,
698 p->bt.min_pixelclock, p->bt.max_pixelclock,
699 p->bt.standards, p->bt.capabilities);
700 break;
701 default:
702 pr_cont("type=%u\n", p->type);
703 break;
704 }
705 }
706
707 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
708 {
709 const struct v4l2_frmsizeenum *p = arg;
710
711 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
712 p->index,
713 (p->pixel_format & 0xff),
714 (p->pixel_format >> 8) & 0xff,
715 (p->pixel_format >> 16) & 0xff,
716 (p->pixel_format >> 24) & 0xff,
717 p->type);
718 switch (p->type) {
719 case V4L2_FRMSIZE_TYPE_DISCRETE:
720 pr_cont(", wxh=%ux%u\n",
721 p->discrete.width, p->discrete.height);
722 break;
723 case V4L2_FRMSIZE_TYPE_STEPWISE:
724 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
725 p->stepwise.min_width, p->stepwise.min_height,
726 p->stepwise.step_width, p->stepwise.step_height,
727 p->stepwise.max_width, p->stepwise.max_height);
728 break;
729 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
730 /* fall through */
731 default:
732 pr_cont("\n");
733 break;
734 }
735 }
736
737 static void v4l_print_frmivalenum(const void *arg, bool write_only)
738 {
739 const struct v4l2_frmivalenum *p = arg;
740
741 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
742 p->index,
743 (p->pixel_format & 0xff),
744 (p->pixel_format >> 8) & 0xff,
745 (p->pixel_format >> 16) & 0xff,
746 (p->pixel_format >> 24) & 0xff,
747 p->width, p->height, p->type);
748 switch (p->type) {
749 case V4L2_FRMIVAL_TYPE_DISCRETE:
750 pr_cont(", fps=%d/%d\n",
751 p->discrete.numerator,
752 p->discrete.denominator);
753 break;
754 case V4L2_FRMIVAL_TYPE_STEPWISE:
755 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
756 p->stepwise.min.numerator,
757 p->stepwise.min.denominator,
758 p->stepwise.max.numerator,
759 p->stepwise.max.denominator,
760 p->stepwise.step.numerator,
761 p->stepwise.step.denominator);
762 break;
763 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
764 /* fall through */
765 default:
766 pr_cont("\n");
767 break;
768 }
769 }
770
771 static void v4l_print_event(const void *arg, bool write_only)
772 {
773 const struct v4l2_event *p = arg;
774 const struct v4l2_event_ctrl *c;
775
776 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
777 "timestamp=%lu.%9.9lu\n",
778 p->type, p->pending, p->sequence, p->id,
779 p->timestamp.tv_sec, p->timestamp.tv_nsec);
780 switch (p->type) {
781 case V4L2_EVENT_VSYNC:
782 printk(KERN_DEBUG "field=%s\n",
783 prt_names(p->u.vsync.field, v4l2_field_names));
784 break;
785 case V4L2_EVENT_CTRL:
786 c = &p->u.ctrl;
787 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
788 c->changes, c->type);
789 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
790 pr_cont("value64=%lld, ", c->value64);
791 else
792 pr_cont("value=%d, ", c->value);
793 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
794 "default_value=%d\n",
795 c->flags, c->minimum, c->maximum,
796 c->step, c->default_value);
797 break;
798 case V4L2_EVENT_FRAME_SYNC:
799 pr_cont("frame_sequence=%u\n",
800 p->u.frame_sync.frame_sequence);
801 break;
802 }
803 }
804
805 static void v4l_print_event_subscription(const void *arg, bool write_only)
806 {
807 const struct v4l2_event_subscription *p = arg;
808
809 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
810 p->type, p->id, p->flags);
811 }
812
813 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
814 {
815 const struct v4l2_sliced_vbi_cap *p = arg;
816 int i;
817
818 pr_cont("type=%s, service_set=0x%08x\n",
819 prt_names(p->type, v4l2_type_names), p->service_set);
820 for (i = 0; i < 24; i++)
821 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
822 p->service_lines[0][i],
823 p->service_lines[1][i]);
824 }
825
826 static void v4l_print_freq_band(const void *arg, bool write_only)
827 {
828 const struct v4l2_frequency_band *p = arg;
829
830 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
831 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
832 p->tuner, p->type, p->index,
833 p->capability, p->rangelow,
834 p->rangehigh, p->modulation);
835 }
836
837 static void v4l_print_u32(const void *arg, bool write_only)
838 {
839 pr_cont("value=%u\n", *(const u32 *)arg);
840 }
841
842 static void v4l_print_newline(const void *arg, bool write_only)
843 {
844 pr_cont("\n");
845 }
846
847 static void v4l_print_default(const void *arg, bool write_only)
848 {
849 pr_cont("driver-specific ioctl\n");
850 }
851
852 static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
853 {
854 __u32 i;
855
856 /* zero the reserved fields */
857 c->reserved[0] = c->reserved[1] = 0;
858 for (i = 0; i < c->count; i++)
859 c->controls[i].reserved2[0] = 0;
860
861 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
862 when using extended controls.
863 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
864 is it allowed for backwards compatibility.
865 */
866 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
867 return 0;
868 /* Check that all controls are from the same control class. */
869 for (i = 0; i < c->count; i++) {
870 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
871 c->error_idx = i;
872 return 0;
873 }
874 }
875 return 1;
876 }
877
878 static int check_fmt(struct file *file, enum v4l2_buf_type type)
879 {
880 struct video_device *vfd = video_devdata(file);
881 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
882 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
883 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
884 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
885 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
886
887 if (ops == NULL)
888 return -EINVAL;
889
890 switch (type) {
891 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
892 if (is_vid && is_rx &&
893 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
894 return 0;
895 break;
896 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
897 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
898 return 0;
899 break;
900 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
901 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
902 return 0;
903 break;
904 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
905 if (is_vid && is_tx &&
906 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
907 return 0;
908 break;
909 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
910 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
911 return 0;
912 break;
913 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
914 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
915 return 0;
916 break;
917 case V4L2_BUF_TYPE_VBI_CAPTURE:
918 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
919 return 0;
920 break;
921 case V4L2_BUF_TYPE_VBI_OUTPUT:
922 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
923 return 0;
924 break;
925 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
926 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
927 return 0;
928 break;
929 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
930 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
931 return 0;
932 break;
933 default:
934 break;
935 }
936 return -EINVAL;
937 }
938
939 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
940 struct file *file, void *fh, void *arg)
941 {
942 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
943
944 cap->version = LINUX_VERSION_CODE;
945 return ops->vidioc_querycap(file, fh, cap);
946 }
947
948 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
949 struct file *file, void *fh, void *arg)
950 {
951 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
952 }
953
954 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
955 struct file *file, void *fh, void *arg)
956 {
957 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
958 }
959
960 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
961 struct file *file, void *fh, void *arg)
962 {
963 struct video_device *vfd;
964 u32 *p = arg;
965
966 if (ops->vidioc_g_priority)
967 return ops->vidioc_g_priority(file, fh, arg);
968 vfd = video_devdata(file);
969 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
970 return 0;
971 }
972
973 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
974 struct file *file, void *fh, void *arg)
975 {
976 struct video_device *vfd;
977 struct v4l2_fh *vfh;
978 u32 *p = arg;
979
980 if (ops->vidioc_s_priority)
981 return ops->vidioc_s_priority(file, fh, *p);
982 vfd = video_devdata(file);
983 vfh = file->private_data;
984 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
985 }
986
987 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
988 struct file *file, void *fh, void *arg)
989 {
990 struct video_device *vfd = video_devdata(file);
991 struct v4l2_input *p = arg;
992
993 /*
994 * We set the flags for CAP_DV_TIMINGS &
995 * CAP_STD here based on ioctl handler provided by the
996 * driver. If the driver doesn't support these
997 * for a specific input, it must override these flags.
998 */
999 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1000 p->capabilities |= V4L2_IN_CAP_STD;
1001
1002 return ops->vidioc_enum_input(file, fh, p);
1003 }
1004
1005 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1006 struct file *file, void *fh, void *arg)
1007 {
1008 struct video_device *vfd = video_devdata(file);
1009 struct v4l2_output *p = arg;
1010
1011 /*
1012 * We set the flags for CAP_DV_TIMINGS &
1013 * CAP_STD here based on ioctl handler provided by the
1014 * driver. If the driver doesn't support these
1015 * for a specific output, it must override these flags.
1016 */
1017 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1018 p->capabilities |= V4L2_OUT_CAP_STD;
1019
1020 return ops->vidioc_enum_output(file, fh, p);
1021 }
1022
1023 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1024 struct file *file, void *fh, void *arg)
1025 {
1026 struct v4l2_fmtdesc *p = arg;
1027 struct video_device *vfd = video_devdata(file);
1028 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1029 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1030
1031 switch (p->type) {
1032 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1033 if (unlikely(!is_rx || !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(!is_rx || !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(!is_rx || !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(!is_tx || !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(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
1050 break;
1051 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1052 }
1053 return -EINVAL;
1054 }
1055
1056 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1057 struct file *file, void *fh, void *arg)
1058 {
1059 struct v4l2_format *p = arg;
1060 struct video_device *vfd = video_devdata(file);
1061 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1062 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1063 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1064
1065 switch (p->type) {
1066 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1067 if (unlikely(!is_rx || !is_vid || !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(!is_rx || !is_vid || !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(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
1076 break;
1077 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1078 case V4L2_BUF_TYPE_VBI_CAPTURE:
1079 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1080 break;
1081 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1082 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1083 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1084 break;
1085 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1086 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1087 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
1088 break;
1089 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1090 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1091 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
1092 break;
1093 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1094 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1095 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
1096 break;
1097 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1098 case V4L2_BUF_TYPE_VBI_OUTPUT:
1099 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
1100 break;
1101 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1102 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1103 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
1104 break;
1105 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1106 }
1107 return -EINVAL;
1108 }
1109
1110 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1111 struct file *file, void *fh, void *arg)
1112 {
1113 struct v4l2_format *p = arg;
1114 struct video_device *vfd = video_devdata(file);
1115 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1116 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1117 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1118
1119 switch (p->type) {
1120 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1121 if (unlikely(!is_rx || !is_vid || !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(!is_rx || !is_vid || !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(!is_rx || !is_vid || !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_VBI_CAPTURE:
1136 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1137 break;
1138 CLEAR_AFTER_FIELD(p, fmt.vbi);
1139 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1140 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1141 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1142 break;
1143 CLEAR_AFTER_FIELD(p, fmt.sliced);
1144 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1145 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1146 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
1147 break;
1148 CLEAR_AFTER_FIELD(p, fmt.pix);
1149 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1150 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1151 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
1152 break;
1153 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1154 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1155 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1156 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
1157 break;
1158 CLEAR_AFTER_FIELD(p, fmt.win);
1159 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1160 case V4L2_BUF_TYPE_VBI_OUTPUT:
1161 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
1162 break;
1163 CLEAR_AFTER_FIELD(p, fmt.vbi);
1164 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1165 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1166 if (unlikely(!is_tx || is_vid || !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 }
1171 return -EINVAL;
1172 }
1173
1174 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1175 struct file *file, void *fh, void *arg)
1176 {
1177 struct v4l2_format *p = arg;
1178 struct video_device *vfd = video_devdata(file);
1179 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1180 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1181 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1182
1183 switch (p->type) {
1184 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1185 if (unlikely(!is_rx || !is_vid || !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(!is_rx || !is_vid || !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(!is_rx || !is_vid || !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_VBI_CAPTURE:
1200 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1201 break;
1202 CLEAR_AFTER_FIELD(p, fmt.vbi);
1203 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1204 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1205 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1206 break;
1207 CLEAR_AFTER_FIELD(p, fmt.sliced);
1208 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1209 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1210 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
1211 break;
1212 CLEAR_AFTER_FIELD(p, fmt.pix);
1213 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1214 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1215 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
1216 break;
1217 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1218 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1219 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1220 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
1221 break;
1222 CLEAR_AFTER_FIELD(p, fmt.win);
1223 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1224 case V4L2_BUF_TYPE_VBI_OUTPUT:
1225 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
1226 break;
1227 CLEAR_AFTER_FIELD(p, fmt.vbi);
1228 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1229 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1230 if (unlikely(!is_tx || is_vid || !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 }
1235 return -EINVAL;
1236 }
1237
1238 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1239 struct file *file, void *fh, void *arg)
1240 {
1241 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1242 }
1243
1244 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1245 struct file *file, void *fh, void *arg)
1246 {
1247 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1248 }
1249
1250 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1251 struct file *file, void *fh, void *arg)
1252 {
1253 struct video_device *vfd = video_devdata(file);
1254 struct v4l2_tuner *p = arg;
1255 int err;
1256
1257 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1258 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1259 err = ops->vidioc_g_tuner(file, fh, p);
1260 if (!err)
1261 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1262 return err;
1263 }
1264
1265 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1266 struct file *file, void *fh, void *arg)
1267 {
1268 struct video_device *vfd = video_devdata(file);
1269 struct v4l2_tuner *p = arg;
1270
1271 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1272 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1273 return ops->vidioc_s_tuner(file, fh, p);
1274 }
1275
1276 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1277 struct file *file, void *fh, void *arg)
1278 {
1279 struct v4l2_modulator *p = arg;
1280 int err;
1281
1282 err = ops->vidioc_g_modulator(file, fh, p);
1283 if (!err)
1284 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1285 return err;
1286 }
1287
1288 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1289 struct file *file, void *fh, void *arg)
1290 {
1291 struct video_device *vfd = video_devdata(file);
1292 struct v4l2_frequency *p = arg;
1293
1294 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1295 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1296 return ops->vidioc_g_frequency(file, fh, p);
1297 }
1298
1299 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1300 struct file *file, void *fh, void *arg)
1301 {
1302 struct video_device *vfd = video_devdata(file);
1303 const struct v4l2_frequency *p = arg;
1304 enum v4l2_tuner_type type;
1305
1306 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1307 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1308 if (p->type != type)
1309 return -EINVAL;
1310 return ops->vidioc_s_frequency(file, fh, p);
1311 }
1312
1313 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1314 struct file *file, void *fh, void *arg)
1315 {
1316 struct video_device *vfd = video_devdata(file);
1317 struct v4l2_standard *p = arg;
1318 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1319 unsigned int index = p->index, i, j = 0;
1320 const char *descr = "";
1321
1322 /* Return -ENODATA if the tvnorms for the current input
1323 or output is 0, meaning that it doesn't support this API. */
1324 if (id == 0)
1325 return -ENODATA;
1326
1327 /* Return norm array in a canonical way */
1328 for (i = 0; i <= index && id; i++) {
1329 /* last std value in the standards array is 0, so this
1330 while always ends there since (id & 0) == 0. */
1331 while ((id & standards[j].std) != standards[j].std)
1332 j++;
1333 curr_id = standards[j].std;
1334 descr = standards[j].descr;
1335 j++;
1336 if (curr_id == 0)
1337 break;
1338 if (curr_id != V4L2_STD_PAL &&
1339 curr_id != V4L2_STD_SECAM &&
1340 curr_id != V4L2_STD_NTSC)
1341 id &= ~curr_id;
1342 }
1343 if (i <= index)
1344 return -EINVAL;
1345
1346 v4l2_video_std_construct(p, curr_id, descr);
1347 return 0;
1348 }
1349
1350 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1351 struct file *file, void *fh, void *arg)
1352 {
1353 struct video_device *vfd = video_devdata(file);
1354 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1355
1356 norm = id & vfd->tvnorms;
1357 if (vfd->tvnorms && !norm) /* Check if std is supported */
1358 return -EINVAL;
1359
1360 /* Calls the specific handler */
1361 return ops->vidioc_s_std(file, fh, norm);
1362 }
1363
1364 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1365 struct file *file, void *fh, void *arg)
1366 {
1367 struct video_device *vfd = video_devdata(file);
1368 v4l2_std_id *p = arg;
1369
1370 /*
1371 * If no signal is detected, then the driver should return
1372 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1373 * any standards that do not apply removed.
1374 *
1375 * This means that tuners, audio and video decoders can join
1376 * their efforts to improve the standards detection.
1377 */
1378 *p = vfd->tvnorms;
1379 return ops->vidioc_querystd(file, fh, arg);
1380 }
1381
1382 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1383 struct file *file, void *fh, void *arg)
1384 {
1385 struct video_device *vfd = video_devdata(file);
1386 struct v4l2_hw_freq_seek *p = arg;
1387 enum v4l2_tuner_type type;
1388
1389 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1390 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1391 if (p->type != type)
1392 return -EINVAL;
1393 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1394 }
1395
1396 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1397 struct file *file, void *fh, void *arg)
1398 {
1399 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1400 }
1401
1402 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1403 struct file *file, void *fh, void *arg)
1404 {
1405 struct v4l2_requestbuffers *p = arg;
1406 int ret = check_fmt(file, p->type);
1407
1408 if (ret)
1409 return ret;
1410
1411 CLEAR_AFTER_FIELD(p, memory);
1412
1413 return ops->vidioc_reqbufs(file, fh, p);
1414 }
1415
1416 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1417 struct file *file, void *fh, void *arg)
1418 {
1419 struct v4l2_buffer *p = arg;
1420 int ret = check_fmt(file, p->type);
1421
1422 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1423 }
1424
1425 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1426 struct file *file, void *fh, void *arg)
1427 {
1428 struct v4l2_buffer *p = arg;
1429 int ret = check_fmt(file, p->type);
1430
1431 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1432 }
1433
1434 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1435 struct file *file, void *fh, void *arg)
1436 {
1437 struct v4l2_buffer *p = arg;
1438 int ret = check_fmt(file, p->type);
1439
1440 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1441 }
1442
1443 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1444 struct file *file, void *fh, void *arg)
1445 {
1446 struct v4l2_create_buffers *create = arg;
1447 int ret = check_fmt(file, create->format.type);
1448
1449 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1450 }
1451
1452 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1453 struct file *file, void *fh, void *arg)
1454 {
1455 struct v4l2_buffer *b = arg;
1456 int ret = check_fmt(file, b->type);
1457
1458 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1459 }
1460
1461 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1462 struct file *file, void *fh, void *arg)
1463 {
1464 struct v4l2_streamparm *p = arg;
1465 v4l2_std_id std;
1466 int ret = check_fmt(file, p->type);
1467
1468 if (ret)
1469 return ret;
1470 if (ops->vidioc_g_parm)
1471 return ops->vidioc_g_parm(file, fh, p);
1472 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1473 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1474 return -EINVAL;
1475 p->parm.capture.readbuffers = 2;
1476 ret = ops->vidioc_g_std(file, fh, &std);
1477 if (ret == 0)
1478 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
1479 return ret;
1480 }
1481
1482 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1483 struct file *file, void *fh, void *arg)
1484 {
1485 struct v4l2_streamparm *p = arg;
1486 int ret = check_fmt(file, p->type);
1487
1488 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1489 }
1490
1491 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1492 struct file *file, void *fh, void *arg)
1493 {
1494 struct video_device *vfd = video_devdata(file);
1495 struct v4l2_queryctrl *p = arg;
1496 struct v4l2_fh *vfh =
1497 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1498
1499 if (vfh && vfh->ctrl_handler)
1500 return v4l2_queryctrl(vfh->ctrl_handler, p);
1501 if (vfd->ctrl_handler)
1502 return v4l2_queryctrl(vfd->ctrl_handler, p);
1503 if (ops->vidioc_queryctrl)
1504 return ops->vidioc_queryctrl(file, fh, p);
1505 return -ENOTTY;
1506 }
1507
1508 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1509 struct file *file, void *fh, void *arg)
1510 {
1511 struct video_device *vfd = video_devdata(file);
1512 struct v4l2_querymenu *p = arg;
1513 struct v4l2_fh *vfh =
1514 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1515
1516 if (vfh && vfh->ctrl_handler)
1517 return v4l2_querymenu(vfh->ctrl_handler, p);
1518 if (vfd->ctrl_handler)
1519 return v4l2_querymenu(vfd->ctrl_handler, p);
1520 if (ops->vidioc_querymenu)
1521 return ops->vidioc_querymenu(file, fh, p);
1522 return -ENOTTY;
1523 }
1524
1525 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1526 struct file *file, void *fh, void *arg)
1527 {
1528 struct video_device *vfd = video_devdata(file);
1529 struct v4l2_control *p = arg;
1530 struct v4l2_fh *vfh =
1531 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1532 struct v4l2_ext_controls ctrls;
1533 struct v4l2_ext_control ctrl;
1534
1535 if (vfh && vfh->ctrl_handler)
1536 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1537 if (vfd->ctrl_handler)
1538 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1539 if (ops->vidioc_g_ctrl)
1540 return ops->vidioc_g_ctrl(file, fh, p);
1541 if (ops->vidioc_g_ext_ctrls == NULL)
1542 return -ENOTTY;
1543
1544 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1545 ctrls.count = 1;
1546 ctrls.controls = &ctrl;
1547 ctrl.id = p->id;
1548 ctrl.value = p->value;
1549 if (check_ext_ctrls(&ctrls, 1)) {
1550 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1551
1552 if (ret == 0)
1553 p->value = ctrl.value;
1554 return ret;
1555 }
1556 return -EINVAL;
1557 }
1558
1559 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1560 struct file *file, void *fh, void *arg)
1561 {
1562 struct video_device *vfd = video_devdata(file);
1563 struct v4l2_control *p = arg;
1564 struct v4l2_fh *vfh =
1565 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1566 struct v4l2_ext_controls ctrls;
1567 struct v4l2_ext_control ctrl;
1568
1569 if (vfh && vfh->ctrl_handler)
1570 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1571 if (vfd->ctrl_handler)
1572 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1573 if (ops->vidioc_s_ctrl)
1574 return ops->vidioc_s_ctrl(file, fh, p);
1575 if (ops->vidioc_s_ext_ctrls == NULL)
1576 return -ENOTTY;
1577
1578 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1579 ctrls.count = 1;
1580 ctrls.controls = &ctrl;
1581 ctrl.id = p->id;
1582 ctrl.value = p->value;
1583 if (check_ext_ctrls(&ctrls, 1))
1584 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1585 return -EINVAL;
1586 }
1587
1588 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1589 struct file *file, void *fh, void *arg)
1590 {
1591 struct video_device *vfd = video_devdata(file);
1592 struct v4l2_ext_controls *p = arg;
1593 struct v4l2_fh *vfh =
1594 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1595
1596 p->error_idx = p->count;
1597 if (vfh && vfh->ctrl_handler)
1598 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1599 if (vfd->ctrl_handler)
1600 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1601 if (ops->vidioc_g_ext_ctrls == NULL)
1602 return -ENOTTY;
1603 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1604 -EINVAL;
1605 }
1606
1607 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1608 struct file *file, void *fh, void *arg)
1609 {
1610 struct video_device *vfd = video_devdata(file);
1611 struct v4l2_ext_controls *p = arg;
1612 struct v4l2_fh *vfh =
1613 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1614
1615 p->error_idx = p->count;
1616 if (vfh && vfh->ctrl_handler)
1617 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1618 if (vfd->ctrl_handler)
1619 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1620 if (ops->vidioc_s_ext_ctrls == NULL)
1621 return -ENOTTY;
1622 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1623 -EINVAL;
1624 }
1625
1626 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1627 struct file *file, void *fh, void *arg)
1628 {
1629 struct video_device *vfd = video_devdata(file);
1630 struct v4l2_ext_controls *p = arg;
1631 struct v4l2_fh *vfh =
1632 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1633
1634 p->error_idx = p->count;
1635 if (vfh && vfh->ctrl_handler)
1636 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1637 if (vfd->ctrl_handler)
1638 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1639 if (ops->vidioc_try_ext_ctrls == NULL)
1640 return -ENOTTY;
1641 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1642 -EINVAL;
1643 }
1644
1645 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1646 struct file *file, void *fh, void *arg)
1647 {
1648 struct v4l2_crop *p = arg;
1649 struct v4l2_selection s = {
1650 .type = p->type,
1651 };
1652 int ret;
1653
1654 if (ops->vidioc_g_crop)
1655 return ops->vidioc_g_crop(file, fh, p);
1656 /* simulate capture crop using selection api */
1657
1658 /* crop means compose for output devices */
1659 if (V4L2_TYPE_IS_OUTPUT(p->type))
1660 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1661 else
1662 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1663
1664 ret = ops->vidioc_g_selection(file, fh, &s);
1665
1666 /* copying results to old structure on success */
1667 if (!ret)
1668 p->c = s.r;
1669 return ret;
1670 }
1671
1672 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1673 struct file *file, void *fh, void *arg)
1674 {
1675 struct v4l2_crop *p = arg;
1676 struct v4l2_selection s = {
1677 .type = p->type,
1678 .r = p->c,
1679 };
1680
1681 if (ops->vidioc_s_crop)
1682 return ops->vidioc_s_crop(file, fh, p);
1683 /* simulate capture crop using selection api */
1684
1685 /* crop means compose for output devices */
1686 if (V4L2_TYPE_IS_OUTPUT(p->type))
1687 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1688 else
1689 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1690
1691 return ops->vidioc_s_selection(file, fh, &s);
1692 }
1693
1694 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1695 struct file *file, void *fh, void *arg)
1696 {
1697 struct v4l2_cropcap *p = arg;
1698 struct v4l2_selection s = { .type = p->type };
1699 int ret;
1700
1701 if (ops->vidioc_cropcap)
1702 return ops->vidioc_cropcap(file, fh, p);
1703
1704 /* obtaining bounds */
1705 if (V4L2_TYPE_IS_OUTPUT(p->type))
1706 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1707 else
1708 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1709
1710 ret = ops->vidioc_g_selection(file, fh, &s);
1711 if (ret)
1712 return ret;
1713 p->bounds = s.r;
1714
1715 /* obtaining defrect */
1716 if (V4L2_TYPE_IS_OUTPUT(p->type))
1717 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1718 else
1719 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1720
1721 ret = ops->vidioc_g_selection(file, fh, &s);
1722 if (ret)
1723 return ret;
1724 p->defrect = s.r;
1725
1726 /* setting trivial pixelaspect */
1727 p->pixelaspect.numerator = 1;
1728 p->pixelaspect.denominator = 1;
1729 return 0;
1730 }
1731
1732 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1733 struct file *file, void *fh, void *arg)
1734 {
1735 struct video_device *vfd = video_devdata(file);
1736 int ret;
1737
1738 if (vfd->v4l2_dev)
1739 pr_info("%s: ================= START STATUS =================\n",
1740 vfd->v4l2_dev->name);
1741 ret = ops->vidioc_log_status(file, fh);
1742 if (vfd->v4l2_dev)
1743 pr_info("%s: ================== END STATUS ==================\n",
1744 vfd->v4l2_dev->name);
1745 return ret;
1746 }
1747
1748 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1749 struct file *file, void *fh, void *arg)
1750 {
1751 #ifdef CONFIG_VIDEO_ADV_DEBUG
1752 struct v4l2_dbg_register *p = arg;
1753 struct video_device *vfd = video_devdata(file);
1754 struct v4l2_subdev *sd;
1755 int idx = 0;
1756
1757 if (!capable(CAP_SYS_ADMIN))
1758 return -EPERM;
1759 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1760 if (vfd->v4l2_dev == NULL)
1761 return -EINVAL;
1762 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1763 if (p->match.addr == idx++)
1764 return v4l2_subdev_call(sd, core, g_register, p);
1765 return -EINVAL;
1766 }
1767 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1768 (ops->vidioc_g_chip_info || p->match.addr == 0))
1769 return ops->vidioc_g_register(file, fh, p);
1770 return -EINVAL;
1771 #else
1772 return -ENOTTY;
1773 #endif
1774 }
1775
1776 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1777 struct file *file, void *fh, void *arg)
1778 {
1779 #ifdef CONFIG_VIDEO_ADV_DEBUG
1780 const struct v4l2_dbg_register *p = arg;
1781 struct video_device *vfd = video_devdata(file);
1782 struct v4l2_subdev *sd;
1783 int idx = 0;
1784
1785 if (!capable(CAP_SYS_ADMIN))
1786 return -EPERM;
1787 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1788 if (vfd->v4l2_dev == NULL)
1789 return -EINVAL;
1790 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1791 if (p->match.addr == idx++)
1792 return v4l2_subdev_call(sd, core, s_register, p);
1793 return -EINVAL;
1794 }
1795 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1796 (ops->vidioc_g_chip_info || p->match.addr == 0))
1797 return ops->vidioc_s_register(file, fh, p);
1798 return -EINVAL;
1799 #else
1800 return -ENOTTY;
1801 #endif
1802 }
1803
1804 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
1805 struct file *file, void *fh, void *arg)
1806 {
1807 #ifdef CONFIG_VIDEO_ADV_DEBUG
1808 struct video_device *vfd = video_devdata(file);
1809 struct v4l2_dbg_chip_info *p = arg;
1810 struct v4l2_subdev *sd;
1811 int idx = 0;
1812
1813 switch (p->match.type) {
1814 case V4L2_CHIP_MATCH_BRIDGE:
1815 if (ops->vidioc_s_register)
1816 p->flags |= V4L2_CHIP_FL_WRITABLE;
1817 if (ops->vidioc_g_register)
1818 p->flags |= V4L2_CHIP_FL_READABLE;
1819 strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
1820 if (ops->vidioc_g_chip_info)
1821 return ops->vidioc_g_chip_info(file, fh, arg);
1822 if (p->match.addr)
1823 return -EINVAL;
1824 return 0;
1825
1826 case V4L2_CHIP_MATCH_SUBDEV:
1827 if (vfd->v4l2_dev == NULL)
1828 break;
1829 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
1830 if (p->match.addr != idx++)
1831 continue;
1832 if (sd->ops->core && sd->ops->core->s_register)
1833 p->flags |= V4L2_CHIP_FL_WRITABLE;
1834 if (sd->ops->core && sd->ops->core->g_register)
1835 p->flags |= V4L2_CHIP_FL_READABLE;
1836 strlcpy(p->name, sd->name, sizeof(p->name));
1837 return 0;
1838 }
1839 break;
1840 }
1841 return -EINVAL;
1842 #else
1843 return -ENOTTY;
1844 #endif
1845 }
1846
1847 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1848 struct file *file, void *fh, void *arg)
1849 {
1850 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1851 }
1852
1853 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1854 struct file *file, void *fh, void *arg)
1855 {
1856 return ops->vidioc_subscribe_event(fh, arg);
1857 }
1858
1859 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1860 struct file *file, void *fh, void *arg)
1861 {
1862 return ops->vidioc_unsubscribe_event(fh, arg);
1863 }
1864
1865 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1866 struct file *file, void *fh, void *arg)
1867 {
1868 struct v4l2_sliced_vbi_cap *p = arg;
1869 int ret = check_fmt(file, p->type);
1870
1871 if (ret)
1872 return ret;
1873
1874 /* Clear up to type, everything after type is zeroed already */
1875 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1876
1877 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1878 }
1879
1880 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1881 struct file *file, void *fh, void *arg)
1882 {
1883 struct video_device *vfd = video_devdata(file);
1884 struct v4l2_frequency_band *p = arg;
1885 enum v4l2_tuner_type type;
1886 int err;
1887
1888 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1889 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1890
1891 if (type != p->type)
1892 return -EINVAL;
1893 if (ops->vidioc_enum_freq_bands)
1894 return ops->vidioc_enum_freq_bands(file, fh, p);
1895 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
1896 struct v4l2_tuner t = {
1897 .index = p->tuner,
1898 .type = type,
1899 };
1900
1901 if (p->index)
1902 return -EINVAL;
1903 err = ops->vidioc_g_tuner(file, fh, &t);
1904 if (err)
1905 return err;
1906 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1907 p->rangelow = t.rangelow;
1908 p->rangehigh = t.rangehigh;
1909 p->modulation = (type == V4L2_TUNER_RADIO) ?
1910 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1911 return 0;
1912 }
1913 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
1914 struct v4l2_modulator m = {
1915 .index = p->tuner,
1916 };
1917
1918 if (type != V4L2_TUNER_RADIO)
1919 return -EINVAL;
1920 if (p->index)
1921 return -EINVAL;
1922 err = ops->vidioc_g_modulator(file, fh, &m);
1923 if (err)
1924 return err;
1925 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1926 p->rangelow = m.rangelow;
1927 p->rangehigh = m.rangehigh;
1928 p->modulation = (type == V4L2_TUNER_RADIO) ?
1929 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1930 return 0;
1931 }
1932 return -ENOTTY;
1933 }
1934
1935 struct v4l2_ioctl_info {
1936 unsigned int ioctl;
1937 u32 flags;
1938 const char * const name;
1939 union {
1940 u32 offset;
1941 int (*func)(const struct v4l2_ioctl_ops *ops,
1942 struct file *file, void *fh, void *p);
1943 } u;
1944 void (*debug)(const void *arg, bool write_only);
1945 };
1946
1947 /* This control needs a priority check */
1948 #define INFO_FL_PRIO (1 << 0)
1949 /* This control can be valid if the filehandle passes a control handler. */
1950 #define INFO_FL_CTRL (1 << 1)
1951 /* This is a standard ioctl, no need for special code */
1952 #define INFO_FL_STD (1 << 2)
1953 /* This is ioctl has its own function */
1954 #define INFO_FL_FUNC (1 << 3)
1955 /* Queuing ioctl */
1956 #define INFO_FL_QUEUE (1 << 4)
1957 /* Zero struct from after the field to the end */
1958 #define INFO_FL_CLEAR(v4l2_struct, field) \
1959 ((offsetof(struct v4l2_struct, field) + \
1960 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1961 #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
1962
1963 #define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1964 [_IOC_NR(_ioctl)] = { \
1965 .ioctl = _ioctl, \
1966 .flags = _flags | INFO_FL_STD, \
1967 .name = #_ioctl, \
1968 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
1969 .debug = _debug, \
1970 }
1971
1972 #define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1973 [_IOC_NR(_ioctl)] = { \
1974 .ioctl = _ioctl, \
1975 .flags = _flags | INFO_FL_FUNC, \
1976 .name = #_ioctl, \
1977 .u.func = _func, \
1978 .debug = _debug, \
1979 }
1980
1981 static struct v4l2_ioctl_info v4l2_ioctls[] = {
1982 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
1983 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1984 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1985 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
1986 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1987 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
1988 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1989 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
1990 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
1991 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
1992 IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
1993 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1994 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1995 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1996 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1997 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
1998 IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
1999 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2000 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2001 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2002 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2003 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2004 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2005 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2006 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2007 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
2008 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2009 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2010 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2011 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2012 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2013 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2014 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2015 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2016 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2017 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2018 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2019 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2020 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2021 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2022 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2023 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2024 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2025 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
2026 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2027 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2028 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2029 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2030 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2031 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2032 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2033 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2034 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)),
2035 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2036 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2037 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2038 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2039 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2040 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2041 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2042 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2043 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2044 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2045 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2046 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2047 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2048 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2049 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2050 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
2051 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2052 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2053 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2054 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2055 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2056 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2057 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
2058 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2059 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2060 IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2061 };
2062 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2063
2064 bool v4l2_is_known_ioctl(unsigned int cmd)
2065 {
2066 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2067 return false;
2068 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2069 }
2070
2071 struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2072 {
2073 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2074 return vdev->lock;
2075 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2076 return NULL;
2077 if (vdev->queue && vdev->queue->lock &&
2078 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2079 return vdev->queue->lock;
2080 return vdev->lock;
2081 }
2082
2083 /* Common ioctl debug function. This function can be used by
2084 external ioctl messages as well as internal V4L ioctl */
2085 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2086 {
2087 const char *dir, *type;
2088
2089 if (prefix)
2090 printk(KERN_DEBUG "%s: ", prefix);
2091
2092 switch (_IOC_TYPE(cmd)) {
2093 case 'd':
2094 type = "v4l2_int";
2095 break;
2096 case 'V':
2097 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2098 type = "v4l2";
2099 break;
2100 }
2101 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2102 return;
2103 default:
2104 type = "unknown";
2105 break;
2106 }
2107
2108 switch (_IOC_DIR(cmd)) {
2109 case _IOC_NONE: dir = "--"; break;
2110 case _IOC_READ: dir = "r-"; break;
2111 case _IOC_WRITE: dir = "-w"; break;
2112 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2113 default: dir = "*ERR*"; break;
2114 }
2115 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2116 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2117 }
2118 EXPORT_SYMBOL(v4l_printk_ioctl);
2119
2120 static long __video_do_ioctl(struct file *file,
2121 unsigned int cmd, void *arg)
2122 {
2123 struct video_device *vfd = video_devdata(file);
2124 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2125 bool write_only = false;
2126 struct v4l2_ioctl_info default_info;
2127 const struct v4l2_ioctl_info *info;
2128 void *fh = file->private_data;
2129 struct v4l2_fh *vfh = NULL;
2130 int use_fh_prio = 0;
2131 int debug = vfd->debug;
2132 long ret = -ENOTTY;
2133
2134 if (ops == NULL) {
2135 pr_warn("%s: has no ioctl_ops.\n",
2136 video_device_node_name(vfd));
2137 return ret;
2138 }
2139
2140 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2141 vfh = file->private_data;
2142 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2143 }
2144
2145 if (v4l2_is_known_ioctl(cmd)) {
2146 info = &v4l2_ioctls[_IOC_NR(cmd)];
2147
2148 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2149 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2150 goto done;
2151
2152 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2153 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2154 if (ret)
2155 goto done;
2156 }
2157 } else {
2158 default_info.ioctl = cmd;
2159 default_info.flags = 0;
2160 default_info.debug = v4l_print_default;
2161 info = &default_info;
2162 }
2163
2164 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2165 if (info->flags & INFO_FL_STD) {
2166 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2167 const void *p = vfd->ioctl_ops;
2168 const vidioc_op *vidioc = p + info->u.offset;
2169
2170 ret = (*vidioc)(file, fh, arg);
2171 } else if (info->flags & INFO_FL_FUNC) {
2172 ret = info->u.func(ops, file, fh, arg);
2173 } else if (!ops->vidioc_default) {
2174 ret = -ENOTTY;
2175 } else {
2176 ret = ops->vidioc_default(file, fh,
2177 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2178 cmd, arg);
2179 }
2180
2181 done:
2182 if (debug) {
2183 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2184 if (ret < 0)
2185 pr_cont(": error %ld", ret);
2186 if (debug == V4L2_DEBUG_IOCTL)
2187 pr_cont("\n");
2188 else if (_IOC_DIR(cmd) == _IOC_NONE)
2189 info->debug(arg, write_only);
2190 else {
2191 pr_cont(": ");
2192 info->debug(arg, write_only);
2193 }
2194 }
2195
2196 return ret;
2197 }
2198
2199 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2200 void * __user *user_ptr, void ***kernel_ptr)
2201 {
2202 int ret = 0;
2203
2204 switch (cmd) {
2205 case VIDIOC_PREPARE_BUF:
2206 case VIDIOC_QUERYBUF:
2207 case VIDIOC_QBUF:
2208 case VIDIOC_DQBUF: {
2209 struct v4l2_buffer *buf = parg;
2210
2211 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2212 if (buf->length > VIDEO_MAX_PLANES) {
2213 ret = -EINVAL;
2214 break;
2215 }
2216 *user_ptr = (void __user *)buf->m.planes;
2217 *kernel_ptr = (void *)&buf->m.planes;
2218 *array_size = sizeof(struct v4l2_plane) * buf->length;
2219 ret = 1;
2220 }
2221 break;
2222 }
2223
2224 case VIDIOC_SUBDEV_G_EDID:
2225 case VIDIOC_SUBDEV_S_EDID: {
2226 struct v4l2_subdev_edid *edid = parg;
2227
2228 if (edid->blocks) {
2229 if (edid->blocks > 256) {
2230 ret = -EINVAL;
2231 break;
2232 }
2233 *user_ptr = (void __user *)edid->edid;
2234 *kernel_ptr = (void *)&edid->edid;
2235 *array_size = edid->blocks * 128;
2236 ret = 1;
2237 }
2238 break;
2239 }
2240
2241 case VIDIOC_S_EXT_CTRLS:
2242 case VIDIOC_G_EXT_CTRLS:
2243 case VIDIOC_TRY_EXT_CTRLS: {
2244 struct v4l2_ext_controls *ctrls = parg;
2245
2246 if (ctrls->count != 0) {
2247 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2248 ret = -EINVAL;
2249 break;
2250 }
2251 *user_ptr = (void __user *)ctrls->controls;
2252 *kernel_ptr = (void *)&ctrls->controls;
2253 *array_size = sizeof(struct v4l2_ext_control)
2254 * ctrls->count;
2255 ret = 1;
2256 }
2257 break;
2258 }
2259 }
2260
2261 return ret;
2262 }
2263
2264 long
2265 video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2266 v4l2_kioctl func)
2267 {
2268 char sbuf[128];
2269 void *mbuf = NULL;
2270 void *parg = (void *)arg;
2271 long err = -EINVAL;
2272 bool has_array_args;
2273 size_t array_size = 0;
2274 void __user *user_ptr = NULL;
2275 void **kernel_ptr = NULL;
2276
2277 /* Copy arguments into temp kernel buffer */
2278 if (_IOC_DIR(cmd) != _IOC_NONE) {
2279 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2280 parg = sbuf;
2281 } else {
2282 /* too big to allocate from stack */
2283 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2284 if (NULL == mbuf)
2285 return -ENOMEM;
2286 parg = mbuf;
2287 }
2288
2289 err = -EFAULT;
2290 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2291 unsigned int n = _IOC_SIZE(cmd);
2292
2293 /*
2294 * In some cases, only a few fields are used as input,
2295 * i.e. when the app sets "index" and then the driver
2296 * fills in the rest of the structure for the thing
2297 * with that index. We only need to copy up the first
2298 * non-input field.
2299 */
2300 if (v4l2_is_known_ioctl(cmd)) {
2301 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2302 if (flags & INFO_FL_CLEAR_MASK)
2303 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2304 }
2305
2306 if (copy_from_user(parg, (void __user *)arg, n))
2307 goto out;
2308
2309 /* zero out anything we don't copy from userspace */
2310 if (n < _IOC_SIZE(cmd))
2311 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2312 } else {
2313 /* read-only ioctl */
2314 memset(parg, 0, _IOC_SIZE(cmd));
2315 }
2316 }
2317
2318 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2319 if (err < 0)
2320 goto out;
2321 has_array_args = err;
2322
2323 if (has_array_args) {
2324 /*
2325 * When adding new types of array args, make sure that the
2326 * parent argument to ioctl (which contains the pointer to the
2327 * array) fits into sbuf (so that mbuf will still remain
2328 * unused up to here).
2329 */
2330 mbuf = kmalloc(array_size, GFP_KERNEL);
2331 err = -ENOMEM;
2332 if (NULL == mbuf)
2333 goto out_array_args;
2334 err = -EFAULT;
2335 if (copy_from_user(mbuf, user_ptr, array_size))
2336 goto out_array_args;
2337 *kernel_ptr = mbuf;
2338 }
2339
2340 /* Handles IOCTL */
2341 err = func(file, cmd, parg);
2342 if (err == -ENOIOCTLCMD)
2343 err = -ENOTTY;
2344 if (err == 0) {
2345 if (cmd == VIDIOC_DQBUF)
2346 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2347 else if (cmd == VIDIOC_QBUF)
2348 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2349 }
2350
2351 if (has_array_args) {
2352 *kernel_ptr = user_ptr;
2353 if (copy_to_user(user_ptr, mbuf, array_size))
2354 err = -EFAULT;
2355 goto out_array_args;
2356 }
2357 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2358 results that must be returned. */
2359 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
2360 goto out;
2361
2362 out_array_args:
2363 /* Copy results into user buffer */
2364 switch (_IOC_DIR(cmd)) {
2365 case _IOC_READ:
2366 case (_IOC_WRITE | _IOC_READ):
2367 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2368 err = -EFAULT;
2369 break;
2370 }
2371
2372 out:
2373 kfree(mbuf);
2374 return err;
2375 }
2376 EXPORT_SYMBOL(video_usercopy);
2377
2378 long video_ioctl2(struct file *file,
2379 unsigned int cmd, unsigned long arg)
2380 {
2381 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2382 }
2383 EXPORT_SYMBOL(video_ioctl2);
This page took 0.083099 seconds and 6 git commands to generate.