blob: f45c625a92f453a8d5a47973e895bdcab841f78b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03002 * Video capture interface for Linux version 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03004 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030012 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
17 */
18
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030019#define dbgarg(cmd, fmt, arg...) \
Enrico Scholz474ce782006-10-09 16:27:05 -030020 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) { \
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030021 printk (KERN_DEBUG "%s: ", vfd->name); \
22 v4l_printk_ioctl(cmd); \
Enrico Scholz474ce782006-10-09 16:27:05 -030023 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg); \
24 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030025
26#define dbgarg2(fmt, arg...) \
27 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
28 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/mm.h>
34#include <linux/string.h>
35#include <linux/errno.h>
36#include <linux/init.h>
37#include <linux/kmod.h>
38#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030042#define __OLD_VIDIOC_ /* To allow fixing old calls*/
43#include <linux/videodev2.h>
44
45#ifdef CONFIG_VIDEO_V4L1
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/videodev.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030047#endif
48#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define VIDEO_NUM_DEVICES 256
51#define VIDEO_NAME "video4linux"
52
53/*
54 * sysfs stuff
55 */
56
57static ssize_t show_name(struct class_device *cd, char *buf)
58{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030059 struct video_device *vfd = container_of(cd, struct video_device,
60 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
62}
63
64static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
65
66struct video_device *video_device_alloc(void)
67{
68 struct video_device *vfd;
69
Panagiotis Issaris74081872006-01-11 19:40:56 -020070 vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return vfd;
72}
73
74void video_device_release(struct video_device *vfd)
75{
76 kfree(vfd);
77}
78
79static void video_release(struct class_device *cd)
80{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030081 struct video_device *vfd = container_of(cd, struct video_device,
82 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -020084#if 1
Michael Krufky50c25ff2006-01-09 15:25:34 -020085 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!vfd->release)
87 return;
88#endif
89 vfd->release(vfd);
90}
91
92static struct class video_class = {
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080093 .name = VIDEO_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .release = video_release,
95};
96
97/*
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080098 * Active devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct video_device *video_device[VIDEO_NUM_DEVICES];
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200102static DEFINE_MUTEX(videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104struct video_device* video_devdata(struct file *file)
105{
Josef Sipek723731b2006-12-08 02:37:47 -0800106 return video_device[iminor(file->f_path.dentry->d_inode)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300110 * Open a video device - FIXME: Obsoleted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112static int video_open(struct inode *inode, struct file *file)
113{
114 unsigned int minor = iminor(inode);
115 int err = 0;
116 struct video_device *vfl;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800117 const struct file_operations *old_fops;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if(minor>=VIDEO_NUM_DEVICES)
120 return -ENODEV;
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200121 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 vfl=video_device[minor];
123 if(vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200124 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200126 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 vfl=video_device[minor];
128 if (vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200129 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -ENODEV;
131 }
132 }
133 old_fops = file->f_op;
134 file->f_op = fops_get(vfl->fops);
135 if(file->f_op->open)
136 err = file->f_op->open(inode,file);
137 if (err) {
138 fops_put(file->f_op);
139 file->f_op = fops_get(old_fops);
140 }
141 fops_put(old_fops);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200142 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return err;
144}
145
146/*
147 * helper function -- handles userspace copying for ioctl arguments
148 */
149
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300150#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static unsigned int
152video_fix_command(unsigned int cmd)
153{
154 switch (cmd) {
155 case VIDIOC_OVERLAY_OLD:
156 cmd = VIDIOC_OVERLAY;
157 break;
158 case VIDIOC_S_PARM_OLD:
159 cmd = VIDIOC_S_PARM;
160 break;
161 case VIDIOC_S_CTRL_OLD:
162 cmd = VIDIOC_S_CTRL;
163 break;
164 case VIDIOC_G_AUDIO_OLD:
165 cmd = VIDIOC_G_AUDIO;
166 break;
167 case VIDIOC_G_AUDOUT_OLD:
168 cmd = VIDIOC_G_AUDOUT;
169 break;
170 case VIDIOC_CROPCAP_OLD:
171 cmd = VIDIOC_CROPCAP;
172 break;
173 }
174 return cmd;
175}
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300178/*
179 * Obsolete usercopy function - Should be removed soon
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181int
182video_usercopy(struct inode *inode, struct file *file,
183 unsigned int cmd, unsigned long arg,
184 int (*func)(struct inode *inode, struct file *file,
185 unsigned int cmd, void *arg))
186{
187 char sbuf[128];
188 void *mbuf = NULL;
189 void *parg = NULL;
190 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300191 int is_ext_ctrl;
192 size_t ctrls_size = 0;
193 void __user *user_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300195#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 cmd = video_fix_command(cmd);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300197#endif
Hans Verkuil05976912006-06-18 13:43:28 -0300198 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
199 cmd == VIDIOC_TRY_EXT_CTRLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* Copy arguments into temp kernel buffer */
202 switch (_IOC_DIR(cmd)) {
203 case _IOC_NONE:
204 parg = NULL;
205 break;
206 case _IOC_READ:
207 case _IOC_WRITE:
208 case (_IOC_WRITE | _IOC_READ):
209 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
210 parg = sbuf;
211 } else {
212 /* too big to allocate from stack */
213 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
214 if (NULL == mbuf)
215 return -ENOMEM;
216 parg = mbuf;
217 }
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 err = -EFAULT;
220 if (_IOC_DIR(cmd) & _IOC_WRITE)
Hans Verkuil05976912006-06-18 13:43:28 -0300221 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto out;
223 break;
224 }
Hans Verkuil05976912006-06-18 13:43:28 -0300225 if (is_ext_ctrl) {
226 struct v4l2_ext_controls *p = parg;
227
228 /* In case of an error, tell the caller that it wasn't
229 a specific control that caused it. */
230 p->error_idx = p->count;
231 user_ptr = (void __user *)p->controls;
232 if (p->count) {
233 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
234 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
235 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
236 err = -ENOMEM;
237 if (NULL == mbuf)
238 goto out_ext_ctrl;
239 err = -EFAULT;
240 if (copy_from_user(mbuf, user_ptr, ctrls_size))
241 goto out_ext_ctrl;
242 p->controls = mbuf;
243 }
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* call driver */
247 err = func(inode, file, cmd, parg);
248 if (err == -ENOIOCTLCMD)
249 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300250 if (is_ext_ctrl) {
251 struct v4l2_ext_controls *p = parg;
252
253 p->controls = (void *)user_ptr;
254 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
255 err = -EFAULT;
256 goto out_ext_ctrl;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (err < 0)
259 goto out;
260
Hans Verkuil05976912006-06-18 13:43:28 -0300261out_ext_ctrl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* Copy results into user buffer */
263 switch (_IOC_DIR(cmd))
264 {
265 case _IOC_READ:
266 case (_IOC_WRITE | _IOC_READ):
267 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
268 err = -EFAULT;
269 break;
270 }
271
272out:
Jesper Juhl2ea75332005-11-07 01:01:31 -0800273 kfree(mbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return err;
275}
276
277/*
278 * open/release helper functions -- handle exclusive opens
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300279 * Should be removed soon
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281int video_exclusive_open(struct inode *inode, struct file *file)
282{
283 struct video_device *vfl = video_devdata(file);
284 int retval = 0;
285
Ingo Molnar3593cab2006-02-07 06:49:14 -0200286 mutex_lock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (vfl->users) {
288 retval = -EBUSY;
289 } else {
290 vfl->users++;
291 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200292 mutex_unlock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return retval;
294}
295
296int video_exclusive_release(struct inode *inode, struct file *file)
297{
298 struct video_device *vfl = video_devdata(file);
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 vfl->users--;
301 return 0;
302}
303
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300304static char *v4l2_memory_names[] = {
305 [V4L2_MEMORY_MMAP] = "mmap",
306 [V4L2_MEMORY_USERPTR] = "userptr",
307 [V4L2_MEMORY_OVERLAY] = "overlay",
308};
309
310
311/* FIXME: Those stuff are replicated also on v4l2-common.c */
312static char *v4l2_type_names_FIXME[] = {
313 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
314 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
315 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
316 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
317 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
318 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
319 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
Hans Verkuilb2787842007-04-27 12:31:02 -0300320 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300321 [V4L2_BUF_TYPE_PRIVATE] = "private",
322};
323
324static char *v4l2_field_names_FIXME[] = {
325 [V4L2_FIELD_ANY] = "any",
326 [V4L2_FIELD_NONE] = "none",
327 [V4L2_FIELD_TOP] = "top",
328 [V4L2_FIELD_BOTTOM] = "bottom",
329 [V4L2_FIELD_INTERLACED] = "interlaced",
330 [V4L2_FIELD_SEQ_TB] = "seq-tb",
331 [V4L2_FIELD_SEQ_BT] = "seq-bt",
332 [V4L2_FIELD_ALTERNATE] = "alternate",
Hans Verkuilb2787842007-04-27 12:31:02 -0300333 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
334 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300335};
336
337#define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
338
339static void dbgbuf(unsigned int cmd, struct video_device *vfd,
340 struct v4l2_buffer *p)
341{
342 struct v4l2_timecode *tc=&p->timecode;
343
344 dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
345 "bytesused=%d, flags=0x%08d, "
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300346 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300347 (p->timestamp.tv_sec/3600),
348 (int)(p->timestamp.tv_sec/60)%60,
349 (int)(p->timestamp.tv_sec%60),
350 p->timestamp.tv_usec,
351 p->index,
352 prt_names(p->type,v4l2_type_names_FIXME),
353 p->bytesused,p->flags,
354 p->field,p->sequence,
355 prt_names(p->memory,v4l2_memory_names),
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300356 p->m.userptr, p->length);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300357 dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
358 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
359 tc->hours,tc->minutes,tc->seconds,
Mauro Carvalho Chehabc18cb012006-06-23 07:05:22 -0300360 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300361}
362
363static inline void dbgrect(struct video_device *vfd, char *s,
364 struct v4l2_rect *r)
365{
366 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
367 r->width, r->height);
368};
369
370static inline void v4l_print_pix_fmt (struct video_device *vfd,
371 struct v4l2_pix_format *fmt)
372{
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300373 dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300374 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300375 fmt->width,fmt->height,
376 (fmt->pixelformat & 0xff),
377 (fmt->pixelformat >> 8) & 0xff,
378 (fmt->pixelformat >> 16) & 0xff,
379 (fmt->pixelformat >> 24) & 0xff,
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300380 prt_names(fmt->field,v4l2_field_names_FIXME),
381 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
382};
383
384
385static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
386{
387 switch (type) {
388 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
389 if (vfd->vidioc_try_fmt_cap)
390 return (0);
391 break;
392 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
393 if (vfd->vidioc_try_fmt_overlay)
394 return (0);
395 break;
396 case V4L2_BUF_TYPE_VBI_CAPTURE:
397 if (vfd->vidioc_try_fmt_vbi)
398 return (0);
399 break;
400 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
401 if (vfd->vidioc_try_fmt_vbi_output)
402 return (0);
403 break;
404 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
405 if (vfd->vidioc_try_fmt_vbi_capture)
406 return (0);
407 break;
408 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
409 if (vfd->vidioc_try_fmt_video_output)
410 return (0);
411 break;
412 case V4L2_BUF_TYPE_VBI_OUTPUT:
413 if (vfd->vidioc_try_fmt_vbi_output)
414 return (0);
415 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300416 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
417 if (vfd->vidioc_try_fmt_output_overlay)
418 return (0);
419 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300420 case V4L2_BUF_TYPE_PRIVATE:
421 if (vfd->vidioc_try_fmt_type_private)
422 return (0);
423 break;
424 }
425 return (-EINVAL);
426}
427
428static int __video_do_ioctl(struct inode *inode, struct file *file,
429 unsigned int cmd, void *arg)
430{
431 struct video_device *vfd = video_devdata(file);
432 void *fh = file->private_data;
433 int ret = -EINVAL;
434
435 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
436 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
437 v4l_print_ioctl(vfd->name, cmd);
438 }
439
Sam Revitch1088b132007-05-01 08:46:30 -0300440#ifdef CONFIG_VIDEO_V4L1_COMPAT
441 /* --- streaming capture ------------------------------------- */
442 if (cmd == VIDIOCGMBUF) {
443 struct video_mbuf *p=arg;
444
445 memset(p,0,sizeof(p));
446
447 if (!vfd->vidiocgmbuf)
448 return ret;
449 ret=vfd->vidiocgmbuf(file, fh, p);
450 if (!ret)
451 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
452 p->size, p->frames,
453 (unsigned long)p->offsets);
454 return ret;
455 }
456#endif
457
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -0300458 if (_IOC_TYPE(cmd)=='v')
459 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
460 __video_do_ioctl);
461
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300462 switch(cmd) {
463 /* --- capabilities ------------------------------------------ */
464 case VIDIOC_QUERYCAP:
465 {
466 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
467 memset(cap, 0, sizeof(*cap));
468
469 if (!vfd->vidioc_querycap)
470 break;
471
472 ret=vfd->vidioc_querycap(file, fh, cap);
473 if (!ret)
474 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
475 "version=0x%08x, "
476 "capabilities=0x%08x\n",
477 cap->driver,cap->card,cap->bus_info,
478 cap->version,
479 cap->capabilities);
480 break;
481 }
482
483 /* --- priority ------------------------------------------ */
484 case VIDIOC_G_PRIORITY:
485 {
486 enum v4l2_priority *p=arg;
487
488 if (!vfd->vidioc_g_priority)
489 break;
490 ret=vfd->vidioc_g_priority(file, fh, p);
491 if (!ret)
492 dbgarg(cmd, "priority is %d\n", *p);
493 break;
494 }
495 case VIDIOC_S_PRIORITY:
496 {
497 enum v4l2_priority *p=arg;
498
499 if (!vfd->vidioc_s_priority)
500 break;
501 dbgarg(cmd, "setting priority to %d\n", *p);
502 ret=vfd->vidioc_s_priority(file, fh, *p);
503 break;
504 }
505
506 /* --- capture ioctls ---------------------------------------- */
507 case VIDIOC_ENUM_FMT:
508 {
509 struct v4l2_fmtdesc *f = arg;
510 enum v4l2_buf_type type;
511 unsigned int index;
512
513 index = f->index;
514 type = f->type;
515 memset(f,0,sizeof(*f));
516 f->index = index;
517 f->type = type;
518
519 switch (type) {
520 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
521 if (vfd->vidioc_enum_fmt_cap)
522 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
523 break;
524 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
525 if (vfd->vidioc_enum_fmt_overlay)
526 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
527 break;
528 case V4L2_BUF_TYPE_VBI_CAPTURE:
529 if (vfd->vidioc_enum_fmt_vbi)
530 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
531 break;
532 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
533 if (vfd->vidioc_enum_fmt_vbi_output)
534 ret=vfd->vidioc_enum_fmt_vbi_output(file,
535 fh, f);
536 break;
537 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
538 if (vfd->vidioc_enum_fmt_vbi_capture)
539 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
540 fh, f);
541 break;
542 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
543 if (vfd->vidioc_enum_fmt_video_output)
544 ret=vfd->vidioc_enum_fmt_video_output(file,
545 fh, f);
546 break;
547 case V4L2_BUF_TYPE_VBI_OUTPUT:
548 if (vfd->vidioc_enum_fmt_vbi_output)
549 ret=vfd->vidioc_enum_fmt_vbi_output(file,
550 fh, f);
551 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300552 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
553 if (vfd->vidioc_enum_fmt_output_overlay)
554 ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
555 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300556 case V4L2_BUF_TYPE_PRIVATE:
557 if (vfd->vidioc_enum_fmt_type_private)
558 ret=vfd->vidioc_enum_fmt_type_private(file,
559 fh, f);
560 break;
561 }
562 if (!ret)
563 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300564 "pixelformat=%c%c%c%c, description='%s'\n",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300565 f->index, f->type, f->flags,
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300566 (f->pixelformat & 0xff),
567 (f->pixelformat >> 8) & 0xff,
568 (f->pixelformat >> 16) & 0xff,
569 (f->pixelformat >> 24) & 0xff,
570 f->description);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300571 break;
572 }
573 case VIDIOC_G_FMT:
574 {
575 struct v4l2_format *f = (struct v4l2_format *)arg;
576 enum v4l2_buf_type type=f->type;
577
578 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
579 f->type=type;
580
581 /* FIXME: Should be one dump per type */
582 dbgarg (cmd, "type=%s\n", prt_names(type,
583 v4l2_type_names_FIXME));
584
585 switch (type) {
586 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
587 if (vfd->vidioc_g_fmt_cap)
588 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
589 if (!ret)
590 v4l_print_pix_fmt(vfd,&f->fmt.pix);
591 break;
592 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
593 if (vfd->vidioc_g_fmt_overlay)
594 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
595 break;
596 case V4L2_BUF_TYPE_VBI_CAPTURE:
597 if (vfd->vidioc_g_fmt_vbi)
598 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
599 break;
600 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
601 if (vfd->vidioc_g_fmt_vbi_output)
602 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
603 break;
604 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
605 if (vfd->vidioc_g_fmt_vbi_capture)
606 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
607 break;
608 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
609 if (vfd->vidioc_g_fmt_video_output)
610 ret=vfd->vidioc_g_fmt_video_output(file,
611 fh, f);
612 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300613 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
614 if (vfd->vidioc_g_fmt_output_overlay)
615 ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
616 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300617 case V4L2_BUF_TYPE_VBI_OUTPUT:
618 if (vfd->vidioc_g_fmt_vbi_output)
619 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
620 break;
621 case V4L2_BUF_TYPE_PRIVATE:
622 if (vfd->vidioc_g_fmt_type_private)
623 ret=vfd->vidioc_g_fmt_type_private(file,
624 fh, f);
625 break;
626 }
627
628 break;
629 }
630 case VIDIOC_S_FMT:
631 {
632 struct v4l2_format *f = (struct v4l2_format *)arg;
633
634 /* FIXME: Should be one dump per type */
635 dbgarg (cmd, "type=%s\n", prt_names(f->type,
636 v4l2_type_names_FIXME));
637
638 switch (f->type) {
639 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
640 v4l_print_pix_fmt(vfd,&f->fmt.pix);
641 if (vfd->vidioc_s_fmt_cap)
642 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
643 break;
644 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
645 if (vfd->vidioc_s_fmt_overlay)
646 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
647 break;
648 case V4L2_BUF_TYPE_VBI_CAPTURE:
649 if (vfd->vidioc_s_fmt_vbi)
650 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
651 break;
652 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
653 if (vfd->vidioc_s_fmt_vbi_output)
654 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
655 break;
656 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
657 if (vfd->vidioc_s_fmt_vbi_capture)
658 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
659 break;
660 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
661 if (vfd->vidioc_s_fmt_video_output)
662 ret=vfd->vidioc_s_fmt_video_output(file,
663 fh, f);
664 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300665 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
666 if (vfd->vidioc_s_fmt_output_overlay)
667 ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
668 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300669 case V4L2_BUF_TYPE_VBI_OUTPUT:
670 if (vfd->vidioc_s_fmt_vbi_output)
671 ret=vfd->vidioc_s_fmt_vbi_output(file,
672 fh, f);
673 break;
674 case V4L2_BUF_TYPE_PRIVATE:
675 if (vfd->vidioc_s_fmt_type_private)
676 ret=vfd->vidioc_s_fmt_type_private(file,
677 fh, f);
678 break;
679 }
680 break;
681 }
682 case VIDIOC_TRY_FMT:
683 {
684 struct v4l2_format *f = (struct v4l2_format *)arg;
685
686 /* FIXME: Should be one dump per type */
687 dbgarg (cmd, "type=%s\n", prt_names(f->type,
688 v4l2_type_names_FIXME));
689 switch (f->type) {
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 if (vfd->vidioc_try_fmt_cap)
692 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
693 if (!ret)
694 v4l_print_pix_fmt(vfd,&f->fmt.pix);
695 break;
696 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
697 if (vfd->vidioc_try_fmt_overlay)
698 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
699 break;
700 case V4L2_BUF_TYPE_VBI_CAPTURE:
701 if (vfd->vidioc_try_fmt_vbi)
702 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
703 break;
704 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
705 if (vfd->vidioc_try_fmt_vbi_output)
706 ret=vfd->vidioc_try_fmt_vbi_output(file,
707 fh, f);
708 break;
709 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
710 if (vfd->vidioc_try_fmt_vbi_capture)
711 ret=vfd->vidioc_try_fmt_vbi_capture(file,
712 fh, f);
713 break;
714 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
715 if (vfd->vidioc_try_fmt_video_output)
716 ret=vfd->vidioc_try_fmt_video_output(file,
717 fh, f);
718 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300719 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
720 if (vfd->vidioc_try_fmt_output_overlay)
721 ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
722 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300723 case V4L2_BUF_TYPE_VBI_OUTPUT:
724 if (vfd->vidioc_try_fmt_vbi_output)
725 ret=vfd->vidioc_try_fmt_vbi_output(file,
726 fh, f);
727 break;
728 case V4L2_BUF_TYPE_PRIVATE:
729 if (vfd->vidioc_try_fmt_type_private)
730 ret=vfd->vidioc_try_fmt_type_private(file,
731 fh, f);
732 break;
733 }
734
735 break;
736 }
737 /* FIXME: Those buf reqs could be handled here,
738 with some changes on videobuf to allow its header to be included at
739 videodev2.h or being merged at videodev2.
740 */
741 case VIDIOC_REQBUFS:
742 {
743 struct v4l2_requestbuffers *p=arg;
744
745 if (!vfd->vidioc_reqbufs)
746 break;
747 ret = check_fmt (vfd, p->type);
748 if (ret)
749 break;
750
751 ret=vfd->vidioc_reqbufs(file, fh, p);
752 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
753 p->count,
754 prt_names(p->type,v4l2_type_names_FIXME),
755 prt_names(p->memory,v4l2_memory_names));
756 break;
757 }
758 case VIDIOC_QUERYBUF:
759 {
760 struct v4l2_buffer *p=arg;
761
762 if (!vfd->vidioc_querybuf)
763 break;
764 ret = check_fmt (vfd, p->type);
765 if (ret)
766 break;
767
768 ret=vfd->vidioc_querybuf(file, fh, p);
769 if (!ret)
770 dbgbuf(cmd,vfd,p);
771 break;
772 }
773 case VIDIOC_QBUF:
774 {
775 struct v4l2_buffer *p=arg;
776
777 if (!vfd->vidioc_qbuf)
778 break;
779 ret = check_fmt (vfd, p->type);
780 if (ret)
781 break;
782
783 ret=vfd->vidioc_qbuf(file, fh, p);
784 if (!ret)
785 dbgbuf(cmd,vfd,p);
786 break;
787 }
788 case VIDIOC_DQBUF:
789 {
790 struct v4l2_buffer *p=arg;
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300791 if (!vfd->vidioc_dqbuf)
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300792 break;
793 ret = check_fmt (vfd, p->type);
794 if (ret)
795 break;
796
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300797 ret=vfd->vidioc_dqbuf(file, fh, p);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300798 if (!ret)
799 dbgbuf(cmd,vfd,p);
800 break;
801 }
802 case VIDIOC_OVERLAY:
803 {
804 int *i = arg;
805
806 if (!vfd->vidioc_overlay)
807 break;
808 dbgarg (cmd, "value=%d\n",*i);
809 ret=vfd->vidioc_overlay(file, fh, *i);
810 break;
811 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300812 case VIDIOC_G_FBUF:
813 {
814 struct v4l2_framebuffer *p=arg;
815 if (!vfd->vidioc_g_fbuf)
816 break;
817 ret=vfd->vidioc_g_fbuf(file, fh, arg);
818 if (!ret) {
819 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
820 p->capability,p->flags,
821 (unsigned long)p->base);
822 v4l_print_pix_fmt (vfd, &p->fmt);
823 }
824 break;
825 }
826 case VIDIOC_S_FBUF:
827 {
828 struct v4l2_framebuffer *p=arg;
829 if (!vfd->vidioc_s_fbuf)
830 break;
831
832 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
833 p->capability,p->flags,(unsigned long)p->base);
834 v4l_print_pix_fmt (vfd, &p->fmt);
835 ret=vfd->vidioc_s_fbuf(file, fh, arg);
836
837 break;
838 }
839 case VIDIOC_STREAMON:
840 {
841 enum v4l2_buf_type i = *(int *)arg;
842 if (!vfd->vidioc_streamon)
843 break;
844 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
845 ret=vfd->vidioc_streamon(file, fh,i);
846 break;
847 }
848 case VIDIOC_STREAMOFF:
849 {
850 enum v4l2_buf_type i = *(int *)arg;
851
852 if (!vfd->vidioc_streamoff)
853 break;
854 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
855 ret=vfd->vidioc_streamoff(file, fh, i);
856 break;
857 }
858 /* ---------- tv norms ---------- */
859 case VIDIOC_ENUMSTD:
860 {
861 struct v4l2_standard *p = arg;
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300862 v4l2_std_id id = vfd->tvnorms,curr_id=0;
863 unsigned int index = p->index,i;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300864
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300865 if (index<0) {
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300866 ret=-EINVAL;
867 break;
868 }
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300869
870 /* Return norm array on a canonical way */
871 for (i=0;i<= index && id; i++) {
872 if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
873 curr_id = V4L2_STD_PAL;
874 } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
875 curr_id = V4L2_STD_PAL_BG;
876 } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
877 curr_id = V4L2_STD_PAL_DK;
878 } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
879 curr_id = V4L2_STD_PAL_B;
880 } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
881 curr_id = V4L2_STD_PAL_B1;
882 } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
883 curr_id = V4L2_STD_PAL_G;
884 } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
885 curr_id = V4L2_STD_PAL_H;
886 } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
887 curr_id = V4L2_STD_PAL_I;
888 } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
889 curr_id = V4L2_STD_PAL_D;
890 } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
891 curr_id = V4L2_STD_PAL_D1;
892 } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
893 curr_id = V4L2_STD_PAL_K;
894 } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
895 curr_id = V4L2_STD_PAL_M;
896 } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
897 curr_id = V4L2_STD_PAL_N;
898 } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
899 curr_id = V4L2_STD_PAL_Nc;
900 } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
901 curr_id = V4L2_STD_PAL_60;
902 } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
903 curr_id = V4L2_STD_NTSC;
904 } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
905 curr_id = V4L2_STD_NTSC_M;
906 } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
907 curr_id = V4L2_STD_NTSC_M_JP;
908 } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
909 curr_id = V4L2_STD_NTSC_443;
910 } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
911 curr_id = V4L2_STD_NTSC_M_KR;
912 } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
913 curr_id = V4L2_STD_SECAM;
914 } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
915 curr_id = V4L2_STD_SECAM_DK;
916 } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
917 curr_id = V4L2_STD_SECAM_B;
918 } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
919 curr_id = V4L2_STD_SECAM_D;
920 } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
921 curr_id = V4L2_STD_SECAM_G;
922 } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
923 curr_id = V4L2_STD_SECAM_H;
924 } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
925 curr_id = V4L2_STD_SECAM_K;
926 } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
927 curr_id = V4L2_STD_SECAM_K1;
928 } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
929 curr_id = V4L2_STD_SECAM_L;
930 } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
931 curr_id = V4L2_STD_SECAM_LC;
932 } else {
933 break;
934 }
935 id &= ~curr_id;
936 }
937 if (i<=index)
938 return -EINVAL;
939
940 v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300941 p->index = index;
942
943 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
944 "framelines=%d\n", p->index,
945 (unsigned long long)p->id, p->name,
946 p->frameperiod.numerator,
947 p->frameperiod.denominator,
948 p->framelines);
949
950 ret=0;
951 break;
952 }
953 case VIDIOC_G_STD:
954 {
955 v4l2_std_id *id = arg;
956
957 *id = vfd->current_norm;
958
959 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
960
961 ret=0;
962 break;
963 }
964 case VIDIOC_S_STD:
965 {
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300966 v4l2_std_id *id = arg,norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300967
968 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
969
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300970 norm = (*id) & vfd->tvnorms;
971 if ( vfd->tvnorms && !norm) /* Check if std is supported */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300972 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300973
974 /* Calls the specific handler */
975 if (vfd->vidioc_s_std)
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300976 ret=vfd->vidioc_s_std(file, fh, &norm);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300977 else
978 ret=-EINVAL;
979
980 /* Updates standard information */
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300981 if (ret>=0)
982 vfd->current_norm=norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300983
984 break;
985 }
986 case VIDIOC_QUERYSTD:
987 {
988 v4l2_std_id *p=arg;
989
990 if (!vfd->vidioc_querystd)
991 break;
992 ret=vfd->vidioc_querystd(file, fh, arg);
993 if (!ret)
994 dbgarg (cmd, "detected std=%Lu\n",
995 (unsigned long long)*p);
996 break;
997 }
998 /* ------ input switching ---------- */
999 /* FIXME: Inputs can be handled inside videodev2 */
1000 case VIDIOC_ENUMINPUT:
1001 {
1002 struct v4l2_input *p=arg;
1003 int i=p->index;
1004
1005 if (!vfd->vidioc_enum_input)
1006 break;
1007 memset(p, 0, sizeof(*p));
1008 p->index=i;
1009
1010 ret=vfd->vidioc_enum_input(file, fh, p);
1011 if (!ret)
1012 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1013 "audioset=%d, "
1014 "tuner=%d, std=%Ld, status=%d\n",
1015 p->index,p->name,p->type,p->audioset,
1016 p->tuner,
1017 (unsigned long long)p->std,
1018 p->status);
1019 break;
1020 }
1021 case VIDIOC_G_INPUT:
1022 {
1023 unsigned int *i = arg;
1024
1025 if (!vfd->vidioc_g_input)
1026 break;
1027 ret=vfd->vidioc_g_input(file, fh, i);
1028 if (!ret)
1029 dbgarg (cmd, "value=%d\n",*i);
1030 break;
1031 }
1032 case VIDIOC_S_INPUT:
1033 {
1034 unsigned int *i = arg;
1035
1036 if (!vfd->vidioc_s_input)
1037 break;
1038 dbgarg (cmd, "value=%d\n",*i);
1039 ret=vfd->vidioc_s_input(file, fh, *i);
1040 break;
1041 }
1042
1043 /* ------ output switching ---------- */
1044 case VIDIOC_G_OUTPUT:
1045 {
1046 unsigned int *i = arg;
1047
1048 if (!vfd->vidioc_g_output)
1049 break;
1050 ret=vfd->vidioc_g_output(file, fh, i);
1051 if (!ret)
1052 dbgarg (cmd, "value=%d\n",*i);
1053 break;
1054 }
1055 case VIDIOC_S_OUTPUT:
1056 {
1057 unsigned int *i = arg;
1058
1059 if (!vfd->vidioc_s_output)
1060 break;
1061 dbgarg (cmd, "value=%d\n",*i);
1062 ret=vfd->vidioc_s_output(file, fh, *i);
1063 break;
1064 }
1065
1066 /* --- controls ---------------------------------------------- */
1067 case VIDIOC_QUERYCTRL:
1068 {
1069 struct v4l2_queryctrl *p=arg;
1070
1071 if (!vfd->vidioc_queryctrl)
1072 break;
1073 ret=vfd->vidioc_queryctrl(file, fh, p);
1074
1075 if (!ret)
1076 dbgarg (cmd, "id=%d, type=%d, name=%s, "
1077 "min/max=%d/%d,"
1078 " step=%d, default=%d, flags=0x%08x\n",
1079 p->id,p->type,p->name,p->minimum,
1080 p->maximum,p->step,p->default_value,
1081 p->flags);
1082 break;
1083 }
1084 case VIDIOC_G_CTRL:
1085 {
1086 struct v4l2_control *p = arg;
1087
1088 if (!vfd->vidioc_g_ctrl)
1089 break;
1090 dbgarg(cmd, "Enum for index=%d\n", p->id);
1091
1092 ret=vfd->vidioc_g_ctrl(file, fh, p);
1093 if (!ret)
1094 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1095 break;
1096 }
1097 case VIDIOC_S_CTRL:
1098 {
1099 struct v4l2_control *p = arg;
1100
1101 if (!vfd->vidioc_s_ctrl)
1102 break;
1103 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1104
1105 ret=vfd->vidioc_s_ctrl(file, fh, p);
1106 break;
1107 }
Hans Verkuil05976912006-06-18 13:43:28 -03001108 case VIDIOC_G_EXT_CTRLS:
1109 {
1110 struct v4l2_ext_controls *p = arg;
1111
1112 if (vfd->vidioc_g_ext_ctrls) {
1113 dbgarg(cmd, "count=%d\n", p->count);
1114
1115 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1116 }
1117 break;
1118 }
1119 case VIDIOC_S_EXT_CTRLS:
1120 {
1121 struct v4l2_ext_controls *p = arg;
1122
1123 if (vfd->vidioc_s_ext_ctrls) {
1124 dbgarg(cmd, "count=%d\n", p->count);
1125
1126 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1127 }
1128 break;
1129 }
1130 case VIDIOC_TRY_EXT_CTRLS:
1131 {
1132 struct v4l2_ext_controls *p = arg;
1133
1134 if (vfd->vidioc_try_ext_ctrls) {
1135 dbgarg(cmd, "count=%d\n", p->count);
1136
1137 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1138 }
1139 break;
1140 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001141 case VIDIOC_QUERYMENU:
1142 {
1143 struct v4l2_querymenu *p=arg;
1144 if (!vfd->vidioc_querymenu)
1145 break;
1146 ret=vfd->vidioc_querymenu(file, fh, p);
1147 if (!ret)
1148 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1149 p->id,p->index,p->name);
1150 break;
1151 }
1152 /* --- audio ---------------------------------------------- */
1153 case VIDIOC_ENUMAUDIO:
1154 {
1155 struct v4l2_audio *p=arg;
1156
1157 if (!vfd->vidioc_enumaudio)
1158 break;
1159 dbgarg(cmd, "Enum for index=%d\n", p->index);
1160 ret=vfd->vidioc_enumaudio(file, fh, p);
1161 if (!ret)
1162 dbgarg2("index=%d, name=%s, capability=%d, "
1163 "mode=%d\n",p->index,p->name,
1164 p->capability, p->mode);
1165 break;
1166 }
1167 case VIDIOC_G_AUDIO:
1168 {
1169 struct v4l2_audio *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001170 __u32 index=p->index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001171
1172 if (!vfd->vidioc_g_audio)
1173 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001174
1175 memset(p,0,sizeof(*p));
1176 p->index=index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001177 dbgarg(cmd, "Get for index=%d\n", p->index);
1178 ret=vfd->vidioc_g_audio(file, fh, p);
1179 if (!ret)
1180 dbgarg2("index=%d, name=%s, capability=%d, "
1181 "mode=%d\n",p->index,
1182 p->name,p->capability, p->mode);
1183 break;
1184 }
1185 case VIDIOC_S_AUDIO:
1186 {
1187 struct v4l2_audio *p=arg;
1188
1189 if (!vfd->vidioc_s_audio)
1190 break;
1191 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1192 "mode=%d\n", p->index, p->name,
1193 p->capability, p->mode);
1194 ret=vfd->vidioc_s_audio(file, fh, p);
1195 break;
1196 }
1197 case VIDIOC_ENUMAUDOUT:
1198 {
1199 struct v4l2_audioout *p=arg;
1200
1201 if (!vfd->vidioc_enumaudout)
1202 break;
1203 dbgarg(cmd, "Enum for index=%d\n", p->index);
1204 ret=vfd->vidioc_enumaudout(file, fh, p);
1205 if (!ret)
1206 dbgarg2("index=%d, name=%s, capability=%d, "
1207 "mode=%d\n", p->index, p->name,
1208 p->capability,p->mode);
1209 break;
1210 }
1211 case VIDIOC_G_AUDOUT:
1212 {
1213 struct v4l2_audioout *p=arg;
1214
1215 if (!vfd->vidioc_g_audout)
1216 break;
1217 dbgarg(cmd, "Enum for index=%d\n", p->index);
1218 ret=vfd->vidioc_g_audout(file, fh, p);
1219 if (!ret)
1220 dbgarg2("index=%d, name=%s, capability=%d, "
1221 "mode=%d\n", p->index, p->name,
1222 p->capability,p->mode);
1223 break;
1224 }
1225 case VIDIOC_S_AUDOUT:
1226 {
1227 struct v4l2_audioout *p=arg;
1228
1229 if (!vfd->vidioc_s_audout)
1230 break;
1231 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1232 "mode=%d\n", p->index, p->name,
1233 p->capability,p->mode);
1234
1235 ret=vfd->vidioc_s_audout(file, fh, p);
1236 break;
1237 }
1238 case VIDIOC_G_MODULATOR:
1239 {
1240 struct v4l2_modulator *p=arg;
1241 if (!vfd->vidioc_g_modulator)
1242 break;
1243 ret=vfd->vidioc_g_modulator(file, fh, p);
1244 if (!ret)
1245 dbgarg(cmd, "index=%d, name=%s, "
1246 "capability=%d, rangelow=%d,"
1247 " rangehigh=%d, txsubchans=%d\n",
1248 p->index, p->name,p->capability,
1249 p->rangelow, p->rangehigh,
1250 p->txsubchans);
1251 break;
1252 }
1253 case VIDIOC_S_MODULATOR:
1254 {
1255 struct v4l2_modulator *p=arg;
1256 if (!vfd->vidioc_s_modulator)
1257 break;
1258 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1259 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1260 p->index, p->name,p->capability,p->rangelow,
1261 p->rangehigh,p->txsubchans);
1262 ret=vfd->vidioc_s_modulator(file, fh, p);
1263 break;
1264 }
1265 case VIDIOC_G_CROP:
1266 {
1267 struct v4l2_crop *p=arg;
1268 if (!vfd->vidioc_g_crop)
1269 break;
1270 ret=vfd->vidioc_g_crop(file, fh, p);
1271 if (!ret) {
1272 dbgarg(cmd, "type=%d\n", p->type);
1273 dbgrect(vfd, "", &p->c);
1274 }
1275 break;
1276 }
1277 case VIDIOC_S_CROP:
1278 {
1279 struct v4l2_crop *p=arg;
1280 if (!vfd->vidioc_s_crop)
1281 break;
1282 dbgarg(cmd, "type=%d\n", p->type);
1283 dbgrect(vfd, "", &p->c);
1284 ret=vfd->vidioc_s_crop(file, fh, p);
1285 break;
1286 }
1287 case VIDIOC_CROPCAP:
1288 {
1289 struct v4l2_cropcap *p=arg;
1290 /*FIXME: Should also show v4l2_fract pixelaspect */
1291 if (!vfd->vidioc_cropcap)
1292 break;
1293 dbgarg(cmd, "type=%d\n", p->type);
1294 dbgrect(vfd, "bounds ", &p->bounds);
1295 dbgrect(vfd, "defrect ", &p->defrect);
1296 ret=vfd->vidioc_cropcap(file, fh, p);
1297 break;
1298 }
1299 case VIDIOC_G_MPEGCOMP:
1300 {
1301 struct v4l2_mpeg_compression *p=arg;
Hans Verkuilf81cf752006-06-18 16:54:20 -03001302
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001303 /*FIXME: Several fields not shown */
1304 if (!vfd->vidioc_g_mpegcomp)
1305 break;
1306 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1307 if (!ret)
1308 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1309 " ts_pid_video=%d, ts_pid_pcr=%d, "
1310 "ps_size=%d, au_sample_rate=%d, "
1311 "au_pesid=%c, vi_frame_rate=%d, "
1312 "vi_frames_per_gop=%d, "
1313 "vi_bframes_count=%d, vi_pesid=%c\n",
1314 p->ts_pid_pmt,p->ts_pid_audio,
1315 p->ts_pid_video,p->ts_pid_pcr,
1316 p->ps_size, p->au_sample_rate,
1317 p->au_pesid, p->vi_frame_rate,
1318 p->vi_frames_per_gop,
1319 p->vi_bframes_count, p->vi_pesid);
1320 break;
1321 }
1322 case VIDIOC_S_MPEGCOMP:
1323 {
1324 struct v4l2_mpeg_compression *p=arg;
1325 /*FIXME: Several fields not shown */
1326 if (!vfd->vidioc_s_mpegcomp)
1327 break;
1328 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1329 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1330 "au_sample_rate=%d, au_pesid=%c, "
1331 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1332 "vi_bframes_count=%d, vi_pesid=%c\n",
1333 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1334 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1335 p->au_pesid, p->vi_frame_rate,
1336 p->vi_frames_per_gop, p->vi_bframes_count,
1337 p->vi_pesid);
1338 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1339 break;
1340 }
1341 case VIDIOC_G_JPEGCOMP:
1342 {
1343 struct v4l2_jpegcompression *p=arg;
1344 if (!vfd->vidioc_g_jpegcomp)
1345 break;
1346 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1347 if (!ret)
1348 dbgarg (cmd, "quality=%d, APPn=%d, "
1349 "APP_len=%d, COM_len=%d, "
1350 "jpeg_markers=%d\n",
1351 p->quality,p->APPn,p->APP_len,
1352 p->COM_len,p->jpeg_markers);
1353 break;
1354 }
1355 case VIDIOC_S_JPEGCOMP:
1356 {
1357 struct v4l2_jpegcompression *p=arg;
1358 if (!vfd->vidioc_g_jpegcomp)
1359 break;
1360 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1361 "COM_len=%d, jpeg_markers=%d\n",
1362 p->quality,p->APPn,p->APP_len,
1363 p->COM_len,p->jpeg_markers);
1364 ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1365 break;
1366 }
Hans Verkuildb6eb5b2007-02-18 14:05:02 -03001367 case VIDIOC_G_ENC_INDEX:
1368 {
1369 struct v4l2_enc_idx *p=arg;
1370
1371 if (!vfd->vidioc_g_enc_index)
1372 break;
1373 ret=vfd->vidioc_g_enc_index(file, fh, p);
1374 if (!ret)
1375 dbgarg (cmd, "entries=%d, entries_cap=%d\n",
1376 p->entries,p->entries_cap);
1377 break;
1378 }
Hans Verkuilada6ecd2007-02-18 14:56:22 -03001379 case VIDIOC_ENCODER_CMD:
1380 {
1381 struct v4l2_encoder_cmd *p=arg;
1382
1383 if (!vfd->vidioc_encoder_cmd)
1384 break;
1385 ret=vfd->vidioc_encoder_cmd(file, fh, p);
1386 if (!ret)
1387 dbgarg (cmd, "cmd=%d, flags=%d\n",
1388 p->cmd,p->flags);
1389 break;
1390 }
1391 case VIDIOC_TRY_ENCODER_CMD:
1392 {
1393 struct v4l2_encoder_cmd *p=arg;
1394
1395 if (!vfd->vidioc_try_encoder_cmd)
1396 break;
1397 ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
1398 if (!ret)
1399 dbgarg (cmd, "cmd=%d, flags=%d\n",
1400 p->cmd,p->flags);
1401 break;
1402 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001403 case VIDIOC_G_PARM:
1404 {
1405 struct v4l2_streamparm *p=arg;
Mauro Carvalho Chehab2aa23422007-04-24 13:40:07 -03001406 __u32 type=p->type;
1407
1408 memset(p,0,sizeof(*p));
1409 p->type=type;
1410
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001411 if (vfd->vidioc_g_parm) {
1412 ret=vfd->vidioc_g_parm(file, fh, p);
1413 } else {
1414 struct v4l2_standard s;
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001415
1416 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1417 return -EINVAL;
1418
Jonathan Corbet83427ac2006-10-13 07:51:16 -03001419 v4l2_video_std_construct(&s, vfd->current_norm,
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001420 v4l2_norm_to_name(vfd->current_norm));
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001421
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001422 p->parm.capture.timeperframe = s.frameperiod;
1423 ret=0;
1424 }
1425
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001426 dbgarg (cmd, "type=%d\n", p->type);
1427 break;
1428 }
1429 case VIDIOC_S_PARM:
1430 {
1431 struct v4l2_streamparm *p=arg;
1432 if (!vfd->vidioc_s_parm)
1433 break;
1434 dbgarg (cmd, "type=%d\n", p->type);
1435 ret=vfd->vidioc_s_parm(file, fh, p);
1436 break;
1437 }
1438 case VIDIOC_G_TUNER:
1439 {
1440 struct v4l2_tuner *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001441 __u32 index=p->index;
1442
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001443 if (!vfd->vidioc_g_tuner)
1444 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001445
1446 memset(p,0,sizeof(*p));
1447 p->index=index;
1448
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001449 ret=vfd->vidioc_g_tuner(file, fh, p);
1450 if (!ret)
1451 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1452 "capability=%d, rangelow=%d, "
1453 "rangehigh=%d, signal=%d, afc=%d, "
1454 "rxsubchans=%d, audmode=%d\n",
1455 p->index, p->name, p->type,
1456 p->capability, p->rangelow,
1457 p->rangehigh, p->rxsubchans,
1458 p->audmode, p->signal, p->afc);
1459 break;
1460 }
1461 case VIDIOC_S_TUNER:
1462 {
1463 struct v4l2_tuner *p=arg;
1464 if (!vfd->vidioc_s_tuner)
1465 break;
1466 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1467 "capability=%d, rangelow=%d, rangehigh=%d, "
1468 "signal=%d, afc=%d, rxsubchans=%d, "
1469 "audmode=%d\n",p->index, p->name, p->type,
1470 p->capability, p->rangelow,p->rangehigh,
1471 p->rxsubchans, p->audmode, p->signal,
1472 p->afc);
1473 ret=vfd->vidioc_s_tuner(file, fh, p);
1474 break;
1475 }
1476 case VIDIOC_G_FREQUENCY:
1477 {
1478 struct v4l2_frequency *p=arg;
1479 if (!vfd->vidioc_g_frequency)
1480 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001481
1482 memset(p,0,sizeof(*p));
1483
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001484 ret=vfd->vidioc_g_frequency(file, fh, p);
1485 if (!ret)
1486 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1487 p->tuner,p->type,p->frequency);
1488 break;
1489 }
1490 case VIDIOC_S_FREQUENCY:
1491 {
1492 struct v4l2_frequency *p=arg;
1493 if (!vfd->vidioc_s_frequency)
1494 break;
1495 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1496 p->tuner,p->type,p->frequency);
1497 ret=vfd->vidioc_s_frequency(file, fh, p);
1498 break;
1499 }
1500 case VIDIOC_G_SLICED_VBI_CAP:
1501 {
1502 struct v4l2_sliced_vbi_cap *p=arg;
1503 if (!vfd->vidioc_g_sliced_vbi_cap)
1504 break;
1505 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1506 if (!ret)
1507 dbgarg (cmd, "service_set=%d\n", p->service_set);
1508 break;
1509 }
1510 case VIDIOC_LOG_STATUS:
1511 {
1512 if (!vfd->vidioc_log_status)
1513 break;
1514 ret=vfd->vidioc_log_status(file, fh);
1515 break;
1516 }
Trent Piephodbbff482007-01-22 23:31:53 -03001517#ifdef CONFIG_VIDEO_ADV_DEBUG
Trent Piepho52ebc762007-01-23 22:38:13 -03001518 case VIDIOC_DBG_G_REGISTER:
Trent Piephodbbff482007-01-22 23:31:53 -03001519 {
1520 struct v4l2_register *p=arg;
Trent Piepho62d50ad2007-01-30 23:25:41 -03001521 if (!capable(CAP_SYS_ADMIN))
1522 ret=-EPERM;
1523 else if (vfd->vidioc_g_register)
Trent Piephodbbff482007-01-22 23:31:53 -03001524 ret=vfd->vidioc_g_register(file, fh, p);
1525 break;
1526 }
Trent Piepho52ebc762007-01-23 22:38:13 -03001527 case VIDIOC_DBG_S_REGISTER:
Trent Piephodbbff482007-01-22 23:31:53 -03001528 {
1529 struct v4l2_register *p=arg;
Trent Piepho52ebc762007-01-23 22:38:13 -03001530 if (!capable(CAP_SYS_ADMIN))
1531 ret=-EPERM;
1532 else if (vfd->vidioc_s_register)
Trent Piephodbbff482007-01-22 23:31:53 -03001533 ret=vfd->vidioc_s_register(file, fh, p);
1534 break;
1535 }
1536#endif
Hans Verkuil3434eb72007-04-27 12:31:08 -03001537 case VIDIOC_G_CHIP_IDENT:
1538 {
1539 struct v4l2_chip_ident *p=arg;
1540 if (!vfd->vidioc_g_chip_ident)
1541 break;
1542 ret=vfd->vidioc_g_chip_ident(file, fh, p);
1543 if (!ret)
1544 dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1545 break;
1546 }
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -03001547 } /* switch */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001548
1549 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1550 if (ret<0) {
1551 printk ("%s: err:\n", vfd->name);
1552 v4l_print_ioctl(vfd->name, cmd);
1553 }
1554 }
1555
1556 return ret;
1557}
1558
1559int video_ioctl2 (struct inode *inode, struct file *file,
1560 unsigned int cmd, unsigned long arg)
1561{
1562 char sbuf[128];
1563 void *mbuf = NULL;
1564 void *parg = NULL;
1565 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001566 int is_ext_ctrl;
1567 size_t ctrls_size = 0;
1568 void __user *user_ptr = NULL;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001569
1570#ifdef __OLD_VIDIOC_
1571 cmd = video_fix_command(cmd);
1572#endif
Hans Verkuil05976912006-06-18 13:43:28 -03001573 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1574 cmd == VIDIOC_TRY_EXT_CTRLS);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001575
1576 /* Copy arguments into temp kernel buffer */
1577 switch (_IOC_DIR(cmd)) {
1578 case _IOC_NONE:
1579 parg = NULL;
1580 break;
1581 case _IOC_READ:
1582 case _IOC_WRITE:
1583 case (_IOC_WRITE | _IOC_READ):
1584 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1585 parg = sbuf;
1586 } else {
1587 /* too big to allocate from stack */
1588 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1589 if (NULL == mbuf)
1590 return -ENOMEM;
1591 parg = mbuf;
1592 }
1593
1594 err = -EFAULT;
1595 if (_IOC_DIR(cmd) & _IOC_WRITE)
1596 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1597 goto out;
1598 break;
1599 }
1600
Hans Verkuil05976912006-06-18 13:43:28 -03001601 if (is_ext_ctrl) {
1602 struct v4l2_ext_controls *p = parg;
1603
1604 /* In case of an error, tell the caller that it wasn't
1605 a specific control that caused it. */
1606 p->error_idx = p->count;
1607 user_ptr = (void __user *)p->controls;
1608 if (p->count) {
1609 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1610 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1611 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1612 err = -ENOMEM;
1613 if (NULL == mbuf)
1614 goto out_ext_ctrl;
1615 err = -EFAULT;
1616 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1617 goto out_ext_ctrl;
1618 p->controls = mbuf;
1619 }
1620 }
1621
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001622 /* Handles IOCTL */
1623 err = __video_do_ioctl(inode, file, cmd, parg);
1624 if (err == -ENOIOCTLCMD)
1625 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001626 if (is_ext_ctrl) {
1627 struct v4l2_ext_controls *p = parg;
1628
1629 p->controls = (void *)user_ptr;
1630 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1631 err = -EFAULT;
1632 goto out_ext_ctrl;
1633 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001634 if (err < 0)
1635 goto out;
1636
Hans Verkuil05976912006-06-18 13:43:28 -03001637out_ext_ctrl:
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001638 /* Copy results into user buffer */
1639 switch (_IOC_DIR(cmd))
1640 {
1641 case _IOC_READ:
1642 case (_IOC_WRITE | _IOC_READ):
1643 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1644 err = -EFAULT;
1645 break;
1646 }
1647
1648out:
1649 kfree(mbuf);
1650 return err;
1651}
1652
1653
Arjan van de Venfa027c22007-02-12 00:55:33 -08001654static const struct file_operations video_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656/**
1657 * video_register_device - register video4linux devices
1658 * @vfd: video device structure we want to register
1659 * @type: type of device to register
1660 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
1661 * -1 == first free)
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001662 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * The registration code assigns minor numbers based on the type
1664 * requested. -ENFILE is returned in all the device slots for this
1665 * category are full. If not then the minor field is set and the
1666 * driver initialize function is called (if non %NULL).
1667 *
1668 * Zero is returned on success.
1669 *
1670 * Valid types are
1671 *
1672 * %VFL_TYPE_GRABBER - A frame grabber
1673 *
1674 * %VFL_TYPE_VTX - A teletext device
1675 *
1676 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1677 *
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001678 * %VFL_TYPE_RADIO - A radio card
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
1680
1681int video_register_device(struct video_device *vfd, int type, int nr)
1682{
1683 int i=0;
1684 int base;
1685 int end;
Michael Krufky3117bee2006-07-19 13:23:38 -03001686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 char *name_base;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 switch(type)
1690 {
1691 case VFL_TYPE_GRABBER:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001692 base=MINOR_VFL_TYPE_GRABBER_MIN;
1693 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 name_base = "video";
1695 break;
1696 case VFL_TYPE_VTX:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001697 base=MINOR_VFL_TYPE_VTX_MIN;
1698 end=MINOR_VFL_TYPE_VTX_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 name_base = "vtx";
1700 break;
1701 case VFL_TYPE_VBI:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001702 base=MINOR_VFL_TYPE_VBI_MIN;
1703 end=MINOR_VFL_TYPE_VBI_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 name_base = "vbi";
1705 break;
1706 case VFL_TYPE_RADIO:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001707 base=MINOR_VFL_TYPE_RADIO_MIN;
1708 end=MINOR_VFL_TYPE_RADIO_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 name_base = "radio";
1710 break;
1711 default:
Trent Piepho53dd8de2006-07-25 09:31:42 -03001712 printk(KERN_ERR "%s called with unknown type: %d\n",
1713 __FUNCTION__, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return -1;
1715 }
1716
1717 /* pick a minor number */
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001718 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (nr >= 0 && nr < end-base) {
1720 /* use the one the driver asked for */
1721 i = base+nr;
1722 if (NULL != video_device[i]) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001723 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return -ENFILE;
1725 }
1726 } else {
1727 /* use first free */
1728 for(i=base;i<end;i++)
1729 if (NULL == video_device[i])
1730 break;
1731 if (i == end) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001732 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return -ENFILE;
1734 }
1735 }
1736 video_device[i]=vfd;
1737 vfd->minor=i;
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001738 mutex_unlock(&videodev_lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001739 mutex_init(&vfd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* sysfs class */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001742 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (vfd->dev)
1744 vfd->class_dev.dev = vfd->dev;
1745 vfd->class_dev.class = &video_class;
Michael Krufky50c25ff2006-01-09 15:25:34 -02001746 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Greg Kroah-Hartman5e483072005-06-20 21:15:16 -07001747 sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
Michael Krufky3117bee2006-07-19 13:23:38 -03001748 ret = class_device_register(&vfd->class_dev);
Trent Piepho8c313112006-07-25 20:37:03 -03001749 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -03001750 printk(KERN_ERR "%s: class_device_register failed\n",
1751 __FUNCTION__);
Trent Piephod94fc9a2006-07-29 17:18:06 -03001752 goto fail_minor;
Michael Krufky3117bee2006-07-19 13:23:38 -03001753 }
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001754 ret = class_device_create_file(&vfd->class_dev, &class_device_attr_name);
1755 if (ret < 0) {
Trent Piephod94fc9a2006-07-29 17:18:06 -03001756 printk(KERN_ERR "%s: class_device_create_file 'name' failed\n",
1757 __FUNCTION__);
1758 goto fail_classdev;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -02001761#if 1
1762 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (!vfd->release)
1764 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1765 "Please fix your driver for proper sysfs support, see "
1766 "http://lwn.net/Articles/36850/\n", vfd->name);
1767#endif
1768 return 0;
Trent Piepho53dd8de2006-07-25 09:31:42 -03001769
1770fail_classdev:
1771 class_device_unregister(&vfd->class_dev);
1772fail_minor:
1773 mutex_lock(&videodev_lock);
1774 video_device[vfd->minor] = NULL;
1775 vfd->minor = -1;
1776 mutex_unlock(&videodev_lock);
1777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780/**
1781 * video_unregister_device - unregister a video4linux device
1782 * @vfd: the device to unregister
1783 *
1784 * This unregisters the passed device and deassigns the minor
1785 * number. Future open calls will be met with errors.
1786 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788void video_unregister_device(struct video_device *vfd)
1789{
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001790 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if(video_device[vfd->minor]!=vfd)
1792 panic("videodev: bad unregister");
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 video_device[vfd->minor]=NULL;
1795 class_device_unregister(&vfd->class_dev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001796 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001799/*
1800 * Video fs operations
1801 */
Arjan van de Venfa027c22007-02-12 00:55:33 -08001802static const struct file_operations video_fops=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 .owner = THIS_MODULE,
1805 .llseek = no_llseek,
1806 .open = video_open,
1807};
1808
1809/*
1810 * Initialise video for linux
1811 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813static int __init videodev_init(void)
1814{
1815 int ret;
1816
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001817 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1819 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1820 return -EIO;
1821 }
1822
1823 ret = class_register(&video_class);
1824 if (ret < 0) {
1825 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1826 printk(KERN_WARNING "video_dev: class_register failed\n");
1827 return -EIO;
1828 }
1829
1830 return 0;
1831}
1832
1833static void __exit videodev_exit(void)
1834{
1835 class_unregister(&video_class);
1836 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1837}
1838
1839module_init(videodev_init)
1840module_exit(videodev_exit)
1841
1842EXPORT_SYMBOL(video_register_device);
1843EXPORT_SYMBOL(video_unregister_device);
1844EXPORT_SYMBOL(video_devdata);
1845EXPORT_SYMBOL(video_usercopy);
1846EXPORT_SYMBOL(video_exclusive_open);
1847EXPORT_SYMBOL(video_exclusive_release);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001848EXPORT_SYMBOL(video_ioctl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849EXPORT_SYMBOL(video_device_alloc);
1850EXPORT_SYMBOL(video_device_release);
1851
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001852MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1853MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854MODULE_LICENSE("GPL");
1855
1856
1857/*
1858 * Local variables:
1859 * c-basic-offset: 8
1860 * End:
1861 */