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)
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/version.h>
18 #include <linux/videodev2.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-device.h>
26 #include <media/videobuf2-v4l2.h>
27 #include <media/v4l2-mc.h>
28 #include <media/v4l2-mem2mem.h>
30 #include <trace/events/v4l2.h>
32 /* Zero out the end of the struct pointed to by p. Everything after, but
33 * not including, the specified field is cleared. */
34 #define CLEAR_AFTER_FIELD(p, field) \
35 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
36 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
38 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
45 static const struct std_descr standards[] = {
46 { V4L2_STD_NTSC, "NTSC" },
47 { V4L2_STD_NTSC_M, "NTSC-M" },
48 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
49 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
50 { V4L2_STD_NTSC_443, "NTSC-443" },
51 { V4L2_STD_PAL, "PAL" },
52 { V4L2_STD_PAL_BG, "PAL-BG" },
53 { V4L2_STD_PAL_B, "PAL-B" },
54 { V4L2_STD_PAL_B1, "PAL-B1" },
55 { V4L2_STD_PAL_G, "PAL-G" },
56 { V4L2_STD_PAL_H, "PAL-H" },
57 { V4L2_STD_PAL_I, "PAL-I" },
58 { V4L2_STD_PAL_DK, "PAL-DK" },
59 { V4L2_STD_PAL_D, "PAL-D" },
60 { V4L2_STD_PAL_D1, "PAL-D1" },
61 { V4L2_STD_PAL_K, "PAL-K" },
62 { V4L2_STD_PAL_M, "PAL-M" },
63 { V4L2_STD_PAL_N, "PAL-N" },
64 { V4L2_STD_PAL_Nc, "PAL-Nc" },
65 { V4L2_STD_PAL_60, "PAL-60" },
66 { V4L2_STD_SECAM, "SECAM" },
67 { V4L2_STD_SECAM_B, "SECAM-B" },
68 { V4L2_STD_SECAM_G, "SECAM-G" },
69 { V4L2_STD_SECAM_H, "SECAM-H" },
70 { V4L2_STD_SECAM_DK, "SECAM-DK" },
71 { V4L2_STD_SECAM_D, "SECAM-D" },
72 { V4L2_STD_SECAM_K, "SECAM-K" },
73 { V4L2_STD_SECAM_K1, "SECAM-K1" },
74 { V4L2_STD_SECAM_L, "SECAM-L" },
75 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
79 /* video4linux standard ID conversion to standard name
81 const char *v4l2_norm_to_name(v4l2_std_id id)
86 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
87 64 bit comparisons. So, on that architecture, with some gcc
88 variants, compilation fails. Currently, the max value is 30bit wide.
92 for (i = 0; standards[i].std; i++)
93 if (myid == standards[i].std)
95 return standards[i].descr;
97 EXPORT_SYMBOL(v4l2_norm_to_name);
99 /* Returns frame period for the given standard */
100 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
102 if (id & V4L2_STD_525_60) {
103 frameperiod->numerator = 1001;
104 frameperiod->denominator = 30000;
106 frameperiod->numerator = 1;
107 frameperiod->denominator = 25;
110 EXPORT_SYMBOL(v4l2_video_std_frame_period);
112 /* Fill in the fields of a v4l2_standard structure according to the
113 'id' and 'transmission' parameters. Returns negative on error. */
114 int v4l2_video_std_construct(struct v4l2_standard *vs,
115 int id, const char *name)
118 v4l2_video_std_frame_period(id, &vs->frameperiod);
119 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
120 strscpy(vs->name, name, sizeof(vs->name));
123 EXPORT_SYMBOL(v4l2_video_std_construct);
125 /* Fill in the fields of a v4l2_standard structure according to the
126 * 'id' and 'vs->index' parameters. Returns negative on error. */
127 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
129 v4l2_std_id curr_id = 0;
130 unsigned int index = vs->index, i, j = 0;
131 const char *descr = "";
133 /* Return -ENODATA if the id for the current input
134 or output is 0, meaning that it doesn't support this API. */
138 /* Return norm array in a canonical way */
139 for (i = 0; i <= index && id; i++) {
140 /* last std value in the standards array is 0, so this
141 while always ends there since (id & 0) == 0. */
142 while ((id & standards[j].std) != standards[j].std)
144 curr_id = standards[j].std;
145 descr = standards[j].descr;
149 if (curr_id != V4L2_STD_PAL &&
150 curr_id != V4L2_STD_SECAM &&
151 curr_id != V4L2_STD_NTSC)
157 v4l2_video_std_construct(vs, curr_id, descr);
161 /* ----------------------------------------------------------------- */
162 /* some arrays for pretty-printing debug messages of enum types */
164 const char *v4l2_field_names[] = {
165 [V4L2_FIELD_ANY] = "any",
166 [V4L2_FIELD_NONE] = "none",
167 [V4L2_FIELD_TOP] = "top",
168 [V4L2_FIELD_BOTTOM] = "bottom",
169 [V4L2_FIELD_INTERLACED] = "interlaced",
170 [V4L2_FIELD_SEQ_TB] = "seq-tb",
171 [V4L2_FIELD_SEQ_BT] = "seq-bt",
172 [V4L2_FIELD_ALTERNATE] = "alternate",
173 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
174 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
176 EXPORT_SYMBOL(v4l2_field_names);
178 const char *v4l2_type_names[] = {
180 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
181 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
182 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
183 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
184 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
185 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
186 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
187 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
188 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
189 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
190 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
191 [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
192 [V4L2_BUF_TYPE_META_CAPTURE] = "meta-cap",
193 [V4L2_BUF_TYPE_META_OUTPUT] = "meta-out",
195 EXPORT_SYMBOL(v4l2_type_names);
197 static const char *v4l2_memory_names[] = {
198 [V4L2_MEMORY_MMAP] = "mmap",
199 [V4L2_MEMORY_USERPTR] = "userptr",
200 [V4L2_MEMORY_OVERLAY] = "overlay",
201 [V4L2_MEMORY_DMABUF] = "dmabuf",
204 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
206 /* ------------------------------------------------------------------ */
207 /* debug help functions */
209 static void v4l_print_querycap(const void *arg, bool write_only)
211 const struct v4l2_capability *p = arg;
213 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
214 (int)sizeof(p->driver), p->driver,
215 (int)sizeof(p->card), p->card,
216 (int)sizeof(p->bus_info), p->bus_info,
217 p->version, p->capabilities, p->device_caps);
220 static void v4l_print_enuminput(const void *arg, bool write_only)
222 const struct v4l2_input *p = arg;
224 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
225 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
226 p->tuner, (unsigned long long)p->std, p->status,
230 static void v4l_print_enumoutput(const void *arg, bool write_only)
232 const struct v4l2_output *p = arg;
234 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
235 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
236 p->modulator, (unsigned long long)p->std, p->capabilities);
239 static void v4l_print_audio(const void *arg, bool write_only)
241 const struct v4l2_audio *p = arg;
244 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
246 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
247 p->index, (int)sizeof(p->name), p->name,
248 p->capability, p->mode);
251 static void v4l_print_audioout(const void *arg, bool write_only)
253 const struct v4l2_audioout *p = arg;
256 pr_cont("index=%u\n", p->index);
258 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
259 p->index, (int)sizeof(p->name), p->name,
260 p->capability, p->mode);
263 static void v4l_print_fmtdesc(const void *arg, bool write_only)
265 const struct v4l2_fmtdesc *p = arg;
267 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, mbus_code=0x%04x, description='%.*s'\n",
268 p->index, prt_names(p->type, v4l2_type_names),
269 p->flags, (p->pixelformat & 0xff),
270 (p->pixelformat >> 8) & 0xff,
271 (p->pixelformat >> 16) & 0xff,
272 (p->pixelformat >> 24) & 0xff,
274 (int)sizeof(p->description), p->description);
277 static void v4l_print_format(const void *arg, bool write_only)
279 const struct v4l2_format *p = arg;
280 const struct v4l2_pix_format *pix;
281 const struct v4l2_pix_format_mplane *mp;
282 const struct v4l2_vbi_format *vbi;
283 const struct v4l2_sliced_vbi_format *sliced;
284 const struct v4l2_window *win;
285 const struct v4l2_sdr_format *sdr;
286 const struct v4l2_meta_format *meta;
290 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
292 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
293 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
295 pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
296 pix->width, pix->height,
297 (pix->pixelformat & 0xff),
298 (pix->pixelformat >> 8) & 0xff,
299 (pix->pixelformat >> 16) & 0xff,
300 (pix->pixelformat >> 24) & 0xff,
301 prt_names(pix->field, v4l2_field_names),
302 pix->bytesperline, pix->sizeimage,
303 pix->colorspace, pix->flags, pix->ycbcr_enc,
304 pix->quantization, pix->xfer_func);
306 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
307 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
309 pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
310 mp->width, mp->height,
311 (mp->pixelformat & 0xff),
312 (mp->pixelformat >> 8) & 0xff,
313 (mp->pixelformat >> 16) & 0xff,
314 (mp->pixelformat >> 24) & 0xff,
315 prt_names(mp->field, v4l2_field_names),
316 mp->colorspace, mp->num_planes, mp->flags,
317 mp->ycbcr_enc, mp->quantization, mp->xfer_func);
318 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
319 for (i = 0; i < planes; i++)
320 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
321 mp->plane_fmt[i].bytesperline,
322 mp->plane_fmt[i].sizeimage);
324 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
325 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
327 /* Note: we can't print the clip list here since the clips
328 * pointer is a userspace pointer, not a kernelspace
330 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",
331 win->w.width, win->w.height, win->w.left, win->w.top,
332 prt_names(win->field, v4l2_field_names),
333 win->chromakey, win->clipcount, win->clips,
334 win->bitmap, win->global_alpha);
336 case V4L2_BUF_TYPE_VBI_CAPTURE:
337 case V4L2_BUF_TYPE_VBI_OUTPUT:
339 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
340 vbi->sampling_rate, vbi->offset,
341 vbi->samples_per_line,
342 (vbi->sample_format & 0xff),
343 (vbi->sample_format >> 8) & 0xff,
344 (vbi->sample_format >> 16) & 0xff,
345 (vbi->sample_format >> 24) & 0xff,
346 vbi->start[0], vbi->start[1],
347 vbi->count[0], vbi->count[1]);
349 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
350 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
351 sliced = &p->fmt.sliced;
352 pr_cont(", service_set=0x%08x, io_size=%d\n",
353 sliced->service_set, sliced->io_size);
354 for (i = 0; i < 24; i++)
355 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
356 sliced->service_lines[0][i],
357 sliced->service_lines[1][i]);
359 case V4L2_BUF_TYPE_SDR_CAPTURE:
360 case V4L2_BUF_TYPE_SDR_OUTPUT:
362 pr_cont(", pixelformat=%c%c%c%c\n",
363 (sdr->pixelformat >> 0) & 0xff,
364 (sdr->pixelformat >> 8) & 0xff,
365 (sdr->pixelformat >> 16) & 0xff,
366 (sdr->pixelformat >> 24) & 0xff);
368 case V4L2_BUF_TYPE_META_CAPTURE:
369 case V4L2_BUF_TYPE_META_OUTPUT:
371 pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
372 (meta->dataformat >> 0) & 0xff,
373 (meta->dataformat >> 8) & 0xff,
374 (meta->dataformat >> 16) & 0xff,
375 (meta->dataformat >> 24) & 0xff,
381 static void v4l_print_framebuffer(const void *arg, bool write_only)
383 const struct v4l2_framebuffer *p = arg;
385 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
386 p->capability, p->flags, p->base,
387 p->fmt.width, p->fmt.height,
388 (p->fmt.pixelformat & 0xff),
389 (p->fmt.pixelformat >> 8) & 0xff,
390 (p->fmt.pixelformat >> 16) & 0xff,
391 (p->fmt.pixelformat >> 24) & 0xff,
392 p->fmt.bytesperline, p->fmt.sizeimage,
396 static void v4l_print_buftype(const void *arg, bool write_only)
398 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
401 static void v4l_print_modulator(const void *arg, bool write_only)
403 const struct v4l2_modulator *p = arg;
406 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
408 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
409 p->index, (int)sizeof(p->name), p->name, p->capability,
410 p->rangelow, p->rangehigh, p->txsubchans);
413 static void v4l_print_tuner(const void *arg, bool write_only)
415 const struct v4l2_tuner *p = arg;
418 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
420 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",
421 p->index, (int)sizeof(p->name), p->name, p->type,
422 p->capability, p->rangelow,
423 p->rangehigh, p->signal, p->afc,
424 p->rxsubchans, p->audmode);
427 static void v4l_print_frequency(const void *arg, bool write_only)
429 const struct v4l2_frequency *p = arg;
431 pr_cont("tuner=%u, type=%u, frequency=%u\n",
432 p->tuner, p->type, p->frequency);
435 static void v4l_print_standard(const void *arg, bool write_only)
437 const struct v4l2_standard *p = arg;
439 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
441 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
442 p->frameperiod.numerator,
443 p->frameperiod.denominator,
447 static void v4l_print_std(const void *arg, bool write_only)
449 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
452 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
454 const struct v4l2_hw_freq_seek *p = arg;
456 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
457 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
458 p->rangelow, p->rangehigh);
461 static void v4l_print_requestbuffers(const void *arg, bool write_only)
463 const struct v4l2_requestbuffers *p = arg;
465 pr_cont("count=%d, type=%s, memory=%s\n",
467 prt_names(p->type, v4l2_type_names),
468 prt_names(p->memory, v4l2_memory_names));
471 static void v4l_print_buffer(const void *arg, bool write_only)
473 const struct v4l2_buffer *p = arg;
474 const struct v4l2_timecode *tc = &p->timecode;
475 const struct v4l2_plane *plane;
478 pr_cont("%02d:%02d:%02d.%09ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
479 (int)p->timestamp.tv_sec / 3600,
480 ((int)p->timestamp.tv_sec / 60) % 60,
481 ((int)p->timestamp.tv_sec % 60),
482 (long)p->timestamp.tv_usec,
484 prt_names(p->type, v4l2_type_names), p->request_fd,
485 p->flags, prt_names(p->field, v4l2_field_names),
486 p->sequence, prt_names(p->memory, v4l2_memory_names));
488 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
490 for (i = 0; i < p->length; ++i) {
491 plane = &p->m.planes[i];
493 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
494 i, plane->bytesused, plane->data_offset,
495 plane->m.userptr, plane->length);
498 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
499 p->bytesused, p->m.userptr, p->length);
502 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
503 tc->hours, tc->minutes, tc->seconds,
504 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
507 static void v4l_print_exportbuffer(const void *arg, bool write_only)
509 const struct v4l2_exportbuffer *p = arg;
511 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
512 p->fd, prt_names(p->type, v4l2_type_names),
513 p->index, p->plane, p->flags);
516 static void v4l_print_create_buffers(const void *arg, bool write_only)
518 const struct v4l2_create_buffers *p = arg;
520 pr_cont("index=%d, count=%d, memory=%s, ",
522 prt_names(p->memory, v4l2_memory_names));
523 v4l_print_format(&p->format, write_only);
526 static void v4l_print_streamparm(const void *arg, bool write_only)
528 const struct v4l2_streamparm *p = arg;
530 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
532 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
533 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
534 const struct v4l2_captureparm *c = &p->parm.capture;
536 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
537 c->capability, c->capturemode,
538 c->timeperframe.numerator, c->timeperframe.denominator,
539 c->extendedmode, c->readbuffers);
540 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
541 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
542 const struct v4l2_outputparm *c = &p->parm.output;
544 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
545 c->capability, c->outputmode,
546 c->timeperframe.numerator, c->timeperframe.denominator,
547 c->extendedmode, c->writebuffers);
553 static void v4l_print_queryctrl(const void *arg, bool write_only)
555 const struct v4l2_queryctrl *p = arg;
557 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
558 p->id, p->type, (int)sizeof(p->name), p->name,
559 p->minimum, p->maximum,
560 p->step, p->default_value, p->flags);
563 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
565 const struct v4l2_query_ext_ctrl *p = arg;
567 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",
568 p->id, p->type, (int)sizeof(p->name), p->name,
569 p->minimum, p->maximum,
570 p->step, p->default_value, p->flags,
571 p->elem_size, p->elems, p->nr_of_dims,
572 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
575 static void v4l_print_querymenu(const void *arg, bool write_only)
577 const struct v4l2_querymenu *p = arg;
579 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
582 static void v4l_print_control(const void *arg, bool write_only)
584 const struct v4l2_control *p = arg;
585 const char *name = v4l2_ctrl_get_name(p->id);
588 pr_cont("name=%s, ", name);
589 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
592 static void v4l_print_ext_controls(const void *arg, bool write_only)
594 const struct v4l2_ext_controls *p = arg;
597 pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
598 p->which, p->count, p->error_idx, p->request_fd);
599 for (i = 0; i < p->count; i++) {
600 unsigned int id = p->controls[i].id;
601 const char *name = v4l2_ctrl_get_name(id);
604 pr_cont(", name=%s", name);
605 if (!p->controls[i].size)
606 pr_cont(", id/val=0x%x/0x%x", id, p->controls[i].value);
608 pr_cont(", id/size=0x%x/%u", id, p->controls[i].size);
613 static void v4l_print_cropcap(const void *arg, bool write_only)
615 const struct v4l2_cropcap *p = arg;
617 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
618 prt_names(p->type, v4l2_type_names),
619 p->bounds.width, p->bounds.height,
620 p->bounds.left, p->bounds.top,
621 p->defrect.width, p->defrect.height,
622 p->defrect.left, p->defrect.top,
623 p->pixelaspect.numerator, p->pixelaspect.denominator);
626 static void v4l_print_crop(const void *arg, bool write_only)
628 const struct v4l2_crop *p = arg;
630 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
631 prt_names(p->type, v4l2_type_names),
632 p->c.width, p->c.height,
633 p->c.left, p->c.top);
636 static void v4l_print_selection(const void *arg, bool write_only)
638 const struct v4l2_selection *p = arg;
640 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
641 prt_names(p->type, v4l2_type_names),
643 p->r.width, p->r.height, p->r.left, p->r.top);
646 static void v4l_print_jpegcompression(const void *arg, bool write_only)
648 const struct v4l2_jpegcompression *p = arg;
650 pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
651 p->quality, p->APPn, p->APP_len,
652 p->COM_len, p->jpeg_markers);
655 static void v4l_print_enc_idx(const void *arg, bool write_only)
657 const struct v4l2_enc_idx *p = arg;
659 pr_cont("entries=%d, entries_cap=%d\n",
660 p->entries, p->entries_cap);
663 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
665 const struct v4l2_encoder_cmd *p = arg;
667 pr_cont("cmd=%d, flags=0x%x\n",
671 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
673 const struct v4l2_decoder_cmd *p = arg;
675 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
677 if (p->cmd == V4L2_DEC_CMD_START)
678 pr_info("speed=%d, format=%u\n",
679 p->start.speed, p->start.format);
680 else if (p->cmd == V4L2_DEC_CMD_STOP)
681 pr_info("pts=%llu\n", p->stop.pts);
684 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
686 const struct v4l2_dbg_chip_info *p = arg;
688 pr_cont("type=%u, ", p->match.type);
689 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
690 pr_cont("name=%.*s, ",
691 (int)sizeof(p->match.name), p->match.name);
693 pr_cont("addr=%u, ", p->match.addr);
694 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
697 static void v4l_print_dbg_register(const void *arg, bool write_only)
699 const struct v4l2_dbg_register *p = arg;
701 pr_cont("type=%u, ", p->match.type);
702 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
703 pr_cont("name=%.*s, ",
704 (int)sizeof(p->match.name), p->match.name);
706 pr_cont("addr=%u, ", p->match.addr);
707 pr_cont("reg=0x%llx, val=0x%llx\n",
711 static void v4l_print_dv_timings(const void *arg, bool write_only)
713 const struct v4l2_dv_timings *p = arg;
716 case V4L2_DV_BT_656_1120:
717 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",
718 p->bt.interlaced, p->bt.pixelclock,
719 p->bt.width, p->bt.height,
720 p->bt.polarities, p->bt.hfrontporch,
721 p->bt.hsync, p->bt.hbackporch,
722 p->bt.vfrontporch, p->bt.vsync,
723 p->bt.vbackporch, p->bt.il_vfrontporch,
724 p->bt.il_vsync, p->bt.il_vbackporch,
725 p->bt.standards, p->bt.flags);
728 pr_cont("type=%d\n", p->type);
733 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
735 const struct v4l2_enum_dv_timings *p = arg;
737 pr_cont("index=%u, ", p->index);
738 v4l_print_dv_timings(&p->timings, write_only);
741 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
743 const struct v4l2_dv_timings_cap *p = arg;
746 case V4L2_DV_BT_656_1120:
747 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
748 p->bt.min_width, p->bt.max_width,
749 p->bt.min_height, p->bt.max_height,
750 p->bt.min_pixelclock, p->bt.max_pixelclock,
751 p->bt.standards, p->bt.capabilities);
754 pr_cont("type=%u\n", p->type);
759 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
761 const struct v4l2_frmsizeenum *p = arg;
763 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
765 (p->pixel_format & 0xff),
766 (p->pixel_format >> 8) & 0xff,
767 (p->pixel_format >> 16) & 0xff,
768 (p->pixel_format >> 24) & 0xff,
771 case V4L2_FRMSIZE_TYPE_DISCRETE:
772 pr_cont(", wxh=%ux%u\n",
773 p->discrete.width, p->discrete.height);
775 case V4L2_FRMSIZE_TYPE_STEPWISE:
776 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
777 p->stepwise.min_width,
778 p->stepwise.min_height,
779 p->stepwise.max_width,
780 p->stepwise.max_height,
781 p->stepwise.step_width,
782 p->stepwise.step_height);
784 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
791 static void v4l_print_frmivalenum(const void *arg, bool write_only)
793 const struct v4l2_frmivalenum *p = arg;
795 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
797 (p->pixel_format & 0xff),
798 (p->pixel_format >> 8) & 0xff,
799 (p->pixel_format >> 16) & 0xff,
800 (p->pixel_format >> 24) & 0xff,
801 p->width, p->height, p->type);
803 case V4L2_FRMIVAL_TYPE_DISCRETE:
804 pr_cont(", fps=%d/%d\n",
805 p->discrete.numerator,
806 p->discrete.denominator);
808 case V4L2_FRMIVAL_TYPE_STEPWISE:
809 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
810 p->stepwise.min.numerator,
811 p->stepwise.min.denominator,
812 p->stepwise.max.numerator,
813 p->stepwise.max.denominator,
814 p->stepwise.step.numerator,
815 p->stepwise.step.denominator);
817 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
824 static void v4l_print_event(const void *arg, bool write_only)
826 const struct v4l2_event *p = arg;
827 const struct v4l2_event_ctrl *c;
829 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%llu.%9.9llu\n",
830 p->type, p->pending, p->sequence, p->id,
831 p->timestamp.tv_sec, p->timestamp.tv_nsec);
833 case V4L2_EVENT_VSYNC:
834 printk(KERN_DEBUG "field=%s\n",
835 prt_names(p->u.vsync.field, v4l2_field_names));
837 case V4L2_EVENT_CTRL:
839 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
840 c->changes, c->type);
841 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
842 pr_cont("value64=%lld, ", c->value64);
844 pr_cont("value=%d, ", c->value);
845 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
846 c->flags, c->minimum, c->maximum,
847 c->step, c->default_value);
849 case V4L2_EVENT_FRAME_SYNC:
850 pr_cont("frame_sequence=%u\n",
851 p->u.frame_sync.frame_sequence);
856 static void v4l_print_event_subscription(const void *arg, bool write_only)
858 const struct v4l2_event_subscription *p = arg;
860 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
861 p->type, p->id, p->flags);
864 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
866 const struct v4l2_sliced_vbi_cap *p = arg;
869 pr_cont("type=%s, service_set=0x%08x\n",
870 prt_names(p->type, v4l2_type_names), p->service_set);
871 for (i = 0; i < 24; i++)
872 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
873 p->service_lines[0][i],
874 p->service_lines[1][i]);
877 static void v4l_print_freq_band(const void *arg, bool write_only)
879 const struct v4l2_frequency_band *p = arg;
881 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
882 p->tuner, p->type, p->index,
883 p->capability, p->rangelow,
884 p->rangehigh, p->modulation);
887 static void v4l_print_edid(const void *arg, bool write_only)
889 const struct v4l2_edid *p = arg;
891 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
892 p->pad, p->start_block, p->blocks);
895 static void v4l_print_u32(const void *arg, bool write_only)
897 pr_cont("value=%u\n", *(const u32 *)arg);
900 static void v4l_print_newline(const void *arg, bool write_only)
905 static void v4l_print_default(const void *arg, bool write_only)
907 pr_cont("driver-specific ioctl\n");
910 static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
914 /* zero the reserved fields */
916 for (i = 0; i < c->count; i++)
917 c->controls[i].reserved2[0] = 0;
920 case V4L2_CID_PRIVATE_BASE:
922 * V4L2_CID_PRIVATE_BASE cannot be used as control class
923 * when using extended controls.
924 * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
925 * is it allowed for backwards compatibility.
927 if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
930 case V4L2_CTRL_WHICH_DEF_VAL:
931 /* Default value cannot be changed */
932 if (ioctl == VIDIOC_S_EXT_CTRLS ||
933 ioctl == VIDIOC_TRY_EXT_CTRLS) {
934 c->error_idx = c->count;
938 case V4L2_CTRL_WHICH_CUR_VAL:
940 case V4L2_CTRL_WHICH_REQUEST_VAL:
941 c->error_idx = c->count;
945 /* Check that all controls are from the same control class. */
946 for (i = 0; i < c->count; i++) {
947 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
948 c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
956 static int check_fmt(struct file *file, enum v4l2_buf_type type)
958 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
959 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
960 V4L2_CAP_VIDEO_OUTPUT |
961 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
962 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
963 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
964 V4L2_CAP_META_OUTPUT;
965 struct video_device *vfd = video_devdata(file);
966 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
967 bool is_vid = vfd->vfl_type == VFL_TYPE_VIDEO &&
968 (vfd->device_caps & vid_caps);
969 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
970 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
971 bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
972 bool is_meta = vfd->vfl_type == VFL_TYPE_VIDEO &&
973 (vfd->device_caps & meta_caps);
974 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
975 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
981 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
982 if ((is_vid || is_tch) && is_rx &&
983 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
986 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
987 if ((is_vid || is_tch) && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
990 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
991 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
994 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
995 if (is_vid && is_tx &&
996 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
999 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1000 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
1003 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1004 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
1007 case V4L2_BUF_TYPE_VBI_CAPTURE:
1008 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
1011 case V4L2_BUF_TYPE_VBI_OUTPUT:
1012 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
1015 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1016 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
1019 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1020 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
1023 case V4L2_BUF_TYPE_SDR_CAPTURE:
1024 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
1027 case V4L2_BUF_TYPE_SDR_OUTPUT:
1028 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
1031 case V4L2_BUF_TYPE_META_CAPTURE:
1032 if (is_meta && is_rx && ops->vidioc_g_fmt_meta_cap)
1035 case V4L2_BUF_TYPE_META_OUTPUT:
1036 if (is_meta && is_tx && ops->vidioc_g_fmt_meta_out)
1045 static void v4l_sanitize_format(struct v4l2_format *fmt)
1047 unsigned int offset;
1049 /* Make sure num_planes is not bogus */
1050 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1051 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1052 fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1056 * The v4l2_pix_format structure has been extended with fields that were
1057 * not previously required to be set to zero by applications. The priv
1058 * field, when set to a magic value, indicates the the extended fields
1059 * are valid. Otherwise they will contain undefined values. To simplify
1060 * the API towards drivers zero the extended fields and set the priv
1061 * field to the magic value when the extended pixel format structure
1062 * isn't used by applications.
1065 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1066 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1069 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1072 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1074 offset = offsetof(struct v4l2_pix_format, priv)
1075 + sizeof(fmt->fmt.pix.priv);
1076 memset(((void *)&fmt->fmt.pix) + offset, 0,
1077 sizeof(fmt->fmt.pix) - offset);
1080 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1081 struct file *file, void *fh, void *arg)
1083 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1084 struct video_device *vfd = video_devdata(file);
1087 cap->version = LINUX_VERSION_CODE;
1088 cap->device_caps = vfd->device_caps;
1089 cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1091 ret = ops->vidioc_querycap(file, fh, cap);
1094 * Drivers must not change device_caps, so check for this and
1095 * warn if this happened.
1097 WARN_ON(cap->device_caps != vfd->device_caps);
1099 * Check that capabilities is a superset of
1100 * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1102 WARN_ON((cap->capabilities &
1103 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1104 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1105 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1106 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1111 static int v4l_g_input(const struct v4l2_ioctl_ops *ops,
1112 struct file *file, void *fh, void *arg)
1114 struct video_device *vfd = video_devdata(file);
1116 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1121 return ops->vidioc_g_input(file, fh, arg);
1124 static int v4l_g_output(const struct v4l2_ioctl_ops *ops,
1125 struct file *file, void *fh, void *arg)
1127 struct video_device *vfd = video_devdata(file);
1129 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1134 return ops->vidioc_g_output(file, fh, arg);
1137 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1138 struct file *file, void *fh, void *arg)
1140 struct video_device *vfd = video_devdata(file);
1143 ret = v4l_enable_media_source(vfd);
1147 if (vfd->device_caps & V4L2_CAP_IO_MC)
1148 return *(int *)arg ? -EINVAL : 0;
1150 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1153 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1154 struct file *file, void *fh, void *arg)
1156 struct video_device *vfd = video_devdata(file);
1158 if (vfd->device_caps & V4L2_CAP_IO_MC)
1159 return *(int *)arg ? -EINVAL : 0;
1161 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1164 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1165 struct file *file, void *fh, void *arg)
1167 struct video_device *vfd;
1170 vfd = video_devdata(file);
1171 *p = v4l2_prio_max(vfd->prio);
1175 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1176 struct file *file, void *fh, void *arg)
1178 struct video_device *vfd;
1179 struct v4l2_fh *vfh;
1182 vfd = video_devdata(file);
1183 if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1185 vfh = file->private_data;
1186 return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1189 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1190 struct file *file, void *fh, void *arg)
1192 struct video_device *vfd = video_devdata(file);
1193 struct v4l2_input *p = arg;
1196 * We set the flags for CAP_DV_TIMINGS &
1197 * CAP_STD here based on ioctl handler provided by the
1198 * driver. If the driver doesn't support these
1199 * for a specific input, it must override these flags.
1201 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1202 p->capabilities |= V4L2_IN_CAP_STD;
1204 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1207 strscpy(p->name, vfd->name, sizeof(p->name));
1208 p->type = V4L2_INPUT_TYPE_CAMERA;
1212 return ops->vidioc_enum_input(file, fh, p);
1215 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1216 struct file *file, void *fh, void *arg)
1218 struct video_device *vfd = video_devdata(file);
1219 struct v4l2_output *p = arg;
1222 * We set the flags for CAP_DV_TIMINGS &
1223 * CAP_STD here based on ioctl handler provided by the
1224 * driver. If the driver doesn't support these
1225 * for a specific output, it must override these flags.
1227 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1228 p->capabilities |= V4L2_OUT_CAP_STD;
1230 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1233 strscpy(p->name, vfd->name, sizeof(p->name));
1234 p->type = V4L2_OUTPUT_TYPE_ANALOG;
1238 return ops->vidioc_enum_output(file, fh, p);
1241 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1243 const unsigned sz = sizeof(fmt->description);
1244 const char *descr = NULL;
1248 * We depart from the normal coding style here since the descriptions
1249 * should be aligned so it is easy to see which descriptions will be
1250 * longer than 31 characters (the max length for a description).
1251 * And frankly, this is easier to read anyway.
1253 * Note that gcc will use O(log N) comparisons to find the right case.
1255 switch (fmt->pixelformat) {
1256 /* Max description length mask: descr = "0123456789012345678901234567890" */
1257 case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
1258 case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
1259 case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
1260 case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
1261 case V4L2_PIX_FMT_RGBA444: descr = "16-bit RGBA 4-4-4-4"; break;
1262 case V4L2_PIX_FMT_RGBX444: descr = "16-bit RGBX 4-4-4-4"; break;
1263 case V4L2_PIX_FMT_ABGR444: descr = "16-bit ABGR 4-4-4-4"; break;
1264 case V4L2_PIX_FMT_XBGR444: descr = "16-bit XBGR 4-4-4-4"; break;
1265 case V4L2_PIX_FMT_BGRA444: descr = "16-bit BGRA 4-4-4-4"; break;
1266 case V4L2_PIX_FMT_BGRX444: descr = "16-bit BGRX 4-4-4-4"; break;
1267 case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
1268 case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
1269 case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
1270 case V4L2_PIX_FMT_ABGR555: descr = "16-bit ABGR 1-5-5-5"; break;
1271 case V4L2_PIX_FMT_XBGR555: descr = "16-bit XBGR 1-5-5-5"; break;
1272 case V4L2_PIX_FMT_RGBA555: descr = "16-bit RGBA 5-5-5-1"; break;
1273 case V4L2_PIX_FMT_RGBX555: descr = "16-bit RGBX 5-5-5-1"; break;
1274 case V4L2_PIX_FMT_BGRA555: descr = "16-bit BGRA 5-5-5-1"; break;
1275 case V4L2_PIX_FMT_BGRX555: descr = "16-bit BGRX 5-5-5-1"; break;
1276 case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
1277 case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1278 case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
1279 case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
1280 case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
1281 case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
1282 case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
1283 case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
1284 case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
1285 case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
1286 case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
1287 case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
1288 case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
1289 case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
1290 case V4L2_PIX_FMT_BGRA32: descr = "32-bit ABGR 8-8-8-8"; break;
1291 case V4L2_PIX_FMT_BGRX32: descr = "32-bit XBGR 8-8-8-8"; break;
1292 case V4L2_PIX_FMT_RGBA32: descr = "32-bit RGBA 8-8-8-8"; break;
1293 case V4L2_PIX_FMT_RGBX32: descr = "32-bit RGBX 8-8-8-8"; break;
1294 case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
1295 case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
1296 case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
1297 case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
1298 case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
1299 case V4L2_PIX_FMT_Y14: descr = "14-bit Greyscale"; break;
1300 case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
1301 case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
1302 case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
1303 case V4L2_PIX_FMT_Y10P: descr = "10-bit Greyscale (MIPI Packed)"; break;
1304 case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
1305 case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
1306 case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
1307 case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
1308 case V4L2_PIX_FMT_CNF4: descr = "4-bit Depth Confidence (Packed)"; break;
1309 case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
1310 case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
1311 case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
1312 case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
1313 case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
1314 case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
1315 case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
1316 case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
1317 case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
1318 case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
1319 case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
1320 case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
1321 case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
1322 case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
1323 case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
1324 case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
1325 case V4L2_PIX_FMT_AYUV32: descr = "32-bit AYUV 8-8-8-8"; break;
1326 case V4L2_PIX_FMT_XYUV32: descr = "32-bit XYUV 8-8-8-8"; break;
1327 case V4L2_PIX_FMT_VUYA32: descr = "32-bit VUYA 8-8-8-8"; break;
1328 case V4L2_PIX_FMT_VUYX32: descr = "32-bit VUYX 8-8-8-8"; break;
1329 case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
1330 case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
1331 case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
1332 case V4L2_PIX_FMT_HM12: descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
1333 case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
1334 case V4L2_PIX_FMT_NV12: descr = "Y/CbCr 4:2:0"; break;
1335 case V4L2_PIX_FMT_NV21: descr = "Y/CrCb 4:2:0"; break;
1336 case V4L2_PIX_FMT_NV16: descr = "Y/CbCr 4:2:2"; break;
1337 case V4L2_PIX_FMT_NV61: descr = "Y/CrCb 4:2:2"; break;
1338 case V4L2_PIX_FMT_NV24: descr = "Y/CbCr 4:4:4"; break;
1339 case V4L2_PIX_FMT_NV42: descr = "Y/CrCb 4:4:4"; break;
1340 case V4L2_PIX_FMT_NV12M: descr = "Y/CbCr 4:2:0 (N-C)"; break;
1341 case V4L2_PIX_FMT_NV21M: descr = "Y/CrCb 4:2:0 (N-C)"; break;
1342 case V4L2_PIX_FMT_NV16M: descr = "Y/CbCr 4:2:2 (N-C)"; break;
1343 case V4L2_PIX_FMT_NV61M: descr = "Y/CrCb 4:2:2 (N-C)"; break;
1344 case V4L2_PIX_FMT_NV12MT: descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1345 case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1346 case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
1347 case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
1348 case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
1349 case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
1350 case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
1351 case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
1352 case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
1353 case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
1354 case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
1355 case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
1356 case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
1357 case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
1358 case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
1359 case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
1360 case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1361 case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1362 case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1363 case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1364 case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1365 case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1366 case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1367 case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1368 case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1369 case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1370 case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1371 case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1372 case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1373 case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1374 case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1375 case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1376 case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
1377 case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
1378 case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
1379 case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
1380 case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1381 case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1382 case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1383 case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1384 case V4L2_PIX_FMT_SBGGR14: descr = "14-bit Bayer BGBG/GRGR"; break;
1385 case V4L2_PIX_FMT_SGBRG14: descr = "14-bit Bayer GBGB/RGRG"; break;
1386 case V4L2_PIX_FMT_SGRBG14: descr = "14-bit Bayer GRGR/BGBG"; break;
1387 case V4L2_PIX_FMT_SRGGB14: descr = "14-bit Bayer RGRG/GBGB"; break;
1388 case V4L2_PIX_FMT_SBGGR14P: descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1389 case V4L2_PIX_FMT_SGBRG14P: descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1390 case V4L2_PIX_FMT_SGRBG14P: descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1391 case V4L2_PIX_FMT_SRGGB14P: descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1392 case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
1393 case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
1394 case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
1395 case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
1396 case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1397 case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
1398 case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
1399 case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
1400 case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
1401 case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
1402 case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
1403 case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
1404 case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
1405 case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
1406 case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
1407 case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
1408 case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
1409 case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
1410 case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
1411 case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
1412 case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
1413 case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
1414 case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit Signed Deltas"; break;
1415 case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit Signed Deltas"; break;
1416 case V4L2_TCH_FMT_TU16: descr = "16-bit Unsigned Touch Data"; break;
1417 case V4L2_TCH_FMT_TU08: descr = "8-bit Unsigned Touch Data"; break;
1418 case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
1419 case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
1420 case V4L2_META_FMT_UVC: descr = "UVC Payload Header Metadata"; break;
1421 case V4L2_META_FMT_D4XX: descr = "Intel D4xx UVC Metadata"; break;
1422 case V4L2_META_FMT_VIVID: descr = "Vivid Metadata"; break;
1425 /* Compressed formats */
1426 flags = V4L2_FMT_FLAG_COMPRESSED;
1427 switch (fmt->pixelformat) {
1428 /* Max description length mask: descr = "0123456789012345678901234567890" */
1429 case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
1430 case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
1431 case V4L2_PIX_FMT_DV: descr = "1394"; break;
1432 case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
1433 case V4L2_PIX_FMT_H264: descr = "H.264"; break;
1434 case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
1435 case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
1436 case V4L2_PIX_FMT_H264_SLICE: descr = "H.264 Parsed Slice Data"; break;
1437 case V4L2_PIX_FMT_H263: descr = "H.263"; break;
1438 case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
1439 case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
1440 case V4L2_PIX_FMT_MPEG2_SLICE: descr = "MPEG-2 Parsed Slice Data"; break;
1441 case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 Part 2 ES"; break;
1442 case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
1443 case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
1444 case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
1445 case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
1446 case V4L2_PIX_FMT_VP8_FRAME: descr = "VP8 Frame"; break;
1447 case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
1448 case V4L2_PIX_FMT_HEVC: descr = "HEVC"; break; /* aka H.265 */
1449 case V4L2_PIX_FMT_HEVC_SLICE: descr = "HEVC Parsed Slice Data"; break;
1450 case V4L2_PIX_FMT_FWHT: descr = "FWHT"; break; /* used in vicodec */
1451 case V4L2_PIX_FMT_FWHT_STATELESS: descr = "FWHT Stateless"; break; /* used in vicodec */
1452 case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
1453 case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
1454 case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
1455 case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
1456 case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
1457 case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
1458 case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
1459 case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
1460 case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
1461 case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
1462 case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
1463 case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
1464 case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
1465 case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
1466 case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
1467 case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
1468 case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
1469 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1470 case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
1471 case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
1473 if (fmt->description[0])
1475 WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1477 snprintf(fmt->description, sz, "%c%c%c%c%s",
1478 (char)(fmt->pixelformat & 0x7f),
1479 (char)((fmt->pixelformat >> 8) & 0x7f),
1480 (char)((fmt->pixelformat >> 16) & 0x7f),
1481 (char)((fmt->pixelformat >> 24) & 0x7f),
1482 (fmt->pixelformat & (1UL << 31)) ? "-BE" : "");
1488 WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1489 fmt->flags |= flags;
1492 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1493 struct file *file, void *fh, void *arg)
1495 struct video_device *vdev = video_devdata(file);
1496 struct v4l2_fmtdesc *p = arg;
1497 int ret = check_fmt(file, p->type);
1505 if (!(vdev->device_caps & V4L2_CAP_IO_MC))
1508 mbus_code = p->mbus_code;
1509 CLEAR_AFTER_FIELD(p, type);
1510 p->mbus_code = mbus_code;
1513 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1514 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1515 cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1516 V4L2_CAP_VIDEO_M2M_MPLANE;
1517 if (!!(vdev->device_caps & cap_mask) !=
1518 (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1521 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1523 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1525 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1526 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1528 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1530 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1531 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1532 cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1533 V4L2_CAP_VIDEO_M2M_MPLANE;
1534 if (!!(vdev->device_caps & cap_mask) !=
1535 (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1538 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1540 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1542 case V4L2_BUF_TYPE_SDR_CAPTURE:
1543 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1545 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1547 case V4L2_BUF_TYPE_SDR_OUTPUT:
1548 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1550 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1552 case V4L2_BUF_TYPE_META_CAPTURE:
1553 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1555 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1557 case V4L2_BUF_TYPE_META_OUTPUT:
1558 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1560 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1564 v4l_fill_fmtdesc(p);
1568 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1571 * The v4l2_pix_format structure contains fields that make no sense for
1572 * touch. Set them to default values in this case.
1575 p->field = V4L2_FIELD_NONE;
1576 p->colorspace = V4L2_COLORSPACE_RAW;
1579 p->quantization = 0;
1583 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1584 struct file *file, void *fh, void *arg)
1586 struct v4l2_format *p = arg;
1587 struct video_device *vfd = video_devdata(file);
1588 int ret = check_fmt(file, p->type);
1594 * fmt can't be cleared for these overlay types due to the 'clips'
1595 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1596 * Those are provided by the user. So handle these two overlay types
1597 * first, and then just do a simple memset for the other types.
1600 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1601 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1602 struct v4l2_clip __user *clips = p->fmt.win.clips;
1603 u32 clipcount = p->fmt.win.clipcount;
1604 void __user *bitmap = p->fmt.win.bitmap;
1606 memset(&p->fmt, 0, sizeof(p->fmt));
1607 p->fmt.win.clips = clips;
1608 p->fmt.win.clipcount = clipcount;
1609 p->fmt.win.bitmap = bitmap;
1613 memset(&p->fmt, 0, sizeof(p->fmt));
1618 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1619 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1621 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1622 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1623 /* just in case the driver zeroed it again */
1624 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1625 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1626 v4l_pix_format_touch(&p->fmt.pix);
1628 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1629 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1630 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1631 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1632 case V4L2_BUF_TYPE_VBI_CAPTURE:
1633 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1634 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1635 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1636 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1637 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1639 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1640 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1641 /* just in case the driver zeroed it again */
1642 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1644 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1645 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1646 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1647 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1648 case V4L2_BUF_TYPE_VBI_OUTPUT:
1649 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1650 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1651 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1652 case V4L2_BUF_TYPE_SDR_CAPTURE:
1653 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1654 case V4L2_BUF_TYPE_SDR_OUTPUT:
1655 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1656 case V4L2_BUF_TYPE_META_CAPTURE:
1657 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1658 case V4L2_BUF_TYPE_META_OUTPUT:
1659 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1664 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1665 struct file *file, void *fh, void *arg)
1667 struct v4l2_format *p = arg;
1668 struct video_device *vfd = video_devdata(file);
1669 int ret = check_fmt(file, p->type);
1675 ret = v4l_enable_media_source(vfd);
1678 v4l_sanitize_format(p);
1681 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1682 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1684 CLEAR_AFTER_FIELD(p, fmt.pix);
1685 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1686 /* just in case the driver zeroed it again */
1687 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1688 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1689 v4l_pix_format_touch(&p->fmt.pix);
1691 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1692 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1694 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1695 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1696 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1698 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1699 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1700 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1702 CLEAR_AFTER_FIELD(p, fmt.win);
1703 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1704 case V4L2_BUF_TYPE_VBI_CAPTURE:
1705 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1707 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1708 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1709 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1710 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1712 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1713 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1714 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1715 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1717 CLEAR_AFTER_FIELD(p, fmt.pix);
1718 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1719 /* just in case the driver zeroed it again */
1720 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1722 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1723 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1725 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1726 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1727 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1729 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1730 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1731 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1733 CLEAR_AFTER_FIELD(p, fmt.win);
1734 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1735 case V4L2_BUF_TYPE_VBI_OUTPUT:
1736 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1738 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1739 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1740 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1741 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1743 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1744 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1745 case V4L2_BUF_TYPE_SDR_CAPTURE:
1746 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1748 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1749 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1750 case V4L2_BUF_TYPE_SDR_OUTPUT:
1751 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1753 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1754 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1755 case V4L2_BUF_TYPE_META_CAPTURE:
1756 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1758 CLEAR_AFTER_FIELD(p, fmt.meta);
1759 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1760 case V4L2_BUF_TYPE_META_OUTPUT:
1761 if (unlikely(!ops->vidioc_s_fmt_meta_out))
1763 CLEAR_AFTER_FIELD(p, fmt.meta);
1764 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1769 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1770 struct file *file, void *fh, void *arg)
1772 struct v4l2_format *p = arg;
1773 struct video_device *vfd = video_devdata(file);
1774 int ret = check_fmt(file, p->type);
1780 v4l_sanitize_format(p);
1783 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1784 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1786 CLEAR_AFTER_FIELD(p, fmt.pix);
1787 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1788 /* just in case the driver zeroed it again */
1789 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1790 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1791 v4l_pix_format_touch(&p->fmt.pix);
1793 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1794 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1796 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1797 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1798 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1800 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1801 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1802 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1804 CLEAR_AFTER_FIELD(p, fmt.win);
1805 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1806 case V4L2_BUF_TYPE_VBI_CAPTURE:
1807 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1809 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1810 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1811 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1812 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1814 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1815 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1816 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1817 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1819 CLEAR_AFTER_FIELD(p, fmt.pix);
1820 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1821 /* just in case the driver zeroed it again */
1822 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1824 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1825 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1827 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1828 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1829 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1831 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1832 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1833 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1835 CLEAR_AFTER_FIELD(p, fmt.win);
1836 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1837 case V4L2_BUF_TYPE_VBI_OUTPUT:
1838 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1840 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1841 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1842 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1843 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1845 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1846 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1847 case V4L2_BUF_TYPE_SDR_CAPTURE:
1848 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1850 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1851 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1852 case V4L2_BUF_TYPE_SDR_OUTPUT:
1853 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1855 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1856 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1857 case V4L2_BUF_TYPE_META_CAPTURE:
1858 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1860 CLEAR_AFTER_FIELD(p, fmt.meta);
1861 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1862 case V4L2_BUF_TYPE_META_OUTPUT:
1863 if (unlikely(!ops->vidioc_try_fmt_meta_out))
1865 CLEAR_AFTER_FIELD(p, fmt.meta);
1866 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1871 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1872 struct file *file, void *fh, void *arg)
1874 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1877 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1878 struct file *file, void *fh, void *arg)
1880 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1883 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1884 struct file *file, void *fh, void *arg)
1886 struct video_device *vfd = video_devdata(file);
1887 struct v4l2_tuner *p = arg;
1890 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1891 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1892 err = ops->vidioc_g_tuner(file, fh, p);
1894 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1898 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1899 struct file *file, void *fh, void *arg)
1901 struct video_device *vfd = video_devdata(file);
1902 struct v4l2_tuner *p = arg;
1905 ret = v4l_enable_media_source(vfd);
1908 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1909 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1910 return ops->vidioc_s_tuner(file, fh, p);
1913 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1914 struct file *file, void *fh, void *arg)
1916 struct video_device *vfd = video_devdata(file);
1917 struct v4l2_modulator *p = arg;
1920 if (vfd->vfl_type == VFL_TYPE_RADIO)
1921 p->type = V4L2_TUNER_RADIO;
1923 err = ops->vidioc_g_modulator(file, fh, p);
1925 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1929 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1930 struct file *file, void *fh, void *arg)
1932 struct video_device *vfd = video_devdata(file);
1933 struct v4l2_modulator *p = arg;
1935 if (vfd->vfl_type == VFL_TYPE_RADIO)
1936 p->type = V4L2_TUNER_RADIO;
1938 return ops->vidioc_s_modulator(file, fh, p);
1941 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1942 struct file *file, void *fh, void *arg)
1944 struct video_device *vfd = video_devdata(file);
1945 struct v4l2_frequency *p = arg;
1947 if (vfd->vfl_type == VFL_TYPE_SDR)
1948 p->type = V4L2_TUNER_SDR;
1950 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1951 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1952 return ops->vidioc_g_frequency(file, fh, p);
1955 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1956 struct file *file, void *fh, void *arg)
1958 struct video_device *vfd = video_devdata(file);
1959 const struct v4l2_frequency *p = arg;
1960 enum v4l2_tuner_type type;
1963 ret = v4l_enable_media_source(vfd);
1966 if (vfd->vfl_type == VFL_TYPE_SDR) {
1967 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1970 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1971 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1972 if (type != p->type)
1975 return ops->vidioc_s_frequency(file, fh, p);
1978 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1979 struct file *file, void *fh, void *arg)
1981 struct video_device *vfd = video_devdata(file);
1982 struct v4l2_standard *p = arg;
1984 return v4l_video_std_enumstd(p, vfd->tvnorms);
1987 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1988 struct file *file, void *fh, void *arg)
1990 struct video_device *vfd = video_devdata(file);
1991 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1994 ret = v4l_enable_media_source(vfd);
1997 norm = id & vfd->tvnorms;
1998 if (vfd->tvnorms && !norm) /* Check if std is supported */
2001 /* Calls the specific handler */
2002 return ops->vidioc_s_std(file, fh, norm);
2005 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
2006 struct file *file, void *fh, void *arg)
2008 struct video_device *vfd = video_devdata(file);
2009 v4l2_std_id *p = arg;
2012 ret = v4l_enable_media_source(vfd);
2016 * If no signal is detected, then the driver should return
2017 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
2018 * any standards that do not apply removed.
2020 * This means that tuners, audio and video decoders can join
2021 * their efforts to improve the standards detection.
2024 return ops->vidioc_querystd(file, fh, arg);
2027 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
2028 struct file *file, void *fh, void *arg)
2030 struct video_device *vfd = video_devdata(file);
2031 struct v4l2_hw_freq_seek *p = arg;
2032 enum v4l2_tuner_type type;
2035 ret = v4l_enable_media_source(vfd);
2038 /* s_hw_freq_seek is not supported for SDR for now */
2039 if (vfd->vfl_type == VFL_TYPE_SDR)
2042 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2043 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2044 if (p->type != type)
2046 return ops->vidioc_s_hw_freq_seek(file, fh, p);
2049 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
2050 struct file *file, void *fh, void *arg)
2052 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
2055 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
2056 struct file *file, void *fh, void *arg)
2058 struct v4l2_requestbuffers *p = arg;
2059 int ret = check_fmt(file, p->type);
2064 CLEAR_AFTER_FIELD(p, capabilities);
2066 return ops->vidioc_reqbufs(file, fh, p);
2069 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
2070 struct file *file, void *fh, void *arg)
2072 struct v4l2_buffer *p = arg;
2073 int ret = check_fmt(file, p->type);
2075 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
2078 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2079 struct file *file, void *fh, void *arg)
2081 struct v4l2_buffer *p = arg;
2082 int ret = check_fmt(file, p->type);
2084 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2087 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2088 struct file *file, void *fh, void *arg)
2090 struct v4l2_buffer *p = arg;
2091 int ret = check_fmt(file, p->type);
2093 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2096 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2097 struct file *file, void *fh, void *arg)
2099 struct v4l2_create_buffers *create = arg;
2100 int ret = check_fmt(file, create->format.type);
2105 CLEAR_AFTER_FIELD(create, capabilities);
2107 v4l_sanitize_format(&create->format);
2109 ret = ops->vidioc_create_bufs(file, fh, create);
2111 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2112 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2113 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2118 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2119 struct file *file, void *fh, void *arg)
2121 struct v4l2_buffer *b = arg;
2122 int ret = check_fmt(file, b->type);
2124 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2127 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2128 struct file *file, void *fh, void *arg)
2130 struct video_device *vfd = video_devdata(file);
2131 struct v4l2_streamparm *p = arg;
2133 int ret = check_fmt(file, p->type);
2137 if (ops->vidioc_g_parm)
2138 return ops->vidioc_g_parm(file, fh, p);
2139 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2140 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2142 if (vfd->device_caps & V4L2_CAP_READWRITE)
2143 p->parm.capture.readbuffers = 2;
2144 ret = ops->vidioc_g_std(file, fh, &std);
2146 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2150 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2151 struct file *file, void *fh, void *arg)
2153 struct v4l2_streamparm *p = arg;
2154 int ret = check_fmt(file, p->type);
2159 /* Note: extendedmode is never used in drivers */
2160 if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2161 memset(p->parm.output.reserved, 0,
2162 sizeof(p->parm.output.reserved));
2163 p->parm.output.extendedmode = 0;
2164 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2166 memset(p->parm.capture.reserved, 0,
2167 sizeof(p->parm.capture.reserved));
2168 p->parm.capture.extendedmode = 0;
2169 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2171 return ops->vidioc_s_parm(file, fh, p);
2174 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2175 struct file *file, void *fh, void *arg)
2177 struct video_device *vfd = video_devdata(file);
2178 struct v4l2_queryctrl *p = arg;
2179 struct v4l2_fh *vfh =
2180 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2182 if (vfh && vfh->ctrl_handler)
2183 return v4l2_queryctrl(vfh->ctrl_handler, p);
2184 if (vfd->ctrl_handler)
2185 return v4l2_queryctrl(vfd->ctrl_handler, p);
2186 if (ops->vidioc_queryctrl)
2187 return ops->vidioc_queryctrl(file, fh, p);
2191 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2192 struct file *file, void *fh, void *arg)
2194 struct video_device *vfd = video_devdata(file);
2195 struct v4l2_query_ext_ctrl *p = arg;
2196 struct v4l2_fh *vfh =
2197 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2199 if (vfh && vfh->ctrl_handler)
2200 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2201 if (vfd->ctrl_handler)
2202 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2203 if (ops->vidioc_query_ext_ctrl)
2204 return ops->vidioc_query_ext_ctrl(file, fh, p);
2208 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2209 struct file *file, void *fh, void *arg)
2211 struct video_device *vfd = video_devdata(file);
2212 struct v4l2_querymenu *p = arg;
2213 struct v4l2_fh *vfh =
2214 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2216 if (vfh && vfh->ctrl_handler)
2217 return v4l2_querymenu(vfh->ctrl_handler, p);
2218 if (vfd->ctrl_handler)
2219 return v4l2_querymenu(vfd->ctrl_handler, p);
2220 if (ops->vidioc_querymenu)
2221 return ops->vidioc_querymenu(file, fh, p);
2225 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2226 struct file *file, void *fh, void *arg)
2228 struct video_device *vfd = video_devdata(file);
2229 struct v4l2_control *p = arg;
2230 struct v4l2_fh *vfh =
2231 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2232 struct v4l2_ext_controls ctrls;
2233 struct v4l2_ext_control ctrl;
2235 if (vfh && vfh->ctrl_handler)
2236 return v4l2_g_ctrl(vfh->ctrl_handler, p);
2237 if (vfd->ctrl_handler)
2238 return v4l2_g_ctrl(vfd->ctrl_handler, p);
2239 if (ops->vidioc_g_ctrl)
2240 return ops->vidioc_g_ctrl(file, fh, p);
2241 if (ops->vidioc_g_ext_ctrls == NULL)
2244 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2246 ctrls.controls = &ctrl;
2248 ctrl.value = p->value;
2249 if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
2250 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2253 p->value = ctrl.value;
2259 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2260 struct file *file, void *fh, void *arg)
2262 struct video_device *vfd = video_devdata(file);
2263 struct v4l2_control *p = arg;
2264 struct v4l2_fh *vfh =
2265 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2266 struct v4l2_ext_controls ctrls;
2267 struct v4l2_ext_control ctrl;
2270 if (vfh && vfh->ctrl_handler)
2271 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2272 if (vfd->ctrl_handler)
2273 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2274 if (ops->vidioc_s_ctrl)
2275 return ops->vidioc_s_ctrl(file, fh, p);
2276 if (ops->vidioc_s_ext_ctrls == NULL)
2279 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2281 ctrls.controls = &ctrl;
2283 ctrl.value = p->value;
2284 if (!check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
2286 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2287 p->value = ctrl.value;
2291 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2292 struct file *file, void *fh, void *arg)
2294 struct video_device *vfd = video_devdata(file);
2295 struct v4l2_ext_controls *p = arg;
2296 struct v4l2_fh *vfh =
2297 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2299 p->error_idx = p->count;
2300 if (vfh && vfh->ctrl_handler)
2301 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2302 vfd, vfd->v4l2_dev->mdev, p);
2303 if (vfd->ctrl_handler)
2304 return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2305 vfd, vfd->v4l2_dev->mdev, p);
2306 if (ops->vidioc_g_ext_ctrls == NULL)
2308 return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
2309 ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
2312 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2313 struct file *file, void *fh, void *arg)
2315 struct video_device *vfd = video_devdata(file);
2316 struct v4l2_ext_controls *p = arg;
2317 struct v4l2_fh *vfh =
2318 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2320 p->error_idx = p->count;
2321 if (vfh && vfh->ctrl_handler)
2322 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2323 vfd, vfd->v4l2_dev->mdev, p);
2324 if (vfd->ctrl_handler)
2325 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2326 vfd, vfd->v4l2_dev->mdev, p);
2327 if (ops->vidioc_s_ext_ctrls == NULL)
2329 return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
2330 ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
2333 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2334 struct file *file, void *fh, void *arg)
2336 struct video_device *vfd = video_devdata(file);
2337 struct v4l2_ext_controls *p = arg;
2338 struct v4l2_fh *vfh =
2339 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2341 p->error_idx = p->count;
2342 if (vfh && vfh->ctrl_handler)
2343 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2344 vfd, vfd->v4l2_dev->mdev, p);
2345 if (vfd->ctrl_handler)
2346 return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2347 vfd, vfd->v4l2_dev->mdev, p);
2348 if (ops->vidioc_try_ext_ctrls == NULL)
2350 return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
2351 ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
2355 * The selection API specified originally that the _MPLANE buffer types
2356 * shouldn't be used. The reasons for this are lost in the mists of time
2357 * (or just really crappy memories). Regardless, this is really annoying
2358 * for userspace. So to keep things simple we map _MPLANE buffer types
2359 * to their 'regular' counterparts before calling the driver. And we
2360 * restore it afterwards. This way applications can use either buffer
2361 * type and drivers don't need to check for both.
2363 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2364 struct file *file, void *fh, void *arg)
2366 struct v4l2_selection *p = arg;
2367 u32 old_type = p->type;
2370 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2371 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2372 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2373 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2374 ret = ops->vidioc_g_selection(file, fh, p);
2379 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2380 struct file *file, void *fh, void *arg)
2382 struct v4l2_selection *p = arg;
2383 u32 old_type = p->type;
2386 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2387 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2388 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2389 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2390 ret = ops->vidioc_s_selection(file, fh, p);
2395 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2396 struct file *file, void *fh, void *arg)
2398 struct video_device *vfd = video_devdata(file);
2399 struct v4l2_crop *p = arg;
2400 struct v4l2_selection s = {
2405 /* simulate capture crop using selection api */
2407 /* crop means compose for output devices */
2408 if (V4L2_TYPE_IS_OUTPUT(p->type))
2409 s.target = V4L2_SEL_TGT_COMPOSE;
2411 s.target = V4L2_SEL_TGT_CROP;
2413 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2414 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2415 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2417 ret = v4l_g_selection(ops, file, fh, &s);
2419 /* copying results to old structure on success */
2425 static int v4l_s_crop(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_crop *p = arg;
2430 struct v4l2_selection s = {
2435 /* simulate capture crop using selection api */
2437 /* crop means compose for output devices */
2438 if (V4L2_TYPE_IS_OUTPUT(p->type))
2439 s.target = V4L2_SEL_TGT_COMPOSE;
2441 s.target = V4L2_SEL_TGT_CROP;
2443 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2444 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2445 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2447 return v4l_s_selection(ops, file, fh, &s);
2450 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2451 struct file *file, void *fh, void *arg)
2453 struct video_device *vfd = video_devdata(file);
2454 struct v4l2_cropcap *p = arg;
2455 struct v4l2_selection s = { .type = p->type };
2458 /* setting trivial pixelaspect */
2459 p->pixelaspect.numerator = 1;
2460 p->pixelaspect.denominator = 1;
2462 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2463 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2464 else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2465 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2468 * The determine_valid_ioctls() call already should ensure
2469 * that this can never happen, but just in case...
2471 if (WARN_ON(!ops->vidioc_g_selection))
2474 if (ops->vidioc_g_pixelaspect)
2475 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2479 * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2480 * square pixel aspect ratio in that case.
2482 if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2485 /* Use g_selection() to fill in the bounds and defrect rectangles */
2487 /* obtaining bounds */
2488 if (V4L2_TYPE_IS_OUTPUT(p->type))
2489 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2491 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2493 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2494 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2495 V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2497 ret = v4l_g_selection(ops, file, fh, &s);
2502 /* obtaining defrect */
2503 if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2504 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2506 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2508 ret = v4l_g_selection(ops, file, fh, &s);
2516 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2517 struct file *file, void *fh, void *arg)
2519 struct video_device *vfd = video_devdata(file);
2523 pr_info("%s: ================= START STATUS =================\n",
2524 vfd->v4l2_dev->name);
2525 ret = ops->vidioc_log_status(file, fh);
2527 pr_info("%s: ================== END STATUS ==================\n",
2528 vfd->v4l2_dev->name);
2532 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2533 struct file *file, void *fh, void *arg)
2535 #ifdef CONFIG_VIDEO_ADV_DEBUG
2536 struct v4l2_dbg_register *p = arg;
2537 struct video_device *vfd = video_devdata(file);
2538 struct v4l2_subdev *sd;
2541 if (!capable(CAP_SYS_ADMIN))
2543 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2544 if (vfd->v4l2_dev == NULL)
2546 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2547 if (p->match.addr == idx++)
2548 return v4l2_subdev_call(sd, core, g_register, p);
2551 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2552 (ops->vidioc_g_chip_info || p->match.addr == 0))
2553 return ops->vidioc_g_register(file, fh, p);
2560 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2561 struct file *file, void *fh, void *arg)
2563 #ifdef CONFIG_VIDEO_ADV_DEBUG
2564 const struct v4l2_dbg_register *p = arg;
2565 struct video_device *vfd = video_devdata(file);
2566 struct v4l2_subdev *sd;
2569 if (!capable(CAP_SYS_ADMIN))
2571 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2572 if (vfd->v4l2_dev == NULL)
2574 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2575 if (p->match.addr == idx++)
2576 return v4l2_subdev_call(sd, core, s_register, p);
2579 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2580 (ops->vidioc_g_chip_info || p->match.addr == 0))
2581 return ops->vidioc_s_register(file, fh, p);
2588 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2589 struct file *file, void *fh, void *arg)
2591 #ifdef CONFIG_VIDEO_ADV_DEBUG
2592 struct video_device *vfd = video_devdata(file);
2593 struct v4l2_dbg_chip_info *p = arg;
2594 struct v4l2_subdev *sd;
2597 switch (p->match.type) {
2598 case V4L2_CHIP_MATCH_BRIDGE:
2599 if (ops->vidioc_s_register)
2600 p->flags |= V4L2_CHIP_FL_WRITABLE;
2601 if (ops->vidioc_g_register)
2602 p->flags |= V4L2_CHIP_FL_READABLE;
2603 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2604 if (ops->vidioc_g_chip_info)
2605 return ops->vidioc_g_chip_info(file, fh, arg);
2610 case V4L2_CHIP_MATCH_SUBDEV:
2611 if (vfd->v4l2_dev == NULL)
2613 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2614 if (p->match.addr != idx++)
2616 if (sd->ops->core && sd->ops->core->s_register)
2617 p->flags |= V4L2_CHIP_FL_WRITABLE;
2618 if (sd->ops->core && sd->ops->core->g_register)
2619 p->flags |= V4L2_CHIP_FL_READABLE;
2620 strscpy(p->name, sd->name, sizeof(p->name));
2631 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2632 struct file *file, void *fh, void *arg)
2634 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2637 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2638 struct file *file, void *fh, void *arg)
2640 return ops->vidioc_subscribe_event(fh, arg);
2643 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2644 struct file *file, void *fh, void *arg)
2646 return ops->vidioc_unsubscribe_event(fh, arg);
2649 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2650 struct file *file, void *fh, void *arg)
2652 struct v4l2_sliced_vbi_cap *p = arg;
2653 int ret = check_fmt(file, p->type);
2658 /* Clear up to type, everything after type is zeroed already */
2659 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2661 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2664 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2665 struct file *file, void *fh, void *arg)
2667 struct video_device *vfd = video_devdata(file);
2668 struct v4l2_frequency_band *p = arg;
2669 enum v4l2_tuner_type type;
2672 if (vfd->vfl_type == VFL_TYPE_SDR) {
2673 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2677 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2678 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2679 if (type != p->type)
2682 if (ops->vidioc_enum_freq_bands) {
2683 err = ops->vidioc_enum_freq_bands(file, fh, p);
2687 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2688 struct v4l2_tuner t = {
2695 err = ops->vidioc_g_tuner(file, fh, &t);
2698 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2699 p->rangelow = t.rangelow;
2700 p->rangehigh = t.rangehigh;
2701 p->modulation = (type == V4L2_TUNER_RADIO) ?
2702 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2705 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2706 struct v4l2_modulator m = {
2710 if (type != V4L2_TUNER_RADIO)
2714 err = ops->vidioc_g_modulator(file, fh, &m);
2717 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2718 p->rangelow = m.rangelow;
2719 p->rangehigh = m.rangehigh;
2720 p->modulation = (type == V4L2_TUNER_RADIO) ?
2721 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2727 struct v4l2_ioctl_info {
2730 const char * const name;
2731 int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2733 void (*debug)(const void *arg, bool write_only);
2736 /* This control needs a priority check */
2737 #define INFO_FL_PRIO (1 << 0)
2738 /* This control can be valid if the filehandle passes a control handler. */
2739 #define INFO_FL_CTRL (1 << 1)
2741 #define INFO_FL_QUEUE (1 << 2)
2742 /* Always copy back result, even on error */
2743 #define INFO_FL_ALWAYS_COPY (1 << 3)
2744 /* Zero struct from after the field to the end */
2745 #define INFO_FL_CLEAR(v4l2_struct, field) \
2746 ((offsetof(struct v4l2_struct, field) + \
2747 sizeof_field(struct v4l2_struct, field)) << 16)
2748 #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2750 #define DEFINE_V4L_STUB_FUNC(_vidioc) \
2751 static int v4l_stub_ ## _vidioc( \
2752 const struct v4l2_ioctl_ops *ops, \
2753 struct file *file, void *fh, void *p) \
2755 return ops->vidioc_ ## _vidioc(file, fh, p); \
2758 #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \
2759 [_IOC_NR(_ioctl)] = { \
2767 DEFINE_V4L_STUB_FUNC(g_fbuf)
2768 DEFINE_V4L_STUB_FUNC(s_fbuf)
2769 DEFINE_V4L_STUB_FUNC(expbuf)
2770 DEFINE_V4L_STUB_FUNC(g_std)
2771 DEFINE_V4L_STUB_FUNC(g_audio)
2772 DEFINE_V4L_STUB_FUNC(s_audio)
2773 DEFINE_V4L_STUB_FUNC(g_edid)
2774 DEFINE_V4L_STUB_FUNC(s_edid)
2775 DEFINE_V4L_STUB_FUNC(g_audout)
2776 DEFINE_V4L_STUB_FUNC(s_audout)
2777 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2778 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2779 DEFINE_V4L_STUB_FUNC(enumaudio)
2780 DEFINE_V4L_STUB_FUNC(enumaudout)
2781 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2782 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2783 DEFINE_V4L_STUB_FUNC(g_enc_index)
2784 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2785 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2786 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2787 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2788 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2789 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2790 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2791 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2792 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2794 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2795 IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2796 IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
2797 IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2798 IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2799 IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2800 IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2801 IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2802 IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2803 IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2804 IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2805 IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2806 IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2807 IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2808 IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2809 IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2810 IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2811 IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2812 IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2813 IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2814 IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2815 IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2816 IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2817 IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2818 IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2819 IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2820 IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2821 IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2822 IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2823 IOCTL_INFO(VIDIOC_G_INPUT, v4l_g_input, v4l_print_u32, 0),
2824 IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2825 IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2826 IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2827 IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_g_output, v4l_print_u32, 0),
2828 IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2829 IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2830 IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2831 IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2832 IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2833 IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2834 IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2835 IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2836 IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2837 IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2838 IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2839 IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2840 IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2841 IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2842 IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2843 IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2844 IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2845 IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2846 IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2847 IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2848 IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2849 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)),
2850 IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2851 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2852 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2853 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2854 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2855 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2856 IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2857 IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2858 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2859 IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2860 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2861 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2862 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2863 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2864 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)),
2865 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2866 IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2867 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2868 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2869 IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2870 IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2871 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2872 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2873 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2874 IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2875 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)),
2876 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)),
2878 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2880 static bool v4l2_is_known_ioctl(unsigned int cmd)
2882 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2884 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2887 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2888 struct v4l2_fh *vfh, unsigned int cmd,
2891 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2893 if (vfh && vfh->m2m_ctx &&
2894 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2895 if (vfh->m2m_ctx->q_lock)
2896 return vfh->m2m_ctx->q_lock;
2898 if (vdev->queue && vdev->queue->lock &&
2899 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2900 return vdev->queue->lock;
2904 /* Common ioctl debug function. This function can be used by
2905 external ioctl messages as well as internal V4L ioctl */
2906 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2908 const char *dir, *type;
2911 printk(KERN_DEBUG "%s: ", prefix);
2913 switch (_IOC_TYPE(cmd)) {
2918 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2922 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2929 switch (_IOC_DIR(cmd)) {
2930 case _IOC_NONE: dir = "--"; break;
2931 case _IOC_READ: dir = "r-"; break;
2932 case _IOC_WRITE: dir = "-w"; break;
2933 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2934 default: dir = "*ERR*"; break;
2936 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2937 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2939 EXPORT_SYMBOL(v4l_printk_ioctl);
2941 static long __video_do_ioctl(struct file *file,
2942 unsigned int cmd, void *arg)
2944 struct video_device *vfd = video_devdata(file);
2945 struct mutex *req_queue_lock = NULL;
2946 struct mutex *lock; /* ioctl serialization mutex */
2947 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2948 bool write_only = false;
2949 struct v4l2_ioctl_info default_info;
2950 const struct v4l2_ioctl_info *info;
2951 void *fh = file->private_data;
2952 struct v4l2_fh *vfh = NULL;
2953 int dev_debug = vfd->dev_debug;
2957 pr_warn("%s: has no ioctl_ops.\n",
2958 video_device_node_name(vfd));
2962 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2963 vfh = file->private_data;
2966 * We need to serialize streamon/off with queueing new requests.
2967 * These ioctls may trigger the cancellation of a streaming
2968 * operation, and that should not be mixed with queueing a new
2969 * request at the same time.
2971 if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2972 (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2973 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2975 if (mutex_lock_interruptible(req_queue_lock))
2976 return -ERESTARTSYS;
2979 lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2981 if (lock && mutex_lock_interruptible(lock)) {
2983 mutex_unlock(req_queue_lock);
2984 return -ERESTARTSYS;
2987 if (!video_is_registered(vfd)) {
2992 if (v4l2_is_known_ioctl(cmd)) {
2993 info = &v4l2_ioctls[_IOC_NR(cmd)];
2995 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2996 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2999 if (vfh && (info->flags & INFO_FL_PRIO)) {
3000 ret = v4l2_prio_check(vfd->prio, vfh->prio);
3005 default_info.ioctl = cmd;
3006 default_info.flags = 0;
3007 default_info.debug = v4l_print_default;
3008 info = &default_info;
3011 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
3012 if (info != &default_info) {
3013 ret = info->func(ops, file, fh, arg);
3014 } else if (!ops->vidioc_default) {
3017 ret = ops->vidioc_default(file, fh,
3018 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
3023 if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
3024 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
3025 (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
3028 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
3030 pr_cont(": error %ld", ret);
3031 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
3033 else if (_IOC_DIR(cmd) == _IOC_NONE)
3034 info->debug(arg, write_only);
3037 info->debug(arg, write_only);
3045 mutex_unlock(req_queue_lock);
3049 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
3050 void __user **user_ptr, void ***kernel_ptr)
3055 case VIDIOC_PREPARE_BUF:
3056 case VIDIOC_QUERYBUF:
3058 case VIDIOC_DQBUF: {
3059 struct v4l2_buffer *buf = parg;
3061 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
3062 if (buf->length > VIDEO_MAX_PLANES) {
3066 *user_ptr = (void __user *)buf->m.planes;
3067 *kernel_ptr = (void **)&buf->m.planes;
3068 *array_size = sizeof(struct v4l2_plane) * buf->length;
3075 case VIDIOC_S_EDID: {
3076 struct v4l2_edid *edid = parg;
3079 if (edid->blocks > 256) {
3083 *user_ptr = (void __user *)edid->edid;
3084 *kernel_ptr = (void **)&edid->edid;
3085 *array_size = edid->blocks * 128;
3091 case VIDIOC_S_EXT_CTRLS:
3092 case VIDIOC_G_EXT_CTRLS:
3093 case VIDIOC_TRY_EXT_CTRLS: {
3094 struct v4l2_ext_controls *ctrls = parg;
3096 if (ctrls->count != 0) {
3097 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3101 *user_ptr = (void __user *)ctrls->controls;
3102 *kernel_ptr = (void **)&ctrls->controls;
3103 *array_size = sizeof(struct v4l2_ext_control)
3114 static unsigned int video_translate_cmd(unsigned int cmd)
3117 #ifdef CONFIG_COMPAT_32BIT_TIME
3118 case VIDIOC_DQEVENT_TIME32:
3119 return VIDIOC_DQEVENT;
3120 case VIDIOC_QUERYBUF_TIME32:
3121 return VIDIOC_QUERYBUF;
3122 case VIDIOC_QBUF_TIME32:
3124 case VIDIOC_DQBUF_TIME32:
3125 return VIDIOC_DQBUF;
3126 case VIDIOC_PREPARE_BUF_TIME32:
3127 return VIDIOC_PREPARE_BUF;
3134 static int video_get_user(void __user *arg, void *parg, unsigned int cmd,
3137 unsigned int n = _IOC_SIZE(cmd);
3139 if (!(_IOC_DIR(cmd) & _IOC_WRITE)) {
3140 /* read-only ioctl */
3146 #ifdef CONFIG_COMPAT_32BIT_TIME
3147 case VIDIOC_QUERYBUF_TIME32:
3148 case VIDIOC_QBUF_TIME32:
3149 case VIDIOC_DQBUF_TIME32:
3150 case VIDIOC_PREPARE_BUF_TIME32: {
3151 struct v4l2_buffer_time32 vb32;
3152 struct v4l2_buffer *vb = parg;
3154 if (copy_from_user(&vb32, arg, sizeof(vb32)))
3157 *vb = (struct v4l2_buffer) {
3158 .index = vb32.index,
3160 .bytesused = vb32.bytesused,
3161 .flags = vb32.flags,
3162 .field = vb32.field,
3163 .timestamp.tv_sec = vb32.timestamp.tv_sec,
3164 .timestamp.tv_usec = vb32.timestamp.tv_usec,
3165 .timecode = vb32.timecode,
3166 .sequence = vb32.sequence,
3167 .memory = vb32.memory,
3168 .m.userptr = vb32.m.userptr,
3169 .length = vb32.length,
3170 .request_fd = vb32.request_fd,
3173 if (cmd == VIDIOC_QUERYBUF_TIME32)
3181 * In some cases, only a few fields are used as input,
3182 * i.e. when the app sets "index" and then the driver
3183 * fills in the rest of the structure for the thing
3184 * with that index. We only need to copy up the first
3187 if (v4l2_is_known_ioctl(cmd)) {
3188 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
3190 if (flags & INFO_FL_CLEAR_MASK)
3191 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3192 *always_copy = flags & INFO_FL_ALWAYS_COPY;
3195 if (copy_from_user(parg, (void __user *)arg, n))
3198 /* zero out anything we don't copy from userspace */
3199 if (n < _IOC_SIZE(cmd))
3200 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
3207 static int video_put_user(void __user *arg, void *parg, unsigned int cmd)
3209 if (!(_IOC_DIR(cmd) & _IOC_READ))
3213 #ifdef CONFIG_COMPAT_32BIT_TIME
3214 case VIDIOC_DQEVENT_TIME32: {
3215 struct v4l2_event *ev = parg;
3216 struct v4l2_event_time32 ev32;
3218 memset(&ev32, 0, sizeof(ev32));
3220 ev32.type = ev->type;
3221 ev32.pending = ev->pending;
3222 ev32.sequence = ev->sequence;
3223 ev32.timestamp.tv_sec = ev->timestamp.tv_sec;
3224 ev32.timestamp.tv_nsec = ev->timestamp.tv_nsec;
3227 memcpy(&ev32.u, &ev->u, sizeof(ev->u));
3228 memcpy(&ev32.reserved, &ev->reserved, sizeof(ev->reserved));
3230 if (copy_to_user(arg, &ev32, sizeof(ev32)))
3234 case VIDIOC_QUERYBUF_TIME32:
3235 case VIDIOC_QBUF_TIME32:
3236 case VIDIOC_DQBUF_TIME32:
3237 case VIDIOC_PREPARE_BUF_TIME32: {
3238 struct v4l2_buffer *vb = parg;
3239 struct v4l2_buffer_time32 vb32;
3241 memset(&vb32, 0, sizeof(vb32));
3243 vb32.index = vb->index;
3244 vb32.type = vb->type;
3245 vb32.bytesused = vb->bytesused;
3246 vb32.flags = vb->flags;
3247 vb32.field = vb->field;
3248 vb32.timestamp.tv_sec = vb->timestamp.tv_sec;
3249 vb32.timestamp.tv_usec = vb->timestamp.tv_usec;
3250 vb32.timecode = vb->timecode;
3251 vb32.sequence = vb->sequence;
3252 vb32.memory = vb->memory;
3253 vb32.m.userptr = vb->m.userptr;
3254 vb32.length = vb->length;
3255 vb32.request_fd = vb->request_fd;
3257 if (copy_to_user(arg, &vb32, sizeof(vb32)))
3263 /* Copy results into user buffer */
3264 if (copy_to_user(arg, parg, _IOC_SIZE(cmd)))
3273 video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
3277 void *mbuf = NULL, *array_buf = NULL;
3278 void *parg = (void *)arg;
3280 bool has_array_args;
3281 bool always_copy = false;
3282 size_t array_size = 0;
3283 void __user *user_ptr = NULL;
3284 void **kernel_ptr = NULL;
3285 unsigned int cmd = video_translate_cmd(orig_cmd);
3286 const size_t ioc_size = _IOC_SIZE(cmd);
3288 /* Copy arguments into temp kernel buffer */
3289 if (_IOC_DIR(cmd) != _IOC_NONE) {
3290 if (ioc_size <= sizeof(sbuf)) {
3293 /* too big to allocate from stack */
3294 mbuf = kvmalloc(ioc_size, GFP_KERNEL);
3300 err = video_get_user((void __user *)arg, parg, orig_cmd,
3306 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3309 has_array_args = err;
3311 if (has_array_args) {
3312 array_buf = kvmalloc(array_size, GFP_KERNEL);
3314 if (array_buf == NULL)
3315 goto out_array_args;
3317 if (copy_from_user(array_buf, user_ptr, array_size))
3318 goto out_array_args;
3319 *kernel_ptr = array_buf;
3323 err = func(file, cmd, parg);
3324 if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3330 if (cmd == VIDIOC_DQBUF)
3331 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3332 else if (cmd == VIDIOC_QBUF)
3333 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3336 if (has_array_args) {
3337 *kernel_ptr = (void __force *)user_ptr;
3338 if (copy_to_user(user_ptr, array_buf, array_size))
3340 goto out_array_args;
3343 * Some ioctls can return an error, but still have valid
3344 * results that must be returned.
3346 if (err < 0 && !always_copy)
3350 if (video_put_user((void __user *)arg, parg, orig_cmd))
3358 long video_ioctl2(struct file *file,
3359 unsigned int cmd, unsigned long arg)
3361 return video_usercopy(file, cmd, arg, __video_do_ioctl);
3363 EXPORT_SYMBOL(video_ioctl2);