1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Video capture interface for Linux version 2
5 * A generic framework to process V4L2 ioctl commands.
7 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
8 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
11 #include <linux/compat.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/version.h>
19 #include <linux/videodev2.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-fh.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-device.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/v4l2-mc.h>
29 #include <media/v4l2-mem2mem.h>
31 #include <trace/events/v4l2.h>
33 /* Zero out the end of the struct pointed to by p. Everything after, but
34 * not including, the specified field is cleared. */
35 #define CLEAR_AFTER_FIELD(p, field) \
36 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
37 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
39 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
46 static const struct std_descr standards[] = {
47 { V4L2_STD_NTSC, "NTSC" },
48 { V4L2_STD_NTSC_M, "NTSC-M" },
49 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
50 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
51 { V4L2_STD_NTSC_443, "NTSC-443" },
52 { V4L2_STD_PAL, "PAL" },
53 { V4L2_STD_PAL_BG, "PAL-BG" },
54 { V4L2_STD_PAL_B, "PAL-B" },
55 { V4L2_STD_PAL_B1, "PAL-B1" },
56 { V4L2_STD_PAL_G, "PAL-G" },
57 { V4L2_STD_PAL_H, "PAL-H" },
58 { V4L2_STD_PAL_I, "PAL-I" },
59 { V4L2_STD_PAL_DK, "PAL-DK" },
60 { V4L2_STD_PAL_D, "PAL-D" },
61 { V4L2_STD_PAL_D1, "PAL-D1" },
62 { V4L2_STD_PAL_K, "PAL-K" },
63 { V4L2_STD_PAL_M, "PAL-M" },
64 { V4L2_STD_PAL_N, "PAL-N" },
65 { V4L2_STD_PAL_Nc, "PAL-Nc" },
66 { V4L2_STD_PAL_60, "PAL-60" },
67 { V4L2_STD_SECAM, "SECAM" },
68 { V4L2_STD_SECAM_B, "SECAM-B" },
69 { V4L2_STD_SECAM_G, "SECAM-G" },
70 { V4L2_STD_SECAM_H, "SECAM-H" },
71 { V4L2_STD_SECAM_DK, "SECAM-DK" },
72 { V4L2_STD_SECAM_D, "SECAM-D" },
73 { V4L2_STD_SECAM_K, "SECAM-K" },
74 { V4L2_STD_SECAM_K1, "SECAM-K1" },
75 { V4L2_STD_SECAM_L, "SECAM-L" },
76 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
80 /* video4linux standard ID conversion to standard name
82 const char *v4l2_norm_to_name(v4l2_std_id id)
87 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
88 64 bit comparisons. So, on that architecture, with some gcc
89 variants, compilation fails. Currently, the max value is 30bit wide.
93 for (i = 0; standards[i].std; i++)
94 if (myid == standards[i].std)
96 return standards[i].descr;
98 EXPORT_SYMBOL(v4l2_norm_to_name);
100 /* Returns frame period for the given standard */
101 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
103 if (id & V4L2_STD_525_60) {
104 frameperiod->numerator = 1001;
105 frameperiod->denominator = 30000;
107 frameperiod->numerator = 1;
108 frameperiod->denominator = 25;
111 EXPORT_SYMBOL(v4l2_video_std_frame_period);
113 /* Fill in the fields of a v4l2_standard structure according to the
114 'id' and 'transmission' parameters. Returns negative on error. */
115 int v4l2_video_std_construct(struct v4l2_standard *vs,
116 int id, const char *name)
119 v4l2_video_std_frame_period(id, &vs->frameperiod);
120 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
121 strscpy(vs->name, name, sizeof(vs->name));
124 EXPORT_SYMBOL(v4l2_video_std_construct);
126 /* Fill in the fields of a v4l2_standard structure according to the
127 * 'id' and 'vs->index' parameters. Returns negative on error. */
128 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
130 v4l2_std_id curr_id = 0;
131 unsigned int index = vs->index, i, j = 0;
132 const char *descr = "";
134 /* Return -ENODATA if the id for the current input
135 or output is 0, meaning that it doesn't support this API. */
139 /* Return norm array in a canonical way */
140 for (i = 0; i <= index && id; i++) {
141 /* last std value in the standards array is 0, so this
142 while always ends there since (id & 0) == 0. */
143 while ((id & standards[j].std) != standards[j].std)
145 curr_id = standards[j].std;
146 descr = standards[j].descr;
150 if (curr_id != V4L2_STD_PAL &&
151 curr_id != V4L2_STD_SECAM &&
152 curr_id != V4L2_STD_NTSC)
158 v4l2_video_std_construct(vs, curr_id, descr);
162 /* ----------------------------------------------------------------- */
163 /* some arrays for pretty-printing debug messages of enum types */
165 const char *v4l2_field_names[] = {
166 [V4L2_FIELD_ANY] = "any",
167 [V4L2_FIELD_NONE] = "none",
168 [V4L2_FIELD_TOP] = "top",
169 [V4L2_FIELD_BOTTOM] = "bottom",
170 [V4L2_FIELD_INTERLACED] = "interlaced",
171 [V4L2_FIELD_SEQ_TB] = "seq-tb",
172 [V4L2_FIELD_SEQ_BT] = "seq-bt",
173 [V4L2_FIELD_ALTERNATE] = "alternate",
174 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
175 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
177 EXPORT_SYMBOL(v4l2_field_names);
179 const char *v4l2_type_names[] = {
181 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
182 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
183 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
184 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
185 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
186 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
187 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
188 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
189 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
190 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
191 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
192 [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
193 [V4L2_BUF_TYPE_META_CAPTURE] = "meta-cap",
194 [V4L2_BUF_TYPE_META_OUTPUT] = "meta-out",
196 EXPORT_SYMBOL(v4l2_type_names);
198 static const char *v4l2_memory_names[] = {
199 [V4L2_MEMORY_MMAP] = "mmap",
200 [V4L2_MEMORY_USERPTR] = "userptr",
201 [V4L2_MEMORY_OVERLAY] = "overlay",
202 [V4L2_MEMORY_DMABUF] = "dmabuf",
205 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
207 /* ------------------------------------------------------------------ */
208 /* debug help functions */
210 static void v4l_print_querycap(const void *arg, bool write_only)
212 const struct v4l2_capability *p = arg;
214 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
215 (int)sizeof(p->driver), p->driver,
216 (int)sizeof(p->card), p->card,
217 (int)sizeof(p->bus_info), p->bus_info,
218 p->version, p->capabilities, p->device_caps);
221 static void v4l_print_enuminput(const void *arg, bool write_only)
223 const struct v4l2_input *p = arg;
225 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
226 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
227 p->tuner, (unsigned long long)p->std, p->status,
231 static void v4l_print_enumoutput(const void *arg, bool write_only)
233 const struct v4l2_output *p = arg;
235 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
236 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
237 p->modulator, (unsigned long long)p->std, p->capabilities);
240 static void v4l_print_audio(const void *arg, bool write_only)
242 const struct v4l2_audio *p = arg;
245 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
247 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
248 p->index, (int)sizeof(p->name), p->name,
249 p->capability, p->mode);
252 static void v4l_print_audioout(const void *arg, bool write_only)
254 const struct v4l2_audioout *p = arg;
257 pr_cont("index=%u\n", p->index);
259 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
260 p->index, (int)sizeof(p->name), p->name,
261 p->capability, p->mode);
264 static void v4l_print_fmtdesc(const void *arg, bool write_only)
266 const struct v4l2_fmtdesc *p = arg;
268 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%p4cc, mbus_code=0x%04x, description='%.*s'\n",
269 p->index, prt_names(p->type, v4l2_type_names),
270 p->flags, &p->pixelformat, p->mbus_code,
271 (int)sizeof(p->description), p->description);
274 static void v4l_print_format(const void *arg, bool write_only)
276 const struct v4l2_format *p = arg;
277 const struct v4l2_pix_format *pix;
278 const struct v4l2_pix_format_mplane *mp;
279 const struct v4l2_vbi_format *vbi;
280 const struct v4l2_sliced_vbi_format *sliced;
281 const struct v4l2_window *win;
282 const struct v4l2_sdr_format *sdr;
283 const struct v4l2_meta_format *meta;
287 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
289 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
290 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
292 pr_cont(", width=%u, height=%u, pixelformat=%p4cc, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
293 pix->width, pix->height, &pix->pixelformat,
294 prt_names(pix->field, v4l2_field_names),
295 pix->bytesperline, pix->sizeimage,
296 pix->colorspace, pix->flags, pix->ycbcr_enc,
297 pix->quantization, pix->xfer_func);
299 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
300 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
302 pr_cont(", width=%u, height=%u, format=%p4cc, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
303 mp->width, mp->height, &mp->pixelformat,
304 prt_names(mp->field, v4l2_field_names),
305 mp->colorspace, mp->num_planes, mp->flags,
306 mp->ycbcr_enc, mp->quantization, mp->xfer_func);
307 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
308 for (i = 0; i < planes; i++)
309 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
310 mp->plane_fmt[i].bytesperline,
311 mp->plane_fmt[i].sizeimage);
313 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
314 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
316 /* Note: we can't print the clip list here since the clips
317 * pointer is a userspace pointer, not a kernelspace
319 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",
320 win->w.width, win->w.height, win->w.left, win->w.top,
321 prt_names(win->field, v4l2_field_names),
322 win->chromakey, win->clipcount, win->clips,
323 win->bitmap, win->global_alpha);
325 case V4L2_BUF_TYPE_VBI_CAPTURE:
326 case V4L2_BUF_TYPE_VBI_OUTPUT:
328 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%p4cc, start=%u,%u, count=%u,%u\n",
329 vbi->sampling_rate, vbi->offset,
330 vbi->samples_per_line, &vbi->sample_format,
331 vbi->start[0], vbi->start[1],
332 vbi->count[0], vbi->count[1]);
334 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
335 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
336 sliced = &p->fmt.sliced;
337 pr_cont(", service_set=0x%08x, io_size=%d\n",
338 sliced->service_set, sliced->io_size);
339 for (i = 0; i < 24; i++)
340 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
341 sliced->service_lines[0][i],
342 sliced->service_lines[1][i]);
344 case V4L2_BUF_TYPE_SDR_CAPTURE:
345 case V4L2_BUF_TYPE_SDR_OUTPUT:
347 pr_cont(", pixelformat=%p4cc\n", &sdr->pixelformat);
349 case V4L2_BUF_TYPE_META_CAPTURE:
350 case V4L2_BUF_TYPE_META_OUTPUT:
352 pr_cont(", dataformat=%p4cc, buffersize=%u\n",
353 &meta->dataformat, meta->buffersize);
358 static void v4l_print_framebuffer(const void *arg, bool write_only)
360 const struct v4l2_framebuffer *p = arg;
362 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%p4cc, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
363 p->capability, p->flags, p->base, p->fmt.width, p->fmt.height,
364 &p->fmt.pixelformat, p->fmt.bytesperline, p->fmt.sizeimage,
368 static void v4l_print_buftype(const void *arg, bool write_only)
370 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
373 static void v4l_print_modulator(const void *arg, bool write_only)
375 const struct v4l2_modulator *p = arg;
378 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
380 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
381 p->index, (int)sizeof(p->name), p->name, p->capability,
382 p->rangelow, p->rangehigh, p->txsubchans);
385 static void v4l_print_tuner(const void *arg, bool write_only)
387 const struct v4l2_tuner *p = arg;
390 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
392 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
393 p->index, (int)sizeof(p->name), p->name, p->type,
394 p->capability, p->rangelow,
395 p->rangehigh, p->signal, p->afc,
396 p->rxsubchans, p->audmode);
399 static void v4l_print_frequency(const void *arg, bool write_only)
401 const struct v4l2_frequency *p = arg;
403 pr_cont("tuner=%u, type=%u, frequency=%u\n",
404 p->tuner, p->type, p->frequency);
407 static void v4l_print_standard(const void *arg, bool write_only)
409 const struct v4l2_standard *p = arg;
411 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
413 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
414 p->frameperiod.numerator,
415 p->frameperiod.denominator,
419 static void v4l_print_std(const void *arg, bool write_only)
421 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
424 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
426 const struct v4l2_hw_freq_seek *p = arg;
428 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
429 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
430 p->rangelow, p->rangehigh);
433 static void v4l_print_requestbuffers(const void *arg, bool write_only)
435 const struct v4l2_requestbuffers *p = arg;
437 pr_cont("count=%d, type=%s, memory=%s\n",
439 prt_names(p->type, v4l2_type_names),
440 prt_names(p->memory, v4l2_memory_names));
443 static void v4l_print_buffer(const void *arg, bool write_only)
445 const struct v4l2_buffer *p = arg;
446 const struct v4l2_timecode *tc = &p->timecode;
447 const struct v4l2_plane *plane;
450 pr_cont("%02d:%02d:%02d.%06ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
451 (int)p->timestamp.tv_sec / 3600,
452 ((int)p->timestamp.tv_sec / 60) % 60,
453 ((int)p->timestamp.tv_sec % 60),
454 (long)p->timestamp.tv_usec,
456 prt_names(p->type, v4l2_type_names), p->request_fd,
457 p->flags, prt_names(p->field, v4l2_field_names),
458 p->sequence, prt_names(p->memory, v4l2_memory_names));
460 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
462 for (i = 0; i < p->length; ++i) {
463 plane = &p->m.planes[i];
465 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
466 i, plane->bytesused, plane->data_offset,
467 plane->m.userptr, plane->length);
470 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
471 p->bytesused, p->m.userptr, p->length);
474 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
475 tc->hours, tc->minutes, tc->seconds,
476 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
479 static void v4l_print_exportbuffer(const void *arg, bool write_only)
481 const struct v4l2_exportbuffer *p = arg;
483 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
484 p->fd, prt_names(p->type, v4l2_type_names),
485 p->index, p->plane, p->flags);
488 static void v4l_print_create_buffers(const void *arg, bool write_only)
490 const struct v4l2_create_buffers *p = arg;
492 pr_cont("index=%d, count=%d, memory=%s, capabilities=0x%08x, ",
493 p->index, p->count, prt_names(p->memory, v4l2_memory_names),
495 v4l_print_format(&p->format, write_only);
498 static void v4l_print_streamparm(const void *arg, bool write_only)
500 const struct v4l2_streamparm *p = arg;
502 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
504 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
505 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
506 const struct v4l2_captureparm *c = &p->parm.capture;
508 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
509 c->capability, c->capturemode,
510 c->timeperframe.numerator, c->timeperframe.denominator,
511 c->extendedmode, c->readbuffers);
512 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
513 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
514 const struct v4l2_outputparm *c = &p->parm.output;
516 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
517 c->capability, c->outputmode,
518 c->timeperframe.numerator, c->timeperframe.denominator,
519 c->extendedmode, c->writebuffers);
525 static void v4l_print_queryctrl(const void *arg, bool write_only)
527 const struct v4l2_queryctrl *p = arg;
529 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
530 p->id, p->type, (int)sizeof(p->name), p->name,
531 p->minimum, p->maximum,
532 p->step, p->default_value, p->flags);
535 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
537 const struct v4l2_query_ext_ctrl *p = arg;
539 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
540 p->id, p->type, (int)sizeof(p->name), p->name,
541 p->minimum, p->maximum,
542 p->step, p->default_value, p->flags,
543 p->elem_size, p->elems, p->nr_of_dims,
544 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
547 static void v4l_print_querymenu(const void *arg, bool write_only)
549 const struct v4l2_querymenu *p = arg;
551 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
554 static void v4l_print_control(const void *arg, bool write_only)
556 const struct v4l2_control *p = arg;
557 const char *name = v4l2_ctrl_get_name(p->id);
560 pr_cont("name=%s, ", name);
561 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
564 static void v4l_print_ext_controls(const void *arg, bool write_only)
566 const struct v4l2_ext_controls *p = arg;
569 pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
570 p->which, p->count, p->error_idx, p->request_fd);
571 for (i = 0; i < p->count; i++) {
572 unsigned int id = p->controls[i].id;
573 const char *name = v4l2_ctrl_get_name(id);
576 pr_cont(", name=%s", name);
577 if (!p->controls[i].size)
578 pr_cont(", id/val=0x%x/0x%x", id, p->controls[i].value);
580 pr_cont(", id/size=0x%x/%u", id, p->controls[i].size);
585 static void v4l_print_cropcap(const void *arg, bool write_only)
587 const struct v4l2_cropcap *p = arg;
589 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
590 prt_names(p->type, v4l2_type_names),
591 p->bounds.width, p->bounds.height,
592 p->bounds.left, p->bounds.top,
593 p->defrect.width, p->defrect.height,
594 p->defrect.left, p->defrect.top,
595 p->pixelaspect.numerator, p->pixelaspect.denominator);
598 static void v4l_print_crop(const void *arg, bool write_only)
600 const struct v4l2_crop *p = arg;
602 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
603 prt_names(p->type, v4l2_type_names),
604 p->c.width, p->c.height,
605 p->c.left, p->c.top);
608 static void v4l_print_selection(const void *arg, bool write_only)
610 const struct v4l2_selection *p = arg;
612 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
613 prt_names(p->type, v4l2_type_names),
615 p->r.width, p->r.height, p->r.left, p->r.top);
618 static void v4l_print_jpegcompression(const void *arg, bool write_only)
620 const struct v4l2_jpegcompression *p = arg;
622 pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
623 p->quality, p->APPn, p->APP_len,
624 p->COM_len, p->jpeg_markers);
627 static void v4l_print_enc_idx(const void *arg, bool write_only)
629 const struct v4l2_enc_idx *p = arg;
631 pr_cont("entries=%d, entries_cap=%d\n",
632 p->entries, p->entries_cap);
635 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
637 const struct v4l2_encoder_cmd *p = arg;
639 pr_cont("cmd=%d, flags=0x%x\n",
643 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
645 const struct v4l2_decoder_cmd *p = arg;
647 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
649 if (p->cmd == V4L2_DEC_CMD_START)
650 pr_info("speed=%d, format=%u\n",
651 p->start.speed, p->start.format);
652 else if (p->cmd == V4L2_DEC_CMD_STOP)
653 pr_info("pts=%llu\n", p->stop.pts);
656 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
658 const struct v4l2_dbg_chip_info *p = arg;
660 pr_cont("type=%u, ", p->match.type);
661 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
662 pr_cont("name=%.*s, ",
663 (int)sizeof(p->match.name), p->match.name);
665 pr_cont("addr=%u, ", p->match.addr);
666 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
669 static void v4l_print_dbg_register(const void *arg, bool write_only)
671 const struct v4l2_dbg_register *p = arg;
673 pr_cont("type=%u, ", p->match.type);
674 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
675 pr_cont("name=%.*s, ",
676 (int)sizeof(p->match.name), p->match.name);
678 pr_cont("addr=%u, ", p->match.addr);
679 pr_cont("reg=0x%llx, val=0x%llx\n",
683 static void v4l_print_dv_timings(const void *arg, bool write_only)
685 const struct v4l2_dv_timings *p = arg;
688 case V4L2_DV_BT_656_1120:
689 pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
690 p->bt.interlaced, p->bt.pixelclock,
691 p->bt.width, p->bt.height,
692 p->bt.polarities, p->bt.hfrontporch,
693 p->bt.hsync, p->bt.hbackporch,
694 p->bt.vfrontporch, p->bt.vsync,
695 p->bt.vbackporch, p->bt.il_vfrontporch,
696 p->bt.il_vsync, p->bt.il_vbackporch,
697 p->bt.standards, p->bt.flags);
700 pr_cont("type=%d\n", p->type);
705 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
707 const struct v4l2_enum_dv_timings *p = arg;
709 pr_cont("index=%u, ", p->index);
710 v4l_print_dv_timings(&p->timings, write_only);
713 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
715 const struct v4l2_dv_timings_cap *p = arg;
718 case V4L2_DV_BT_656_1120:
719 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
720 p->bt.min_width, p->bt.max_width,
721 p->bt.min_height, p->bt.max_height,
722 p->bt.min_pixelclock, p->bt.max_pixelclock,
723 p->bt.standards, p->bt.capabilities);
726 pr_cont("type=%u\n", p->type);
731 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
733 const struct v4l2_frmsizeenum *p = arg;
735 pr_cont("index=%u, pixelformat=%p4cc, type=%u",
736 p->index, &p->pixel_format, p->type);
738 case V4L2_FRMSIZE_TYPE_DISCRETE:
739 pr_cont(", wxh=%ux%u\n",
740 p->discrete.width, p->discrete.height);
742 case V4L2_FRMSIZE_TYPE_STEPWISE:
743 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
744 p->stepwise.min_width,
745 p->stepwise.min_height,
746 p->stepwise.max_width,
747 p->stepwise.max_height,
748 p->stepwise.step_width,
749 p->stepwise.step_height);
751 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
758 static void v4l_print_frmivalenum(const void *arg, bool write_only)
760 const struct v4l2_frmivalenum *p = arg;
762 pr_cont("index=%u, pixelformat=%p4cc, wxh=%ux%u, type=%u",
763 p->index, &p->pixel_format, p->width, p->height, p->type);
765 case V4L2_FRMIVAL_TYPE_DISCRETE:
766 pr_cont(", fps=%d/%d\n",
767 p->discrete.numerator,
768 p->discrete.denominator);
770 case V4L2_FRMIVAL_TYPE_STEPWISE:
771 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
772 p->stepwise.min.numerator,
773 p->stepwise.min.denominator,
774 p->stepwise.max.numerator,
775 p->stepwise.max.denominator,
776 p->stepwise.step.numerator,
777 p->stepwise.step.denominator);
779 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
786 static void v4l_print_event(const void *arg, bool write_only)
788 const struct v4l2_event *p = arg;
789 const struct v4l2_event_ctrl *c;
791 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%llu.%9.9llu\n",
792 p->type, p->pending, p->sequence, p->id,
793 p->timestamp.tv_sec, p->timestamp.tv_nsec);
795 case V4L2_EVENT_VSYNC:
796 printk(KERN_DEBUG "field=%s\n",
797 prt_names(p->u.vsync.field, v4l2_field_names));
799 case V4L2_EVENT_CTRL:
801 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
802 c->changes, c->type);
803 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
804 pr_cont("value64=%lld, ", c->value64);
806 pr_cont("value=%d, ", c->value);
807 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
808 c->flags, c->minimum, c->maximum,
809 c->step, c->default_value);
811 case V4L2_EVENT_FRAME_SYNC:
812 pr_cont("frame_sequence=%u\n",
813 p->u.frame_sync.frame_sequence);
818 static void v4l_print_event_subscription(const void *arg, bool write_only)
820 const struct v4l2_event_subscription *p = arg;
822 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
823 p->type, p->id, p->flags);
826 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
828 const struct v4l2_sliced_vbi_cap *p = arg;
831 pr_cont("type=%s, service_set=0x%08x\n",
832 prt_names(p->type, v4l2_type_names), p->service_set);
833 for (i = 0; i < 24; i++)
834 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
835 p->service_lines[0][i],
836 p->service_lines[1][i]);
839 static void v4l_print_freq_band(const void *arg, bool write_only)
841 const struct v4l2_frequency_band *p = arg;
843 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
844 p->tuner, p->type, p->index,
845 p->capability, p->rangelow,
846 p->rangehigh, p->modulation);
849 static void v4l_print_edid(const void *arg, bool write_only)
851 const struct v4l2_edid *p = arg;
853 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
854 p->pad, p->start_block, p->blocks);
857 static void v4l_print_u32(const void *arg, bool write_only)
859 pr_cont("value=%u\n", *(const u32 *)arg);
862 static void v4l_print_newline(const void *arg, bool write_only)
867 static void v4l_print_default(const void *arg, bool write_only)
869 pr_cont("driver-specific ioctl\n");
872 static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
876 /* zero the reserved fields */
878 for (i = 0; i < c->count; i++)
879 c->controls[i].reserved2[0] = 0;
882 case V4L2_CID_PRIVATE_BASE:
884 * V4L2_CID_PRIVATE_BASE cannot be used as control class
885 * when using extended controls.
886 * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
887 * is it allowed for backwards compatibility.
889 if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
892 case V4L2_CTRL_WHICH_DEF_VAL:
893 /* Default value cannot be changed */
894 if (ioctl == VIDIOC_S_EXT_CTRLS ||
895 ioctl == VIDIOC_TRY_EXT_CTRLS) {
896 c->error_idx = c->count;
900 case V4L2_CTRL_WHICH_CUR_VAL:
902 case V4L2_CTRL_WHICH_REQUEST_VAL:
903 c->error_idx = c->count;
907 /* Check that all controls are from the same control class. */
908 for (i = 0; i < c->count; i++) {
909 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
910 c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
918 static int check_fmt(struct file *file, enum v4l2_buf_type type)
920 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
921 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
922 V4L2_CAP_VIDEO_OUTPUT |
923 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
924 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
925 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
926 V4L2_CAP_META_OUTPUT;
927 struct video_device *vfd = video_devdata(file);
928 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
929 bool is_vid = vfd->vfl_type == VFL_TYPE_VIDEO &&
930 (vfd->device_caps & vid_caps);
931 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
932 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
933 bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
934 bool is_meta = vfd->vfl_type == VFL_TYPE_VIDEO &&
935 (vfd->device_caps & meta_caps);
936 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
937 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
943 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
944 if ((is_vid || is_tch) && is_rx &&
945 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
948 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
949 if ((is_vid || is_tch) && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
952 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
953 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
956 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
957 if (is_vid && is_tx &&
958 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
961 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
962 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
965 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
966 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
969 case V4L2_BUF_TYPE_VBI_CAPTURE:
970 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
973 case V4L2_BUF_TYPE_VBI_OUTPUT:
974 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
977 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
978 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
981 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
982 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
985 case V4L2_BUF_TYPE_SDR_CAPTURE:
986 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
989 case V4L2_BUF_TYPE_SDR_OUTPUT:
990 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
993 case V4L2_BUF_TYPE_META_CAPTURE:
994 if (is_meta && is_rx && ops->vidioc_g_fmt_meta_cap)
997 case V4L2_BUF_TYPE_META_OUTPUT:
998 if (is_meta && is_tx && ops->vidioc_g_fmt_meta_out)
1007 static void v4l_sanitize_format(struct v4l2_format *fmt)
1009 unsigned int offset;
1011 /* Make sure num_planes is not bogus */
1012 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1013 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1014 fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1018 * The v4l2_pix_format structure has been extended with fields that were
1019 * not previously required to be set to zero by applications. The priv
1020 * field, when set to a magic value, indicates the the extended fields
1021 * are valid. Otherwise they will contain undefined values. To simplify
1022 * the API towards drivers zero the extended fields and set the priv
1023 * field to the magic value when the extended pixel format structure
1024 * isn't used by applications.
1027 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1028 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1031 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1034 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1036 offset = offsetof(struct v4l2_pix_format, priv)
1037 + sizeof(fmt->fmt.pix.priv);
1038 memset(((void *)&fmt->fmt.pix) + offset, 0,
1039 sizeof(fmt->fmt.pix) - offset);
1042 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1043 struct file *file, void *fh, void *arg)
1045 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1046 struct video_device *vfd = video_devdata(file);
1049 cap->version = LINUX_VERSION_CODE;
1050 cap->device_caps = vfd->device_caps;
1051 cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1053 ret = ops->vidioc_querycap(file, fh, cap);
1056 * Drivers must not change device_caps, so check for this and
1057 * warn if this happened.
1059 WARN_ON(cap->device_caps != vfd->device_caps);
1061 * Check that capabilities is a superset of
1062 * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1064 WARN_ON((cap->capabilities &
1065 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1066 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1067 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1068 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1073 static int v4l_g_input(const struct v4l2_ioctl_ops *ops,
1074 struct file *file, void *fh, void *arg)
1076 struct video_device *vfd = video_devdata(file);
1078 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1083 return ops->vidioc_g_input(file, fh, arg);
1086 static int v4l_g_output(const struct v4l2_ioctl_ops *ops,
1087 struct file *file, void *fh, void *arg)
1089 struct video_device *vfd = video_devdata(file);
1091 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1096 return ops->vidioc_g_output(file, fh, arg);
1099 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1100 struct file *file, void *fh, void *arg)
1102 struct video_device *vfd = video_devdata(file);
1105 ret = v4l_enable_media_source(vfd);
1109 if (vfd->device_caps & V4L2_CAP_IO_MC)
1110 return *(int *)arg ? -EINVAL : 0;
1112 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1115 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1116 struct file *file, void *fh, void *arg)
1118 struct video_device *vfd = video_devdata(file);
1120 if (vfd->device_caps & V4L2_CAP_IO_MC)
1121 return *(int *)arg ? -EINVAL : 0;
1123 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1126 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1127 struct file *file, void *fh, void *arg)
1129 struct video_device *vfd;
1132 vfd = video_devdata(file);
1133 *p = v4l2_prio_max(vfd->prio);
1137 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1138 struct file *file, void *fh, void *arg)
1140 struct video_device *vfd;
1141 struct v4l2_fh *vfh;
1144 vfd = video_devdata(file);
1145 if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1147 vfh = file->private_data;
1148 return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1151 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1152 struct file *file, void *fh, void *arg)
1154 struct video_device *vfd = video_devdata(file);
1155 struct v4l2_input *p = arg;
1158 * We set the flags for CAP_DV_TIMINGS &
1159 * CAP_STD here based on ioctl handler provided by the
1160 * driver. If the driver doesn't support these
1161 * for a specific input, it must override these flags.
1163 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1164 p->capabilities |= V4L2_IN_CAP_STD;
1166 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1169 strscpy(p->name, vfd->name, sizeof(p->name));
1170 p->type = V4L2_INPUT_TYPE_CAMERA;
1174 return ops->vidioc_enum_input(file, fh, p);
1177 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1178 struct file *file, void *fh, void *arg)
1180 struct video_device *vfd = video_devdata(file);
1181 struct v4l2_output *p = arg;
1184 * We set the flags for CAP_DV_TIMINGS &
1185 * CAP_STD here based on ioctl handler provided by the
1186 * driver. If the driver doesn't support these
1187 * for a specific output, it must override these flags.
1189 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1190 p->capabilities |= V4L2_OUT_CAP_STD;
1192 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1195 strscpy(p->name, vfd->name, sizeof(p->name));
1196 p->type = V4L2_OUTPUT_TYPE_ANALOG;
1200 return ops->vidioc_enum_output(file, fh, p);
1203 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1205 const unsigned sz = sizeof(fmt->description);
1206 const char *descr = NULL;
1210 * We depart from the normal coding style here since the descriptions
1211 * should be aligned so it is easy to see which descriptions will be
1212 * longer than 31 characters (the max length for a description).
1213 * And frankly, this is easier to read anyway.
1215 * Note that gcc will use O(log N) comparisons to find the right case.
1217 switch (fmt->pixelformat) {
1218 /* Max description length mask: descr = "0123456789012345678901234567890" */
1219 case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
1220 case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
1221 case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
1222 case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
1223 case V4L2_PIX_FMT_RGBA444: descr = "16-bit RGBA 4-4-4-4"; break;
1224 case V4L2_PIX_FMT_RGBX444: descr = "16-bit RGBX 4-4-4-4"; break;
1225 case V4L2_PIX_FMT_ABGR444: descr = "16-bit ABGR 4-4-4-4"; break;
1226 case V4L2_PIX_FMT_XBGR444: descr = "16-bit XBGR 4-4-4-4"; break;
1227 case V4L2_PIX_FMT_BGRA444: descr = "16-bit BGRA 4-4-4-4"; break;
1228 case V4L2_PIX_FMT_BGRX444: descr = "16-bit BGRX 4-4-4-4"; break;
1229 case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
1230 case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
1231 case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
1232 case V4L2_PIX_FMT_ABGR555: descr = "16-bit ABGR 1-5-5-5"; break;
1233 case V4L2_PIX_FMT_XBGR555: descr = "16-bit XBGR 1-5-5-5"; break;
1234 case V4L2_PIX_FMT_RGBA555: descr = "16-bit RGBA 5-5-5-1"; break;
1235 case V4L2_PIX_FMT_RGBX555: descr = "16-bit RGBX 5-5-5-1"; break;
1236 case V4L2_PIX_FMT_BGRA555: descr = "16-bit BGRA 5-5-5-1"; break;
1237 case V4L2_PIX_FMT_BGRX555: descr = "16-bit BGRX 5-5-5-1"; break;
1238 case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
1239 case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1240 case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
1241 case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
1242 case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
1243 case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
1244 case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
1245 case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
1246 case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
1247 case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
1248 case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
1249 case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
1250 case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
1251 case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
1252 case V4L2_PIX_FMT_BGRA32: descr = "32-bit ABGR 8-8-8-8"; break;
1253 case V4L2_PIX_FMT_BGRX32: descr = "32-bit XBGR 8-8-8-8"; break;
1254 case V4L2_PIX_FMT_RGBA32: descr = "32-bit RGBA 8-8-8-8"; break;
1255 case V4L2_PIX_FMT_RGBX32: descr = "32-bit RGBX 8-8-8-8"; break;
1256 case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
1257 case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
1258 case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
1259 case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
1260 case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
1261 case V4L2_PIX_FMT_Y14: descr = "14-bit Greyscale"; break;
1262 case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
1263 case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
1264 case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
1265 case V4L2_PIX_FMT_Y10P: descr = "10-bit Greyscale (MIPI Packed)"; break;
1266 case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
1267 case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
1268 case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
1269 case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
1270 case V4L2_PIX_FMT_CNF4: descr = "4-bit Depth Confidence (Packed)"; break;
1271 case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
1272 case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
1273 case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
1274 case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
1275 case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
1276 case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
1277 case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
1278 case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
1279 case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
1280 case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
1281 case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
1282 case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
1283 case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
1284 case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
1285 case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
1286 case V4L2_PIX_FMT_YUV24: descr = "24-bit YUV 4:4:4 8-8-8"; break;
1287 case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
1288 case V4L2_PIX_FMT_AYUV32: descr = "32-bit AYUV 8-8-8-8"; break;
1289 case V4L2_PIX_FMT_XYUV32: descr = "32-bit XYUV 8-8-8-8"; break;
1290 case V4L2_PIX_FMT_VUYA32: descr = "32-bit VUYA 8-8-8-8"; break;
1291 case V4L2_PIX_FMT_VUYX32: descr = "32-bit VUYX 8-8-8-8"; break;
1292 case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
1293 case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
1294 case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
1295 case V4L2_PIX_FMT_HM12: descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
1296 case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
1297 case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
1298 case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
1299 case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
1300 case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
1301 case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
1302 case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
1303 case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
1304 case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
1305 case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
1306 case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
1307 case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1308 case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1309 case V4L2_PIX_FMT_NV12_COL128: descr = "Y/CbCr 4:2:0 (128b cols)"; break;
1310 case V4L2_PIX_FMT_NV12_10_COL128: descr = "10-bit Y/CbCr 4:2:0 (128b cols)"; break;
1311 case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
1312 case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
1313 case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
1314 case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
1315 case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
1316 case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
1317 case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
1318 case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
1319 case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
1320 case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
1321 case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
1322 case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
1323 case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
1324 case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
1325 case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1326 case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1327 case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1328 case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1329 case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1330 case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1331 case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1332 case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1333 case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1334 case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1335 case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1336 case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1337 case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1338 case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1339 case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1340 case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1341 case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
1342 case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
1343 case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
1344 case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
1345 case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1346 case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1347 case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1348 case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1349 case V4L2_PIX_FMT_SBGGR14: descr = "14-bit Bayer BGBG/GRGR"; break;
1350 case V4L2_PIX_FMT_SGBRG14: descr = "14-bit Bayer GBGB/RGRG"; break;
1351 case V4L2_PIX_FMT_SGRBG14: descr = "14-bit Bayer GRGR/BGBG"; break;
1352 case V4L2_PIX_FMT_SRGGB14: descr = "14-bit Bayer RGRG/GBGB"; break;
1353 case V4L2_PIX_FMT_SBGGR14P: descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1354 case V4L2_PIX_FMT_SGBRG14P: descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1355 case V4L2_PIX_FMT_SGRBG14P: descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1356 case V4L2_PIX_FMT_SRGGB14P: descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1357 case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
1358 case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
1359 case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
1360 case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
1361 case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1362 case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
1363 case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
1364 case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
1365 case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
1366 case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
1367 case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
1368 case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
1369 case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
1370 case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
1371 case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
1372 case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
1373 case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
1374 case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
1375 case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
1376 case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
1377 case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
1378 case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
1379 case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit Signed Deltas"; break;
1380 case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit Signed Deltas"; break;
1381 case V4L2_TCH_FMT_TU16: descr = "16-bit Unsigned Touch Data"; break;
1382 case V4L2_TCH_FMT_TU08: descr = "8-bit Unsigned Touch Data"; break;
1383 case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
1384 case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
1385 case V4L2_META_FMT_UVC: descr = "UVC Payload Header Metadata"; break;
1386 case V4L2_META_FMT_D4XX: descr = "Intel D4xx UVC Metadata"; break;
1387 case V4L2_META_FMT_VIVID: descr = "Vivid Metadata"; break;
1388 case V4L2_META_FMT_RK_ISP1_PARAMS: descr = "Rockchip ISP1 3A Parameters"; break;
1389 case V4L2_META_FMT_RK_ISP1_STAT_3A: descr = "Rockchip ISP1 3A Statistics"; break;
1390 case V4L2_META_FMT_SENSOR_DATA: descr = "Sensor Ancillary Metadata"; break;
1391 case V4L2_META_FMT_BCM2835_ISP_STATS: descr = "BCM2835 ISP Image Statistics"; break;
1394 /* Compressed formats */
1395 flags = V4L2_FMT_FLAG_COMPRESSED;
1396 switch (fmt->pixelformat) {
1397 /* Max description length mask: descr = "0123456789012345678901234567890" */
1398 case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
1399 case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
1400 case V4L2_PIX_FMT_DV: descr = "1394"; break;
1401 case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
1402 case V4L2_PIX_FMT_H264: descr = "H.264"; break;
1403 case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
1404 case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
1405 case V4L2_PIX_FMT_H264_SLICE: descr = "H.264 Parsed Slice Data"; break;
1406 case V4L2_PIX_FMT_H263: descr = "H.263"; break;
1407 case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
1408 case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
1409 case V4L2_PIX_FMT_MPEG2_SLICE: descr = "MPEG-2 Parsed Slice Data"; break;
1410 case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 Part 2 ES"; break;
1411 case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
1412 case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
1413 case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
1414 case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
1415 case V4L2_PIX_FMT_VP8_FRAME: descr = "VP8 Frame"; break;
1416 case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
1417 case V4L2_PIX_FMT_HEVC: descr = "HEVC"; break; /* aka H.265 */
1418 case V4L2_PIX_FMT_HEVC_SLICE: descr = "HEVC Parsed Slice Data"; break;
1419 case V4L2_PIX_FMT_FWHT: descr = "FWHT"; break; /* used in vicodec */
1420 case V4L2_PIX_FMT_FWHT_STATELESS: descr = "FWHT Stateless"; break; /* used in vicodec */
1421 case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
1422 case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
1423 case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
1424 case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
1425 case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
1426 case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
1427 case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
1428 case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
1429 case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
1430 case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
1431 case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
1432 case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
1433 case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
1434 case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
1435 case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
1436 case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
1437 case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
1438 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1439 case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
1440 case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
1442 if (fmt->description[0])
1444 WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1446 snprintf(fmt->description, sz, "%p4cc",
1453 WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1454 fmt->flags |= flags;
1457 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1458 struct file *file, void *fh, void *arg)
1460 struct video_device *vdev = video_devdata(file);
1461 struct v4l2_fmtdesc *p = arg;
1462 int ret = check_fmt(file, p->type);
1470 if (!(vdev->device_caps & V4L2_CAP_IO_MC))
1473 mbus_code = p->mbus_code;
1474 CLEAR_AFTER_FIELD(p, type);
1475 p->mbus_code = mbus_code;
1478 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1479 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1480 cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1481 V4L2_CAP_VIDEO_M2M_MPLANE;
1482 if (!!(vdev->device_caps & cap_mask) !=
1483 (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1486 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1488 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1490 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1491 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1493 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1495 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1496 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1497 cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1498 V4L2_CAP_VIDEO_M2M_MPLANE;
1499 if (!!(vdev->device_caps & cap_mask) !=
1500 (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1503 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1505 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1507 case V4L2_BUF_TYPE_SDR_CAPTURE:
1508 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1510 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1512 case V4L2_BUF_TYPE_SDR_OUTPUT:
1513 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1515 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1517 case V4L2_BUF_TYPE_META_CAPTURE:
1518 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1520 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1522 case V4L2_BUF_TYPE_META_OUTPUT:
1523 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1525 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1529 v4l_fill_fmtdesc(p);
1533 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1536 * The v4l2_pix_format structure contains fields that make no sense for
1537 * touch. Set them to default values in this case.
1540 p->field = V4L2_FIELD_NONE;
1541 p->colorspace = V4L2_COLORSPACE_RAW;
1544 p->quantization = 0;
1548 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1549 struct file *file, void *fh, void *arg)
1551 struct v4l2_format *p = arg;
1552 struct video_device *vfd = video_devdata(file);
1553 int ret = check_fmt(file, p->type);
1559 * fmt can't be cleared for these overlay types due to the 'clips'
1560 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1561 * Those are provided by the user. So handle these two overlay types
1562 * first, and then just do a simple memset for the other types.
1565 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1566 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1567 struct v4l2_clip *clips = p->fmt.win.clips;
1568 u32 clipcount = p->fmt.win.clipcount;
1569 void __user *bitmap = p->fmt.win.bitmap;
1571 memset(&p->fmt, 0, sizeof(p->fmt));
1572 p->fmt.win.clips = clips;
1573 p->fmt.win.clipcount = clipcount;
1574 p->fmt.win.bitmap = bitmap;
1578 memset(&p->fmt, 0, sizeof(p->fmt));
1583 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1584 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1586 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1587 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1588 /* just in case the driver zeroed it again */
1589 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1590 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1591 v4l_pix_format_touch(&p->fmt.pix);
1593 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1594 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1595 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1596 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1597 case V4L2_BUF_TYPE_VBI_CAPTURE:
1598 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1599 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1600 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1601 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1602 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1604 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1605 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1606 /* just in case the driver zeroed it again */
1607 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1609 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1610 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1611 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1612 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1613 case V4L2_BUF_TYPE_VBI_OUTPUT:
1614 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1615 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1616 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1617 case V4L2_BUF_TYPE_SDR_CAPTURE:
1618 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1619 case V4L2_BUF_TYPE_SDR_OUTPUT:
1620 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1621 case V4L2_BUF_TYPE_META_CAPTURE:
1622 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1623 case V4L2_BUF_TYPE_META_OUTPUT:
1624 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1629 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1630 struct file *file, void *fh, void *arg)
1632 struct v4l2_format *p = arg;
1633 struct video_device *vfd = video_devdata(file);
1634 int ret = check_fmt(file, p->type);
1640 ret = v4l_enable_media_source(vfd);
1643 v4l_sanitize_format(p);
1646 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1647 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1649 CLEAR_AFTER_FIELD(p, fmt.pix);
1650 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1651 /* just in case the driver zeroed it again */
1652 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1653 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1654 v4l_pix_format_touch(&p->fmt.pix);
1656 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1657 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1659 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1660 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1661 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1663 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1664 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1665 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1667 CLEAR_AFTER_FIELD(p, fmt.win);
1668 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1669 case V4L2_BUF_TYPE_VBI_CAPTURE:
1670 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1672 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1673 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1674 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1675 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1677 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1678 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1679 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1680 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1682 CLEAR_AFTER_FIELD(p, fmt.pix);
1683 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1684 /* just in case the driver zeroed it again */
1685 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1687 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1688 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1690 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1691 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1692 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1694 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1695 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1696 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1698 CLEAR_AFTER_FIELD(p, fmt.win);
1699 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1700 case V4L2_BUF_TYPE_VBI_OUTPUT:
1701 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1703 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1704 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1705 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1706 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1708 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1709 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1710 case V4L2_BUF_TYPE_SDR_CAPTURE:
1711 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1713 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1714 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1715 case V4L2_BUF_TYPE_SDR_OUTPUT:
1716 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1718 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1719 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1720 case V4L2_BUF_TYPE_META_CAPTURE:
1721 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1723 CLEAR_AFTER_FIELD(p, fmt.meta);
1724 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1725 case V4L2_BUF_TYPE_META_OUTPUT:
1726 if (unlikely(!ops->vidioc_s_fmt_meta_out))
1728 CLEAR_AFTER_FIELD(p, fmt.meta);
1729 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1734 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1735 struct file *file, void *fh, void *arg)
1737 struct v4l2_format *p = arg;
1738 struct video_device *vfd = video_devdata(file);
1739 int ret = check_fmt(file, p->type);
1745 v4l_sanitize_format(p);
1748 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1749 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1751 CLEAR_AFTER_FIELD(p, fmt.pix);
1752 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1753 /* just in case the driver zeroed it again */
1754 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1755 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1756 v4l_pix_format_touch(&p->fmt.pix);
1758 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1759 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1761 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1762 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1763 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1765 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1766 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1767 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1769 CLEAR_AFTER_FIELD(p, fmt.win);
1770 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1771 case V4L2_BUF_TYPE_VBI_CAPTURE:
1772 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1774 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1775 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1776 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1777 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1779 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1780 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1781 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1782 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1784 CLEAR_AFTER_FIELD(p, fmt.pix);
1785 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1786 /* just in case the driver zeroed it again */
1787 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1789 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1790 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1792 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1793 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1794 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1796 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1797 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1798 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1800 CLEAR_AFTER_FIELD(p, fmt.win);
1801 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1802 case V4L2_BUF_TYPE_VBI_OUTPUT:
1803 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1805 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1806 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1807 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1808 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1810 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1811 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1812 case V4L2_BUF_TYPE_SDR_CAPTURE:
1813 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1815 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1816 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1817 case V4L2_BUF_TYPE_SDR_OUTPUT:
1818 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1820 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1821 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1822 case V4L2_BUF_TYPE_META_CAPTURE:
1823 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1825 CLEAR_AFTER_FIELD(p, fmt.meta);
1826 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1827 case V4L2_BUF_TYPE_META_OUTPUT:
1828 if (unlikely(!ops->vidioc_try_fmt_meta_out))
1830 CLEAR_AFTER_FIELD(p, fmt.meta);
1831 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1836 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1837 struct file *file, void *fh, void *arg)
1839 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1842 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1843 struct file *file, void *fh, void *arg)
1845 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1848 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1849 struct file *file, void *fh, void *arg)
1851 struct video_device *vfd = video_devdata(file);
1852 struct v4l2_tuner *p = arg;
1855 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1856 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1857 err = ops->vidioc_g_tuner(file, fh, p);
1859 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1863 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1864 struct file *file, void *fh, void *arg)
1866 struct video_device *vfd = video_devdata(file);
1867 struct v4l2_tuner *p = arg;
1870 ret = v4l_enable_media_source(vfd);
1873 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1874 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1875 return ops->vidioc_s_tuner(file, fh, p);
1878 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1879 struct file *file, void *fh, void *arg)
1881 struct video_device *vfd = video_devdata(file);
1882 struct v4l2_modulator *p = arg;
1885 if (vfd->vfl_type == VFL_TYPE_RADIO)
1886 p->type = V4L2_TUNER_RADIO;
1888 err = ops->vidioc_g_modulator(file, fh, p);
1890 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1894 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1895 struct file *file, void *fh, void *arg)
1897 struct video_device *vfd = video_devdata(file);
1898 struct v4l2_modulator *p = arg;
1900 if (vfd->vfl_type == VFL_TYPE_RADIO)
1901 p->type = V4L2_TUNER_RADIO;
1903 return ops->vidioc_s_modulator(file, fh, p);
1906 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1907 struct file *file, void *fh, void *arg)
1909 struct video_device *vfd = video_devdata(file);
1910 struct v4l2_frequency *p = arg;
1912 if (vfd->vfl_type == VFL_TYPE_SDR)
1913 p->type = V4L2_TUNER_SDR;
1915 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1916 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1917 return ops->vidioc_g_frequency(file, fh, p);
1920 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1921 struct file *file, void *fh, void *arg)
1923 struct video_device *vfd = video_devdata(file);
1924 const struct v4l2_frequency *p = arg;
1925 enum v4l2_tuner_type type;
1928 ret = v4l_enable_media_source(vfd);
1931 if (vfd->vfl_type == VFL_TYPE_SDR) {
1932 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1935 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1936 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1937 if (type != p->type)
1940 return ops->vidioc_s_frequency(file, fh, p);
1943 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1944 struct file *file, void *fh, void *arg)
1946 struct video_device *vfd = video_devdata(file);
1947 struct v4l2_standard *p = arg;
1949 return v4l_video_std_enumstd(p, vfd->tvnorms);
1952 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1953 struct file *file, void *fh, void *arg)
1955 struct video_device *vfd = video_devdata(file);
1956 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1959 ret = v4l_enable_media_source(vfd);
1962 norm = id & vfd->tvnorms;
1963 if (vfd->tvnorms && !norm) /* Check if std is supported */
1966 /* Calls the specific handler */
1967 return ops->vidioc_s_std(file, fh, norm);
1970 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1971 struct file *file, void *fh, void *arg)
1973 struct video_device *vfd = video_devdata(file);
1974 v4l2_std_id *p = arg;
1977 ret = v4l_enable_media_source(vfd);
1981 * If no signal is detected, then the driver should return
1982 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1983 * any standards that do not apply removed.
1985 * This means that tuners, audio and video decoders can join
1986 * their efforts to improve the standards detection.
1989 return ops->vidioc_querystd(file, fh, arg);
1992 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1993 struct file *file, void *fh, void *arg)
1995 struct video_device *vfd = video_devdata(file);
1996 struct v4l2_hw_freq_seek *p = arg;
1997 enum v4l2_tuner_type type;
2000 ret = v4l_enable_media_source(vfd);
2003 /* s_hw_freq_seek is not supported for SDR for now */
2004 if (vfd->vfl_type == VFL_TYPE_SDR)
2007 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2008 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2009 if (p->type != type)
2011 return ops->vidioc_s_hw_freq_seek(file, fh, p);
2014 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
2015 struct file *file, void *fh, void *arg)
2017 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
2020 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
2021 struct file *file, void *fh, void *arg)
2023 struct v4l2_requestbuffers *p = arg;
2024 int ret = check_fmt(file, p->type);
2029 CLEAR_AFTER_FIELD(p, capabilities);
2031 return ops->vidioc_reqbufs(file, fh, p);
2034 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
2035 struct file *file, void *fh, void *arg)
2037 struct v4l2_buffer *p = arg;
2038 int ret = check_fmt(file, p->type);
2040 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
2043 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2044 struct file *file, void *fh, void *arg)
2046 struct v4l2_buffer *p = arg;
2047 int ret = check_fmt(file, p->type);
2049 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2052 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2053 struct file *file, void *fh, void *arg)
2055 struct v4l2_buffer *p = arg;
2056 int ret = check_fmt(file, p->type);
2058 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2061 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2062 struct file *file, void *fh, void *arg)
2064 struct v4l2_create_buffers *create = arg;
2065 int ret = check_fmt(file, create->format.type);
2070 CLEAR_AFTER_FIELD(create, capabilities);
2072 v4l_sanitize_format(&create->format);
2074 ret = ops->vidioc_create_bufs(file, fh, create);
2076 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2077 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2078 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2083 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2084 struct file *file, void *fh, void *arg)
2086 struct v4l2_buffer *b = arg;
2087 int ret = check_fmt(file, b->type);
2089 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2092 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2093 struct file *file, void *fh, void *arg)
2095 struct video_device *vfd = video_devdata(file);
2096 struct v4l2_streamparm *p = arg;
2098 int ret = check_fmt(file, p->type);
2102 if (ops->vidioc_g_parm)
2103 return ops->vidioc_g_parm(file, fh, p);
2104 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2105 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2107 if (vfd->device_caps & V4L2_CAP_READWRITE)
2108 p->parm.capture.readbuffers = 2;
2109 ret = ops->vidioc_g_std(file, fh, &std);
2111 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2115 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2116 struct file *file, void *fh, void *arg)
2118 struct v4l2_streamparm *p = arg;
2119 int ret = check_fmt(file, p->type);
2124 /* Note: extendedmode is never used in drivers */
2125 if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2126 memset(p->parm.output.reserved, 0,
2127 sizeof(p->parm.output.reserved));
2128 p->parm.output.extendedmode = 0;
2129 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2131 memset(p->parm.capture.reserved, 0,
2132 sizeof(p->parm.capture.reserved));
2133 p->parm.capture.extendedmode = 0;
2134 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2136 return ops->vidioc_s_parm(file, fh, p);
2139 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2140 struct file *file, void *fh, void *arg)
2142 struct video_device *vfd = video_devdata(file);
2143 struct v4l2_queryctrl *p = arg;
2144 struct v4l2_fh *vfh =
2145 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2147 if (vfh && vfh->ctrl_handler)
2148 return v4l2_queryctrl(vfh->ctrl_handler, p);
2149 if (vfd->ctrl_handler)
2150 return v4l2_queryctrl(vfd->ctrl_handler, p);
2151 if (ops->vidioc_queryctrl)
2152 return ops->vidioc_queryctrl(file, fh, p);
2156 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2157 struct file *file, void *fh, void *arg)
2159 struct video_device *vfd = video_devdata(file);
2160 struct v4l2_query_ext_ctrl *p = arg;
2161 struct v4l2_fh *vfh =
2162 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2164 if (vfh && vfh->ctrl_handler)
2165 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2166 if (vfd->ctrl_handler)
2167 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2168 if (ops->vidioc_query_ext_ctrl)
2169 return ops->vidioc_query_ext_ctrl(file, fh, p);
2173 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2174 struct file *file, void *fh, void *arg)
2176 struct video_device *vfd = video_devdata(file);
2177 struct v4l2_querymenu *p = arg;
2178 struct v4l2_fh *vfh =
2179 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2181 if (vfh && vfh->ctrl_handler)
2182 return v4l2_querymenu(vfh->ctrl_handler, p);
2183 if (vfd->ctrl_handler)
2184 return v4l2_querymenu(vfd->ctrl_handler, p);
2185 if (ops->vidioc_querymenu)
2186 return ops->vidioc_querymenu(file, fh, p);
2190 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2191 struct file *file, void *fh, void *arg)
2193 struct video_device *vfd = video_devdata(file);
2194 struct v4l2_control *p = arg;
2195 struct v4l2_fh *vfh =
2196 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2197 struct v4l2_ext_controls ctrls;
2198 struct v4l2_ext_control ctrl;
2200 if (vfh && vfh->ctrl_handler)
2201 return v4l2_g_ctrl(vfh->ctrl_handler, p);
2202 if (vfd->ctrl_handler)
2203 return v4l2_g_ctrl(vfd->ctrl_handler, p);
2204 if (ops->vidioc_g_ctrl)
2205 return ops->vidioc_g_ctrl(file, fh, p);
2206 if (ops->vidioc_g_ext_ctrls == NULL)
2209 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2211 ctrls.controls = &ctrl;
2213 ctrl.value = p->value;
2214 if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
2215 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2218 p->value = ctrl.value;
2224 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2225 struct file *file, void *fh, void *arg)
2227 struct video_device *vfd = video_devdata(file);
2228 struct v4l2_control *p = arg;
2229 struct v4l2_fh *vfh =
2230 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2231 struct v4l2_ext_controls ctrls;
2232 struct v4l2_ext_control ctrl;
2235 if (vfh && vfh->ctrl_handler)
2236 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2237 if (vfd->ctrl_handler)
2238 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2239 if (ops->vidioc_s_ctrl)
2240 return ops->vidioc_s_ctrl(file, fh, p);
2241 if (ops->vidioc_s_ext_ctrls == NULL)
2244 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2246 ctrls.controls = &ctrl;
2248 ctrl.value = p->value;
2249 if (!check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
2251 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2252 p->value = ctrl.value;
2256 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2257 struct file *file, void *fh, void *arg)
2259 struct video_device *vfd = video_devdata(file);
2260 struct v4l2_ext_controls *p = arg;
2261 struct v4l2_fh *vfh =
2262 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2264 p->error_idx = p->count;
2265 if (vfh && vfh->ctrl_handler)
2266 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2267 vfd, vfd->v4l2_dev->mdev, p);
2268 if (vfd->ctrl_handler)
2269 return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2270 vfd, vfd->v4l2_dev->mdev, p);
2271 if (ops->vidioc_g_ext_ctrls == NULL)
2273 return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
2274 ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
2277 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2278 struct file *file, void *fh, void *arg)
2280 struct video_device *vfd = video_devdata(file);
2281 struct v4l2_ext_controls *p = arg;
2282 struct v4l2_fh *vfh =
2283 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2285 p->error_idx = p->count;
2286 if (vfh && vfh->ctrl_handler)
2287 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2288 vfd, vfd->v4l2_dev->mdev, p);
2289 if (vfd->ctrl_handler)
2290 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2291 vfd, vfd->v4l2_dev->mdev, p);
2292 if (ops->vidioc_s_ext_ctrls == NULL)
2294 return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
2295 ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
2298 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2299 struct file *file, void *fh, void *arg)
2301 struct video_device *vfd = video_devdata(file);
2302 struct v4l2_ext_controls *p = arg;
2303 struct v4l2_fh *vfh =
2304 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2306 p->error_idx = p->count;
2307 if (vfh && vfh->ctrl_handler)
2308 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2309 vfd, vfd->v4l2_dev->mdev, p);
2310 if (vfd->ctrl_handler)
2311 return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2312 vfd, vfd->v4l2_dev->mdev, p);
2313 if (ops->vidioc_try_ext_ctrls == NULL)
2315 return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
2316 ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
2320 * The selection API specified originally that the _MPLANE buffer types
2321 * shouldn't be used. The reasons for this are lost in the mists of time
2322 * (or just really crappy memories). Regardless, this is really annoying
2323 * for userspace. So to keep things simple we map _MPLANE buffer types
2324 * to their 'regular' counterparts before calling the driver. And we
2325 * restore it afterwards. This way applications can use either buffer
2326 * type and drivers don't need to check for both.
2328 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2329 struct file *file, void *fh, void *arg)
2331 struct v4l2_selection *p = arg;
2332 u32 old_type = p->type;
2335 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2336 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2337 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2338 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2339 ret = ops->vidioc_g_selection(file, fh, p);
2344 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2345 struct file *file, void *fh, void *arg)
2347 struct v4l2_selection *p = arg;
2348 u32 old_type = p->type;
2351 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2352 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2353 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2354 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2355 ret = ops->vidioc_s_selection(file, fh, p);
2360 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2361 struct file *file, void *fh, void *arg)
2363 struct video_device *vfd = video_devdata(file);
2364 struct v4l2_crop *p = arg;
2365 struct v4l2_selection s = {
2370 /* simulate capture crop using selection api */
2372 /* crop means compose for output devices */
2373 if (V4L2_TYPE_IS_OUTPUT(p->type))
2374 s.target = V4L2_SEL_TGT_COMPOSE;
2376 s.target = V4L2_SEL_TGT_CROP;
2378 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2379 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2380 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2382 ret = v4l_g_selection(ops, file, fh, &s);
2384 /* copying results to old structure on success */
2390 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2391 struct file *file, void *fh, void *arg)
2393 struct video_device *vfd = video_devdata(file);
2394 struct v4l2_crop *p = arg;
2395 struct v4l2_selection s = {
2400 /* simulate capture crop using selection api */
2402 /* crop means compose for output devices */
2403 if (V4L2_TYPE_IS_OUTPUT(p->type))
2404 s.target = V4L2_SEL_TGT_COMPOSE;
2406 s.target = V4L2_SEL_TGT_CROP;
2408 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2409 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2410 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2412 return v4l_s_selection(ops, file, fh, &s);
2415 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2416 struct file *file, void *fh, void *arg)
2418 struct video_device *vfd = video_devdata(file);
2419 struct v4l2_cropcap *p = arg;
2420 struct v4l2_selection s = { .type = p->type };
2423 /* setting trivial pixelaspect */
2424 p->pixelaspect.numerator = 1;
2425 p->pixelaspect.denominator = 1;
2427 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2428 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2429 else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2430 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2433 * The determine_valid_ioctls() call already should ensure
2434 * that this can never happen, but just in case...
2436 if (WARN_ON(!ops->vidioc_g_selection))
2439 if (ops->vidioc_g_pixelaspect)
2440 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2444 * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2445 * square pixel aspect ratio in that case.
2447 if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2450 /* Use g_selection() to fill in the bounds and defrect rectangles */
2452 /* obtaining bounds */
2453 if (V4L2_TYPE_IS_OUTPUT(p->type))
2454 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2456 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2458 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2459 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2460 V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2462 ret = v4l_g_selection(ops, file, fh, &s);
2467 /* obtaining defrect */
2468 if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2469 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2471 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2473 ret = v4l_g_selection(ops, file, fh, &s);
2481 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2482 struct file *file, void *fh, void *arg)
2484 struct video_device *vfd = video_devdata(file);
2488 pr_info("%s: ================= START STATUS =================\n",
2489 vfd->v4l2_dev->name);
2490 ret = ops->vidioc_log_status(file, fh);
2492 pr_info("%s: ================== END STATUS ==================\n",
2493 vfd->v4l2_dev->name);
2497 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2498 struct file *file, void *fh, void *arg)
2500 #ifdef CONFIG_VIDEO_ADV_DEBUG
2501 struct v4l2_dbg_register *p = arg;
2502 struct video_device *vfd = video_devdata(file);
2503 struct v4l2_subdev *sd;
2506 if (!capable(CAP_SYS_ADMIN))
2508 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2509 if (vfd->v4l2_dev == NULL)
2511 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2512 if (p->match.addr == idx++)
2513 return v4l2_subdev_call(sd, core, g_register, p);
2516 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2517 (ops->vidioc_g_chip_info || p->match.addr == 0))
2518 return ops->vidioc_g_register(file, fh, p);
2525 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2526 struct file *file, void *fh, void *arg)
2528 #ifdef CONFIG_VIDEO_ADV_DEBUG
2529 const struct v4l2_dbg_register *p = arg;
2530 struct video_device *vfd = video_devdata(file);
2531 struct v4l2_subdev *sd;
2534 if (!capable(CAP_SYS_ADMIN))
2536 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2537 if (vfd->v4l2_dev == NULL)
2539 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2540 if (p->match.addr == idx++)
2541 return v4l2_subdev_call(sd, core, s_register, p);
2544 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2545 (ops->vidioc_g_chip_info || p->match.addr == 0))
2546 return ops->vidioc_s_register(file, fh, p);
2553 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2554 struct file *file, void *fh, void *arg)
2556 #ifdef CONFIG_VIDEO_ADV_DEBUG
2557 struct video_device *vfd = video_devdata(file);
2558 struct v4l2_dbg_chip_info *p = arg;
2559 struct v4l2_subdev *sd;
2562 switch (p->match.type) {
2563 case V4L2_CHIP_MATCH_BRIDGE:
2564 if (ops->vidioc_s_register)
2565 p->flags |= V4L2_CHIP_FL_WRITABLE;
2566 if (ops->vidioc_g_register)
2567 p->flags |= V4L2_CHIP_FL_READABLE;
2568 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2569 if (ops->vidioc_g_chip_info)
2570 return ops->vidioc_g_chip_info(file, fh, arg);
2575 case V4L2_CHIP_MATCH_SUBDEV:
2576 if (vfd->v4l2_dev == NULL)
2578 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2579 if (p->match.addr != idx++)
2581 if (sd->ops->core && sd->ops->core->s_register)
2582 p->flags |= V4L2_CHIP_FL_WRITABLE;
2583 if (sd->ops->core && sd->ops->core->g_register)
2584 p->flags |= V4L2_CHIP_FL_READABLE;
2585 strscpy(p->name, sd->name, sizeof(p->name));
2596 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2597 struct file *file, void *fh, void *arg)
2599 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2602 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2603 struct file *file, void *fh, void *arg)
2605 return ops->vidioc_subscribe_event(fh, arg);
2608 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2609 struct file *file, void *fh, void *arg)
2611 return ops->vidioc_unsubscribe_event(fh, arg);
2614 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2615 struct file *file, void *fh, void *arg)
2617 struct v4l2_sliced_vbi_cap *p = arg;
2618 int ret = check_fmt(file, p->type);
2623 /* Clear up to type, everything after type is zeroed already */
2624 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2626 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2629 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2630 struct file *file, void *fh, void *arg)
2632 struct video_device *vfd = video_devdata(file);
2633 struct v4l2_frequency_band *p = arg;
2634 enum v4l2_tuner_type type;
2637 if (vfd->vfl_type == VFL_TYPE_SDR) {
2638 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2642 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2643 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2644 if (type != p->type)
2647 if (ops->vidioc_enum_freq_bands) {
2648 err = ops->vidioc_enum_freq_bands(file, fh, p);
2652 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2653 struct v4l2_tuner t = {
2660 err = ops->vidioc_g_tuner(file, fh, &t);
2663 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2664 p->rangelow = t.rangelow;
2665 p->rangehigh = t.rangehigh;
2666 p->modulation = (type == V4L2_TUNER_RADIO) ?
2667 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2670 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2671 struct v4l2_modulator m = {
2675 if (type != V4L2_TUNER_RADIO)
2679 err = ops->vidioc_g_modulator(file, fh, &m);
2682 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2683 p->rangelow = m.rangelow;
2684 p->rangehigh = m.rangehigh;
2685 p->modulation = (type == V4L2_TUNER_RADIO) ?
2686 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2692 struct v4l2_ioctl_info {
2695 const char * const name;
2696 int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2698 void (*debug)(const void *arg, bool write_only);
2701 /* This control needs a priority check */
2702 #define INFO_FL_PRIO (1 << 0)
2703 /* This control can be valid if the filehandle passes a control handler. */
2704 #define INFO_FL_CTRL (1 << 1)
2706 #define INFO_FL_QUEUE (1 << 2)
2707 /* Always copy back result, even on error */
2708 #define INFO_FL_ALWAYS_COPY (1 << 3)
2709 /* Zero struct from after the field to the end */
2710 #define INFO_FL_CLEAR(v4l2_struct, field) \
2711 ((offsetof(struct v4l2_struct, field) + \
2712 sizeof_field(struct v4l2_struct, field)) << 16)
2713 #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2715 #define DEFINE_V4L_STUB_FUNC(_vidioc) \
2716 static int v4l_stub_ ## _vidioc( \
2717 const struct v4l2_ioctl_ops *ops, \
2718 struct file *file, void *fh, void *p) \
2720 return ops->vidioc_ ## _vidioc(file, fh, p); \
2723 #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \
2724 [_IOC_NR(_ioctl)] = { \
2732 DEFINE_V4L_STUB_FUNC(g_fbuf)
2733 DEFINE_V4L_STUB_FUNC(s_fbuf)
2734 DEFINE_V4L_STUB_FUNC(expbuf)
2735 DEFINE_V4L_STUB_FUNC(g_std)
2736 DEFINE_V4L_STUB_FUNC(g_audio)
2737 DEFINE_V4L_STUB_FUNC(s_audio)
2738 DEFINE_V4L_STUB_FUNC(g_edid)
2739 DEFINE_V4L_STUB_FUNC(s_edid)
2740 DEFINE_V4L_STUB_FUNC(g_audout)
2741 DEFINE_V4L_STUB_FUNC(s_audout)
2742 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2743 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2744 DEFINE_V4L_STUB_FUNC(enumaudio)
2745 DEFINE_V4L_STUB_FUNC(enumaudout)
2746 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2747 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2748 DEFINE_V4L_STUB_FUNC(g_enc_index)
2749 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2750 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2751 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2752 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2753 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2754 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2755 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2756 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2757 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2759 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2760 IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2761 IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
2762 IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2763 IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2764 IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2765 IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2766 IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2767 IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2768 IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2769 IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2770 IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2771 IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2772 IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2773 IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2774 IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2775 IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2776 IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2777 IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2778 IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2779 IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2780 IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2781 IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2782 IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2783 IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2784 IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2785 IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2786 IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2787 IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2788 IOCTL_INFO(VIDIOC_G_INPUT, v4l_g_input, v4l_print_u32, 0),
2789 IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2790 IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2791 IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2792 IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_g_output, v4l_print_u32, 0),
2793 IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2794 IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2795 IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2796 IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2797 IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2798 IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2799 IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2800 IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2801 IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2802 IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2803 IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2804 IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2805 IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2806 IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2807 IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2808 IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2809 IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2810 IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2811 IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2812 IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2813 IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2814 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
2815 IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2816 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2817 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2818 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2819 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2820 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2821 IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2822 IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2823 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2824 IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2825 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2826 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2827 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2828 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2829 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
2830 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2831 IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2832 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2833 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2834 IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2835 IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2836 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2837 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2838 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2839 IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2840 IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2841 IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
2843 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2845 static bool v4l2_is_known_ioctl(unsigned int cmd)
2847 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2849 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2852 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2853 struct v4l2_fh *vfh, unsigned int cmd,
2856 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2858 if (vfh && vfh->m2m_ctx &&
2859 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2860 if (vfh->m2m_ctx->q_lock)
2861 return vfh->m2m_ctx->q_lock;
2863 if (vdev->queue && vdev->queue->lock &&
2864 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2865 return vdev->queue->lock;
2869 /* Common ioctl debug function. This function can be used by
2870 external ioctl messages as well as internal V4L ioctl */
2871 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2873 const char *dir, *type;
2876 printk(KERN_DEBUG "%s: ", prefix);
2878 switch (_IOC_TYPE(cmd)) {
2883 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2887 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2894 switch (_IOC_DIR(cmd)) {
2895 case _IOC_NONE: dir = "--"; break;
2896 case _IOC_READ: dir = "r-"; break;
2897 case _IOC_WRITE: dir = "-w"; break;
2898 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2899 default: dir = "*ERR*"; break;
2901 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2902 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2904 EXPORT_SYMBOL(v4l_printk_ioctl);
2906 static long __video_do_ioctl(struct file *file,
2907 unsigned int cmd, void *arg)
2909 struct video_device *vfd = video_devdata(file);
2910 struct mutex *req_queue_lock = NULL;
2911 struct mutex *lock; /* ioctl serialization mutex */
2912 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2913 bool write_only = false;
2914 struct v4l2_ioctl_info default_info;
2915 const struct v4l2_ioctl_info *info;
2916 void *fh = file->private_data;
2917 struct v4l2_fh *vfh = NULL;
2918 int dev_debug = vfd->dev_debug;
2922 pr_warn("%s: has no ioctl_ops.\n",
2923 video_device_node_name(vfd));
2927 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2928 vfh = file->private_data;
2931 * We need to serialize streamon/off with queueing new requests.
2932 * These ioctls may trigger the cancellation of a streaming
2933 * operation, and that should not be mixed with queueing a new
2934 * request at the same time.
2936 if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2937 (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2938 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2940 if (mutex_lock_interruptible(req_queue_lock))
2941 return -ERESTARTSYS;
2944 lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2946 if (lock && mutex_lock_interruptible(lock)) {
2948 mutex_unlock(req_queue_lock);
2949 return -ERESTARTSYS;
2952 if (!video_is_registered(vfd)) {
2957 if (v4l2_is_known_ioctl(cmd)) {
2958 info = &v4l2_ioctls[_IOC_NR(cmd)];
2960 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2961 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2964 if (vfh && (info->flags & INFO_FL_PRIO)) {
2965 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2970 default_info.ioctl = cmd;
2971 default_info.flags = 0;
2972 default_info.debug = v4l_print_default;
2973 info = &default_info;
2976 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2977 if (info != &default_info) {
2978 ret = info->func(ops, file, fh, arg);
2979 } else if (!ops->vidioc_default) {
2982 ret = ops->vidioc_default(file, fh,
2983 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2988 if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
2989 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
2990 (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
2993 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2995 pr_cont(": error %ld", ret);
2996 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
2998 else if (_IOC_DIR(cmd) == _IOC_NONE)
2999 info->debug(arg, write_only);
3002 info->debug(arg, write_only);
3010 mutex_unlock(req_queue_lock);
3014 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
3015 void __user **user_ptr, void ***kernel_ptr)
3020 case VIDIOC_PREPARE_BUF:
3021 case VIDIOC_QUERYBUF:
3023 case VIDIOC_DQBUF: {
3024 struct v4l2_buffer *buf = parg;
3026 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
3027 if (buf->length > VIDEO_MAX_PLANES) {
3031 *user_ptr = (void __user *)buf->m.planes;
3032 *kernel_ptr = (void **)&buf->m.planes;
3033 *array_size = sizeof(struct v4l2_plane) * buf->length;
3040 case VIDIOC_S_EDID: {
3041 struct v4l2_edid *edid = parg;
3044 if (edid->blocks > 256) {
3048 *user_ptr = (void __user *)edid->edid;
3049 *kernel_ptr = (void **)&edid->edid;
3050 *array_size = edid->blocks * 128;
3056 case VIDIOC_S_EXT_CTRLS:
3057 case VIDIOC_G_EXT_CTRLS:
3058 case VIDIOC_TRY_EXT_CTRLS: {
3059 struct v4l2_ext_controls *ctrls = parg;
3061 if (ctrls->count != 0) {
3062 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3066 *user_ptr = (void __user *)ctrls->controls;
3067 *kernel_ptr = (void **)&ctrls->controls;
3068 *array_size = sizeof(struct v4l2_ext_control)
3076 case VIDIOC_TRY_FMT: {
3077 struct v4l2_format *fmt = parg;
3079 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OVERLAY &&
3080 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY)
3082 if (fmt->fmt.win.clipcount > 2048)
3084 if (!fmt->fmt.win.clipcount)
3087 *user_ptr = (void __user *)fmt->fmt.win.clips;
3088 *kernel_ptr = (void **)&fmt->fmt.win.clips;
3089 *array_size = sizeof(struct v4l2_clip)
3090 * fmt->fmt.win.clipcount;
3100 static unsigned int video_translate_cmd(unsigned int cmd)
3102 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3104 case VIDIOC_DQEVENT_TIME32:
3105 return VIDIOC_DQEVENT;
3106 case VIDIOC_QUERYBUF_TIME32:
3107 return VIDIOC_QUERYBUF;
3108 case VIDIOC_QBUF_TIME32:
3110 case VIDIOC_DQBUF_TIME32:
3111 return VIDIOC_DQBUF;
3112 case VIDIOC_PREPARE_BUF_TIME32:
3113 return VIDIOC_PREPARE_BUF;
3116 if (in_compat_syscall())
3117 return v4l2_compat_translate_cmd(cmd);
3122 static int video_get_user(void __user *arg, void *parg,
3123 unsigned int real_cmd, unsigned int cmd,
3126 unsigned int n = _IOC_SIZE(real_cmd);
3129 if (!(_IOC_DIR(cmd) & _IOC_WRITE)) {
3130 /* read-only ioctl */
3136 * In some cases, only a few fields are used as input,
3137 * i.e. when the app sets "index" and then the driver
3138 * fills in the rest of the structure for the thing
3139 * with that index. We only need to copy up the first
3142 if (v4l2_is_known_ioctl(real_cmd)) {
3143 u32 flags = v4l2_ioctls[_IOC_NR(real_cmd)].flags;
3145 if (flags & INFO_FL_CLEAR_MASK)
3146 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3147 *always_copy = flags & INFO_FL_ALWAYS_COPY;
3150 if (cmd == real_cmd) {
3151 if (copy_from_user(parg, (void __user *)arg, n))
3153 } else if (in_compat_syscall()) {
3155 err = v4l2_compat_get_user(arg, parg, cmd);
3158 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3160 case VIDIOC_QUERYBUF_TIME32:
3161 case VIDIOC_QBUF_TIME32:
3162 case VIDIOC_DQBUF_TIME32:
3163 case VIDIOC_PREPARE_BUF_TIME32: {
3164 struct v4l2_buffer_time32 vb32;
3165 struct v4l2_buffer *vb = parg;
3167 if (copy_from_user(&vb32, arg, sizeof(vb32)))
3170 *vb = (struct v4l2_buffer) {
3171 .index = vb32.index,
3173 .bytesused = vb32.bytesused,
3174 .flags = vb32.flags,
3175 .field = vb32.field,
3176 .timestamp.tv_sec = vb32.timestamp.tv_sec,
3177 .timestamp.tv_usec = vb32.timestamp.tv_usec,
3178 .timecode = vb32.timecode,
3179 .sequence = vb32.sequence,
3180 .memory = vb32.memory,
3181 .m.userptr = vb32.m.userptr,
3182 .length = vb32.length,
3183 .request_fd = vb32.request_fd,
3191 /* zero out anything we don't copy from userspace */
3192 if (!err && n < _IOC_SIZE(real_cmd))
3193 memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
3197 static int video_put_user(void __user *arg, void *parg,
3198 unsigned int real_cmd, unsigned int cmd)
3200 if (!(_IOC_DIR(cmd) & _IOC_READ))
3203 if (cmd == real_cmd) {
3204 /* Copy results into user buffer */
3205 if (copy_to_user(arg, parg, _IOC_SIZE(cmd)))
3210 if (in_compat_syscall())
3211 return v4l2_compat_put_user(arg, parg, cmd);
3213 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3215 case VIDIOC_DQEVENT_TIME32: {
3216 struct v4l2_event *ev = parg;
3217 struct v4l2_event_time32 ev32;
3219 memset(&ev32, 0, sizeof(ev32));
3221 ev32.type = ev->type;
3222 ev32.pending = ev->pending;
3223 ev32.sequence = ev->sequence;
3224 ev32.timestamp.tv_sec = ev->timestamp.tv_sec;
3225 ev32.timestamp.tv_nsec = ev->timestamp.tv_nsec;
3228 memcpy(&ev32.u, &ev->u, sizeof(ev->u));
3229 memcpy(&ev32.reserved, &ev->reserved, sizeof(ev->reserved));
3231 if (copy_to_user(arg, &ev32, sizeof(ev32)))
3235 case VIDIOC_QUERYBUF_TIME32:
3236 case VIDIOC_QBUF_TIME32:
3237 case VIDIOC_DQBUF_TIME32:
3238 case VIDIOC_PREPARE_BUF_TIME32: {
3239 struct v4l2_buffer *vb = parg;
3240 struct v4l2_buffer_time32 vb32;
3242 memset(&vb32, 0, sizeof(vb32));
3244 vb32.index = vb->index;
3245 vb32.type = vb->type;
3246 vb32.bytesused = vb->bytesused;
3247 vb32.flags = vb->flags;
3248 vb32.field = vb->field;
3249 vb32.timestamp.tv_sec = vb->timestamp.tv_sec;
3250 vb32.timestamp.tv_usec = vb->timestamp.tv_usec;
3251 vb32.timecode = vb->timecode;
3252 vb32.sequence = vb->sequence;
3253 vb32.memory = vb->memory;
3254 vb32.m.userptr = vb->m.userptr;
3255 vb32.length = vb->length;
3256 vb32.request_fd = vb->request_fd;
3258 if (copy_to_user(arg, &vb32, sizeof(vb32)))
3269 video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
3273 void *mbuf = NULL, *array_buf = NULL;
3274 void *parg = (void *)arg;
3276 bool has_array_args;
3277 bool always_copy = false;
3278 size_t array_size = 0;
3279 void __user *user_ptr = NULL;
3280 void **kernel_ptr = NULL;
3281 unsigned int cmd = video_translate_cmd(orig_cmd);
3282 const size_t ioc_size = _IOC_SIZE(cmd);
3284 /* Copy arguments into temp kernel buffer */
3285 if (_IOC_DIR(cmd) != _IOC_NONE) {
3286 if (ioc_size <= sizeof(sbuf)) {
3289 /* too big to allocate from stack */
3290 mbuf = kmalloc(ioc_size, GFP_KERNEL);
3296 err = video_get_user((void __user *)arg, parg, cmd,
3297 orig_cmd, &always_copy);
3302 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3305 has_array_args = err;
3307 if (has_array_args) {
3308 array_buf = kvmalloc(array_size, GFP_KERNEL);
3310 if (array_buf == NULL)
3311 goto out_array_args;
3313 if (in_compat_syscall())
3314 err = v4l2_compat_get_array_args(file, array_buf,
3315 user_ptr, array_size,
3318 err = copy_from_user(array_buf, user_ptr, array_size) ?
3321 goto out_array_args;
3322 *kernel_ptr = array_buf;
3326 err = func(file, cmd, parg);
3327 if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3333 if (cmd == VIDIOC_DQBUF)
3334 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3335 else if (cmd == VIDIOC_QBUF)
3336 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3339 if (has_array_args) {
3340 *kernel_ptr = (void __force *)user_ptr;
3341 if (in_compat_syscall()) {
3344 put_err = v4l2_compat_put_array_args(file, user_ptr,
3350 } else if (copy_to_user(user_ptr, array_buf, array_size)) {
3353 goto out_array_args;
3356 * Some ioctls can return an error, but still have valid
3357 * results that must be returned.
3359 if (err < 0 && !always_copy)
3363 if (video_put_user((void __user *)arg, parg, cmd, orig_cmd))
3371 long video_ioctl2(struct file *file,
3372 unsigned int cmd, unsigned long arg)
3374 return video_usercopy(file, cmd, arg, __video_do_ioctl);
3376 EXPORT_SYMBOL(video_ioctl2);