blob: 5c9f2116d7bf52bbff5db53182a18da50257736f [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/smp_lock.h>
34#include <linux/mm.h>
35#include <linux/string.h>
36#include <linux/errno.h>
37#include <linux/init.h>
38#include <linux/kmod.h>
39#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030043#define __OLD_VIDIOC_ /* To allow fixing old calls*/
44#include <linux/videodev2.h>
45
46#ifdef CONFIG_VIDEO_V4L1
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/videodev.h>
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030048#endif
49#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define VIDEO_NUM_DEVICES 256
52#define VIDEO_NAME "video4linux"
53
54/*
55 * sysfs stuff
56 */
57
58static ssize_t show_name(struct class_device *cd, char *buf)
59{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030060 struct video_device *vfd = container_of(cd, struct video_device,
61 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
63}
64
65static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
66
67struct video_device *video_device_alloc(void)
68{
69 struct video_device *vfd;
70
Panagiotis Issaris74081872006-01-11 19:40:56 -020071 vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return vfd;
73}
74
75void video_device_release(struct video_device *vfd)
76{
77 kfree(vfd);
78}
79
80static void video_release(struct class_device *cd)
81{
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -030082 struct video_device *vfd = container_of(cd, struct video_device,
83 class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -020085#if 1
Michael Krufky50c25ff2006-01-09 15:25:34 -020086 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (!vfd->release)
88 return;
89#endif
90 vfd->release(vfd);
91}
92
93static struct class video_class = {
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080094 .name = VIDEO_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .release = video_release,
96};
97
98/*
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080099 * Active devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct video_device *video_device[VIDEO_NUM_DEVICES];
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200103static DEFINE_MUTEX(videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105struct video_device* video_devdata(struct file *file)
106{
Josef Sipek723731b2006-12-08 02:37:47 -0800107 return video_device[iminor(file->f_path.dentry->d_inode)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/*
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300111 * Open a video device - FIXME: Obsoleted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
113static int video_open(struct inode *inode, struct file *file)
114{
115 unsigned int minor = iminor(inode);
116 int err = 0;
117 struct video_device *vfl;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800118 const struct file_operations *old_fops;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if(minor>=VIDEO_NUM_DEVICES)
121 return -ENODEV;
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200122 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 vfl=video_device[minor];
124 if(vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200125 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200127 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 vfl=video_device[minor];
129 if (vfl==NULL) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200130 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -ENODEV;
132 }
133 }
134 old_fops = file->f_op;
135 file->f_op = fops_get(vfl->fops);
136 if(file->f_op->open)
137 err = file->f_op->open(inode,file);
138 if (err) {
139 fops_put(file->f_op);
140 file->f_op = fops_get(old_fops);
141 }
142 fops_put(old_fops);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200143 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return err;
145}
146
147/*
148 * helper function -- handles userspace copying for ioctl arguments
149 */
150
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300151#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static unsigned int
153video_fix_command(unsigned int cmd)
154{
155 switch (cmd) {
156 case VIDIOC_OVERLAY_OLD:
157 cmd = VIDIOC_OVERLAY;
158 break;
159 case VIDIOC_S_PARM_OLD:
160 cmd = VIDIOC_S_PARM;
161 break;
162 case VIDIOC_S_CTRL_OLD:
163 cmd = VIDIOC_S_CTRL;
164 break;
165 case VIDIOC_G_AUDIO_OLD:
166 cmd = VIDIOC_G_AUDIO;
167 break;
168 case VIDIOC_G_AUDOUT_OLD:
169 cmd = VIDIOC_G_AUDOUT;
170 break;
171 case VIDIOC_CROPCAP_OLD:
172 cmd = VIDIOC_CROPCAP;
173 break;
174 }
175 return cmd;
176}
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300179/*
180 * Obsolete usercopy function - Should be removed soon
181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182int
183video_usercopy(struct inode *inode, struct file *file,
184 unsigned int cmd, unsigned long arg,
185 int (*func)(struct inode *inode, struct file *file,
186 unsigned int cmd, void *arg))
187{
188 char sbuf[128];
189 void *mbuf = NULL;
190 void *parg = NULL;
191 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300192 int is_ext_ctrl;
193 size_t ctrls_size = 0;
194 void __user *user_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300196#ifdef __OLD_VIDIOC_
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 cmd = video_fix_command(cmd);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300198#endif
Hans Verkuil05976912006-06-18 13:43:28 -0300199 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
200 cmd == VIDIOC_TRY_EXT_CTRLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Copy arguments into temp kernel buffer */
203 switch (_IOC_DIR(cmd)) {
204 case _IOC_NONE:
205 parg = NULL;
206 break;
207 case _IOC_READ:
208 case _IOC_WRITE:
209 case (_IOC_WRITE | _IOC_READ):
210 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
211 parg = sbuf;
212 } else {
213 /* too big to allocate from stack */
214 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
215 if (NULL == mbuf)
216 return -ENOMEM;
217 parg = mbuf;
218 }
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 err = -EFAULT;
221 if (_IOC_DIR(cmd) & _IOC_WRITE)
Hans Verkuil05976912006-06-18 13:43:28 -0300222 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto out;
224 break;
225 }
Hans Verkuil05976912006-06-18 13:43:28 -0300226 if (is_ext_ctrl) {
227 struct v4l2_ext_controls *p = parg;
228
229 /* In case of an error, tell the caller that it wasn't
230 a specific control that caused it. */
231 p->error_idx = p->count;
232 user_ptr = (void __user *)p->controls;
233 if (p->count) {
234 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
235 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
236 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
237 err = -ENOMEM;
238 if (NULL == mbuf)
239 goto out_ext_ctrl;
240 err = -EFAULT;
241 if (copy_from_user(mbuf, user_ptr, ctrls_size))
242 goto out_ext_ctrl;
243 p->controls = mbuf;
244 }
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* call driver */
248 err = func(inode, file, cmd, parg);
249 if (err == -ENOIOCTLCMD)
250 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -0300251 if (is_ext_ctrl) {
252 struct v4l2_ext_controls *p = parg;
253
254 p->controls = (void *)user_ptr;
255 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
256 err = -EFAULT;
257 goto out_ext_ctrl;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (err < 0)
260 goto out;
261
Hans Verkuil05976912006-06-18 13:43:28 -0300262out_ext_ctrl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* Copy results into user buffer */
264 switch (_IOC_DIR(cmd))
265 {
266 case _IOC_READ:
267 case (_IOC_WRITE | _IOC_READ):
268 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
269 err = -EFAULT;
270 break;
271 }
272
273out:
Jesper Juhl2ea75332005-11-07 01:01:31 -0800274 kfree(mbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return err;
276}
277
278/*
279 * open/release helper functions -- handle exclusive opens
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300280 * Should be removed soon
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
282int video_exclusive_open(struct inode *inode, struct file *file)
283{
284 struct video_device *vfl = video_devdata(file);
285 int retval = 0;
286
Ingo Molnar3593cab2006-02-07 06:49:14 -0200287 mutex_lock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (vfl->users) {
289 retval = -EBUSY;
290 } else {
291 vfl->users++;
292 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200293 mutex_unlock(&vfl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return retval;
295}
296
297int video_exclusive_release(struct inode *inode, struct file *file)
298{
299 struct video_device *vfl = video_devdata(file);
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 vfl->users--;
302 return 0;
303}
304
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300305static char *v4l2_memory_names[] = {
306 [V4L2_MEMORY_MMAP] = "mmap",
307 [V4L2_MEMORY_USERPTR] = "userptr",
308 [V4L2_MEMORY_OVERLAY] = "overlay",
309};
310
311
312/* FIXME: Those stuff are replicated also on v4l2-common.c */
313static char *v4l2_type_names_FIXME[] = {
314 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
315 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
316 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
317 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
318 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
319 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
320 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
Hans Verkuilb2787842007-04-27 12:31:02 -0300321 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300322 [V4L2_BUF_TYPE_PRIVATE] = "private",
323};
324
325static char *v4l2_field_names_FIXME[] = {
326 [V4L2_FIELD_ANY] = "any",
327 [V4L2_FIELD_NONE] = "none",
328 [V4L2_FIELD_TOP] = "top",
329 [V4L2_FIELD_BOTTOM] = "bottom",
330 [V4L2_FIELD_INTERLACED] = "interlaced",
331 [V4L2_FIELD_SEQ_TB] = "seq-tb",
332 [V4L2_FIELD_SEQ_BT] = "seq-bt",
333 [V4L2_FIELD_ALTERNATE] = "alternate",
Hans Verkuilb2787842007-04-27 12:31:02 -0300334 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
335 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300336};
337
338#define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
339
340static void dbgbuf(unsigned int cmd, struct video_device *vfd,
341 struct v4l2_buffer *p)
342{
343 struct v4l2_timecode *tc=&p->timecode;
344
345 dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
346 "bytesused=%d, flags=0x%08d, "
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300347 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300348 (p->timestamp.tv_sec/3600),
349 (int)(p->timestamp.tv_sec/60)%60,
350 (int)(p->timestamp.tv_sec%60),
351 p->timestamp.tv_usec,
352 p->index,
353 prt_names(p->type,v4l2_type_names_FIXME),
354 p->bytesused,p->flags,
355 p->field,p->sequence,
356 prt_names(p->memory,v4l2_memory_names),
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300357 p->m.userptr, p->length);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300358 dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
359 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
360 tc->hours,tc->minutes,tc->seconds,
Mauro Carvalho Chehabc18cb012006-06-23 07:05:22 -0300361 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300362}
363
364static inline void dbgrect(struct video_device *vfd, char *s,
365 struct v4l2_rect *r)
366{
367 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
368 r->width, r->height);
369};
370
371static inline void v4l_print_pix_fmt (struct video_device *vfd,
372 struct v4l2_pix_format *fmt)
373{
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300374 dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300375 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300376 fmt->width,fmt->height,
377 (fmt->pixelformat & 0xff),
378 (fmt->pixelformat >> 8) & 0xff,
379 (fmt->pixelformat >> 16) & 0xff,
380 (fmt->pixelformat >> 24) & 0xff,
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300381 prt_names(fmt->field,v4l2_field_names_FIXME),
382 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
383};
384
385
386static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
387{
388 switch (type) {
389 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
390 if (vfd->vidioc_try_fmt_cap)
391 return (0);
392 break;
393 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
394 if (vfd->vidioc_try_fmt_overlay)
395 return (0);
396 break;
397 case V4L2_BUF_TYPE_VBI_CAPTURE:
398 if (vfd->vidioc_try_fmt_vbi)
399 return (0);
400 break;
401 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
402 if (vfd->vidioc_try_fmt_vbi_output)
403 return (0);
404 break;
405 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
406 if (vfd->vidioc_try_fmt_vbi_capture)
407 return (0);
408 break;
409 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
410 if (vfd->vidioc_try_fmt_video_output)
411 return (0);
412 break;
413 case V4L2_BUF_TYPE_VBI_OUTPUT:
414 if (vfd->vidioc_try_fmt_vbi_output)
415 return (0);
416 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300417 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
418 if (vfd->vidioc_try_fmt_output_overlay)
419 return (0);
420 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300421 case V4L2_BUF_TYPE_PRIVATE:
422 if (vfd->vidioc_try_fmt_type_private)
423 return (0);
424 break;
425 }
426 return (-EINVAL);
427}
428
429static int __video_do_ioctl(struct inode *inode, struct file *file,
430 unsigned int cmd, void *arg)
431{
432 struct video_device *vfd = video_devdata(file);
433 void *fh = file->private_data;
434 int ret = -EINVAL;
435
436 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
437 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
438 v4l_print_ioctl(vfd->name, cmd);
439 }
440
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -0300441 if (_IOC_TYPE(cmd)=='v')
442 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
443 __video_do_ioctl);
444
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300445 switch(cmd) {
446 /* --- capabilities ------------------------------------------ */
447 case VIDIOC_QUERYCAP:
448 {
449 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
450 memset(cap, 0, sizeof(*cap));
451
452 if (!vfd->vidioc_querycap)
453 break;
454
455 ret=vfd->vidioc_querycap(file, fh, cap);
456 if (!ret)
457 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
458 "version=0x%08x, "
459 "capabilities=0x%08x\n",
460 cap->driver,cap->card,cap->bus_info,
461 cap->version,
462 cap->capabilities);
463 break;
464 }
465
466 /* --- priority ------------------------------------------ */
467 case VIDIOC_G_PRIORITY:
468 {
469 enum v4l2_priority *p=arg;
470
471 if (!vfd->vidioc_g_priority)
472 break;
473 ret=vfd->vidioc_g_priority(file, fh, p);
474 if (!ret)
475 dbgarg(cmd, "priority is %d\n", *p);
476 break;
477 }
478 case VIDIOC_S_PRIORITY:
479 {
480 enum v4l2_priority *p=arg;
481
482 if (!vfd->vidioc_s_priority)
483 break;
484 dbgarg(cmd, "setting priority to %d\n", *p);
485 ret=vfd->vidioc_s_priority(file, fh, *p);
486 break;
487 }
488
489 /* --- capture ioctls ---------------------------------------- */
490 case VIDIOC_ENUM_FMT:
491 {
492 struct v4l2_fmtdesc *f = arg;
493 enum v4l2_buf_type type;
494 unsigned int index;
495
496 index = f->index;
497 type = f->type;
498 memset(f,0,sizeof(*f));
499 f->index = index;
500 f->type = type;
501
502 switch (type) {
503 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
504 if (vfd->vidioc_enum_fmt_cap)
505 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
506 break;
507 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
508 if (vfd->vidioc_enum_fmt_overlay)
509 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
510 break;
511 case V4L2_BUF_TYPE_VBI_CAPTURE:
512 if (vfd->vidioc_enum_fmt_vbi)
513 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
514 break;
515 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
516 if (vfd->vidioc_enum_fmt_vbi_output)
517 ret=vfd->vidioc_enum_fmt_vbi_output(file,
518 fh, f);
519 break;
520 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
521 if (vfd->vidioc_enum_fmt_vbi_capture)
522 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
523 fh, f);
524 break;
525 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
526 if (vfd->vidioc_enum_fmt_video_output)
527 ret=vfd->vidioc_enum_fmt_video_output(file,
528 fh, f);
529 break;
530 case V4L2_BUF_TYPE_VBI_OUTPUT:
531 if (vfd->vidioc_enum_fmt_vbi_output)
532 ret=vfd->vidioc_enum_fmt_vbi_output(file,
533 fh, f);
534 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300535 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
536 if (vfd->vidioc_enum_fmt_output_overlay)
537 ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
538 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300539 case V4L2_BUF_TYPE_PRIVATE:
540 if (vfd->vidioc_enum_fmt_type_private)
541 ret=vfd->vidioc_enum_fmt_type_private(file,
542 fh, f);
543 break;
544 }
545 if (!ret)
546 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300547 "pixelformat=%c%c%c%c, description='%s'\n",
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300548 f->index, f->type, f->flags,
Mauro Carvalho Chehabbf5dbed2006-12-01 12:39:46 -0300549 (f->pixelformat & 0xff),
550 (f->pixelformat >> 8) & 0xff,
551 (f->pixelformat >> 16) & 0xff,
552 (f->pixelformat >> 24) & 0xff,
553 f->description);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300554 break;
555 }
556 case VIDIOC_G_FMT:
557 {
558 struct v4l2_format *f = (struct v4l2_format *)arg;
559 enum v4l2_buf_type type=f->type;
560
561 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
562 f->type=type;
563
564 /* FIXME: Should be one dump per type */
565 dbgarg (cmd, "type=%s\n", prt_names(type,
566 v4l2_type_names_FIXME));
567
568 switch (type) {
569 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
570 if (vfd->vidioc_g_fmt_cap)
571 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
572 if (!ret)
573 v4l_print_pix_fmt(vfd,&f->fmt.pix);
574 break;
575 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
576 if (vfd->vidioc_g_fmt_overlay)
577 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
578 break;
579 case V4L2_BUF_TYPE_VBI_CAPTURE:
580 if (vfd->vidioc_g_fmt_vbi)
581 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
582 break;
583 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
584 if (vfd->vidioc_g_fmt_vbi_output)
585 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
586 break;
587 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
588 if (vfd->vidioc_g_fmt_vbi_capture)
589 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
590 break;
591 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
592 if (vfd->vidioc_g_fmt_video_output)
593 ret=vfd->vidioc_g_fmt_video_output(file,
594 fh, f);
595 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300596 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
597 if (vfd->vidioc_g_fmt_output_overlay)
598 ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
599 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300600 case V4L2_BUF_TYPE_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_PRIVATE:
605 if (vfd->vidioc_g_fmt_type_private)
606 ret=vfd->vidioc_g_fmt_type_private(file,
607 fh, f);
608 break;
609 }
610
611 break;
612 }
613 case VIDIOC_S_FMT:
614 {
615 struct v4l2_format *f = (struct v4l2_format *)arg;
616
617 /* FIXME: Should be one dump per type */
618 dbgarg (cmd, "type=%s\n", prt_names(f->type,
619 v4l2_type_names_FIXME));
620
621 switch (f->type) {
622 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
623 v4l_print_pix_fmt(vfd,&f->fmt.pix);
624 if (vfd->vidioc_s_fmt_cap)
625 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
626 break;
627 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
628 if (vfd->vidioc_s_fmt_overlay)
629 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
630 break;
631 case V4L2_BUF_TYPE_VBI_CAPTURE:
632 if (vfd->vidioc_s_fmt_vbi)
633 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
634 break;
635 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
636 if (vfd->vidioc_s_fmt_vbi_output)
637 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
638 break;
639 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
640 if (vfd->vidioc_s_fmt_vbi_capture)
641 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
642 break;
643 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
644 if (vfd->vidioc_s_fmt_video_output)
645 ret=vfd->vidioc_s_fmt_video_output(file,
646 fh, f);
647 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300648 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
649 if (vfd->vidioc_s_fmt_output_overlay)
650 ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
651 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300652 case V4L2_BUF_TYPE_VBI_OUTPUT:
653 if (vfd->vidioc_s_fmt_vbi_output)
654 ret=vfd->vidioc_s_fmt_vbi_output(file,
655 fh, f);
656 break;
657 case V4L2_BUF_TYPE_PRIVATE:
658 if (vfd->vidioc_s_fmt_type_private)
659 ret=vfd->vidioc_s_fmt_type_private(file,
660 fh, f);
661 break;
662 }
663 break;
664 }
665 case VIDIOC_TRY_FMT:
666 {
667 struct v4l2_format *f = (struct v4l2_format *)arg;
668
669 /* FIXME: Should be one dump per type */
670 dbgarg (cmd, "type=%s\n", prt_names(f->type,
671 v4l2_type_names_FIXME));
672 switch (f->type) {
673 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
674 if (vfd->vidioc_try_fmt_cap)
675 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
676 if (!ret)
677 v4l_print_pix_fmt(vfd,&f->fmt.pix);
678 break;
679 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
680 if (vfd->vidioc_try_fmt_overlay)
681 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
682 break;
683 case V4L2_BUF_TYPE_VBI_CAPTURE:
684 if (vfd->vidioc_try_fmt_vbi)
685 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
686 break;
687 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
688 if (vfd->vidioc_try_fmt_vbi_output)
689 ret=vfd->vidioc_try_fmt_vbi_output(file,
690 fh, f);
691 break;
692 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
693 if (vfd->vidioc_try_fmt_vbi_capture)
694 ret=vfd->vidioc_try_fmt_vbi_capture(file,
695 fh, f);
696 break;
697 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
698 if (vfd->vidioc_try_fmt_video_output)
699 ret=vfd->vidioc_try_fmt_video_output(file,
700 fh, f);
701 break;
Hans Verkuilb2787842007-04-27 12:31:02 -0300702 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
703 if (vfd->vidioc_try_fmt_output_overlay)
704 ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
705 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300706 case V4L2_BUF_TYPE_VBI_OUTPUT:
707 if (vfd->vidioc_try_fmt_vbi_output)
708 ret=vfd->vidioc_try_fmt_vbi_output(file,
709 fh, f);
710 break;
711 case V4L2_BUF_TYPE_PRIVATE:
712 if (vfd->vidioc_try_fmt_type_private)
713 ret=vfd->vidioc_try_fmt_type_private(file,
714 fh, f);
715 break;
716 }
717
718 break;
719 }
720 /* FIXME: Those buf reqs could be handled here,
721 with some changes on videobuf to allow its header to be included at
722 videodev2.h or being merged at videodev2.
723 */
724 case VIDIOC_REQBUFS:
725 {
726 struct v4l2_requestbuffers *p=arg;
727
728 if (!vfd->vidioc_reqbufs)
729 break;
730 ret = check_fmt (vfd, p->type);
731 if (ret)
732 break;
733
734 ret=vfd->vidioc_reqbufs(file, fh, p);
735 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
736 p->count,
737 prt_names(p->type,v4l2_type_names_FIXME),
738 prt_names(p->memory,v4l2_memory_names));
739 break;
740 }
741 case VIDIOC_QUERYBUF:
742 {
743 struct v4l2_buffer *p=arg;
744
745 if (!vfd->vidioc_querybuf)
746 break;
747 ret = check_fmt (vfd, p->type);
748 if (ret)
749 break;
750
751 ret=vfd->vidioc_querybuf(file, fh, p);
752 if (!ret)
753 dbgbuf(cmd,vfd,p);
754 break;
755 }
756 case VIDIOC_QBUF:
757 {
758 struct v4l2_buffer *p=arg;
759
760 if (!vfd->vidioc_qbuf)
761 break;
762 ret = check_fmt (vfd, p->type);
763 if (ret)
764 break;
765
766 ret=vfd->vidioc_qbuf(file, fh, p);
767 if (!ret)
768 dbgbuf(cmd,vfd,p);
769 break;
770 }
771 case VIDIOC_DQBUF:
772 {
773 struct v4l2_buffer *p=arg;
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300774 if (!vfd->vidioc_dqbuf)
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300775 break;
776 ret = check_fmt (vfd, p->type);
777 if (ret)
778 break;
779
Sascha Hauerc93a5c32006-09-11 09:49:19 -0300780 ret=vfd->vidioc_dqbuf(file, fh, p);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300781 if (!ret)
782 dbgbuf(cmd,vfd,p);
783 break;
784 }
785 case VIDIOC_OVERLAY:
786 {
787 int *i = arg;
788
789 if (!vfd->vidioc_overlay)
790 break;
791 dbgarg (cmd, "value=%d\n",*i);
792 ret=vfd->vidioc_overlay(file, fh, *i);
793 break;
794 }
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -0300795#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300796 /* --- streaming capture ------------------------------------- */
797 case VIDIOCGMBUF:
798 {
799 struct video_mbuf *p=arg;
800
Mauro Carvalho Chehab4ceb04e2006-06-17 08:52:30 -0300801 memset(p,0,sizeof(p));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300802
803 if (!vfd->vidiocgmbuf)
804 break;
805 ret=vfd->vidiocgmbuf(file, fh, p);
806 if (!ret)
807 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
808 p->size, p->frames,
809 (unsigned long)p->offsets);
810 break;
811 }
812#endif
813 case VIDIOC_G_FBUF:
814 {
815 struct v4l2_framebuffer *p=arg;
816 if (!vfd->vidioc_g_fbuf)
817 break;
818 ret=vfd->vidioc_g_fbuf(file, fh, arg);
819 if (!ret) {
820 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
821 p->capability,p->flags,
822 (unsigned long)p->base);
823 v4l_print_pix_fmt (vfd, &p->fmt);
824 }
825 break;
826 }
827 case VIDIOC_S_FBUF:
828 {
829 struct v4l2_framebuffer *p=arg;
830 if (!vfd->vidioc_s_fbuf)
831 break;
832
833 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
834 p->capability,p->flags,(unsigned long)p->base);
835 v4l_print_pix_fmt (vfd, &p->fmt);
836 ret=vfd->vidioc_s_fbuf(file, fh, arg);
837
838 break;
839 }
840 case VIDIOC_STREAMON:
841 {
842 enum v4l2_buf_type i = *(int *)arg;
843 if (!vfd->vidioc_streamon)
844 break;
845 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
846 ret=vfd->vidioc_streamon(file, fh,i);
847 break;
848 }
849 case VIDIOC_STREAMOFF:
850 {
851 enum v4l2_buf_type i = *(int *)arg;
852
853 if (!vfd->vidioc_streamoff)
854 break;
855 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
856 ret=vfd->vidioc_streamoff(file, fh, i);
857 break;
858 }
859 /* ---------- tv norms ---------- */
860 case VIDIOC_ENUMSTD:
861 {
862 struct v4l2_standard *p = arg;
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300863 v4l2_std_id id = vfd->tvnorms,curr_id=0;
864 unsigned int index = p->index,i;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300865
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300866 if (index<0) {
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300867 ret=-EINVAL;
868 break;
869 }
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300870
871 /* Return norm array on a canonical way */
872 for (i=0;i<= index && id; i++) {
873 if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
874 curr_id = V4L2_STD_PAL;
875 } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
876 curr_id = V4L2_STD_PAL_BG;
877 } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
878 curr_id = V4L2_STD_PAL_DK;
879 } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
880 curr_id = V4L2_STD_PAL_B;
881 } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
882 curr_id = V4L2_STD_PAL_B1;
883 } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
884 curr_id = V4L2_STD_PAL_G;
885 } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
886 curr_id = V4L2_STD_PAL_H;
887 } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
888 curr_id = V4L2_STD_PAL_I;
889 } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
890 curr_id = V4L2_STD_PAL_D;
891 } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
892 curr_id = V4L2_STD_PAL_D1;
893 } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
894 curr_id = V4L2_STD_PAL_K;
895 } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
896 curr_id = V4L2_STD_PAL_M;
897 } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
898 curr_id = V4L2_STD_PAL_N;
899 } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
900 curr_id = V4L2_STD_PAL_Nc;
901 } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
902 curr_id = V4L2_STD_PAL_60;
903 } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
904 curr_id = V4L2_STD_NTSC;
905 } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
906 curr_id = V4L2_STD_NTSC_M;
907 } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
908 curr_id = V4L2_STD_NTSC_M_JP;
909 } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
910 curr_id = V4L2_STD_NTSC_443;
911 } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
912 curr_id = V4L2_STD_NTSC_M_KR;
913 } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
914 curr_id = V4L2_STD_SECAM;
915 } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
916 curr_id = V4L2_STD_SECAM_DK;
917 } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
918 curr_id = V4L2_STD_SECAM_B;
919 } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
920 curr_id = V4L2_STD_SECAM_D;
921 } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
922 curr_id = V4L2_STD_SECAM_G;
923 } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
924 curr_id = V4L2_STD_SECAM_H;
925 } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
926 curr_id = V4L2_STD_SECAM_K;
927 } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
928 curr_id = V4L2_STD_SECAM_K1;
929 } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
930 curr_id = V4L2_STD_SECAM_L;
931 } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
932 curr_id = V4L2_STD_SECAM_LC;
933 } else {
934 break;
935 }
936 id &= ~curr_id;
937 }
938 if (i<=index)
939 return -EINVAL;
940
941 v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300942 p->index = index;
943
944 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
945 "framelines=%d\n", p->index,
946 (unsigned long long)p->id, p->name,
947 p->frameperiod.numerator,
948 p->frameperiod.denominator,
949 p->framelines);
950
951 ret=0;
952 break;
953 }
954 case VIDIOC_G_STD:
955 {
956 v4l2_std_id *id = arg;
957
958 *id = vfd->current_norm;
959
960 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
961
962 ret=0;
963 break;
964 }
965 case VIDIOC_S_STD:
966 {
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300967 v4l2_std_id *id = arg,norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300968
969 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
970
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300971 norm = (*id) & vfd->tvnorms;
972 if ( vfd->tvnorms && !norm) /* Check if std is supported */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300973 break;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300974
975 /* Calls the specific handler */
976 if (vfd->vidioc_s_std)
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300977 ret=vfd->vidioc_s_std(file, fh, &norm);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300978 else
979 ret=-EINVAL;
980
981 /* Updates standard information */
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300982 if (ret>=0)
983 vfd->current_norm=norm;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300984
985 break;
986 }
987 case VIDIOC_QUERYSTD:
988 {
989 v4l2_std_id *p=arg;
990
991 if (!vfd->vidioc_querystd)
992 break;
993 ret=vfd->vidioc_querystd(file, fh, arg);
994 if (!ret)
995 dbgarg (cmd, "detected std=%Lu\n",
996 (unsigned long long)*p);
997 break;
998 }
999 /* ------ input switching ---------- */
1000 /* FIXME: Inputs can be handled inside videodev2 */
1001 case VIDIOC_ENUMINPUT:
1002 {
1003 struct v4l2_input *p=arg;
1004 int i=p->index;
1005
1006 if (!vfd->vidioc_enum_input)
1007 break;
1008 memset(p, 0, sizeof(*p));
1009 p->index=i;
1010
1011 ret=vfd->vidioc_enum_input(file, fh, p);
1012 if (!ret)
1013 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1014 "audioset=%d, "
1015 "tuner=%d, std=%Ld, status=%d\n",
1016 p->index,p->name,p->type,p->audioset,
1017 p->tuner,
1018 (unsigned long long)p->std,
1019 p->status);
1020 break;
1021 }
1022 case VIDIOC_G_INPUT:
1023 {
1024 unsigned int *i = arg;
1025
1026 if (!vfd->vidioc_g_input)
1027 break;
1028 ret=vfd->vidioc_g_input(file, fh, i);
1029 if (!ret)
1030 dbgarg (cmd, "value=%d\n",*i);
1031 break;
1032 }
1033 case VIDIOC_S_INPUT:
1034 {
1035 unsigned int *i = arg;
1036
1037 if (!vfd->vidioc_s_input)
1038 break;
1039 dbgarg (cmd, "value=%d\n",*i);
1040 ret=vfd->vidioc_s_input(file, fh, *i);
1041 break;
1042 }
1043
1044 /* ------ output switching ---------- */
1045 case VIDIOC_G_OUTPUT:
1046 {
1047 unsigned int *i = arg;
1048
1049 if (!vfd->vidioc_g_output)
1050 break;
1051 ret=vfd->vidioc_g_output(file, fh, i);
1052 if (!ret)
1053 dbgarg (cmd, "value=%d\n",*i);
1054 break;
1055 }
1056 case VIDIOC_S_OUTPUT:
1057 {
1058 unsigned int *i = arg;
1059
1060 if (!vfd->vidioc_s_output)
1061 break;
1062 dbgarg (cmd, "value=%d\n",*i);
1063 ret=vfd->vidioc_s_output(file, fh, *i);
1064 break;
1065 }
1066
1067 /* --- controls ---------------------------------------------- */
1068 case VIDIOC_QUERYCTRL:
1069 {
1070 struct v4l2_queryctrl *p=arg;
1071
1072 if (!vfd->vidioc_queryctrl)
1073 break;
1074 ret=vfd->vidioc_queryctrl(file, fh, p);
1075
1076 if (!ret)
1077 dbgarg (cmd, "id=%d, type=%d, name=%s, "
1078 "min/max=%d/%d,"
1079 " step=%d, default=%d, flags=0x%08x\n",
1080 p->id,p->type,p->name,p->minimum,
1081 p->maximum,p->step,p->default_value,
1082 p->flags);
1083 break;
1084 }
1085 case VIDIOC_G_CTRL:
1086 {
1087 struct v4l2_control *p = arg;
1088
1089 if (!vfd->vidioc_g_ctrl)
1090 break;
1091 dbgarg(cmd, "Enum for index=%d\n", p->id);
1092
1093 ret=vfd->vidioc_g_ctrl(file, fh, p);
1094 if (!ret)
1095 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1096 break;
1097 }
1098 case VIDIOC_S_CTRL:
1099 {
1100 struct v4l2_control *p = arg;
1101
1102 if (!vfd->vidioc_s_ctrl)
1103 break;
1104 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1105
1106 ret=vfd->vidioc_s_ctrl(file, fh, p);
1107 break;
1108 }
Hans Verkuil05976912006-06-18 13:43:28 -03001109 case VIDIOC_G_EXT_CTRLS:
1110 {
1111 struct v4l2_ext_controls *p = arg;
1112
1113 if (vfd->vidioc_g_ext_ctrls) {
1114 dbgarg(cmd, "count=%d\n", p->count);
1115
1116 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1117 }
1118 break;
1119 }
1120 case VIDIOC_S_EXT_CTRLS:
1121 {
1122 struct v4l2_ext_controls *p = arg;
1123
1124 if (vfd->vidioc_s_ext_ctrls) {
1125 dbgarg(cmd, "count=%d\n", p->count);
1126
1127 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1128 }
1129 break;
1130 }
1131 case VIDIOC_TRY_EXT_CTRLS:
1132 {
1133 struct v4l2_ext_controls *p = arg;
1134
1135 if (vfd->vidioc_try_ext_ctrls) {
1136 dbgarg(cmd, "count=%d\n", p->count);
1137
1138 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1139 }
1140 break;
1141 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001142 case VIDIOC_QUERYMENU:
1143 {
1144 struct v4l2_querymenu *p=arg;
1145 if (!vfd->vidioc_querymenu)
1146 break;
1147 ret=vfd->vidioc_querymenu(file, fh, p);
1148 if (!ret)
1149 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1150 p->id,p->index,p->name);
1151 break;
1152 }
1153 /* --- audio ---------------------------------------------- */
1154 case VIDIOC_ENUMAUDIO:
1155 {
1156 struct v4l2_audio *p=arg;
1157
1158 if (!vfd->vidioc_enumaudio)
1159 break;
1160 dbgarg(cmd, "Enum for index=%d\n", p->index);
1161 ret=vfd->vidioc_enumaudio(file, fh, p);
1162 if (!ret)
1163 dbgarg2("index=%d, name=%s, capability=%d, "
1164 "mode=%d\n",p->index,p->name,
1165 p->capability, p->mode);
1166 break;
1167 }
1168 case VIDIOC_G_AUDIO:
1169 {
1170 struct v4l2_audio *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001171 __u32 index=p->index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001172
1173 if (!vfd->vidioc_g_audio)
1174 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001175
1176 memset(p,0,sizeof(*p));
1177 p->index=index;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001178 dbgarg(cmd, "Get for index=%d\n", p->index);
1179 ret=vfd->vidioc_g_audio(file, fh, p);
1180 if (!ret)
1181 dbgarg2("index=%d, name=%s, capability=%d, "
1182 "mode=%d\n",p->index,
1183 p->name,p->capability, p->mode);
1184 break;
1185 }
1186 case VIDIOC_S_AUDIO:
1187 {
1188 struct v4l2_audio *p=arg;
1189
1190 if (!vfd->vidioc_s_audio)
1191 break;
1192 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1193 "mode=%d\n", p->index, p->name,
1194 p->capability, p->mode);
1195 ret=vfd->vidioc_s_audio(file, fh, p);
1196 break;
1197 }
1198 case VIDIOC_ENUMAUDOUT:
1199 {
1200 struct v4l2_audioout *p=arg;
1201
1202 if (!vfd->vidioc_enumaudout)
1203 break;
1204 dbgarg(cmd, "Enum for index=%d\n", p->index);
1205 ret=vfd->vidioc_enumaudout(file, fh, p);
1206 if (!ret)
1207 dbgarg2("index=%d, name=%s, capability=%d, "
1208 "mode=%d\n", p->index, p->name,
1209 p->capability,p->mode);
1210 break;
1211 }
1212 case VIDIOC_G_AUDOUT:
1213 {
1214 struct v4l2_audioout *p=arg;
1215
1216 if (!vfd->vidioc_g_audout)
1217 break;
1218 dbgarg(cmd, "Enum for index=%d\n", p->index);
1219 ret=vfd->vidioc_g_audout(file, fh, p);
1220 if (!ret)
1221 dbgarg2("index=%d, name=%s, capability=%d, "
1222 "mode=%d\n", p->index, p->name,
1223 p->capability,p->mode);
1224 break;
1225 }
1226 case VIDIOC_S_AUDOUT:
1227 {
1228 struct v4l2_audioout *p=arg;
1229
1230 if (!vfd->vidioc_s_audout)
1231 break;
1232 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1233 "mode=%d\n", p->index, p->name,
1234 p->capability,p->mode);
1235
1236 ret=vfd->vidioc_s_audout(file, fh, p);
1237 break;
1238 }
1239 case VIDIOC_G_MODULATOR:
1240 {
1241 struct v4l2_modulator *p=arg;
1242 if (!vfd->vidioc_g_modulator)
1243 break;
1244 ret=vfd->vidioc_g_modulator(file, fh, p);
1245 if (!ret)
1246 dbgarg(cmd, "index=%d, name=%s, "
1247 "capability=%d, rangelow=%d,"
1248 " rangehigh=%d, txsubchans=%d\n",
1249 p->index, p->name,p->capability,
1250 p->rangelow, p->rangehigh,
1251 p->txsubchans);
1252 break;
1253 }
1254 case VIDIOC_S_MODULATOR:
1255 {
1256 struct v4l2_modulator *p=arg;
1257 if (!vfd->vidioc_s_modulator)
1258 break;
1259 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1260 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1261 p->index, p->name,p->capability,p->rangelow,
1262 p->rangehigh,p->txsubchans);
1263 ret=vfd->vidioc_s_modulator(file, fh, p);
1264 break;
1265 }
1266 case VIDIOC_G_CROP:
1267 {
1268 struct v4l2_crop *p=arg;
1269 if (!vfd->vidioc_g_crop)
1270 break;
1271 ret=vfd->vidioc_g_crop(file, fh, p);
1272 if (!ret) {
1273 dbgarg(cmd, "type=%d\n", p->type);
1274 dbgrect(vfd, "", &p->c);
1275 }
1276 break;
1277 }
1278 case VIDIOC_S_CROP:
1279 {
1280 struct v4l2_crop *p=arg;
1281 if (!vfd->vidioc_s_crop)
1282 break;
1283 dbgarg(cmd, "type=%d\n", p->type);
1284 dbgrect(vfd, "", &p->c);
1285 ret=vfd->vidioc_s_crop(file, fh, p);
1286 break;
1287 }
1288 case VIDIOC_CROPCAP:
1289 {
1290 struct v4l2_cropcap *p=arg;
1291 /*FIXME: Should also show v4l2_fract pixelaspect */
1292 if (!vfd->vidioc_cropcap)
1293 break;
1294 dbgarg(cmd, "type=%d\n", p->type);
1295 dbgrect(vfd, "bounds ", &p->bounds);
1296 dbgrect(vfd, "defrect ", &p->defrect);
1297 ret=vfd->vidioc_cropcap(file, fh, p);
1298 break;
1299 }
1300 case VIDIOC_G_MPEGCOMP:
1301 {
1302 struct v4l2_mpeg_compression *p=arg;
Hans Verkuilf81cf752006-06-18 16:54:20 -03001303
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001304 /*FIXME: Several fields not shown */
1305 if (!vfd->vidioc_g_mpegcomp)
1306 break;
1307 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1308 if (!ret)
1309 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1310 " ts_pid_video=%d, ts_pid_pcr=%d, "
1311 "ps_size=%d, au_sample_rate=%d, "
1312 "au_pesid=%c, vi_frame_rate=%d, "
1313 "vi_frames_per_gop=%d, "
1314 "vi_bframes_count=%d, vi_pesid=%c\n",
1315 p->ts_pid_pmt,p->ts_pid_audio,
1316 p->ts_pid_video,p->ts_pid_pcr,
1317 p->ps_size, p->au_sample_rate,
1318 p->au_pesid, p->vi_frame_rate,
1319 p->vi_frames_per_gop,
1320 p->vi_bframes_count, p->vi_pesid);
1321 break;
1322 }
1323 case VIDIOC_S_MPEGCOMP:
1324 {
1325 struct v4l2_mpeg_compression *p=arg;
1326 /*FIXME: Several fields not shown */
1327 if (!vfd->vidioc_s_mpegcomp)
1328 break;
1329 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1330 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1331 "au_sample_rate=%d, au_pesid=%c, "
1332 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1333 "vi_bframes_count=%d, vi_pesid=%c\n",
1334 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1335 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1336 p->au_pesid, p->vi_frame_rate,
1337 p->vi_frames_per_gop, p->vi_bframes_count,
1338 p->vi_pesid);
1339 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1340 break;
1341 }
1342 case VIDIOC_G_JPEGCOMP:
1343 {
1344 struct v4l2_jpegcompression *p=arg;
1345 if (!vfd->vidioc_g_jpegcomp)
1346 break;
1347 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1348 if (!ret)
1349 dbgarg (cmd, "quality=%d, APPn=%d, "
1350 "APP_len=%d, COM_len=%d, "
1351 "jpeg_markers=%d\n",
1352 p->quality,p->APPn,p->APP_len,
1353 p->COM_len,p->jpeg_markers);
1354 break;
1355 }
1356 case VIDIOC_S_JPEGCOMP:
1357 {
1358 struct v4l2_jpegcompression *p=arg;
1359 if (!vfd->vidioc_g_jpegcomp)
1360 break;
1361 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1362 "COM_len=%d, jpeg_markers=%d\n",
1363 p->quality,p->APPn,p->APP_len,
1364 p->COM_len,p->jpeg_markers);
1365 ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1366 break;
1367 }
Hans Verkuildb6eb5b2007-02-18 14:05:02 -03001368 case VIDIOC_G_ENC_INDEX:
1369 {
1370 struct v4l2_enc_idx *p=arg;
1371
1372 if (!vfd->vidioc_g_enc_index)
1373 break;
1374 ret=vfd->vidioc_g_enc_index(file, fh, p);
1375 if (!ret)
1376 dbgarg (cmd, "entries=%d, entries_cap=%d\n",
1377 p->entries,p->entries_cap);
1378 break;
1379 }
Hans Verkuilada6ecd2007-02-18 14:56:22 -03001380 case VIDIOC_ENCODER_CMD:
1381 {
1382 struct v4l2_encoder_cmd *p=arg;
1383
1384 if (!vfd->vidioc_encoder_cmd)
1385 break;
1386 ret=vfd->vidioc_encoder_cmd(file, fh, p);
1387 if (!ret)
1388 dbgarg (cmd, "cmd=%d, flags=%d\n",
1389 p->cmd,p->flags);
1390 break;
1391 }
1392 case VIDIOC_TRY_ENCODER_CMD:
1393 {
1394 struct v4l2_encoder_cmd *p=arg;
1395
1396 if (!vfd->vidioc_try_encoder_cmd)
1397 break;
1398 ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
1399 if (!ret)
1400 dbgarg (cmd, "cmd=%d, flags=%d\n",
1401 p->cmd,p->flags);
1402 break;
1403 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001404 case VIDIOC_G_PARM:
1405 {
1406 struct v4l2_streamparm *p=arg;
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001407 if (vfd->vidioc_g_parm) {
1408 ret=vfd->vidioc_g_parm(file, fh, p);
1409 } else {
1410 struct v4l2_standard s;
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001411
1412 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1413 return -EINVAL;
1414
Jonathan Corbet83427ac2006-10-13 07:51:16 -03001415 v4l2_video_std_construct(&s, vfd->current_norm,
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001416 v4l2_norm_to_name(vfd->current_norm));
Mauro Carvalho Chehab1c2d0342006-09-14 13:36:34 -03001417
1418 memset(p,0,sizeof(*p));
1419
1420 p->parm.capture.timeperframe = s.frameperiod;
1421 ret=0;
1422 }
1423
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001424 dbgarg (cmd, "type=%d\n", p->type);
1425 break;
1426 }
1427 case VIDIOC_S_PARM:
1428 {
1429 struct v4l2_streamparm *p=arg;
1430 if (!vfd->vidioc_s_parm)
1431 break;
1432 dbgarg (cmd, "type=%d\n", p->type);
1433 ret=vfd->vidioc_s_parm(file, fh, p);
1434 break;
1435 }
1436 case VIDIOC_G_TUNER:
1437 {
1438 struct v4l2_tuner *p=arg;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001439 __u32 index=p->index;
1440
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001441 if (!vfd->vidioc_g_tuner)
1442 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001443
1444 memset(p,0,sizeof(*p));
1445 p->index=index;
1446
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001447 ret=vfd->vidioc_g_tuner(file, fh, p);
1448 if (!ret)
1449 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1450 "capability=%d, rangelow=%d, "
1451 "rangehigh=%d, signal=%d, afc=%d, "
1452 "rxsubchans=%d, audmode=%d\n",
1453 p->index, p->name, p->type,
1454 p->capability, p->rangelow,
1455 p->rangehigh, p->rxsubchans,
1456 p->audmode, p->signal, p->afc);
1457 break;
1458 }
1459 case VIDIOC_S_TUNER:
1460 {
1461 struct v4l2_tuner *p=arg;
1462 if (!vfd->vidioc_s_tuner)
1463 break;
1464 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1465 "capability=%d, rangelow=%d, rangehigh=%d, "
1466 "signal=%d, afc=%d, rxsubchans=%d, "
1467 "audmode=%d\n",p->index, p->name, p->type,
1468 p->capability, p->rangelow,p->rangehigh,
1469 p->rxsubchans, p->audmode, p->signal,
1470 p->afc);
1471 ret=vfd->vidioc_s_tuner(file, fh, p);
1472 break;
1473 }
1474 case VIDIOC_G_FREQUENCY:
1475 {
1476 struct v4l2_frequency *p=arg;
1477 if (!vfd->vidioc_g_frequency)
1478 break;
Mauro Carvalho Chehab7964b1b2006-11-20 12:10:43 -03001479
1480 memset(p,0,sizeof(*p));
1481
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001482 ret=vfd->vidioc_g_frequency(file, fh, p);
1483 if (!ret)
1484 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1485 p->tuner,p->type,p->frequency);
1486 break;
1487 }
1488 case VIDIOC_S_FREQUENCY:
1489 {
1490 struct v4l2_frequency *p=arg;
1491 if (!vfd->vidioc_s_frequency)
1492 break;
1493 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1494 p->tuner,p->type,p->frequency);
1495 ret=vfd->vidioc_s_frequency(file, fh, p);
1496 break;
1497 }
1498 case VIDIOC_G_SLICED_VBI_CAP:
1499 {
1500 struct v4l2_sliced_vbi_cap *p=arg;
1501 if (!vfd->vidioc_g_sliced_vbi_cap)
1502 break;
1503 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1504 if (!ret)
1505 dbgarg (cmd, "service_set=%d\n", p->service_set);
1506 break;
1507 }
1508 case VIDIOC_LOG_STATUS:
1509 {
1510 if (!vfd->vidioc_log_status)
1511 break;
1512 ret=vfd->vidioc_log_status(file, fh);
1513 break;
1514 }
Trent Piephodbbff482007-01-22 23:31:53 -03001515#ifdef CONFIG_VIDEO_ADV_DEBUG
Trent Piepho52ebc762007-01-23 22:38:13 -03001516 case VIDIOC_DBG_G_REGISTER:
Trent Piephodbbff482007-01-22 23:31:53 -03001517 {
1518 struct v4l2_register *p=arg;
Trent Piepho62d50ad2007-01-30 23:25:41 -03001519 if (!capable(CAP_SYS_ADMIN))
1520 ret=-EPERM;
1521 else if (vfd->vidioc_g_register)
Trent Piephodbbff482007-01-22 23:31:53 -03001522 ret=vfd->vidioc_g_register(file, fh, p);
1523 break;
1524 }
Trent Piepho52ebc762007-01-23 22:38:13 -03001525 case VIDIOC_DBG_S_REGISTER:
Trent Piephodbbff482007-01-22 23:31:53 -03001526 {
1527 struct v4l2_register *p=arg;
Trent Piepho52ebc762007-01-23 22:38:13 -03001528 if (!capable(CAP_SYS_ADMIN))
1529 ret=-EPERM;
1530 else if (vfd->vidioc_s_register)
Trent Piephodbbff482007-01-22 23:31:53 -03001531 ret=vfd->vidioc_s_register(file, fh, p);
1532 break;
1533 }
1534#endif
Mauro Carvalho Chehab207705c2006-11-20 12:13:25 -03001535 } /* switch */
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001536
1537 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1538 if (ret<0) {
1539 printk ("%s: err:\n", vfd->name);
1540 v4l_print_ioctl(vfd->name, cmd);
1541 }
1542 }
1543
1544 return ret;
1545}
1546
1547int video_ioctl2 (struct inode *inode, struct file *file,
1548 unsigned int cmd, unsigned long arg)
1549{
1550 char sbuf[128];
1551 void *mbuf = NULL;
1552 void *parg = NULL;
1553 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001554 int is_ext_ctrl;
1555 size_t ctrls_size = 0;
1556 void __user *user_ptr = NULL;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001557
1558#ifdef __OLD_VIDIOC_
1559 cmd = video_fix_command(cmd);
1560#endif
Hans Verkuil05976912006-06-18 13:43:28 -03001561 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1562 cmd == VIDIOC_TRY_EXT_CTRLS);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001563
1564 /* Copy arguments into temp kernel buffer */
1565 switch (_IOC_DIR(cmd)) {
1566 case _IOC_NONE:
1567 parg = NULL;
1568 break;
1569 case _IOC_READ:
1570 case _IOC_WRITE:
1571 case (_IOC_WRITE | _IOC_READ):
1572 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1573 parg = sbuf;
1574 } else {
1575 /* too big to allocate from stack */
1576 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1577 if (NULL == mbuf)
1578 return -ENOMEM;
1579 parg = mbuf;
1580 }
1581
1582 err = -EFAULT;
1583 if (_IOC_DIR(cmd) & _IOC_WRITE)
1584 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1585 goto out;
1586 break;
1587 }
1588
Hans Verkuil05976912006-06-18 13:43:28 -03001589 if (is_ext_ctrl) {
1590 struct v4l2_ext_controls *p = parg;
1591
1592 /* In case of an error, tell the caller that it wasn't
1593 a specific control that caused it. */
1594 p->error_idx = p->count;
1595 user_ptr = (void __user *)p->controls;
1596 if (p->count) {
1597 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1598 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1599 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1600 err = -ENOMEM;
1601 if (NULL == mbuf)
1602 goto out_ext_ctrl;
1603 err = -EFAULT;
1604 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1605 goto out_ext_ctrl;
1606 p->controls = mbuf;
1607 }
1608 }
1609
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001610 /* Handles IOCTL */
1611 err = __video_do_ioctl(inode, file, cmd, parg);
1612 if (err == -ENOIOCTLCMD)
1613 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001614 if (is_ext_ctrl) {
1615 struct v4l2_ext_controls *p = parg;
1616
1617 p->controls = (void *)user_ptr;
1618 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1619 err = -EFAULT;
1620 goto out_ext_ctrl;
1621 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001622 if (err < 0)
1623 goto out;
1624
Hans Verkuil05976912006-06-18 13:43:28 -03001625out_ext_ctrl:
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001626 /* Copy results into user buffer */
1627 switch (_IOC_DIR(cmd))
1628 {
1629 case _IOC_READ:
1630 case (_IOC_WRITE | _IOC_READ):
1631 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1632 err = -EFAULT;
1633 break;
1634 }
1635
1636out:
1637 kfree(mbuf);
1638 return err;
1639}
1640
1641
Arjan van de Venfa027c22007-02-12 00:55:33 -08001642static const struct file_operations video_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644/**
1645 * video_register_device - register video4linux devices
1646 * @vfd: video device structure we want to register
1647 * @type: type of device to register
1648 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
1649 * -1 == first free)
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001650 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * The registration code assigns minor numbers based on the type
1652 * requested. -ENFILE is returned in all the device slots for this
1653 * category are full. If not then the minor field is set and the
1654 * driver initialize function is called (if non %NULL).
1655 *
1656 * Zero is returned on success.
1657 *
1658 * Valid types are
1659 *
1660 * %VFL_TYPE_GRABBER - A frame grabber
1661 *
1662 * %VFL_TYPE_VTX - A teletext device
1663 *
1664 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1665 *
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001666 * %VFL_TYPE_RADIO - A radio card
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
1668
1669int video_register_device(struct video_device *vfd, int type, int nr)
1670{
1671 int i=0;
1672 int base;
1673 int end;
Michael Krufky3117bee2006-07-19 13:23:38 -03001674 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 char *name_base;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 switch(type)
1678 {
1679 case VFL_TYPE_GRABBER:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001680 base=MINOR_VFL_TYPE_GRABBER_MIN;
1681 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 name_base = "video";
1683 break;
1684 case VFL_TYPE_VTX:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001685 base=MINOR_VFL_TYPE_VTX_MIN;
1686 end=MINOR_VFL_TYPE_VTX_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 name_base = "vtx";
1688 break;
1689 case VFL_TYPE_VBI:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001690 base=MINOR_VFL_TYPE_VBI_MIN;
1691 end=MINOR_VFL_TYPE_VBI_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 name_base = "vbi";
1693 break;
1694 case VFL_TYPE_RADIO:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001695 base=MINOR_VFL_TYPE_RADIO_MIN;
1696 end=MINOR_VFL_TYPE_RADIO_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 name_base = "radio";
1698 break;
1699 default:
Trent Piepho53dd8de2006-07-25 09:31:42 -03001700 printk(KERN_ERR "%s called with unknown type: %d\n",
1701 __FUNCTION__, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return -1;
1703 }
1704
1705 /* pick a minor number */
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001706 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (nr >= 0 && nr < end-base) {
1708 /* use the one the driver asked for */
1709 i = base+nr;
1710 if (NULL != video_device[i]) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001711 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return -ENFILE;
1713 }
1714 } else {
1715 /* use first free */
1716 for(i=base;i<end;i++)
1717 if (NULL == video_device[i])
1718 break;
1719 if (i == end) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001720 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -ENFILE;
1722 }
1723 }
1724 video_device[i]=vfd;
1725 vfd->minor=i;
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001726 mutex_unlock(&videodev_lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001727 mutex_init(&vfd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* sysfs class */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001730 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (vfd->dev)
1732 vfd->class_dev.dev = vfd->dev;
1733 vfd->class_dev.class = &video_class;
Michael Krufky50c25ff2006-01-09 15:25:34 -02001734 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Greg Kroah-Hartman5e483072005-06-20 21:15:16 -07001735 sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
Michael Krufky3117bee2006-07-19 13:23:38 -03001736 ret = class_device_register(&vfd->class_dev);
Trent Piepho8c313112006-07-25 20:37:03 -03001737 if (ret < 0) {
Michael Krufky3117bee2006-07-19 13:23:38 -03001738 printk(KERN_ERR "%s: class_device_register failed\n",
1739 __FUNCTION__);
Trent Piephod94fc9a2006-07-29 17:18:06 -03001740 goto fail_minor;
Michael Krufky3117bee2006-07-19 13:23:38 -03001741 }
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001742 ret = class_device_create_file(&vfd->class_dev, &class_device_attr_name);
1743 if (ret < 0) {
Trent Piephod94fc9a2006-07-29 17:18:06 -03001744 printk(KERN_ERR "%s: class_device_create_file 'name' failed\n",
1745 __FUNCTION__);
1746 goto fail_classdev;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -03001747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -02001749#if 1
1750 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (!vfd->release)
1752 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1753 "Please fix your driver for proper sysfs support, see "
1754 "http://lwn.net/Articles/36850/\n", vfd->name);
1755#endif
1756 return 0;
Trent Piepho53dd8de2006-07-25 09:31:42 -03001757
1758fail_classdev:
1759 class_device_unregister(&vfd->class_dev);
1760fail_minor:
1761 mutex_lock(&videodev_lock);
1762 video_device[vfd->minor] = NULL;
1763 vfd->minor = -1;
1764 mutex_unlock(&videodev_lock);
1765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768/**
1769 * video_unregister_device - unregister a video4linux device
1770 * @vfd: the device to unregister
1771 *
1772 * This unregisters the passed device and deassigns the minor
1773 * number. Future open calls will be met with errors.
1774 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776void video_unregister_device(struct video_device *vfd)
1777{
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001778 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if(video_device[vfd->minor]!=vfd)
1780 panic("videodev: bad unregister");
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 video_device[vfd->minor]=NULL;
1783 class_device_unregister(&vfd->class_dev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001784 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001787/*
1788 * Video fs operations
1789 */
Arjan van de Venfa027c22007-02-12 00:55:33 -08001790static const struct file_operations video_fops=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 .owner = THIS_MODULE,
1793 .llseek = no_llseek,
1794 .open = video_open,
1795};
1796
1797/*
1798 * Initialise video for linux
1799 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801static int __init videodev_init(void)
1802{
1803 int ret;
1804
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001805 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1807 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1808 return -EIO;
1809 }
1810
1811 ret = class_register(&video_class);
1812 if (ret < 0) {
1813 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1814 printk(KERN_WARNING "video_dev: class_register failed\n");
1815 return -EIO;
1816 }
1817
1818 return 0;
1819}
1820
1821static void __exit videodev_exit(void)
1822{
1823 class_unregister(&video_class);
1824 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1825}
1826
1827module_init(videodev_init)
1828module_exit(videodev_exit)
1829
1830EXPORT_SYMBOL(video_register_device);
1831EXPORT_SYMBOL(video_unregister_device);
1832EXPORT_SYMBOL(video_devdata);
1833EXPORT_SYMBOL(video_usercopy);
1834EXPORT_SYMBOL(video_exclusive_open);
1835EXPORT_SYMBOL(video_exclusive_release);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001836EXPORT_SYMBOL(video_ioctl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837EXPORT_SYMBOL(video_device_alloc);
1838EXPORT_SYMBOL(video_device_release);
1839
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001840MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1841MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842MODULE_LICENSE("GPL");
1843
1844
1845/*
1846 * Local variables:
1847 * c-basic-offset: 8
1848 * End:
1849 */