2 * Video capture interface for Linux version 2
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
7 * 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.
12 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
19 #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);
25 #define dbgarg2(fmt, arg...) \
26 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) \
27 printk (KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
29 #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>
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>
40 #include <linux/devfs_fs_kernel.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
45 #include <linux/videodev2.h>
47 #ifdef CONFIG_VIDEO_V4L1
48 #include <linux/videodev.h>
50 #include <media/v4l2-common.h>
52 #define VIDEO_NUM_DEVICES 256
53 #define VIDEO_NAME "video4linux"
59 static ssize_t show_name(struct class_device *cd, char *buf)
61 struct video_device *vfd = container_of(cd, struct video_device,
63 return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
66 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
68 struct video_device *video_device_alloc(void)
70 struct video_device *vfd;
72 vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
76 void video_device_release(struct video_device *vfd)
81 static void video_release(struct class_device *cd)
83 struct video_device *vfd = container_of(cd, struct video_device,
87 /* needed until all drivers are fixed */
94 static struct class video_class = {
96 .release = video_release,
103 static struct video_device *video_device[VIDEO_NUM_DEVICES];
104 static DEFINE_MUTEX(videodev_lock);
106 struct video_device* video_devdata(struct file *file)
108 return video_device[iminor(file->f_dentry->d_inode)];
112 * Open a video device - FIXME: Obsoleted
114 static int video_open(struct inode *inode, struct file *file)
116 unsigned int minor = iminor(inode);
118 struct video_device *vfl;
119 const struct file_operations *old_fops;
121 if(minor>=VIDEO_NUM_DEVICES)
123 mutex_lock(&videodev_lock);
124 vfl=video_device[minor];
126 mutex_unlock(&videodev_lock);
127 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
128 mutex_lock(&videodev_lock);
129 vfl=video_device[minor];
131 mutex_unlock(&videodev_lock);
135 old_fops = file->f_op;
136 file->f_op = fops_get(vfl->fops);
138 err = file->f_op->open(inode,file);
140 fops_put(file->f_op);
141 file->f_op = fops_get(old_fops);
144 mutex_unlock(&videodev_lock);
149 * helper function -- handles userspace copying for ioctl arguments
154 video_fix_command(unsigned int cmd)
157 case VIDIOC_OVERLAY_OLD:
158 cmd = VIDIOC_OVERLAY;
160 case VIDIOC_S_PARM_OLD:
163 case VIDIOC_S_CTRL_OLD:
166 case VIDIOC_G_AUDIO_OLD:
167 cmd = VIDIOC_G_AUDIO;
169 case VIDIOC_G_AUDOUT_OLD:
170 cmd = VIDIOC_G_AUDOUT;
172 case VIDIOC_CROPCAP_OLD:
173 cmd = VIDIOC_CROPCAP;
181 * Obsolete usercopy function - Should be removed soon
184 video_usercopy(struct inode *inode, struct file *file,
185 unsigned int cmd, unsigned long arg,
186 int (*func)(struct inode *inode, struct file *file,
187 unsigned int cmd, void *arg))
194 size_t ctrls_size = 0;
195 void __user *user_ptr = NULL;
198 cmd = video_fix_command(cmd);
200 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
201 cmd == VIDIOC_TRY_EXT_CTRLS);
203 /* Copy arguments into temp kernel buffer */
204 switch (_IOC_DIR(cmd)) {
210 case (_IOC_WRITE | _IOC_READ):
211 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
214 /* too big to allocate from stack */
215 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
222 if (_IOC_DIR(cmd) & _IOC_WRITE)
223 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
228 struct v4l2_ext_controls *p = parg;
230 /* In case of an error, tell the caller that it wasn't
231 a specific control that caused it. */
232 p->error_idx = p->count;
233 user_ptr = (void __user *)p->controls;
235 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
236 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
237 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
242 if (copy_from_user(mbuf, user_ptr, ctrls_size))
249 err = func(inode, file, cmd, parg);
250 if (err == -ENOIOCTLCMD)
253 struct v4l2_ext_controls *p = parg;
255 p->controls = (void *)user_ptr;
256 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
264 /* Copy results into user buffer */
265 switch (_IOC_DIR(cmd))
268 case (_IOC_WRITE | _IOC_READ):
269 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
280 * open/release helper functions -- handle exclusive opens
281 * Should be removed soon
283 int video_exclusive_open(struct inode *inode, struct file *file)
285 struct video_device *vfl = video_devdata(file);
288 mutex_lock(&vfl->lock);
294 mutex_unlock(&vfl->lock);
298 int video_exclusive_release(struct inode *inode, struct file *file)
300 struct video_device *vfl = video_devdata(file);
306 static char *v4l2_memory_names[] = {
307 [V4L2_MEMORY_MMAP] = "mmap",
308 [V4L2_MEMORY_USERPTR] = "userptr",
309 [V4L2_MEMORY_OVERLAY] = "overlay",
313 /* FIXME: Those stuff are replicated also on v4l2-common.c */
314 static char *v4l2_type_names_FIXME[] = {
315 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
316 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
317 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
318 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
319 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
320 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
321 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-capture",
322 [V4L2_BUF_TYPE_PRIVATE] = "private",
325 static 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",
336 #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
338 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
339 struct v4l2_buffer *p)
341 struct v4l2_timecode *tc=&p->timecode;
343 dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
344 "bytesused=%d, flags=0x%08d, "
345 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx\n",
346 (p->timestamp.tv_sec/3600),
347 (int)(p->timestamp.tv_sec/60)%60,
348 (int)(p->timestamp.tv_sec%60),
349 p->timestamp.tv_usec,
351 prt_names(p->type,v4l2_type_names_FIXME),
352 p->bytesused,p->flags,
353 p->field,p->sequence,
354 prt_names(p->memory,v4l2_memory_names),
356 dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
357 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
358 tc->hours,tc->minutes,tc->seconds,
359 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
362 static inline void dbgrect(struct video_device *vfd, char *s,
365 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
366 r->width, r->height);
369 static inline void v4l_print_pix_fmt (struct video_device *vfd,
370 struct v4l2_pix_format *fmt)
372 dbgarg2 ("width=%d, height=%d, format=0x%08x, field=%s, "
373 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
374 fmt->width,fmt->height,fmt->pixelformat,
375 prt_names(fmt->field,v4l2_field_names_FIXME),
376 fmt->bytesperline,fmt->sizeimage,fmt->colorspace);
380 static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
383 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
384 if (vfd->vidioc_try_fmt_cap)
387 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
388 if (vfd->vidioc_try_fmt_overlay)
391 case V4L2_BUF_TYPE_VBI_CAPTURE:
392 if (vfd->vidioc_try_fmt_vbi)
395 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
396 if (vfd->vidioc_try_fmt_vbi_output)
399 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
400 if (vfd->vidioc_try_fmt_vbi_capture)
403 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
404 if (vfd->vidioc_try_fmt_video_output)
407 case V4L2_BUF_TYPE_VBI_OUTPUT:
408 if (vfd->vidioc_try_fmt_vbi_output)
411 case V4L2_BUF_TYPE_PRIVATE:
412 if (vfd->vidioc_try_fmt_type_private)
419 static int __video_do_ioctl(struct inode *inode, struct file *file,
420 unsigned int cmd, void *arg)
422 struct video_device *vfd = video_devdata(file);
423 void *fh = file->private_data;
426 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
427 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
428 v4l_print_ioctl(vfd->name, cmd);
432 /* --- capabilities ------------------------------------------ */
433 case VIDIOC_QUERYCAP:
435 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
436 memset(cap, 0, sizeof(*cap));
438 if (!vfd->vidioc_querycap)
441 ret=vfd->vidioc_querycap(file, fh, cap);
443 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
445 "capabilities=0x%08x\n",
446 cap->driver,cap->card,cap->bus_info,
452 /* --- priority ------------------------------------------ */
453 case VIDIOC_G_PRIORITY:
455 enum v4l2_priority *p=arg;
457 if (!vfd->vidioc_g_priority)
459 ret=vfd->vidioc_g_priority(file, fh, p);
461 dbgarg(cmd, "priority is %d\n", *p);
464 case VIDIOC_S_PRIORITY:
466 enum v4l2_priority *p=arg;
468 if (!vfd->vidioc_s_priority)
470 dbgarg(cmd, "setting priority to %d\n", *p);
471 ret=vfd->vidioc_s_priority(file, fh, *p);
475 /* --- capture ioctls ---------------------------------------- */
476 case VIDIOC_ENUM_FMT:
478 struct v4l2_fmtdesc *f = arg;
479 enum v4l2_buf_type type;
484 memset(f,0,sizeof(*f));
489 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
490 if (vfd->vidioc_enum_fmt_cap)
491 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
493 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
494 if (vfd->vidioc_enum_fmt_overlay)
495 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
497 case V4L2_BUF_TYPE_VBI_CAPTURE:
498 if (vfd->vidioc_enum_fmt_vbi)
499 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
501 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
502 if (vfd->vidioc_enum_fmt_vbi_output)
503 ret=vfd->vidioc_enum_fmt_vbi_output(file,
506 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
507 if (vfd->vidioc_enum_fmt_vbi_capture)
508 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
511 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
512 if (vfd->vidioc_enum_fmt_video_output)
513 ret=vfd->vidioc_enum_fmt_video_output(file,
516 case V4L2_BUF_TYPE_VBI_OUTPUT:
517 if (vfd->vidioc_enum_fmt_vbi_output)
518 ret=vfd->vidioc_enum_fmt_vbi_output(file,
521 case V4L2_BUF_TYPE_PRIVATE:
522 if (vfd->vidioc_enum_fmt_type_private)
523 ret=vfd->vidioc_enum_fmt_type_private(file,
528 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
530 " pixelformat=0x%8x\n",
531 f->index, f->type, f->flags,
539 struct v4l2_format *f = (struct v4l2_format *)arg;
540 enum v4l2_buf_type type=f->type;
542 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
545 /* FIXME: Should be one dump per type */
546 dbgarg (cmd, "type=%s\n", prt_names(type,
547 v4l2_type_names_FIXME));
550 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
551 if (vfd->vidioc_g_fmt_cap)
552 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
554 v4l_print_pix_fmt(vfd,&f->fmt.pix);
556 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
557 if (vfd->vidioc_g_fmt_overlay)
558 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
560 case V4L2_BUF_TYPE_VBI_CAPTURE:
561 if (vfd->vidioc_g_fmt_vbi)
562 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
564 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
565 if (vfd->vidioc_g_fmt_vbi_output)
566 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
568 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
569 if (vfd->vidioc_g_fmt_vbi_capture)
570 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
572 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
573 if (vfd->vidioc_g_fmt_video_output)
574 ret=vfd->vidioc_g_fmt_video_output(file,
577 case V4L2_BUF_TYPE_VBI_OUTPUT:
578 if (vfd->vidioc_g_fmt_vbi_output)
579 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
581 case V4L2_BUF_TYPE_PRIVATE:
582 if (vfd->vidioc_g_fmt_type_private)
583 ret=vfd->vidioc_g_fmt_type_private(file,
592 struct v4l2_format *f = (struct v4l2_format *)arg;
594 /* FIXME: Should be one dump per type */
595 dbgarg (cmd, "type=%s\n", prt_names(f->type,
596 v4l2_type_names_FIXME));
599 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
600 v4l_print_pix_fmt(vfd,&f->fmt.pix);
601 if (vfd->vidioc_s_fmt_cap)
602 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
604 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
605 if (vfd->vidioc_s_fmt_overlay)
606 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
608 case V4L2_BUF_TYPE_VBI_CAPTURE:
609 if (vfd->vidioc_s_fmt_vbi)
610 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
612 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
613 if (vfd->vidioc_s_fmt_vbi_output)
614 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
616 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
617 if (vfd->vidioc_s_fmt_vbi_capture)
618 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
620 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
621 if (vfd->vidioc_s_fmt_video_output)
622 ret=vfd->vidioc_s_fmt_video_output(file,
625 case V4L2_BUF_TYPE_VBI_OUTPUT:
626 if (vfd->vidioc_s_fmt_vbi_output)
627 ret=vfd->vidioc_s_fmt_vbi_output(file,
630 case V4L2_BUF_TYPE_PRIVATE:
631 if (vfd->vidioc_s_fmt_type_private)
632 ret=vfd->vidioc_s_fmt_type_private(file,
640 struct v4l2_format *f = (struct v4l2_format *)arg;
642 /* FIXME: Should be one dump per type */
643 dbgarg (cmd, "type=%s\n", prt_names(f->type,
644 v4l2_type_names_FIXME));
646 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
647 if (vfd->vidioc_try_fmt_cap)
648 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
650 v4l_print_pix_fmt(vfd,&f->fmt.pix);
652 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
653 if (vfd->vidioc_try_fmt_overlay)
654 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
656 case V4L2_BUF_TYPE_VBI_CAPTURE:
657 if (vfd->vidioc_try_fmt_vbi)
658 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
660 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
661 if (vfd->vidioc_try_fmt_vbi_output)
662 ret=vfd->vidioc_try_fmt_vbi_output(file,
665 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
666 if (vfd->vidioc_try_fmt_vbi_capture)
667 ret=vfd->vidioc_try_fmt_vbi_capture(file,
670 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
671 if (vfd->vidioc_try_fmt_video_output)
672 ret=vfd->vidioc_try_fmt_video_output(file,
675 case V4L2_BUF_TYPE_VBI_OUTPUT:
676 if (vfd->vidioc_try_fmt_vbi_output)
677 ret=vfd->vidioc_try_fmt_vbi_output(file,
680 case V4L2_BUF_TYPE_PRIVATE:
681 if (vfd->vidioc_try_fmt_type_private)
682 ret=vfd->vidioc_try_fmt_type_private(file,
689 /* FIXME: Those buf reqs could be handled here,
690 with some changes on videobuf to allow its header to be included at
691 videodev2.h or being merged at videodev2.
695 struct v4l2_requestbuffers *p=arg;
697 if (!vfd->vidioc_reqbufs)
699 ret = check_fmt (vfd, p->type);
703 ret=vfd->vidioc_reqbufs(file, fh, p);
704 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
706 prt_names(p->type,v4l2_type_names_FIXME),
707 prt_names(p->memory,v4l2_memory_names));
710 case VIDIOC_QUERYBUF:
712 struct v4l2_buffer *p=arg;
714 if (!vfd->vidioc_querybuf)
716 ret = check_fmt (vfd, p->type);
720 ret=vfd->vidioc_querybuf(file, fh, p);
727 struct v4l2_buffer *p=arg;
729 if (!vfd->vidioc_qbuf)
731 ret = check_fmt (vfd, p->type);
735 ret=vfd->vidioc_qbuf(file, fh, p);
742 struct v4l2_buffer *p=arg;
743 if (!vfd->vidioc_qbuf)
745 ret = check_fmt (vfd, p->type);
749 ret=vfd->vidioc_qbuf(file, fh, p);
758 if (!vfd->vidioc_overlay)
760 dbgarg (cmd, "value=%d\n",*i);
761 ret=vfd->vidioc_overlay(file, fh, *i);
765 /* --- streaming capture ------------------------------------- */
768 struct video_mbuf *p=arg;
770 memset(p,0,sizeof(p));
772 if (!vfd->vidiocgmbuf)
774 ret=vfd->vidiocgmbuf(file, fh, p);
776 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
778 (unsigned long)p->offsets);
784 struct v4l2_framebuffer *p=arg;
785 if (!vfd->vidioc_g_fbuf)
787 ret=vfd->vidioc_g_fbuf(file, fh, arg);
789 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
790 p->capability,p->flags,
791 (unsigned long)p->base);
792 v4l_print_pix_fmt (vfd, &p->fmt);
798 struct v4l2_framebuffer *p=arg;
799 if (!vfd->vidioc_s_fbuf)
802 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
803 p->capability,p->flags,(unsigned long)p->base);
804 v4l_print_pix_fmt (vfd, &p->fmt);
805 ret=vfd->vidioc_s_fbuf(file, fh, arg);
809 case VIDIOC_STREAMON:
811 enum v4l2_buf_type i = *(int *)arg;
812 if (!vfd->vidioc_streamon)
814 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
815 ret=vfd->vidioc_streamon(file, fh,i);
818 case VIDIOC_STREAMOFF:
820 enum v4l2_buf_type i = *(int *)arg;
822 if (!vfd->vidioc_streamoff)
824 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
825 ret=vfd->vidioc_streamoff(file, fh, i);
828 /* ---------- tv norms ---------- */
831 struct v4l2_standard *p = arg;
832 unsigned int index = p->index;
834 if (!vfd->tvnormsize) {
835 printk (KERN_WARNING "%s: no TV norms defined!\n",
840 if (index<=0 || index >= vfd->tvnormsize) {
844 v4l2_video_std_construct(p, vfd->tvnorms[p->index].id,
845 vfd->tvnorms[p->index].name);
848 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
849 "framelines=%d\n", p->index,
850 (unsigned long long)p->id, p->name,
851 p->frameperiod.numerator,
852 p->frameperiod.denominator,
860 v4l2_std_id *id = arg;
862 *id = vfd->current_norm;
864 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
871 v4l2_std_id *id = arg;
874 if (!vfd->tvnormsize) {
875 printk (KERN_WARNING "%s: no TV norms defined!\n",
880 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
882 /* First search for exact match */
883 for (i = 0; i < vfd->tvnormsize; i++)
884 if (*id == vfd->tvnorms[i].id)
886 /* Then for a generic video std that contains desired std */
887 if (i == vfd->tvnormsize)
888 for (i = 0; i < vfd->tvnormsize; i++)
889 if (*id & vfd->tvnorms[i].id)
891 if (i == vfd->tvnormsize) {
895 /* Calls the specific handler */
896 if (vfd->vidioc_s_std)
897 ret=vfd->vidioc_s_std(file, fh, i);
901 /* Updates standard information */
903 vfd->current_norm=*id;
907 case VIDIOC_QUERYSTD:
911 if (!vfd->vidioc_querystd)
913 ret=vfd->vidioc_querystd(file, fh, arg);
915 dbgarg (cmd, "detected std=%Lu\n",
916 (unsigned long long)*p);
919 /* ------ input switching ---------- */
920 /* FIXME: Inputs can be handled inside videodev2 */
921 case VIDIOC_ENUMINPUT:
923 struct v4l2_input *p=arg;
926 if (!vfd->vidioc_enum_input)
928 memset(p, 0, sizeof(*p));
931 ret=vfd->vidioc_enum_input(file, fh, p);
933 dbgarg (cmd, "index=%d, name=%s, type=%d, "
935 "tuner=%d, std=%Ld, status=%d\n",
936 p->index,p->name,p->type,p->audioset,
938 (unsigned long long)p->std,
944 unsigned int *i = arg;
946 if (!vfd->vidioc_g_input)
948 ret=vfd->vidioc_g_input(file, fh, i);
950 dbgarg (cmd, "value=%d\n",*i);
955 unsigned int *i = arg;
957 if (!vfd->vidioc_s_input)
959 dbgarg (cmd, "value=%d\n",*i);
960 ret=vfd->vidioc_s_input(file, fh, *i);
964 /* ------ output switching ---------- */
965 case VIDIOC_G_OUTPUT:
967 unsigned int *i = arg;
969 if (!vfd->vidioc_g_output)
971 ret=vfd->vidioc_g_output(file, fh, i);
973 dbgarg (cmd, "value=%d\n",*i);
976 case VIDIOC_S_OUTPUT:
978 unsigned int *i = arg;
980 if (!vfd->vidioc_s_output)
982 dbgarg (cmd, "value=%d\n",*i);
983 ret=vfd->vidioc_s_output(file, fh, *i);
987 /* --- controls ---------------------------------------------- */
988 case VIDIOC_QUERYCTRL:
990 struct v4l2_queryctrl *p=arg;
992 if (!vfd->vidioc_queryctrl)
994 ret=vfd->vidioc_queryctrl(file, fh, p);
997 dbgarg (cmd, "id=%d, type=%d, name=%s, "
999 " step=%d, default=%d, flags=0x%08x\n",
1000 p->id,p->type,p->name,p->minimum,
1001 p->maximum,p->step,p->default_value,
1007 struct v4l2_control *p = arg;
1009 if (!vfd->vidioc_g_ctrl)
1011 dbgarg(cmd, "Enum for index=%d\n", p->id);
1013 ret=vfd->vidioc_g_ctrl(file, fh, p);
1015 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1020 struct v4l2_control *p = arg;
1022 if (!vfd->vidioc_s_ctrl)
1024 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1026 ret=vfd->vidioc_s_ctrl(file, fh, p);
1029 case VIDIOC_G_EXT_CTRLS:
1031 struct v4l2_ext_controls *p = arg;
1033 if (vfd->vidioc_g_ext_ctrls) {
1034 dbgarg(cmd, "count=%d\n", p->count);
1036 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1040 case VIDIOC_S_EXT_CTRLS:
1042 struct v4l2_ext_controls *p = arg;
1044 if (vfd->vidioc_s_ext_ctrls) {
1045 dbgarg(cmd, "count=%d\n", p->count);
1047 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1051 case VIDIOC_TRY_EXT_CTRLS:
1053 struct v4l2_ext_controls *p = arg;
1055 if (vfd->vidioc_try_ext_ctrls) {
1056 dbgarg(cmd, "count=%d\n", p->count);
1058 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1062 case VIDIOC_QUERYMENU:
1064 struct v4l2_querymenu *p=arg;
1065 if (!vfd->vidioc_querymenu)
1067 ret=vfd->vidioc_querymenu(file, fh, p);
1069 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1070 p->id,p->index,p->name);
1073 /* --- audio ---------------------------------------------- */
1074 case VIDIOC_ENUMAUDIO:
1076 struct v4l2_audio *p=arg;
1078 if (!vfd->vidioc_enumaudio)
1080 dbgarg(cmd, "Enum for index=%d\n", p->index);
1081 ret=vfd->vidioc_enumaudio(file, fh, p);
1083 dbgarg2("index=%d, name=%s, capability=%d, "
1084 "mode=%d\n",p->index,p->name,
1085 p->capability, p->mode);
1088 case VIDIOC_G_AUDIO:
1090 struct v4l2_audio *p=arg;
1092 if (!vfd->vidioc_g_audio)
1094 dbgarg(cmd, "Get for index=%d\n", p->index);
1095 ret=vfd->vidioc_g_audio(file, fh, p);
1097 dbgarg2("index=%d, name=%s, capability=%d, "
1098 "mode=%d\n",p->index,
1099 p->name,p->capability, p->mode);
1102 case VIDIOC_S_AUDIO:
1104 struct v4l2_audio *p=arg;
1106 if (!vfd->vidioc_s_audio)
1108 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1109 "mode=%d\n", p->index, p->name,
1110 p->capability, p->mode);
1111 ret=vfd->vidioc_s_audio(file, fh, p);
1114 case VIDIOC_ENUMAUDOUT:
1116 struct v4l2_audioout *p=arg;
1118 if (!vfd->vidioc_enumaudout)
1120 dbgarg(cmd, "Enum for index=%d\n", p->index);
1121 ret=vfd->vidioc_enumaudout(file, fh, p);
1123 dbgarg2("index=%d, name=%s, capability=%d, "
1124 "mode=%d\n", p->index, p->name,
1125 p->capability,p->mode);
1128 case VIDIOC_G_AUDOUT:
1130 struct v4l2_audioout *p=arg;
1132 if (!vfd->vidioc_g_audout)
1134 dbgarg(cmd, "Enum for index=%d\n", p->index);
1135 ret=vfd->vidioc_g_audout(file, fh, p);
1137 dbgarg2("index=%d, name=%s, capability=%d, "
1138 "mode=%d\n", p->index, p->name,
1139 p->capability,p->mode);
1142 case VIDIOC_S_AUDOUT:
1144 struct v4l2_audioout *p=arg;
1146 if (!vfd->vidioc_s_audout)
1148 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1149 "mode=%d\n", p->index, p->name,
1150 p->capability,p->mode);
1152 ret=vfd->vidioc_s_audout(file, fh, p);
1155 case VIDIOC_G_MODULATOR:
1157 struct v4l2_modulator *p=arg;
1158 if (!vfd->vidioc_g_modulator)
1160 ret=vfd->vidioc_g_modulator(file, fh, p);
1162 dbgarg(cmd, "index=%d, name=%s, "
1163 "capability=%d, rangelow=%d,"
1164 " rangehigh=%d, txsubchans=%d\n",
1165 p->index, p->name,p->capability,
1166 p->rangelow, p->rangehigh,
1170 case VIDIOC_S_MODULATOR:
1172 struct v4l2_modulator *p=arg;
1173 if (!vfd->vidioc_s_modulator)
1175 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1176 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1177 p->index, p->name,p->capability,p->rangelow,
1178 p->rangehigh,p->txsubchans);
1179 ret=vfd->vidioc_s_modulator(file, fh, p);
1184 struct v4l2_crop *p=arg;
1185 if (!vfd->vidioc_g_crop)
1187 ret=vfd->vidioc_g_crop(file, fh, p);
1189 dbgarg(cmd, "type=%d\n", p->type);
1190 dbgrect(vfd, "", &p->c);
1196 struct v4l2_crop *p=arg;
1197 if (!vfd->vidioc_s_crop)
1199 dbgarg(cmd, "type=%d\n", p->type);
1200 dbgrect(vfd, "", &p->c);
1201 ret=vfd->vidioc_s_crop(file, fh, p);
1204 case VIDIOC_CROPCAP:
1206 struct v4l2_cropcap *p=arg;
1207 /*FIXME: Should also show v4l2_fract pixelaspect */
1208 if (!vfd->vidioc_cropcap)
1210 dbgarg(cmd, "type=%d\n", p->type);
1211 dbgrect(vfd, "bounds ", &p->bounds);
1212 dbgrect(vfd, "defrect ", &p->defrect);
1213 ret=vfd->vidioc_cropcap(file, fh, p);
1216 case VIDIOC_G_MPEGCOMP:
1218 struct v4l2_mpeg_compression *p=arg;
1220 /*FIXME: Several fields not shown */
1221 if (!vfd->vidioc_g_mpegcomp)
1223 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
1225 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d,"
1226 " ts_pid_video=%d, ts_pid_pcr=%d, "
1227 "ps_size=%d, au_sample_rate=%d, "
1228 "au_pesid=%c, vi_frame_rate=%d, "
1229 "vi_frames_per_gop=%d, "
1230 "vi_bframes_count=%d, vi_pesid=%c\n",
1231 p->ts_pid_pmt,p->ts_pid_audio,
1232 p->ts_pid_video,p->ts_pid_pcr,
1233 p->ps_size, p->au_sample_rate,
1234 p->au_pesid, p->vi_frame_rate,
1235 p->vi_frames_per_gop,
1236 p->vi_bframes_count, p->vi_pesid);
1239 case VIDIOC_S_MPEGCOMP:
1241 struct v4l2_mpeg_compression *p=arg;
1242 /*FIXME: Several fields not shown */
1243 if (!vfd->vidioc_s_mpegcomp)
1245 dbgarg (cmd, "ts_pid_pmt=%d, ts_pid_audio=%d, "
1246 "ts_pid_video=%d, ts_pid_pcr=%d, ps_size=%d, "
1247 "au_sample_rate=%d, au_pesid=%c, "
1248 "vi_frame_rate=%d, vi_frames_per_gop=%d, "
1249 "vi_bframes_count=%d, vi_pesid=%c\n",
1250 p->ts_pid_pmt,p->ts_pid_audio, p->ts_pid_video,
1251 p->ts_pid_pcr, p->ps_size, p->au_sample_rate,
1252 p->au_pesid, p->vi_frame_rate,
1253 p->vi_frames_per_gop, p->vi_bframes_count,
1255 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1258 case VIDIOC_G_JPEGCOMP:
1260 struct v4l2_jpegcompression *p=arg;
1261 if (!vfd->vidioc_g_jpegcomp)
1263 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1265 dbgarg (cmd, "quality=%d, APPn=%d, "
1266 "APP_len=%d, COM_len=%d, "
1267 "jpeg_markers=%d\n",
1268 p->quality,p->APPn,p->APP_len,
1269 p->COM_len,p->jpeg_markers);
1272 case VIDIOC_S_JPEGCOMP:
1274 struct v4l2_jpegcompression *p=arg;
1275 if (!vfd->vidioc_g_jpegcomp)
1277 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1278 "COM_len=%d, jpeg_markers=%d\n",
1279 p->quality,p->APPn,p->APP_len,
1280 p->COM_len,p->jpeg_markers);
1281 ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1286 struct v4l2_streamparm *p=arg;
1287 if (!vfd->vidioc_g_parm)
1289 ret=vfd->vidioc_g_parm(file, fh, p);
1290 dbgarg (cmd, "type=%d\n", p->type);
1295 struct v4l2_streamparm *p=arg;
1296 if (!vfd->vidioc_s_parm)
1298 dbgarg (cmd, "type=%d\n", p->type);
1299 ret=vfd->vidioc_s_parm(file, fh, p);
1302 case VIDIOC_G_TUNER:
1304 struct v4l2_tuner *p=arg;
1305 if (!vfd->vidioc_g_tuner)
1307 ret=vfd->vidioc_g_tuner(file, fh, p);
1309 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1310 "capability=%d, rangelow=%d, "
1311 "rangehigh=%d, signal=%d, afc=%d, "
1312 "rxsubchans=%d, audmode=%d\n",
1313 p->index, p->name, p->type,
1314 p->capability, p->rangelow,
1315 p->rangehigh, p->rxsubchans,
1316 p->audmode, p->signal, p->afc);
1319 case VIDIOC_S_TUNER:
1321 struct v4l2_tuner *p=arg;
1322 if (!vfd->vidioc_s_tuner)
1324 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1325 "capability=%d, rangelow=%d, rangehigh=%d, "
1326 "signal=%d, afc=%d, rxsubchans=%d, "
1327 "audmode=%d\n",p->index, p->name, p->type,
1328 p->capability, p->rangelow,p->rangehigh,
1329 p->rxsubchans, p->audmode, p->signal,
1331 ret=vfd->vidioc_s_tuner(file, fh, p);
1334 case VIDIOC_G_FREQUENCY:
1336 struct v4l2_frequency *p=arg;
1337 if (!vfd->vidioc_g_frequency)
1339 ret=vfd->vidioc_g_frequency(file, fh, p);
1341 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1342 p->tuner,p->type,p->frequency);
1345 case VIDIOC_S_FREQUENCY:
1347 struct v4l2_frequency *p=arg;
1348 if (!vfd->vidioc_s_frequency)
1350 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1351 p->tuner,p->type,p->frequency);
1352 ret=vfd->vidioc_s_frequency(file, fh, p);
1355 case VIDIOC_G_SLICED_VBI_CAP:
1357 struct v4l2_sliced_vbi_cap *p=arg;
1358 if (!vfd->vidioc_g_sliced_vbi_cap)
1360 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1362 dbgarg (cmd, "service_set=%d\n", p->service_set);
1365 case VIDIOC_LOG_STATUS:
1367 if (!vfd->vidioc_log_status)
1369 ret=vfd->vidioc_log_status(file, fh);
1373 /* --- Others --------------------------------------------- */
1376 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,__video_do_ioctl);
1379 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1381 printk ("%s: err:\n", vfd->name);
1382 v4l_print_ioctl(vfd->name, cmd);
1389 int video_ioctl2 (struct inode *inode, struct file *file,
1390 unsigned int cmd, unsigned long arg)
1397 size_t ctrls_size = 0;
1398 void __user *user_ptr = NULL;
1400 #ifdef __OLD_VIDIOC_
1401 cmd = video_fix_command(cmd);
1403 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1404 cmd == VIDIOC_TRY_EXT_CTRLS);
1406 /* Copy arguments into temp kernel buffer */
1407 switch (_IOC_DIR(cmd)) {
1413 case (_IOC_WRITE | _IOC_READ):
1414 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1417 /* too big to allocate from stack */
1418 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1425 if (_IOC_DIR(cmd) & _IOC_WRITE)
1426 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1432 struct v4l2_ext_controls *p = parg;
1434 /* In case of an error, tell the caller that it wasn't
1435 a specific control that caused it. */
1436 p->error_idx = p->count;
1437 user_ptr = (void __user *)p->controls;
1439 ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1440 /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1441 mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1446 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1453 err = __video_do_ioctl(inode, file, cmd, parg);
1454 if (err == -ENOIOCTLCMD)
1457 struct v4l2_ext_controls *p = parg;
1459 p->controls = (void *)user_ptr;
1460 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1468 /* Copy results into user buffer */
1469 switch (_IOC_DIR(cmd))
1472 case (_IOC_WRITE | _IOC_READ):
1473 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1484 static struct file_operations video_fops;
1487 * video_register_device - register video4linux devices
1488 * @vfd: video device structure we want to register
1489 * @type: type of device to register
1490 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
1493 * The registration code assigns minor numbers based on the type
1494 * requested. -ENFILE is returned in all the device slots for this
1495 * category are full. If not then the minor field is set and the
1496 * driver initialize function is called (if non %NULL).
1498 * Zero is returned on success.
1502 * %VFL_TYPE_GRABBER - A frame grabber
1504 * %VFL_TYPE_VTX - A teletext device
1506 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1508 * %VFL_TYPE_RADIO - A radio card
1511 int video_register_device(struct video_device *vfd, int type, int nr)
1520 case VFL_TYPE_GRABBER:
1521 base=MINOR_VFL_TYPE_GRABBER_MIN;
1522 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
1523 name_base = "video";
1526 base=MINOR_VFL_TYPE_VTX_MIN;
1527 end=MINOR_VFL_TYPE_VTX_MAX+1;
1531 base=MINOR_VFL_TYPE_VBI_MIN;
1532 end=MINOR_VFL_TYPE_VBI_MAX+1;
1535 case VFL_TYPE_RADIO:
1536 base=MINOR_VFL_TYPE_RADIO_MIN;
1537 end=MINOR_VFL_TYPE_RADIO_MAX+1;
1538 name_base = "radio";
1544 /* pick a minor number */
1545 mutex_lock(&videodev_lock);
1546 if (nr >= 0 && nr < end-base) {
1547 /* use the one the driver asked for */
1549 if (NULL != video_device[i]) {
1550 mutex_unlock(&videodev_lock);
1554 /* use first free */
1555 for(i=base;i<end;i++)
1556 if (NULL == video_device[i])
1559 mutex_unlock(&videodev_lock);
1563 video_device[i]=vfd;
1565 mutex_unlock(&videodev_lock);
1567 sprintf(vfd->devfs_name, "v4l/%s%d", name_base, i - base);
1568 devfs_mk_cdev(MKDEV(VIDEO_MAJOR, vfd->minor),
1569 S_IFCHR | S_IRUSR | S_IWUSR, vfd->devfs_name);
1570 mutex_init(&vfd->lock);
1573 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
1575 vfd->class_dev.dev = vfd->dev;
1576 vfd->class_dev.class = &video_class;
1577 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
1578 strlcpy(vfd->class_dev.class_id, vfd->devfs_name + 4, BUS_ID_SIZE);
1579 class_device_register(&vfd->class_dev);
1580 class_device_create_file(&vfd->class_dev,
1581 &class_device_attr_name);
1584 /* needed until all drivers are fixed */
1586 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
1587 "Please fix your driver for proper sysfs support, see "
1588 "http://lwn.net/Articles/36850/\n", vfd->name);
1594 * video_unregister_device - unregister a video4linux device
1595 * @vfd: the device to unregister
1597 * This unregisters the passed device and deassigns the minor
1598 * number. Future open calls will be met with errors.
1601 void video_unregister_device(struct video_device *vfd)
1603 mutex_lock(&videodev_lock);
1604 if(video_device[vfd->minor]!=vfd)
1605 panic("videodev: bad unregister");
1607 devfs_remove(vfd->devfs_name);
1608 video_device[vfd->minor]=NULL;
1609 class_device_unregister(&vfd->class_dev);
1610 mutex_unlock(&videodev_lock);
1614 * Video fs operations
1616 static struct file_operations video_fops=
1618 .owner = THIS_MODULE,
1619 .llseek = no_llseek,
1624 * Initialise video for linux
1627 static int __init videodev_init(void)
1631 printk(KERN_INFO "Linux video capture interface: v2.00\n");
1632 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1633 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1637 ret = class_register(&video_class);
1639 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1640 printk(KERN_WARNING "video_dev: class_register failed\n");
1647 static void __exit videodev_exit(void)
1649 class_unregister(&video_class);
1650 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1653 module_init(videodev_init)
1654 module_exit(videodev_exit)
1656 EXPORT_SYMBOL(video_register_device);
1657 EXPORT_SYMBOL(video_unregister_device);
1658 EXPORT_SYMBOL(video_devdata);
1659 EXPORT_SYMBOL(video_usercopy);
1660 EXPORT_SYMBOL(video_exclusive_open);
1661 EXPORT_SYMBOL(video_exclusive_release);
1662 EXPORT_SYMBOL(video_ioctl2);
1663 EXPORT_SYMBOL(video_device_alloc);
1664 EXPORT_SYMBOL(video_device_release);
1666 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1667 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1668 MODULE_LICENSE("GPL");