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/media-device.h> /* for media_set_bus_info() */
22 #include <media/v4l2-common.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-device.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/v4l2-mc.h>
30 #include <media/v4l2-mem2mem.h>
32 #include <trace/events/v4l2.h>
34 /* Zero out the end of the struct pointed to by p. Everything after, but
35 * not including, the specified field is cleared. */
36 #define CLEAR_AFTER_FIELD(p, field) \
37 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
38 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
40 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
47 static const struct std_descr standards[] = {
48 { V4L2_STD_NTSC, "NTSC" },
49 { V4L2_STD_NTSC_M, "NTSC-M" },
50 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
51 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
52 { V4L2_STD_NTSC_443, "NTSC-443" },
53 { V4L2_STD_PAL, "PAL" },
54 { V4L2_STD_PAL_BG, "PAL-BG" },
55 { V4L2_STD_PAL_B, "PAL-B" },
56 { V4L2_STD_PAL_B1, "PAL-B1" },
57 { V4L2_STD_PAL_G, "PAL-G" },
58 { V4L2_STD_PAL_H, "PAL-H" },
59 { V4L2_STD_PAL_I, "PAL-I" },
60 { V4L2_STD_PAL_DK, "PAL-DK" },
61 { V4L2_STD_PAL_D, "PAL-D" },
62 { V4L2_STD_PAL_D1, "PAL-D1" },
63 { V4L2_STD_PAL_K, "PAL-K" },
64 { V4L2_STD_PAL_M, "PAL-M" },
65 { V4L2_STD_PAL_N, "PAL-N" },
66 { V4L2_STD_PAL_Nc, "PAL-Nc" },
67 { V4L2_STD_PAL_60, "PAL-60" },
68 { V4L2_STD_SECAM, "SECAM" },
69 { V4L2_STD_SECAM_B, "SECAM-B" },
70 { V4L2_STD_SECAM_G, "SECAM-G" },
71 { V4L2_STD_SECAM_H, "SECAM-H" },
72 { V4L2_STD_SECAM_DK, "SECAM-DK" },
73 { V4L2_STD_SECAM_D, "SECAM-D" },
74 { V4L2_STD_SECAM_K, "SECAM-K" },
75 { V4L2_STD_SECAM_K1, "SECAM-K1" },
76 { V4L2_STD_SECAM_L, "SECAM-L" },
77 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
81 /* video4linux standard ID conversion to standard name
83 const char *v4l2_norm_to_name(v4l2_std_id id)
88 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
89 64 bit comparisons. So, on that architecture, with some gcc
90 variants, compilation fails. Currently, the max value is 30bit wide.
94 for (i = 0; standards[i].std; i++)
95 if (myid == standards[i].std)
97 return standards[i].descr;
99 EXPORT_SYMBOL(v4l2_norm_to_name);
101 /* Returns frame period for the given standard */
102 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
104 if (id & V4L2_STD_525_60) {
105 frameperiod->numerator = 1001;
106 frameperiod->denominator = 30000;
108 frameperiod->numerator = 1;
109 frameperiod->denominator = 25;
112 EXPORT_SYMBOL(v4l2_video_std_frame_period);
114 /* Fill in the fields of a v4l2_standard structure according to the
115 'id' and 'transmission' parameters. Returns negative on error. */
116 int v4l2_video_std_construct(struct v4l2_standard *vs,
117 int id, const char *name)
120 v4l2_video_std_frame_period(id, &vs->frameperiod);
121 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
122 strscpy(vs->name, name, sizeof(vs->name));
125 EXPORT_SYMBOL(v4l2_video_std_construct);
127 /* Fill in the fields of a v4l2_standard structure according to the
128 * 'id' and 'vs->index' parameters. Returns negative on error. */
129 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
131 v4l2_std_id curr_id = 0;
132 unsigned int index = vs->index, i, j = 0;
133 const char *descr = "";
135 /* Return -ENODATA if the id for the current input
136 or output is 0, meaning that it doesn't support this API. */
140 /* Return norm array in a canonical way */
141 for (i = 0; i <= index && id; i++) {
142 /* last std value in the standards array is 0, so this
143 while always ends there since (id & 0) == 0. */
144 while ((id & standards[j].std) != standards[j].std)
146 curr_id = standards[j].std;
147 descr = standards[j].descr;
151 if (curr_id != V4L2_STD_PAL &&
152 curr_id != V4L2_STD_SECAM &&
153 curr_id != V4L2_STD_NTSC)
159 v4l2_video_std_construct(vs, curr_id, descr);
163 /* ----------------------------------------------------------------- */
164 /* some arrays for pretty-printing debug messages of enum types */
166 const char *v4l2_field_names[] = {
167 [V4L2_FIELD_ANY] = "any",
168 [V4L2_FIELD_NONE] = "none",
169 [V4L2_FIELD_TOP] = "top",
170 [V4L2_FIELD_BOTTOM] = "bottom",
171 [V4L2_FIELD_INTERLACED] = "interlaced",
172 [V4L2_FIELD_SEQ_TB] = "seq-tb",
173 [V4L2_FIELD_SEQ_BT] = "seq-bt",
174 [V4L2_FIELD_ALTERNATE] = "alternate",
175 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
176 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
178 EXPORT_SYMBOL(v4l2_field_names);
180 const char *v4l2_type_names[] = {
182 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
183 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
184 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
185 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
186 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
187 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
188 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
189 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
190 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
191 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
192 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
193 [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
194 [V4L2_BUF_TYPE_META_CAPTURE] = "meta-cap",
195 [V4L2_BUF_TYPE_META_OUTPUT] = "meta-out",
197 EXPORT_SYMBOL(v4l2_type_names);
199 static const char *v4l2_memory_names[] = {
200 [V4L2_MEMORY_MMAP] = "mmap",
201 [V4L2_MEMORY_USERPTR] = "userptr",
202 [V4L2_MEMORY_OVERLAY] = "overlay",
203 [V4L2_MEMORY_DMABUF] = "dmabuf",
206 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
208 /* ------------------------------------------------------------------ */
209 /* debug help functions */
211 static void v4l_print_querycap(const void *arg, bool write_only)
213 const struct v4l2_capability *p = arg;
215 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
216 (int)sizeof(p->driver), p->driver,
217 (int)sizeof(p->card), p->card,
218 (int)sizeof(p->bus_info), p->bus_info,
219 p->version, p->capabilities, p->device_caps);
222 static void v4l_print_enuminput(const void *arg, bool write_only)
224 const struct v4l2_input *p = arg;
226 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
227 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
228 p->tuner, (unsigned long long)p->std, p->status,
232 static void v4l_print_enumoutput(const void *arg, bool write_only)
234 const struct v4l2_output *p = arg;
236 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
237 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
238 p->modulator, (unsigned long long)p->std, p->capabilities);
241 static void v4l_print_audio(const void *arg, bool write_only)
243 const struct v4l2_audio *p = arg;
246 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
248 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
249 p->index, (int)sizeof(p->name), p->name,
250 p->capability, p->mode);
253 static void v4l_print_audioout(const void *arg, bool write_only)
255 const struct v4l2_audioout *p = arg;
258 pr_cont("index=%u\n", p->index);
260 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
261 p->index, (int)sizeof(p->name), p->name,
262 p->capability, p->mode);
265 static void v4l_print_fmtdesc(const void *arg, bool write_only)
267 const struct v4l2_fmtdesc *p = arg;
269 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%p4cc, mbus_code=0x%04x, description='%.*s'\n",
270 p->index, prt_names(p->type, v4l2_type_names),
271 p->flags, &p->pixelformat, p->mbus_code,
272 (int)sizeof(p->description), p->description);
275 static void v4l_print_format(const void *arg, bool write_only)
277 const struct v4l2_format *p = arg;
278 const struct v4l2_pix_format *pix;
279 const struct v4l2_pix_format_mplane *mp;
280 const struct v4l2_vbi_format *vbi;
281 const struct v4l2_sliced_vbi_format *sliced;
282 const struct v4l2_window *win;
283 const struct v4l2_meta_format *meta;
288 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
290 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
291 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
293 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",
294 pix->width, pix->height, &pix->pixelformat,
295 prt_names(pix->field, v4l2_field_names),
296 pix->bytesperline, pix->sizeimage,
297 pix->colorspace, pix->flags, pix->ycbcr_enc,
298 pix->quantization, pix->xfer_func);
300 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
301 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
303 pixelformat = mp->pixelformat;
304 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",
305 mp->width, mp->height, &pixelformat,
306 prt_names(mp->field, v4l2_field_names),
307 mp->colorspace, mp->num_planes, mp->flags,
308 mp->ycbcr_enc, mp->quantization, mp->xfer_func);
309 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
310 for (i = 0; i < planes; i++)
311 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
312 mp->plane_fmt[i].bytesperline,
313 mp->plane_fmt[i].sizeimage);
315 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
316 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
318 /* Note: we can't print the clip list here since the clips
319 * pointer is a userspace pointer, not a kernelspace
321 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",
322 win->w.width, win->w.height, win->w.left, win->w.top,
323 prt_names(win->field, v4l2_field_names),
324 win->chromakey, win->clipcount, win->clips,
325 win->bitmap, win->global_alpha);
327 case V4L2_BUF_TYPE_VBI_CAPTURE:
328 case V4L2_BUF_TYPE_VBI_OUTPUT:
330 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%p4cc, start=%u,%u, count=%u,%u\n",
331 vbi->sampling_rate, vbi->offset,
332 vbi->samples_per_line, &vbi->sample_format,
333 vbi->start[0], vbi->start[1],
334 vbi->count[0], vbi->count[1]);
336 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
337 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
338 sliced = &p->fmt.sliced;
339 pr_cont(", service_set=0x%08x, io_size=%d\n",
340 sliced->service_set, sliced->io_size);
341 for (i = 0; i < 24; i++)
342 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
343 sliced->service_lines[0][i],
344 sliced->service_lines[1][i]);
346 case V4L2_BUF_TYPE_SDR_CAPTURE:
347 case V4L2_BUF_TYPE_SDR_OUTPUT:
348 pixelformat = p->fmt.sdr.pixelformat;
349 pr_cont(", pixelformat=%p4cc\n", &pixelformat);
351 case V4L2_BUF_TYPE_META_CAPTURE:
352 case V4L2_BUF_TYPE_META_OUTPUT:
354 pixelformat = meta->dataformat;
355 pr_cont(", dataformat=%p4cc, buffersize=%u\n",
356 &pixelformat, meta->buffersize);
361 static void v4l_print_framebuffer(const void *arg, bool write_only)
363 const struct v4l2_framebuffer *p = arg;
365 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%p4cc, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
366 p->capability, p->flags, p->base, p->fmt.width, p->fmt.height,
367 &p->fmt.pixelformat, p->fmt.bytesperline, p->fmt.sizeimage,
371 static void v4l_print_buftype(const void *arg, bool write_only)
373 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
376 static void v4l_print_modulator(const void *arg, bool write_only)
378 const struct v4l2_modulator *p = arg;
381 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
383 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
384 p->index, (int)sizeof(p->name), p->name, p->capability,
385 p->rangelow, p->rangehigh, p->txsubchans);
388 static void v4l_print_tuner(const void *arg, bool write_only)
390 const struct v4l2_tuner *p = arg;
393 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
395 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",
396 p->index, (int)sizeof(p->name), p->name, p->type,
397 p->capability, p->rangelow,
398 p->rangehigh, p->signal, p->afc,
399 p->rxsubchans, p->audmode);
402 static void v4l_print_frequency(const void *arg, bool write_only)
404 const struct v4l2_frequency *p = arg;
406 pr_cont("tuner=%u, type=%u, frequency=%u\n",
407 p->tuner, p->type, p->frequency);
410 static void v4l_print_standard(const void *arg, bool write_only)
412 const struct v4l2_standard *p = arg;
414 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
416 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
417 p->frameperiod.numerator,
418 p->frameperiod.denominator,
422 static void v4l_print_std(const void *arg, bool write_only)
424 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
427 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
429 const struct v4l2_hw_freq_seek *p = arg;
431 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
432 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
433 p->rangelow, p->rangehigh);
436 static void v4l_print_requestbuffers(const void *arg, bool write_only)
438 const struct v4l2_requestbuffers *p = arg;
440 pr_cont("count=%d, type=%s, memory=%s\n",
442 prt_names(p->type, v4l2_type_names),
443 prt_names(p->memory, v4l2_memory_names));
446 static void v4l_print_buffer(const void *arg, bool write_only)
448 const struct v4l2_buffer *p = arg;
449 const struct v4l2_timecode *tc = &p->timecode;
450 const struct v4l2_plane *plane;
453 pr_cont("%02d:%02d:%02d.%06ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
454 (int)p->timestamp.tv_sec / 3600,
455 ((int)p->timestamp.tv_sec / 60) % 60,
456 ((int)p->timestamp.tv_sec % 60),
457 (long)p->timestamp.tv_usec,
459 prt_names(p->type, v4l2_type_names), p->request_fd,
460 p->flags, prt_names(p->field, v4l2_field_names),
461 p->sequence, prt_names(p->memory, v4l2_memory_names));
463 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
465 for (i = 0; i < p->length; ++i) {
466 plane = &p->m.planes[i];
468 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
469 i, plane->bytesused, plane->data_offset,
470 plane->m.userptr, plane->length);
473 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
474 p->bytesused, p->m.userptr, p->length);
477 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
478 tc->hours, tc->minutes, tc->seconds,
479 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
482 static void v4l_print_exportbuffer(const void *arg, bool write_only)
484 const struct v4l2_exportbuffer *p = arg;
486 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
487 p->fd, prt_names(p->type, v4l2_type_names),
488 p->index, p->plane, p->flags);
491 static void v4l_print_create_buffers(const void *arg, bool write_only)
493 const struct v4l2_create_buffers *p = arg;
495 pr_cont("index=%d, count=%d, memory=%s, capabilities=0x%08x, ",
496 p->index, p->count, prt_names(p->memory, v4l2_memory_names),
498 v4l_print_format(&p->format, write_only);
501 static void v4l_print_streamparm(const void *arg, bool write_only)
503 const struct v4l2_streamparm *p = arg;
505 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
507 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
508 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
509 const struct v4l2_captureparm *c = &p->parm.capture;
511 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
512 c->capability, c->capturemode,
513 c->timeperframe.numerator, c->timeperframe.denominator,
514 c->extendedmode, c->readbuffers);
515 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
516 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
517 const struct v4l2_outputparm *c = &p->parm.output;
519 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
520 c->capability, c->outputmode,
521 c->timeperframe.numerator, c->timeperframe.denominator,
522 c->extendedmode, c->writebuffers);
528 static void v4l_print_queryctrl(const void *arg, bool write_only)
530 const struct v4l2_queryctrl *p = arg;
532 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
533 p->id, p->type, (int)sizeof(p->name), p->name,
534 p->minimum, p->maximum,
535 p->step, p->default_value, p->flags);
538 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
540 const struct v4l2_query_ext_ctrl *p = arg;
542 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",
543 p->id, p->type, (int)sizeof(p->name), p->name,
544 p->minimum, p->maximum,
545 p->step, p->default_value, p->flags,
546 p->elem_size, p->elems, p->nr_of_dims,
547 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
550 static void v4l_print_querymenu(const void *arg, bool write_only)
552 const struct v4l2_querymenu *p = arg;
554 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
557 static void v4l_print_control(const void *arg, bool write_only)
559 const struct v4l2_control *p = arg;
560 const char *name = v4l2_ctrl_get_name(p->id);
563 pr_cont("name=%s, ", name);
564 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
567 static void v4l_print_ext_controls(const void *arg, bool write_only)
569 const struct v4l2_ext_controls *p = arg;
572 pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
573 p->which, p->count, p->error_idx, p->request_fd);
574 for (i = 0; i < p->count; i++) {
575 unsigned int id = p->controls[i].id;
576 const char *name = v4l2_ctrl_get_name(id);
579 pr_cont(", name=%s", name);
580 if (!p->controls[i].size)
581 pr_cont(", id/val=0x%x/0x%x", id, p->controls[i].value);
583 pr_cont(", id/size=0x%x/%u", id, p->controls[i].size);
588 static void v4l_print_cropcap(const void *arg, bool write_only)
590 const struct v4l2_cropcap *p = arg;
592 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
593 prt_names(p->type, v4l2_type_names),
594 p->bounds.width, p->bounds.height,
595 p->bounds.left, p->bounds.top,
596 p->defrect.width, p->defrect.height,
597 p->defrect.left, p->defrect.top,
598 p->pixelaspect.numerator, p->pixelaspect.denominator);
601 static void v4l_print_crop(const void *arg, bool write_only)
603 const struct v4l2_crop *p = arg;
605 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
606 prt_names(p->type, v4l2_type_names),
607 p->c.width, p->c.height,
608 p->c.left, p->c.top);
611 static void v4l_print_selection(const void *arg, bool write_only)
613 const struct v4l2_selection *p = arg;
615 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
616 prt_names(p->type, v4l2_type_names),
618 p->r.width, p->r.height, p->r.left, p->r.top);
621 static void v4l_print_jpegcompression(const void *arg, bool write_only)
623 const struct v4l2_jpegcompression *p = arg;
625 pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
626 p->quality, p->APPn, p->APP_len,
627 p->COM_len, p->jpeg_markers);
630 static void v4l_print_enc_idx(const void *arg, bool write_only)
632 const struct v4l2_enc_idx *p = arg;
634 pr_cont("entries=%d, entries_cap=%d\n",
635 p->entries, p->entries_cap);
638 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
640 const struct v4l2_encoder_cmd *p = arg;
642 pr_cont("cmd=%d, flags=0x%x\n",
646 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
648 const struct v4l2_decoder_cmd *p = arg;
650 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
652 if (p->cmd == V4L2_DEC_CMD_START)
653 pr_info("speed=%d, format=%u\n",
654 p->start.speed, p->start.format);
655 else if (p->cmd == V4L2_DEC_CMD_STOP)
656 pr_info("pts=%llu\n", p->stop.pts);
659 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
661 const struct v4l2_dbg_chip_info *p = arg;
663 pr_cont("type=%u, ", p->match.type);
664 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
665 pr_cont("name=%.*s, ",
666 (int)sizeof(p->match.name), p->match.name);
668 pr_cont("addr=%u, ", p->match.addr);
669 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
672 static void v4l_print_dbg_register(const void *arg, bool write_only)
674 const struct v4l2_dbg_register *p = arg;
676 pr_cont("type=%u, ", p->match.type);
677 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
678 pr_cont("name=%.*s, ",
679 (int)sizeof(p->match.name), p->match.name);
681 pr_cont("addr=%u, ", p->match.addr);
682 pr_cont("reg=0x%llx, val=0x%llx\n",
686 static void v4l_print_dv_timings(const void *arg, bool write_only)
688 const struct v4l2_dv_timings *p = arg;
691 case V4L2_DV_BT_656_1120:
692 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",
693 p->bt.interlaced, p->bt.pixelclock,
694 p->bt.width, p->bt.height,
695 p->bt.polarities, p->bt.hfrontporch,
696 p->bt.hsync, p->bt.hbackporch,
697 p->bt.vfrontporch, p->bt.vsync,
698 p->bt.vbackporch, p->bt.il_vfrontporch,
699 p->bt.il_vsync, p->bt.il_vbackporch,
700 p->bt.standards, p->bt.flags);
703 pr_cont("type=%d\n", p->type);
708 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
710 const struct v4l2_enum_dv_timings *p = arg;
712 pr_cont("index=%u, ", p->index);
713 v4l_print_dv_timings(&p->timings, write_only);
716 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
718 const struct v4l2_dv_timings_cap *p = arg;
721 case V4L2_DV_BT_656_1120:
722 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
723 p->bt.min_width, p->bt.max_width,
724 p->bt.min_height, p->bt.max_height,
725 p->bt.min_pixelclock, p->bt.max_pixelclock,
726 p->bt.standards, p->bt.capabilities);
729 pr_cont("type=%u\n", p->type);
734 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
736 const struct v4l2_frmsizeenum *p = arg;
738 pr_cont("index=%u, pixelformat=%p4cc, type=%u",
739 p->index, &p->pixel_format, p->type);
741 case V4L2_FRMSIZE_TYPE_DISCRETE:
742 pr_cont(", wxh=%ux%u\n",
743 p->discrete.width, p->discrete.height);
745 case V4L2_FRMSIZE_TYPE_STEPWISE:
746 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
747 p->stepwise.min_width,
748 p->stepwise.min_height,
749 p->stepwise.max_width,
750 p->stepwise.max_height,
751 p->stepwise.step_width,
752 p->stepwise.step_height);
754 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
761 static void v4l_print_frmivalenum(const void *arg, bool write_only)
763 const struct v4l2_frmivalenum *p = arg;
765 pr_cont("index=%u, pixelformat=%p4cc, wxh=%ux%u, type=%u",
766 p->index, &p->pixel_format, p->width, p->height, p->type);
768 case V4L2_FRMIVAL_TYPE_DISCRETE:
769 pr_cont(", fps=%d/%d\n",
770 p->discrete.numerator,
771 p->discrete.denominator);
773 case V4L2_FRMIVAL_TYPE_STEPWISE:
774 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
775 p->stepwise.min.numerator,
776 p->stepwise.min.denominator,
777 p->stepwise.max.numerator,
778 p->stepwise.max.denominator,
779 p->stepwise.step.numerator,
780 p->stepwise.step.denominator);
782 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
789 static void v4l_print_event(const void *arg, bool write_only)
791 const struct v4l2_event *p = arg;
792 const struct v4l2_event_ctrl *c;
794 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%llu.%9.9llu\n",
795 p->type, p->pending, p->sequence, p->id,
796 p->timestamp.tv_sec, p->timestamp.tv_nsec);
798 case V4L2_EVENT_VSYNC:
799 printk(KERN_DEBUG "field=%s\n",
800 prt_names(p->u.vsync.field, v4l2_field_names));
802 case V4L2_EVENT_CTRL:
804 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
805 c->changes, c->type);
806 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
807 pr_cont("value64=%lld, ", c->value64);
809 pr_cont("value=%d, ", c->value);
810 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
811 c->flags, c->minimum, c->maximum,
812 c->step, c->default_value);
814 case V4L2_EVENT_FRAME_SYNC:
815 pr_cont("frame_sequence=%u\n",
816 p->u.frame_sync.frame_sequence);
821 static void v4l_print_event_subscription(const void *arg, bool write_only)
823 const struct v4l2_event_subscription *p = arg;
825 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
826 p->type, p->id, p->flags);
829 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
831 const struct v4l2_sliced_vbi_cap *p = arg;
834 pr_cont("type=%s, service_set=0x%08x\n",
835 prt_names(p->type, v4l2_type_names), p->service_set);
836 for (i = 0; i < 24; i++)
837 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
838 p->service_lines[0][i],
839 p->service_lines[1][i]);
842 static void v4l_print_freq_band(const void *arg, bool write_only)
844 const struct v4l2_frequency_band *p = arg;
846 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
847 p->tuner, p->type, p->index,
848 p->capability, p->rangelow,
849 p->rangehigh, p->modulation);
852 static void v4l_print_edid(const void *arg, bool write_only)
854 const struct v4l2_edid *p = arg;
856 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
857 p->pad, p->start_block, p->blocks);
860 static void v4l_print_u32(const void *arg, bool write_only)
862 pr_cont("value=%u\n", *(const u32 *)arg);
865 static void v4l_print_newline(const void *arg, bool write_only)
870 static void v4l_print_default(const void *arg, bool write_only)
872 pr_cont("driver-specific ioctl\n");
875 static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
879 /* zero the reserved fields */
881 for (i = 0; i < c->count; i++)
882 c->controls[i].reserved2[0] = 0;
885 case V4L2_CID_PRIVATE_BASE:
887 * V4L2_CID_PRIVATE_BASE cannot be used as control class
888 * when using extended controls.
889 * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
890 * is it allowed for backwards compatibility.
892 if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
895 case V4L2_CTRL_WHICH_DEF_VAL:
896 /* Default value cannot be changed */
897 if (ioctl == VIDIOC_S_EXT_CTRLS ||
898 ioctl == VIDIOC_TRY_EXT_CTRLS) {
899 c->error_idx = c->count;
903 case V4L2_CTRL_WHICH_CUR_VAL:
905 case V4L2_CTRL_WHICH_REQUEST_VAL:
906 c->error_idx = c->count;
910 /* Check that all controls are from the same control class. */
911 for (i = 0; i < c->count; i++) {
912 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
913 c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
921 static int check_fmt(struct file *file, enum v4l2_buf_type type)
923 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
924 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
925 V4L2_CAP_VIDEO_OUTPUT |
926 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
927 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
928 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
929 V4L2_CAP_META_OUTPUT;
930 struct video_device *vfd = video_devdata(file);
931 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
932 bool is_vid = vfd->vfl_type == VFL_TYPE_VIDEO &&
933 (vfd->device_caps & vid_caps);
934 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
935 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
936 bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
937 bool is_meta = vfd->vfl_type == VFL_TYPE_VIDEO &&
938 (vfd->device_caps & meta_caps);
939 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
940 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
946 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
947 if ((is_vid || is_tch) && is_rx &&
948 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
951 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
952 if ((is_vid || is_tch) && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
955 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
956 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
959 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
960 if (is_vid && is_tx &&
961 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
964 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
965 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
968 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
969 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
972 case V4L2_BUF_TYPE_VBI_CAPTURE:
973 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
976 case V4L2_BUF_TYPE_VBI_OUTPUT:
977 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
980 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
981 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
984 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
985 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
988 case V4L2_BUF_TYPE_SDR_CAPTURE:
989 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
992 case V4L2_BUF_TYPE_SDR_OUTPUT:
993 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
996 case V4L2_BUF_TYPE_META_CAPTURE:
997 if (is_meta && is_rx && ops->vidioc_g_fmt_meta_cap)
1000 case V4L2_BUF_TYPE_META_OUTPUT:
1001 if (is_meta && is_tx && ops->vidioc_g_fmt_meta_out)
1010 static void v4l_sanitize_format(struct v4l2_format *fmt)
1012 unsigned int offset;
1014 /* Make sure num_planes is not bogus */
1015 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1016 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1017 fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1021 * The v4l2_pix_format structure has been extended with fields that were
1022 * not previously required to be set to zero by applications. The priv
1023 * field, when set to a magic value, indicates the the extended fields
1024 * are valid. Otherwise they will contain undefined values. To simplify
1025 * the API towards drivers zero the extended fields and set the priv
1026 * field to the magic value when the extended pixel format structure
1027 * isn't used by applications.
1030 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1031 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1034 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1037 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1039 offset = offsetof(struct v4l2_pix_format, priv)
1040 + sizeof(fmt->fmt.pix.priv);
1041 memset(((void *)&fmt->fmt.pix) + offset, 0,
1042 sizeof(fmt->fmt.pix) - offset);
1045 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1046 struct file *file, void *fh, void *arg)
1048 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1049 struct video_device *vfd = video_devdata(file);
1052 cap->version = LINUX_VERSION_CODE;
1053 cap->device_caps = vfd->device_caps;
1054 cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1056 media_set_bus_info(cap->bus_info, sizeof(cap->bus_info),
1059 ret = ops->vidioc_querycap(file, fh, cap);
1062 * Drivers must not change device_caps, so check for this and
1063 * warn if this happened.
1065 WARN_ON(cap->device_caps != vfd->device_caps);
1067 * Check that capabilities is a superset of
1068 * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1070 WARN_ON((cap->capabilities &
1071 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1072 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1073 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1074 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1079 static int v4l_g_input(const struct v4l2_ioctl_ops *ops,
1080 struct file *file, void *fh, void *arg)
1082 struct video_device *vfd = video_devdata(file);
1084 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1089 return ops->vidioc_g_input(file, fh, arg);
1092 static int v4l_g_output(const struct v4l2_ioctl_ops *ops,
1093 struct file *file, void *fh, void *arg)
1095 struct video_device *vfd = video_devdata(file);
1097 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1102 return ops->vidioc_g_output(file, fh, arg);
1105 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1106 struct file *file, void *fh, void *arg)
1108 struct video_device *vfd = video_devdata(file);
1111 ret = v4l_enable_media_source(vfd);
1115 if (vfd->device_caps & V4L2_CAP_IO_MC)
1116 return *(int *)arg ? -EINVAL : 0;
1118 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1121 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1122 struct file *file, void *fh, void *arg)
1124 struct video_device *vfd = video_devdata(file);
1126 if (vfd->device_caps & V4L2_CAP_IO_MC)
1127 return *(int *)arg ? -EINVAL : 0;
1129 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1132 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1133 struct file *file, void *fh, void *arg)
1135 struct video_device *vfd;
1138 vfd = video_devdata(file);
1139 *p = v4l2_prio_max(vfd->prio);
1143 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1144 struct file *file, void *fh, void *arg)
1146 struct video_device *vfd;
1147 struct v4l2_fh *vfh;
1150 vfd = video_devdata(file);
1151 if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1153 vfh = file->private_data;
1154 return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1157 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1158 struct file *file, void *fh, void *arg)
1160 struct video_device *vfd = video_devdata(file);
1161 struct v4l2_input *p = arg;
1164 * We set the flags for CAP_DV_TIMINGS &
1165 * CAP_STD here based on ioctl handler provided by the
1166 * driver. If the driver doesn't support these
1167 * for a specific input, it must override these flags.
1169 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1170 p->capabilities |= V4L2_IN_CAP_STD;
1172 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1175 strscpy(p->name, vfd->name, sizeof(p->name));
1176 p->type = V4L2_INPUT_TYPE_CAMERA;
1180 return ops->vidioc_enum_input(file, fh, p);
1183 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1184 struct file *file, void *fh, void *arg)
1186 struct video_device *vfd = video_devdata(file);
1187 struct v4l2_output *p = arg;
1190 * We set the flags for CAP_DV_TIMINGS &
1191 * CAP_STD here based on ioctl handler provided by the
1192 * driver. If the driver doesn't support these
1193 * for a specific output, it must override these flags.
1195 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1196 p->capabilities |= V4L2_OUT_CAP_STD;
1198 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1201 strscpy(p->name, vfd->name, sizeof(p->name));
1202 p->type = V4L2_OUTPUT_TYPE_ANALOG;
1206 return ops->vidioc_enum_output(file, fh, p);
1209 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1211 const unsigned sz = sizeof(fmt->description);
1212 const char *descr = NULL;
1216 * We depart from the normal coding style here since the descriptions
1217 * should be aligned so it is easy to see which descriptions will be
1218 * longer than 31 characters (the max length for a description).
1219 * And frankly, this is easier to read anyway.
1221 * Note that gcc will use O(log N) comparisons to find the right case.
1223 switch (fmt->pixelformat) {
1224 /* Max description length mask: descr = "0123456789012345678901234567890" */
1225 case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
1226 case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
1227 case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
1228 case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
1229 case V4L2_PIX_FMT_RGBA444: descr = "16-bit RGBA 4-4-4-4"; break;
1230 case V4L2_PIX_FMT_RGBX444: descr = "16-bit RGBX 4-4-4-4"; break;
1231 case V4L2_PIX_FMT_ABGR444: descr = "16-bit ABGR 4-4-4-4"; break;
1232 case V4L2_PIX_FMT_XBGR444: descr = "16-bit XBGR 4-4-4-4"; break;
1233 case V4L2_PIX_FMT_BGRA444: descr = "16-bit BGRA 4-4-4-4"; break;
1234 case V4L2_PIX_FMT_BGRX444: descr = "16-bit BGRX 4-4-4-4"; break;
1235 case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
1236 case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
1237 case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
1238 case V4L2_PIX_FMT_ABGR555: descr = "16-bit ABGR 1-5-5-5"; break;
1239 case V4L2_PIX_FMT_XBGR555: descr = "16-bit XBGR 1-5-5-5"; break;
1240 case V4L2_PIX_FMT_RGBA555: descr = "16-bit RGBA 5-5-5-1"; break;
1241 case V4L2_PIX_FMT_RGBX555: descr = "16-bit RGBX 5-5-5-1"; break;
1242 case V4L2_PIX_FMT_BGRA555: descr = "16-bit BGRA 5-5-5-1"; break;
1243 case V4L2_PIX_FMT_BGRX555: descr = "16-bit BGRX 5-5-5-1"; break;
1244 case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
1245 case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1246 case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
1247 case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
1248 case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
1249 case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
1250 case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
1251 case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
1252 case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
1253 case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
1254 case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
1255 case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
1256 case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
1257 case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
1258 case V4L2_PIX_FMT_BGRA32: descr = "32-bit ABGR 8-8-8-8"; break;
1259 case V4L2_PIX_FMT_BGRX32: descr = "32-bit XBGR 8-8-8-8"; break;
1260 case V4L2_PIX_FMT_RGBA32: descr = "32-bit RGBA 8-8-8-8"; break;
1261 case V4L2_PIX_FMT_RGBX32: descr = "32-bit RGBX 8-8-8-8"; break;
1262 case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
1263 case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
1264 case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
1265 case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
1266 case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
1267 case V4L2_PIX_FMT_Y14: descr = "14-bit Greyscale"; break;
1268 case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
1269 case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
1270 case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
1271 case V4L2_PIX_FMT_Y10P: descr = "10-bit Greyscale (MIPI Packed)"; break;
1272 case V4L2_PIX_FMT_IPU3_Y10: descr = "10-bit greyscale (IPU3 Packed)"; break;
1273 case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
1274 case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
1275 case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
1276 case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
1277 case V4L2_PIX_FMT_CNF4: descr = "4-bit Depth Confidence (Packed)"; break;
1278 case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
1279 case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
1280 case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
1281 case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
1282 case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
1283 case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
1284 case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
1285 case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
1286 case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
1287 case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
1288 case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
1289 case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
1290 case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
1291 case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
1292 case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
1293 case V4L2_PIX_FMT_YUV24: descr = "24-bit YUV 4:4:4 8-8-8"; break;
1294 case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
1295 case V4L2_PIX_FMT_AYUV32: descr = "32-bit AYUV 8-8-8-8"; break;
1296 case V4L2_PIX_FMT_XYUV32: descr = "32-bit XYUV 8-8-8-8"; break;
1297 case V4L2_PIX_FMT_VUYA32: descr = "32-bit VUYA 8-8-8-8"; break;
1298 case V4L2_PIX_FMT_VUYX32: descr = "32-bit VUYX 8-8-8-8"; break;
1299 case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
1300 case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
1301 case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
1302 case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
1303 case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
1304 case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
1305 case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
1306 case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
1307 case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
1308 case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
1309 case V4L2_PIX_FMT_NV12_4L4: descr = "Y/CbCr 4:2:0 (4x4 Linear)"; break;
1310 case V4L2_PIX_FMT_NV12_16L16: descr = "Y/CbCr 4:2:0 (16x16 Linear)"; break;
1311 case V4L2_PIX_FMT_NV12_32L32: descr = "Y/CbCr 4:2:0 (32x32 Linear)"; break;
1312 case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
1313 case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
1314 case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
1315 case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
1316 case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1317 case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1318 case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
1319 case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
1320 case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
1321 case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
1322 case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
1323 case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
1324 case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
1325 case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
1326 case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
1327 case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
1328 case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
1329 case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
1330 case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
1331 case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
1332 case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1333 case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1334 case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1335 case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1336 case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1337 case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1338 case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1339 case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1340 case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1341 case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1342 case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1343 case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1344 case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1345 case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1346 case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1347 case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1348 case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
1349 case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
1350 case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
1351 case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
1352 case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1353 case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1354 case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1355 case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1356 case V4L2_PIX_FMT_SBGGR14: descr = "14-bit Bayer BGBG/GRGR"; break;
1357 case V4L2_PIX_FMT_SGBRG14: descr = "14-bit Bayer GBGB/RGRG"; break;
1358 case V4L2_PIX_FMT_SGRBG14: descr = "14-bit Bayer GRGR/BGBG"; break;
1359 case V4L2_PIX_FMT_SRGGB14: descr = "14-bit Bayer RGRG/GBGB"; break;
1360 case V4L2_PIX_FMT_SBGGR14P: descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1361 case V4L2_PIX_FMT_SGBRG14P: descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1362 case V4L2_PIX_FMT_SGRBG14P: descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1363 case V4L2_PIX_FMT_SRGGB14P: descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1364 case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
1365 case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
1366 case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
1367 case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
1368 case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1369 case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
1370 case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
1371 case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
1372 case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
1373 case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
1374 case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
1375 case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
1376 case V4L2_PIX_FMT_MM21: descr = "Mediatek 8-bit Block Format"; break;
1377 case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
1378 case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
1379 case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
1380 case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
1381 case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
1382 case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
1383 case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
1384 case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
1385 case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
1386 case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
1387 case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit Signed Deltas"; break;
1388 case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit Signed Deltas"; break;
1389 case V4L2_TCH_FMT_TU16: descr = "16-bit Unsigned Touch Data"; break;
1390 case V4L2_TCH_FMT_TU08: descr = "8-bit Unsigned Touch Data"; break;
1391 case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
1392 case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
1393 case V4L2_META_FMT_UVC: descr = "UVC Payload Header Metadata"; break;
1394 case V4L2_META_FMT_D4XX: descr = "Intel D4xx UVC Metadata"; break;
1395 case V4L2_META_FMT_VIVID: descr = "Vivid Metadata"; break;
1396 case V4L2_META_FMT_RK_ISP1_PARAMS: descr = "Rockchip ISP1 3A Parameters"; break;
1397 case V4L2_META_FMT_RK_ISP1_STAT_3A: descr = "Rockchip ISP1 3A Statistics"; break;
1398 case V4L2_PIX_FMT_NV12M_8L128: descr = "NV12M (8x128 Linear)"; break;
1399 case V4L2_PIX_FMT_NV12M_10BE_8L128: descr = "10-bit NV12M (8x128 Linear, BE)"; break;
1402 /* Compressed formats */
1403 flags = V4L2_FMT_FLAG_COMPRESSED;
1404 switch (fmt->pixelformat) {
1405 /* Max description length mask: descr = "0123456789012345678901234567890" */
1406 case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
1407 case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
1408 case V4L2_PIX_FMT_DV: descr = "1394"; break;
1409 case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
1410 case V4L2_PIX_FMT_H264: descr = "H.264"; break;
1411 case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
1412 case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
1413 case V4L2_PIX_FMT_H264_SLICE: descr = "H.264 Parsed Slice Data"; break;
1414 case V4L2_PIX_FMT_H263: descr = "H.263"; break;
1415 case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
1416 case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
1417 case V4L2_PIX_FMT_MPEG2_SLICE: descr = "MPEG-2 Parsed Slice Data"; break;
1418 case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 Part 2 ES"; break;
1419 case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
1420 case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
1421 case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
1422 case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
1423 case V4L2_PIX_FMT_VP8_FRAME: descr = "VP8 Frame"; break;
1424 case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
1425 case V4L2_PIX_FMT_VP9_FRAME: descr = "VP9 Frame"; break;
1426 case V4L2_PIX_FMT_HEVC: descr = "HEVC"; break; /* aka H.265 */
1427 case V4L2_PIX_FMT_HEVC_SLICE: descr = "HEVC Parsed Slice Data"; break;
1428 case V4L2_PIX_FMT_FWHT: descr = "FWHT"; break; /* used in vicodec */
1429 case V4L2_PIX_FMT_FWHT_STATELESS: descr = "FWHT Stateless"; break; /* used in vicodec */
1430 case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
1431 case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
1432 case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
1433 case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
1434 case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
1435 case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
1436 case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
1437 case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
1438 case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
1439 case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
1440 case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
1441 case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
1442 case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
1443 case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
1444 case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
1445 case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
1446 case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
1447 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1448 case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
1449 case V4L2_PIX_FMT_QC08C: descr = "QCOM Compressed 8-bit Format"; break;
1450 case V4L2_PIX_FMT_QC10C: descr = "QCOM Compressed 10-bit Format"; break;
1452 if (fmt->description[0])
1454 WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1456 snprintf(fmt->description, sz, "%p4cc",
1463 WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1464 fmt->flags |= flags;
1467 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1468 struct file *file, void *fh, void *arg)
1470 struct video_device *vdev = video_devdata(file);
1471 struct v4l2_fmtdesc *p = arg;
1472 int ret = check_fmt(file, p->type);
1480 if (!(vdev->device_caps & V4L2_CAP_IO_MC))
1483 mbus_code = p->mbus_code;
1484 CLEAR_AFTER_FIELD(p, type);
1485 p->mbus_code = mbus_code;
1488 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1489 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1490 cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1491 V4L2_CAP_VIDEO_M2M_MPLANE;
1492 if (!!(vdev->device_caps & cap_mask) !=
1493 (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1496 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1498 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1500 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1501 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1503 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1505 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1506 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1507 cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1508 V4L2_CAP_VIDEO_M2M_MPLANE;
1509 if (!!(vdev->device_caps & cap_mask) !=
1510 (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1513 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1515 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1517 case V4L2_BUF_TYPE_SDR_CAPTURE:
1518 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1520 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1522 case V4L2_BUF_TYPE_SDR_OUTPUT:
1523 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1525 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1527 case V4L2_BUF_TYPE_META_CAPTURE:
1528 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1530 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1532 case V4L2_BUF_TYPE_META_OUTPUT:
1533 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1535 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1539 v4l_fill_fmtdesc(p);
1543 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1546 * The v4l2_pix_format structure contains fields that make no sense for
1547 * touch. Set them to default values in this case.
1550 p->field = V4L2_FIELD_NONE;
1551 p->colorspace = V4L2_COLORSPACE_RAW;
1554 p->quantization = 0;
1558 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1559 struct file *file, void *fh, void *arg)
1561 struct v4l2_format *p = arg;
1562 struct video_device *vfd = video_devdata(file);
1563 int ret = check_fmt(file, p->type);
1569 * fmt can't be cleared for these overlay types due to the 'clips'
1570 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1571 * Those are provided by the user. So handle these two overlay types
1572 * first, and then just do a simple memset for the other types.
1575 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1576 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1577 struct v4l2_clip *clips = p->fmt.win.clips;
1578 u32 clipcount = p->fmt.win.clipcount;
1579 void __user *bitmap = p->fmt.win.bitmap;
1581 memset(&p->fmt, 0, sizeof(p->fmt));
1582 p->fmt.win.clips = clips;
1583 p->fmt.win.clipcount = clipcount;
1584 p->fmt.win.bitmap = bitmap;
1588 memset(&p->fmt, 0, sizeof(p->fmt));
1593 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1594 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1596 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1597 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1598 /* just in case the driver zeroed it again */
1599 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1600 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1601 v4l_pix_format_touch(&p->fmt.pix);
1603 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1604 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1605 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1606 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1607 case V4L2_BUF_TYPE_VBI_CAPTURE:
1608 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1609 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1610 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1611 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1612 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1614 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1615 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1616 /* just in case the driver zeroed it again */
1617 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1619 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1620 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1621 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1622 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1623 case V4L2_BUF_TYPE_VBI_OUTPUT:
1624 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1625 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1626 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1627 case V4L2_BUF_TYPE_SDR_CAPTURE:
1628 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1629 case V4L2_BUF_TYPE_SDR_OUTPUT:
1630 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1631 case V4L2_BUF_TYPE_META_CAPTURE:
1632 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1633 case V4L2_BUF_TYPE_META_OUTPUT:
1634 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1639 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1640 struct file *file, void *fh, void *arg)
1642 struct v4l2_format *p = arg;
1643 struct video_device *vfd = video_devdata(file);
1644 int ret = check_fmt(file, p->type);
1650 ret = v4l_enable_media_source(vfd);
1653 v4l_sanitize_format(p);
1656 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1657 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1659 CLEAR_AFTER_FIELD(p, fmt.pix);
1660 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1661 /* just in case the driver zeroed it again */
1662 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1663 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1664 v4l_pix_format_touch(&p->fmt.pix);
1666 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1667 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1669 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1670 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1671 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1673 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1674 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1675 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1677 CLEAR_AFTER_FIELD(p, fmt.win);
1678 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1679 case V4L2_BUF_TYPE_VBI_CAPTURE:
1680 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1682 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1683 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1684 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1685 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1687 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1688 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1689 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1690 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1692 CLEAR_AFTER_FIELD(p, fmt.pix);
1693 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1694 /* just in case the driver zeroed it again */
1695 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1697 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1698 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1700 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1701 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1702 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1704 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1705 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1706 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1708 CLEAR_AFTER_FIELD(p, fmt.win);
1709 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1710 case V4L2_BUF_TYPE_VBI_OUTPUT:
1711 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1713 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1714 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1715 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1716 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1718 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1719 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1720 case V4L2_BUF_TYPE_SDR_CAPTURE:
1721 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1723 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1724 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1725 case V4L2_BUF_TYPE_SDR_OUTPUT:
1726 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1728 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1729 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1730 case V4L2_BUF_TYPE_META_CAPTURE:
1731 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1733 CLEAR_AFTER_FIELD(p, fmt.meta);
1734 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1735 case V4L2_BUF_TYPE_META_OUTPUT:
1736 if (unlikely(!ops->vidioc_s_fmt_meta_out))
1738 CLEAR_AFTER_FIELD(p, fmt.meta);
1739 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1744 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1745 struct file *file, void *fh, void *arg)
1747 struct v4l2_format *p = arg;
1748 struct video_device *vfd = video_devdata(file);
1749 int ret = check_fmt(file, p->type);
1755 v4l_sanitize_format(p);
1758 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1759 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1761 CLEAR_AFTER_FIELD(p, fmt.pix);
1762 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1763 /* just in case the driver zeroed it again */
1764 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1765 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1766 v4l_pix_format_touch(&p->fmt.pix);
1768 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1769 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1771 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1772 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1773 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1775 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1776 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1777 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1779 CLEAR_AFTER_FIELD(p, fmt.win);
1780 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1781 case V4L2_BUF_TYPE_VBI_CAPTURE:
1782 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1784 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1785 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1786 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1787 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1789 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1790 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1791 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1792 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1794 CLEAR_AFTER_FIELD(p, fmt.pix);
1795 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1796 /* just in case the driver zeroed it again */
1797 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1799 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1800 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1802 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1803 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1804 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1806 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1807 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1808 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1810 CLEAR_AFTER_FIELD(p, fmt.win);
1811 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1812 case V4L2_BUF_TYPE_VBI_OUTPUT:
1813 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1815 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1816 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1817 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1818 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1820 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1821 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1822 case V4L2_BUF_TYPE_SDR_CAPTURE:
1823 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1825 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1826 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1827 case V4L2_BUF_TYPE_SDR_OUTPUT:
1828 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1830 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1831 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1832 case V4L2_BUF_TYPE_META_CAPTURE:
1833 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1835 CLEAR_AFTER_FIELD(p, fmt.meta);
1836 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1837 case V4L2_BUF_TYPE_META_OUTPUT:
1838 if (unlikely(!ops->vidioc_try_fmt_meta_out))
1840 CLEAR_AFTER_FIELD(p, fmt.meta);
1841 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1846 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1847 struct file *file, void *fh, void *arg)
1849 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1852 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1853 struct file *file, void *fh, void *arg)
1855 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1858 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1859 struct file *file, void *fh, void *arg)
1861 struct video_device *vfd = video_devdata(file);
1862 struct v4l2_tuner *p = arg;
1865 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1866 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1867 err = ops->vidioc_g_tuner(file, fh, p);
1869 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1873 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1874 struct file *file, void *fh, void *arg)
1876 struct video_device *vfd = video_devdata(file);
1877 struct v4l2_tuner *p = arg;
1880 ret = v4l_enable_media_source(vfd);
1883 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1884 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1885 return ops->vidioc_s_tuner(file, fh, p);
1888 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1889 struct file *file, void *fh, void *arg)
1891 struct video_device *vfd = video_devdata(file);
1892 struct v4l2_modulator *p = arg;
1895 if (vfd->vfl_type == VFL_TYPE_RADIO)
1896 p->type = V4L2_TUNER_RADIO;
1898 err = ops->vidioc_g_modulator(file, fh, p);
1900 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1904 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1905 struct file *file, void *fh, void *arg)
1907 struct video_device *vfd = video_devdata(file);
1908 struct v4l2_modulator *p = arg;
1910 if (vfd->vfl_type == VFL_TYPE_RADIO)
1911 p->type = V4L2_TUNER_RADIO;
1913 return ops->vidioc_s_modulator(file, fh, p);
1916 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1917 struct file *file, void *fh, void *arg)
1919 struct video_device *vfd = video_devdata(file);
1920 struct v4l2_frequency *p = arg;
1922 if (vfd->vfl_type == VFL_TYPE_SDR)
1923 p->type = V4L2_TUNER_SDR;
1925 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1926 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1927 return ops->vidioc_g_frequency(file, fh, p);
1930 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1931 struct file *file, void *fh, void *arg)
1933 struct video_device *vfd = video_devdata(file);
1934 const struct v4l2_frequency *p = arg;
1935 enum v4l2_tuner_type type;
1938 ret = v4l_enable_media_source(vfd);
1941 if (vfd->vfl_type == VFL_TYPE_SDR) {
1942 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1945 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1946 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1947 if (type != p->type)
1950 return ops->vidioc_s_frequency(file, fh, p);
1953 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1954 struct file *file, void *fh, void *arg)
1956 struct video_device *vfd = video_devdata(file);
1957 struct v4l2_standard *p = arg;
1959 return v4l_video_std_enumstd(p, vfd->tvnorms);
1962 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1963 struct file *file, void *fh, void *arg)
1965 struct video_device *vfd = video_devdata(file);
1966 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1969 ret = v4l_enable_media_source(vfd);
1972 norm = id & vfd->tvnorms;
1973 if (vfd->tvnorms && !norm) /* Check if std is supported */
1976 /* Calls the specific handler */
1977 return ops->vidioc_s_std(file, fh, norm);
1980 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1981 struct file *file, void *fh, void *arg)
1983 struct video_device *vfd = video_devdata(file);
1984 v4l2_std_id *p = arg;
1987 ret = v4l_enable_media_source(vfd);
1991 * If no signal is detected, then the driver should return
1992 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1993 * any standards that do not apply removed.
1995 * This means that tuners, audio and video decoders can join
1996 * their efforts to improve the standards detection.
1999 return ops->vidioc_querystd(file, fh, arg);
2002 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
2003 struct file *file, void *fh, void *arg)
2005 struct video_device *vfd = video_devdata(file);
2006 struct v4l2_hw_freq_seek *p = arg;
2007 enum v4l2_tuner_type type;
2010 ret = v4l_enable_media_source(vfd);
2013 /* s_hw_freq_seek is not supported for SDR for now */
2014 if (vfd->vfl_type == VFL_TYPE_SDR)
2017 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2018 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2019 if (p->type != type)
2021 return ops->vidioc_s_hw_freq_seek(file, fh, p);
2024 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
2025 struct file *file, void *fh, void *arg)
2027 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
2030 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
2031 struct file *file, void *fh, void *arg)
2033 struct v4l2_requestbuffers *p = arg;
2034 int ret = check_fmt(file, p->type);
2039 CLEAR_AFTER_FIELD(p, flags);
2041 return ops->vidioc_reqbufs(file, fh, p);
2044 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
2045 struct file *file, void *fh, void *arg)
2047 struct v4l2_buffer *p = arg;
2048 int ret = check_fmt(file, p->type);
2050 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
2053 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2054 struct file *file, void *fh, void *arg)
2056 struct v4l2_buffer *p = arg;
2057 int ret = check_fmt(file, p->type);
2059 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2062 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2063 struct file *file, void *fh, void *arg)
2065 struct v4l2_buffer *p = arg;
2066 int ret = check_fmt(file, p->type);
2068 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2071 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2072 struct file *file, void *fh, void *arg)
2074 struct v4l2_create_buffers *create = arg;
2075 int ret = check_fmt(file, create->format.type);
2080 CLEAR_AFTER_FIELD(create, flags);
2082 v4l_sanitize_format(&create->format);
2084 ret = ops->vidioc_create_bufs(file, fh, create);
2086 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2087 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2088 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2093 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2094 struct file *file, void *fh, void *arg)
2096 struct v4l2_buffer *b = arg;
2097 int ret = check_fmt(file, b->type);
2099 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2102 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2103 struct file *file, void *fh, void *arg)
2105 struct video_device *vfd = video_devdata(file);
2106 struct v4l2_streamparm *p = arg;
2108 int ret = check_fmt(file, p->type);
2112 if (ops->vidioc_g_parm)
2113 return ops->vidioc_g_parm(file, fh, p);
2114 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2115 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2117 if (vfd->device_caps & V4L2_CAP_READWRITE)
2118 p->parm.capture.readbuffers = 2;
2119 ret = ops->vidioc_g_std(file, fh, &std);
2121 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2125 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2126 struct file *file, void *fh, void *arg)
2128 struct v4l2_streamparm *p = arg;
2129 int ret = check_fmt(file, p->type);
2134 /* Note: extendedmode is never used in drivers */
2135 if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2136 memset(p->parm.output.reserved, 0,
2137 sizeof(p->parm.output.reserved));
2138 p->parm.output.extendedmode = 0;
2139 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2141 memset(p->parm.capture.reserved, 0,
2142 sizeof(p->parm.capture.reserved));
2143 p->parm.capture.extendedmode = 0;
2144 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2146 return ops->vidioc_s_parm(file, fh, p);
2149 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2150 struct file *file, void *fh, void *arg)
2152 struct video_device *vfd = video_devdata(file);
2153 struct v4l2_queryctrl *p = arg;
2154 struct v4l2_fh *vfh =
2155 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2157 if (vfh && vfh->ctrl_handler)
2158 return v4l2_queryctrl(vfh->ctrl_handler, p);
2159 if (vfd->ctrl_handler)
2160 return v4l2_queryctrl(vfd->ctrl_handler, p);
2161 if (ops->vidioc_queryctrl)
2162 return ops->vidioc_queryctrl(file, fh, p);
2166 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2167 struct file *file, void *fh, void *arg)
2169 struct video_device *vfd = video_devdata(file);
2170 struct v4l2_query_ext_ctrl *p = arg;
2171 struct v4l2_fh *vfh =
2172 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2174 if (vfh && vfh->ctrl_handler)
2175 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2176 if (vfd->ctrl_handler)
2177 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2178 if (ops->vidioc_query_ext_ctrl)
2179 return ops->vidioc_query_ext_ctrl(file, fh, p);
2183 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2184 struct file *file, void *fh, void *arg)
2186 struct video_device *vfd = video_devdata(file);
2187 struct v4l2_querymenu *p = arg;
2188 struct v4l2_fh *vfh =
2189 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2191 if (vfh && vfh->ctrl_handler)
2192 return v4l2_querymenu(vfh->ctrl_handler, p);
2193 if (vfd->ctrl_handler)
2194 return v4l2_querymenu(vfd->ctrl_handler, p);
2195 if (ops->vidioc_querymenu)
2196 return ops->vidioc_querymenu(file, fh, p);
2200 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2201 struct file *file, void *fh, void *arg)
2203 struct video_device *vfd = video_devdata(file);
2204 struct v4l2_control *p = arg;
2205 struct v4l2_fh *vfh =
2206 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2207 struct v4l2_ext_controls ctrls;
2208 struct v4l2_ext_control ctrl;
2210 if (vfh && vfh->ctrl_handler)
2211 return v4l2_g_ctrl(vfh->ctrl_handler, p);
2212 if (vfd->ctrl_handler)
2213 return v4l2_g_ctrl(vfd->ctrl_handler, p);
2214 if (ops->vidioc_g_ctrl)
2215 return ops->vidioc_g_ctrl(file, fh, p);
2216 if (ops->vidioc_g_ext_ctrls == NULL)
2219 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2221 ctrls.controls = &ctrl;
2223 ctrl.value = p->value;
2224 if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
2225 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2228 p->value = ctrl.value;
2234 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2235 struct file *file, void *fh, void *arg)
2237 struct video_device *vfd = video_devdata(file);
2238 struct v4l2_control *p = arg;
2239 struct v4l2_fh *vfh =
2240 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2241 struct v4l2_ext_controls ctrls;
2242 struct v4l2_ext_control ctrl;
2245 if (vfh && vfh->ctrl_handler)
2246 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2247 if (vfd->ctrl_handler)
2248 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2249 if (ops->vidioc_s_ctrl)
2250 return ops->vidioc_s_ctrl(file, fh, p);
2251 if (ops->vidioc_s_ext_ctrls == NULL)
2254 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2256 ctrls.controls = &ctrl;
2258 ctrl.value = p->value;
2259 if (!check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
2261 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2262 p->value = ctrl.value;
2266 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2267 struct file *file, void *fh, void *arg)
2269 struct video_device *vfd = video_devdata(file);
2270 struct v4l2_ext_controls *p = arg;
2271 struct v4l2_fh *vfh =
2272 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2274 p->error_idx = p->count;
2275 if (vfh && vfh->ctrl_handler)
2276 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2277 vfd, vfd->v4l2_dev->mdev, p);
2278 if (vfd->ctrl_handler)
2279 return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2280 vfd, vfd->v4l2_dev->mdev, p);
2281 if (ops->vidioc_g_ext_ctrls == NULL)
2283 return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
2284 ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
2287 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2288 struct file *file, void *fh, void *arg)
2290 struct video_device *vfd = video_devdata(file);
2291 struct v4l2_ext_controls *p = arg;
2292 struct v4l2_fh *vfh =
2293 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2295 p->error_idx = p->count;
2296 if (vfh && vfh->ctrl_handler)
2297 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2298 vfd, vfd->v4l2_dev->mdev, p);
2299 if (vfd->ctrl_handler)
2300 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2301 vfd, vfd->v4l2_dev->mdev, p);
2302 if (ops->vidioc_s_ext_ctrls == NULL)
2304 return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
2305 ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
2308 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2309 struct file *file, void *fh, void *arg)
2311 struct video_device *vfd = video_devdata(file);
2312 struct v4l2_ext_controls *p = arg;
2313 struct v4l2_fh *vfh =
2314 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2316 p->error_idx = p->count;
2317 if (vfh && vfh->ctrl_handler)
2318 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2319 vfd, vfd->v4l2_dev->mdev, p);
2320 if (vfd->ctrl_handler)
2321 return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2322 vfd, vfd->v4l2_dev->mdev, p);
2323 if (ops->vidioc_try_ext_ctrls == NULL)
2325 return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
2326 ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
2330 * The selection API specified originally that the _MPLANE buffer types
2331 * shouldn't be used. The reasons for this are lost in the mists of time
2332 * (or just really crappy memories). Regardless, this is really annoying
2333 * for userspace. So to keep things simple we map _MPLANE buffer types
2334 * to their 'regular' counterparts before calling the driver. And we
2335 * restore it afterwards. This way applications can use either buffer
2336 * type and drivers don't need to check for both.
2338 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2339 struct file *file, void *fh, void *arg)
2341 struct v4l2_selection *p = arg;
2342 u32 old_type = p->type;
2345 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2346 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2347 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2348 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2349 ret = ops->vidioc_g_selection(file, fh, p);
2354 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2355 struct file *file, void *fh, void *arg)
2357 struct v4l2_selection *p = arg;
2358 u32 old_type = p->type;
2361 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2362 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2363 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2364 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2365 ret = ops->vidioc_s_selection(file, fh, p);
2370 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2371 struct file *file, void *fh, void *arg)
2373 struct video_device *vfd = video_devdata(file);
2374 struct v4l2_crop *p = arg;
2375 struct v4l2_selection s = {
2380 /* simulate capture crop using selection api */
2382 /* crop means compose for output devices */
2383 if (V4L2_TYPE_IS_OUTPUT(p->type))
2384 s.target = V4L2_SEL_TGT_COMPOSE;
2386 s.target = V4L2_SEL_TGT_CROP;
2388 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2389 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2390 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2392 ret = v4l_g_selection(ops, file, fh, &s);
2394 /* copying results to old structure on success */
2400 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2401 struct file *file, void *fh, void *arg)
2403 struct video_device *vfd = video_devdata(file);
2404 struct v4l2_crop *p = arg;
2405 struct v4l2_selection s = {
2410 /* simulate capture crop using selection api */
2412 /* crop means compose for output devices */
2413 if (V4L2_TYPE_IS_OUTPUT(p->type))
2414 s.target = V4L2_SEL_TGT_COMPOSE;
2416 s.target = V4L2_SEL_TGT_CROP;
2418 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2419 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2420 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2422 return v4l_s_selection(ops, file, fh, &s);
2425 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2426 struct file *file, void *fh, void *arg)
2428 struct video_device *vfd = video_devdata(file);
2429 struct v4l2_cropcap *p = arg;
2430 struct v4l2_selection s = { .type = p->type };
2433 /* setting trivial pixelaspect */
2434 p->pixelaspect.numerator = 1;
2435 p->pixelaspect.denominator = 1;
2437 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2438 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2439 else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2440 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2443 * The determine_valid_ioctls() call already should ensure
2444 * that this can never happen, but just in case...
2446 if (WARN_ON(!ops->vidioc_g_selection))
2449 if (ops->vidioc_g_pixelaspect)
2450 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2454 * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2455 * square pixel aspect ratio in that case.
2457 if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2460 /* Use g_selection() to fill in the bounds and defrect rectangles */
2462 /* obtaining bounds */
2463 if (V4L2_TYPE_IS_OUTPUT(p->type))
2464 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2466 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2468 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2469 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2470 V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2472 ret = v4l_g_selection(ops, file, fh, &s);
2477 /* obtaining defrect */
2478 if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2479 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2481 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2483 ret = v4l_g_selection(ops, file, fh, &s);
2491 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2492 struct file *file, void *fh, void *arg)
2494 struct video_device *vfd = video_devdata(file);
2498 pr_info("%s: ================= START STATUS =================\n",
2499 vfd->v4l2_dev->name);
2500 ret = ops->vidioc_log_status(file, fh);
2502 pr_info("%s: ================== END STATUS ==================\n",
2503 vfd->v4l2_dev->name);
2507 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2508 struct file *file, void *fh, void *arg)
2510 #ifdef CONFIG_VIDEO_ADV_DEBUG
2511 struct v4l2_dbg_register *p = arg;
2512 struct video_device *vfd = video_devdata(file);
2513 struct v4l2_subdev *sd;
2516 if (!capable(CAP_SYS_ADMIN))
2518 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2519 if (vfd->v4l2_dev == NULL)
2521 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2522 if (p->match.addr == idx++)
2523 return v4l2_subdev_call(sd, core, g_register, p);
2526 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2527 (ops->vidioc_g_chip_info || p->match.addr == 0))
2528 return ops->vidioc_g_register(file, fh, p);
2535 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2536 struct file *file, void *fh, void *arg)
2538 #ifdef CONFIG_VIDEO_ADV_DEBUG
2539 const struct v4l2_dbg_register *p = arg;
2540 struct video_device *vfd = video_devdata(file);
2541 struct v4l2_subdev *sd;
2544 if (!capable(CAP_SYS_ADMIN))
2546 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2547 if (vfd->v4l2_dev == NULL)
2549 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2550 if (p->match.addr == idx++)
2551 return v4l2_subdev_call(sd, core, s_register, p);
2554 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2555 (ops->vidioc_g_chip_info || p->match.addr == 0))
2556 return ops->vidioc_s_register(file, fh, p);
2563 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2564 struct file *file, void *fh, void *arg)
2566 #ifdef CONFIG_VIDEO_ADV_DEBUG
2567 struct video_device *vfd = video_devdata(file);
2568 struct v4l2_dbg_chip_info *p = arg;
2569 struct v4l2_subdev *sd;
2572 switch (p->match.type) {
2573 case V4L2_CHIP_MATCH_BRIDGE:
2574 if (ops->vidioc_s_register)
2575 p->flags |= V4L2_CHIP_FL_WRITABLE;
2576 if (ops->vidioc_g_register)
2577 p->flags |= V4L2_CHIP_FL_READABLE;
2578 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2579 if (ops->vidioc_g_chip_info)
2580 return ops->vidioc_g_chip_info(file, fh, arg);
2585 case V4L2_CHIP_MATCH_SUBDEV:
2586 if (vfd->v4l2_dev == NULL)
2588 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2589 if (p->match.addr != idx++)
2591 if (sd->ops->core && sd->ops->core->s_register)
2592 p->flags |= V4L2_CHIP_FL_WRITABLE;
2593 if (sd->ops->core && sd->ops->core->g_register)
2594 p->flags |= V4L2_CHIP_FL_READABLE;
2595 strscpy(p->name, sd->name, sizeof(p->name));
2606 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2607 struct file *file, void *fh, void *arg)
2609 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2612 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2613 struct file *file, void *fh, void *arg)
2615 return ops->vidioc_subscribe_event(fh, arg);
2618 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2619 struct file *file, void *fh, void *arg)
2621 return ops->vidioc_unsubscribe_event(fh, arg);
2624 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2625 struct file *file, void *fh, void *arg)
2627 struct v4l2_sliced_vbi_cap *p = arg;
2628 int ret = check_fmt(file, p->type);
2633 /* Clear up to type, everything after type is zeroed already */
2634 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2636 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2639 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2640 struct file *file, void *fh, void *arg)
2642 struct video_device *vfd = video_devdata(file);
2643 struct v4l2_frequency_band *p = arg;
2644 enum v4l2_tuner_type type;
2647 if (vfd->vfl_type == VFL_TYPE_SDR) {
2648 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2652 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2653 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2654 if (type != p->type)
2657 if (ops->vidioc_enum_freq_bands) {
2658 err = ops->vidioc_enum_freq_bands(file, fh, p);
2662 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2663 struct v4l2_tuner t = {
2670 err = ops->vidioc_g_tuner(file, fh, &t);
2673 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2674 p->rangelow = t.rangelow;
2675 p->rangehigh = t.rangehigh;
2676 p->modulation = (type == V4L2_TUNER_RADIO) ?
2677 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2680 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2681 struct v4l2_modulator m = {
2685 if (type != V4L2_TUNER_RADIO)
2689 err = ops->vidioc_g_modulator(file, fh, &m);
2692 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2693 p->rangelow = m.rangelow;
2694 p->rangehigh = m.rangehigh;
2695 p->modulation = (type == V4L2_TUNER_RADIO) ?
2696 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2702 struct v4l2_ioctl_info {
2705 const char * const name;
2706 int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2708 void (*debug)(const void *arg, bool write_only);
2711 /* This control needs a priority check */
2712 #define INFO_FL_PRIO (1 << 0)
2713 /* This control can be valid if the filehandle passes a control handler. */
2714 #define INFO_FL_CTRL (1 << 1)
2716 #define INFO_FL_QUEUE (1 << 2)
2717 /* Always copy back result, even on error */
2718 #define INFO_FL_ALWAYS_COPY (1 << 3)
2719 /* Zero struct from after the field to the end */
2720 #define INFO_FL_CLEAR(v4l2_struct, field) \
2721 ((offsetof(struct v4l2_struct, field) + \
2722 sizeof_field(struct v4l2_struct, field)) << 16)
2723 #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2725 #define DEFINE_V4L_STUB_FUNC(_vidioc) \
2726 static int v4l_stub_ ## _vidioc( \
2727 const struct v4l2_ioctl_ops *ops, \
2728 struct file *file, void *fh, void *p) \
2730 return ops->vidioc_ ## _vidioc(file, fh, p); \
2733 #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \
2734 [_IOC_NR(_ioctl)] = { \
2742 DEFINE_V4L_STUB_FUNC(g_fbuf)
2743 DEFINE_V4L_STUB_FUNC(s_fbuf)
2744 DEFINE_V4L_STUB_FUNC(expbuf)
2745 DEFINE_V4L_STUB_FUNC(g_std)
2746 DEFINE_V4L_STUB_FUNC(g_audio)
2747 DEFINE_V4L_STUB_FUNC(s_audio)
2748 DEFINE_V4L_STUB_FUNC(g_edid)
2749 DEFINE_V4L_STUB_FUNC(s_edid)
2750 DEFINE_V4L_STUB_FUNC(g_audout)
2751 DEFINE_V4L_STUB_FUNC(s_audout)
2752 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2753 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2754 DEFINE_V4L_STUB_FUNC(enumaudio)
2755 DEFINE_V4L_STUB_FUNC(enumaudout)
2756 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2757 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2758 DEFINE_V4L_STUB_FUNC(g_enc_index)
2759 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2760 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2761 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2762 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2763 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2764 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2765 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2766 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2767 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2769 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2770 IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2771 IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
2772 IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2773 IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2774 IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2775 IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2776 IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2777 IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2778 IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2779 IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2780 IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2781 IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2782 IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2783 IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2784 IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2785 IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2786 IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2787 IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2788 IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2789 IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2790 IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2791 IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2792 IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2793 IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2794 IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2795 IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2796 IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2797 IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2798 IOCTL_INFO(VIDIOC_G_INPUT, v4l_g_input, v4l_print_u32, 0),
2799 IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2800 IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2801 IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2802 IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_g_output, v4l_print_u32, 0),
2803 IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2804 IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2805 IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2806 IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2807 IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2808 IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2809 IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2810 IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2811 IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2812 IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2813 IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2814 IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2815 IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2816 IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2817 IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2818 IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2819 IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2820 IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2821 IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2822 IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2823 IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2824 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)),
2825 IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2826 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2827 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2828 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2829 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2830 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2831 IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2832 IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2833 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2834 IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2835 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2836 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2837 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2838 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2839 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)),
2840 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2841 IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2842 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2843 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2844 IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2845 IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2846 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2847 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2848 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2849 IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2850 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)),
2851 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)),
2853 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2855 static bool v4l2_is_known_ioctl(unsigned int cmd)
2857 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2859 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2862 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2863 struct v4l2_fh *vfh, unsigned int cmd,
2866 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2868 if (vfh && vfh->m2m_ctx &&
2869 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2870 if (vfh->m2m_ctx->q_lock)
2871 return vfh->m2m_ctx->q_lock;
2873 if (vdev->queue && vdev->queue->lock &&
2874 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2875 return vdev->queue->lock;
2879 /* Common ioctl debug function. This function can be used by
2880 external ioctl messages as well as internal V4L ioctl */
2881 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2883 const char *dir, *type;
2886 printk(KERN_DEBUG "%s: ", prefix);
2888 switch (_IOC_TYPE(cmd)) {
2893 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2897 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2904 switch (_IOC_DIR(cmd)) {
2905 case _IOC_NONE: dir = "--"; break;
2906 case _IOC_READ: dir = "r-"; break;
2907 case _IOC_WRITE: dir = "-w"; break;
2908 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2909 default: dir = "*ERR*"; break;
2911 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2912 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2914 EXPORT_SYMBOL(v4l_printk_ioctl);
2916 static long __video_do_ioctl(struct file *file,
2917 unsigned int cmd, void *arg)
2919 struct video_device *vfd = video_devdata(file);
2920 struct mutex *req_queue_lock = NULL;
2921 struct mutex *lock; /* ioctl serialization mutex */
2922 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2923 bool write_only = false;
2924 struct v4l2_ioctl_info default_info;
2925 const struct v4l2_ioctl_info *info;
2926 void *fh = file->private_data;
2927 struct v4l2_fh *vfh = NULL;
2928 int dev_debug = vfd->dev_debug;
2932 pr_warn("%s: has no ioctl_ops.\n",
2933 video_device_node_name(vfd));
2937 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2938 vfh = file->private_data;
2941 * We need to serialize streamon/off with queueing new requests.
2942 * These ioctls may trigger the cancellation of a streaming
2943 * operation, and that should not be mixed with queueing a new
2944 * request at the same time.
2946 if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2947 (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2948 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2950 if (mutex_lock_interruptible(req_queue_lock))
2951 return -ERESTARTSYS;
2954 lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2956 if (lock && mutex_lock_interruptible(lock)) {
2958 mutex_unlock(req_queue_lock);
2959 return -ERESTARTSYS;
2962 if (!video_is_registered(vfd)) {
2967 if (v4l2_is_known_ioctl(cmd)) {
2968 info = &v4l2_ioctls[_IOC_NR(cmd)];
2970 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2971 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2974 if (vfh && (info->flags & INFO_FL_PRIO)) {
2975 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2980 default_info.ioctl = cmd;
2981 default_info.flags = 0;
2982 default_info.debug = v4l_print_default;
2983 info = &default_info;
2986 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2987 if (info != &default_info) {
2988 ret = info->func(ops, file, fh, arg);
2989 } else if (!ops->vidioc_default) {
2992 ret = ops->vidioc_default(file, fh,
2993 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2998 if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
2999 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
3000 (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
3003 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
3005 pr_cont(": error %ld", ret);
3006 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
3008 else if (_IOC_DIR(cmd) == _IOC_NONE)
3009 info->debug(arg, write_only);
3012 info->debug(arg, write_only);
3020 mutex_unlock(req_queue_lock);
3024 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
3025 void __user **user_ptr, void ***kernel_ptr)
3030 case VIDIOC_PREPARE_BUF:
3031 case VIDIOC_QUERYBUF:
3033 case VIDIOC_DQBUF: {
3034 struct v4l2_buffer *buf = parg;
3036 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
3037 if (buf->length > VIDEO_MAX_PLANES) {
3041 *user_ptr = (void __user *)buf->m.planes;
3042 *kernel_ptr = (void **)&buf->m.planes;
3043 *array_size = sizeof(struct v4l2_plane) * buf->length;
3050 case VIDIOC_S_EDID: {
3051 struct v4l2_edid *edid = parg;
3054 if (edid->blocks > 256) {
3058 *user_ptr = (void __user *)edid->edid;
3059 *kernel_ptr = (void **)&edid->edid;
3060 *array_size = edid->blocks * 128;
3066 case VIDIOC_S_EXT_CTRLS:
3067 case VIDIOC_G_EXT_CTRLS:
3068 case VIDIOC_TRY_EXT_CTRLS: {
3069 struct v4l2_ext_controls *ctrls = parg;
3071 if (ctrls->count != 0) {
3072 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3076 *user_ptr = (void __user *)ctrls->controls;
3077 *kernel_ptr = (void **)&ctrls->controls;
3078 *array_size = sizeof(struct v4l2_ext_control)
3086 case VIDIOC_TRY_FMT: {
3087 struct v4l2_format *fmt = parg;
3089 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OVERLAY &&
3090 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY)
3092 if (fmt->fmt.win.clipcount > 2048)
3094 if (!fmt->fmt.win.clipcount)
3097 *user_ptr = (void __user *)fmt->fmt.win.clips;
3098 *kernel_ptr = (void **)&fmt->fmt.win.clips;
3099 *array_size = sizeof(struct v4l2_clip)
3100 * fmt->fmt.win.clipcount;
3110 static unsigned int video_translate_cmd(unsigned int cmd)
3112 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3114 case VIDIOC_DQEVENT_TIME32:
3115 return VIDIOC_DQEVENT;
3116 case VIDIOC_QUERYBUF_TIME32:
3117 return VIDIOC_QUERYBUF;
3118 case VIDIOC_QBUF_TIME32:
3120 case VIDIOC_DQBUF_TIME32:
3121 return VIDIOC_DQBUF;
3122 case VIDIOC_PREPARE_BUF_TIME32:
3123 return VIDIOC_PREPARE_BUF;
3126 if (in_compat_syscall())
3127 return v4l2_compat_translate_cmd(cmd);
3132 static int video_get_user(void __user *arg, void *parg,
3133 unsigned int real_cmd, unsigned int cmd,
3136 unsigned int n = _IOC_SIZE(real_cmd);
3139 if (!(_IOC_DIR(cmd) & _IOC_WRITE)) {
3140 /* read-only ioctl */
3146 * In some cases, only a few fields are used as input,
3147 * i.e. when the app sets "index" and then the driver
3148 * fills in the rest of the structure for the thing
3149 * with that index. We only need to copy up the first
3152 if (v4l2_is_known_ioctl(real_cmd)) {
3153 u32 flags = v4l2_ioctls[_IOC_NR(real_cmd)].flags;
3155 if (flags & INFO_FL_CLEAR_MASK)
3156 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3157 *always_copy = flags & INFO_FL_ALWAYS_COPY;
3160 if (cmd == real_cmd) {
3161 if (copy_from_user(parg, (void __user *)arg, n))
3163 } else if (in_compat_syscall()) {
3165 err = v4l2_compat_get_user(arg, parg, cmd);
3168 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3170 case VIDIOC_QUERYBUF_TIME32:
3171 case VIDIOC_QBUF_TIME32:
3172 case VIDIOC_DQBUF_TIME32:
3173 case VIDIOC_PREPARE_BUF_TIME32: {
3174 struct v4l2_buffer_time32 vb32;
3175 struct v4l2_buffer *vb = parg;
3177 if (copy_from_user(&vb32, arg, sizeof(vb32)))
3180 *vb = (struct v4l2_buffer) {
3181 .index = vb32.index,
3183 .bytesused = vb32.bytesused,
3184 .flags = vb32.flags,
3185 .field = vb32.field,
3186 .timestamp.tv_sec = vb32.timestamp.tv_sec,
3187 .timestamp.tv_usec = vb32.timestamp.tv_usec,
3188 .timecode = vb32.timecode,
3189 .sequence = vb32.sequence,
3190 .memory = vb32.memory,
3191 .m.userptr = vb32.m.userptr,
3192 .length = vb32.length,
3193 .request_fd = vb32.request_fd,
3201 /* zero out anything we don't copy from userspace */
3202 if (!err && n < _IOC_SIZE(real_cmd))
3203 memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
3207 static int video_put_user(void __user *arg, void *parg,
3208 unsigned int real_cmd, unsigned int cmd)
3210 if (!(_IOC_DIR(cmd) & _IOC_READ))
3213 if (cmd == real_cmd) {
3214 /* Copy results into user buffer */
3215 if (copy_to_user(arg, parg, _IOC_SIZE(cmd)))
3220 if (in_compat_syscall())
3221 return v4l2_compat_put_user(arg, parg, cmd);
3223 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3225 case VIDIOC_DQEVENT_TIME32: {
3226 struct v4l2_event *ev = parg;
3227 struct v4l2_event_time32 ev32;
3229 memset(&ev32, 0, sizeof(ev32));
3231 ev32.type = ev->type;
3232 ev32.pending = ev->pending;
3233 ev32.sequence = ev->sequence;
3234 ev32.timestamp.tv_sec = ev->timestamp.tv_sec;
3235 ev32.timestamp.tv_nsec = ev->timestamp.tv_nsec;
3238 memcpy(&ev32.u, &ev->u, sizeof(ev->u));
3239 memcpy(&ev32.reserved, &ev->reserved, sizeof(ev->reserved));
3241 if (copy_to_user(arg, &ev32, sizeof(ev32)))
3245 case VIDIOC_QUERYBUF_TIME32:
3246 case VIDIOC_QBUF_TIME32:
3247 case VIDIOC_DQBUF_TIME32:
3248 case VIDIOC_PREPARE_BUF_TIME32: {
3249 struct v4l2_buffer *vb = parg;
3250 struct v4l2_buffer_time32 vb32;
3252 memset(&vb32, 0, sizeof(vb32));
3254 vb32.index = vb->index;
3255 vb32.type = vb->type;
3256 vb32.bytesused = vb->bytesused;
3257 vb32.flags = vb->flags;
3258 vb32.field = vb->field;
3259 vb32.timestamp.tv_sec = vb->timestamp.tv_sec;
3260 vb32.timestamp.tv_usec = vb->timestamp.tv_usec;
3261 vb32.timecode = vb->timecode;
3262 vb32.sequence = vb->sequence;
3263 vb32.memory = vb->memory;
3264 vb32.m.userptr = vb->m.userptr;
3265 vb32.length = vb->length;
3266 vb32.request_fd = vb->request_fd;
3268 if (copy_to_user(arg, &vb32, sizeof(vb32)))
3279 video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
3283 void *mbuf = NULL, *array_buf = NULL;
3284 void *parg = (void *)arg;
3286 bool has_array_args;
3287 bool always_copy = false;
3288 size_t array_size = 0;
3289 void __user *user_ptr = NULL;
3290 void **kernel_ptr = NULL;
3291 unsigned int cmd = video_translate_cmd(orig_cmd);
3292 const size_t ioc_size = _IOC_SIZE(cmd);
3294 /* Copy arguments into temp kernel buffer */
3295 if (_IOC_DIR(cmd) != _IOC_NONE) {
3296 if (ioc_size <= sizeof(sbuf)) {
3299 /* too big to allocate from stack */
3300 mbuf = kmalloc(ioc_size, GFP_KERNEL);
3306 err = video_get_user((void __user *)arg, parg, cmd,
3307 orig_cmd, &always_copy);
3312 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3315 has_array_args = err;
3317 if (has_array_args) {
3318 array_buf = kvmalloc(array_size, GFP_KERNEL);
3320 if (array_buf == NULL)
3321 goto out_array_args;
3323 if (in_compat_syscall())
3324 err = v4l2_compat_get_array_args(file, array_buf,
3325 user_ptr, array_size,
3328 err = copy_from_user(array_buf, user_ptr, array_size) ?
3331 goto out_array_args;
3332 *kernel_ptr = array_buf;
3336 err = func(file, cmd, parg);
3337 if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3343 if (cmd == VIDIOC_DQBUF)
3344 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3345 else if (cmd == VIDIOC_QBUF)
3346 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3349 if (has_array_args) {
3350 *kernel_ptr = (void __force *)user_ptr;
3351 if (in_compat_syscall()) {
3354 put_err = v4l2_compat_put_array_args(file, user_ptr,
3360 } else if (copy_to_user(user_ptr, array_buf, array_size)) {
3363 goto out_array_args;
3366 * Some ioctls can return an error, but still have valid
3367 * results that must be returned.
3369 if (err < 0 && !always_copy)
3373 if (video_put_user((void __user *)arg, parg, cmd, orig_cmd))
3381 long video_ioctl2(struct file *file,
3382 unsigned int cmd, unsigned long arg)
3384 return video_usercopy(file, cmd, arg, __video_do_ioctl);
3386 EXPORT_SYMBOL(video_ioctl2);