2 * Video capture interface for Linux
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 * Author: Alan Cox, <alan@redhat.com>
14 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
15 * - Added procfs support
18 #include <linux/config.h>
19 #include <linux/version.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/kmod.h>
30 #include <linux/slab.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <asm/semaphore.h>
35 #include <linux/videodev.h>
37 #define VIDEO_NUM_DEVICES 256
43 static struct video_device *video_device[VIDEO_NUM_DEVICES];
44 static DECLARE_MUTEX(videodev_lock);
47 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
49 #include <linux/proc_fs.h>
51 struct videodev_proc_data {
52 struct list_head proc_list;
54 struct video_device *vdev;
55 struct proc_dir_entry *proc_entry;
58 static struct proc_dir_entry *video_dev_proc_entry = NULL;
59 struct proc_dir_entry *video_proc_entry = NULL;
60 EXPORT_SYMBOL(video_proc_entry);
61 LIST_HEAD(videodev_proc_list);
63 #endif /* CONFIG_PROC_FS && CONFIG_VIDEO_PROC_FS */
67 * Read will do some smarts later on. Buffer pin etc.
70 static ssize_t video_read(struct file *file,
71 char *buf, size_t count, loff_t *ppos)
73 struct video_device *vfl = video_devdata(file);
75 return vfl->read(vfl, buf, count, file->f_flags&O_NONBLOCK);
82 * Write for now does nothing. No reason it shouldnt do overlay setting
83 * for some boards I guess..
86 static ssize_t video_write(struct file *file, const char *buf,
87 size_t count, loff_t *ppos)
89 struct video_device *vfl = video_devdata(file);
91 return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK);
96 struct video_device* video_devdata(struct file *file)
98 return video_device[minor(file->f_dentry->d_inode->i_rdev)];
102 * Poll to see if we're readable, can probably be used for timing on incoming
106 static unsigned int video_poll(struct file *file, poll_table * wait)
108 struct video_device *vfl = video_devdata(file);
110 return vfl->poll(vfl, file, wait);
117 * Open a video device.
120 static int video_open(struct inode *inode, struct file *file)
122 unsigned int minor = minor(inode->i_rdev);
124 struct video_device *vfl;
126 if(minor>=VIDEO_NUM_DEVICES)
128 down(&videodev_lock);
129 vfl=video_device[minor];
134 sprintf (modname, "char-major-%d-%d", VIDEO_MAJOR, minor);
135 request_module(modname);
136 down(&videodev_lock);
137 vfl=video_device[minor];
144 struct file_operations *old_fops;
146 old_fops = file->f_op;
147 file->f_op = fops_get(vfl->fops);
149 err = file->f_op->open(inode,file);
151 fops_put(file->f_op);
152 file->f_op = fops_get(old_fops);
161 vfl->users++; /* In case vfl->open sleeps */
164 __MOD_INC_USE_COUNT(vfl->owner);
167 err=vfl->open(vfl,0); /* Tell the device it is open */
171 __MOD_DEC_USE_COUNT(vfl->owner);
183 * Last close of a video for Linux device
186 static int video_release(struct inode *inode, struct file *file)
188 struct video_device *vfl;
189 struct module *owner;
191 vfl = video_devdata(file);
196 down(&videodev_lock);
197 /* ->close() might have called video_device_unregister()
198 in case of a hot unplug, thus we have to get vfl again */
199 vfl = video_devdata(file);
203 __MOD_DEC_USE_COUNT(owner);
208 static int video_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, unsigned long arg)
211 struct video_device *vfl = video_devdata(file);
212 int err=vfl->ioctl(vfl, cmd, (void *)arg);
214 if(err!=-ENOIOCTLCMD)
225 * We need to do MMAP support
228 int video_mmap(struct file *file, struct vm_area_struct *vma)
231 struct video_device *vfl = video_devdata(file);
234 ret = vfl->mmap(vfl, (char *)vma->vm_start,
235 (unsigned long)(vma->vm_end-vma->vm_start));
242 * helper function -- handles userspace copying for ioctl arguments
245 video_usercopy(struct inode *inode, struct file *file,
246 unsigned int cmd, unsigned long arg,
247 int (*func)(struct inode *inode, struct file *file,
248 unsigned int cmd, void *arg))
255 /* Copy arguments into temp kernel buffer */
256 switch (_IOC_DIR(cmd)) {
260 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
262 case (_IOC_WRITE | _IOC_READ):
263 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
266 /* too big to allocate from stack */
267 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
274 if (copy_from_user(parg, (void *)arg, _IOC_SIZE(cmd)))
280 err = func(inode, file, cmd, parg);
281 if (err == -ENOIOCTLCMD)
286 /* Copy results into user buffer */
287 switch (_IOC_DIR(cmd))
290 case (_IOC_WRITE | _IOC_READ):
291 if (copy_to_user((void *)arg, parg, _IOC_SIZE(cmd)))
303 * open/release helper functions -- handle exclusive opens
305 extern int video_exclusive_open(struct inode *inode, struct file *file)
307 struct video_device *vfl = video_devdata(file);
320 extern int video_exclusive_release(struct inode *inode, struct file *file)
322 struct video_device *vfl = video_devdata(file);
332 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
334 /* Hmm... i'd like to see video_capability information here, but
335 * how can I access it (without changing the other drivers? -claudio
337 static int videodev_proc_read(char *page, char **start, off_t off,
338 int count, int *eof, void *data)
341 struct video_device *vfd = data;
342 struct videodev_proc_data *d;
343 struct list_head *tmp;
347 list_for_each (tmp, &videodev_proc_list) {
348 d = list_entry(tmp, struct videodev_proc_data, proc_list);
354 if (tmp == &videodev_proc_list)
357 #define PRINT_VID_TYPE(x) do { if (vfd->type & x) \
358 out += sprintf (out, "%c%s", c, #x); c='|';} while (0)
360 out += sprintf (out, "name : %s\n", vfd->name);
361 out += sprintf (out, "type :");
362 PRINT_VID_TYPE(VID_TYPE_CAPTURE);
363 PRINT_VID_TYPE(VID_TYPE_TUNER);
364 PRINT_VID_TYPE(VID_TYPE_TELETEXT);
365 PRINT_VID_TYPE(VID_TYPE_OVERLAY);
366 PRINT_VID_TYPE(VID_TYPE_CHROMAKEY);
367 PRINT_VID_TYPE(VID_TYPE_CLIPPING);
368 PRINT_VID_TYPE(VID_TYPE_FRAMERAM);
369 PRINT_VID_TYPE(VID_TYPE_SCALES);
370 PRINT_VID_TYPE(VID_TYPE_MONOCHROME);
371 PRINT_VID_TYPE(VID_TYPE_SUBCAPTURE);
372 PRINT_VID_TYPE(VID_TYPE_MPEG_DECODER);
373 PRINT_VID_TYPE(VID_TYPE_MPEG_ENCODER);
374 PRINT_VID_TYPE(VID_TYPE_MJPEG_DECODER);
375 PRINT_VID_TYPE(VID_TYPE_MJPEG_ENCODER);
376 out += sprintf (out, "\n");
377 out += sprintf (out, "hardware : 0x%x\n", vfd->hardware);
379 out += sprintf (out, "channels : %d\n", d->vcap.channels);
380 out += sprintf (out, "audios : %d\n", d->vcap.audios);
381 out += sprintf (out, "maxwidth : %d\n", d->vcap.maxwidth);
382 out += sprintf (out, "maxheight : %d\n", d->vcap.maxheight);
383 out += sprintf (out, "minwidth : %d\n", d->vcap.minwidth);
384 out += sprintf (out, "minheight : %d\n", d->vcap.minheight);
402 static void videodev_proc_create(void)
404 video_proc_entry = create_proc_entry("video", S_IFDIR, &proc_root);
406 if (video_proc_entry == NULL) {
407 printk("video_dev: unable to initialise /proc/video\n");
411 video_proc_entry->owner = THIS_MODULE;
412 video_dev_proc_entry = create_proc_entry("dev", S_IFDIR, video_proc_entry);
414 if (video_dev_proc_entry == NULL) {
415 printk("video_dev: unable to initialise /proc/video/dev\n");
419 video_dev_proc_entry->owner = THIS_MODULE;
422 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
423 static void videodev_proc_destroy(void)
425 if (video_dev_proc_entry != NULL)
426 remove_proc_entry("dev", video_proc_entry);
428 if (video_proc_entry != NULL)
429 remove_proc_entry("video", &proc_root);
433 static void videodev_proc_create_dev (struct video_device *vfd, char *name)
435 struct videodev_proc_data *d;
436 struct proc_dir_entry *p;
438 if (video_dev_proc_entry == NULL)
441 d = kmalloc (sizeof (struct videodev_proc_data), GFP_KERNEL);
445 p = create_proc_entry(name, S_IFREG|S_IRUGO|S_IWUSR, video_dev_proc_entry);
449 p->read_proc = videodev_proc_read;
453 strcpy (d->name, name);
455 /* How can I get capability information ? */
457 list_add (&d->proc_list, &videodev_proc_list);
460 static void videodev_proc_destroy_dev (struct video_device *vfd)
462 struct list_head *tmp;
463 struct videodev_proc_data *d;
465 list_for_each (tmp, &videodev_proc_list) {
466 d = list_entry(tmp, struct videodev_proc_data, proc_list);
467 if (vfd == d->vdev) {
468 remove_proc_entry(d->name, video_dev_proc_entry);
469 list_del (&d->proc_list);
476 #endif /* CONFIG_VIDEO_PROC_FS */
478 extern struct file_operations video_fops;
481 * video_register_device - register video4linux devices
482 * @vfd: video device structure we want to register
483 * @type: type of device to register
484 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
487 * The registration code assigns minor numbers based on the type
488 * requested. -ENFILE is returned in all the device slots for this
489 * category are full. If not then the minor field is set and the
490 * driver initialize function is called (if non %NULL).
492 * Zero is returned on success.
496 * %VFL_TYPE_GRABBER - A frame grabber
498 * %VFL_TYPE_VTX - A teletext device
500 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
502 * %VFL_TYPE_RADIO - A radio card
505 int video_register_device(struct video_device *vfd, int type, int nr)
516 case VFL_TYPE_GRABBER:
540 /* pick a minor number */
541 down(&videodev_lock);
544 for(i=base;i<end;i++)
545 if (NULL == video_device[i])
552 /* use the one the driver asked for */
554 if (NULL != video_device[i]) {
563 /* The init call may sleep so we book the slot out
566 if(vfd->initialize) {
567 err=vfd->initialize(vfd);
569 video_device[i]=NULL;
574 sprintf (name, "v4l/%s%d", name_base, i - base);
576 * Start the device root only. Anything else
577 * has serious privacy issues.
580 devfs_register (NULL, name, DEVFS_FL_DEFAULT,
581 VIDEO_MAJOR, vfd->minor,
582 S_IFCHR | S_IRUSR | S_IWUSR,
585 init_MUTEX(&vfd->lock);
587 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
588 sprintf (name, "%s%d", name_base, i - base);
589 videodev_proc_create_dev (vfd, name);
595 * video_unregister_device - unregister a video4linux device
596 * @vfd: the device to unregister
598 * This unregisters the passed device and deassigns the minor
599 * number. Future open calls will be met with errors.
602 void video_unregister_device(struct video_device *vfd)
604 down(&videodev_lock);
606 if(video_device[vfd->minor]!=vfd)
607 panic("videodev: bad unregister");
609 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
610 videodev_proc_destroy_dev (vfd);
613 devfs_unregister (vfd->devfs_handle);
614 video_device[vfd->minor]=NULL;
620 static struct file_operations video_fops=
629 release: video_release,
634 * Initialise video for linux
637 static int __init videodev_init(void)
639 printk(KERN_INFO "Linux video capture interface: v1.00\n");
640 if(devfs_register_chrdev(VIDEO_MAJOR,"video_capture", &video_fops))
642 printk("video_dev: unable to get major %d\n", VIDEO_MAJOR);
646 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
647 videodev_proc_create ();
653 static void __exit videodev_exit(void)
655 #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
656 videodev_proc_destroy ();
658 devfs_unregister_chrdev(VIDEO_MAJOR, "video_capture");
661 module_init(videodev_init)
662 module_exit(videodev_exit)
664 EXPORT_SYMBOL(video_register_device);
665 EXPORT_SYMBOL(video_unregister_device);
666 EXPORT_SYMBOL(video_devdata);
667 EXPORT_SYMBOL(video_usercopy);
668 EXPORT_SYMBOL(video_exclusive_open);
669 EXPORT_SYMBOL(video_exclusive_release);
671 MODULE_AUTHOR("Alan Cox");
672 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
673 MODULE_LICENSE("GPL");