blob: 9f80e9d1fdb728a82bf68153fa93e614924711c5 [file] [log] [blame]
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001/*
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 *
Alan Coxd9b01442008-10-27 15:13:47 -030011 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil35ea11f2008-07-20 08:12:02 -030012 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030017#include <linux/types.h>
18#include <linux/kernel.h>
Mauro Carvalho Chehabae6db512011-06-24 13:14:14 -030019#include <linux/version.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030020
Hans Verkuil35ea11f2008-07-20 08:12:02 -030021#include <linux/videodev2.h>
22
Hans Verkuil35ea11f2008-07-20 08:12:02 -030023#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030025#include <media/v4l2-ctrls.h>
Sakari Ailusd3d7c962010-03-27 11:02:10 -030026#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
Hans Verkuil99cd47bc2011-03-11 19:00:56 -030028#include <media/v4l2-device.h>
Hans Verkuil80b36e02009-02-07 11:00:02 -030029#include <media/v4l2-chip-ident.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030
31#define dbgarg(cmd, fmt, arg...) \
32 do { \
33 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
34 printk(KERN_DEBUG "%s: ", vfd->name); \
35 v4l_printk_ioctl(cmd); \
36 printk(" " fmt, ## arg); \
37 } \
38 } while (0)
39
40#define dbgarg2(fmt, arg...) \
41 do { \
42 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
43 printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
44 } while (0)
45
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -030046#define dbgarg3(fmt, arg...) \
47 do { \
48 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
49 printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
50 } while (0)
51
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030053 * not including, the specified field is cleared. */
54#define CLEAR_AFTER_FIELD(p, field) \
55 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
56 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
57
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -030058#define no_ioctl_err(foo) ( ( \
59 ops->vidioc_##foo##_fmt_vid_cap || \
60 ops->vidioc_##foo##_fmt_vid_out || \
61 ops->vidioc_##foo##_fmt_vid_cap_mplane || \
62 ops->vidioc_##foo##_fmt_vid_out_mplane || \
63 ops->vidioc_##foo##_fmt_vid_overlay || \
64 ops->vidioc_##foo##_fmt_type_private) ? -EINVAL : -ENOTTY)
65
Hans Verkuil35ea11f2008-07-20 08:12:02 -030066struct std_descr {
67 v4l2_std_id std;
68 const char *descr;
69};
70
71static const struct std_descr standards[] = {
72 { V4L2_STD_NTSC, "NTSC" },
73 { V4L2_STD_NTSC_M, "NTSC-M" },
74 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
75 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
76 { V4L2_STD_NTSC_443, "NTSC-443" },
77 { V4L2_STD_PAL, "PAL" },
78 { V4L2_STD_PAL_BG, "PAL-BG" },
79 { V4L2_STD_PAL_B, "PAL-B" },
80 { V4L2_STD_PAL_B1, "PAL-B1" },
81 { V4L2_STD_PAL_G, "PAL-G" },
82 { V4L2_STD_PAL_H, "PAL-H" },
83 { V4L2_STD_PAL_I, "PAL-I" },
84 { V4L2_STD_PAL_DK, "PAL-DK" },
85 { V4L2_STD_PAL_D, "PAL-D" },
86 { V4L2_STD_PAL_D1, "PAL-D1" },
87 { V4L2_STD_PAL_K, "PAL-K" },
88 { V4L2_STD_PAL_M, "PAL-M" },
89 { V4L2_STD_PAL_N, "PAL-N" },
90 { V4L2_STD_PAL_Nc, "PAL-Nc" },
91 { V4L2_STD_PAL_60, "PAL-60" },
92 { V4L2_STD_SECAM, "SECAM" },
93 { V4L2_STD_SECAM_B, "SECAM-B" },
94 { V4L2_STD_SECAM_G, "SECAM-G" },
95 { V4L2_STD_SECAM_H, "SECAM-H" },
96 { V4L2_STD_SECAM_DK, "SECAM-DK" },
97 { V4L2_STD_SECAM_D, "SECAM-D" },
98 { V4L2_STD_SECAM_K, "SECAM-K" },
99 { V4L2_STD_SECAM_K1, "SECAM-K1" },
100 { V4L2_STD_SECAM_L, "SECAM-L" },
101 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
102 { 0, "Unknown" }
103};
104
105/* video4linux standard ID conversion to standard name
106 */
107const char *v4l2_norm_to_name(v4l2_std_id id)
108{
109 u32 myid = id;
110 int i;
111
112 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
113 64 bit comparations. So, on that architecture, with some gcc
114 variants, compilation fails. Currently, the max value is 30bit wide.
115 */
116 BUG_ON(myid != id);
117
118 for (i = 0; standards[i].std; i++)
119 if (myid == standards[i].std)
120 break;
121 return standards[i].descr;
122}
123EXPORT_SYMBOL(v4l2_norm_to_name);
124
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300125/* Returns frame period for the given standard */
126void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
127{
128 if (id & V4L2_STD_525_60) {
129 frameperiod->numerator = 1001;
130 frameperiod->denominator = 30000;
131 } else {
132 frameperiod->numerator = 1;
133 frameperiod->denominator = 25;
134 }
135}
136EXPORT_SYMBOL(v4l2_video_std_frame_period);
137
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300138/* Fill in the fields of a v4l2_standard structure according to the
139 'id' and 'transmission' parameters. Returns negative on error. */
140int v4l2_video_std_construct(struct v4l2_standard *vs,
141 int id, const char *name)
142{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300143 vs->id = id;
144 v4l2_video_std_frame_period(id, &vs->frameperiod);
145 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300146 strlcpy(vs->name, name, sizeof(vs->name));
147 return 0;
148}
149EXPORT_SYMBOL(v4l2_video_std_construct);
150
151/* ----------------------------------------------------------------- */
152/* some arrays for pretty-printing debug messages of enum types */
153
154const char *v4l2_field_names[] = {
155 [V4L2_FIELD_ANY] = "any",
156 [V4L2_FIELD_NONE] = "none",
157 [V4L2_FIELD_TOP] = "top",
158 [V4L2_FIELD_BOTTOM] = "bottom",
159 [V4L2_FIELD_INTERLACED] = "interlaced",
160 [V4L2_FIELD_SEQ_TB] = "seq-tb",
161 [V4L2_FIELD_SEQ_BT] = "seq-bt",
162 [V4L2_FIELD_ALTERNATE] = "alternate",
163 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
164 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
165};
166EXPORT_SYMBOL(v4l2_field_names);
167
168const char *v4l2_type_names[] = {
169 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
170 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
171 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
172 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
173 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
174 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
175 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
176 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300177 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
178 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300179};
180EXPORT_SYMBOL(v4l2_type_names);
181
182static const char *v4l2_memory_names[] = {
183 [V4L2_MEMORY_MMAP] = "mmap",
184 [V4L2_MEMORY_USERPTR] = "userptr",
185 [V4L2_MEMORY_OVERLAY] = "overlay",
186};
187
188#define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
189 arr[a] : "unknown")
190
191/* ------------------------------------------------------------------ */
192/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300193static const char *v4l2_ioctls[] = {
194 [_IOC_NR(VIDIOC_QUERYCAP)] = "VIDIOC_QUERYCAP",
195 [_IOC_NR(VIDIOC_RESERVED)] = "VIDIOC_RESERVED",
196 [_IOC_NR(VIDIOC_ENUM_FMT)] = "VIDIOC_ENUM_FMT",
197 [_IOC_NR(VIDIOC_G_FMT)] = "VIDIOC_G_FMT",
198 [_IOC_NR(VIDIOC_S_FMT)] = "VIDIOC_S_FMT",
199 [_IOC_NR(VIDIOC_REQBUFS)] = "VIDIOC_REQBUFS",
200 [_IOC_NR(VIDIOC_QUERYBUF)] = "VIDIOC_QUERYBUF",
201 [_IOC_NR(VIDIOC_G_FBUF)] = "VIDIOC_G_FBUF",
202 [_IOC_NR(VIDIOC_S_FBUF)] = "VIDIOC_S_FBUF",
203 [_IOC_NR(VIDIOC_OVERLAY)] = "VIDIOC_OVERLAY",
204 [_IOC_NR(VIDIOC_QBUF)] = "VIDIOC_QBUF",
205 [_IOC_NR(VIDIOC_DQBUF)] = "VIDIOC_DQBUF",
206 [_IOC_NR(VIDIOC_STREAMON)] = "VIDIOC_STREAMON",
207 [_IOC_NR(VIDIOC_STREAMOFF)] = "VIDIOC_STREAMOFF",
208 [_IOC_NR(VIDIOC_G_PARM)] = "VIDIOC_G_PARM",
209 [_IOC_NR(VIDIOC_S_PARM)] = "VIDIOC_S_PARM",
210 [_IOC_NR(VIDIOC_G_STD)] = "VIDIOC_G_STD",
211 [_IOC_NR(VIDIOC_S_STD)] = "VIDIOC_S_STD",
212 [_IOC_NR(VIDIOC_ENUMSTD)] = "VIDIOC_ENUMSTD",
213 [_IOC_NR(VIDIOC_ENUMINPUT)] = "VIDIOC_ENUMINPUT",
214 [_IOC_NR(VIDIOC_G_CTRL)] = "VIDIOC_G_CTRL",
215 [_IOC_NR(VIDIOC_S_CTRL)] = "VIDIOC_S_CTRL",
216 [_IOC_NR(VIDIOC_G_TUNER)] = "VIDIOC_G_TUNER",
217 [_IOC_NR(VIDIOC_S_TUNER)] = "VIDIOC_S_TUNER",
218 [_IOC_NR(VIDIOC_G_AUDIO)] = "VIDIOC_G_AUDIO",
219 [_IOC_NR(VIDIOC_S_AUDIO)] = "VIDIOC_S_AUDIO",
220 [_IOC_NR(VIDIOC_QUERYCTRL)] = "VIDIOC_QUERYCTRL",
221 [_IOC_NR(VIDIOC_QUERYMENU)] = "VIDIOC_QUERYMENU",
222 [_IOC_NR(VIDIOC_G_INPUT)] = "VIDIOC_G_INPUT",
223 [_IOC_NR(VIDIOC_S_INPUT)] = "VIDIOC_S_INPUT",
224 [_IOC_NR(VIDIOC_G_OUTPUT)] = "VIDIOC_G_OUTPUT",
225 [_IOC_NR(VIDIOC_S_OUTPUT)] = "VIDIOC_S_OUTPUT",
226 [_IOC_NR(VIDIOC_ENUMOUTPUT)] = "VIDIOC_ENUMOUTPUT",
227 [_IOC_NR(VIDIOC_G_AUDOUT)] = "VIDIOC_G_AUDOUT",
228 [_IOC_NR(VIDIOC_S_AUDOUT)] = "VIDIOC_S_AUDOUT",
229 [_IOC_NR(VIDIOC_G_MODULATOR)] = "VIDIOC_G_MODULATOR",
230 [_IOC_NR(VIDIOC_S_MODULATOR)] = "VIDIOC_S_MODULATOR",
231 [_IOC_NR(VIDIOC_G_FREQUENCY)] = "VIDIOC_G_FREQUENCY",
232 [_IOC_NR(VIDIOC_S_FREQUENCY)] = "VIDIOC_S_FREQUENCY",
233 [_IOC_NR(VIDIOC_CROPCAP)] = "VIDIOC_CROPCAP",
234 [_IOC_NR(VIDIOC_G_CROP)] = "VIDIOC_G_CROP",
235 [_IOC_NR(VIDIOC_S_CROP)] = "VIDIOC_S_CROP",
236 [_IOC_NR(VIDIOC_G_JPEGCOMP)] = "VIDIOC_G_JPEGCOMP",
237 [_IOC_NR(VIDIOC_S_JPEGCOMP)] = "VIDIOC_S_JPEGCOMP",
238 [_IOC_NR(VIDIOC_QUERYSTD)] = "VIDIOC_QUERYSTD",
239 [_IOC_NR(VIDIOC_TRY_FMT)] = "VIDIOC_TRY_FMT",
240 [_IOC_NR(VIDIOC_ENUMAUDIO)] = "VIDIOC_ENUMAUDIO",
241 [_IOC_NR(VIDIOC_ENUMAUDOUT)] = "VIDIOC_ENUMAUDOUT",
242 [_IOC_NR(VIDIOC_G_PRIORITY)] = "VIDIOC_G_PRIORITY",
243 [_IOC_NR(VIDIOC_S_PRIORITY)] = "VIDIOC_S_PRIORITY",
244 [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
245 [_IOC_NR(VIDIOC_LOG_STATUS)] = "VIDIOC_LOG_STATUS",
246 [_IOC_NR(VIDIOC_G_EXT_CTRLS)] = "VIDIOC_G_EXT_CTRLS",
247 [_IOC_NR(VIDIOC_S_EXT_CTRLS)] = "VIDIOC_S_EXT_CTRLS",
248 [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)] = "VIDIOC_TRY_EXT_CTRLS",
249#if 1
250 [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)] = "VIDIOC_ENUM_FRAMESIZES",
251 [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
252 [_IOC_NR(VIDIOC_G_ENC_INDEX)] = "VIDIOC_G_ENC_INDEX",
253 [_IOC_NR(VIDIOC_ENCODER_CMD)] = "VIDIOC_ENCODER_CMD",
254 [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)] = "VIDIOC_TRY_ENCODER_CMD",
255
256 [_IOC_NR(VIDIOC_DBG_S_REGISTER)] = "VIDIOC_DBG_S_REGISTER",
257 [_IOC_NR(VIDIOC_DBG_G_REGISTER)] = "VIDIOC_DBG_G_REGISTER",
258
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300259 [_IOC_NR(VIDIOC_DBG_G_CHIP_IDENT)] = "VIDIOC_DBG_G_CHIP_IDENT",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300260 [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)] = "VIDIOC_S_HW_FREQ_SEEK",
261#endif
Muralidharan Karicherib6456c02009-11-19 12:00:31 -0300262 [_IOC_NR(VIDIOC_ENUM_DV_PRESETS)] = "VIDIOC_ENUM_DV_PRESETS",
263 [_IOC_NR(VIDIOC_S_DV_PRESET)] = "VIDIOC_S_DV_PRESET",
264 [_IOC_NR(VIDIOC_G_DV_PRESET)] = "VIDIOC_G_DV_PRESET",
265 [_IOC_NR(VIDIOC_QUERY_DV_PRESET)] = "VIDIOC_QUERY_DV_PRESET",
266 [_IOC_NR(VIDIOC_S_DV_TIMINGS)] = "VIDIOC_S_DV_TIMINGS",
267 [_IOC_NR(VIDIOC_G_DV_TIMINGS)] = "VIDIOC_G_DV_TIMINGS",
Sakari Ailusfda10212010-02-24 19:19:05 -0300268 [_IOC_NR(VIDIOC_DQEVENT)] = "VIDIOC_DQEVENT",
269 [_IOC_NR(VIDIOC_SUBSCRIBE_EVENT)] = "VIDIOC_SUBSCRIBE_EVENT",
270 [_IOC_NR(VIDIOC_UNSUBSCRIBE_EVENT)] = "VIDIOC_UNSUBSCRIBE_EVENT",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300271};
272#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
273
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300274/* Common ioctl debug function. This function can be used by
275 external ioctl messages as well as internal V4L ioctl */
276void v4l_printk_ioctl(unsigned int cmd)
277{
278 char *dir, *type;
279
280 switch (_IOC_TYPE(cmd)) {
281 case 'd':
Hans Verkuil78a3b4d2009-04-01 03:41:09 -0300282 type = "v4l2_int";
283 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300284 case 'V':
285 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
286 type = "v4l2";
287 break;
288 }
289 printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
290 return;
291 default:
292 type = "unknown";
293 }
294
295 switch (_IOC_DIR(cmd)) {
296 case _IOC_NONE: dir = "--"; break;
297 case _IOC_READ: dir = "r-"; break;
298 case _IOC_WRITE: dir = "-w"; break;
299 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
300 default: dir = "*ERR*"; break;
301 }
302 printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
303 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
304}
305EXPORT_SYMBOL(v4l_printk_ioctl);
306
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300307static void dbgbuf(unsigned int cmd, struct video_device *vfd,
308 struct v4l2_buffer *p)
309{
310 struct v4l2_timecode *tc = &p->timecode;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300311 struct v4l2_plane *plane;
312 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300313
314 dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300315 "flags=0x%08d, field=%0d, sequence=%d, memory=%s\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300316 p->timestamp.tv_sec / 3600,
317 (int)(p->timestamp.tv_sec / 60) % 60,
318 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300319 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300320 p->index,
321 prt_names(p->type, v4l2_type_names),
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300322 p->flags, p->field, p->sequence,
323 prt_names(p->memory, v4l2_memory_names));
324
325 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
326 for (i = 0; i < p->length; ++i) {
327 plane = &p->m.planes[i];
328 dbgarg2("plane %d: bytesused=%d, data_offset=0x%08x "
329 "offset/userptr=0x%08lx, length=%d\n",
330 i, plane->bytesused, plane->data_offset,
331 plane->m.userptr, plane->length);
332 }
333 } else {
334 dbgarg2("bytesused=%d, offset/userptr=0x%08lx, length=%d\n",
335 p->bytesused, p->m.userptr, p->length);
336 }
337
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300338 dbgarg2("timecode=%02d:%02d:%02d type=%d, "
339 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
340 tc->hours, tc->minutes, tc->seconds,
341 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
342}
343
344static inline void dbgrect(struct video_device *vfd, char *s,
345 struct v4l2_rect *r)
346{
347 dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
348 r->width, r->height);
349};
350
351static inline void v4l_print_pix_fmt(struct video_device *vfd,
352 struct v4l2_pix_format *fmt)
353{
354 dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
355 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
356 fmt->width, fmt->height,
357 (fmt->pixelformat & 0xff),
358 (fmt->pixelformat >> 8) & 0xff,
359 (fmt->pixelformat >> 16) & 0xff,
360 (fmt->pixelformat >> 24) & 0xff,
361 prt_names(fmt->field, v4l2_field_names),
362 fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
363};
364
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300365static inline void v4l_print_pix_fmt_mplane(struct video_device *vfd,
366 struct v4l2_pix_format_mplane *fmt)
367{
368 int i;
369
370 dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
371 "colorspace=%d, num_planes=%d\n",
372 fmt->width, fmt->height,
373 (fmt->pixelformat & 0xff),
374 (fmt->pixelformat >> 8) & 0xff,
375 (fmt->pixelformat >> 16) & 0xff,
376 (fmt->pixelformat >> 24) & 0xff,
377 prt_names(fmt->field, v4l2_field_names),
378 fmt->colorspace, fmt->num_planes);
379
380 for (i = 0; i < fmt->num_planes; ++i)
381 dbgarg2("plane %d: bytesperline=%d sizeimage=%d\n", i,
382 fmt->plane_fmt[i].bytesperline,
383 fmt->plane_fmt[i].sizeimage);
384}
385
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300386static inline void v4l_print_ext_ctrls(unsigned int cmd,
387 struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
388{
389 __u32 i;
390
391 if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
392 return;
393 dbgarg(cmd, "");
394 printk(KERN_CONT "class=0x%x", c->ctrl_class);
395 for (i = 0; i < c->count; i++) {
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300396 if (show_vals && !c->controls[i].size)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300397 printk(KERN_CONT " id/val=0x%x/0x%x",
398 c->controls[i].id, c->controls[i].value);
399 else
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300400 printk(KERN_CONT " id=0x%x,size=%u",
401 c->controls[i].id, c->controls[i].size);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300402 }
403 printk(KERN_CONT "\n");
404};
405
406static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
407{
408 __u32 i;
409
410 /* zero the reserved fields */
411 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300412 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300413 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300414
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300415 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
416 when using extended controls.
417 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
418 is it allowed for backwards compatibility.
419 */
420 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
421 return 0;
422 /* Check that all controls are from the same control class. */
423 for (i = 0; i < c->count; i++) {
424 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
425 c->error_idx = i;
426 return 0;
427 }
428 }
429 return 1;
430}
431
Hans Verkuila3998102008-07-21 02:57:38 -0300432static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300433{
Hans Verkuila3998102008-07-21 02:57:38 -0300434 if (ops == NULL)
435 return -EINVAL;
436
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300437 switch (type) {
438 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300439 if (ops->vidioc_g_fmt_vid_cap ||
440 ops->vidioc_g_fmt_vid_cap_mplane)
441 return 0;
442 break;
443 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
444 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300445 return 0;
446 break;
447 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300448 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300449 return 0;
450 break;
451 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300452 if (ops->vidioc_g_fmt_vid_out ||
453 ops->vidioc_g_fmt_vid_out_mplane)
454 return 0;
455 break;
456 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
457 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300458 return 0;
459 break;
460 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300461 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300462 return 0;
463 break;
464 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300465 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300466 return 0;
467 break;
468 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300469 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300470 return 0;
471 break;
472 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300473 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300474 return 0;
475 break;
476 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300477 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300478 return 0;
479 break;
480 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300481 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300482 return 0;
483 break;
484 }
485 return -EINVAL;
486}
487
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300488/**
489 * fmt_sp_to_mp() - Convert a single-plane format to its multi-planar 1-plane
490 * equivalent
491 */
492static int fmt_sp_to_mp(const struct v4l2_format *f_sp,
493 struct v4l2_format *f_mp)
494{
495 struct v4l2_pix_format_mplane *pix_mp = &f_mp->fmt.pix_mp;
496 const struct v4l2_pix_format *pix = &f_sp->fmt.pix;
497
498 if (f_sp->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
499 f_mp->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
500 else if (f_sp->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
501 f_mp->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
502 else
503 return -EINVAL;
504
505 pix_mp->width = pix->width;
506 pix_mp->height = pix->height;
507 pix_mp->pixelformat = pix->pixelformat;
508 pix_mp->field = pix->field;
509 pix_mp->colorspace = pix->colorspace;
510 pix_mp->num_planes = 1;
511 pix_mp->plane_fmt[0].sizeimage = pix->sizeimage;
512 pix_mp->plane_fmt[0].bytesperline = pix->bytesperline;
513
514 return 0;
515}
516
517/**
518 * fmt_mp_to_sp() - Convert a multi-planar 1-plane format to its single-planar
519 * equivalent
520 */
521static int fmt_mp_to_sp(const struct v4l2_format *f_mp,
522 struct v4l2_format *f_sp)
523{
524 const struct v4l2_pix_format_mplane *pix_mp = &f_mp->fmt.pix_mp;
525 struct v4l2_pix_format *pix = &f_sp->fmt.pix;
526
527 if (f_mp->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
528 f_sp->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
529 else if (f_mp->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
530 f_sp->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
531 else
532 return -EINVAL;
533
534 pix->width = pix_mp->width;
535 pix->height = pix_mp->height;
536 pix->pixelformat = pix_mp->pixelformat;
537 pix->field = pix_mp->field;
538 pix->colorspace = pix_mp->colorspace;
539 pix->sizeimage = pix_mp->plane_fmt[0].sizeimage;
540 pix->bytesperline = pix_mp->plane_fmt[0].bytesperline;
541
542 return 0;
543}
544
Hans Verkuil069b7472008-12-30 07:04:34 -0300545static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300546 unsigned int cmd, void *arg)
547{
548 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -0300549 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuild5fbf322008-10-18 13:39:53 -0300550 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300551 struct v4l2_fh *vfh = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300552 struct v4l2_format f_copy;
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300553 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300554 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300555
Hans Verkuil6a717882010-04-06 15:56:08 -0300556 if (ops == NULL) {
557 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
558 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300559 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -0300560 }
561
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300562 if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
563 !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
564 v4l_print_ioctl(vfd->name, cmd);
565 printk(KERN_CONT "\n");
566 }
567
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300568 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300569 vfh = file->private_data;
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300570 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
571 }
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300572
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300573 if (use_fh_prio) {
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300574 switch (cmd) {
575 case VIDIOC_S_CTRL:
576 case VIDIOC_S_STD:
577 case VIDIOC_S_INPUT:
578 case VIDIOC_S_OUTPUT:
579 case VIDIOC_S_TUNER:
580 case VIDIOC_S_FREQUENCY:
581 case VIDIOC_S_FMT:
582 case VIDIOC_S_CROP:
583 case VIDIOC_S_AUDIO:
584 case VIDIOC_S_AUDOUT:
585 case VIDIOC_S_EXT_CTRLS:
586 case VIDIOC_S_FBUF:
587 case VIDIOC_S_PRIORITY:
588 case VIDIOC_S_DV_PRESET:
589 case VIDIOC_S_DV_TIMINGS:
590 case VIDIOC_S_JPEGCOMP:
591 case VIDIOC_S_MODULATOR:
592 case VIDIOC_S_PARM:
593 case VIDIOC_S_HW_FREQ_SEEK:
594 case VIDIOC_ENCODER_CMD:
595 case VIDIOC_OVERLAY:
596 case VIDIOC_REQBUFS:
597 case VIDIOC_STREAMON:
598 case VIDIOC_STREAMOFF:
599 ret = v4l2_prio_check(vfd->prio, vfh->prio);
600 if (ret)
601 goto exit_prio;
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300602 ret = -ENOTTY;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300603 break;
604 }
605 }
606
Hans Verkuil6a717882010-04-06 15:56:08 -0300607 switch (cmd) {
Hans Verkuila3998102008-07-21 02:57:38 -0300608
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300609 /* --- capabilities ------------------------------------------ */
610 case VIDIOC_QUERYCAP:
611 {
612 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300613
Hans Verkuila3998102008-07-21 02:57:38 -0300614 if (!ops->vidioc_querycap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300615 break;
616
Mauro Carvalho Chehabae6db512011-06-24 13:14:14 -0300617 cap->version = LINUX_VERSION_CODE;
Hans Verkuila3998102008-07-21 02:57:38 -0300618 ret = ops->vidioc_querycap(file, fh, cap);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300619 if (!ret)
620 dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
621 "version=0x%08x, "
622 "capabilities=0x%08x\n",
623 cap->driver, cap->card, cap->bus_info,
624 cap->version,
625 cap->capabilities);
626 break;
627 }
628
629 /* --- priority ------------------------------------------ */
630 case VIDIOC_G_PRIORITY:
631 {
632 enum v4l2_priority *p = arg;
633
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300634 if (ops->vidioc_g_priority) {
635 ret = ops->vidioc_g_priority(file, fh, p);
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300636 } else if (use_fh_prio) {
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300637 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
638 ret = 0;
639 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300640 if (!ret)
641 dbgarg(cmd, "priority is %d\n", *p);
642 break;
643 }
644 case VIDIOC_S_PRIORITY:
645 {
646 enum v4l2_priority *p = arg;
647
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300648 if (!ops->vidioc_s_priority && !use_fh_prio)
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300649 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300650 dbgarg(cmd, "setting priority to %d\n", *p);
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300651 if (ops->vidioc_s_priority)
652 ret = ops->vidioc_s_priority(file, fh, *p);
653 else
654 ret = v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300655 break;
656 }
657
658 /* --- capture ioctls ---------------------------------------- */
659 case VIDIOC_ENUM_FMT:
660 {
661 struct v4l2_fmtdesc *f = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300662
Trent Piepho19c96e42009-03-04 01:21:02 -0300663 switch (f->type) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300664 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300665 if (likely(ops->vidioc_enum_fmt_vid_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300666 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300667 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300668 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300669 if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300670 ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
671 fh, f);
672 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300673 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300674 if (likely(ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300675 ret = ops->vidioc_enum_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300676 fh, f);
677 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300678 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300679 if (likely(ops->vidioc_enum_fmt_vid_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300680 ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300681 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300682 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300683 if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300684 ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
685 fh, f);
686 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300687 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300688 if (likely(ops->vidioc_enum_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300689 ret = ops->vidioc_enum_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300690 fh, f);
691 break;
692 default:
693 break;
694 }
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300695 if (likely (!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300696 dbgarg(cmd, "index=%d, type=%d, flags=%d, "
697 "pixelformat=%c%c%c%c, description='%s'\n",
698 f->index, f->type, f->flags,
699 (f->pixelformat & 0xff),
700 (f->pixelformat >> 8) & 0xff,
701 (f->pixelformat >> 16) & 0xff,
702 (f->pixelformat >> 24) & 0xff,
703 f->description);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300704 else if (ret == -ENOTTY)
705 ret = no_ioctl_err(enum);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300706 break;
707 }
708 case VIDIOC_G_FMT:
709 {
710 struct v4l2_format *f = (struct v4l2_format *)arg;
711
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300712 /* FIXME: Should be one dump per type */
713 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
714
715 switch (f->type) {
716 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300717 if (ops->vidioc_g_fmt_vid_cap) {
Hans Verkuila3998102008-07-21 02:57:38 -0300718 ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300719 } else if (ops->vidioc_g_fmt_vid_cap_mplane) {
720 if (fmt_sp_to_mp(f, &f_copy))
721 break;
722 ret = ops->vidioc_g_fmt_vid_cap_mplane(file, fh,
723 &f_copy);
724 if (ret)
725 break;
726
727 /* Driver is currently in multi-planar format,
728 * we can't return it in single-planar API*/
729 if (f_copy.fmt.pix_mp.num_planes > 1) {
730 ret = -EBUSY;
731 break;
732 }
733
734 ret = fmt_mp_to_sp(&f_copy, f);
735 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300736 if (!ret)
737 v4l_print_pix_fmt(vfd, &f->fmt.pix);
738 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300739 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
740 if (ops->vidioc_g_fmt_vid_cap_mplane) {
741 ret = ops->vidioc_g_fmt_vid_cap_mplane(file,
742 fh, f);
743 } else if (ops->vidioc_g_fmt_vid_cap) {
744 if (fmt_mp_to_sp(f, &f_copy))
745 break;
746 ret = ops->vidioc_g_fmt_vid_cap(file,
747 fh, &f_copy);
748 if (ret)
749 break;
750
751 ret = fmt_sp_to_mp(&f_copy, f);
752 }
753 if (!ret)
754 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
755 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300756 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300757 if (likely(ops->vidioc_g_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300758 ret = ops->vidioc_g_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300759 fh, f);
760 break;
761 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300762 if (ops->vidioc_g_fmt_vid_out) {
Hans Verkuila3998102008-07-21 02:57:38 -0300763 ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300764 } else if (ops->vidioc_g_fmt_vid_out_mplane) {
765 if (fmt_sp_to_mp(f, &f_copy))
766 break;
767 ret = ops->vidioc_g_fmt_vid_out_mplane(file, fh,
768 &f_copy);
769 if (ret)
770 break;
771
772 /* Driver is currently in multi-planar format,
773 * we can't return it in single-planar API*/
774 if (f_copy.fmt.pix_mp.num_planes > 1) {
775 ret = -EBUSY;
776 break;
777 }
778
779 ret = fmt_mp_to_sp(&f_copy, f);
780 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300781 if (!ret)
782 v4l_print_pix_fmt(vfd, &f->fmt.pix);
783 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300784 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
785 if (ops->vidioc_g_fmt_vid_out_mplane) {
786 ret = ops->vidioc_g_fmt_vid_out_mplane(file,
787 fh, f);
788 } else if (ops->vidioc_g_fmt_vid_out) {
789 if (fmt_mp_to_sp(f, &f_copy))
790 break;
791 ret = ops->vidioc_g_fmt_vid_out(file,
792 fh, &f_copy);
793 if (ret)
794 break;
795
796 ret = fmt_sp_to_mp(&f_copy, f);
797 }
798 if (!ret)
799 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
800 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300801 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300802 if (likely(ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300803 ret = ops->vidioc_g_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300804 fh, f);
805 break;
806 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300807 if (likely(ops->vidioc_g_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300808 ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300809 break;
810 case V4L2_BUF_TYPE_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300811 if (likely(ops->vidioc_g_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300812 ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300813 break;
814 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300815 if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300816 ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300817 fh, f);
818 break;
819 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300820 if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300821 ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300822 fh, f);
823 break;
824 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300825 if (likely(ops->vidioc_g_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300826 ret = ops->vidioc_g_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300827 fh, f);
828 break;
829 }
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300830 if (unlikely(ret == -ENOTTY))
831 ret = no_ioctl_err(g);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300832
833 break;
834 }
835 case VIDIOC_S_FMT:
836 {
837 struct v4l2_format *f = (struct v4l2_format *)arg;
838
839 /* FIXME: Should be one dump per type */
840 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
841
842 switch (f->type) {
843 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300844 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300845 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300846 if (ops->vidioc_s_fmt_vid_cap) {
Hans Verkuila3998102008-07-21 02:57:38 -0300847 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300848 } else if (ops->vidioc_s_fmt_vid_cap_mplane) {
849 if (fmt_sp_to_mp(f, &f_copy))
850 break;
851 ret = ops->vidioc_s_fmt_vid_cap_mplane(file, fh,
852 &f_copy);
853 if (ret)
854 break;
855
856 if (f_copy.fmt.pix_mp.num_planes > 1) {
857 /* Drivers shouldn't adjust from 1-plane
858 * to more than 1-plane formats */
859 ret = -EBUSY;
860 WARN_ON(1);
861 break;
862 }
863
864 ret = fmt_mp_to_sp(&f_copy, f);
865 }
866 break;
867 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
868 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
869 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
870 if (ops->vidioc_s_fmt_vid_cap_mplane) {
871 ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
872 fh, f);
873 } else if (ops->vidioc_s_fmt_vid_cap &&
874 f->fmt.pix_mp.num_planes == 1) {
875 if (fmt_mp_to_sp(f, &f_copy))
876 break;
877 ret = ops->vidioc_s_fmt_vid_cap(file,
878 fh, &f_copy);
879 if (ret)
880 break;
881
882 ret = fmt_sp_to_mp(&f_copy, f);
883 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300884 break;
885 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300886 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300887 if (ops->vidioc_s_fmt_vid_overlay)
888 ret = ops->vidioc_s_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300889 fh, f);
890 break;
891 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300892 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300893 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300894 if (ops->vidioc_s_fmt_vid_out) {
Hans Verkuila3998102008-07-21 02:57:38 -0300895 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300896 } else if (ops->vidioc_s_fmt_vid_out_mplane) {
897 if (fmt_sp_to_mp(f, &f_copy))
898 break;
899 ret = ops->vidioc_s_fmt_vid_out_mplane(file, fh,
900 &f_copy);
901 if (ret)
902 break;
903
904 if (f_copy.fmt.pix_mp.num_planes > 1) {
905 /* Drivers shouldn't adjust from 1-plane
906 * to more than 1-plane formats */
907 ret = -EBUSY;
908 WARN_ON(1);
909 break;
910 }
911
912 ret = fmt_mp_to_sp(&f_copy, f);
913 }
914 break;
915 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
916 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
917 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
918 if (ops->vidioc_s_fmt_vid_out_mplane) {
919 ret = ops->vidioc_s_fmt_vid_out_mplane(file,
920 fh, f);
921 } else if (ops->vidioc_s_fmt_vid_out &&
922 f->fmt.pix_mp.num_planes == 1) {
923 if (fmt_mp_to_sp(f, &f_copy))
924 break;
925 ret = ops->vidioc_s_fmt_vid_out(file,
926 fh, &f_copy);
927 if (ret)
928 break;
929
930 ret = fmt_mp_to_sp(&f_copy, f);
931 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300932 break;
933 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300934 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300935 if (ops->vidioc_s_fmt_vid_out_overlay)
936 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300937 fh, f);
938 break;
939 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300940 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300941 if (likely(ops->vidioc_s_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300942 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300943 break;
944 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300945 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300946 if (likely(ops->vidioc_s_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300947 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300948 break;
949 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300950 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300951 if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300952 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300953 fh, f);
954 break;
955 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300956 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300957 if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300958 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300959 fh, f);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300960
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300961 break;
962 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300963 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300964 if (likely(ops->vidioc_s_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300965 ret = ops->vidioc_s_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300966 fh, f);
967 break;
968 }
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300969 if (unlikely(ret == -ENOTTY))
970 ret = no_ioctl_err(g);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300971 break;
972 }
973 case VIDIOC_TRY_FMT:
974 {
975 struct v4l2_format *f = (struct v4l2_format *)arg;
976
977 /* FIXME: Should be one dump per type */
978 dbgarg(cmd, "type=%s\n", prt_names(f->type,
979 v4l2_type_names));
980 switch (f->type) {
981 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300982 CLEAR_AFTER_FIELD(f, fmt.pix);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300983 if (ops->vidioc_try_fmt_vid_cap) {
Hans Verkuila3998102008-07-21 02:57:38 -0300984 ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300985 } else if (ops->vidioc_try_fmt_vid_cap_mplane) {
986 if (fmt_sp_to_mp(f, &f_copy))
987 break;
988 ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
989 fh, &f_copy);
990 if (ret)
991 break;
992
993 if (f_copy.fmt.pix_mp.num_planes > 1) {
994 /* Drivers shouldn't adjust from 1-plane
995 * to more than 1-plane formats */
996 ret = -EBUSY;
997 WARN_ON(1);
998 break;
999 }
1000 ret = fmt_mp_to_sp(&f_copy, f);
1001 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001002 if (!ret)
1003 v4l_print_pix_fmt(vfd, &f->fmt.pix);
1004 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001005 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1006 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
1007 if (ops->vidioc_try_fmt_vid_cap_mplane) {
1008 ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
1009 fh, f);
1010 } else if (ops->vidioc_try_fmt_vid_cap &&
1011 f->fmt.pix_mp.num_planes == 1) {
1012 if (fmt_mp_to_sp(f, &f_copy))
1013 break;
1014 ret = ops->vidioc_try_fmt_vid_cap(file,
1015 fh, &f_copy);
1016 if (ret)
1017 break;
1018
1019 ret = fmt_sp_to_mp(&f_copy, f);
1020 }
1021 if (!ret)
1022 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1023 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001024 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001025 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001026 if (likely(ops->vidioc_try_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001027 ret = ops->vidioc_try_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001028 fh, f);
1029 break;
1030 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001031 CLEAR_AFTER_FIELD(f, fmt.pix);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001032 if (ops->vidioc_try_fmt_vid_out) {
Hans Verkuila3998102008-07-21 02:57:38 -03001033 ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001034 } else if (ops->vidioc_try_fmt_vid_out_mplane) {
1035 if (fmt_sp_to_mp(f, &f_copy))
1036 break;
1037 ret = ops->vidioc_try_fmt_vid_out_mplane(file,
1038 fh, &f_copy);
1039 if (ret)
1040 break;
1041
1042 if (f_copy.fmt.pix_mp.num_planes > 1) {
1043 /* Drivers shouldn't adjust from 1-plane
1044 * to more than 1-plane formats */
1045 ret = -EBUSY;
1046 WARN_ON(1);
1047 break;
1048 }
1049 ret = fmt_mp_to_sp(&f_copy, f);
1050 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001051 if (!ret)
1052 v4l_print_pix_fmt(vfd, &f->fmt.pix);
1053 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001054 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1055 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
1056 if (ops->vidioc_try_fmt_vid_out_mplane) {
1057 ret = ops->vidioc_try_fmt_vid_out_mplane(file,
1058 fh, f);
1059 } else if (ops->vidioc_try_fmt_vid_out &&
1060 f->fmt.pix_mp.num_planes == 1) {
1061 if (fmt_mp_to_sp(f, &f_copy))
1062 break;
1063 ret = ops->vidioc_try_fmt_vid_out(file,
1064 fh, &f_copy);
1065 if (ret)
1066 break;
1067
1068 ret = fmt_sp_to_mp(&f_copy, f);
1069 }
1070 if (!ret)
1071 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1072 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001073 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001074 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001075 if (likely(ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001076 ret = ops->vidioc_try_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001077 fh, f);
1078 break;
1079 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001080 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001081 if (likely(ops->vidioc_try_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001082 ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001083 break;
1084 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001085 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001086 if (likely(ops->vidioc_try_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001087 ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001088 break;
1089 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001090 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001091 if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001092 ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001093 fh, f);
1094 break;
1095 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001096 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001097 if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001098 ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001099 fh, f);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001100 else
1101 ret = no_ioctl_err(try);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001102 break;
1103 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001104 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001105 if (likely(ops->vidioc_try_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -03001106 ret = ops->vidioc_try_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001107 fh, f);
1108 break;
1109 }
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001110 if (unlikely(ret == -ENOTTY))
1111 ret = no_ioctl_err(g);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001112 break;
1113 }
1114 /* FIXME: Those buf reqs could be handled here,
1115 with some changes on videobuf to allow its header to be included at
1116 videodev2.h or being merged at videodev2.
1117 */
1118 case VIDIOC_REQBUFS:
1119 {
1120 struct v4l2_requestbuffers *p = arg;
1121
Hans Verkuila3998102008-07-21 02:57:38 -03001122 if (!ops->vidioc_reqbufs)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001123 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001124 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001125 if (ret)
1126 break;
1127
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001128 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1129 CLEAR_AFTER_FIELD(p, memory);
1130
Hans Verkuila3998102008-07-21 02:57:38 -03001131 ret = ops->vidioc_reqbufs(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001132 dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
1133 p->count,
1134 prt_names(p->type, v4l2_type_names),
1135 prt_names(p->memory, v4l2_memory_names));
1136 break;
1137 }
1138 case VIDIOC_QUERYBUF:
1139 {
1140 struct v4l2_buffer *p = arg;
1141
Hans Verkuila3998102008-07-21 02:57:38 -03001142 if (!ops->vidioc_querybuf)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001143 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001144 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001145 if (ret)
1146 break;
1147
Hans Verkuila3998102008-07-21 02:57:38 -03001148 ret = ops->vidioc_querybuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001149 if (!ret)
1150 dbgbuf(cmd, vfd, p);
1151 break;
1152 }
1153 case VIDIOC_QBUF:
1154 {
1155 struct v4l2_buffer *p = arg;
1156
Hans Verkuila3998102008-07-21 02:57:38 -03001157 if (!ops->vidioc_qbuf)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001158 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001159 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001160 if (ret)
1161 break;
1162
Hans Verkuila3998102008-07-21 02:57:38 -03001163 ret = ops->vidioc_qbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001164 if (!ret)
1165 dbgbuf(cmd, vfd, p);
1166 break;
1167 }
1168 case VIDIOC_DQBUF:
1169 {
1170 struct v4l2_buffer *p = arg;
1171
Hans Verkuila3998102008-07-21 02:57:38 -03001172 if (!ops->vidioc_dqbuf)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001173 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001174 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001175 if (ret)
1176 break;
1177
Hans Verkuila3998102008-07-21 02:57:38 -03001178 ret = ops->vidioc_dqbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001179 if (!ret)
1180 dbgbuf(cmd, vfd, p);
1181 break;
1182 }
1183 case VIDIOC_OVERLAY:
1184 {
1185 int *i = arg;
1186
Hans Verkuila3998102008-07-21 02:57:38 -03001187 if (!ops->vidioc_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001188 break;
1189 dbgarg(cmd, "value=%d\n", *i);
Hans Verkuila3998102008-07-21 02:57:38 -03001190 ret = ops->vidioc_overlay(file, fh, *i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001191 break;
1192 }
1193 case VIDIOC_G_FBUF:
1194 {
1195 struct v4l2_framebuffer *p = arg;
1196
Hans Verkuila3998102008-07-21 02:57:38 -03001197 if (!ops->vidioc_g_fbuf)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001198 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001199 ret = ops->vidioc_g_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001200 if (!ret) {
1201 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1202 p->capability, p->flags,
1203 (unsigned long)p->base);
1204 v4l_print_pix_fmt(vfd, &p->fmt);
1205 }
1206 break;
1207 }
1208 case VIDIOC_S_FBUF:
1209 {
1210 struct v4l2_framebuffer *p = arg;
1211
Hans Verkuila3998102008-07-21 02:57:38 -03001212 if (!ops->vidioc_s_fbuf)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001213 break;
1214 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1215 p->capability, p->flags, (unsigned long)p->base);
1216 v4l_print_pix_fmt(vfd, &p->fmt);
Hans Verkuila3998102008-07-21 02:57:38 -03001217 ret = ops->vidioc_s_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001218 break;
1219 }
1220 case VIDIOC_STREAMON:
1221 {
1222 enum v4l2_buf_type i = *(int *)arg;
1223
Hans Verkuila3998102008-07-21 02:57:38 -03001224 if (!ops->vidioc_streamon)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001225 break;
1226 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001227 ret = ops->vidioc_streamon(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001228 break;
1229 }
1230 case VIDIOC_STREAMOFF:
1231 {
1232 enum v4l2_buf_type i = *(int *)arg;
1233
Hans Verkuila3998102008-07-21 02:57:38 -03001234 if (!ops->vidioc_streamoff)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001235 break;
1236 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001237 ret = ops->vidioc_streamoff(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001238 break;
1239 }
1240 /* ---------- tv norms ---------- */
1241 case VIDIOC_ENUMSTD:
1242 {
1243 struct v4l2_standard *p = arg;
1244 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1245 unsigned int index = p->index, i, j = 0;
1246 const char *descr = "";
1247
1248 /* Return norm array in a canonical way */
1249 for (i = 0; i <= index && id; i++) {
1250 /* last std value in the standards array is 0, so this
1251 while always ends there since (id & 0) == 0. */
1252 while ((id & standards[j].std) != standards[j].std)
1253 j++;
1254 curr_id = standards[j].std;
1255 descr = standards[j].descr;
1256 j++;
1257 if (curr_id == 0)
1258 break;
1259 if (curr_id != V4L2_STD_PAL &&
1260 curr_id != V4L2_STD_SECAM &&
1261 curr_id != V4L2_STD_NTSC)
1262 id &= ~curr_id;
1263 }
1264 if (i <= index)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001265 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001266
1267 v4l2_video_std_construct(p, curr_id, descr);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001268
1269 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1270 "framelines=%d\n", p->index,
1271 (unsigned long long)p->id, p->name,
1272 p->frameperiod.numerator,
1273 p->frameperiod.denominator,
1274 p->framelines);
1275
1276 ret = 0;
1277 break;
1278 }
1279 case VIDIOC_G_STD:
1280 {
1281 v4l2_std_id *id = arg;
1282
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001283 /* Calls the specific handler */
Hans Verkuila3998102008-07-21 02:57:38 -03001284 if (ops->vidioc_g_std)
1285 ret = ops->vidioc_g_std(file, fh, id);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001286 else if (vfd->current_norm) {
1287 ret = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001288 *id = vfd->current_norm;
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001289 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001290
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001291 if (likely(!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001292 dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1293 break;
1294 }
1295 case VIDIOC_S_STD:
1296 {
1297 v4l2_std_id *id = arg, norm;
1298
1299 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1300
1301 norm = (*id) & vfd->tvnorms;
1302 if (vfd->tvnorms && !norm) /* Check if std is supported */
1303 break;
1304
1305 /* Calls the specific handler */
Hans Verkuila3998102008-07-21 02:57:38 -03001306 if (ops->vidioc_s_std)
1307 ret = ops->vidioc_s_std(file, fh, &norm);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001308
1309 /* Updates standard information */
1310 if (ret >= 0)
1311 vfd->current_norm = norm;
1312 break;
1313 }
1314 case VIDIOC_QUERYSTD:
1315 {
1316 v4l2_std_id *p = arg;
1317
Hans Verkuila3998102008-07-21 02:57:38 -03001318 if (!ops->vidioc_querystd)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001319 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001320 ret = ops->vidioc_querystd(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001321 if (!ret)
1322 dbgarg(cmd, "detected std=%08Lx\n",
1323 (unsigned long long)*p);
1324 break;
1325 }
1326 /* ------ input switching ---------- */
1327 /* FIXME: Inputs can be handled inside videodev2 */
1328 case VIDIOC_ENUMINPUT:
1329 {
1330 struct v4l2_input *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001331
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001332 /*
1333 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1334 * CAP_STD here based on ioctl handler provided by the
1335 * driver. If the driver doesn't support these
1336 * for a specific input, it must override these flags.
1337 */
1338 if (ops->vidioc_s_std)
1339 p->capabilities |= V4L2_IN_CAP_STD;
1340 if (ops->vidioc_s_dv_preset)
1341 p->capabilities |= V4L2_IN_CAP_PRESETS;
1342 if (ops->vidioc_s_dv_timings)
1343 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
1344
Hans Verkuila3998102008-07-21 02:57:38 -03001345 if (!ops->vidioc_enum_input)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001346 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001347
Hans Verkuila3998102008-07-21 02:57:38 -03001348 ret = ops->vidioc_enum_input(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001349 if (!ret)
1350 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1351 "audioset=%d, "
1352 "tuner=%d, std=%08Lx, status=%d\n",
1353 p->index, p->name, p->type, p->audioset,
1354 p->tuner,
1355 (unsigned long long)p->std,
1356 p->status);
1357 break;
1358 }
1359 case VIDIOC_G_INPUT:
1360 {
1361 unsigned int *i = arg;
1362
Hans Verkuila3998102008-07-21 02:57:38 -03001363 if (!ops->vidioc_g_input)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001364 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001365 ret = ops->vidioc_g_input(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001366 if (!ret)
1367 dbgarg(cmd, "value=%d\n", *i);
1368 break;
1369 }
1370 case VIDIOC_S_INPUT:
1371 {
1372 unsigned int *i = arg;
1373
Hans Verkuila3998102008-07-21 02:57:38 -03001374 if (!ops->vidioc_s_input)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001375 break;
1376 dbgarg(cmd, "value=%d\n", *i);
Hans Verkuila3998102008-07-21 02:57:38 -03001377 ret = ops->vidioc_s_input(file, fh, *i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001378 break;
1379 }
1380
1381 /* ------ output switching ---------- */
1382 case VIDIOC_ENUMOUTPUT:
1383 {
1384 struct v4l2_output *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001385
Hans Verkuila3998102008-07-21 02:57:38 -03001386 if (!ops->vidioc_enum_output)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001387 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001388
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001389 /*
1390 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1391 * CAP_STD here based on ioctl handler provided by the
1392 * driver. If the driver doesn't support these
1393 * for a specific output, it must override these flags.
1394 */
1395 if (ops->vidioc_s_std)
1396 p->capabilities |= V4L2_OUT_CAP_STD;
1397 if (ops->vidioc_s_dv_preset)
1398 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1399 if (ops->vidioc_s_dv_timings)
1400 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1401
Hans Verkuila3998102008-07-21 02:57:38 -03001402 ret = ops->vidioc_enum_output(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001403 if (!ret)
1404 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1405 "audioset=0x%x, "
1406 "modulator=%d, std=0x%08Lx\n",
1407 p->index, p->name, p->type, p->audioset,
1408 p->modulator, (unsigned long long)p->std);
1409 break;
1410 }
1411 case VIDIOC_G_OUTPUT:
1412 {
1413 unsigned int *i = arg;
1414
Hans Verkuila3998102008-07-21 02:57:38 -03001415 if (!ops->vidioc_g_output)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001416 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001417 ret = ops->vidioc_g_output(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001418 if (!ret)
1419 dbgarg(cmd, "value=%d\n", *i);
1420 break;
1421 }
1422 case VIDIOC_S_OUTPUT:
1423 {
1424 unsigned int *i = arg;
1425
Hans Verkuila3998102008-07-21 02:57:38 -03001426 if (!ops->vidioc_s_output)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001427 break;
1428 dbgarg(cmd, "value=%d\n", *i);
Hans Verkuila3998102008-07-21 02:57:38 -03001429 ret = ops->vidioc_s_output(file, fh, *i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001430 break;
1431 }
1432
1433 /* --- controls ---------------------------------------------- */
1434 case VIDIOC_QUERYCTRL:
1435 {
1436 struct v4l2_queryctrl *p = arg;
1437
Hans Verkuil2d28b682011-03-12 08:54:43 -03001438 if (vfh && vfh->ctrl_handler)
1439 ret = v4l2_queryctrl(vfh->ctrl_handler, p);
1440 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001441 ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1442 else if (ops->vidioc_queryctrl)
1443 ret = ops->vidioc_queryctrl(file, fh, p);
1444 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001445 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001446 if (!ret)
1447 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1448 "step=%d, default=%d, flags=0x%08x\n",
1449 p->id, p->type, p->name,
1450 p->minimum, p->maximum,
1451 p->step, p->default_value, p->flags);
1452 else
1453 dbgarg(cmd, "id=0x%x\n", p->id);
1454 break;
1455 }
1456 case VIDIOC_G_CTRL:
1457 {
1458 struct v4l2_control *p = arg;
1459
Hans Verkuil2d28b682011-03-12 08:54:43 -03001460 if (vfh && vfh->ctrl_handler)
1461 ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
1462 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001463 ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1464 else if (ops->vidioc_g_ctrl)
Hans Verkuila3998102008-07-21 02:57:38 -03001465 ret = ops->vidioc_g_ctrl(file, fh, p);
1466 else if (ops->vidioc_g_ext_ctrls) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001467 struct v4l2_ext_controls ctrls;
1468 struct v4l2_ext_control ctrl;
1469
1470 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1471 ctrls.count = 1;
1472 ctrls.controls = &ctrl;
1473 ctrl.id = p->id;
1474 ctrl.value = p->value;
1475 if (check_ext_ctrls(&ctrls, 1)) {
Hans Verkuila3998102008-07-21 02:57:38 -03001476 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001477 if (ret == 0)
1478 p->value = ctrl.value;
1479 }
1480 } else
1481 break;
1482 if (!ret)
1483 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1484 else
1485 dbgarg(cmd, "id=0x%x\n", p->id);
1486 break;
1487 }
1488 case VIDIOC_S_CTRL:
1489 {
1490 struct v4l2_control *p = arg;
1491 struct v4l2_ext_controls ctrls;
1492 struct v4l2_ext_control ctrl;
1493
Hans Verkuil2d28b682011-03-12 08:54:43 -03001494 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001495 !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001496 break;
1497
1498 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1499
Hans Verkuil2d28b682011-03-12 08:54:43 -03001500 if (vfh && vfh->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001501 ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001502 break;
1503 }
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001504 if (vfd->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001505 ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001506 break;
1507 }
Hans Verkuila3998102008-07-21 02:57:38 -03001508 if (ops->vidioc_s_ctrl) {
1509 ret = ops->vidioc_s_ctrl(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001510 break;
1511 }
Hans Verkuila3998102008-07-21 02:57:38 -03001512 if (!ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001513 break;
1514
1515 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1516 ctrls.count = 1;
1517 ctrls.controls = &ctrl;
1518 ctrl.id = p->id;
1519 ctrl.value = p->value;
1520 if (check_ext_ctrls(&ctrls, 1))
Hans Verkuila3998102008-07-21 02:57:38 -03001521 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001522 break;
1523 }
1524 case VIDIOC_G_EXT_CTRLS:
1525 {
1526 struct v4l2_ext_controls *p = arg;
1527
1528 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001529 if (vfh && vfh->ctrl_handler)
1530 ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1531 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001532 ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1533 else if (ops->vidioc_g_ext_ctrls && check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001534 ret = ops->vidioc_g_ext_ctrls(file, fh, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001535 else
1536 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001537 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1538 break;
1539 }
1540 case VIDIOC_S_EXT_CTRLS:
1541 {
1542 struct v4l2_ext_controls *p = arg;
1543
1544 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001545 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1546 !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001547 break;
1548 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001549 if (vfh && vfh->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001550 ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001551 else if (vfd->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001552 ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001553 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001554 ret = ops->vidioc_s_ext_ctrls(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001555 break;
1556 }
1557 case VIDIOC_TRY_EXT_CTRLS:
1558 {
1559 struct v4l2_ext_controls *p = arg;
1560
1561 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001562 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1563 !ops->vidioc_try_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001564 break;
1565 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001566 if (vfh && vfh->ctrl_handler)
1567 ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1568 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001569 ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1570 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001571 ret = ops->vidioc_try_ext_ctrls(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001572 break;
1573 }
1574 case VIDIOC_QUERYMENU:
1575 {
1576 struct v4l2_querymenu *p = arg;
1577
Hans Verkuil2d28b682011-03-12 08:54:43 -03001578 if (vfh && vfh->ctrl_handler)
1579 ret = v4l2_querymenu(vfh->ctrl_handler, p);
1580 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001581 ret = v4l2_querymenu(vfd->ctrl_handler, p);
1582 else if (ops->vidioc_querymenu)
1583 ret = ops->vidioc_querymenu(file, fh, p);
1584 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001585 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001586 if (!ret)
1587 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1588 p->id, p->index, p->name);
1589 else
1590 dbgarg(cmd, "id=0x%x, index=%d\n",
1591 p->id, p->index);
1592 break;
1593 }
1594 /* --- audio ---------------------------------------------- */
1595 case VIDIOC_ENUMAUDIO:
1596 {
1597 struct v4l2_audio *p = arg;
1598
Hans Verkuila3998102008-07-21 02:57:38 -03001599 if (!ops->vidioc_enumaudio)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001600 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001601 ret = ops->vidioc_enumaudio(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001602 if (!ret)
1603 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1604 "mode=0x%x\n", p->index, p->name,
1605 p->capability, p->mode);
1606 else
1607 dbgarg(cmd, "index=%d\n", p->index);
1608 break;
1609 }
1610 case VIDIOC_G_AUDIO:
1611 {
1612 struct v4l2_audio *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001613
Hans Verkuila3998102008-07-21 02:57:38 -03001614 if (!ops->vidioc_g_audio)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001615 break;
1616
Hans Verkuila3998102008-07-21 02:57:38 -03001617 ret = ops->vidioc_g_audio(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001618 if (!ret)
1619 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1620 "mode=0x%x\n", p->index,
1621 p->name, p->capability, p->mode);
1622 else
1623 dbgarg(cmd, "index=%d\n", p->index);
1624 break;
1625 }
1626 case VIDIOC_S_AUDIO:
1627 {
1628 struct v4l2_audio *p = arg;
1629
Hans Verkuila3998102008-07-21 02:57:38 -03001630 if (!ops->vidioc_s_audio)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001631 break;
1632 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1633 "mode=0x%x\n", p->index, p->name,
1634 p->capability, p->mode);
Hans Verkuila3998102008-07-21 02:57:38 -03001635 ret = ops->vidioc_s_audio(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001636 break;
1637 }
1638 case VIDIOC_ENUMAUDOUT:
1639 {
1640 struct v4l2_audioout *p = arg;
1641
Hans Verkuila3998102008-07-21 02:57:38 -03001642 if (!ops->vidioc_enumaudout)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001643 break;
1644 dbgarg(cmd, "Enum for index=%d\n", p->index);
Hans Verkuila3998102008-07-21 02:57:38 -03001645 ret = ops->vidioc_enumaudout(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001646 if (!ret)
1647 dbgarg2("index=%d, name=%s, capability=%d, "
1648 "mode=%d\n", p->index, p->name,
1649 p->capability, p->mode);
1650 break;
1651 }
1652 case VIDIOC_G_AUDOUT:
1653 {
1654 struct v4l2_audioout *p = arg;
1655
Hans Verkuila3998102008-07-21 02:57:38 -03001656 if (!ops->vidioc_g_audout)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001657 break;
Trent Piepho337f9d22009-03-04 01:21:02 -03001658
Hans Verkuila3998102008-07-21 02:57:38 -03001659 ret = ops->vidioc_g_audout(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001660 if (!ret)
1661 dbgarg2("index=%d, name=%s, capability=%d, "
1662 "mode=%d\n", p->index, p->name,
1663 p->capability, p->mode);
1664 break;
1665 }
1666 case VIDIOC_S_AUDOUT:
1667 {
1668 struct v4l2_audioout *p = arg;
1669
Hans Verkuila3998102008-07-21 02:57:38 -03001670 if (!ops->vidioc_s_audout)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001671 break;
1672 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1673 "mode=%d\n", p->index, p->name,
1674 p->capability, p->mode);
1675
Hans Verkuila3998102008-07-21 02:57:38 -03001676 ret = ops->vidioc_s_audout(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001677 break;
1678 }
1679 case VIDIOC_G_MODULATOR:
1680 {
1681 struct v4l2_modulator *p = arg;
1682
Hans Verkuila3998102008-07-21 02:57:38 -03001683 if (!ops->vidioc_g_modulator)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001684 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001685 ret = ops->vidioc_g_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001686 if (!ret)
1687 dbgarg(cmd, "index=%d, name=%s, "
1688 "capability=%d, rangelow=%d,"
1689 " rangehigh=%d, txsubchans=%d\n",
1690 p->index, p->name, p->capability,
1691 p->rangelow, p->rangehigh,
1692 p->txsubchans);
1693 break;
1694 }
1695 case VIDIOC_S_MODULATOR:
1696 {
1697 struct v4l2_modulator *p = arg;
1698
Hans Verkuila3998102008-07-21 02:57:38 -03001699 if (!ops->vidioc_s_modulator)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001700 break;
1701 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1702 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1703 p->index, p->name, p->capability, p->rangelow,
1704 p->rangehigh, p->txsubchans);
Hans Verkuila3998102008-07-21 02:57:38 -03001705 ret = ops->vidioc_s_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001706 break;
1707 }
1708 case VIDIOC_G_CROP:
1709 {
1710 struct v4l2_crop *p = arg;
1711
Hans Verkuila3998102008-07-21 02:57:38 -03001712 if (!ops->vidioc_g_crop)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001713 break;
Mauro Carvalho Chehaba56a18c2008-11-11 21:15:03 -03001714
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001715 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001716 ret = ops->vidioc_g_crop(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001717 if (!ret)
1718 dbgrect(vfd, "", &p->c);
1719 break;
1720 }
1721 case VIDIOC_S_CROP:
1722 {
1723 struct v4l2_crop *p = arg;
1724
Hans Verkuila3998102008-07-21 02:57:38 -03001725 if (!ops->vidioc_s_crop)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001726 break;
1727 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1728 dbgrect(vfd, "", &p->c);
Hans Verkuila3998102008-07-21 02:57:38 -03001729 ret = ops->vidioc_s_crop(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001730 break;
1731 }
1732 case VIDIOC_CROPCAP:
1733 {
1734 struct v4l2_cropcap *p = arg;
1735
1736 /*FIXME: Should also show v4l2_fract pixelaspect */
Hans Verkuila3998102008-07-21 02:57:38 -03001737 if (!ops->vidioc_cropcap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001738 break;
Mauro Carvalho Chehaba56a18c2008-11-11 21:15:03 -03001739
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001740 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001741 ret = ops->vidioc_cropcap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001742 if (!ret) {
1743 dbgrect(vfd, "bounds ", &p->bounds);
1744 dbgrect(vfd, "defrect ", &p->defrect);
1745 }
1746 break;
1747 }
1748 case VIDIOC_G_JPEGCOMP:
1749 {
1750 struct v4l2_jpegcompression *p = arg;
1751
Hans Verkuila3998102008-07-21 02:57:38 -03001752 if (!ops->vidioc_g_jpegcomp)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001753 break;
Mauro Carvalho Chehaba56a18c2008-11-11 21:15:03 -03001754
Hans Verkuila3998102008-07-21 02:57:38 -03001755 ret = ops->vidioc_g_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001756 if (!ret)
1757 dbgarg(cmd, "quality=%d, APPn=%d, "
1758 "APP_len=%d, COM_len=%d, "
1759 "jpeg_markers=%d\n",
1760 p->quality, p->APPn, p->APP_len,
1761 p->COM_len, p->jpeg_markers);
1762 break;
1763 }
1764 case VIDIOC_S_JPEGCOMP:
1765 {
1766 struct v4l2_jpegcompression *p = arg;
1767
Hans Verkuila3998102008-07-21 02:57:38 -03001768 if (!ops->vidioc_g_jpegcomp)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001769 break;
1770 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1771 "COM_len=%d, jpeg_markers=%d\n",
1772 p->quality, p->APPn, p->APP_len,
1773 p->COM_len, p->jpeg_markers);
Hans Verkuila3998102008-07-21 02:57:38 -03001774 ret = ops->vidioc_s_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001775 break;
1776 }
1777 case VIDIOC_G_ENC_INDEX:
1778 {
1779 struct v4l2_enc_idx *p = arg;
1780
Hans Verkuila3998102008-07-21 02:57:38 -03001781 if (!ops->vidioc_g_enc_index)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001782 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001783 ret = ops->vidioc_g_enc_index(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001784 if (!ret)
1785 dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1786 p->entries, p->entries_cap);
1787 break;
1788 }
1789 case VIDIOC_ENCODER_CMD:
1790 {
1791 struct v4l2_encoder_cmd *p = arg;
1792
Hans Verkuila3998102008-07-21 02:57:38 -03001793 if (!ops->vidioc_encoder_cmd)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001794 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001795 ret = ops->vidioc_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001796 if (!ret)
1797 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1798 break;
1799 }
1800 case VIDIOC_TRY_ENCODER_CMD:
1801 {
1802 struct v4l2_encoder_cmd *p = arg;
1803
Hans Verkuila3998102008-07-21 02:57:38 -03001804 if (!ops->vidioc_try_encoder_cmd)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001805 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001806 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001807 if (!ret)
1808 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1809 break;
1810 }
1811 case VIDIOC_G_PARM:
1812 {
1813 struct v4l2_streamparm *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001814
Hans Verkuila3998102008-07-21 02:57:38 -03001815 if (ops->vidioc_g_parm) {
Trent Piepho34796bc2009-03-28 22:25:35 -03001816 ret = check_fmt(ops, p->type);
1817 if (ret)
1818 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001819 ret = ops->vidioc_g_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001820 } else {
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001821 v4l2_std_id std = vfd->current_norm;
1822
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001823 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001824 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001825
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001826 ret = 0;
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001827 if (ops->vidioc_g_std)
1828 ret = ops->vidioc_g_std(file, fh, &std);
1829 else if (std == 0)
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001830 ret = -ENOTTY;
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001831 if (ret == 0)
1832 v4l2_video_std_frame_period(std,
1833 &p->parm.capture.timeperframe);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001834 }
1835
1836 dbgarg(cmd, "type=%d\n", p->type);
1837 break;
1838 }
1839 case VIDIOC_S_PARM:
1840 {
1841 struct v4l2_streamparm *p = arg;
1842
Hans Verkuila3998102008-07-21 02:57:38 -03001843 if (!ops->vidioc_s_parm)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001844 break;
Trent Piepho34796bc2009-03-28 22:25:35 -03001845 ret = check_fmt(ops, p->type);
1846 if (ret)
1847 break;
1848
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001849 dbgarg(cmd, "type=%d\n", p->type);
Hans Verkuila3998102008-07-21 02:57:38 -03001850 ret = ops->vidioc_s_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001851 break;
1852 }
1853 case VIDIOC_G_TUNER:
1854 {
1855 struct v4l2_tuner *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001856
Hans Verkuila3998102008-07-21 02:57:38 -03001857 if (!ops->vidioc_g_tuner)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001858 break;
1859
Hans Verkuil227690d2011-06-12 06:36:41 -03001860 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1861 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001862 ret = ops->vidioc_g_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001863 if (!ret)
1864 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1865 "capability=0x%x, rangelow=%d, "
1866 "rangehigh=%d, signal=%d, afc=%d, "
1867 "rxsubchans=0x%x, audmode=%d\n",
1868 p->index, p->name, p->type,
1869 p->capability, p->rangelow,
1870 p->rangehigh, p->signal, p->afc,
1871 p->rxsubchans, p->audmode);
1872 break;
1873 }
1874 case VIDIOC_S_TUNER:
1875 {
1876 struct v4l2_tuner *p = arg;
1877
Hans Verkuila3998102008-07-21 02:57:38 -03001878 if (!ops->vidioc_s_tuner)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001879 break;
Hans Verkuil227690d2011-06-12 06:36:41 -03001880 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1881 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001882 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1883 "capability=0x%x, rangelow=%d, "
1884 "rangehigh=%d, signal=%d, afc=%d, "
1885 "rxsubchans=0x%x, audmode=%d\n",
1886 p->index, p->name, p->type,
1887 p->capability, p->rangelow,
1888 p->rangehigh, p->signal, p->afc,
1889 p->rxsubchans, p->audmode);
Hans Verkuila3998102008-07-21 02:57:38 -03001890 ret = ops->vidioc_s_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001891 break;
1892 }
1893 case VIDIOC_G_FREQUENCY:
1894 {
1895 struct v4l2_frequency *p = arg;
1896
Hans Verkuila3998102008-07-21 02:57:38 -03001897 if (!ops->vidioc_g_frequency)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001898 break;
1899
Hans Verkuil227690d2011-06-12 06:36:41 -03001900 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1901 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001902 ret = ops->vidioc_g_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001903 if (!ret)
1904 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1905 p->tuner, p->type, p->frequency);
1906 break;
1907 }
1908 case VIDIOC_S_FREQUENCY:
1909 {
1910 struct v4l2_frequency *p = arg;
1911
Hans Verkuila3998102008-07-21 02:57:38 -03001912 if (!ops->vidioc_s_frequency)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001913 break;
1914 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1915 p->tuner, p->type, p->frequency);
Hans Verkuila3998102008-07-21 02:57:38 -03001916 ret = ops->vidioc_s_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001917 break;
1918 }
1919 case VIDIOC_G_SLICED_VBI_CAP:
1920 {
1921 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001922
Hans Verkuila3998102008-07-21 02:57:38 -03001923 if (!ops->vidioc_g_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001924 break;
Trent Piepho19c96e42009-03-04 01:21:02 -03001925
1926 /* Clear up to type, everything after type is zerod already */
1927 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1928
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001929 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001930 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001931 if (!ret)
1932 dbgarg2("service_set=%d\n", p->service_set);
1933 break;
1934 }
1935 case VIDIOC_LOG_STATUS:
1936 {
Hans Verkuila3998102008-07-21 02:57:38 -03001937 if (!ops->vidioc_log_status)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001938 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001939 ret = ops->vidioc_log_status(file, fh);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001940 break;
1941 }
1942#ifdef CONFIG_VIDEO_ADV_DEBUG
1943 case VIDIOC_DBG_G_REGISTER:
1944 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001945 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001946
Hans Verkuila1198cc2011-01-08 09:53:32 -03001947 if (ops->vidioc_g_register) {
1948 if (!capable(CAP_SYS_ADMIN))
1949 ret = -EPERM;
1950 else
1951 ret = ops->vidioc_g_register(file, fh, p);
1952 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001953 break;
1954 }
1955 case VIDIOC_DBG_S_REGISTER:
1956 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001957 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001958
Hans Verkuila1198cc2011-01-08 09:53:32 -03001959 if (ops->vidioc_s_register) {
1960 if (!capable(CAP_SYS_ADMIN))
1961 ret = -EPERM;
1962 else
1963 ret = ops->vidioc_s_register(file, fh, p);
1964 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001965 break;
1966 }
1967#endif
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001968 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001969 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001970 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001971
Hans Verkuila3998102008-07-21 02:57:38 -03001972 if (!ops->vidioc_g_chip_ident)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001973 break;
Hans Verkuil80b36e02009-02-07 11:00:02 -03001974 p->ident = V4L2_IDENT_NONE;
1975 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001976 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001977 if (!ret)
1978 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1979 break;
1980 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001981 case VIDIOC_S_HW_FREQ_SEEK:
1982 {
1983 struct v4l2_hw_freq_seek *p = arg;
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001984 enum v4l2_tuner_type type;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001985
Hans Verkuila3998102008-07-21 02:57:38 -03001986 if (!ops->vidioc_s_hw_freq_seek)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001987 break;
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001988 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1989 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001990 dbgarg(cmd,
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001991 "tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
1992 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
1993 if (p->type != type)
1994 ret = -EINVAL;
1995 else
1996 ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
Hans Verkuila3998102008-07-21 02:57:38 -03001997 break;
1998 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001999 case VIDIOC_ENUM_FRAMESIZES:
2000 {
2001 struct v4l2_frmsizeenum *p = arg;
2002
2003 if (!ops->vidioc_enum_framesizes)
2004 break;
2005
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002006 ret = ops->vidioc_enum_framesizes(file, fh, p);
2007 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03002008 "index=%d, pixelformat=%c%c%c%c, type=%d ",
2009 p->index,
2010 (p->pixel_format & 0xff),
2011 (p->pixel_format >> 8) & 0xff,
2012 (p->pixel_format >> 16) & 0xff,
2013 (p->pixel_format >> 24) & 0xff,
2014 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002015 switch (p->type) {
2016 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03002017 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002018 p->discrete.width, p->discrete.height);
2019 break;
2020 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03002021 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002022 p->stepwise.min_width, p->stepwise.min_height,
2023 p->stepwise.step_width, p->stepwise.step_height,
2024 p->stepwise.max_width, p->stepwise.max_height);
2025 break;
2026 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03002027 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002028 break;
2029 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03002030 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002031 }
2032
2033 break;
2034 }
2035 case VIDIOC_ENUM_FRAMEINTERVALS:
2036 {
2037 struct v4l2_frmivalenum *p = arg;
2038
2039 if (!ops->vidioc_enum_frameintervals)
2040 break;
2041
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002042 ret = ops->vidioc_enum_frameintervals(file, fh, p);
2043 dbgarg(cmd,
2044 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
2045 p->index, p->pixel_format,
2046 p->width, p->height, p->type);
2047 switch (p->type) {
2048 case V4L2_FRMIVAL_TYPE_DISCRETE:
2049 dbgarg2("fps=%d/%d\n",
2050 p->discrete.numerator,
2051 p->discrete.denominator);
2052 break;
2053 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03002054 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
2055 p->stepwise.min.numerator,
2056 p->stepwise.min.denominator,
2057 p->stepwise.max.numerator,
2058 p->stepwise.max.denominator,
2059 p->stepwise.step.numerator,
2060 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002061 break;
2062 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2063 dbgarg2("continuous\n");
2064 break;
2065 default:
2066 dbgarg2("- Unknown type!\n");
2067 }
2068 break;
2069 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002070 case VIDIOC_ENUM_DV_PRESETS:
2071 {
2072 struct v4l2_dv_enum_preset *p = arg;
2073
2074 if (!ops->vidioc_enum_dv_presets)
2075 break;
2076
2077 ret = ops->vidioc_enum_dv_presets(file, fh, p);
2078 if (!ret)
2079 dbgarg(cmd,
2080 "index=%d, preset=%d, name=%s, width=%d,"
2081 " height=%d ",
2082 p->index, p->preset, p->name, p->width,
2083 p->height);
2084 break;
2085 }
2086 case VIDIOC_S_DV_PRESET:
2087 {
2088 struct v4l2_dv_preset *p = arg;
2089
2090 if (!ops->vidioc_s_dv_preset)
2091 break;
2092
2093 dbgarg(cmd, "preset=%d\n", p->preset);
2094 ret = ops->vidioc_s_dv_preset(file, fh, p);
2095 break;
2096 }
2097 case VIDIOC_G_DV_PRESET:
2098 {
2099 struct v4l2_dv_preset *p = arg;
2100
2101 if (!ops->vidioc_g_dv_preset)
2102 break;
2103
2104 ret = ops->vidioc_g_dv_preset(file, fh, p);
2105 if (!ret)
2106 dbgarg(cmd, "preset=%d\n", p->preset);
2107 break;
2108 }
2109 case VIDIOC_QUERY_DV_PRESET:
2110 {
2111 struct v4l2_dv_preset *p = arg;
2112
2113 if (!ops->vidioc_query_dv_preset)
2114 break;
2115
2116 ret = ops->vidioc_query_dv_preset(file, fh, p);
2117 if (!ret)
2118 dbgarg(cmd, "preset=%d\n", p->preset);
2119 break;
2120 }
2121 case VIDIOC_S_DV_TIMINGS:
2122 {
2123 struct v4l2_dv_timings *p = arg;
2124
2125 if (!ops->vidioc_s_dv_timings)
2126 break;
2127
2128 switch (p->type) {
2129 case V4L2_DV_BT_656_1120:
2130 dbgarg2("bt-656/1120:interlaced=%d, pixelclock=%lld,"
2131 " width=%d, height=%d, polarities=%x,"
2132 " hfrontporch=%d, hsync=%d, hbackporch=%d,"
2133 " vfrontporch=%d, vsync=%d, vbackporch=%d,"
2134 " il_vfrontporch=%d, il_vsync=%d,"
2135 " il_vbackporch=%d\n",
2136 p->bt.interlaced, p->bt.pixelclock,
2137 p->bt.width, p->bt.height, p->bt.polarities,
2138 p->bt.hfrontporch, p->bt.hsync,
2139 p->bt.hbackporch, p->bt.vfrontporch,
2140 p->bt.vsync, p->bt.vbackporch,
2141 p->bt.il_vfrontporch, p->bt.il_vsync,
2142 p->bt.il_vbackporch);
2143 ret = ops->vidioc_s_dv_timings(file, fh, p);
2144 break;
2145 default:
2146 dbgarg2("Unknown type %d!\n", p->type);
2147 break;
2148 }
2149 break;
2150 }
2151 case VIDIOC_G_DV_TIMINGS:
2152 {
2153 struct v4l2_dv_timings *p = arg;
2154
2155 if (!ops->vidioc_g_dv_timings)
2156 break;
2157
2158 ret = ops->vidioc_g_dv_timings(file, fh, p);
2159 if (!ret) {
2160 switch (p->type) {
2161 case V4L2_DV_BT_656_1120:
2162 dbgarg2("bt-656/1120:interlaced=%d,"
2163 " pixelclock=%lld,"
2164 " width=%d, height=%d, polarities=%x,"
2165 " hfrontporch=%d, hsync=%d,"
2166 " hbackporch=%d, vfrontporch=%d,"
2167 " vsync=%d, vbackporch=%d,"
2168 " il_vfrontporch=%d, il_vsync=%d,"
2169 " il_vbackporch=%d\n",
2170 p->bt.interlaced, p->bt.pixelclock,
2171 p->bt.width, p->bt.height,
2172 p->bt.polarities, p->bt.hfrontporch,
2173 p->bt.hsync, p->bt.hbackporch,
2174 p->bt.vfrontporch, p->bt.vsync,
2175 p->bt.vbackporch, p->bt.il_vfrontporch,
2176 p->bt.il_vsync, p->bt.il_vbackporch);
2177 break;
2178 default:
2179 dbgarg2("Unknown type %d!\n", p->type);
2180 break;
2181 }
2182 }
2183 break;
2184 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002185 case VIDIOC_DQEVENT:
2186 {
2187 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002188
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002189 if (!ops->vidioc_subscribe_event)
2190 break;
2191
2192 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2193 if (ret < 0) {
2194 dbgarg(cmd, "no pending events?");
2195 break;
2196 }
2197 dbgarg(cmd,
2198 "pending=%d, type=0x%8.8x, sequence=%d, "
2199 "timestamp=%lu.%9.9lu ",
2200 ev->pending, ev->type, ev->sequence,
2201 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2202 break;
2203 }
2204 case VIDIOC_SUBSCRIBE_EVENT:
2205 {
2206 struct v4l2_event_subscription *sub = arg;
2207
2208 if (!ops->vidioc_subscribe_event)
2209 break;
2210
2211 ret = ops->vidioc_subscribe_event(fh, sub);
2212 if (ret < 0) {
2213 dbgarg(cmd, "failed, ret=%ld", ret);
2214 break;
2215 }
2216 dbgarg(cmd, "type=0x%8.8x", sub->type);
2217 break;
2218 }
2219 case VIDIOC_UNSUBSCRIBE_EVENT:
2220 {
2221 struct v4l2_event_subscription *sub = arg;
2222
2223 if (!ops->vidioc_unsubscribe_event)
2224 break;
2225
2226 ret = ops->vidioc_unsubscribe_event(fh, sub);
2227 if (ret < 0) {
2228 dbgarg(cmd, "failed, ret=%ld", ret);
2229 break;
2230 }
2231 dbgarg(cmd, "type=0x%8.8x", sub->type);
2232 break;
2233 }
Hans Verkuila3998102008-07-21 02:57:38 -03002234 default:
2235 {
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002236 bool valid_prio = true;
2237
Hans Verkuila3998102008-07-21 02:57:38 -03002238 if (!ops->vidioc_default)
2239 break;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002240 if (use_fh_prio)
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002241 valid_prio = v4l2_prio_check(vfd->prio, vfh->prio) >= 0;
2242 ret = ops->vidioc_default(file, fh, valid_prio, cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002243 break;
2244 }
2245 } /* switch */
2246
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002247exit_prio:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002248 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
2249 if (ret < 0) {
2250 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil069b7472008-12-30 07:04:34 -03002251 printk(KERN_CONT " error %ld\n", ret);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002252 }
2253 }
2254
2255 return ret;
2256}
2257
Trent Piepho19c96e42009-03-04 01:21:02 -03002258/* In some cases, only a few fields are used as input, i.e. when the app sets
2259 * "index" and then the driver fills in the rest of the structure for the thing
2260 * with that index. We only need to copy up the first non-input field. */
2261static unsigned long cmd_input_size(unsigned int cmd)
2262{
2263 /* Size of structure up to and including 'field' */
Hans Verkuil9f1a6932009-03-08 10:35:23 -03002264#define CMDINSIZE(cmd, type, field) \
2265 case VIDIOC_##cmd: \
2266 return offsetof(struct v4l2_##type, field) + \
2267 sizeof(((struct v4l2_##type *)0)->field);
Trent Piepho19c96e42009-03-04 01:21:02 -03002268
Hans Verkuil9f1a6932009-03-08 10:35:23 -03002269 switch (cmd) {
Trent Piepho19c96e42009-03-04 01:21:02 -03002270 CMDINSIZE(ENUM_FMT, fmtdesc, type);
2271 CMDINSIZE(G_FMT, format, type);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002272 CMDINSIZE(QUERYBUF, buffer, length);
Trent Piepho19c96e42009-03-04 01:21:02 -03002273 CMDINSIZE(G_PARM, streamparm, type);
2274 CMDINSIZE(ENUMSTD, standard, index);
2275 CMDINSIZE(ENUMINPUT, input, index);
2276 CMDINSIZE(G_CTRL, control, id);
2277 CMDINSIZE(G_TUNER, tuner, index);
2278 CMDINSIZE(QUERYCTRL, queryctrl, id);
2279 CMDINSIZE(QUERYMENU, querymenu, index);
2280 CMDINSIZE(ENUMOUTPUT, output, index);
2281 CMDINSIZE(G_MODULATOR, modulator, index);
2282 CMDINSIZE(G_FREQUENCY, frequency, tuner);
2283 CMDINSIZE(CROPCAP, cropcap, type);
2284 CMDINSIZE(G_CROP, crop, type);
2285 CMDINSIZE(ENUMAUDIO, audio, index);
2286 CMDINSIZE(ENUMAUDOUT, audioout, index);
2287 CMDINSIZE(ENCODER_CMD, encoder_cmd, flags);
2288 CMDINSIZE(TRY_ENCODER_CMD, encoder_cmd, flags);
2289 CMDINSIZE(G_SLICED_VBI_CAP, sliced_vbi_cap, type);
2290 CMDINSIZE(ENUM_FRAMESIZES, frmsizeenum, pixel_format);
2291 CMDINSIZE(ENUM_FRAMEINTERVALS, frmivalenum, height);
2292 default:
2293 return _IOC_SIZE(cmd);
2294 }
2295}
2296
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002297static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2298 void * __user *user_ptr, void ***kernel_ptr)
2299{
2300 int ret = 0;
2301
2302 switch (cmd) {
2303 case VIDIOC_QUERYBUF:
2304 case VIDIOC_QBUF:
2305 case VIDIOC_DQBUF: {
2306 struct v4l2_buffer *buf = parg;
2307
2308 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2309 if (buf->length > VIDEO_MAX_PLANES) {
2310 ret = -EINVAL;
2311 break;
2312 }
2313 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002314 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002315 *array_size = sizeof(struct v4l2_plane) * buf->length;
2316 ret = 1;
2317 }
2318 break;
2319 }
2320
2321 case VIDIOC_S_EXT_CTRLS:
2322 case VIDIOC_G_EXT_CTRLS:
2323 case VIDIOC_TRY_EXT_CTRLS: {
2324 struct v4l2_ext_controls *ctrls = parg;
2325
2326 if (ctrls->count != 0) {
2327 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002328 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002329 *array_size = sizeof(struct v4l2_ext_control)
2330 * ctrls->count;
2331 ret = 1;
2332 }
2333 break;
2334 }
2335 }
2336
2337 return ret;
2338}
2339
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002340long
2341video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2342 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002343{
2344 char sbuf[128];
2345 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002346 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002347 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002348 bool has_array_args;
2349 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002350 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002351 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002352
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002353 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002354 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002355 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2356 parg = sbuf;
2357 } else {
2358 /* too big to allocate from stack */
2359 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2360 if (NULL == mbuf)
2361 return -ENOMEM;
2362 parg = mbuf;
2363 }
2364
2365 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002366 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2367 unsigned long n = cmd_input_size(cmd);
2368
2369 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002370 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002371
2372 /* zero out anything we don't copy from userspace */
2373 if (n < _IOC_SIZE(cmd))
2374 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002375 } else {
2376 /* read-only ioctl */
2377 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002378 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002379 }
2380
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002381 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2382 if (err < 0)
2383 goto out;
2384 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002385
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002386 if (has_array_args) {
2387 /*
2388 * When adding new types of array args, make sure that the
2389 * parent argument to ioctl (which contains the pointer to the
2390 * array) fits into sbuf (so that mbuf will still remain
2391 * unused up to here).
2392 */
2393 mbuf = kmalloc(array_size, GFP_KERNEL);
2394 err = -ENOMEM;
2395 if (NULL == mbuf)
2396 goto out_array_args;
2397 err = -EFAULT;
2398 if (copy_from_user(mbuf, user_ptr, array_size))
2399 goto out_array_args;
2400 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002401 }
2402
2403 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002404 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002405 if (err == -ENOIOCTLCMD)
2406 err = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002407
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002408 if (has_array_args) {
2409 *kernel_ptr = user_ptr;
2410 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002411 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002412 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002413 }
2414 if (err < 0)
2415 goto out;
2416
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002417out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002418 /* Copy results into user buffer */
2419 switch (_IOC_DIR(cmd)) {
2420 case _IOC_READ:
2421 case (_IOC_WRITE | _IOC_READ):
2422 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2423 err = -EFAULT;
2424 break;
2425 }
2426
2427out:
2428 kfree(mbuf);
2429 return err;
2430}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002431EXPORT_SYMBOL(video_usercopy);
2432
2433long video_ioctl2(struct file *file,
2434 unsigned int cmd, unsigned long arg)
2435{
2436 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2437}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002438EXPORT_SYMBOL(video_ioctl2);