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 <asm/uaccess.h>
41 #include <asm/system.h>
43 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
44 #include <linux/videodev2.h>
46 #ifdef CONFIG_VIDEO_V4L1
47 #include <linux/videodev.h>
49 #include <media/v4l2-common.h>
51 #define VIDEO_NUM_DEVICES 256
52 #define VIDEO_NAME "video4linux"
58 static ssize_t show_name(struct class_device *cd, char *buf)
60 struct video_device *vfd = container_of(cd, struct video_device,
62 return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
65 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
67 struct video_device *video_device_alloc(void)
69 struct video_device *vfd;
71 vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
75 void video_device_release(struct video_device *vfd)
80 static void video_release(struct class_device *cd)
82 struct video_device *vfd = container_of(cd, struct video_device,
86 /* needed until all drivers are fixed */
93 static struct class video_class = {
95 .release = video_release,
102 static struct video_device *video_device[VIDEO_NUM_DEVICES];
103 static DEFINE_MUTEX(videodev_lock);
105 struct video_device* video_devdata(struct file *file)
107 return video_device[iminor(file->f_dentry->d_inode)];
111 * Open a video device - FIXME: Obsoleted
113 static int video_open(struct inode *inode, struct file *file)
115 unsigned int minor = iminor(inode);
117 struct video_device *vfl;
118 const struct file_operations *old_fops;
120 if(minor>=VIDEO_NUM_DEVICES)
122 mutex_lock(&videodev_lock);
123 vfl=video_device[minor];
125 mutex_unlock(&videodev_lock);
126 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
127 mutex_lock(&videodev_lock);
128 vfl=video_device[minor];
130 mutex_unlock(&videodev_lock);
134 old_fops = file->f_op;
135 file->f_op = fops_get(vfl->fops);
137 err = file->f_op->open(inode,file);
139 fops_put(file->f_op);
140 file->f_op = fops_get(old_fops);
143 mutex_unlock(&videodev_lock);
148 * helper function -- handles userspace copying for ioctl arguments
153 video_fix_command(unsigned int cmd)
156 case VIDIOC_OVERLAY_OLD:
157 cmd = VIDIOC_OVERLAY;
159 case VIDIOC_S_PARM_OLD:
162 case VIDIOC_S_CTRL_OLD:
165 case VIDIOC_G_AUDIO_OLD:
166 cmd = VIDIOC_G_AUDIO;
168 case VIDIOC_G_AUDOUT_OLD:
169 cmd = VIDIOC_G_AUDOUT;
171 case VIDIOC_CROPCAP_OLD:
172 cmd = VIDIOC_CROPCAP;
180 * Obsolete usercopy function - Should be removed soon
183 video_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))
193 size_t ctrls_size = 0;
194 void __user *user_ptr = NULL;
197 cmd = video_fix_command(cmd);
199 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
200 cmd == VIDIOC_TRY_EXT_CTRLS);
202 /* Copy arguments into temp kernel buffer */
203 switch (_IOC_DIR(cmd)) {
209 case (_IOC_WRITE | _IOC_READ):
210 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
213 /* too big to allocate from stack */
214 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
221 if (_IOC_DIR(cmd) & _IOC_WRITE)
222 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
227 struct v4l2_ext_controls *p = parg;
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;
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);
241 if (copy_from_user(mbuf, user_ptr, ctrls_size))
248 err = func(inode, file, cmd, parg);
249 if (err == -ENOIOCTLCMD)
252 struct v4l2_ext_controls *p = parg;
254 p->controls = (void *)user_ptr;
255 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
263 /* Copy results into user buffer */
264 switch (_IOC_DIR(cmd))
267 case (_IOC_WRITE | _IOC_READ):
268 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
279 * open/release helper functions -- handle exclusive opens
280 * Should be removed soon
282 int video_exclusive_open(struct inode *inode, struct file *file)
284 struct video_device *vfl = video_devdata(file);
287 mutex_lock(&vfl->lock);
293 mutex_unlock(&vfl->lock);
297 int video_exclusive_release(struct inode *inode, struct file *file)
299 struct video_device *vfl = video_devdata(file);
305 static char *v4l2_memory_names[] = {
306 [V4L2_MEMORY_MMAP] = "mmap",
307 [V4L2_MEMORY_USERPTR] = "userptr",
308 [V4L2_MEMORY_OVERLAY] = "overlay",
312 /* FIXME: Those stuff are replicated also on v4l2-common.c */
313 static 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",
324 static 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",
335 #define prt_names(a,arr) (((a)>=0)&&((a)<ARRAY_SIZE(arr)))?arr[a]:"unknown"
337 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
338 struct v4l2_buffer *p)
340 struct v4l2_timecode *tc=&p->timecode;
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,
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),
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,
358 tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
361 static inline void dbgrect(struct video_device *vfd, char *s,
364 dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
365 r->width, r->height);
368 static inline void v4l_print_pix_fmt (struct video_device *vfd,
369 struct v4l2_pix_format *fmt)
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);
379 static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
382 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
383 if (vfd->vidioc_try_fmt_cap)
386 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
387 if (vfd->vidioc_try_fmt_overlay)
390 case V4L2_BUF_TYPE_VBI_CAPTURE:
391 if (vfd->vidioc_try_fmt_vbi)
394 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
395 if (vfd->vidioc_try_fmt_vbi_output)
398 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
399 if (vfd->vidioc_try_fmt_vbi_capture)
402 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
403 if (vfd->vidioc_try_fmt_video_output)
406 case V4L2_BUF_TYPE_VBI_OUTPUT:
407 if (vfd->vidioc_try_fmt_vbi_output)
410 case V4L2_BUF_TYPE_PRIVATE:
411 if (vfd->vidioc_try_fmt_type_private)
418 static int __video_do_ioctl(struct inode *inode, struct file *file,
419 unsigned int cmd, void *arg)
421 struct video_device *vfd = video_devdata(file);
422 void *fh = file->private_data;
425 if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
426 !(vfd->debug | V4L2_DEBUG_IOCTL_ARG)) {
427 v4l_print_ioctl(vfd->name, cmd);
431 /* --- capabilities ------------------------------------------ */
432 case VIDIOC_QUERYCAP:
434 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
435 memset(cap, 0, sizeof(*cap));
437 if (!vfd->vidioc_querycap)
440 ret=vfd->vidioc_querycap(file, fh, cap);
442 dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
444 "capabilities=0x%08x\n",
445 cap->driver,cap->card,cap->bus_info,
451 /* --- priority ------------------------------------------ */
452 case VIDIOC_G_PRIORITY:
454 enum v4l2_priority *p=arg;
456 if (!vfd->vidioc_g_priority)
458 ret=vfd->vidioc_g_priority(file, fh, p);
460 dbgarg(cmd, "priority is %d\n", *p);
463 case VIDIOC_S_PRIORITY:
465 enum v4l2_priority *p=arg;
467 if (!vfd->vidioc_s_priority)
469 dbgarg(cmd, "setting priority to %d\n", *p);
470 ret=vfd->vidioc_s_priority(file, fh, *p);
474 /* --- capture ioctls ---------------------------------------- */
475 case VIDIOC_ENUM_FMT:
477 struct v4l2_fmtdesc *f = arg;
478 enum v4l2_buf_type type;
483 memset(f,0,sizeof(*f));
488 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
489 if (vfd->vidioc_enum_fmt_cap)
490 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
492 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
493 if (vfd->vidioc_enum_fmt_overlay)
494 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
496 case V4L2_BUF_TYPE_VBI_CAPTURE:
497 if (vfd->vidioc_enum_fmt_vbi)
498 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
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,
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,
510 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
511 if (vfd->vidioc_enum_fmt_video_output)
512 ret=vfd->vidioc_enum_fmt_video_output(file,
515 case V4L2_BUF_TYPE_VBI_OUTPUT:
516 if (vfd->vidioc_enum_fmt_vbi_output)
517 ret=vfd->vidioc_enum_fmt_vbi_output(file,
520 case V4L2_BUF_TYPE_PRIVATE:
521 if (vfd->vidioc_enum_fmt_type_private)
522 ret=vfd->vidioc_enum_fmt_type_private(file,
527 dbgarg (cmd, "index=%d, type=%d, flags=%d, "
529 " pixelformat=0x%8x\n",
530 f->index, f->type, f->flags,
538 struct v4l2_format *f = (struct v4l2_format *)arg;
539 enum v4l2_buf_type type=f->type;
541 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
544 /* FIXME: Should be one dump per type */
545 dbgarg (cmd, "type=%s\n", prt_names(type,
546 v4l2_type_names_FIXME));
549 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
550 if (vfd->vidioc_g_fmt_cap)
551 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
553 v4l_print_pix_fmt(vfd,&f->fmt.pix);
555 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
556 if (vfd->vidioc_g_fmt_overlay)
557 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
559 case V4L2_BUF_TYPE_VBI_CAPTURE:
560 if (vfd->vidioc_g_fmt_vbi)
561 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
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);
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);
571 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
572 if (vfd->vidioc_g_fmt_video_output)
573 ret=vfd->vidioc_g_fmt_video_output(file,
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);
580 case V4L2_BUF_TYPE_PRIVATE:
581 if (vfd->vidioc_g_fmt_type_private)
582 ret=vfd->vidioc_g_fmt_type_private(file,
591 struct v4l2_format *f = (struct v4l2_format *)arg;
593 /* FIXME: Should be one dump per type */
594 dbgarg (cmd, "type=%s\n", prt_names(f->type,
595 v4l2_type_names_FIXME));
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);
603 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
604 if (vfd->vidioc_s_fmt_overlay)
605 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
607 case V4L2_BUF_TYPE_VBI_CAPTURE:
608 if (vfd->vidioc_s_fmt_vbi)
609 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
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);
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);
619 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
620 if (vfd->vidioc_s_fmt_video_output)
621 ret=vfd->vidioc_s_fmt_video_output(file,
624 case V4L2_BUF_TYPE_VBI_OUTPUT:
625 if (vfd->vidioc_s_fmt_vbi_output)
626 ret=vfd->vidioc_s_fmt_vbi_output(file,
629 case V4L2_BUF_TYPE_PRIVATE:
630 if (vfd->vidioc_s_fmt_type_private)
631 ret=vfd->vidioc_s_fmt_type_private(file,
639 struct v4l2_format *f = (struct v4l2_format *)arg;
641 /* FIXME: Should be one dump per type */
642 dbgarg (cmd, "type=%s\n", prt_names(f->type,
643 v4l2_type_names_FIXME));
645 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
646 if (vfd->vidioc_try_fmt_cap)
647 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
649 v4l_print_pix_fmt(vfd,&f->fmt.pix);
651 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
652 if (vfd->vidioc_try_fmt_overlay)
653 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
655 case V4L2_BUF_TYPE_VBI_CAPTURE:
656 if (vfd->vidioc_try_fmt_vbi)
657 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
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,
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,
669 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
670 if (vfd->vidioc_try_fmt_video_output)
671 ret=vfd->vidioc_try_fmt_video_output(file,
674 case V4L2_BUF_TYPE_VBI_OUTPUT:
675 if (vfd->vidioc_try_fmt_vbi_output)
676 ret=vfd->vidioc_try_fmt_vbi_output(file,
679 case V4L2_BUF_TYPE_PRIVATE:
680 if (vfd->vidioc_try_fmt_type_private)
681 ret=vfd->vidioc_try_fmt_type_private(file,
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.
694 struct v4l2_requestbuffers *p=arg;
696 if (!vfd->vidioc_reqbufs)
698 ret = check_fmt (vfd, p->type);
702 ret=vfd->vidioc_reqbufs(file, fh, p);
703 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
705 prt_names(p->type,v4l2_type_names_FIXME),
706 prt_names(p->memory,v4l2_memory_names));
709 case VIDIOC_QUERYBUF:
711 struct v4l2_buffer *p=arg;
713 if (!vfd->vidioc_querybuf)
715 ret = check_fmt (vfd, p->type);
719 ret=vfd->vidioc_querybuf(file, fh, p);
726 struct v4l2_buffer *p=arg;
728 if (!vfd->vidioc_qbuf)
730 ret = check_fmt (vfd, p->type);
734 ret=vfd->vidioc_qbuf(file, fh, p);
741 struct v4l2_buffer *p=arg;
742 if (!vfd->vidioc_qbuf)
744 ret = check_fmt (vfd, p->type);
748 ret=vfd->vidioc_qbuf(file, fh, p);
757 if (!vfd->vidioc_overlay)
759 dbgarg (cmd, "value=%d\n",*i);
760 ret=vfd->vidioc_overlay(file, fh, *i);
764 /* --- streaming capture ------------------------------------- */
767 struct video_mbuf *p=arg;
769 memset(p,0,sizeof(p));
771 if (!vfd->vidiocgmbuf)
773 ret=vfd->vidiocgmbuf(file, fh, p);
775 dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
777 (unsigned long)p->offsets);
783 struct v4l2_framebuffer *p=arg;
784 if (!vfd->vidioc_g_fbuf)
786 ret=vfd->vidioc_g_fbuf(file, fh, arg);
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);
797 struct v4l2_framebuffer *p=arg;
798 if (!vfd->vidioc_s_fbuf)
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);
808 case VIDIOC_STREAMON:
810 enum v4l2_buf_type i = *(int *)arg;
811 if (!vfd->vidioc_streamon)
813 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
814 ret=vfd->vidioc_streamon(file, fh,i);
817 case VIDIOC_STREAMOFF:
819 enum v4l2_buf_type i = *(int *)arg;
821 if (!vfd->vidioc_streamoff)
823 dbgarg (cmd, "type=%s\n", prt_names(i,v4l2_type_names_FIXME));
824 ret=vfd->vidioc_streamoff(file, fh, i);
827 /* ---------- tv norms ---------- */
830 struct v4l2_standard *p = arg;
831 unsigned int index = p->index;
833 if (!vfd->tvnormsize) {
834 printk (KERN_WARNING "%s: no TV norms defined!\n",
839 if (index<=0 || index >= vfd->tvnormsize) {
843 v4l2_video_std_construct(p, vfd->tvnorms[p->index].id,
844 vfd->tvnorms[p->index].name);
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,
859 v4l2_std_id *id = arg;
861 *id = vfd->current_norm;
863 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
870 v4l2_std_id *id = arg;
873 if (!vfd->tvnormsize) {
874 printk (KERN_WARNING "%s: no TV norms defined!\n",
879 dbgarg (cmd, "value=%Lu\n", (long long unsigned) *id);
881 /* First search for exact match */
882 for (i = 0; i < vfd->tvnormsize; i++)
883 if (*id == vfd->tvnorms[i].id)
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)
890 if (i == vfd->tvnormsize) {
894 /* Calls the specific handler */
895 if (vfd->vidioc_s_std)
896 ret=vfd->vidioc_s_std(file, fh, i);
900 /* Updates standard information */
902 vfd->current_norm=*id;
906 case VIDIOC_QUERYSTD:
910 if (!vfd->vidioc_querystd)
912 ret=vfd->vidioc_querystd(file, fh, arg);
914 dbgarg (cmd, "detected std=%Lu\n",
915 (unsigned long long)*p);
918 /* ------ input switching ---------- */
919 /* FIXME: Inputs can be handled inside videodev2 */
920 case VIDIOC_ENUMINPUT:
922 struct v4l2_input *p=arg;
925 if (!vfd->vidioc_enum_input)
927 memset(p, 0, sizeof(*p));
930 ret=vfd->vidioc_enum_input(file, fh, p);
932 dbgarg (cmd, "index=%d, name=%s, type=%d, "
934 "tuner=%d, std=%Ld, status=%d\n",
935 p->index,p->name,p->type,p->audioset,
937 (unsigned long long)p->std,
943 unsigned int *i = arg;
945 if (!vfd->vidioc_g_input)
947 ret=vfd->vidioc_g_input(file, fh, i);
949 dbgarg (cmd, "value=%d\n",*i);
954 unsigned int *i = arg;
956 if (!vfd->vidioc_s_input)
958 dbgarg (cmd, "value=%d\n",*i);
959 ret=vfd->vidioc_s_input(file, fh, *i);
963 /* ------ output switching ---------- */
964 case VIDIOC_G_OUTPUT:
966 unsigned int *i = arg;
968 if (!vfd->vidioc_g_output)
970 ret=vfd->vidioc_g_output(file, fh, i);
972 dbgarg (cmd, "value=%d\n",*i);
975 case VIDIOC_S_OUTPUT:
977 unsigned int *i = arg;
979 if (!vfd->vidioc_s_output)
981 dbgarg (cmd, "value=%d\n",*i);
982 ret=vfd->vidioc_s_output(file, fh, *i);
986 /* --- controls ---------------------------------------------- */
987 case VIDIOC_QUERYCTRL:
989 struct v4l2_queryctrl *p=arg;
991 if (!vfd->vidioc_queryctrl)
993 ret=vfd->vidioc_queryctrl(file, fh, p);
996 dbgarg (cmd, "id=%d, type=%d, name=%s, "
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,
1006 struct v4l2_control *p = arg;
1008 if (!vfd->vidioc_g_ctrl)
1010 dbgarg(cmd, "Enum for index=%d\n", p->id);
1012 ret=vfd->vidioc_g_ctrl(file, fh, p);
1014 dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1019 struct v4l2_control *p = arg;
1021 if (!vfd->vidioc_s_ctrl)
1023 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1025 ret=vfd->vidioc_s_ctrl(file, fh, p);
1028 case VIDIOC_G_EXT_CTRLS:
1030 struct v4l2_ext_controls *p = arg;
1032 if (vfd->vidioc_g_ext_ctrls) {
1033 dbgarg(cmd, "count=%d\n", p->count);
1035 ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1039 case VIDIOC_S_EXT_CTRLS:
1041 struct v4l2_ext_controls *p = arg;
1043 if (vfd->vidioc_s_ext_ctrls) {
1044 dbgarg(cmd, "count=%d\n", p->count);
1046 ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1050 case VIDIOC_TRY_EXT_CTRLS:
1052 struct v4l2_ext_controls *p = arg;
1054 if (vfd->vidioc_try_ext_ctrls) {
1055 dbgarg(cmd, "count=%d\n", p->count);
1057 ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1061 case VIDIOC_QUERYMENU:
1063 struct v4l2_querymenu *p=arg;
1064 if (!vfd->vidioc_querymenu)
1066 ret=vfd->vidioc_querymenu(file, fh, p);
1068 dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1069 p->id,p->index,p->name);
1072 /* --- audio ---------------------------------------------- */
1073 case VIDIOC_ENUMAUDIO:
1075 struct v4l2_audio *p=arg;
1077 if (!vfd->vidioc_enumaudio)
1079 dbgarg(cmd, "Enum for index=%d\n", p->index);
1080 ret=vfd->vidioc_enumaudio(file, fh, p);
1082 dbgarg2("index=%d, name=%s, capability=%d, "
1083 "mode=%d\n",p->index,p->name,
1084 p->capability, p->mode);
1087 case VIDIOC_G_AUDIO:
1089 struct v4l2_audio *p=arg;
1091 if (!vfd->vidioc_g_audio)
1093 dbgarg(cmd, "Get for index=%d\n", p->index);
1094 ret=vfd->vidioc_g_audio(file, fh, p);
1096 dbgarg2("index=%d, name=%s, capability=%d, "
1097 "mode=%d\n",p->index,
1098 p->name,p->capability, p->mode);
1101 case VIDIOC_S_AUDIO:
1103 struct v4l2_audio *p=arg;
1105 if (!vfd->vidioc_s_audio)
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);
1113 case VIDIOC_ENUMAUDOUT:
1115 struct v4l2_audioout *p=arg;
1117 if (!vfd->vidioc_enumaudout)
1119 dbgarg(cmd, "Enum for index=%d\n", p->index);
1120 ret=vfd->vidioc_enumaudout(file, fh, p);
1122 dbgarg2("index=%d, name=%s, capability=%d, "
1123 "mode=%d\n", p->index, p->name,
1124 p->capability,p->mode);
1127 case VIDIOC_G_AUDOUT:
1129 struct v4l2_audioout *p=arg;
1131 if (!vfd->vidioc_g_audout)
1133 dbgarg(cmd, "Enum for index=%d\n", p->index);
1134 ret=vfd->vidioc_g_audout(file, fh, p);
1136 dbgarg2("index=%d, name=%s, capability=%d, "
1137 "mode=%d\n", p->index, p->name,
1138 p->capability,p->mode);
1141 case VIDIOC_S_AUDOUT:
1143 struct v4l2_audioout *p=arg;
1145 if (!vfd->vidioc_s_audout)
1147 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1148 "mode=%d\n", p->index, p->name,
1149 p->capability,p->mode);
1151 ret=vfd->vidioc_s_audout(file, fh, p);
1154 case VIDIOC_G_MODULATOR:
1156 struct v4l2_modulator *p=arg;
1157 if (!vfd->vidioc_g_modulator)
1159 ret=vfd->vidioc_g_modulator(file, fh, p);
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,
1169 case VIDIOC_S_MODULATOR:
1171 struct v4l2_modulator *p=arg;
1172 if (!vfd->vidioc_s_modulator)
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);
1183 struct v4l2_crop *p=arg;
1184 if (!vfd->vidioc_g_crop)
1186 ret=vfd->vidioc_g_crop(file, fh, p);
1188 dbgarg(cmd, "type=%d\n", p->type);
1189 dbgrect(vfd, "", &p->c);
1195 struct v4l2_crop *p=arg;
1196 if (!vfd->vidioc_s_crop)
1198 dbgarg(cmd, "type=%d\n", p->type);
1199 dbgrect(vfd, "", &p->c);
1200 ret=vfd->vidioc_s_crop(file, fh, p);
1203 case VIDIOC_CROPCAP:
1205 struct v4l2_cropcap *p=arg;
1206 /*FIXME: Should also show v4l2_fract pixelaspect */
1207 if (!vfd->vidioc_cropcap)
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);
1215 case VIDIOC_G_MPEGCOMP:
1217 struct v4l2_mpeg_compression *p=arg;
1219 /*FIXME: Several fields not shown */
1220 if (!vfd->vidioc_g_mpegcomp)
1222 ret=vfd->vidioc_g_mpegcomp(file, fh, p);
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);
1238 case VIDIOC_S_MPEGCOMP:
1240 struct v4l2_mpeg_compression *p=arg;
1241 /*FIXME: Several fields not shown */
1242 if (!vfd->vidioc_s_mpegcomp)
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,
1254 ret=vfd->vidioc_s_mpegcomp(file, fh, p);
1257 case VIDIOC_G_JPEGCOMP:
1259 struct v4l2_jpegcompression *p=arg;
1260 if (!vfd->vidioc_g_jpegcomp)
1262 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
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);
1271 case VIDIOC_S_JPEGCOMP:
1273 struct v4l2_jpegcompression *p=arg;
1274 if (!vfd->vidioc_g_jpegcomp)
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);
1285 struct v4l2_streamparm *p=arg;
1286 if (!vfd->vidioc_g_parm)
1288 ret=vfd->vidioc_g_parm(file, fh, p);
1289 dbgarg (cmd, "type=%d\n", p->type);
1294 struct v4l2_streamparm *p=arg;
1295 if (!vfd->vidioc_s_parm)
1297 dbgarg (cmd, "type=%d\n", p->type);
1298 ret=vfd->vidioc_s_parm(file, fh, p);
1301 case VIDIOC_G_TUNER:
1303 struct v4l2_tuner *p=arg;
1304 if (!vfd->vidioc_g_tuner)
1306 ret=vfd->vidioc_g_tuner(file, fh, p);
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);
1318 case VIDIOC_S_TUNER:
1320 struct v4l2_tuner *p=arg;
1321 if (!vfd->vidioc_s_tuner)
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,
1330 ret=vfd->vidioc_s_tuner(file, fh, p);
1333 case VIDIOC_G_FREQUENCY:
1335 struct v4l2_frequency *p=arg;
1336 if (!vfd->vidioc_g_frequency)
1338 ret=vfd->vidioc_g_frequency(file, fh, p);
1340 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1341 p->tuner,p->type,p->frequency);
1344 case VIDIOC_S_FREQUENCY:
1346 struct v4l2_frequency *p=arg;
1347 if (!vfd->vidioc_s_frequency)
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);
1354 case VIDIOC_G_SLICED_VBI_CAP:
1356 struct v4l2_sliced_vbi_cap *p=arg;
1357 if (!vfd->vidioc_g_sliced_vbi_cap)
1359 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1361 dbgarg (cmd, "service_set=%d\n", p->service_set);
1364 case VIDIOC_LOG_STATUS:
1366 if (!vfd->vidioc_log_status)
1368 ret=vfd->vidioc_log_status(file, fh);
1372 /* --- Others --------------------------------------------- */
1375 ret=v4l_compat_translate_ioctl(inode,file,cmd,arg,__video_do_ioctl);
1378 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1380 printk ("%s: err:\n", vfd->name);
1381 v4l_print_ioctl(vfd->name, cmd);
1388 int video_ioctl2 (struct inode *inode, struct file *file,
1389 unsigned int cmd, unsigned long arg)
1396 size_t ctrls_size = 0;
1397 void __user *user_ptr = NULL;
1399 #ifdef __OLD_VIDIOC_
1400 cmd = video_fix_command(cmd);
1402 is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1403 cmd == VIDIOC_TRY_EXT_CTRLS);
1405 /* Copy arguments into temp kernel buffer */
1406 switch (_IOC_DIR(cmd)) {
1412 case (_IOC_WRITE | _IOC_READ):
1413 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1416 /* too big to allocate from stack */
1417 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1424 if (_IOC_DIR(cmd) & _IOC_WRITE)
1425 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1431 struct v4l2_ext_controls *p = parg;
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;
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);
1445 if (copy_from_user(mbuf, user_ptr, ctrls_size))
1452 err = __video_do_ioctl(inode, file, cmd, parg);
1453 if (err == -ENOIOCTLCMD)
1456 struct v4l2_ext_controls *p = parg;
1458 p->controls = (void *)user_ptr;
1459 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1467 /* Copy results into user buffer */
1468 switch (_IOC_DIR(cmd))
1471 case (_IOC_WRITE | _IOC_READ):
1472 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1483 static struct file_operations video_fops;
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, ...
1492 * 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).
1497 * Zero is returned on success.
1501 * %VFL_TYPE_GRABBER - A frame grabber
1503 * %VFL_TYPE_VTX - A teletext device
1505 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
1507 * %VFL_TYPE_RADIO - A radio card
1510 int video_register_device(struct video_device *vfd, int type, int nr)
1519 case VFL_TYPE_GRABBER:
1520 base=MINOR_VFL_TYPE_GRABBER_MIN;
1521 end=MINOR_VFL_TYPE_GRABBER_MAX+1;
1522 name_base = "video";
1525 base=MINOR_VFL_TYPE_VTX_MIN;
1526 end=MINOR_VFL_TYPE_VTX_MAX+1;
1530 base=MINOR_VFL_TYPE_VBI_MIN;
1531 end=MINOR_VFL_TYPE_VBI_MAX+1;
1534 case VFL_TYPE_RADIO:
1535 base=MINOR_VFL_TYPE_RADIO_MIN;
1536 end=MINOR_VFL_TYPE_RADIO_MAX+1;
1537 name_base = "radio";
1543 /* pick a minor number */
1544 mutex_lock(&videodev_lock);
1545 if (nr >= 0 && nr < end-base) {
1546 /* use the one the driver asked for */
1548 if (NULL != video_device[i]) {
1549 mutex_unlock(&videodev_lock);
1553 /* use first free */
1554 for(i=base;i<end;i++)
1555 if (NULL == video_device[i])
1558 mutex_unlock(&videodev_lock);
1562 video_device[i]=vfd;
1564 mutex_unlock(&videodev_lock);
1565 mutex_init(&vfd->lock);
1568 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
1570 vfd->class_dev.dev = vfd->dev;
1571 vfd->class_dev.class = &video_class;
1572 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
1573 sprintf(vfd->class_dev.class_id, "%s%d", name_base, i - base);
1574 class_device_register(&vfd->class_dev);
1575 class_device_create_file(&vfd->class_dev,
1576 &class_device_attr_name);
1579 /* needed until all drivers are fixed */
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);
1589 * video_unregister_device - unregister a video4linux device
1590 * @vfd: the device to unregister
1592 * This unregisters the passed device and deassigns the minor
1593 * number. Future open calls will be met with errors.
1596 void video_unregister_device(struct video_device *vfd)
1598 mutex_lock(&videodev_lock);
1599 if(video_device[vfd->minor]!=vfd)
1600 panic("videodev: bad unregister");
1602 video_device[vfd->minor]=NULL;
1603 class_device_unregister(&vfd->class_dev);
1604 mutex_unlock(&videodev_lock);
1608 * Video fs operations
1610 static struct file_operations video_fops=
1612 .owner = THIS_MODULE,
1613 .llseek = no_llseek,
1618 * Initialise video for linux
1621 static int __init videodev_init(void)
1625 printk(KERN_INFO "Linux video capture interface: v2.00\n");
1626 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
1627 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
1631 ret = class_register(&video_class);
1633 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1634 printk(KERN_WARNING "video_dev: class_register failed\n");
1641 static void __exit videodev_exit(void)
1643 class_unregister(&video_class);
1644 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
1647 module_init(videodev_init)
1648 module_exit(videodev_exit)
1650 EXPORT_SYMBOL(video_register_device);
1651 EXPORT_SYMBOL(video_unregister_device);
1652 EXPORT_SYMBOL(video_devdata);
1653 EXPORT_SYMBOL(video_usercopy);
1654 EXPORT_SYMBOL(video_exclusive_open);
1655 EXPORT_SYMBOL(video_exclusive_release);
1656 EXPORT_SYMBOL(video_ioctl2);
1657 EXPORT_SYMBOL(video_device_alloc);
1658 EXPORT_SYMBOL(video_device_release);
1660 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1661 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1662 MODULE_LICENSE("GPL");