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 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/slab.h>
28 #include <linux/smp_lock.h>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
32 #include <media/v4l2-common.h>
34 #define VIDEO_NUM_DEVICES 256
35 #define VIDEO_NAME "video4linux"
41 static ssize_t show_index(struct device *cd,
42 struct device_attribute *attr, char *buf)
44 struct video_device *vfd = container_of(cd, struct video_device, dev);
45 return sprintf(buf, "%i\n", vfd->index);
48 static ssize_t show_name(struct device *cd,
49 struct device_attribute *attr, char *buf)
51 struct video_device *vfd = container_of(cd, struct video_device, dev);
52 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
55 static struct device_attribute video_device_attrs[] = {
56 __ATTR(name, S_IRUGO, show_name, NULL),
57 __ATTR(index, S_IRUGO, show_index, NULL),
61 struct video_device *video_device_alloc(void)
63 struct video_device *vfd;
65 vfd = kzalloc(sizeof(*vfd), GFP_KERNEL);
68 EXPORT_SYMBOL(video_device_alloc);
70 void video_device_release(struct video_device *vfd)
74 EXPORT_SYMBOL(video_device_release);
76 void video_device_release_empty(struct video_device *vfd)
79 /* Only valid when the video_device struct is a static. */
81 EXPORT_SYMBOL(video_device_release_empty);
83 static void video_release(struct device *cd)
85 struct video_device *vfd = container_of(cd, struct video_device, dev);
88 /* needed until all drivers are fixed */
95 static struct class video_class = {
97 .dev_attrs = video_device_attrs,
98 .dev_release = video_release,
105 static struct video_device *video_device[VIDEO_NUM_DEVICES];
106 static DEFINE_MUTEX(videodev_lock);
108 struct video_device *video_devdata(struct file *file)
110 return video_device[iminor(file->f_path.dentry->d_inode)];
112 EXPORT_SYMBOL(video_devdata);
115 * Open a video device - FIXME: Obsoleted
117 static int video_open(struct inode *inode, struct file *file)
119 unsigned int minor = iminor(inode);
121 struct video_device *vfl;
122 const struct file_operations *old_fops;
124 if (minor >= VIDEO_NUM_DEVICES)
126 mutex_lock(&videodev_lock);
127 vfl = video_device[minor];
129 mutex_unlock(&videodev_lock);
130 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
131 mutex_lock(&videodev_lock);
132 vfl = video_device[minor];
134 mutex_unlock(&videodev_lock);
138 old_fops = file->f_op;
139 file->f_op = fops_get(vfl->fops);
140 if (file->f_op->open)
141 err = file->f_op->open(inode, file);
143 fops_put(file->f_op);
144 file->f_op = fops_get(old_fops);
147 mutex_unlock(&videodev_lock);
152 * get_index - assign stream number based on parent device
153 * @vdev: video_device to assign index number to, vdev->dev should be assigned
154 * @num: -1 if auto assign, requested number otherwise
157 * returns -ENFILE if num is already in use, a free index number if
160 static int get_index(struct video_device *vdev, int num)
163 const int max_index = sizeof(used) * 8 - 1;
166 /* Currently a single v4l driver instance cannot create more than
168 Increase to u64 or an array of u32 if more are needed. */
169 if (num > max_index) {
170 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
174 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
175 if (video_device[i] != NULL &&
176 video_device[i] != vdev &&
177 video_device[i]->parent == vdev->parent) {
178 used |= 1 << video_device[i]->index;
183 if (used & (1 << num))
189 return i > max_index ? -ENFILE : i;
192 static const struct file_operations video_fops;
194 int video_register_device(struct video_device *vfd, int type, int nr)
196 return video_register_device_index(vfd, type, nr, -1);
198 EXPORT_SYMBOL(video_register_device);
201 * video_register_device_index - register video4linux devices
202 * @vfd: video device structure we want to register
203 * @type: type of device to register
204 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
206 * @index: stream number based on parent device;
207 * -1 if auto assign, requested number otherwise
209 * The registration code assigns minor numbers based on the type
210 * requested. -ENFILE is returned in all the device slots for this
211 * category are full. If not then the minor field is set and the
212 * driver initialize function is called (if non %NULL).
214 * Zero is returned on success.
218 * %VFL_TYPE_GRABBER - A frame grabber
220 * %VFL_TYPE_VTX - A teletext device
222 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
224 * %VFL_TYPE_RADIO - A radio card
227 int video_register_device_index(struct video_device *vfd, int type, int nr,
243 case VFL_TYPE_GRABBER:
244 base = MINOR_VFL_TYPE_GRABBER_MIN;
245 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
249 base = MINOR_VFL_TYPE_VTX_MIN;
250 end = MINOR_VFL_TYPE_VTX_MAX+1;
254 base = MINOR_VFL_TYPE_VBI_MIN;
255 end = MINOR_VFL_TYPE_VBI_MAX+1;
259 base = MINOR_VFL_TYPE_RADIO_MIN;
260 end = MINOR_VFL_TYPE_RADIO_MAX+1;
264 printk(KERN_ERR "%s called with unknown type: %d\n",
269 /* pick a minor number */
270 mutex_lock(&videodev_lock);
271 if (nr >= 0 && nr < end-base) {
272 /* use the one the driver asked for */
274 if (NULL != video_device[i]) {
275 mutex_unlock(&videodev_lock);
280 for (i = base; i < end; i++)
281 if (NULL == video_device[i])
284 mutex_unlock(&videodev_lock);
288 video_device[i] = vfd;
289 vfd->vfl_type = type;
292 ret = get_index(vfd, index);
295 mutex_unlock(&videodev_lock);
298 printk(KERN_ERR "%s: get_index failed\n", __func__);
303 memset(&vfd->dev, 0x00, sizeof(vfd->dev));
304 vfd->dev.class = &video_class;
305 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
307 vfd->dev.parent = vfd->parent;
308 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
309 ret = device_register(&vfd->dev);
311 printk(KERN_ERR "%s: device_register failed\n", __func__);
316 /* needed until all drivers are fixed */
318 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
319 "Please fix your driver for proper sysfs support, see "
320 "http://lwn.net/Articles/36850/\n", vfd->name);
325 mutex_lock(&videodev_lock);
326 video_device[vfd->minor] = NULL;
328 mutex_unlock(&videodev_lock);
331 EXPORT_SYMBOL(video_register_device_index);
334 * video_unregister_device - unregister a video4linux device
335 * @vfd: the device to unregister
337 * This unregisters the passed device and deassigns the minor
338 * number. Future open calls will be met with errors.
341 void video_unregister_device(struct video_device *vfd)
343 mutex_lock(&videodev_lock);
344 if (video_device[vfd->minor] != vfd)
345 panic("videodev: bad unregister");
347 video_device[vfd->minor] = NULL;
348 device_unregister(&vfd->dev);
349 mutex_unlock(&videodev_lock);
351 EXPORT_SYMBOL(video_unregister_device);
354 * Video fs operations
356 static const struct file_operations video_fops = {
357 .owner = THIS_MODULE,
363 * Initialise video for linux
366 static int __init videodev_init(void)
370 printk(KERN_INFO "Linux video capture interface: v2.00\n");
371 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
372 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
376 ret = class_register(&video_class);
378 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
379 printk(KERN_WARNING "video_dev: class_register failed\n");
386 static void __exit videodev_exit(void)
388 class_unregister(&video_class);
389 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
392 module_init(videodev_init)
393 module_exit(videodev_exit)
395 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
396 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
397 MODULE_LICENSE("GPL");