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@lxorguk.ukuu.org.uk> (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
*vdev
= to_video_device(cd
);
46 return sprintf(buf
, "%i\n", vdev
->index
);
49 static ssize_t
show_name(struct device
*cd
,
50 struct device_attribute
*attr
, char *buf
)
52 struct video_device
*vdev
= to_video_device(cd
);
54 return sprintf(buf
, "%.*s\n", (int)sizeof(vdev
->name
), vdev
->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
),
66 static struct video_device
*video_device
[VIDEO_NUM_DEVICES
];
67 static DEFINE_MUTEX(videodev_lock
);
68 static DECLARE_BITMAP(video_nums
[VFL_TYPE_MAX
], VIDEO_NUM_DEVICES
);
70 struct video_device
*video_device_alloc(void)
72 return kzalloc(sizeof(struct video_device
), GFP_KERNEL
);
74 EXPORT_SYMBOL(video_device_alloc
);
76 void video_device_release(struct video_device
*vdev
)
80 EXPORT_SYMBOL(video_device_release
);
82 void video_device_release_empty(struct video_device
*vdev
)
85 /* Only valid when the video_device struct is a static. */
87 EXPORT_SYMBOL(video_device_release_empty
);
89 static inline void video_get(struct video_device
*vdev
)
91 get_device(&vdev
->dev
);
94 static inline void video_put(struct video_device
*vdev
)
96 put_device(&vdev
->dev
);
99 /* Called when the last user of the video device exits. */
100 static void v4l2_device_release(struct device
*cd
)
102 struct video_device
*vdev
= to_video_device(cd
);
104 mutex_lock(&videodev_lock
);
105 if (video_device
[vdev
->minor
] != vdev
) {
106 mutex_unlock(&videodev_lock
);
107 /* should not happen */
112 /* Free up this device for reuse */
113 video_device
[vdev
->minor
] = NULL
;
115 /* Delete the cdev on this minor as well */
116 cdev_del(vdev
->cdev
);
117 /* Just in case some driver tries to access this from
118 the release() callback. */
121 /* Mark minor as free */
122 clear_bit(vdev
->num
, video_nums
[vdev
->vfl_type
]);
124 mutex_unlock(&videodev_lock
);
126 /* Release video_device and perform other
127 cleanups as needed. */
131 static struct class video_class
= {
133 .dev_attrs
= video_device_attrs
,
136 struct video_device
*video_devdata(struct file
*file
)
138 return video_device
[iminor(file
->f_path
.dentry
->d_inode
)];
140 EXPORT_SYMBOL(video_devdata
);
142 static ssize_t
v4l2_read(struct file
*filp
, char __user
*buf
,
143 size_t sz
, loff_t
*off
)
145 struct video_device
*vdev
= video_devdata(filp
);
147 if (!vdev
->fops
->read
)
149 if (video_is_unregistered(vdev
))
151 return vdev
->fops
->read(filp
, buf
, sz
, off
);
154 static ssize_t
v4l2_write(struct file
*filp
, const char __user
*buf
,
155 size_t sz
, loff_t
*off
)
157 struct video_device
*vdev
= video_devdata(filp
);
159 if (!vdev
->fops
->write
)
161 if (video_is_unregistered(vdev
))
163 return vdev
->fops
->write(filp
, buf
, sz
, off
);
166 static unsigned int v4l2_poll(struct file
*filp
, struct poll_table_struct
*poll
)
168 struct video_device
*vdev
= video_devdata(filp
);
170 if (!vdev
->fops
->poll
|| video_is_unregistered(vdev
))
171 return DEFAULT_POLLMASK
;
172 return vdev
->fops
->poll(filp
, poll
);
175 static int v4l2_ioctl(struct inode
*inode
, struct file
*filp
,
176 unsigned int cmd
, unsigned long arg
)
178 struct video_device
*vdev
= video_devdata(filp
);
180 if (!vdev
->fops
->ioctl
)
182 /* Allow ioctl to continue even if the device was unregistered.
183 Things like dequeueing buffers might still be useful. */
184 return vdev
->fops
->ioctl(inode
, filp
, cmd
, arg
);
187 static long v4l2_unlocked_ioctl(struct file
*filp
,
188 unsigned int cmd
, unsigned long arg
)
190 struct video_device
*vdev
= video_devdata(filp
);
192 if (!vdev
->fops
->unlocked_ioctl
)
194 /* Allow ioctl to continue even if the device was unregistered.
195 Things like dequeueing buffers might still be useful. */
196 return vdev
->fops
->unlocked_ioctl(filp
, cmd
, arg
);
200 static long v4l2_compat_ioctl(struct file
*filp
,
201 unsigned int cmd
, unsigned long arg
)
203 struct video_device
*vdev
= video_devdata(filp
);
205 if (!vdev
->fops
->compat_ioctl
)
207 /* Allow ioctl to continue even if the device was unregistered.
208 Things like dequeueing buffers might still be useful. */
209 return vdev
->fops
->compat_ioctl(filp
, cmd
, arg
);
213 static int v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vm
)
215 struct video_device
*vdev
= video_devdata(filp
);
217 if (!vdev
->fops
->mmap
||
218 video_is_unregistered(vdev
))
220 return vdev
->fops
->mmap(filp
, vm
);
223 /* Override for the open function */
224 static int v4l2_open(struct inode
*inode
, struct file
*filp
)
226 struct video_device
*vdev
;
229 /* Check if the video device is available */
230 mutex_lock(&videodev_lock
);
231 vdev
= video_devdata(filp
);
232 /* return ENODEV if the video device has been removed
233 already or if it is not registered anymore. */
234 if (vdev
== NULL
|| video_is_unregistered(vdev
)) {
235 mutex_unlock(&videodev_lock
);
238 /* and increase the device refcount */
240 mutex_unlock(&videodev_lock
);
241 ret
= vdev
->fops
->open(inode
, filp
);
242 /* decrease the refcount in case of an error */
248 /* Override for the release function */
249 static int v4l2_release(struct inode
*inode
, struct file
*filp
)
251 struct video_device
*vdev
= video_devdata(filp
);
252 int ret
= vdev
->fops
->release(inode
, filp
);
254 /* decrease the refcount unconditionally since the release()
255 return value is ignored. */
260 static const struct file_operations v4l2_unlocked_fops
= {
261 .owner
= THIS_MODULE
,
266 .unlocked_ioctl
= v4l2_unlocked_ioctl
,
268 .compat_ioctl
= v4l2_compat_ioctl
,
270 .release
= v4l2_release
,
275 static const struct file_operations v4l2_fops
= {
276 .owner
= THIS_MODULE
,
283 .compat_ioctl
= v4l2_compat_ioctl
,
285 .release
= v4l2_release
,
291 * get_index - assign stream number based on parent device
292 * @vdev: video_device to assign index number to, vdev->parent should be assigned
293 * @num: -1 if auto assign, requested number otherwise
295 * Note that when this is called the new device has not yet been registered
296 * in the video_device array.
298 * Returns -ENFILE if num is already in use, a free index number if
301 static int get_index(struct video_device
*vdev
, int num
)
304 const int max_index
= sizeof(used
) * 8 - 1;
307 /* Currently a single v4l driver instance cannot create more than
309 Increase to u64 or an array of u32 if more are needed. */
310 if (num
> max_index
) {
311 printk(KERN_ERR
"videodev: %s num is too large\n", __func__
);
315 /* Some drivers do not set the parent. In that case always return 0. */
316 if (vdev
->parent
== NULL
)
319 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++) {
320 if (video_device
[i
] != NULL
&&
321 video_device
[i
]->parent
== vdev
->parent
) {
322 used
|= 1 << video_device
[i
]->index
;
327 if (used
& (1 << num
))
333 return i
> max_index
? -ENFILE
: i
;
336 int video_register_device(struct video_device
*vdev
, int type
, int nr
)
338 return video_register_device_index(vdev
, type
, nr
, -1);
340 EXPORT_SYMBOL(video_register_device
);
343 * video_register_device_index - register video4linux devices
344 * @vdev: video device structure we want to register
345 * @type: type of device to register
346 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
348 * @index: stream number based on parent device;
349 * -1 if auto assign, requested number otherwise
351 * The registration code assigns minor numbers based on the type
352 * requested. -ENFILE is returned in all the device slots for this
353 * category are full. If not then the minor field is set and the
354 * driver initialize function is called (if non %NULL).
356 * Zero is returned on success.
360 * %VFL_TYPE_GRABBER - A frame grabber
362 * %VFL_TYPE_VTX - A teletext device
364 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
366 * %VFL_TYPE_RADIO - A radio card
368 int video_register_device_index(struct video_device
*vdev
, int type
, int nr
,
373 int minor_offset
= 0;
374 int minor_cnt
= VIDEO_NUM_DEVICES
;
375 const char *name_base
;
376 void *priv
= video_get_drvdata(vdev
);
378 /* A minor value of -1 marks this video device as never
379 having been registered */
383 /* the release callback MUST be present */
384 WARN_ON(!vdev
|| !vdev
->release
);
385 if (!vdev
|| !vdev
->release
)
388 /* Part 1: check device type */
390 case VFL_TYPE_GRABBER
:
403 printk(KERN_ERR
"%s called with unknown type: %d\n",
408 vdev
->vfl_type
= type
;
411 /* Part 2: find a free minor, kernel number and device index. */
412 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
413 /* Keep the ranges for the first four types for historical
415 * Newer devices (not yet in place) should use the range
416 * of 128-191 and just pick the first free minor there
419 case VFL_TYPE_GRABBER
:
442 /* Pick a minor number */
443 mutex_lock(&videodev_lock
);
444 nr
= find_next_zero_bit(video_nums
[type
], minor_cnt
, nr
== -1 ? 0 : nr
);
446 nr
= find_first_zero_bit(video_nums
[type
], minor_cnt
);
447 if (nr
== minor_cnt
) {
448 printk(KERN_ERR
"could not get a free kernel number\n");
449 mutex_unlock(&videodev_lock
);
452 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
453 /* 1-on-1 mapping of kernel number to minor number */
456 /* The kernel number and minor numbers are independent */
457 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++)
458 if (video_device
[i
] == NULL
)
460 if (i
== VIDEO_NUM_DEVICES
) {
461 mutex_unlock(&videodev_lock
);
462 printk(KERN_ERR
"could not get a free minor\n");
466 vdev
->minor
= i
+ minor_offset
;
468 set_bit(nr
, video_nums
[type
]);
469 /* Should not happen since we thought this minor was free */
470 WARN_ON(video_device
[vdev
->minor
] != NULL
);
471 ret
= vdev
->index
= get_index(vdev
, index
);
472 mutex_unlock(&videodev_lock
);
475 printk(KERN_ERR
"%s: get_index failed\n", __func__
);
479 /* Part 3: Initialize the character device */
480 vdev
->cdev
= cdev_alloc();
481 if (vdev
->cdev
== NULL
) {
485 if (vdev
->fops
->unlocked_ioctl
)
486 vdev
->cdev
->ops
= &v4l2_unlocked_fops
;
488 vdev
->cdev
->ops
= &v4l2_fops
;
489 vdev
->cdev
->owner
= vdev
->fops
->owner
;
490 ret
= cdev_add(vdev
->cdev
, MKDEV(VIDEO_MAJOR
, vdev
->minor
), 1);
492 printk(KERN_ERR
"%s: cdev_add failed\n", __func__
);
498 /* Part 4: register the device with sysfs */
499 memset(&vdev
->dev
, 0, sizeof(vdev
->dev
));
500 /* The memset above cleared the device's drvdata, so
501 put back the copy we made earlier. */
502 video_set_drvdata(vdev
, priv
);
503 vdev
->dev
.class = &video_class
;
504 vdev
->dev
.devt
= MKDEV(VIDEO_MAJOR
, vdev
->minor
);
506 vdev
->dev
.parent
= vdev
->parent
;
507 dev_set_name(&vdev
->dev
, "%s%d", name_base
, nr
);
508 ret
= device_register(&vdev
->dev
);
510 printk(KERN_ERR
"%s: device_register failed\n", __func__
);
513 /* Register the release callback that will be called when the last
514 reference to the device goes away. */
515 vdev
->dev
.release
= v4l2_device_release
;
517 /* Part 5: Activate this minor. The char device can now be used. */
518 mutex_lock(&videodev_lock
);
519 video_device
[vdev
->minor
] = vdev
;
520 mutex_unlock(&videodev_lock
);
524 mutex_lock(&videodev_lock
);
526 cdev_del(vdev
->cdev
);
527 clear_bit(vdev
->num
, video_nums
[type
]);
528 mutex_unlock(&videodev_lock
);
529 /* Mark this video device as never having been registered. */
533 EXPORT_SYMBOL(video_register_device_index
);
536 * video_unregister_device - unregister a video4linux device
537 * @vdev: the device to unregister
539 * This unregisters the passed device. Future open calls will
540 * be met with errors.
542 void video_unregister_device(struct video_device
*vdev
)
544 /* Check if vdev was ever registered at all */
545 if (!vdev
|| vdev
->minor
< 0)
548 mutex_lock(&videodev_lock
);
549 set_bit(V4L2_FL_UNREGISTERED
, &vdev
->flags
);
550 mutex_unlock(&videodev_lock
);
551 device_unregister(&vdev
->dev
);
553 EXPORT_SYMBOL(video_unregister_device
);
556 * Initialise video for linux
558 static int __init
videodev_init(void)
560 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
563 printk(KERN_INFO
"Linux video capture interface: v2.00\n");
564 ret
= register_chrdev_region(dev
, VIDEO_NUM_DEVICES
, VIDEO_NAME
);
566 printk(KERN_WARNING
"videodev: unable to get major %d\n",
571 ret
= class_register(&video_class
);
573 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
574 printk(KERN_WARNING
"video_dev: class_register failed\n");
581 static void __exit
videodev_exit(void)
583 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
585 class_unregister(&video_class
);
586 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
589 module_init(videodev_init
)
590 module_exit(videodev_exit
)
592 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
593 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
594 MODULE_LICENSE("GPL");