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