1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Video capture interface for Linux version 2
4 *
5 * A generic framework to process V4L2 ioctl commands.
6 *
7 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
8 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
9 */
10
11 #include <linux/compat.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/version.h>
18
19 #include <linux/videodev2.h>
20
21 #include <media/media-device.h> /* for media_set_bus_info() */
22 #include <media/v4l2-common.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-device.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/v4l2-mc.h>
30 #include <media/v4l2-mem2mem.h>
31
32 #include <trace/events/v4l2.h>
33
34 /* Zero out the end of the struct pointed to by p. Everything after, but
35 * not including, the specified field is cleared. */
36 #define CLEAR_AFTER_FIELD(p, field) \
37 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
38 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
39
40 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
41
42 struct std_descr {
43 v4l2_std_id std;
44 const char *descr;
45 };
46
47 static const struct std_descr standards[] = {
48 { V4L2_STD_NTSC, "NTSC" },
49 { V4L2_STD_NTSC_M, "NTSC-M" },
50 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
51 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
52 { V4L2_STD_NTSC_443, "NTSC-443" },
53 { V4L2_STD_PAL, "PAL" },
54 { V4L2_STD_PAL_BG, "PAL-BG" },
55 { V4L2_STD_PAL_B, "PAL-B" },
56 { V4L2_STD_PAL_B1, "PAL-B1" },
57 { V4L2_STD_PAL_G, "PAL-G" },
58 { V4L2_STD_PAL_H, "PAL-H" },
59 { V4L2_STD_PAL_I, "PAL-I" },
60 { V4L2_STD_PAL_DK, "PAL-DK" },
61 { V4L2_STD_PAL_D, "PAL-D" },
62 { V4L2_STD_PAL_D1, "PAL-D1" },
63 { V4L2_STD_PAL_K, "PAL-K" },
64 { V4L2_STD_PAL_M, "PAL-M" },
65 { V4L2_STD_PAL_N, "PAL-N" },
66 { V4L2_STD_PAL_Nc, "PAL-Nc" },
67 { V4L2_STD_PAL_60, "PAL-60" },
68 { V4L2_STD_SECAM, "SECAM" },
69 { V4L2_STD_SECAM_B, "SECAM-B" },
70 { V4L2_STD_SECAM_G, "SECAM-G" },
71 { V4L2_STD_SECAM_H, "SECAM-H" },
72 { V4L2_STD_SECAM_DK, "SECAM-DK" },
73 { V4L2_STD_SECAM_D, "SECAM-D" },
74 { V4L2_STD_SECAM_K, "SECAM-K" },
75 { V4L2_STD_SECAM_K1, "SECAM-K1" },
76 { V4L2_STD_SECAM_L, "SECAM-L" },
77 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
78 { 0, "Unknown" }
79 };
80
81 /* video4linux standard ID conversion to standard name
82 */
v4l2_norm_to_name(v4l2_std_id id)83 const char *v4l2_norm_to_name(v4l2_std_id id)
84 {
85 u32 myid = id;
86 int i;
87
88 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
89 64 bit comparisons. So, on that architecture, with some gcc
90 variants, compilation fails. Currently, the max value is 30bit wide.
91 */
92 BUG_ON(myid != id);
93
94 for (i = 0; standards[i].std; i++)
95 if (myid == standards[i].std)
96 break;
97 return standards[i].descr;
98 }
99 EXPORT_SYMBOL(v4l2_norm_to_name);
100
101 /* Returns frame period for the given standard */
v4l2_video_std_frame_period(int id,struct v4l2_fract * frameperiod)102 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
103 {
104 if (id & V4L2_STD_525_60) {
105 frameperiod->numerator = 1001;
106 frameperiod->denominator = 30000;
107 } else {
108 frameperiod->numerator = 1;
109 frameperiod->denominator = 25;
110 }
111 }
112 EXPORT_SYMBOL(v4l2_video_std_frame_period);
113
114 /* Fill in the fields of a v4l2_standard structure according to the
115 'id' and 'transmission' parameters. Returns negative on error. */
v4l2_video_std_construct(struct v4l2_standard * vs,int id,const char * name)116 int v4l2_video_std_construct(struct v4l2_standard *vs,
117 int id, const char *name)
118 {
119 vs->id = id;
120 v4l2_video_std_frame_period(id, &vs->frameperiod);
121 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
122 strscpy(vs->name, name, sizeof(vs->name));
123 return 0;
124 }
125 EXPORT_SYMBOL(v4l2_video_std_construct);
126
127 /* Fill in the fields of a v4l2_standard structure according to the
128 * 'id' and 'vs->index' parameters. Returns negative on error. */
v4l_video_std_enumstd(struct v4l2_standard * vs,v4l2_std_id id)129 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
130 {
131 v4l2_std_id curr_id = 0;
132 unsigned int index = vs->index, i, j = 0;
133 const char *descr = "";
134
135 /* Return -ENODATA if the id for the current input
136 or output is 0, meaning that it doesn't support this API. */
137 if (id == 0)
138 return -ENODATA;
139
140 /* Return norm array in a canonical way */
141 for (i = 0; i <= index && id; i++) {
142 /* last std value in the standards array is 0, so this
143 while always ends there since (id & 0) == 0. */
144 while ((id & standards[j].std) != standards[j].std)
145 j++;
146 curr_id = standards[j].std;
147 descr = standards[j].descr;
148 j++;
149 if (curr_id == 0)
150 break;
151 if (curr_id != V4L2_STD_PAL &&
152 curr_id != V4L2_STD_SECAM &&
153 curr_id != V4L2_STD_NTSC)
154 id &= ~curr_id;
155 }
156 if (i <= index)
157 return -EINVAL;
158
159 v4l2_video_std_construct(vs, curr_id, descr);
160 return 0;
161 }
162
163 /* ----------------------------------------------------------------- */
164 /* some arrays for pretty-printing debug messages of enum types */
165
166 const char *v4l2_field_names[] = {
167 [V4L2_FIELD_ANY] = "any",
168 [V4L2_FIELD_NONE] = "none",
169 [V4L2_FIELD_TOP] = "top",
170 [V4L2_FIELD_BOTTOM] = "bottom",
171 [V4L2_FIELD_INTERLACED] = "interlaced",
172 [V4L2_FIELD_SEQ_TB] = "seq-tb",
173 [V4L2_FIELD_SEQ_BT] = "seq-bt",
174 [V4L2_FIELD_ALTERNATE] = "alternate",
175 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
176 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
177 };
178 EXPORT_SYMBOL(v4l2_field_names);
179
180 const char *v4l2_type_names[] = {
181 [0] = "0",
182 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
183 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
184 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
185 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
186 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
187 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
188 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
189 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
190 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
191 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
192 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
193 [V4L2_BUF_TYPE_SDR_OUTPUT] = "sdr-out",
194 [V4L2_BUF_TYPE_META_CAPTURE] = "meta-cap",
195 [V4L2_BUF_TYPE_META_OUTPUT] = "meta-out",
196 };
197 EXPORT_SYMBOL(v4l2_type_names);
198
199 static const char *v4l2_memory_names[] = {
200 [V4L2_MEMORY_MMAP] = "mmap",
201 [V4L2_MEMORY_USERPTR] = "userptr",
202 [V4L2_MEMORY_OVERLAY] = "overlay",
203 [V4L2_MEMORY_DMABUF] = "dmabuf",
204 };
205
206 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
207
208 /* ------------------------------------------------------------------ */
209 /* debug help functions */
210
v4l_print_querycap(const void * arg,bool write_only)211 static void v4l_print_querycap(const void *arg, bool write_only)
212 {
213 const struct v4l2_capability *p = arg;
214
215 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
216 (int)sizeof(p->driver), p->driver,
217 (int)sizeof(p->card), p->card,
218 (int)sizeof(p->bus_info), p->bus_info,
219 p->version, p->capabilities, p->device_caps);
220 }
221
v4l_print_enuminput(const void * arg,bool write_only)222 static void v4l_print_enuminput(const void *arg, bool write_only)
223 {
224 const struct v4l2_input *p = arg;
225
226 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
227 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
228 p->tuner, (unsigned long long)p->std, p->status,
229 p->capabilities);
230 }
231
v4l_print_enumoutput(const void * arg,bool write_only)232 static void v4l_print_enumoutput(const void *arg, bool write_only)
233 {
234 const struct v4l2_output *p = arg;
235
236 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
237 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
238 p->modulator, (unsigned long long)p->std, p->capabilities);
239 }
240
v4l_print_audio(const void * arg,bool write_only)241 static void v4l_print_audio(const void *arg, bool write_only)
242 {
243 const struct v4l2_audio *p = arg;
244
245 if (write_only)
246 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
247 else
248 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
249 p->index, (int)sizeof(p->name), p->name,
250 p->capability, p->mode);
251 }
252
v4l_print_audioout(const void * arg,bool write_only)253 static void v4l_print_audioout(const void *arg, bool write_only)
254 {
255 const struct v4l2_audioout *p = arg;
256
257 if (write_only)
258 pr_cont("index=%u\n", p->index);
259 else
260 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
261 p->index, (int)sizeof(p->name), p->name,
262 p->capability, p->mode);
263 }
264
v4l_print_fmtdesc(const void * arg,bool write_only)265 static void v4l_print_fmtdesc(const void *arg, bool write_only)
266 {
267 const struct v4l2_fmtdesc *p = arg;
268
269 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%p4cc, mbus_code=0x%04x, description='%.*s'\n",
270 p->index, prt_names(p->type, v4l2_type_names),
271 p->flags, &p->pixelformat, p->mbus_code,
272 (int)sizeof(p->description), p->description);
273 }
274
v4l_print_format(const void * arg,bool write_only)275 static void v4l_print_format(const void *arg, bool write_only)
276 {
277 const struct v4l2_format *p = arg;
278 const struct v4l2_pix_format *pix;
279 const struct v4l2_pix_format_mplane *mp;
280 const struct v4l2_vbi_format *vbi;
281 const struct v4l2_sliced_vbi_format *sliced;
282 const struct v4l2_window *win;
283 const struct v4l2_meta_format *meta;
284 u32 pixelformat;
285 u32 planes;
286 unsigned i;
287
288 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
289 switch (p->type) {
290 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
291 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
292 pix = &p->fmt.pix;
293 pr_cont(", width=%u, height=%u, pixelformat=%p4cc, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
294 pix->width, pix->height, &pix->pixelformat,
295 prt_names(pix->field, v4l2_field_names),
296 pix->bytesperline, pix->sizeimage,
297 pix->colorspace, pix->flags, pix->ycbcr_enc,
298 pix->quantization, pix->xfer_func);
299 break;
300 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
301 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
302 mp = &p->fmt.pix_mp;
303 pixelformat = mp->pixelformat;
304 pr_cont(", width=%u, height=%u, format=%p4cc, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
305 mp->width, mp->height, &pixelformat,
306 prt_names(mp->field, v4l2_field_names),
307 mp->colorspace, mp->num_planes, mp->flags,
308 mp->ycbcr_enc, mp->quantization, mp->xfer_func);
309 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
310 for (i = 0; i < planes; i++)
311 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
312 mp->plane_fmt[i].bytesperline,
313 mp->plane_fmt[i].sizeimage);
314 break;
315 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
316 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
317 win = &p->fmt.win;
318 /* Note: we can't print the clip list here since the clips
319 * pointer is a userspace pointer, not a kernelspace
320 * pointer. */
321 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
322 win->w.width, win->w.height, win->w.left, win->w.top,
323 prt_names(win->field, v4l2_field_names),
324 win->chromakey, win->clipcount, win->clips,
325 win->bitmap, win->global_alpha);
326 break;
327 case V4L2_BUF_TYPE_VBI_CAPTURE:
328 case V4L2_BUF_TYPE_VBI_OUTPUT:
329 vbi = &p->fmt.vbi;
330 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%p4cc, start=%u,%u, count=%u,%u\n",
331 vbi->sampling_rate, vbi->offset,
332 vbi->samples_per_line, &vbi->sample_format,
333 vbi->start[0], vbi->start[1],
334 vbi->count[0], vbi->count[1]);
335 break;
336 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
337 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
338 sliced = &p->fmt.sliced;
339 pr_cont(", service_set=0x%08x, io_size=%d\n",
340 sliced->service_set, sliced->io_size);
341 for (i = 0; i < 24; i++)
342 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
343 sliced->service_lines[0][i],
344 sliced->service_lines[1][i]);
345 break;
346 case V4L2_BUF_TYPE_SDR_CAPTURE:
347 case V4L2_BUF_TYPE_SDR_OUTPUT:
348 pixelformat = p->fmt.sdr.pixelformat;
349 pr_cont(", pixelformat=%p4cc\n", &pixelformat);
350 break;
351 case V4L2_BUF_TYPE_META_CAPTURE:
352 case V4L2_BUF_TYPE_META_OUTPUT:
353 meta = &p->fmt.meta;
354 pixelformat = meta->dataformat;
355 pr_cont(", dataformat=%p4cc, buffersize=%u\n",
356 &pixelformat, meta->buffersize);
357 break;
358 }
359 }
360
v4l_print_framebuffer(const void * arg,bool write_only)361 static void v4l_print_framebuffer(const void *arg, bool write_only)
362 {
363 const struct v4l2_framebuffer *p = arg;
364
365 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%p4cc, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
366 p->capability, p->flags, p->base, p->fmt.width, p->fmt.height,
367 &p->fmt.pixelformat, p->fmt.bytesperline, p->fmt.sizeimage,
368 p->fmt.colorspace);
369 }
370
v4l_print_buftype(const void * arg,bool write_only)371 static void v4l_print_buftype(const void *arg, bool write_only)
372 {
373 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
374 }
375
v4l_print_modulator(const void * arg,bool write_only)376 static void v4l_print_modulator(const void *arg, bool write_only)
377 {
378 const struct v4l2_modulator *p = arg;
379
380 if (write_only)
381 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
382 else
383 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
384 p->index, (int)sizeof(p->name), p->name, p->capability,
385 p->rangelow, p->rangehigh, p->txsubchans);
386 }
387
v4l_print_tuner(const void * arg,bool write_only)388 static void v4l_print_tuner(const void *arg, bool write_only)
389 {
390 const struct v4l2_tuner *p = arg;
391
392 if (write_only)
393 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
394 else
395 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
396 p->index, (int)sizeof(p->name), p->name, p->type,
397 p->capability, p->rangelow,
398 p->rangehigh, p->signal, p->afc,
399 p->rxsubchans, p->audmode);
400 }
401
v4l_print_frequency(const void * arg,bool write_only)402 static void v4l_print_frequency(const void *arg, bool write_only)
403 {
404 const struct v4l2_frequency *p = arg;
405
406 pr_cont("tuner=%u, type=%u, frequency=%u\n",
407 p->tuner, p->type, p->frequency);
408 }
409
v4l_print_standard(const void * arg,bool write_only)410 static void v4l_print_standard(const void *arg, bool write_only)
411 {
412 const struct v4l2_standard *p = arg;
413
414 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
415 p->index,
416 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
417 p->frameperiod.numerator,
418 p->frameperiod.denominator,
419 p->framelines);
420 }
421
v4l_print_std(const void * arg,bool write_only)422 static void v4l_print_std(const void *arg, bool write_only)
423 {
424 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
425 }
426
v4l_print_hw_freq_seek(const void * arg,bool write_only)427 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
428 {
429 const struct v4l2_hw_freq_seek *p = arg;
430
431 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
432 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
433 p->rangelow, p->rangehigh);
434 }
435
v4l_print_requestbuffers(const void * arg,bool write_only)436 static void v4l_print_requestbuffers(const void *arg, bool write_only)
437 {
438 const struct v4l2_requestbuffers *p = arg;
439
440 pr_cont("count=%d, type=%s, memory=%s\n",
441 p->count,
442 prt_names(p->type, v4l2_type_names),
443 prt_names(p->memory, v4l2_memory_names));
444 }
445
v4l_print_buffer(const void * arg,bool write_only)446 static void v4l_print_buffer(const void *arg, bool write_only)
447 {
448 const struct v4l2_buffer *p = arg;
449 const struct v4l2_timecode *tc = &p->timecode;
450 const struct v4l2_plane *plane;
451 int i;
452
453 pr_cont("%02d:%02d:%02d.%06ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
454 (int)p->timestamp.tv_sec / 3600,
455 ((int)p->timestamp.tv_sec / 60) % 60,
456 ((int)p->timestamp.tv_sec % 60),
457 (long)p->timestamp.tv_usec,
458 p->index,
459 prt_names(p->type, v4l2_type_names), p->request_fd,
460 p->flags, prt_names(p->field, v4l2_field_names),
461 p->sequence, prt_names(p->memory, v4l2_memory_names));
462
463 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
464 pr_cont("\n");
465 for (i = 0; i < p->length; ++i) {
466 plane = &p->m.planes[i];
467 printk(KERN_DEBUG
468 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
469 i, plane->bytesused, plane->data_offset,
470 plane->m.userptr, plane->length);
471 }
472 } else {
473 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
474 p->bytesused, p->m.userptr, p->length);
475 }
476
477 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
478 tc->hours, tc->minutes, tc->seconds,
479 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
480 }
481
v4l_print_exportbuffer(const void * arg,bool write_only)482 static void v4l_print_exportbuffer(const void *arg, bool write_only)
483 {
484 const struct v4l2_exportbuffer *p = arg;
485
486 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
487 p->fd, prt_names(p->type, v4l2_type_names),
488 p->index, p->plane, p->flags);
489 }
490
v4l_print_create_buffers(const void * arg,bool write_only)491 static void v4l_print_create_buffers(const void *arg, bool write_only)
492 {
493 const struct v4l2_create_buffers *p = arg;
494
495 pr_cont("index=%d, count=%d, memory=%s, capabilities=0x%08x, ",
496 p->index, p->count, prt_names(p->memory, v4l2_memory_names),
497 p->capabilities);
498 v4l_print_format(&p->format, write_only);
499 }
500
v4l_print_streamparm(const void * arg,bool write_only)501 static void v4l_print_streamparm(const void *arg, bool write_only)
502 {
503 const struct v4l2_streamparm *p = arg;
504
505 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
506
507 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
508 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
509 const struct v4l2_captureparm *c = &p->parm.capture;
510
511 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
512 c->capability, c->capturemode,
513 c->timeperframe.numerator, c->timeperframe.denominator,
514 c->extendedmode, c->readbuffers);
515 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
516 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
517 const struct v4l2_outputparm *c = &p->parm.output;
518
519 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
520 c->capability, c->outputmode,
521 c->timeperframe.numerator, c->timeperframe.denominator,
522 c->extendedmode, c->writebuffers);
523 } else {
524 pr_cont("\n");
525 }
526 }
527
v4l_print_queryctrl(const void * arg,bool write_only)528 static void v4l_print_queryctrl(const void *arg, bool write_only)
529 {
530 const struct v4l2_queryctrl *p = arg;
531
532 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
533 p->id, p->type, (int)sizeof(p->name), p->name,
534 p->minimum, p->maximum,
535 p->step, p->default_value, p->flags);
536 }
537
v4l_print_query_ext_ctrl(const void * arg,bool write_only)538 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
539 {
540 const struct v4l2_query_ext_ctrl *p = arg;
541
542 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
543 p->id, p->type, (int)sizeof(p->name), p->name,
544 p->minimum, p->maximum,
545 p->step, p->default_value, p->flags,
546 p->elem_size, p->elems, p->nr_of_dims,
547 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
548 }
549
v4l_print_querymenu(const void * arg,bool write_only)550 static void v4l_print_querymenu(const void *arg, bool write_only)
551 {
552 const struct v4l2_querymenu *p = arg;
553
554 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
555 }
556
v4l_print_control(const void * arg,bool write_only)557 static void v4l_print_control(const void *arg, bool write_only)
558 {
559 const struct v4l2_control *p = arg;
560 const char *name = v4l2_ctrl_get_name(p->id);
561
562 if (name)
563 pr_cont("name=%s, ", name);
564 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
565 }
566
v4l_print_ext_controls(const void * arg,bool write_only)567 static void v4l_print_ext_controls(const void *arg, bool write_only)
568 {
569 const struct v4l2_ext_controls *p = arg;
570 int i;
571
572 pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
573 p->which, p->count, p->error_idx, p->request_fd);
574 for (i = 0; i < p->count; i++) {
575 unsigned int id = p->controls[i].id;
576 const char *name = v4l2_ctrl_get_name(id);
577
578 if (name)
579 pr_cont(", name=%s", name);
580 if (!p->controls[i].size)
581 pr_cont(", id/val=0x%x/0x%x", id, p->controls[i].value);
582 else
583 pr_cont(", id/size=0x%x/%u", id, p->controls[i].size);
584 }
585 pr_cont("\n");
586 }
587
v4l_print_cropcap(const void * arg,bool write_only)588 static void v4l_print_cropcap(const void *arg, bool write_only)
589 {
590 const struct v4l2_cropcap *p = arg;
591
592 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
593 prt_names(p->type, v4l2_type_names),
594 p->bounds.width, p->bounds.height,
595 p->bounds.left, p->bounds.top,
596 p->defrect.width, p->defrect.height,
597 p->defrect.left, p->defrect.top,
598 p->pixelaspect.numerator, p->pixelaspect.denominator);
599 }
600
v4l_print_crop(const void * arg,bool write_only)601 static void v4l_print_crop(const void *arg, bool write_only)
602 {
603 const struct v4l2_crop *p = arg;
604
605 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
606 prt_names(p->type, v4l2_type_names),
607 p->c.width, p->c.height,
608 p->c.left, p->c.top);
609 }
610
v4l_print_selection(const void * arg,bool write_only)611 static void v4l_print_selection(const void *arg, bool write_only)
612 {
613 const struct v4l2_selection *p = arg;
614
615 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
616 prt_names(p->type, v4l2_type_names),
617 p->target, p->flags,
618 p->r.width, p->r.height, p->r.left, p->r.top);
619 }
620
v4l_print_jpegcompression(const void * arg,bool write_only)621 static void v4l_print_jpegcompression(const void *arg, bool write_only)
622 {
623 const struct v4l2_jpegcompression *p = arg;
624
625 pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
626 p->quality, p->APPn, p->APP_len,
627 p->COM_len, p->jpeg_markers);
628 }
629
v4l_print_enc_idx(const void * arg,bool write_only)630 static void v4l_print_enc_idx(const void *arg, bool write_only)
631 {
632 const struct v4l2_enc_idx *p = arg;
633
634 pr_cont("entries=%d, entries_cap=%d\n",
635 p->entries, p->entries_cap);
636 }
637
v4l_print_encoder_cmd(const void * arg,bool write_only)638 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
639 {
640 const struct v4l2_encoder_cmd *p = arg;
641
642 pr_cont("cmd=%d, flags=0x%x\n",
643 p->cmd, p->flags);
644 }
645
v4l_print_decoder_cmd(const void * arg,bool write_only)646 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
647 {
648 const struct v4l2_decoder_cmd *p = arg;
649
650 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
651
652 if (p->cmd == V4L2_DEC_CMD_START)
653 pr_info("speed=%d, format=%u\n",
654 p->start.speed, p->start.format);
655 else if (p->cmd == V4L2_DEC_CMD_STOP)
656 pr_info("pts=%llu\n", p->stop.pts);
657 }
658
v4l_print_dbg_chip_info(const void * arg,bool write_only)659 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
660 {
661 const struct v4l2_dbg_chip_info *p = arg;
662
663 pr_cont("type=%u, ", p->match.type);
664 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
665 pr_cont("name=%.*s, ",
666 (int)sizeof(p->match.name), p->match.name);
667 else
668 pr_cont("addr=%u, ", p->match.addr);
669 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
670 }
671
v4l_print_dbg_register(const void * arg,bool write_only)672 static void v4l_print_dbg_register(const void *arg, bool write_only)
673 {
674 const struct v4l2_dbg_register *p = arg;
675
676 pr_cont("type=%u, ", p->match.type);
677 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
678 pr_cont("name=%.*s, ",
679 (int)sizeof(p->match.name), p->match.name);
680 else
681 pr_cont("addr=%u, ", p->match.addr);
682 pr_cont("reg=0x%llx, val=0x%llx\n",
683 p->reg, p->val);
684 }
685
v4l_print_dv_timings(const void * arg,bool write_only)686 static void v4l_print_dv_timings(const void *arg, bool write_only)
687 {
688 const struct v4l2_dv_timings *p = arg;
689
690 switch (p->type) {
691 case V4L2_DV_BT_656_1120:
692 pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
693 p->bt.interlaced, p->bt.pixelclock,
694 p->bt.width, p->bt.height,
695 p->bt.polarities, p->bt.hfrontporch,
696 p->bt.hsync, p->bt.hbackporch,
697 p->bt.vfrontporch, p->bt.vsync,
698 p->bt.vbackporch, p->bt.il_vfrontporch,
699 p->bt.il_vsync, p->bt.il_vbackporch,
700 p->bt.standards, p->bt.flags);
701 break;
702 default:
703 pr_cont("type=%d\n", p->type);
704 break;
705 }
706 }
707
v4l_print_enum_dv_timings(const void * arg,bool write_only)708 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
709 {
710 const struct v4l2_enum_dv_timings *p = arg;
711
712 pr_cont("index=%u, ", p->index);
713 v4l_print_dv_timings(&p->timings, write_only);
714 }
715
v4l_print_dv_timings_cap(const void * arg,bool write_only)716 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
717 {
718 const struct v4l2_dv_timings_cap *p = arg;
719
720 switch (p->type) {
721 case V4L2_DV_BT_656_1120:
722 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
723 p->bt.min_width, p->bt.max_width,
724 p->bt.min_height, p->bt.max_height,
725 p->bt.min_pixelclock, p->bt.max_pixelclock,
726 p->bt.standards, p->bt.capabilities);
727 break;
728 default:
729 pr_cont("type=%u\n", p->type);
730 break;
731 }
732 }
733
v4l_print_frmsizeenum(const void * arg,bool write_only)734 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
735 {
736 const struct v4l2_frmsizeenum *p = arg;
737
738 pr_cont("index=%u, pixelformat=%p4cc, type=%u",
739 p->index, &p->pixel_format, p->type);
740 switch (p->type) {
741 case V4L2_FRMSIZE_TYPE_DISCRETE:
742 pr_cont(", wxh=%ux%u\n",
743 p->discrete.width, p->discrete.height);
744 break;
745 case V4L2_FRMSIZE_TYPE_STEPWISE:
746 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
747 p->stepwise.min_width,
748 p->stepwise.min_height,
749 p->stepwise.max_width,
750 p->stepwise.max_height,
751 p->stepwise.step_width,
752 p->stepwise.step_height);
753 break;
754 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
755 default:
756 pr_cont("\n");
757 break;
758 }
759 }
760
v4l_print_frmivalenum(const void * arg,bool write_only)761 static void v4l_print_frmivalenum(const void *arg, bool write_only)
762 {
763 const struct v4l2_frmivalenum *p = arg;
764
765 pr_cont("index=%u, pixelformat=%p4cc, wxh=%ux%u, type=%u",
766 p->index, &p->pixel_format, p->width, p->height, p->type);
767 switch (p->type) {
768 case V4L2_FRMIVAL_TYPE_DISCRETE:
769 pr_cont(", fps=%d/%d\n",
770 p->discrete.numerator,
771 p->discrete.denominator);
772 break;
773 case V4L2_FRMIVAL_TYPE_STEPWISE:
774 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
775 p->stepwise.min.numerator,
776 p->stepwise.min.denominator,
777 p->stepwise.max.numerator,
778 p->stepwise.max.denominator,
779 p->stepwise.step.numerator,
780 p->stepwise.step.denominator);
781 break;
782 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
783 default:
784 pr_cont("\n");
785 break;
786 }
787 }
788
v4l_print_event(const void * arg,bool write_only)789 static void v4l_print_event(const void *arg, bool write_only)
790 {
791 const struct v4l2_event *p = arg;
792 const struct v4l2_event_ctrl *c;
793
794 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%llu.%9.9llu\n",
795 p->type, p->pending, p->sequence, p->id,
796 p->timestamp.tv_sec, p->timestamp.tv_nsec);
797 switch (p->type) {
798 case V4L2_EVENT_VSYNC:
799 printk(KERN_DEBUG "field=%s\n",
800 prt_names(p->u.vsync.field, v4l2_field_names));
801 break;
802 case V4L2_EVENT_CTRL:
803 c = &p->u.ctrl;
804 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
805 c->changes, c->type);
806 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
807 pr_cont("value64=%lld, ", c->value64);
808 else
809 pr_cont("value=%d, ", c->value);
810 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
811 c->flags, c->minimum, c->maximum,
812 c->step, c->default_value);
813 break;
814 case V4L2_EVENT_FRAME_SYNC:
815 pr_cont("frame_sequence=%u\n",
816 p->u.frame_sync.frame_sequence);
817 break;
818 }
819 }
820
v4l_print_event_subscription(const void * arg,bool write_only)821 static void v4l_print_event_subscription(const void *arg, bool write_only)
822 {
823 const struct v4l2_event_subscription *p = arg;
824
825 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
826 p->type, p->id, p->flags);
827 }
828
v4l_print_sliced_vbi_cap(const void * arg,bool write_only)829 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
830 {
831 const struct v4l2_sliced_vbi_cap *p = arg;
832 int i;
833
834 pr_cont("type=%s, service_set=0x%08x\n",
835 prt_names(p->type, v4l2_type_names), p->service_set);
836 for (i = 0; i < 24; i++)
837 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
838 p->service_lines[0][i],
839 p->service_lines[1][i]);
840 }
841
v4l_print_freq_band(const void * arg,bool write_only)842 static void v4l_print_freq_band(const void *arg, bool write_only)
843 {
844 const struct v4l2_frequency_band *p = arg;
845
846 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
847 p->tuner, p->type, p->index,
848 p->capability, p->rangelow,
849 p->rangehigh, p->modulation);
850 }
851
v4l_print_edid(const void * arg,bool write_only)852 static void v4l_print_edid(const void *arg, bool write_only)
853 {
854 const struct v4l2_edid *p = arg;
855
856 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
857 p->pad, p->start_block, p->blocks);
858 }
859
v4l_print_u32(const void * arg,bool write_only)860 static void v4l_print_u32(const void *arg, bool write_only)
861 {
862 pr_cont("value=%u\n", *(const u32 *)arg);
863 }
864
v4l_print_newline(const void * arg,bool write_only)865 static void v4l_print_newline(const void *arg, bool write_only)
866 {
867 pr_cont("\n");
868 }
869
v4l_print_default(const void * arg,bool write_only)870 static void v4l_print_default(const void *arg, bool write_only)
871 {
872 pr_cont("driver-specific ioctl\n");
873 }
874
check_ext_ctrls(struct v4l2_ext_controls * c,unsigned long ioctl)875 static bool check_ext_ctrls(struct v4l2_ext_controls *c, unsigned long ioctl)
876 {
877 __u32 i;
878
879 /* zero the reserved fields */
880 c->reserved[0] = 0;
881 for (i = 0; i < c->count; i++)
882 c->controls[i].reserved2[0] = 0;
883
884 switch (c->which) {
885 case V4L2_CID_PRIVATE_BASE:
886 /*
887 * V4L2_CID_PRIVATE_BASE cannot be used as control class
888 * when using extended controls.
889 * Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
890 * is it allowed for backwards compatibility.
891 */
892 if (ioctl == VIDIOC_G_CTRL || ioctl == VIDIOC_S_CTRL)
893 return false;
894 break;
895 case V4L2_CTRL_WHICH_DEF_VAL:
896 /* Default value cannot be changed */
897 if (ioctl == VIDIOC_S_EXT_CTRLS ||
898 ioctl == VIDIOC_TRY_EXT_CTRLS) {
899 c->error_idx = c->count;
900 return false;
901 }
902 return true;
903 case V4L2_CTRL_WHICH_CUR_VAL:
904 return true;
905 case V4L2_CTRL_WHICH_REQUEST_VAL:
906 c->error_idx = c->count;
907 return false;
908 }
909
910 /* Check that all controls are from the same control class. */
911 for (i = 0; i < c->count; i++) {
912 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
913 c->error_idx = ioctl == VIDIOC_TRY_EXT_CTRLS ? i :
914 c->count;
915 return false;
916 }
917 }
918 return true;
919 }
920
check_fmt(struct file * file,enum v4l2_buf_type type)921 static int check_fmt(struct file *file, enum v4l2_buf_type type)
922 {
923 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
924 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
925 V4L2_CAP_VIDEO_OUTPUT |
926 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
927 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
928 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
929 V4L2_CAP_META_OUTPUT;
930 struct video_device *vfd = video_devdata(file);
931 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
932 bool is_vid = vfd->vfl_type == VFL_TYPE_VIDEO &&
933 (vfd->device_caps & vid_caps);
934 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
935 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
936 bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
937 bool is_meta = vfd->vfl_type == VFL_TYPE_VIDEO &&
938 (vfd->device_caps & meta_caps);
939 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
940 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
941
942 if (ops == NULL)
943 return -EINVAL;
944
945 switch (type) {
946 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
947 if ((is_vid || is_tch) && is_rx &&
948 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
949 return 0;
950 break;
951 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
952 if ((is_vid || is_tch) && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
953 return 0;
954 break;
955 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
956 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
957 return 0;
958 break;
959 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
960 if (is_vid && is_tx &&
961 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
962 return 0;
963 break;
964 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
965 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
966 return 0;
967 break;
968 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
969 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
970 return 0;
971 break;
972 case V4L2_BUF_TYPE_VBI_CAPTURE:
973 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
974 return 0;
975 break;
976 case V4L2_BUF_TYPE_VBI_OUTPUT:
977 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
978 return 0;
979 break;
980 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
981 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
982 return 0;
983 break;
984 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
985 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
986 return 0;
987 break;
988 case V4L2_BUF_TYPE_SDR_CAPTURE:
989 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
990 return 0;
991 break;
992 case V4L2_BUF_TYPE_SDR_OUTPUT:
993 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
994 return 0;
995 break;
996 case V4L2_BUF_TYPE_META_CAPTURE:
997 if (is_meta && is_rx && ops->vidioc_g_fmt_meta_cap)
998 return 0;
999 break;
1000 case V4L2_BUF_TYPE_META_OUTPUT:
1001 if (is_meta && is_tx && ops->vidioc_g_fmt_meta_out)
1002 return 0;
1003 break;
1004 default:
1005 break;
1006 }
1007 return -EINVAL;
1008 }
1009
v4l_sanitize_colorspace(u32 pixelformat,u32 * colorspace,u32 * encoding,u32 * quantization,u32 * xfer_func)1010 static void v4l_sanitize_colorspace(u32 pixelformat, u32 *colorspace,
1011 u32 *encoding, u32 *quantization,
1012 u32 *xfer_func)
1013 {
1014 bool is_hsv = pixelformat == V4L2_PIX_FMT_HSV24 ||
1015 pixelformat == V4L2_PIX_FMT_HSV32;
1016
1017 if (!v4l2_is_colorspace_valid(*colorspace)) {
1018 *colorspace = V4L2_COLORSPACE_DEFAULT;
1019 *encoding = V4L2_YCBCR_ENC_DEFAULT;
1020 *quantization = V4L2_QUANTIZATION_DEFAULT;
1021 *xfer_func = V4L2_XFER_FUNC_DEFAULT;
1022 }
1023
1024 if ((!is_hsv && !v4l2_is_ycbcr_enc_valid(*encoding)) ||
1025 (is_hsv && !v4l2_is_hsv_enc_valid(*encoding)))
1026 *encoding = V4L2_YCBCR_ENC_DEFAULT;
1027
1028 if (!v4l2_is_quant_valid(*quantization))
1029 *quantization = V4L2_QUANTIZATION_DEFAULT;
1030
1031 if (!v4l2_is_xfer_func_valid(*xfer_func))
1032 *xfer_func = V4L2_XFER_FUNC_DEFAULT;
1033 }
1034
v4l_sanitize_format(struct v4l2_format * fmt)1035 static void v4l_sanitize_format(struct v4l2_format *fmt)
1036 {
1037 unsigned int offset;
1038
1039 /* Make sure num_planes is not bogus */
1040 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1041 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1042 fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1043 VIDEO_MAX_PLANES);
1044
1045 /*
1046 * The v4l2_pix_format structure has been extended with fields that were
1047 * not previously required to be set to zero by applications. The priv
1048 * field, when set to a magic value, indicates that the extended fields
1049 * are valid. Otherwise they will contain undefined values. To simplify
1050 * the API towards drivers zero the extended fields and set the priv
1051 * field to the magic value when the extended pixel format structure
1052 * isn't used by applications.
1053 */
1054 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1055 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1056 if (fmt->fmt.pix.priv != V4L2_PIX_FMT_PRIV_MAGIC) {
1057 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1058
1059 offset = offsetof(struct v4l2_pix_format, priv)
1060 + sizeof(fmt->fmt.pix.priv);
1061 memset(((void *)&fmt->fmt.pix) + offset, 0,
1062 sizeof(fmt->fmt.pix) - offset);
1063 }
1064 }
1065
1066 /* Replace invalid colorspace values with defaults. */
1067 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1068 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1069 v4l_sanitize_colorspace(fmt->fmt.pix.pixelformat,
1070 &fmt->fmt.pix.colorspace,
1071 &fmt->fmt.pix.ycbcr_enc,
1072 &fmt->fmt.pix.quantization,
1073 &fmt->fmt.pix.xfer_func);
1074 } else if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1075 fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1076 u32 ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;
1077 u32 quantization = fmt->fmt.pix_mp.quantization;
1078 u32 xfer_func = fmt->fmt.pix_mp.xfer_func;
1079
1080 v4l_sanitize_colorspace(fmt->fmt.pix_mp.pixelformat,
1081 &fmt->fmt.pix_mp.colorspace, &ycbcr_enc,
1082 &quantization, &xfer_func);
1083
1084 fmt->fmt.pix_mp.ycbcr_enc = ycbcr_enc;
1085 fmt->fmt.pix_mp.quantization = quantization;
1086 fmt->fmt.pix_mp.xfer_func = xfer_func;
1087 }
1088 }
1089
v4l_querycap(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1090 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1091 struct file *file, void *fh, void *arg)
1092 {
1093 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1094 struct video_device *vfd = video_devdata(file);
1095 int ret;
1096
1097 cap->version = LINUX_VERSION_CODE;
1098 cap->device_caps = vfd->device_caps;
1099 cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1100
1101 media_set_bus_info(cap->bus_info, sizeof(cap->bus_info),
1102 vfd->dev_parent);
1103
1104 ret = ops->vidioc_querycap(file, fh, cap);
1105
1106 /*
1107 * Drivers must not change device_caps, so check for this and
1108 * warn if this happened.
1109 */
1110 WARN_ON(cap->device_caps != vfd->device_caps);
1111 /*
1112 * Check that capabilities is a superset of
1113 * vfd->device_caps | V4L2_CAP_DEVICE_CAPS
1114 */
1115 WARN_ON((cap->capabilities &
1116 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS)) !=
1117 (vfd->device_caps | V4L2_CAP_DEVICE_CAPS));
1118 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1119 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1120
1121 return ret;
1122 }
1123
v4l_g_input(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1124 static int v4l_g_input(const struct v4l2_ioctl_ops *ops,
1125 struct file *file, void *fh, void *arg)
1126 {
1127 struct video_device *vfd = video_devdata(file);
1128
1129 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1130 *(int *)arg = 0;
1131 return 0;
1132 }
1133
1134 return ops->vidioc_g_input(file, fh, arg);
1135 }
1136
v4l_g_output(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1137 static int v4l_g_output(const struct v4l2_ioctl_ops *ops,
1138 struct file *file, void *fh, void *arg)
1139 {
1140 struct video_device *vfd = video_devdata(file);
1141
1142 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1143 *(int *)arg = 0;
1144 return 0;
1145 }
1146
1147 return ops->vidioc_g_output(file, fh, arg);
1148 }
1149
v4l_s_input(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1150 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1151 struct file *file, void *fh, void *arg)
1152 {
1153 struct video_device *vfd = video_devdata(file);
1154 int ret;
1155
1156 ret = v4l_enable_media_source(vfd);
1157 if (ret)
1158 return ret;
1159
1160 if (vfd->device_caps & V4L2_CAP_IO_MC)
1161 return *(int *)arg ? -EINVAL : 0;
1162
1163 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1164 }
1165
v4l_s_output(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1166 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1167 struct file *file, void *fh, void *arg)
1168 {
1169 struct video_device *vfd = video_devdata(file);
1170
1171 if (vfd->device_caps & V4L2_CAP_IO_MC)
1172 return *(int *)arg ? -EINVAL : 0;
1173
1174 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1175 }
1176
v4l_g_priority(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1177 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1178 struct file *file, void *fh, void *arg)
1179 {
1180 struct video_device *vfd;
1181 u32 *p = arg;
1182
1183 vfd = video_devdata(file);
1184 *p = v4l2_prio_max(vfd->prio);
1185 return 0;
1186 }
1187
v4l_s_priority(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1188 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1189 struct file *file, void *fh, void *arg)
1190 {
1191 struct video_device *vfd;
1192 struct v4l2_fh *vfh;
1193 u32 *p = arg;
1194
1195 vfd = video_devdata(file);
1196 if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1197 return -ENOTTY;
1198 vfh = file->private_data;
1199 return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1200 }
1201
v4l_enuminput(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1202 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1203 struct file *file, void *fh, void *arg)
1204 {
1205 struct video_device *vfd = video_devdata(file);
1206 struct v4l2_input *p = arg;
1207
1208 /*
1209 * We set the flags for CAP_DV_TIMINGS &
1210 * CAP_STD here based on ioctl handler provided by the
1211 * driver. If the driver doesn't support these
1212 * for a specific input, it must override these flags.
1213 */
1214 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1215 p->capabilities |= V4L2_IN_CAP_STD;
1216
1217 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1218 if (p->index)
1219 return -EINVAL;
1220 strscpy(p->name, vfd->name, sizeof(p->name));
1221 p->type = V4L2_INPUT_TYPE_CAMERA;
1222 return 0;
1223 }
1224
1225 return ops->vidioc_enum_input(file, fh, p);
1226 }
1227
v4l_enumoutput(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1228 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1229 struct file *file, void *fh, void *arg)
1230 {
1231 struct video_device *vfd = video_devdata(file);
1232 struct v4l2_output *p = arg;
1233
1234 /*
1235 * We set the flags for CAP_DV_TIMINGS &
1236 * CAP_STD here based on ioctl handler provided by the
1237 * driver. If the driver doesn't support these
1238 * for a specific output, it must override these flags.
1239 */
1240 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1241 p->capabilities |= V4L2_OUT_CAP_STD;
1242
1243 if (vfd->device_caps & V4L2_CAP_IO_MC) {
1244 if (p->index)
1245 return -EINVAL;
1246 strscpy(p->name, vfd->name, sizeof(p->name));
1247 p->type = V4L2_OUTPUT_TYPE_ANALOG;
1248 return 0;
1249 }
1250
1251 return ops->vidioc_enum_output(file, fh, p);
1252 }
1253
v4l_fill_fmtdesc(struct v4l2_fmtdesc * fmt)1254 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1255 {
1256 const unsigned sz = sizeof(fmt->description);
1257 const char *descr = NULL;
1258 u32 flags = 0;
1259
1260 /*
1261 * We depart from the normal coding style here since the descriptions
1262 * should be aligned so it is easy to see which descriptions will be
1263 * longer than 31 characters (the max length for a description).
1264 * And frankly, this is easier to read anyway.
1265 *
1266 * Note that gcc will use O(log N) comparisons to find the right case.
1267 */
1268 switch (fmt->pixelformat) {
1269 /* Max description length mask: descr = "0123456789012345678901234567890" */
1270 case V4L2_PIX_FMT_RGB332: descr = "8-bit RGB 3-3-2"; break;
1271 case V4L2_PIX_FMT_RGB444: descr = "16-bit A/XRGB 4-4-4-4"; break;
1272 case V4L2_PIX_FMT_ARGB444: descr = "16-bit ARGB 4-4-4-4"; break;
1273 case V4L2_PIX_FMT_XRGB444: descr = "16-bit XRGB 4-4-4-4"; break;
1274 case V4L2_PIX_FMT_RGBA444: descr = "16-bit RGBA 4-4-4-4"; break;
1275 case V4L2_PIX_FMT_RGBX444: descr = "16-bit RGBX 4-4-4-4"; break;
1276 case V4L2_PIX_FMT_ABGR444: descr = "16-bit ABGR 4-4-4-4"; break;
1277 case V4L2_PIX_FMT_XBGR444: descr = "16-bit XBGR 4-4-4-4"; break;
1278 case V4L2_PIX_FMT_BGRA444: descr = "16-bit BGRA 4-4-4-4"; break;
1279 case V4L2_PIX_FMT_BGRX444: descr = "16-bit BGRX 4-4-4-4"; break;
1280 case V4L2_PIX_FMT_RGB555: descr = "16-bit A/XRGB 1-5-5-5"; break;
1281 case V4L2_PIX_FMT_ARGB555: descr = "16-bit ARGB 1-5-5-5"; break;
1282 case V4L2_PIX_FMT_XRGB555: descr = "16-bit XRGB 1-5-5-5"; break;
1283 case V4L2_PIX_FMT_ABGR555: descr = "16-bit ABGR 1-5-5-5"; break;
1284 case V4L2_PIX_FMT_XBGR555: descr = "16-bit XBGR 1-5-5-5"; break;
1285 case V4L2_PIX_FMT_RGBA555: descr = "16-bit RGBA 5-5-5-1"; break;
1286 case V4L2_PIX_FMT_RGBX555: descr = "16-bit RGBX 5-5-5-1"; break;
1287 case V4L2_PIX_FMT_BGRA555: descr = "16-bit BGRA 5-5-5-1"; break;
1288 case V4L2_PIX_FMT_BGRX555: descr = "16-bit BGRX 5-5-5-1"; break;
1289 case V4L2_PIX_FMT_RGB565: descr = "16-bit RGB 5-6-5"; break;
1290 case V4L2_PIX_FMT_RGB555X: descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1291 case V4L2_PIX_FMT_ARGB555X: descr = "16-bit ARGB 1-5-5-5 BE"; break;
1292 case V4L2_PIX_FMT_XRGB555X: descr = "16-bit XRGB 1-5-5-5 BE"; break;
1293 case V4L2_PIX_FMT_RGB565X: descr = "16-bit RGB 5-6-5 BE"; break;
1294 case V4L2_PIX_FMT_BGR666: descr = "18-bit BGRX 6-6-6-14"; break;
1295 case V4L2_PIX_FMT_BGR24: descr = "24-bit BGR 8-8-8"; break;
1296 case V4L2_PIX_FMT_RGB24: descr = "24-bit RGB 8-8-8"; break;
1297 case V4L2_PIX_FMT_BGR32: descr = "32-bit BGRA/X 8-8-8-8"; break;
1298 case V4L2_PIX_FMT_ABGR32: descr = "32-bit BGRA 8-8-8-8"; break;
1299 case V4L2_PIX_FMT_XBGR32: descr = "32-bit BGRX 8-8-8-8"; break;
1300 case V4L2_PIX_FMT_RGB32: descr = "32-bit A/XRGB 8-8-8-8"; break;
1301 case V4L2_PIX_FMT_ARGB32: descr = "32-bit ARGB 8-8-8-8"; break;
1302 case V4L2_PIX_FMT_XRGB32: descr = "32-bit XRGB 8-8-8-8"; break;
1303 case V4L2_PIX_FMT_BGRA32: descr = "32-bit ABGR 8-8-8-8"; break;
1304 case V4L2_PIX_FMT_BGRX32: descr = "32-bit XBGR 8-8-8-8"; break;
1305 case V4L2_PIX_FMT_RGBA32: descr = "32-bit RGBA 8-8-8-8"; break;
1306 case V4L2_PIX_FMT_RGBX32: descr = "32-bit RGBX 8-8-8-8"; break;
1307 case V4L2_PIX_FMT_BGR48_12: descr = "12-bit Depth BGR"; break;
1308 case V4L2_PIX_FMT_ABGR64_12: descr = "12-bit Depth BGRA"; break;
1309 case V4L2_PIX_FMT_GREY: descr = "8-bit Greyscale"; break;
1310 case V4L2_PIX_FMT_Y4: descr = "4-bit Greyscale"; break;
1311 case V4L2_PIX_FMT_Y6: descr = "6-bit Greyscale"; break;
1312 case V4L2_PIX_FMT_Y10: descr = "10-bit Greyscale"; break;
1313 case V4L2_PIX_FMT_Y12: descr = "12-bit Greyscale"; break;
1314 case V4L2_PIX_FMT_Y012: descr = "12-bit Greyscale (bits 15-4)"; break;
1315 case V4L2_PIX_FMT_Y14: descr = "14-bit Greyscale"; break;
1316 case V4L2_PIX_FMT_Y16: descr = "16-bit Greyscale"; break;
1317 case V4L2_PIX_FMT_Y16_BE: descr = "16-bit Greyscale BE"; break;
1318 case V4L2_PIX_FMT_Y10BPACK: descr = "10-bit Greyscale (Packed)"; break;
1319 case V4L2_PIX_FMT_Y10P: descr = "10-bit Greyscale (MIPI Packed)"; break;
1320 case V4L2_PIX_FMT_IPU3_Y10: descr = "10-bit greyscale (IPU3 Packed)"; break;
1321 case V4L2_PIX_FMT_Y8I: descr = "Interleaved 8-bit Greyscale"; break;
1322 case V4L2_PIX_FMT_Y12I: descr = "Interleaved 12-bit Greyscale"; break;
1323 case V4L2_PIX_FMT_Z16: descr = "16-bit Depth"; break;
1324 case V4L2_PIX_FMT_INZI: descr = "Planar 10:16 Greyscale Depth"; break;
1325 case V4L2_PIX_FMT_CNF4: descr = "4-bit Depth Confidence (Packed)"; break;
1326 case V4L2_PIX_FMT_PAL8: descr = "8-bit Palette"; break;
1327 case V4L2_PIX_FMT_UV8: descr = "8-bit Chrominance UV 4-4"; break;
1328 case V4L2_PIX_FMT_YVU410: descr = "Planar YVU 4:1:0"; break;
1329 case V4L2_PIX_FMT_YVU420: descr = "Planar YVU 4:2:0"; break;
1330 case V4L2_PIX_FMT_YUYV: descr = "YUYV 4:2:2"; break;
1331 case V4L2_PIX_FMT_YYUV: descr = "YYUV 4:2:2"; break;
1332 case V4L2_PIX_FMT_YVYU: descr = "YVYU 4:2:2"; break;
1333 case V4L2_PIX_FMT_UYVY: descr = "UYVY 4:2:2"; break;
1334 case V4L2_PIX_FMT_VYUY: descr = "VYUY 4:2:2"; break;
1335 case V4L2_PIX_FMT_YUV422P: descr = "Planar YUV 4:2:2"; break;
1336 case V4L2_PIX_FMT_YUV411P: descr = "Planar YUV 4:1:1"; break;
1337 case V4L2_PIX_FMT_Y41P: descr = "YUV 4:1:1 (Packed)"; break;
1338 case V4L2_PIX_FMT_YUV444: descr = "16-bit A/XYUV 4-4-4-4"; break;
1339 case V4L2_PIX_FMT_YUV555: descr = "16-bit A/XYUV 1-5-5-5"; break;
1340 case V4L2_PIX_FMT_YUV565: descr = "16-bit YUV 5-6-5"; break;
1341 case V4L2_PIX_FMT_YUV24: descr = "24-bit YUV 4:4:4 8-8-8"; break;
1342 case V4L2_PIX_FMT_YUV32: descr = "32-bit A/XYUV 8-8-8-8"; break;
1343 case V4L2_PIX_FMT_AYUV32: descr = "32-bit AYUV 8-8-8-8"; break;
1344 case V4L2_PIX_FMT_XYUV32: descr = "32-bit XYUV 8-8-8-8"; break;
1345 case V4L2_PIX_FMT_VUYA32: descr = "32-bit VUYA 8-8-8-8"; break;
1346 case V4L2_PIX_FMT_VUYX32: descr = "32-bit VUYX 8-8-8-8"; break;
1347 case V4L2_PIX_FMT_YUVA32: descr = "32-bit YUVA 8-8-8-8"; break;
1348 case V4L2_PIX_FMT_YUVX32: descr = "32-bit YUVX 8-8-8-8"; break;
1349 case V4L2_PIX_FMT_YUV410: descr = "Planar YUV 4:1:0"; break;
1350 case V4L2_PIX_FMT_YUV420: descr = "Planar YUV 4:2:0"; break;
1351 case V4L2_PIX_FMT_HI240: descr = "8-bit Dithered RGB (BTTV)"; break;
1352 case V4L2_PIX_FMT_M420: descr = "YUV 4:2:0 (M420)"; break;
1353 case V4L2_PIX_FMT_YUV48_12: descr = "12-bit YUV 4:4:4 Packed"; break;
1354 case V4L2_PIX_FMT_NV12: descr = "Y/UV 4:2:0"; break;
1355 case V4L2_PIX_FMT_NV21: descr = "Y/VU 4:2:0"; break;
1356 case V4L2_PIX_FMT_NV16: descr = "Y/UV 4:2:2"; break;
1357 case V4L2_PIX_FMT_NV61: descr = "Y/VU 4:2:2"; break;
1358 case V4L2_PIX_FMT_NV24: descr = "Y/UV 4:4:4"; break;
1359 case V4L2_PIX_FMT_NV42: descr = "Y/VU 4:4:4"; break;
1360 case V4L2_PIX_FMT_P010: descr = "10-bit Y/UV 4:2:0"; break;
1361 case V4L2_PIX_FMT_P012: descr = "12-bit Y/UV 4:2:0"; break;
1362 case V4L2_PIX_FMT_NV12_4L4: descr = "Y/UV 4:2:0 (4x4 Linear)"; break;
1363 case V4L2_PIX_FMT_NV12_16L16: descr = "Y/UV 4:2:0 (16x16 Linear)"; break;
1364 case V4L2_PIX_FMT_NV12_32L32: descr = "Y/UV 4:2:0 (32x32 Linear)"; break;
1365 case V4L2_PIX_FMT_P010_4L4: descr = "10-bit Y/UV 4:2:0 (4x4 Linear)"; break;
1366 case V4L2_PIX_FMT_NV12M: descr = "Y/UV 4:2:0 (N-C)"; break;
1367 case V4L2_PIX_FMT_NV21M: descr = "Y/VU 4:2:0 (N-C)"; break;
1368 case V4L2_PIX_FMT_NV16M: descr = "Y/UV 4:2:2 (N-C)"; break;
1369 case V4L2_PIX_FMT_NV61M: descr = "Y/VU 4:2:2 (N-C)"; break;
1370 case V4L2_PIX_FMT_NV12MT: descr = "Y/UV 4:2:0 (64x32 MB, N-C)"; break;
1371 case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/UV 4:2:0 (16x16 MB, N-C)"; break;
1372 case V4L2_PIX_FMT_P012M: descr = "12-bit Y/UV 4:2:0 (N-C)"; break;
1373 case V4L2_PIX_FMT_YUV420M: descr = "Planar YUV 4:2:0 (N-C)"; break;
1374 case V4L2_PIX_FMT_YVU420M: descr = "Planar YVU 4:2:0 (N-C)"; break;
1375 case V4L2_PIX_FMT_YUV422M: descr = "Planar YUV 4:2:2 (N-C)"; break;
1376 case V4L2_PIX_FMT_YVU422M: descr = "Planar YVU 4:2:2 (N-C)"; break;
1377 case V4L2_PIX_FMT_YUV444M: descr = "Planar YUV 4:4:4 (N-C)"; break;
1378 case V4L2_PIX_FMT_YVU444M: descr = "Planar YVU 4:4:4 (N-C)"; break;
1379 case V4L2_PIX_FMT_SBGGR8: descr = "8-bit Bayer BGBG/GRGR"; break;
1380 case V4L2_PIX_FMT_SGBRG8: descr = "8-bit Bayer GBGB/RGRG"; break;
1381 case V4L2_PIX_FMT_SGRBG8: descr = "8-bit Bayer GRGR/BGBG"; break;
1382 case V4L2_PIX_FMT_SRGGB8: descr = "8-bit Bayer RGRG/GBGB"; break;
1383 case V4L2_PIX_FMT_SBGGR10: descr = "10-bit Bayer BGBG/GRGR"; break;
1384 case V4L2_PIX_FMT_SGBRG10: descr = "10-bit Bayer GBGB/RGRG"; break;
1385 case V4L2_PIX_FMT_SGRBG10: descr = "10-bit Bayer GRGR/BGBG"; break;
1386 case V4L2_PIX_FMT_SRGGB10: descr = "10-bit Bayer RGRG/GBGB"; break;
1387 case V4L2_PIX_FMT_SBGGR10P: descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1388 case V4L2_PIX_FMT_SGBRG10P: descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1389 case V4L2_PIX_FMT_SGRBG10P: descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1390 case V4L2_PIX_FMT_SRGGB10P: descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1391 case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1392 case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1393 case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1394 case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1395 case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1396 case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1397 case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1398 case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1399 case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1400 case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1401 case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1402 case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1403 case V4L2_PIX_FMT_SBGGR12: descr = "12-bit Bayer BGBG/GRGR"; break;
1404 case V4L2_PIX_FMT_SGBRG12: descr = "12-bit Bayer GBGB/RGRG"; break;
1405 case V4L2_PIX_FMT_SGRBG12: descr = "12-bit Bayer GRGR/BGBG"; break;
1406 case V4L2_PIX_FMT_SRGGB12: descr = "12-bit Bayer RGRG/GBGB"; break;
1407 case V4L2_PIX_FMT_SBGGR12P: descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1408 case V4L2_PIX_FMT_SGBRG12P: descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1409 case V4L2_PIX_FMT_SGRBG12P: descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1410 case V4L2_PIX_FMT_SRGGB12P: descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1411 case V4L2_PIX_FMT_SBGGR14: descr = "14-bit Bayer BGBG/GRGR"; break;
1412 case V4L2_PIX_FMT_SGBRG14: descr = "14-bit Bayer GBGB/RGRG"; break;
1413 case V4L2_PIX_FMT_SGRBG14: descr = "14-bit Bayer GRGR/BGBG"; break;
1414 case V4L2_PIX_FMT_SRGGB14: descr = "14-bit Bayer RGRG/GBGB"; break;
1415 case V4L2_PIX_FMT_SBGGR14P: descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1416 case V4L2_PIX_FMT_SGBRG14P: descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1417 case V4L2_PIX_FMT_SGRBG14P: descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1418 case V4L2_PIX_FMT_SRGGB14P: descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1419 case V4L2_PIX_FMT_SBGGR16: descr = "16-bit Bayer BGBG/GRGR"; break;
1420 case V4L2_PIX_FMT_SGBRG16: descr = "16-bit Bayer GBGB/RGRG"; break;
1421 case V4L2_PIX_FMT_SGRBG16: descr = "16-bit Bayer GRGR/BGBG"; break;
1422 case V4L2_PIX_FMT_SRGGB16: descr = "16-bit Bayer RGRG/GBGB"; break;
1423 case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1424 case V4L2_PIX_FMT_SPCA501: descr = "GSPCA SPCA501"; break;
1425 case V4L2_PIX_FMT_SPCA505: descr = "GSPCA SPCA505"; break;
1426 case V4L2_PIX_FMT_SPCA508: descr = "GSPCA SPCA508"; break;
1427 case V4L2_PIX_FMT_STV0680: descr = "GSPCA STV0680"; break;
1428 case V4L2_PIX_FMT_TM6000: descr = "A/V + VBI Mux Packet"; break;
1429 case V4L2_PIX_FMT_CIT_YYVYUY: descr = "GSPCA CIT YYVYUY"; break;
1430 case V4L2_PIX_FMT_KONICA420: descr = "GSPCA KONICA420"; break;
1431 case V4L2_PIX_FMT_MM21: descr = "Mediatek 8-bit Block Format"; break;
1432 case V4L2_PIX_FMT_HSV24: descr = "24-bit HSV 8-8-8"; break;
1433 case V4L2_PIX_FMT_HSV32: descr = "32-bit XHSV 8-8-8-8"; break;
1434 case V4L2_SDR_FMT_CU8: descr = "Complex U8"; break;
1435 case V4L2_SDR_FMT_CU16LE: descr = "Complex U16LE"; break;
1436 case V4L2_SDR_FMT_CS8: descr = "Complex S8"; break;
1437 case V4L2_SDR_FMT_CS14LE: descr = "Complex S14LE"; break;
1438 case V4L2_SDR_FMT_RU12LE: descr = "Real U12LE"; break;
1439 case V4L2_SDR_FMT_PCU16BE: descr = "Planar Complex U16BE"; break;
1440 case V4L2_SDR_FMT_PCU18BE: descr = "Planar Complex U18BE"; break;
1441 case V4L2_SDR_FMT_PCU20BE: descr = "Planar Complex U20BE"; break;
1442 case V4L2_TCH_FMT_DELTA_TD16: descr = "16-bit Signed Deltas"; break;
1443 case V4L2_TCH_FMT_DELTA_TD08: descr = "8-bit Signed Deltas"; break;
1444 case V4L2_TCH_FMT_TU16: descr = "16-bit Unsigned Touch Data"; break;
1445 case V4L2_TCH_FMT_TU08: descr = "8-bit Unsigned Touch Data"; break;
1446 case V4L2_META_FMT_VSP1_HGO: descr = "R-Car VSP1 1-D Histogram"; break;
1447 case V4L2_META_FMT_VSP1_HGT: descr = "R-Car VSP1 2-D Histogram"; break;
1448 case V4L2_META_FMT_UVC: descr = "UVC Payload Header Metadata"; break;
1449 case V4L2_META_FMT_D4XX: descr = "Intel D4xx UVC Metadata"; break;
1450 case V4L2_META_FMT_VIVID: descr = "Vivid Metadata"; break;
1451 case V4L2_META_FMT_RK_ISP1_PARAMS: descr = "Rockchip ISP1 3A Parameters"; break;
1452 case V4L2_META_FMT_RK_ISP1_STAT_3A: descr = "Rockchip ISP1 3A Statistics"; break;
1453 case V4L2_PIX_FMT_NV12_8L128: descr = "NV12 (8x128 Linear)"; break;
1454 case V4L2_PIX_FMT_NV12M_8L128: descr = "NV12M (8x128 Linear)"; break;
1455 case V4L2_PIX_FMT_NV12_10BE_8L128: descr = "10-bit NV12 (8x128 Linear, BE)"; break;
1456 case V4L2_PIX_FMT_NV12M_10BE_8L128: descr = "10-bit NV12M (8x128 Linear, BE)"; break;
1457 case V4L2_PIX_FMT_Y210: descr = "10-bit YUYV Packed"; break;
1458 case V4L2_PIX_FMT_Y212: descr = "12-bit YUYV Packed"; break;
1459 case V4L2_PIX_FMT_Y216: descr = "16-bit YUYV Packed"; break;
1460
1461 default:
1462 /* Compressed formats */
1463 flags = V4L2_FMT_FLAG_COMPRESSED;
1464 switch (fmt->pixelformat) {
1465 /* Max description length mask: descr = "0123456789012345678901234567890" */
1466 case V4L2_PIX_FMT_MJPEG: descr = "Motion-JPEG"; break;
1467 case V4L2_PIX_FMT_JPEG: descr = "JFIF JPEG"; break;
1468 case V4L2_PIX_FMT_DV: descr = "1394"; break;
1469 case V4L2_PIX_FMT_MPEG: descr = "MPEG-1/2/4"; break;
1470 case V4L2_PIX_FMT_H264: descr = "H.264"; break;
1471 case V4L2_PIX_FMT_H264_NO_SC: descr = "H.264 (No Start Codes)"; break;
1472 case V4L2_PIX_FMT_H264_MVC: descr = "H.264 MVC"; break;
1473 case V4L2_PIX_FMT_H264_SLICE: descr = "H.264 Parsed Slice Data"; break;
1474 case V4L2_PIX_FMT_H263: descr = "H.263"; break;
1475 case V4L2_PIX_FMT_MPEG1: descr = "MPEG-1 ES"; break;
1476 case V4L2_PIX_FMT_MPEG2: descr = "MPEG-2 ES"; break;
1477 case V4L2_PIX_FMT_MPEG2_SLICE: descr = "MPEG-2 Parsed Slice Data"; break;
1478 case V4L2_PIX_FMT_MPEG4: descr = "MPEG-4 Part 2 ES"; break;
1479 case V4L2_PIX_FMT_XVID: descr = "Xvid"; break;
1480 case V4L2_PIX_FMT_VC1_ANNEX_G: descr = "VC-1 (SMPTE 412M Annex G)"; break;
1481 case V4L2_PIX_FMT_VC1_ANNEX_L: descr = "VC-1 (SMPTE 412M Annex L)"; break;
1482 case V4L2_PIX_FMT_VP8: descr = "VP8"; break;
1483 case V4L2_PIX_FMT_VP8_FRAME: descr = "VP8 Frame"; break;
1484 case V4L2_PIX_FMT_VP9: descr = "VP9"; break;
1485 case V4L2_PIX_FMT_VP9_FRAME: descr = "VP9 Frame"; break;
1486 case V4L2_PIX_FMT_HEVC: descr = "HEVC"; break; /* aka H.265 */
1487 case V4L2_PIX_FMT_HEVC_SLICE: descr = "HEVC Parsed Slice Data"; break;
1488 case V4L2_PIX_FMT_FWHT: descr = "FWHT"; break; /* used in vicodec */
1489 case V4L2_PIX_FMT_FWHT_STATELESS: descr = "FWHT Stateless"; break; /* used in vicodec */
1490 case V4L2_PIX_FMT_SPK: descr = "Sorenson Spark"; break;
1491 case V4L2_PIX_FMT_RV30: descr = "RealVideo 8"; break;
1492 case V4L2_PIX_FMT_RV40: descr = "RealVideo 9 & 10"; break;
1493 case V4L2_PIX_FMT_CPIA1: descr = "GSPCA CPiA YUV"; break;
1494 case V4L2_PIX_FMT_WNVA: descr = "WNVA"; break;
1495 case V4L2_PIX_FMT_SN9C10X: descr = "GSPCA SN9C10X"; break;
1496 case V4L2_PIX_FMT_PWC1: descr = "Raw Philips Webcam Type (Old)"; break;
1497 case V4L2_PIX_FMT_PWC2: descr = "Raw Philips Webcam Type (New)"; break;
1498 case V4L2_PIX_FMT_ET61X251: descr = "GSPCA ET61X251"; break;
1499 case V4L2_PIX_FMT_SPCA561: descr = "GSPCA SPCA561"; break;
1500 case V4L2_PIX_FMT_PAC207: descr = "GSPCA PAC207"; break;
1501 case V4L2_PIX_FMT_MR97310A: descr = "GSPCA MR97310A"; break;
1502 case V4L2_PIX_FMT_JL2005BCD: descr = "GSPCA JL2005BCD"; break;
1503 case V4L2_PIX_FMT_SN9C2028: descr = "GSPCA SN9C2028"; break;
1504 case V4L2_PIX_FMT_SQ905C: descr = "GSPCA SQ905C"; break;
1505 case V4L2_PIX_FMT_PJPG: descr = "GSPCA PJPG"; break;
1506 case V4L2_PIX_FMT_OV511: descr = "GSPCA OV511"; break;
1507 case V4L2_PIX_FMT_OV518: descr = "GSPCA OV518"; break;
1508 case V4L2_PIX_FMT_JPGL: descr = "JPEG Lite"; break;
1509 case V4L2_PIX_FMT_SE401: descr = "GSPCA SE401"; break;
1510 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1511 case V4L2_PIX_FMT_MT21C: descr = "Mediatek Compressed Format"; break;
1512 case V4L2_PIX_FMT_QC08C: descr = "QCOM Compressed 8-bit Format"; break;
1513 case V4L2_PIX_FMT_QC10C: descr = "QCOM Compressed 10-bit Format"; break;
1514 default:
1515 if (fmt->description[0])
1516 return;
1517 WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1518 flags = 0;
1519 snprintf(fmt->description, sz, "%p4cc",
1520 &fmt->pixelformat);
1521 break;
1522 }
1523 }
1524
1525 if (descr)
1526 WARN_ON(strscpy(fmt->description, descr, sz) < 0);
1527 fmt->flags |= flags;
1528 }
1529
v4l_enum_fmt(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1530 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1531 struct file *file, void *fh, void *arg)
1532 {
1533 struct video_device *vdev = video_devdata(file);
1534 struct v4l2_fmtdesc *p = arg;
1535 int ret = check_fmt(file, p->type);
1536 u32 mbus_code;
1537 u32 cap_mask;
1538
1539 if (ret)
1540 return ret;
1541 ret = -EINVAL;
1542
1543 if (!(vdev->device_caps & V4L2_CAP_IO_MC))
1544 p->mbus_code = 0;
1545
1546 mbus_code = p->mbus_code;
1547 CLEAR_AFTER_FIELD(p, type);
1548 p->mbus_code = mbus_code;
1549
1550 switch (p->type) {
1551 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1552 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1553 cap_mask = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1554 V4L2_CAP_VIDEO_M2M_MPLANE;
1555 if (!!(vdev->device_caps & cap_mask) !=
1556 (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
1557 break;
1558
1559 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1560 break;
1561 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1562 break;
1563 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1564 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1565 break;
1566 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1567 break;
1568 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1569 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1570 cap_mask = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1571 V4L2_CAP_VIDEO_M2M_MPLANE;
1572 if (!!(vdev->device_caps & cap_mask) !=
1573 (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
1574 break;
1575
1576 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1577 break;
1578 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1579 break;
1580 case V4L2_BUF_TYPE_SDR_CAPTURE:
1581 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1582 break;
1583 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1584 break;
1585 case V4L2_BUF_TYPE_SDR_OUTPUT:
1586 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1587 break;
1588 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1589 break;
1590 case V4L2_BUF_TYPE_META_CAPTURE:
1591 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1592 break;
1593 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1594 break;
1595 case V4L2_BUF_TYPE_META_OUTPUT:
1596 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1597 break;
1598 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1599 break;
1600 }
1601 if (ret == 0)
1602 v4l_fill_fmtdesc(p);
1603 return ret;
1604 }
1605
v4l_pix_format_touch(struct v4l2_pix_format * p)1606 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1607 {
1608 /*
1609 * The v4l2_pix_format structure contains fields that make no sense for
1610 * touch. Set them to default values in this case.
1611 */
1612
1613 p->field = V4L2_FIELD_NONE;
1614 p->colorspace = V4L2_COLORSPACE_RAW;
1615 p->flags = 0;
1616 p->ycbcr_enc = 0;
1617 p->quantization = 0;
1618 p->xfer_func = 0;
1619 }
1620
v4l_g_fmt(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1621 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1622 struct file *file, void *fh, void *arg)
1623 {
1624 struct v4l2_format *p = arg;
1625 struct video_device *vfd = video_devdata(file);
1626 int ret = check_fmt(file, p->type);
1627
1628 if (ret)
1629 return ret;
1630
1631 /*
1632 * fmt can't be cleared for these overlay types due to the 'clips'
1633 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1634 * Those are provided by the user. So handle these two overlay types
1635 * first, and then just do a simple memset for the other types.
1636 */
1637 switch (p->type) {
1638 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1639 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1640 struct v4l2_clip *clips = p->fmt.win.clips;
1641 u32 clipcount = p->fmt.win.clipcount;
1642 void __user *bitmap = p->fmt.win.bitmap;
1643
1644 memset(&p->fmt, 0, sizeof(p->fmt));
1645 p->fmt.win.clips = clips;
1646 p->fmt.win.clipcount = clipcount;
1647 p->fmt.win.bitmap = bitmap;
1648 break;
1649 }
1650 default:
1651 memset(&p->fmt, 0, sizeof(p->fmt));
1652 break;
1653 }
1654
1655 switch (p->type) {
1656 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1657 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1658 break;
1659 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1660 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1661 /* just in case the driver zeroed it again */
1662 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1663 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1664 v4l_pix_format_touch(&p->fmt.pix);
1665 return ret;
1666 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1667 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1668 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1669 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1670 case V4L2_BUF_TYPE_VBI_CAPTURE:
1671 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1672 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1673 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1674 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1675 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1676 break;
1677 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1678 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1679 /* just in case the driver zeroed it again */
1680 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1681 return ret;
1682 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1683 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1684 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1685 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1686 case V4L2_BUF_TYPE_VBI_OUTPUT:
1687 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1688 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1689 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1690 case V4L2_BUF_TYPE_SDR_CAPTURE:
1691 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1692 case V4L2_BUF_TYPE_SDR_OUTPUT:
1693 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1694 case V4L2_BUF_TYPE_META_CAPTURE:
1695 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1696 case V4L2_BUF_TYPE_META_OUTPUT:
1697 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1698 }
1699 return -EINVAL;
1700 }
1701
v4l_s_fmt(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1702 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1703 struct file *file, void *fh, void *arg)
1704 {
1705 struct v4l2_format *p = arg;
1706 struct video_device *vfd = video_devdata(file);
1707 int ret = check_fmt(file, p->type);
1708 unsigned int i;
1709
1710 if (ret)
1711 return ret;
1712
1713 ret = v4l_enable_media_source(vfd);
1714 if (ret)
1715 return ret;
1716 v4l_sanitize_format(p);
1717
1718 switch (p->type) {
1719 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1720 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1721 break;
1722 CLEAR_AFTER_FIELD(p, fmt.pix);
1723 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1724 /* just in case the driver zeroed it again */
1725 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1726 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1727 v4l_pix_format_touch(&p->fmt.pix);
1728 return ret;
1729 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1730 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1731 break;
1732 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1733 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1734 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1735 bytesperline);
1736 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1737 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1738 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1739 break;
1740 CLEAR_AFTER_FIELD(p, fmt.win);
1741 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1742 case V4L2_BUF_TYPE_VBI_CAPTURE:
1743 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1744 break;
1745 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1746 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1747 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1748 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1749 break;
1750 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1751 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1752 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1753 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1754 break;
1755 CLEAR_AFTER_FIELD(p, fmt.pix);
1756 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1757 /* just in case the driver zeroed it again */
1758 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1759 return ret;
1760 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1761 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1762 break;
1763 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1764 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1765 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1766 bytesperline);
1767 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1768 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1769 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1770 break;
1771 CLEAR_AFTER_FIELD(p, fmt.win);
1772 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1773 case V4L2_BUF_TYPE_VBI_OUTPUT:
1774 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1775 break;
1776 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1777 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1778 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1779 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1780 break;
1781 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1782 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1783 case V4L2_BUF_TYPE_SDR_CAPTURE:
1784 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1785 break;
1786 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1787 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1788 case V4L2_BUF_TYPE_SDR_OUTPUT:
1789 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1790 break;
1791 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1792 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1793 case V4L2_BUF_TYPE_META_CAPTURE:
1794 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1795 break;
1796 CLEAR_AFTER_FIELD(p, fmt.meta);
1797 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1798 case V4L2_BUF_TYPE_META_OUTPUT:
1799 if (unlikely(!ops->vidioc_s_fmt_meta_out))
1800 break;
1801 CLEAR_AFTER_FIELD(p, fmt.meta);
1802 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1803 }
1804 return -EINVAL;
1805 }
1806
v4l_try_fmt(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1807 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1808 struct file *file, void *fh, void *arg)
1809 {
1810 struct v4l2_format *p = arg;
1811 struct video_device *vfd = video_devdata(file);
1812 int ret = check_fmt(file, p->type);
1813 unsigned int i;
1814
1815 if (ret)
1816 return ret;
1817
1818 v4l_sanitize_format(p);
1819
1820 switch (p->type) {
1821 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1822 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1823 break;
1824 CLEAR_AFTER_FIELD(p, fmt.pix);
1825 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1826 /* just in case the driver zeroed it again */
1827 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1828 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1829 v4l_pix_format_touch(&p->fmt.pix);
1830 return ret;
1831 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1832 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1833 break;
1834 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1835 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1836 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1837 bytesperline);
1838 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1839 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1840 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1841 break;
1842 CLEAR_AFTER_FIELD(p, fmt.win);
1843 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1844 case V4L2_BUF_TYPE_VBI_CAPTURE:
1845 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1846 break;
1847 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1848 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1849 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1850 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1851 break;
1852 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1853 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1854 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1855 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1856 break;
1857 CLEAR_AFTER_FIELD(p, fmt.pix);
1858 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1859 /* just in case the driver zeroed it again */
1860 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1861 return ret;
1862 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1863 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1864 break;
1865 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1866 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1867 CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1868 bytesperline);
1869 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1870 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1871 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1872 break;
1873 CLEAR_AFTER_FIELD(p, fmt.win);
1874 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1875 case V4L2_BUF_TYPE_VBI_OUTPUT:
1876 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1877 break;
1878 CLEAR_AFTER_FIELD(p, fmt.vbi.flags);
1879 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1880 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1881 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1882 break;
1883 CLEAR_AFTER_FIELD(p, fmt.sliced.io_size);
1884 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1885 case V4L2_BUF_TYPE_SDR_CAPTURE:
1886 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1887 break;
1888 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1889 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1890 case V4L2_BUF_TYPE_SDR_OUTPUT:
1891 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1892 break;
1893 CLEAR_AFTER_FIELD(p, fmt.sdr.buffersize);
1894 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1895 case V4L2_BUF_TYPE_META_CAPTURE:
1896 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1897 break;
1898 CLEAR_AFTER_FIELD(p, fmt.meta);
1899 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1900 case V4L2_BUF_TYPE_META_OUTPUT:
1901 if (unlikely(!ops->vidioc_try_fmt_meta_out))
1902 break;
1903 CLEAR_AFTER_FIELD(p, fmt.meta);
1904 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1905 }
1906 return -EINVAL;
1907 }
1908
v4l_streamon(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1909 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1910 struct file *file, void *fh, void *arg)
1911 {
1912 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1913 }
1914
v4l_streamoff(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1915 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1916 struct file *file, void *fh, void *arg)
1917 {
1918 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1919 }
1920
v4l_g_tuner(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1921 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1922 struct file *file, void *fh, void *arg)
1923 {
1924 struct video_device *vfd = video_devdata(file);
1925 struct v4l2_tuner *p = arg;
1926 int err;
1927
1928 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1929 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1930 err = ops->vidioc_g_tuner(file, fh, p);
1931 if (!err)
1932 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1933 return err;
1934 }
1935
v4l_s_tuner(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1936 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1937 struct file *file, void *fh, void *arg)
1938 {
1939 struct video_device *vfd = video_devdata(file);
1940 struct v4l2_tuner *p = arg;
1941 int ret;
1942
1943 ret = v4l_enable_media_source(vfd);
1944 if (ret)
1945 return ret;
1946 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1947 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1948 return ops->vidioc_s_tuner(file, fh, p);
1949 }
1950
v4l_g_modulator(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1951 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1952 struct file *file, void *fh, void *arg)
1953 {
1954 struct video_device *vfd = video_devdata(file);
1955 struct v4l2_modulator *p = arg;
1956 int err;
1957
1958 if (vfd->vfl_type == VFL_TYPE_RADIO)
1959 p->type = V4L2_TUNER_RADIO;
1960
1961 err = ops->vidioc_g_modulator(file, fh, p);
1962 if (!err)
1963 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1964 return err;
1965 }
1966
v4l_s_modulator(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1967 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1968 struct file *file, void *fh, void *arg)
1969 {
1970 struct video_device *vfd = video_devdata(file);
1971 struct v4l2_modulator *p = arg;
1972
1973 if (vfd->vfl_type == VFL_TYPE_RADIO)
1974 p->type = V4L2_TUNER_RADIO;
1975
1976 return ops->vidioc_s_modulator(file, fh, p);
1977 }
1978
v4l_g_frequency(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1979 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1980 struct file *file, void *fh, void *arg)
1981 {
1982 struct video_device *vfd = video_devdata(file);
1983 struct v4l2_frequency *p = arg;
1984
1985 if (vfd->vfl_type == VFL_TYPE_SDR)
1986 p->type = V4L2_TUNER_SDR;
1987 else
1988 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1989 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1990 return ops->vidioc_g_frequency(file, fh, p);
1991 }
1992
v4l_s_frequency(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)1993 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1994 struct file *file, void *fh, void *arg)
1995 {
1996 struct video_device *vfd = video_devdata(file);
1997 const struct v4l2_frequency *p = arg;
1998 enum v4l2_tuner_type type;
1999 int ret;
2000
2001 ret = v4l_enable_media_source(vfd);
2002 if (ret)
2003 return ret;
2004 if (vfd->vfl_type == VFL_TYPE_SDR) {
2005 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2006 return -EINVAL;
2007 } else {
2008 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2009 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2010 if (type != p->type)
2011 return -EINVAL;
2012 }
2013 return ops->vidioc_s_frequency(file, fh, p);
2014 }
2015
v4l_enumstd(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2016 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
2017 struct file *file, void *fh, void *arg)
2018 {
2019 struct video_device *vfd = video_devdata(file);
2020 struct v4l2_standard *p = arg;
2021
2022 return v4l_video_std_enumstd(p, vfd->tvnorms);
2023 }
2024
v4l_s_std(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2025 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
2026 struct file *file, void *fh, void *arg)
2027 {
2028 struct video_device *vfd = video_devdata(file);
2029 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
2030 int ret;
2031
2032 ret = v4l_enable_media_source(vfd);
2033 if (ret)
2034 return ret;
2035 norm = id & vfd->tvnorms;
2036 if (vfd->tvnorms && !norm) /* Check if std is supported */
2037 return -EINVAL;
2038
2039 /* Calls the specific handler */
2040 return ops->vidioc_s_std(file, fh, norm);
2041 }
2042
v4l_querystd(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2043 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
2044 struct file *file, void *fh, void *arg)
2045 {
2046 struct video_device *vfd = video_devdata(file);
2047 v4l2_std_id *p = arg;
2048 int ret;
2049
2050 ret = v4l_enable_media_source(vfd);
2051 if (ret)
2052 return ret;
2053 /*
2054 * If no signal is detected, then the driver should return
2055 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
2056 * any standards that do not apply removed.
2057 *
2058 * This means that tuners, audio and video decoders can join
2059 * their efforts to improve the standards detection.
2060 */
2061 *p = vfd->tvnorms;
2062 return ops->vidioc_querystd(file, fh, arg);
2063 }
2064
v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2065 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
2066 struct file *file, void *fh, void *arg)
2067 {
2068 struct video_device *vfd = video_devdata(file);
2069 struct v4l2_hw_freq_seek *p = arg;
2070 enum v4l2_tuner_type type;
2071 int ret;
2072
2073 ret = v4l_enable_media_source(vfd);
2074 if (ret)
2075 return ret;
2076 /* s_hw_freq_seek is not supported for SDR for now */
2077 if (vfd->vfl_type == VFL_TYPE_SDR)
2078 return -EINVAL;
2079
2080 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2081 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2082 if (p->type != type)
2083 return -EINVAL;
2084 return ops->vidioc_s_hw_freq_seek(file, fh, p);
2085 }
2086
v4l_overlay(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2087 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
2088 struct file *file, void *fh, void *arg)
2089 {
2090 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
2091 }
2092
v4l_reqbufs(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2093 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
2094 struct file *file, void *fh, void *arg)
2095 {
2096 struct v4l2_requestbuffers *p = arg;
2097 int ret = check_fmt(file, p->type);
2098
2099 if (ret)
2100 return ret;
2101
2102 CLEAR_AFTER_FIELD(p, flags);
2103
2104 return ops->vidioc_reqbufs(file, fh, p);
2105 }
2106
v4l_querybuf(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2107 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
2108 struct file *file, void *fh, void *arg)
2109 {
2110 struct v4l2_buffer *p = arg;
2111 int ret = check_fmt(file, p->type);
2112
2113 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
2114 }
2115
v4l_qbuf(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2116 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
2117 struct file *file, void *fh, void *arg)
2118 {
2119 struct v4l2_buffer *p = arg;
2120 int ret = check_fmt(file, p->type);
2121
2122 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
2123 }
2124
v4l_dqbuf(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2125 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
2126 struct file *file, void *fh, void *arg)
2127 {
2128 struct v4l2_buffer *p = arg;
2129 int ret = check_fmt(file, p->type);
2130
2131 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
2132 }
2133
v4l_create_bufs(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2134 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
2135 struct file *file, void *fh, void *arg)
2136 {
2137 struct v4l2_create_buffers *create = arg;
2138 int ret = check_fmt(file, create->format.type);
2139
2140 if (ret)
2141 return ret;
2142
2143 CLEAR_AFTER_FIELD(create, flags);
2144
2145 v4l_sanitize_format(&create->format);
2146
2147 ret = ops->vidioc_create_bufs(file, fh, create);
2148
2149 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2150 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
2151 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
2152
2153 return ret;
2154 }
2155
v4l_prepare_buf(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2156 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
2157 struct file *file, void *fh, void *arg)
2158 {
2159 struct v4l2_buffer *b = arg;
2160 int ret = check_fmt(file, b->type);
2161
2162 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
2163 }
2164
v4l_g_parm(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2165 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
2166 struct file *file, void *fh, void *arg)
2167 {
2168 struct video_device *vfd = video_devdata(file);
2169 struct v4l2_streamparm *p = arg;
2170 v4l2_std_id std;
2171 int ret = check_fmt(file, p->type);
2172
2173 if (ret)
2174 return ret;
2175 if (ops->vidioc_g_parm)
2176 return ops->vidioc_g_parm(file, fh, p);
2177 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2178 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2179 return -EINVAL;
2180 if (vfd->device_caps & V4L2_CAP_READWRITE)
2181 p->parm.capture.readbuffers = 2;
2182 ret = ops->vidioc_g_std(file, fh, &std);
2183 if (ret == 0)
2184 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2185 return ret;
2186 }
2187
v4l_s_parm(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2188 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2189 struct file *file, void *fh, void *arg)
2190 {
2191 struct v4l2_streamparm *p = arg;
2192 int ret = check_fmt(file, p->type);
2193
2194 if (ret)
2195 return ret;
2196
2197 /* Note: extendedmode is never used in drivers */
2198 if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2199 memset(p->parm.output.reserved, 0,
2200 sizeof(p->parm.output.reserved));
2201 p->parm.output.extendedmode = 0;
2202 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2203 } else {
2204 memset(p->parm.capture.reserved, 0,
2205 sizeof(p->parm.capture.reserved));
2206 p->parm.capture.extendedmode = 0;
2207 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2208 }
2209 return ops->vidioc_s_parm(file, fh, p);
2210 }
2211
v4l_queryctrl(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2212 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2213 struct file *file, void *fh, void *arg)
2214 {
2215 struct video_device *vfd = video_devdata(file);
2216 struct v4l2_queryctrl *p = arg;
2217 struct v4l2_fh *vfh =
2218 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2219
2220 if (vfh && vfh->ctrl_handler)
2221 return v4l2_queryctrl(vfh->ctrl_handler, p);
2222 if (vfd->ctrl_handler)
2223 return v4l2_queryctrl(vfd->ctrl_handler, p);
2224 if (ops->vidioc_queryctrl)
2225 return ops->vidioc_queryctrl(file, fh, p);
2226 return -ENOTTY;
2227 }
2228
v4l_query_ext_ctrl(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2229 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2230 struct file *file, void *fh, void *arg)
2231 {
2232 struct video_device *vfd = video_devdata(file);
2233 struct v4l2_query_ext_ctrl *p = arg;
2234 struct v4l2_fh *vfh =
2235 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2236
2237 if (vfh && vfh->ctrl_handler)
2238 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2239 if (vfd->ctrl_handler)
2240 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2241 if (ops->vidioc_query_ext_ctrl)
2242 return ops->vidioc_query_ext_ctrl(file, fh, p);
2243 return -ENOTTY;
2244 }
2245
v4l_querymenu(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2246 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2247 struct file *file, void *fh, void *arg)
2248 {
2249 struct video_device *vfd = video_devdata(file);
2250 struct v4l2_querymenu *p = arg;
2251 struct v4l2_fh *vfh =
2252 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2253
2254 if (vfh && vfh->ctrl_handler)
2255 return v4l2_querymenu(vfh->ctrl_handler, p);
2256 if (vfd->ctrl_handler)
2257 return v4l2_querymenu(vfd->ctrl_handler, p);
2258 if (ops->vidioc_querymenu)
2259 return ops->vidioc_querymenu(file, fh, p);
2260 return -ENOTTY;
2261 }
2262
v4l_g_ctrl(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2263 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2264 struct file *file, void *fh, void *arg)
2265 {
2266 struct video_device *vfd = video_devdata(file);
2267 struct v4l2_control *p = arg;
2268 struct v4l2_fh *vfh =
2269 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2270 struct v4l2_ext_controls ctrls;
2271 struct v4l2_ext_control ctrl;
2272
2273 if (vfh && vfh->ctrl_handler)
2274 return v4l2_g_ctrl(vfh->ctrl_handler, p);
2275 if (vfd->ctrl_handler)
2276 return v4l2_g_ctrl(vfd->ctrl_handler, p);
2277 if (ops->vidioc_g_ctrl)
2278 return ops->vidioc_g_ctrl(file, fh, p);
2279 if (ops->vidioc_g_ext_ctrls == NULL)
2280 return -ENOTTY;
2281
2282 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2283 ctrls.count = 1;
2284 ctrls.controls = &ctrl;
2285 ctrl.id = p->id;
2286 ctrl.value = p->value;
2287 if (check_ext_ctrls(&ctrls, VIDIOC_G_CTRL)) {
2288 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2289
2290 if (ret == 0)
2291 p->value = ctrl.value;
2292 return ret;
2293 }
2294 return -EINVAL;
2295 }
2296
v4l_s_ctrl(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2297 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2298 struct file *file, void *fh, void *arg)
2299 {
2300 struct video_device *vfd = video_devdata(file);
2301 struct v4l2_control *p = arg;
2302 struct v4l2_fh *vfh =
2303 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2304 struct v4l2_ext_controls ctrls;
2305 struct v4l2_ext_control ctrl;
2306 int ret;
2307
2308 if (vfh && vfh->ctrl_handler)
2309 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2310 if (vfd->ctrl_handler)
2311 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2312 if (ops->vidioc_s_ctrl)
2313 return ops->vidioc_s_ctrl(file, fh, p);
2314 if (ops->vidioc_s_ext_ctrls == NULL)
2315 return -ENOTTY;
2316
2317 ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2318 ctrls.count = 1;
2319 ctrls.controls = &ctrl;
2320 ctrl.id = p->id;
2321 ctrl.value = p->value;
2322 if (!check_ext_ctrls(&ctrls, VIDIOC_S_CTRL))
2323 return -EINVAL;
2324 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2325 p->value = ctrl.value;
2326 return ret;
2327 }
2328
v4l_g_ext_ctrls(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2329 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2330 struct file *file, void *fh, void *arg)
2331 {
2332 struct video_device *vfd = video_devdata(file);
2333 struct v4l2_ext_controls *p = arg;
2334 struct v4l2_fh *vfh =
2335 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2336
2337 p->error_idx = p->count;
2338 if (vfh && vfh->ctrl_handler)
2339 return v4l2_g_ext_ctrls(vfh->ctrl_handler,
2340 vfd, vfd->v4l2_dev->mdev, p);
2341 if (vfd->ctrl_handler)
2342 return v4l2_g_ext_ctrls(vfd->ctrl_handler,
2343 vfd, vfd->v4l2_dev->mdev, p);
2344 if (ops->vidioc_g_ext_ctrls == NULL)
2345 return -ENOTTY;
2346 return check_ext_ctrls(p, VIDIOC_G_EXT_CTRLS) ?
2347 ops->vidioc_g_ext_ctrls(file, fh, p) : -EINVAL;
2348 }
2349
v4l_s_ext_ctrls(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2350 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2351 struct file *file, void *fh, void *arg)
2352 {
2353 struct video_device *vfd = video_devdata(file);
2354 struct v4l2_ext_controls *p = arg;
2355 struct v4l2_fh *vfh =
2356 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2357
2358 p->error_idx = p->count;
2359 if (vfh && vfh->ctrl_handler)
2360 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
2361 vfd, vfd->v4l2_dev->mdev, p);
2362 if (vfd->ctrl_handler)
2363 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler,
2364 vfd, vfd->v4l2_dev->mdev, p);
2365 if (ops->vidioc_s_ext_ctrls == NULL)
2366 return -ENOTTY;
2367 return check_ext_ctrls(p, VIDIOC_S_EXT_CTRLS) ?
2368 ops->vidioc_s_ext_ctrls(file, fh, p) : -EINVAL;
2369 }
2370
v4l_try_ext_ctrls(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2371 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2372 struct file *file, void *fh, void *arg)
2373 {
2374 struct video_device *vfd = video_devdata(file);
2375 struct v4l2_ext_controls *p = arg;
2376 struct v4l2_fh *vfh =
2377 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2378
2379 p->error_idx = p->count;
2380 if (vfh && vfh->ctrl_handler)
2381 return v4l2_try_ext_ctrls(vfh->ctrl_handler,
2382 vfd, vfd->v4l2_dev->mdev, p);
2383 if (vfd->ctrl_handler)
2384 return v4l2_try_ext_ctrls(vfd->ctrl_handler,
2385 vfd, vfd->v4l2_dev->mdev, p);
2386 if (ops->vidioc_try_ext_ctrls == NULL)
2387 return -ENOTTY;
2388 return check_ext_ctrls(p, VIDIOC_TRY_EXT_CTRLS) ?
2389 ops->vidioc_try_ext_ctrls(file, fh, p) : -EINVAL;
2390 }
2391
2392 /*
2393 * The selection API specified originally that the _MPLANE buffer types
2394 * shouldn't be used. The reasons for this are lost in the mists of time
2395 * (or just really crappy memories). Regardless, this is really annoying
2396 * for userspace. So to keep things simple we map _MPLANE buffer types
2397 * to their 'regular' counterparts before calling the driver. And we
2398 * restore it afterwards. This way applications can use either buffer
2399 * type and drivers don't need to check for both.
2400 */
v4l_g_selection(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2401 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2402 struct file *file, void *fh, void *arg)
2403 {
2404 struct v4l2_selection *p = arg;
2405 u32 old_type = p->type;
2406 int ret;
2407
2408 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2409 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2410 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2411 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2412 ret = ops->vidioc_g_selection(file, fh, p);
2413 p->type = old_type;
2414 return ret;
2415 }
2416
v4l_s_selection(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2417 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2418 struct file *file, void *fh, void *arg)
2419 {
2420 struct v4l2_selection *p = arg;
2421 u32 old_type = p->type;
2422 int ret;
2423
2424 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2425 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2426 else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2427 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2428 ret = ops->vidioc_s_selection(file, fh, p);
2429 p->type = old_type;
2430 return ret;
2431 }
2432
v4l_g_crop(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2433 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2434 struct file *file, void *fh, void *arg)
2435 {
2436 struct video_device *vfd = video_devdata(file);
2437 struct v4l2_crop *p = arg;
2438 struct v4l2_selection s = {
2439 .type = p->type,
2440 };
2441 int ret;
2442
2443 /* simulate capture crop using selection api */
2444
2445 /* crop means compose for output devices */
2446 if (V4L2_TYPE_IS_OUTPUT(p->type))
2447 s.target = V4L2_SEL_TGT_COMPOSE;
2448 else
2449 s.target = V4L2_SEL_TGT_CROP;
2450
2451 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2452 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2453 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2454
2455 ret = v4l_g_selection(ops, file, fh, &s);
2456
2457 /* copying results to old structure on success */
2458 if (!ret)
2459 p->c = s.r;
2460 return ret;
2461 }
2462
v4l_s_crop(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2463 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2464 struct file *file, void *fh, void *arg)
2465 {
2466 struct video_device *vfd = video_devdata(file);
2467 struct v4l2_crop *p = arg;
2468 struct v4l2_selection s = {
2469 .type = p->type,
2470 .r = p->c,
2471 };
2472
2473 /* simulate capture crop using selection api */
2474
2475 /* crop means compose for output devices */
2476 if (V4L2_TYPE_IS_OUTPUT(p->type))
2477 s.target = V4L2_SEL_TGT_COMPOSE;
2478 else
2479 s.target = V4L2_SEL_TGT_CROP;
2480
2481 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2482 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2483 V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2484
2485 return v4l_s_selection(ops, file, fh, &s);
2486 }
2487
v4l_cropcap(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2488 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2489 struct file *file, void *fh, void *arg)
2490 {
2491 struct video_device *vfd = video_devdata(file);
2492 struct v4l2_cropcap *p = arg;
2493 struct v4l2_selection s = { .type = p->type };
2494 int ret = 0;
2495
2496 /* setting trivial pixelaspect */
2497 p->pixelaspect.numerator = 1;
2498 p->pixelaspect.denominator = 1;
2499
2500 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2501 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2502 else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2503 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2504
2505 /*
2506 * The determine_valid_ioctls() call already should ensure
2507 * that this can never happen, but just in case...
2508 */
2509 if (WARN_ON(!ops->vidioc_g_selection))
2510 return -ENOTTY;
2511
2512 if (ops->vidioc_g_pixelaspect)
2513 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2514 &p->pixelaspect);
2515
2516 /*
2517 * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2518 * square pixel aspect ratio in that case.
2519 */
2520 if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2521 return ret;
2522
2523 /* Use g_selection() to fill in the bounds and defrect rectangles */
2524
2525 /* obtaining bounds */
2526 if (V4L2_TYPE_IS_OUTPUT(p->type))
2527 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2528 else
2529 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2530
2531 if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2532 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2533 V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2534
2535 ret = v4l_g_selection(ops, file, fh, &s);
2536 if (ret)
2537 return ret;
2538 p->bounds = s.r;
2539
2540 /* obtaining defrect */
2541 if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2542 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2543 else
2544 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2545
2546 ret = v4l_g_selection(ops, file, fh, &s);
2547 if (ret)
2548 return ret;
2549 p->defrect = s.r;
2550
2551 return 0;
2552 }
2553
v4l_log_status(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2554 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2555 struct file *file, void *fh, void *arg)
2556 {
2557 struct video_device *vfd = video_devdata(file);
2558 int ret;
2559
2560 if (vfd->v4l2_dev)
2561 pr_info("%s: ================= START STATUS =================\n",
2562 vfd->v4l2_dev->name);
2563 ret = ops->vidioc_log_status(file, fh);
2564 if (vfd->v4l2_dev)
2565 pr_info("%s: ================== END STATUS ==================\n",
2566 vfd->v4l2_dev->name);
2567 return ret;
2568 }
2569
v4l_dbg_g_register(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2570 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2571 struct file *file, void *fh, void *arg)
2572 {
2573 #ifdef CONFIG_VIDEO_ADV_DEBUG
2574 struct v4l2_dbg_register *p = arg;
2575 struct video_device *vfd = video_devdata(file);
2576 struct v4l2_subdev *sd;
2577 int idx = 0;
2578
2579 if (!capable(CAP_SYS_ADMIN))
2580 return -EPERM;
2581 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2582 if (vfd->v4l2_dev == NULL)
2583 return -EINVAL;
2584 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2585 if (p->match.addr == idx++)
2586 return v4l2_subdev_call(sd, core, g_register, p);
2587 return -EINVAL;
2588 }
2589 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2590 (ops->vidioc_g_chip_info || p->match.addr == 0))
2591 return ops->vidioc_g_register(file, fh, p);
2592 return -EINVAL;
2593 #else
2594 return -ENOTTY;
2595 #endif
2596 }
2597
v4l_dbg_s_register(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2598 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2599 struct file *file, void *fh, void *arg)
2600 {
2601 #ifdef CONFIG_VIDEO_ADV_DEBUG
2602 const struct v4l2_dbg_register *p = arg;
2603 struct video_device *vfd = video_devdata(file);
2604 struct v4l2_subdev *sd;
2605 int idx = 0;
2606
2607 if (!capable(CAP_SYS_ADMIN))
2608 return -EPERM;
2609 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2610 if (vfd->v4l2_dev == NULL)
2611 return -EINVAL;
2612 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2613 if (p->match.addr == idx++)
2614 return v4l2_subdev_call(sd, core, s_register, p);
2615 return -EINVAL;
2616 }
2617 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2618 (ops->vidioc_g_chip_info || p->match.addr == 0))
2619 return ops->vidioc_s_register(file, fh, p);
2620 return -EINVAL;
2621 #else
2622 return -ENOTTY;
2623 #endif
2624 }
2625
v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2626 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2627 struct file *file, void *fh, void *arg)
2628 {
2629 #ifdef CONFIG_VIDEO_ADV_DEBUG
2630 struct video_device *vfd = video_devdata(file);
2631 struct v4l2_dbg_chip_info *p = arg;
2632 struct v4l2_subdev *sd;
2633 int idx = 0;
2634
2635 switch (p->match.type) {
2636 case V4L2_CHIP_MATCH_BRIDGE:
2637 if (ops->vidioc_s_register)
2638 p->flags |= V4L2_CHIP_FL_WRITABLE;
2639 if (ops->vidioc_g_register)
2640 p->flags |= V4L2_CHIP_FL_READABLE;
2641 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2642 if (ops->vidioc_g_chip_info)
2643 return ops->vidioc_g_chip_info(file, fh, arg);
2644 if (p->match.addr)
2645 return -EINVAL;
2646 return 0;
2647
2648 case V4L2_CHIP_MATCH_SUBDEV:
2649 if (vfd->v4l2_dev == NULL)
2650 break;
2651 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2652 if (p->match.addr != idx++)
2653 continue;
2654 if (sd->ops->core && sd->ops->core->s_register)
2655 p->flags |= V4L2_CHIP_FL_WRITABLE;
2656 if (sd->ops->core && sd->ops->core->g_register)
2657 p->flags |= V4L2_CHIP_FL_READABLE;
2658 strscpy(p->name, sd->name, sizeof(p->name));
2659 return 0;
2660 }
2661 break;
2662 }
2663 return -EINVAL;
2664 #else
2665 return -ENOTTY;
2666 #endif
2667 }
2668
v4l_dqevent(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2669 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2670 struct file *file, void *fh, void *arg)
2671 {
2672 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2673 }
2674
v4l_subscribe_event(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2675 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2676 struct file *file, void *fh, void *arg)
2677 {
2678 return ops->vidioc_subscribe_event(fh, arg);
2679 }
2680
v4l_unsubscribe_event(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2681 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2682 struct file *file, void *fh, void *arg)
2683 {
2684 return ops->vidioc_unsubscribe_event(fh, arg);
2685 }
2686
v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2687 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2688 struct file *file, void *fh, void *arg)
2689 {
2690 struct v4l2_sliced_vbi_cap *p = arg;
2691 int ret = check_fmt(file, p->type);
2692
2693 if (ret)
2694 return ret;
2695
2696 /* Clear up to type, everything after type is zeroed already */
2697 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2698
2699 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2700 }
2701
v4l_enum_freq_bands(const struct v4l2_ioctl_ops * ops,struct file * file,void * fh,void * arg)2702 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2703 struct file *file, void *fh, void *arg)
2704 {
2705 struct video_device *vfd = video_devdata(file);
2706 struct v4l2_frequency_band *p = arg;
2707 enum v4l2_tuner_type type;
2708 int err;
2709
2710 if (vfd->vfl_type == VFL_TYPE_SDR) {
2711 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2712 return -EINVAL;
2713 type = p->type;
2714 } else {
2715 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2716 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2717 if (type != p->type)
2718 return -EINVAL;
2719 }
2720 if (ops->vidioc_enum_freq_bands) {
2721 err = ops->vidioc_enum_freq_bands(file, fh, p);
2722 if (err != -ENOTTY)
2723 return err;
2724 }
2725 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2726 struct v4l2_tuner t = {
2727 .index = p->tuner,
2728 .type = type,
2729 };
2730
2731 if (p->index)
2732 return -EINVAL;
2733 err = ops->vidioc_g_tuner(file, fh, &t);
2734 if (err)
2735 return err;
2736 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2737 p->rangelow = t.rangelow;
2738 p->rangehigh = t.rangehigh;
2739 p->modulation = (type == V4L2_TUNER_RADIO) ?
2740 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2741 return 0;
2742 }
2743 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2744 struct v4l2_modulator m = {
2745 .index = p->tuner,
2746 };
2747
2748 if (type != V4L2_TUNER_RADIO)
2749 return -EINVAL;
2750 if (p->index)
2751 return -EINVAL;
2752 err = ops->vidioc_g_modulator(file, fh, &m);
2753 if (err)
2754 return err;
2755 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2756 p->rangelow = m.rangelow;
2757 p->rangehigh = m.rangehigh;
2758 p->modulation = (type == V4L2_TUNER_RADIO) ?
2759 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2760 return 0;
2761 }
2762 return -ENOTTY;
2763 }
2764
2765 struct v4l2_ioctl_info {
2766 unsigned int ioctl;
2767 u32 flags;
2768 const char * const name;
2769 int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2770 void *fh, void *p);
2771 void (*debug)(const void *arg, bool write_only);
2772 };
2773
2774 /* This control needs a priority check */
2775 #define INFO_FL_PRIO (1 << 0)
2776 /* This control can be valid if the filehandle passes a control handler. */
2777 #define INFO_FL_CTRL (1 << 1)
2778 /* Queuing ioctl */
2779 #define INFO_FL_QUEUE (1 << 2)
2780 /* Always copy back result, even on error */
2781 #define INFO_FL_ALWAYS_COPY (1 << 3)
2782 /* Zero struct from after the field to the end */
2783 #define INFO_FL_CLEAR(v4l2_struct, field) \
2784 ((offsetof(struct v4l2_struct, field) + \
2785 sizeof_field(struct v4l2_struct, field)) << 16)
2786 #define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2787
2788 #define DEFINE_V4L_STUB_FUNC(_vidioc) \
2789 static int v4l_stub_ ## _vidioc( \
2790 const struct v4l2_ioctl_ops *ops, \
2791 struct file *file, void *fh, void *p) \
2792 { \
2793 return ops->vidioc_ ## _vidioc(file, fh, p); \
2794 }
2795
2796 #define IOCTL_INFO(_ioctl, _func, _debug, _flags) \
2797 [_IOC_NR(_ioctl)] = { \
2798 .ioctl = _ioctl, \
2799 .flags = _flags, \
2800 .name = #_ioctl, \
2801 .func = _func, \
2802 .debug = _debug, \
2803 }
2804
2805 DEFINE_V4L_STUB_FUNC(g_fbuf)
2806 DEFINE_V4L_STUB_FUNC(s_fbuf)
2807 DEFINE_V4L_STUB_FUNC(expbuf)
2808 DEFINE_V4L_STUB_FUNC(g_std)
2809 DEFINE_V4L_STUB_FUNC(g_audio)
2810 DEFINE_V4L_STUB_FUNC(s_audio)
2811 DEFINE_V4L_STUB_FUNC(g_edid)
2812 DEFINE_V4L_STUB_FUNC(s_edid)
2813 DEFINE_V4L_STUB_FUNC(g_audout)
2814 DEFINE_V4L_STUB_FUNC(s_audout)
2815 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2816 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2817 DEFINE_V4L_STUB_FUNC(enumaudio)
2818 DEFINE_V4L_STUB_FUNC(enumaudout)
2819 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2820 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2821 DEFINE_V4L_STUB_FUNC(g_enc_index)
2822 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2823 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2824 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2825 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2826 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2827 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2828 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2829 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2830 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2831
2832 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2833 IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2834 IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, 0),
2835 IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2836 IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2837 IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2838 IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2839 IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2840 IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2841 IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2842 IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2843 IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2844 IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2845 IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2846 IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2847 IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2848 IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2849 IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2850 IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2851 IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2852 IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2853 IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2854 IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2855 IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2856 IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2857 IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2858 IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2859 IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2860 IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2861 IOCTL_INFO(VIDIOC_G_INPUT, v4l_g_input, v4l_print_u32, 0),
2862 IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2863 IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2864 IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2865 IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_g_output, v4l_print_u32, 0),
2866 IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2867 IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2868 IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2869 IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2870 IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2871 IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2872 IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2873 IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2874 IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2875 IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2876 IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2877 IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2878 IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2879 IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2880 IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2881 IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2882 IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2883 IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2884 IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2885 IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2886 IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2887 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)),
2888 IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2889 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2890 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2891 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL | INFO_FL_ALWAYS_COPY),
2892 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2893 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2894 IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2895 IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2896 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2897 IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2898 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2899 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2900 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2901 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2902 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)),
2903 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2904 IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2905 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2906 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2907 IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2908 IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2909 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2910 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2911 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2912 IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2913 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)),
2914 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)),
2915 };
2916 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2917
v4l2_is_known_ioctl(unsigned int cmd)2918 static bool v4l2_is_known_ioctl(unsigned int cmd)
2919 {
2920 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2921 return false;
2922 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2923 }
2924
v4l2_ioctl_get_lock(struct video_device * vdev,struct v4l2_fh * vfh,unsigned int cmd,void * arg)2925 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2926 struct v4l2_fh *vfh, unsigned int cmd,
2927 void *arg)
2928 {
2929 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2930 return vdev->lock;
2931 if (vfh && vfh->m2m_ctx &&
2932 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2933 if (vfh->m2m_ctx->q_lock)
2934 return vfh->m2m_ctx->q_lock;
2935 }
2936 if (vdev->queue && vdev->queue->lock &&
2937 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2938 return vdev->queue->lock;
2939 return vdev->lock;
2940 }
2941
2942 /* Common ioctl debug function. This function can be used by
2943 external ioctl messages as well as internal V4L ioctl */
v4l_printk_ioctl(const char * prefix,unsigned int cmd)2944 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2945 {
2946 const char *dir, *type;
2947
2948 if (prefix)
2949 printk(KERN_DEBUG "%s: ", prefix);
2950
2951 switch (_IOC_TYPE(cmd)) {
2952 case 'd':
2953 type = "v4l2_int";
2954 break;
2955 case 'V':
2956 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2957 type = "v4l2";
2958 break;
2959 }
2960 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2961 return;
2962 default:
2963 type = "unknown";
2964 break;
2965 }
2966
2967 switch (_IOC_DIR(cmd)) {
2968 case _IOC_NONE: dir = "--"; break;
2969 case _IOC_READ: dir = "r-"; break;
2970 case _IOC_WRITE: dir = "-w"; break;
2971 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2972 default: dir = "*ERR*"; break;
2973 }
2974 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2975 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2976 }
2977 EXPORT_SYMBOL(v4l_printk_ioctl);
2978
__video_do_ioctl(struct file * file,unsigned int cmd,void * arg)2979 static long __video_do_ioctl(struct file *file,
2980 unsigned int cmd, void *arg)
2981 {
2982 struct video_device *vfd = video_devdata(file);
2983 struct mutex *req_queue_lock = NULL;
2984 struct mutex *lock; /* ioctl serialization mutex */
2985 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2986 bool write_only = false;
2987 struct v4l2_ioctl_info default_info;
2988 const struct v4l2_ioctl_info *info;
2989 void *fh = file->private_data;
2990 struct v4l2_fh *vfh = NULL;
2991 int dev_debug = vfd->dev_debug;
2992 long ret = -ENOTTY;
2993
2994 if (ops == NULL) {
2995 pr_warn("%s: has no ioctl_ops.\n",
2996 video_device_node_name(vfd));
2997 return ret;
2998 }
2999
3000 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
3001 vfh = file->private_data;
3002
3003 /*
3004 * We need to serialize streamon/off with queueing new requests.
3005 * These ioctls may trigger the cancellation of a streaming
3006 * operation, and that should not be mixed with queueing a new
3007 * request at the same time.
3008 */
3009 if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
3010 (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
3011 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
3012
3013 if (mutex_lock_interruptible(req_queue_lock))
3014 return -ERESTARTSYS;
3015 }
3016
3017 lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
3018
3019 if (lock && mutex_lock_interruptible(lock)) {
3020 if (req_queue_lock)
3021 mutex_unlock(req_queue_lock);
3022 return -ERESTARTSYS;
3023 }
3024
3025 if (!video_is_registered(vfd)) {
3026 ret = -ENODEV;
3027 goto unlock;
3028 }
3029
3030 if (v4l2_is_known_ioctl(cmd)) {
3031 info = &v4l2_ioctls[_IOC_NR(cmd)];
3032
3033 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
3034 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
3035 goto done;
3036
3037 if (vfh && (info->flags & INFO_FL_PRIO)) {
3038 ret = v4l2_prio_check(vfd->prio, vfh->prio);
3039 if (ret)
3040 goto done;
3041 }
3042 } else {
3043 default_info.ioctl = cmd;
3044 default_info.flags = 0;
3045 default_info.debug = v4l_print_default;
3046 info = &default_info;
3047 }
3048
3049 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
3050 if (info != &default_info) {
3051 ret = info->func(ops, file, fh, arg);
3052 } else if (!ops->vidioc_default) {
3053 ret = -ENOTTY;
3054 } else {
3055 ret = ops->vidioc_default(file, fh,
3056 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
3057 cmd, arg);
3058 }
3059
3060 done:
3061 if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
3062 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
3063 (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
3064 goto unlock;
3065
3066 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
3067 if (ret < 0)
3068 pr_cont(": error %ld", ret);
3069 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
3070 pr_cont("\n");
3071 else if (_IOC_DIR(cmd) == _IOC_NONE)
3072 info->debug(arg, write_only);
3073 else {
3074 pr_cont(": ");
3075 info->debug(arg, write_only);
3076 }
3077 }
3078
3079 unlock:
3080 if (lock)
3081 mutex_unlock(lock);
3082 if (req_queue_lock)
3083 mutex_unlock(req_queue_lock);
3084 return ret;
3085 }
3086
check_array_args(unsigned int cmd,void * parg,size_t * array_size,void __user ** user_ptr,void *** kernel_ptr)3087 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
3088 void __user **user_ptr, void ***kernel_ptr)
3089 {
3090 int ret = 0;
3091
3092 switch (cmd) {
3093 case VIDIOC_PREPARE_BUF:
3094 case VIDIOC_QUERYBUF:
3095 case VIDIOC_QBUF:
3096 case VIDIOC_DQBUF: {
3097 struct v4l2_buffer *buf = parg;
3098
3099 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
3100 if (buf->length > VIDEO_MAX_PLANES) {
3101 ret = -EINVAL;
3102 break;
3103 }
3104 *user_ptr = (void __user *)buf->m.planes;
3105 *kernel_ptr = (void **)&buf->m.planes;
3106 *array_size = sizeof(struct v4l2_plane) * buf->length;
3107 ret = 1;
3108 }
3109 break;
3110 }
3111
3112 case VIDIOC_G_EDID:
3113 case VIDIOC_S_EDID: {
3114 struct v4l2_edid *edid = parg;
3115
3116 if (edid->blocks) {
3117 if (edid->blocks > 256) {
3118 ret = -EINVAL;
3119 break;
3120 }
3121 *user_ptr = (void __user *)edid->edid;
3122 *kernel_ptr = (void **)&edid->edid;
3123 *array_size = edid->blocks * 128;
3124 ret = 1;
3125 }
3126 break;
3127 }
3128
3129 case VIDIOC_S_EXT_CTRLS:
3130 case VIDIOC_G_EXT_CTRLS:
3131 case VIDIOC_TRY_EXT_CTRLS: {
3132 struct v4l2_ext_controls *ctrls = parg;
3133
3134 if (ctrls->count != 0) {
3135 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
3136 ret = -EINVAL;
3137 break;
3138 }
3139 *user_ptr = (void __user *)ctrls->controls;
3140 *kernel_ptr = (void **)&ctrls->controls;
3141 *array_size = sizeof(struct v4l2_ext_control)
3142 * ctrls->count;
3143 ret = 1;
3144 }
3145 break;
3146 }
3147 case VIDIOC_G_FMT:
3148 case VIDIOC_S_FMT:
3149 case VIDIOC_TRY_FMT: {
3150 struct v4l2_format *fmt = parg;
3151
3152 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OVERLAY &&
3153 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY)
3154 break;
3155 if (fmt->fmt.win.clipcount > 2048)
3156 return -EINVAL;
3157 if (!fmt->fmt.win.clipcount)
3158 break;
3159
3160 *user_ptr = (void __user *)fmt->fmt.win.clips;
3161 *kernel_ptr = (void **)&fmt->fmt.win.clips;
3162 *array_size = sizeof(struct v4l2_clip)
3163 * fmt->fmt.win.clipcount;
3164
3165 ret = 1;
3166 break;
3167 }
3168 }
3169
3170 return ret;
3171 }
3172
video_translate_cmd(unsigned int cmd)3173 static unsigned int video_translate_cmd(unsigned int cmd)
3174 {
3175 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3176 switch (cmd) {
3177 case VIDIOC_DQEVENT_TIME32:
3178 return VIDIOC_DQEVENT;
3179 case VIDIOC_QUERYBUF_TIME32:
3180 return VIDIOC_QUERYBUF;
3181 case VIDIOC_QBUF_TIME32:
3182 return VIDIOC_QBUF;
3183 case VIDIOC_DQBUF_TIME32:
3184 return VIDIOC_DQBUF;
3185 case VIDIOC_PREPARE_BUF_TIME32:
3186 return VIDIOC_PREPARE_BUF;
3187 }
3188 #endif
3189 if (in_compat_syscall())
3190 return v4l2_compat_translate_cmd(cmd);
3191
3192 return cmd;
3193 }
3194
video_get_user(void __user * arg,void * parg,unsigned int real_cmd,unsigned int cmd,bool * always_copy)3195 static int video_get_user(void __user *arg, void *parg,
3196 unsigned int real_cmd, unsigned int cmd,
3197 bool *always_copy)
3198 {
3199 unsigned int n = _IOC_SIZE(real_cmd);
3200 int err = 0;
3201
3202 if (!(_IOC_DIR(cmd) & _IOC_WRITE)) {
3203 /* read-only ioctl */
3204 memset(parg, 0, n);
3205 return 0;
3206 }
3207
3208 /*
3209 * In some cases, only a few fields are used as input,
3210 * i.e. when the app sets "index" and then the driver
3211 * fills in the rest of the structure for the thing
3212 * with that index. We only need to copy up the first
3213 * non-input field.
3214 */
3215 if (v4l2_is_known_ioctl(real_cmd)) {
3216 u32 flags = v4l2_ioctls[_IOC_NR(real_cmd)].flags;
3217
3218 if (flags & INFO_FL_CLEAR_MASK)
3219 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3220 *always_copy = flags & INFO_FL_ALWAYS_COPY;
3221 }
3222
3223 if (cmd == real_cmd) {
3224 if (copy_from_user(parg, (void __user *)arg, n))
3225 err = -EFAULT;
3226 } else if (in_compat_syscall()) {
3227 memset(parg, 0, n);
3228 err = v4l2_compat_get_user(arg, parg, cmd);
3229 } else {
3230 memset(parg, 0, n);
3231 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3232 switch (cmd) {
3233 case VIDIOC_QUERYBUF_TIME32:
3234 case VIDIOC_QBUF_TIME32:
3235 case VIDIOC_DQBUF_TIME32:
3236 case VIDIOC_PREPARE_BUF_TIME32: {
3237 struct v4l2_buffer_time32 vb32;
3238 struct v4l2_buffer *vb = parg;
3239
3240 if (copy_from_user(&vb32, arg, sizeof(vb32)))
3241 return -EFAULT;
3242
3243 *vb = (struct v4l2_buffer) {
3244 .index = vb32.index,
3245 .type = vb32.type,
3246 .bytesused = vb32.bytesused,
3247 .flags = vb32.flags,
3248 .field = vb32.field,
3249 .timestamp.tv_sec = vb32.timestamp.tv_sec,
3250 .timestamp.tv_usec = vb32.timestamp.tv_usec,
3251 .timecode = vb32.timecode,
3252 .sequence = vb32.sequence,
3253 .memory = vb32.memory,
3254 .m.userptr = vb32.m.userptr,
3255 .length = vb32.length,
3256 .request_fd = vb32.request_fd,
3257 };
3258 break;
3259 }
3260 }
3261 #endif
3262 }
3263
3264 /* zero out anything we don't copy from userspace */
3265 if (!err && n < _IOC_SIZE(real_cmd))
3266 memset((u8 *)parg + n, 0, _IOC_SIZE(real_cmd) - n);
3267 return err;
3268 }
3269
video_put_user(void __user * arg,void * parg,unsigned int real_cmd,unsigned int cmd)3270 static int video_put_user(void __user *arg, void *parg,
3271 unsigned int real_cmd, unsigned int cmd)
3272 {
3273 if (!(_IOC_DIR(cmd) & _IOC_READ))
3274 return 0;
3275
3276 if (cmd == real_cmd) {
3277 /* Copy results into user buffer */
3278 if (copy_to_user(arg, parg, _IOC_SIZE(cmd)))
3279 return -EFAULT;
3280 return 0;
3281 }
3282
3283 if (in_compat_syscall())
3284 return v4l2_compat_put_user(arg, parg, cmd);
3285
3286 #if !defined(CONFIG_64BIT) && defined(CONFIG_COMPAT_32BIT_TIME)
3287 switch (cmd) {
3288 case VIDIOC_DQEVENT_TIME32: {
3289 struct v4l2_event *ev = parg;
3290 struct v4l2_event_time32 ev32;
3291
3292 memset(&ev32, 0, sizeof(ev32));
3293
3294 ev32.type = ev->type;
3295 ev32.pending = ev->pending;
3296 ev32.sequence = ev->sequence;
3297 ev32.timestamp.tv_sec = ev->timestamp.tv_sec;
3298 ev32.timestamp.tv_nsec = ev->timestamp.tv_nsec;
3299 ev32.id = ev->id;
3300
3301 memcpy(&ev32.u, &ev->u, sizeof(ev->u));
3302 memcpy(&ev32.reserved, &ev->reserved, sizeof(ev->reserved));
3303
3304 if (copy_to_user(arg, &ev32, sizeof(ev32)))
3305 return -EFAULT;
3306 break;
3307 }
3308 case VIDIOC_QUERYBUF_TIME32:
3309 case VIDIOC_QBUF_TIME32:
3310 case VIDIOC_DQBUF_TIME32:
3311 case VIDIOC_PREPARE_BUF_TIME32: {
3312 struct v4l2_buffer *vb = parg;
3313 struct v4l2_buffer_time32 vb32;
3314
3315 memset(&vb32, 0, sizeof(vb32));
3316
3317 vb32.index = vb->index;
3318 vb32.type = vb->type;
3319 vb32.bytesused = vb->bytesused;
3320 vb32.flags = vb->flags;
3321 vb32.field = vb->field;
3322 vb32.timestamp.tv_sec = vb->timestamp.tv_sec;
3323 vb32.timestamp.tv_usec = vb->timestamp.tv_usec;
3324 vb32.timecode = vb->timecode;
3325 vb32.sequence = vb->sequence;
3326 vb32.memory = vb->memory;
3327 vb32.m.userptr = vb->m.userptr;
3328 vb32.length = vb->length;
3329 vb32.request_fd = vb->request_fd;
3330
3331 if (copy_to_user(arg, &vb32, sizeof(vb32)))
3332 return -EFAULT;
3333 break;
3334 }
3335 }
3336 #endif
3337
3338 return 0;
3339 }
3340
3341 long
video_usercopy(struct file * file,unsigned int orig_cmd,unsigned long arg,v4l2_kioctl func)3342 video_usercopy(struct file *file, unsigned int orig_cmd, unsigned long arg,
3343 v4l2_kioctl func)
3344 {
3345 char sbuf[128];
3346 void *mbuf = NULL, *array_buf = NULL;
3347 void *parg = (void *)arg;
3348 long err = -EINVAL;
3349 bool has_array_args;
3350 bool always_copy = false;
3351 size_t array_size = 0;
3352 void __user *user_ptr = NULL;
3353 void **kernel_ptr = NULL;
3354 unsigned int cmd = video_translate_cmd(orig_cmd);
3355 const size_t ioc_size = _IOC_SIZE(cmd);
3356
3357 /* Copy arguments into temp kernel buffer */
3358 if (_IOC_DIR(cmd) != _IOC_NONE) {
3359 if (ioc_size <= sizeof(sbuf)) {
3360 parg = sbuf;
3361 } else {
3362 /* too big to allocate from stack */
3363 mbuf = kmalloc(ioc_size, GFP_KERNEL);
3364 if (NULL == mbuf)
3365 return -ENOMEM;
3366 parg = mbuf;
3367 }
3368
3369 err = video_get_user((void __user *)arg, parg, cmd,
3370 orig_cmd, &always_copy);
3371 if (err)
3372 goto out;
3373 }
3374
3375 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3376 if (err < 0)
3377 goto out;
3378 has_array_args = err;
3379
3380 if (has_array_args) {
3381 array_buf = kvmalloc(array_size, GFP_KERNEL);
3382 err = -ENOMEM;
3383 if (array_buf == NULL)
3384 goto out;
3385 if (in_compat_syscall())
3386 err = v4l2_compat_get_array_args(file, array_buf,
3387 user_ptr, array_size,
3388 orig_cmd, parg);
3389 else
3390 err = copy_from_user(array_buf, user_ptr, array_size) ?
3391 -EFAULT : 0;
3392 if (err)
3393 goto out;
3394 *kernel_ptr = array_buf;
3395 }
3396
3397 /* Handles IOCTL */
3398 err = func(file, cmd, parg);
3399 if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3400 err = -ENOTTY;
3401 goto out;
3402 }
3403
3404 if (err == 0) {
3405 if (cmd == VIDIOC_DQBUF)
3406 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3407 else if (cmd == VIDIOC_QBUF)
3408 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3409 }
3410
3411 /*
3412 * Some ioctls can return an error, but still have valid
3413 * results that must be returned.
3414 */
3415 if (err < 0 && !always_copy)
3416 goto out;
3417
3418 if (has_array_args) {
3419 *kernel_ptr = (void __force *)user_ptr;
3420 if (in_compat_syscall()) {
3421 int put_err;
3422
3423 put_err = v4l2_compat_put_array_args(file, user_ptr,
3424 array_buf,
3425 array_size,
3426 orig_cmd, parg);
3427 if (put_err)
3428 err = put_err;
3429 } else if (copy_to_user(user_ptr, array_buf, array_size)) {
3430 err = -EFAULT;
3431 }
3432 }
3433
3434 if (video_put_user((void __user *)arg, parg, cmd, orig_cmd))
3435 err = -EFAULT;
3436 out:
3437 kvfree(array_buf);
3438 kfree(mbuf);
3439 return err;
3440 }
3441
video_ioctl2(struct file * file,unsigned int cmd,unsigned long arg)3442 long video_ioctl2(struct file *file,
3443 unsigned int cmd, unsigned long arg)
3444 {
3445 return video_usercopy(file, cmd, arg, __video_do_ioctl);
3446 }
3447 EXPORT_SYMBOL(video_ioctl2);
3448