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);
46 return sprintf(buf, "%i\n", vfd->index);
49 static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
52 struct video_device *vfd = container_of(cd, struct video_device, dev);
54 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
57 static struct device_attribute video_device_attrs[] = {
58 __ATTR(name, S_IRUGO, show_name, NULL),
59 __ATTR(index, S_IRUGO, show_index, NULL),
63 struct video_device *video_device_alloc(void)
65 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
67 EXPORT_SYMBOL(video_device_alloc);
69 void video_device_release(struct video_device *vfd)
73 EXPORT_SYMBOL(video_device_release);
75 void video_device_release_empty(struct video_device *vfd)
78 /* Only valid when the video_device struct is a static. */
80 EXPORT_SYMBOL(video_device_release_empty);
82 static void video_release(struct device *cd)
84 struct video_device *vfd = container_of(cd, struct video_device, dev);
89 static struct class video_class = {
91 .dev_attrs = video_device_attrs,
92 .dev_release = video_release,
99 static struct video_device *video_device[VIDEO_NUM_DEVICES];
100 static DEFINE_MUTEX(videodev_lock);
102 struct video_device *video_devdata(struct file *file)
104 return video_device[iminor(file->f_path.dentry->d_inode)];
106 EXPORT_SYMBOL(video_devdata);
109 * Open a video device - FIXME: Obsoleted
111 static int video_open(struct inode *inode, struct file *file)
113 unsigned int minor = iminor(inode);
115 struct video_device *vfl;
116 const struct file_operations *old_fops;
118 if (minor >= VIDEO_NUM_DEVICES)
120 mutex_lock(&videodev_lock);
121 vfl = video_device[minor];
123 mutex_unlock(&videodev_lock);
124 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
125 mutex_lock(&videodev_lock);
126 vfl = video_device[minor];
128 mutex_unlock(&videodev_lock);
132 old_fops = file->f_op;
133 file->f_op = fops_get(vfl->fops);
134 if (file->f_op->open)
135 err = file->f_op->open(inode, file);
137 fops_put(file->f_op);
138 file->f_op = fops_get(old_fops);
141 mutex_unlock(&videodev_lock);
146 * get_index - assign stream number based on parent device
147 * @vdev: video_device to assign index number to, vdev->dev should be assigned
148 * @num: -1 if auto assign, requested number otherwise
151 * returns -ENFILE if num is already in use, a free index number if
154 static int get_index(struct video_device *vdev, int num)
157 const int max_index = sizeof(used) * 8 - 1;
160 /* Currently a single v4l driver instance cannot create more than
162 Increase to u64 or an array of u32 if more are needed. */
163 if (num > max_index) {
164 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
168 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
169 if (video_device[i] != NULL &&
170 video_device[i] != vdev &&
171 video_device[i]->parent == vdev->parent) {
172 used |= 1 << video_device[i]->index;
177 if (used & (1 << num))
183 return i > max_index ? -ENFILE : i;
186 static const struct file_operations video_fops;
188 int video_register_device(struct video_device *vfd, int type, int nr)
190 return video_register_device_index(vfd, type, nr, -1);
192 EXPORT_SYMBOL(video_register_device);
195 * video_register_device_index - register video4linux devices
196 * @vfd: video device structure we want to register
197 * @type: type of device to register
198 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
200 * @index: stream number based on parent device;
201 * -1 if auto assign, requested number otherwise
203 * The registration code assigns minor numbers based on the type
204 * requested. -ENFILE is returned in all the device slots for this
205 * category are full. If not then the minor field is set and the
206 * driver initialize function is called (if non %NULL).
208 * Zero is returned on success.
212 * %VFL_TYPE_GRABBER - A frame grabber
214 * %VFL_TYPE_VTX - A teletext device
216 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
218 * %VFL_TYPE_RADIO - A radio card
221 int video_register_device_index(struct video_device *vfd, int type, int nr,
229 void *priv = video_get_drvdata(vfd);
231 /* the release callback MUST be present */
232 BUG_ON(!vfd->release);
238 case VFL_TYPE_GRABBER:
239 base = MINOR_VFL_TYPE_GRABBER_MIN;
240 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
244 base = MINOR_VFL_TYPE_VTX_MIN;
245 end = MINOR_VFL_TYPE_VTX_MAX+1;
249 base = MINOR_VFL_TYPE_VBI_MIN;
250 end = MINOR_VFL_TYPE_VBI_MAX+1;
254 base = MINOR_VFL_TYPE_RADIO_MIN;
255 end = MINOR_VFL_TYPE_RADIO_MAX+1;
259 printk(KERN_ERR "%s called with unknown type: %d\n",
264 /* pick a minor number */
265 mutex_lock(&videodev_lock);
266 if (nr >= 0 && nr < end-base) {
267 /* use the one the driver asked for */
269 if (NULL != video_device[i]) {
270 mutex_unlock(&videodev_lock);
275 for (i = base; i < end; i++)
276 if (NULL == video_device[i])
279 mutex_unlock(&videodev_lock);
283 video_device[i] = vfd;
284 vfd->vfl_type = type;
287 ret = get_index(vfd, index);
290 mutex_unlock(&videodev_lock);
293 printk(KERN_ERR "%s: get_index failed\n", __func__);
298 memset(&vfd->dev, 0, sizeof(vfd->dev));
299 /* The memset above cleared the device's drvdata, so
300 put back the copy we made earlier. */
301 video_set_drvdata(vfd, priv);
302 vfd->dev.class = &video_class;
303 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
305 vfd->dev.parent = vfd->parent;
306 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
307 ret = device_register(&vfd->dev);
309 printk(KERN_ERR "%s: device_register failed\n", __func__);
316 mutex_lock(&videodev_lock);
317 video_device[vfd->minor] = NULL;
318 mutex_unlock(&videodev_lock);
322 EXPORT_SYMBOL(video_register_device_index);
325 * video_unregister_device - unregister a video4linux device
326 * @vfd: the device to unregister
328 * This unregisters the passed device and deassigns the minor
329 * number. Future open calls will be met with errors.
332 void video_unregister_device(struct video_device *vfd)
334 mutex_lock(&videodev_lock);
335 if (video_device[vfd->minor] != vfd)
336 panic("videodev: bad unregister");
338 video_device[vfd->minor] = NULL;
339 device_unregister(&vfd->dev);
340 mutex_unlock(&videodev_lock);
342 EXPORT_SYMBOL(video_unregister_device);
345 * Video fs operations
347 static const struct file_operations video_fops = {
348 .owner = THIS_MODULE,
354 * Initialise video for linux
357 static int __init videodev_init(void)
361 printk(KERN_INFO "Linux video capture interface: v2.00\n");
362 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
363 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
367 ret = class_register(&video_class);
369 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
370 printk(KERN_WARNING "video_dev: class_register failed\n");
377 static void __exit videodev_exit(void)
379 class_unregister(&video_class);
380 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
383 module_init(videodev_init)
384 module_exit(videodev_exit)
386 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
387 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
388 MODULE_LICENSE("GPL");