blob: b26ebaff226f49182537c0a350ff4189969eaddf [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...) \
20 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
21 printk (KERN_DEBUG "%s: ", vfd->name); \
22 v4l_printk_ioctl(cmd); \
23 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
24
25#define dbgarg2(fmt, arg...) \
26 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
27 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/types.h>
31#include <linux/kernel.h>
32#include <linux/sched.h>
33#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{
107 return video_device[iminor(file->f_dentry->d_inode)];
108}
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",
321 [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",
333};
334
335#define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
336
337static void dbgbuf(unsigned int cmd, struct video_device *vfd,
338 struct v4l2_buffer *p)
339{
340 struct v4l2_timecode *tc=&p->timecode;
341
342 dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
343 "bytesused=%d, flags=0x%08d, "
344 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx\n",
345 (p->timestamp.tv_sec/3600),
346 (int)(p->timestamp.tv_sec/60)%60,
347 (int)(p->timestamp.tv_sec%60),
348 p->timestamp.tv_usec,
349 p->index,
350 prt_names(p->type,v4l2_type_names_FIXME),
351 p->bytesused,p->flags,
352 p->field,p->sequence,
353 prt_names(p->memory,v4l2_memory_names),
354 p->m.userptr);
355 dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
356 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
357 tc->hours,tc->minutes,tc->seconds,
Mauro Carvalho Chehabc18cb012006-06-23 07:05:22 -0300358 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300359}
360
361static inline void dbgrect(struct video_device *vfd, char *s,
362 struct v4l2_rect *r)
363{
364 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
365 r->width, r->height);
366};
367
368static inline void v4l_print_pix_fmt (struct video_device *vfd,
369 struct v4l2_pix_format *fmt)
370{
371 dbgarg2 ("width=%d, height=%d, format=0x%08x, field=%s, "
372 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
373 fmt->width,fmt->height,fmt->pixelformat,
374 prt_names(fmt->field,v4l2_field_names_FIXME),
375 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
376};
377
378
379static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
380{
381 switch (type) {
382 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
383 if (vfd->vidioc_try_fmt_cap)
384 return (0);
385 break;
386 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
387 if (vfd->vidioc_try_fmt_overlay)
388 return (0);
389 break;
390 case V4L2_BUF_TYPE_VBI_CAPTURE:
391 if (vfd->vidioc_try_fmt_vbi)
392 return (0);
393 break;
394 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
395 if (vfd->vidioc_try_fmt_vbi_output)
396 return (0);
397 break;
398 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
399 if (vfd->vidioc_try_fmt_vbi_capture)
400 return (0);
401 break;
402 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
403 if (vfd->vidioc_try_fmt_video_output)
404 return (0);
405 break;
406 case V4L2_BUF_TYPE_VBI_OUTPUT:
407 if (vfd->vidioc_try_fmt_vbi_output)
408 return (0);
409 break;
410 case V4L2_BUF_TYPE_PRIVATE:
411 if (vfd->vidioc_try_fmt_type_private)
412 return (0);
413 break;
414 }
415 return (-EINVAL);
416}
417
418static int __video_do_ioctl(struct inode *inode, struct file *file,
419 unsigned int cmd, void *arg)
420{
421 struct video_device *vfd = video_devdata(file);
422 void *fh = file->private_data;
423 int ret = -EINVAL;
424
425 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
426 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
427 v4l_print_ioctl(vfd->name, cmd);
428 }
429
430 switch(cmd) {
431 /* --- capabilities ------------------------------------------ */
432 case VIDIOC_QUERYCAP:
433 {
434 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
435 memset(cap, 0, sizeof(*cap));
436
437 if (!vfd->vidioc_querycap)
438 break;
439
440 ret=vfd->vidioc_querycap(file, fh, cap);
441 if (!ret)
442 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
443 "version=0x%08x, "
444 "capabilities=0x%08x\n",
445 cap->driver,cap->card,cap->bus_info,
446 cap->version,
447 cap->capabilities);
448 break;
449 }
450
451 /* --- priority ------------------------------------------ */
452 case VIDIOC_G_PRIORITY:
453 {
454 enum v4l2_priority *p=arg;
455
456 if (!vfd->vidioc_g_priority)
457 break;
458 ret=vfd->vidioc_g_priority(file, fh, p);
459 if (!ret)
460 dbgarg(cmd, "priority is %d\n", *p);
461 break;
462 }
463 case VIDIOC_S_PRIORITY:
464 {
465 enum v4l2_priority *p=arg;
466
467 if (!vfd->vidioc_s_priority)
468 break;
469 dbgarg(cmd, "setting priority to %d\n", *p);
470 ret=vfd->vidioc_s_priority(file, fh, *p);
471 break;
472 }
473
474 /* --- capture ioctls ---------------------------------------- */
475 case VIDIOC_ENUM_FMT:
476 {
477 struct v4l2_fmtdesc *f = arg;
478 enum v4l2_buf_type type;
479 unsigned int index;
480
481 index = f->index;
482 type = f->type;
483 memset(f,0,sizeof(*f));
484 f->index = index;
485 f->type = type;
486
487 switch (type) {
488 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
489 if (vfd->vidioc_enum_fmt_cap)
490 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
491 break;
492 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
493 if (vfd->vidioc_enum_fmt_overlay)
494 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
495 break;
496 case V4L2_BUF_TYPE_VBI_CAPTURE:
497 if (vfd->vidioc_enum_fmt_vbi)
498 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
499 break;
500 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
501 if (vfd->vidioc_enum_fmt_vbi_output)
502 ret=vfd->vidioc_enum_fmt_vbi_output(file,
503 fh, f);
504 break;
505 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
506 if (vfd->vidioc_enum_fmt_vbi_capture)
507 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
508 fh, f);
509 break;
510 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
511 if (vfd->vidioc_enum_fmt_video_output)
512 ret=vfd->vidioc_enum_fmt_video_output(file,
513 fh, f);
514 break;
515 case V4L2_BUF_TYPE_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_PRIVATE:
521 if (vfd->vidioc_enum_fmt_type_private)
522 ret=vfd->vidioc_enum_fmt_type_private(file,
523 fh, f);
524 break;
525 }
526 if (!ret)
527 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
528 "description=%s,"
529 " pixelformat=0x%8x\n",
530 f->index, f->type, f->flags,
531 f->description,
532 f->pixelformat);
533
534 break;
535 }
536 case VIDIOC_G_FMT:
537 {
538 struct v4l2_format *f = (struct v4l2_format *)arg;
539 enum v4l2_buf_type type=f->type;
540
541 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
542 f->type=type;
543
544 /* FIXME: Should be one dump per type */
545 dbgarg (cmd, "type=%s\n", prt_names(type,
546 v4l2_type_names_FIXME));
547
548 switch (type) {
549 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
550 if (vfd->vidioc_g_fmt_cap)
551 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
552 if (!ret)
553 v4l_print_pix_fmt(vfd,&f->fmt.pix);
554 break;
555 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
556 if (vfd->vidioc_g_fmt_overlay)
557 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
558 break;
559 case V4L2_BUF_TYPE_VBI_CAPTURE:
560 if (vfd->vidioc_g_fmt_vbi)
561 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
562 break;
563 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
564 if (vfd->vidioc_g_fmt_vbi_output)
565 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
566 break;
567 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
568 if (vfd->vidioc_g_fmt_vbi_capture)
569 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
570 break;
571 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
572 if (vfd->vidioc_g_fmt_video_output)
573 ret=vfd->vidioc_g_fmt_video_output(file,
574 fh, f);
575 break;
576 case V4L2_BUF_TYPE_VBI_OUTPUT:
577 if (vfd->vidioc_g_fmt_vbi_output)
578 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
579 break;
580 case V4L2_BUF_TYPE_PRIVATE:
581 if (vfd->vidioc_g_fmt_type_private)
582 ret=vfd->vidioc_g_fmt_type_private(file,
583 fh, f);
584 break;
585 }
586
587 break;
588 }
589 case VIDIOC_S_FMT:
590 {
591 struct v4l2_format *f = (struct v4l2_format *)arg;
592
593 /* FIXME: Should be one dump per type */
594 dbgarg (cmd, "type=%s\n", prt_names(f->type,
595 v4l2_type_names_FIXME));
596
597 switch (f->type) {
598 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
599 v4l_print_pix_fmt(vfd,&f->fmt.pix);
600 if (vfd->vidioc_s_fmt_cap)
601 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
602 break;
603 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
604 if (vfd->vidioc_s_fmt_overlay)
605 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
606 break;
607 case V4L2_BUF_TYPE_VBI_CAPTURE:
608 if (vfd->vidioc_s_fmt_vbi)
609 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
610 break;
611 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
612 if (vfd->vidioc_s_fmt_vbi_output)
613 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
614 break;
615 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
616 if (vfd->vidioc_s_fmt_vbi_capture)
617 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
618 break;
619 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
620 if (vfd->vidioc_s_fmt_video_output)
621 ret=vfd->vidioc_s_fmt_video_output(file,
622 fh, f);
623 break;
624 case V4L2_BUF_TYPE_VBI_OUTPUT:
625 if (vfd->vidioc_s_fmt_vbi_output)
626 ret=vfd->vidioc_s_fmt_vbi_output(file,
627 fh, f);
628 break;
629 case V4L2_BUF_TYPE_PRIVATE:
630 if (vfd->vidioc_s_fmt_type_private)
631 ret=vfd->vidioc_s_fmt_type_private(file,
632 fh, f);
633 break;
634 }
635 break;
636 }
637 case VIDIOC_TRY_FMT:
638 {
639 struct v4l2_format *f = (struct v4l2_format *)arg;
640
641 /* FIXME: Should be one dump per type */
642 dbgarg (cmd, "type=%s\n", prt_names(f->type,
643 v4l2_type_names_FIXME));
644 switch (f->type) {
645 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
646 if (vfd->vidioc_try_fmt_cap)
647 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
648 if (!ret)
649 v4l_print_pix_fmt(vfd,&f->fmt.pix);
650 break;
651 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
652 if (vfd->vidioc_try_fmt_overlay)
653 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
654 break;
655 case V4L2_BUF_TYPE_VBI_CAPTURE:
656 if (vfd->vidioc_try_fmt_vbi)
657 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
658 break;
659 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
660 if (vfd->vidioc_try_fmt_vbi_output)
661 ret=vfd->vidioc_try_fmt_vbi_output(file,
662 fh, f);
663 break;
664 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
665 if (vfd->vidioc_try_fmt_vbi_capture)
666 ret=vfd->vidioc_try_fmt_vbi_capture(file,
667 fh, f);
668 break;
669 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
670 if (vfd->vidioc_try_fmt_video_output)
671 ret=vfd->vidioc_try_fmt_video_output(file,
672 fh, f);
673 break;
674 case V4L2_BUF_TYPE_VBI_OUTPUT:
675 if (vfd->vidioc_try_fmt_vbi_output)
676 ret=vfd->vidioc_try_fmt_vbi_output(file,
677 fh, f);
678 break;
679 case V4L2_BUF_TYPE_PRIVATE:
680 if (vfd->vidioc_try_fmt_type_private)
681 ret=vfd->vidioc_try_fmt_type_private(file,
682 fh, f);
683 break;
684 }
685
686 break;
687 }
688 /* FIXME: Those buf reqs could be handled here,
689 with some changes on videobuf to allow its header to be included at
690 videodev2.h or being merged at videodev2.
691 */
692 case VIDIOC_REQBUFS:
693 {
694 struct v4l2_requestbuffers *p=arg;
695
696 if (!vfd->vidioc_reqbufs)
697 break;
698 ret = check_fmt (vfd, p->type);
699 if (ret)
700 break;
701
702 ret=vfd->vidioc_reqbufs(file, fh, p);
703 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
704 p->count,
705 prt_names(p->type,v4l2_type_names_FIXME),
706 prt_names(p->memory,v4l2_memory_names));
707 break;
708 }
709 case VIDIOC_QUERYBUF:
710 {
711 struct v4l2_buffer *p=arg;
712
713 if (!vfd->vidioc_querybuf)
714 break;
715 ret = check_fmt (vfd, p->type);
716 if (ret)
717 break;
718
719 ret=vfd->vidioc_querybuf(file, fh, p);
720 if (!ret)
721 dbgbuf(cmd,vfd,p);
722 break;
723 }
724 case VIDIOC_QBUF:
725 {
726 struct v4l2_buffer *p=arg;
727
728 if (!vfd->vidioc_qbuf)
729 break;
730 ret = check_fmt (vfd, p->type);
731 if (ret)
732 break;
733
734 ret=vfd->vidioc_qbuf(file, fh, p);
735 if (!ret)
736 dbgbuf(cmd,vfd,p);
737 break;
738 }
739 case VIDIOC_DQBUF:
740 {
741 struct v4l2_buffer *p=arg;
742 if (!vfd->vidioc_qbuf)
743 break;
744 ret = check_fmt (vfd, p->type);
745 if (ret)
746 break;
747
748 ret=vfd->vidioc_qbuf(file, fh, p);
749 if (!ret)
750 dbgbuf(cmd,vfd,p);
751 break;
752 }
753 case VIDIOC_OVERLAY:
754 {
755 int *i = arg;
756
757 if (!vfd->vidioc_overlay)
758 break;
759 dbgarg (cmd, "value=%d\n",*i);
760 ret=vfd->vidioc_overlay(file, fh, *i);
761 break;
762 }
763#ifdef HAVE_V4L1
764 /* --- streaming capture ------------------------------------- */
765 case VIDIOCGMBUF:
766 {
767 struct video_mbuf *p=arg;
768
Mauro Carvalho Chehab4ceb04e2006-06-17 08:52:30 -0300769 memset(p,0,sizeof(p));
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -0300770
771 if (!vfd->vidiocgmbuf)
772 break;
773 ret=vfd->vidiocgmbuf(file, fh, p);
774 if (!ret)
775 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
776 p->size, p->frames,
777 (unsigned long)p->offsets);
778 break;
779 }
780#endif
781 case VIDIOC_G_FBUF:
782 {
783 struct v4l2_framebuffer *p=arg;
784 if (!vfd->vidioc_g_fbuf)
785 break;
786 ret=vfd->vidioc_g_fbuf(file, fh, arg);
787 if (!ret) {
788 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
789 p->capability,p->flags,
790 (unsigned long)p->base);
791 v4l_print_pix_fmt (vfd, &p->fmt);
792 }
793 break;
794 }
795 case VIDIOC_S_FBUF:
796 {
797 struct v4l2_framebuffer *p=arg;
798 if (!vfd->vidioc_s_fbuf)
799 break;
800
801 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
802 p->capability,p->flags,(unsigned long)p->base);
803 v4l_print_pix_fmt (vfd, &p->fmt);
804 ret=vfd->vidioc_s_fbuf(file, fh, arg);
805
806 break;
807 }
808 case VIDIOC_STREAMON:
809 {
810 enum v4l2_buf_type i = *(int *)arg;
811 if (!vfd->vidioc_streamon)
812 break;
813 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
814 ret=vfd->vidioc_streamon(file, fh,i);
815 break;
816 }
817 case VIDIOC_STREAMOFF:
818 {
819 enum v4l2_buf_type i = *(int *)arg;
820
821 if (!vfd->vidioc_streamoff)
822 break;
823 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
824 ret=vfd->vidioc_streamoff(file, fh, i);
825 break;
826 }
827 /* ---------- tv norms ---------- */
828 case VIDIOC_ENUMSTD:
829 {
830 struct v4l2_standard *p = arg;
831 unsigned int index = p->index;
832
833 if (!vfd->tvnormsize) {
834 printk (KERN_WARNING "%s: no TV norms defined!\n",
835 vfd->name);
836 break;
837 }
838
839 if (index<=0 || index >= vfd->tvnormsize) {
840 ret=-EINVAL;
841 break;
842 }
843 v4l2_video_std_construct(p, vfd->tvnorms[p->index].id,
844 vfd->tvnorms[p->index].name);
845 p->index = index;
846
847 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
848 "framelines=%d\n", p->index,
849 (unsigned long long)p->id, p->name,
850 p->frameperiod.numerator,
851 p->frameperiod.denominator,
852 p->framelines);
853
854 ret=0;
855 break;
856 }
857 case VIDIOC_G_STD:
858 {
859 v4l2_std_id *id = arg;
860
861 *id = vfd->current_norm;
862
863 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
864
865 ret=0;
866 break;
867 }
868 case VIDIOC_S_STD:
869 {
870 v4l2_std_id *id = arg;
871 unsigned int i;
872
873 if (!vfd->tvnormsize) {
874 printk (KERN_WARNING "%s: no TV norms defined!\n",
875 vfd->name);
876 break;
877 }
878
879 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
880
881 /* First search for exact match */
882 for (i = 0; i < vfd->tvnormsize; i++)
883 if (*id == vfd->tvnorms[i].id)
884 break;
885 /* Then for a generic video std that contains desired std */
886 if (i == vfd->tvnormsize)
887 for (i = 0; i < vfd->tvnormsize; i++)
888 if (*id & vfd->tvnorms[i].id)
889 break;
890 if (i == vfd->tvnormsize) {
891 break;
892 }
893
894 /* Calls the specific handler */
895 if (vfd->vidioc_s_std)
896 ret=vfd->vidioc_s_std(file, fh, i);
897 else
898 ret=-EINVAL;
899
900 /* Updates standard information */
901 if (!ret)
902 vfd->current_norm=*id;
903
904 break;
905 }
906 case VIDIOC_QUERYSTD:
907 {
908 v4l2_std_id *p=arg;
909
910 if (!vfd->vidioc_querystd)
911 break;
912 ret=vfd->vidioc_querystd(file, fh, arg);
913 if (!ret)
914 dbgarg (cmd, "detected std=%Lu\n",
915 (unsigned long long)*p);
916 break;
917 }
918 /* ------ input switching ---------- */
919 /* FIXME: Inputs can be handled inside videodev2 */
920 case VIDIOC_ENUMINPUT:
921 {
922 struct v4l2_input *p=arg;
923 int i=p->index;
924
925 if (!vfd->vidioc_enum_input)
926 break;
927 memset(p, 0, sizeof(*p));
928 p->index=i;
929
930 ret=vfd->vidioc_enum_input(file, fh, p);
931 if (!ret)
932 dbgarg (cmd, "index=%d, name=%s, type=%d, "
933 "audioset=%d, "
934 "tuner=%d, std=%Ld, status=%d\n",
935 p->index,p->name,p->type,p->audioset,
936 p->tuner,
937 (unsigned long long)p->std,
938 p->status);
939 break;
940 }
941 case VIDIOC_G_INPUT:
942 {
943 unsigned int *i = arg;
944
945 if (!vfd->vidioc_g_input)
946 break;
947 ret=vfd->vidioc_g_input(file, fh, i);
948 if (!ret)
949 dbgarg (cmd, "value=%d\n",*i);
950 break;
951 }
952 case VIDIOC_S_INPUT:
953 {
954 unsigned int *i = arg;
955
956 if (!vfd->vidioc_s_input)
957 break;
958 dbgarg (cmd, "value=%d\n",*i);
959 ret=vfd->vidioc_s_input(file, fh, *i);
960 break;
961 }
962
963 /* ------ output switching ---------- */
964 case VIDIOC_G_OUTPUT:
965 {
966 unsigned int *i = arg;
967
968 if (!vfd->vidioc_g_output)
969 break;
970 ret=vfd->vidioc_g_output(file, fh, i);
971 if (!ret)
972 dbgarg (cmd, "value=%d\n",*i);
973 break;
974 }
975 case VIDIOC_S_OUTPUT:
976 {
977 unsigned int *i = arg;
978
979 if (!vfd->vidioc_s_output)
980 break;
981 dbgarg (cmd, "value=%d\n",*i);
982 ret=vfd->vidioc_s_output(file, fh, *i);
983 break;
984 }
985
986 /* --- controls ---------------------------------------------- */
987 case VIDIOC_QUERYCTRL:
988 {
989 struct v4l2_queryctrl *p=arg;
990
991 if (!vfd->vidioc_queryctrl)
992 break;
993 ret=vfd->vidioc_queryctrl(file, fh, p);
994
995 if (!ret)
996 dbgarg (cmd, "id=%d, type=%d, name=%s, "
997 "min/max=%d/%d,"
998 " step=%d, default=%d, flags=0x%08x\n",
999 p->id,p->type,p->name,p->minimum,
1000 p->maximum,p->step,p->default_value,
1001 p->flags);
1002 break;
1003 }
1004 case VIDIOC_G_CTRL:
1005 {
1006 struct v4l2_control *p = arg;
1007
1008 if (!vfd->vidioc_g_ctrl)
1009 break;
1010 dbgarg(cmd, "Enum for index=%d\n", p->id);
1011
1012 ret=vfd->vidioc_g_ctrl(file, fh, p);
1013 if (!ret)
1014 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1015 break;
1016 }
1017 case VIDIOC_S_CTRL:
1018 {
1019 struct v4l2_control *p = arg;
1020
1021 if (!vfd->vidioc_s_ctrl)
1022 break;
1023 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1024
1025 ret=vfd->vidioc_s_ctrl(file, fh, p);
1026 break;
1027 }
Hans Verkuil05976912006-06-18 13:43:28 -03001028 case VIDIOC_G_EXT_CTRLS:
1029 {
1030 struct v4l2_ext_controls *p = arg;
1031
1032 if (vfd->vidioc_g_ext_ctrls) {
1033 dbgarg(cmd, "count=%d\n", p->count);
1034
1035 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1036 }
1037 break;
1038 }
1039 case VIDIOC_S_EXT_CTRLS:
1040 {
1041 struct v4l2_ext_controls *p = arg;
1042
1043 if (vfd->vidioc_s_ext_ctrls) {
1044 dbgarg(cmd, "count=%d\n", p->count);
1045
1046 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1047 }
1048 break;
1049 }
1050 case VIDIOC_TRY_EXT_CTRLS:
1051 {
1052 struct v4l2_ext_controls *p = arg;
1053
1054 if (vfd->vidioc_try_ext_ctrls) {
1055 dbgarg(cmd, "count=%d\n", p->count);
1056
1057 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1058 }
1059 break;
1060 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001061 case VIDIOC_QUERYMENU:
1062 {
1063 struct v4l2_querymenu *p=arg;
1064 if (!vfd->vidioc_querymenu)
1065 break;
1066 ret=vfd->vidioc_querymenu(file, fh, p);
1067 if (!ret)
1068 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1069 p->id,p->index,p->name);
1070 break;
1071 }
1072 /* --- audio ---------------------------------------------- */
1073 case VIDIOC_ENUMAUDIO:
1074 {
1075 struct v4l2_audio *p=arg;
1076
1077 if (!vfd->vidioc_enumaudio)
1078 break;
1079 dbgarg(cmd, "Enum for index=%d\n", p->index);
1080 ret=vfd->vidioc_enumaudio(file, fh, p);
1081 if (!ret)
1082 dbgarg2("index=%d, name=%s, capability=%d, "
1083 "mode=%d\n",p->index,p->name,
1084 p->capability, p->mode);
1085 break;
1086 }
1087 case VIDIOC_G_AUDIO:
1088 {
1089 struct v4l2_audio *p=arg;
1090
1091 if (!vfd->vidioc_g_audio)
1092 break;
1093 dbgarg(cmd, "Get for index=%d\n", p->index);
1094 ret=vfd->vidioc_g_audio(file, fh, p);
1095 if (!ret)
1096 dbgarg2("index=%d, name=%s, capability=%d, "
1097 "mode=%d\n",p->index,
1098 p->name,p->capability, p->mode);
1099 break;
1100 }
1101 case VIDIOC_S_AUDIO:
1102 {
1103 struct v4l2_audio *p=arg;
1104
1105 if (!vfd->vidioc_s_audio)
1106 break;
1107 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1108 "mode=%d\n", p->index, p->name,
1109 p->capability, p->mode);
1110 ret=vfd->vidioc_s_audio(file, fh, p);
1111 break;
1112 }
1113 case VIDIOC_ENUMAUDOUT:
1114 {
1115 struct v4l2_audioout *p=arg;
1116
1117 if (!vfd->vidioc_enumaudout)
1118 break;
1119 dbgarg(cmd, "Enum for index=%d\n", p->index);
1120 ret=vfd->vidioc_enumaudout(file, fh, p);
1121 if (!ret)
1122 dbgarg2("index=%d, name=%s, capability=%d, "
1123 "mode=%d\n", p->index, p->name,
1124 p->capability,p->mode);
1125 break;
1126 }
1127 case VIDIOC_G_AUDOUT:
1128 {
1129 struct v4l2_audioout *p=arg;
1130
1131 if (!vfd->vidioc_g_audout)
1132 break;
1133 dbgarg(cmd, "Enum for index=%d\n", p->index);
1134 ret=vfd->vidioc_g_audout(file, fh, p);
1135 if (!ret)
1136 dbgarg2("index=%d, name=%s, capability=%d, "
1137 "mode=%d\n", p->index, p->name,
1138 p->capability,p->mode);
1139 break;
1140 }
1141 case VIDIOC_S_AUDOUT:
1142 {
1143 struct v4l2_audioout *p=arg;
1144
1145 if (!vfd->vidioc_s_audout)
1146 break;
1147 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1148 "mode=%d\n", p->index, p->name,
1149 p->capability,p->mode);
1150
1151 ret=vfd->vidioc_s_audout(file, fh, p);
1152 break;
1153 }
1154 case VIDIOC_G_MODULATOR:
1155 {
1156 struct v4l2_modulator *p=arg;
1157 if (!vfd->vidioc_g_modulator)
1158 break;
1159 ret=vfd->vidioc_g_modulator(file, fh, p);
1160 if (!ret)
1161 dbgarg(cmd, "index=%d, name=%s, "
1162 "capability=%d, rangelow=%d,"
1163 " rangehigh=%d, txsubchans=%d\n",
1164 p->index, p->name,p->capability,
1165 p->rangelow, p->rangehigh,
1166 p->txsubchans);
1167 break;
1168 }
1169 case VIDIOC_S_MODULATOR:
1170 {
1171 struct v4l2_modulator *p=arg;
1172 if (!vfd->vidioc_s_modulator)
1173 break;
1174 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1175 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1176 p->index, p->name,p->capability,p->rangelow,
1177 p->rangehigh,p->txsubchans);
1178 ret=vfd->vidioc_s_modulator(file, fh, p);
1179 break;
1180 }
1181 case VIDIOC_G_CROP:
1182 {
1183 struct v4l2_crop *p=arg;
1184 if (!vfd->vidioc_g_crop)
1185 break;
1186 ret=vfd->vidioc_g_crop(file, fh, p);
1187 if (!ret) {
1188 dbgarg(cmd, "type=%d\n", p->type);
1189 dbgrect(vfd, "", &p->c);
1190 }
1191 break;
1192 }
1193 case VIDIOC_S_CROP:
1194 {
1195 struct v4l2_crop *p=arg;
1196 if (!vfd->vidioc_s_crop)
1197 break;
1198 dbgarg(cmd, "type=%d\n", p->type);
1199 dbgrect(vfd, "", &p->c);
1200 ret=vfd->vidioc_s_crop(file, fh, p);
1201 break;
1202 }
1203 case VIDIOC_CROPCAP:
1204 {
1205 struct v4l2_cropcap *p=arg;
1206 /*FIXME: Should also show v4l2_fract pixelaspect */
1207 if (!vfd->vidioc_cropcap)
1208 break;
1209 dbgarg(cmd, "type=%d\n", p->type);
1210 dbgrect(vfd, "bounds ", &p->bounds);
1211 dbgrect(vfd, "defrect ", &p->defrect);
1212 ret=vfd->vidioc_cropcap(file, fh, p);
1213 break;
1214 }
1215 case VIDIOC_G_MPEGCOMP:
1216 {
1217 struct v4l2_mpeg_compression *p=arg;
Hans Verkuilf81cf752006-06-18 16:54:20 -03001218
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001219 /*FIXME: Several fields not shown */
1220 if (!vfd->vidioc_g_mpegcomp)
1221 break;
1222 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1223 if (!ret)
1224 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1225 " ts_pid_video=%d, ts_pid_pcr=%d, "
1226 "ps_size=%d, au_sample_rate=%d, "
1227 "au_pesid=%c, vi_frame_rate=%d, "
1228 "vi_frames_per_gop=%d, "
1229 "vi_bframes_count=%d, vi_pesid=%c\n",
1230 p->ts_pid_pmt,p->ts_pid_audio,
1231 p->ts_pid_video,p->ts_pid_pcr,
1232 p->ps_size, p->au_sample_rate,
1233 p->au_pesid, p->vi_frame_rate,
1234 p->vi_frames_per_gop,
1235 p->vi_bframes_count, p->vi_pesid);
1236 break;
1237 }
1238 case VIDIOC_S_MPEGCOMP:
1239 {
1240 struct v4l2_mpeg_compression *p=arg;
1241 /*FIXME: Several fields not shown */
1242 if (!vfd->vidioc_s_mpegcomp)
1243 break;
1244 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1245 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1246 "au_sample_rate=%d, au_pesid=%c, "
1247 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1248 "vi_bframes_count=%d, vi_pesid=%c\n",
1249 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1250 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1251 p->au_pesid, p->vi_frame_rate,
1252 p->vi_frames_per_gop, p->vi_bframes_count,
1253 p->vi_pesid);
1254 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1255 break;
1256 }
1257 case VIDIOC_G_JPEGCOMP:
1258 {
1259 struct v4l2_jpegcompression *p=arg;
1260 if (!vfd->vidioc_g_jpegcomp)
1261 break;
1262 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1263 if (!ret)
1264 dbgarg (cmd, "quality=%d, APPn=%d, "
1265 "APP_len=%d, COM_len=%d, "
1266 "jpeg_markers=%d\n",
1267 p->quality,p->APPn,p->APP_len,
1268 p->COM_len,p->jpeg_markers);
1269 break;
1270 }
1271 case VIDIOC_S_JPEGCOMP:
1272 {
1273 struct v4l2_jpegcompression *p=arg;
1274 if (!vfd->vidioc_g_jpegcomp)
1275 break;
1276 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1277 "COM_len=%d, jpeg_markers=%d\n",
1278 p->quality,p->APPn,p->APP_len,
1279 p->COM_len,p->jpeg_markers);
1280 ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1281 break;
1282 }
1283 case VIDIOC_G_PARM:
1284 {
1285 struct v4l2_streamparm *p=arg;
1286 if (!vfd->vidioc_g_parm)
1287 break;
1288 ret=vfd->vidioc_g_parm(file, fh, p);
1289 dbgarg (cmd, "type=%d\n", p->type);
1290 break;
1291 }
1292 case VIDIOC_S_PARM:
1293 {
1294 struct v4l2_streamparm *p=arg;
1295 if (!vfd->vidioc_s_parm)
1296 break;
1297 dbgarg (cmd, "type=%d\n", p->type);
1298 ret=vfd->vidioc_s_parm(file, fh, p);
1299 break;
1300 }
1301 case VIDIOC_G_TUNER:
1302 {
1303 struct v4l2_tuner *p=arg;
1304 if (!vfd->vidioc_g_tuner)
1305 break;
1306 ret=vfd->vidioc_g_tuner(file, fh, p);
1307 if (!ret)
1308 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1309 "capability=%d, rangelow=%d, "
1310 "rangehigh=%d, signal=%d, afc=%d, "
1311 "rxsubchans=%d, audmode=%d\n",
1312 p->index, p->name, p->type,
1313 p->capability, p->rangelow,
1314 p->rangehigh, p->rxsubchans,
1315 p->audmode, p->signal, p->afc);
1316 break;
1317 }
1318 case VIDIOC_S_TUNER:
1319 {
1320 struct v4l2_tuner *p=arg;
1321 if (!vfd->vidioc_s_tuner)
1322 break;
1323 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1324 "capability=%d, rangelow=%d, rangehigh=%d, "
1325 "signal=%d, afc=%d, rxsubchans=%d, "
1326 "audmode=%d\n",p->index, p->name, p->type,
1327 p->capability, p->rangelow,p->rangehigh,
1328 p->rxsubchans, p->audmode, p->signal,
1329 p->afc);
1330 ret=vfd->vidioc_s_tuner(file, fh, p);
1331 break;
1332 }
1333 case VIDIOC_G_FREQUENCY:
1334 {
1335 struct v4l2_frequency *p=arg;
1336 if (!vfd->vidioc_g_frequency)
1337 break;
1338 ret=vfd->vidioc_g_frequency(file, fh, p);
1339 if (!ret)
1340 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1341 p->tuner,p->type,p->frequency);
1342 break;
1343 }
1344 case VIDIOC_S_FREQUENCY:
1345 {
1346 struct v4l2_frequency *p=arg;
1347 if (!vfd->vidioc_s_frequency)
1348 break;
1349 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1350 p->tuner,p->type,p->frequency);
1351 ret=vfd->vidioc_s_frequency(file, fh, p);
1352 break;
1353 }
1354 case VIDIOC_G_SLICED_VBI_CAP:
1355 {
1356 struct v4l2_sliced_vbi_cap *p=arg;
1357 if (!vfd->vidioc_g_sliced_vbi_cap)
1358 break;
1359 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1360 if (!ret)
1361 dbgarg (cmd, "service_set=%d\n", p->service_set);
1362 break;
1363 }
1364 case VIDIOC_LOG_STATUS:
1365 {
1366 if (!vfd->vidioc_log_status)
1367 break;
1368 ret=vfd->vidioc_log_status(file, fh);
1369 break;
1370 }
1371
1372 /* --- Others --------------------------------------------- */
1373
1374 default:
1375 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,__video_do_ioctl);
1376 }
1377
1378 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1379 if (ret<0) {
1380 printk ("%s: err:\n", vfd->name);
1381 v4l_print_ioctl(vfd->name, cmd);
1382 }
1383 }
1384
1385 return ret;
1386}
1387
1388int video_ioctl2 (struct inode *inode, struct file *file,
1389 unsigned int cmd, unsigned long arg)
1390{
1391 char sbuf[128];
1392 void *mbuf = NULL;
1393 void *parg = NULL;
1394 int err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001395 int is_ext_ctrl;
1396 size_t ctrls_size = 0;
1397 void __user *user_ptr = NULL;
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001398
1399#ifdef __OLD_VIDIOC_
1400 cmd = video_fix_command(cmd);
1401#endif
Hans Verkuil05976912006-06-18 13:43:28 -03001402 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1403 cmd == VIDIOC_TRY_EXT_CTRLS);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001404
1405 /* Copy arguments into temp kernel buffer */
1406 switch (_IOC_DIR(cmd)) {
1407 case _IOC_NONE:
1408 parg = NULL;
1409 break;
1410 case _IOC_READ:
1411 case _IOC_WRITE:
1412 case (_IOC_WRITE | _IOC_READ):
1413 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1414 parg = sbuf;
1415 } else {
1416 /* too big to allocate from stack */
1417 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1418 if (NULL == mbuf)
1419 return -ENOMEM;
1420 parg = mbuf;
1421 }
1422
1423 err = -EFAULT;
1424 if (_IOC_DIR(cmd) & _IOC_WRITE)
1425 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1426 goto out;
1427 break;
1428 }
1429
Hans Verkuil05976912006-06-18 13:43:28 -03001430 if (is_ext_ctrl) {
1431 struct v4l2_ext_controls *p = parg;
1432
1433 /* In case of an error, tell the caller that it wasn't
1434 a specific control that caused it. */
1435 p->error_idx = p->count;
1436 user_ptr = (void __user *)p->controls;
1437 if (p->count) {
1438 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1439 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1440 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1441 err = -ENOMEM;
1442 if (NULL == mbuf)
1443 goto out_ext_ctrl;
1444 err = -EFAULT;
1445 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1446 goto out_ext_ctrl;
1447 p->controls = mbuf;
1448 }
1449 }
1450
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001451 /* Handles IOCTL */
1452 err = __video_do_ioctl(inode, file, cmd, parg);
1453 if (err == -ENOIOCTLCMD)
1454 err = -EINVAL;
Hans Verkuil05976912006-06-18 13:43:28 -03001455 if (is_ext_ctrl) {
1456 struct v4l2_ext_controls *p = parg;
1457
1458 p->controls = (void *)user_ptr;
1459 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1460 err = -EFAULT;
1461 goto out_ext_ctrl;
1462 }
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001463 if (err < 0)
1464 goto out;
1465
Hans Verkuil05976912006-06-18 13:43:28 -03001466out_ext_ctrl:
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001467 /* Copy results into user buffer */
1468 switch (_IOC_DIR(cmd))
1469 {
1470 case _IOC_READ:
1471 case (_IOC_WRITE | _IOC_READ):
1472 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1473 err = -EFAULT;
1474 break;
1475 }
1476
1477out:
1478 kfree(mbuf);
1479 return err;
1480}
1481
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483static struct file_operations video_fops;
1484
1485/**
1486 * video_register_device - register video4linux devices
1487 * @vfd: video device structure we want to register
1488 * @type: type of device to register
1489 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
1490 * -1 == first free)
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001491 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * The registration code assigns minor numbers based on the type
1493 * requested. -ENFILE is returned in all the device slots for this
1494 * category are full. If not then the minor field is set and the
1495 * driver initialize function is called (if non %NULL).
1496 *
1497 * Zero is returned on success.
1498 *
1499 * Valid types are
1500 *
1501 * %VFL_TYPE_GRABBER - A frame grabber
1502 *
1503 * %VFL_TYPE_VTX - A teletext device
1504 *
1505 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1506 *
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001507 * %VFL_TYPE_RADIO - A radio card
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 */
1509
1510int video_register_device(struct video_device *vfd, int type, int nr)
1511{
1512 int i=0;
1513 int base;
1514 int end;
1515 char *name_base;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 switch(type)
1518 {
1519 case VFL_TYPE_GRABBER:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001520 base=MINOR_VFL_TYPE_GRABBER_MIN;
1521 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 name_base = "video";
1523 break;
1524 case VFL_TYPE_VTX:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001525 base=MINOR_VFL_TYPE_VTX_MIN;
1526 end=MINOR_VFL_TYPE_VTX_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 name_base = "vtx";
1528 break;
1529 case VFL_TYPE_VBI:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001530 base=MINOR_VFL_TYPE_VBI_MIN;
1531 end=MINOR_VFL_TYPE_VBI_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 name_base = "vbi";
1533 break;
1534 case VFL_TYPE_RADIO:
Mauro Carvalho Chehab4d0dddb2006-01-23 17:11:07 -02001535 base=MINOR_VFL_TYPE_RADIO_MIN;
1536 end=MINOR_VFL_TYPE_RADIO_MAX+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 name_base = "radio";
1538 break;
1539 default:
1540 return -1;
1541 }
1542
1543 /* pick a minor number */
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001544 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (nr >= 0 && nr < end-base) {
1546 /* use the one the driver asked for */
1547 i = base+nr;
1548 if (NULL != video_device[i]) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001549 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return -ENFILE;
1551 }
1552 } else {
1553 /* use first free */
1554 for(i=base;i<end;i++)
1555 if (NULL == video_device[i])
1556 break;
1557 if (i == end) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001558 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -ENFILE;
1560 }
1561 }
1562 video_device[i]=vfd;
1563 vfd->minor=i;
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001564 mutex_unlock(&videodev_lock);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001565 mutex_init(&vfd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /* sysfs class */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001568 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (vfd->dev)
1570 vfd->class_dev.dev = vfd->dev;
1571 vfd->class_dev.class = &video_class;
Michael Krufky50c25ff2006-01-09 15:25:34 -02001572 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Greg Kroah-Hartman5e483072005-06-20 21:15:16 -07001573 sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 class_device_register(&vfd->class_dev);
1575 class_device_create_file(&vfd->class_dev,
Michael Krufky50c25ff2006-01-09 15:25:34 -02001576 &class_device_attr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -02001578#if 1
1579 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (!vfd->release)
1581 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1582 "Please fix your driver for proper sysfs support, see "
1583 "http://lwn.net/Articles/36850/\n", vfd->name);
1584#endif
1585 return 0;
1586}
1587
1588/**
1589 * video_unregister_device - unregister a video4linux device
1590 * @vfd: the device to unregister
1591 *
1592 * This unregisters the passed device and deassigns the minor
1593 * number. Future open calls will be met with errors.
1594 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596void video_unregister_device(struct video_device *vfd)
1597{
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001598 mutex_lock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if(video_device[vfd->minor]!=vfd)
1600 panic("videodev: bad unregister");
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 video_device[vfd->minor]=NULL;
1603 class_device_unregister(&vfd->class_dev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -02001604 mutex_unlock(&videodev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001607/*
1608 * Video fs operations
1609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610static struct file_operations video_fops=
1611{
1612 .owner = THIS_MODULE,
1613 .llseek = no_llseek,
1614 .open = video_open,
1615};
1616
1617/*
1618 * Initialise video for linux
1619 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -08001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static int __init videodev_init(void)
1622{
1623 int ret;
1624
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001625 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1627 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1628 return -EIO;
1629 }
1630
1631 ret = class_register(&video_class);
1632 if (ret < 0) {
1633 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1634 printk(KERN_WARNING "video_dev: class_register failed\n");
1635 return -EIO;
1636 }
1637
1638 return 0;
1639}
1640
1641static void __exit videodev_exit(void)
1642{
1643 class_unregister(&video_class);
1644 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1645}
1646
1647module_init(videodev_init)
1648module_exit(videodev_exit)
1649
1650EXPORT_SYMBOL(video_register_device);
1651EXPORT_SYMBOL(video_unregister_device);
1652EXPORT_SYMBOL(video_devdata);
1653EXPORT_SYMBOL(video_usercopy);
1654EXPORT_SYMBOL(video_exclusive_open);
1655EXPORT_SYMBOL(video_exclusive_release);
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001656EXPORT_SYMBOL(video_ioctl2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657EXPORT_SYMBOL(video_device_alloc);
1658EXPORT_SYMBOL(video_device_release);
1659
Mauro Carvalho Chehab401998f2006-06-04 10:06:18 -03001660MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1661MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662MODULE_LICENSE("GPL");
1663
1664
1665/*
1666 * Local variables:
1667 * c-basic-offset: 8
1668 * End:
1669 */