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>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
36 #define VIDEO_NUM_DEVICES 256
37 #define VIDEO_NAME "video4linux"
43 static ssize_t
show_index(struct device
*cd
,
44 struct device_attribute
*attr
, char *buf
)
46 struct video_device
*vdev
= to_video_device(cd
);
48 return sprintf(buf
, "%i\n", vdev
->index
);
51 static ssize_t
show_name(struct device
*cd
,
52 struct device_attribute
*attr
, char *buf
)
54 struct video_device
*vdev
= to_video_device(cd
);
56 return sprintf(buf
, "%.*s\n", (int)sizeof(vdev
->name
), vdev
->name
);
59 static struct device_attribute video_device_attrs
[] = {
60 __ATTR(name
, S_IRUGO
, show_name
, NULL
),
61 __ATTR(index
, S_IRUGO
, show_index
, NULL
),
68 static struct video_device
*video_device
[VIDEO_NUM_DEVICES
];
69 static DEFINE_MUTEX(videodev_lock
);
70 static DECLARE_BITMAP(video_nums
[VFL_TYPE_MAX
], VIDEO_NUM_DEVICES
);
72 struct video_device
*video_device_alloc(void)
74 return kzalloc(sizeof(struct video_device
), GFP_KERNEL
);
76 EXPORT_SYMBOL(video_device_alloc
);
78 void video_device_release(struct video_device
*vdev
)
82 EXPORT_SYMBOL(video_device_release
);
84 void video_device_release_empty(struct video_device
*vdev
)
87 /* Only valid when the video_device struct is a static. */
89 EXPORT_SYMBOL(video_device_release_empty
);
91 static inline void video_get(struct video_device
*vdev
)
93 get_device(&vdev
->dev
);
96 static inline void video_put(struct video_device
*vdev
)
98 put_device(&vdev
->dev
);
101 /* Called when the last user of the video device exits. */
102 static void v4l2_device_release(struct device
*cd
)
104 struct video_device
*vdev
= to_video_device(cd
);
106 mutex_lock(&videodev_lock
);
107 if (video_device
[vdev
->minor
] != vdev
) {
108 mutex_unlock(&videodev_lock
);
109 /* should not happen */
114 /* Free up this device for reuse */
115 video_device
[vdev
->minor
] = NULL
;
117 /* Delete the cdev on this minor as well */
118 cdev_del(vdev
->cdev
);
119 /* Just in case some driver tries to access this from
120 the release() callback. */
123 /* Mark minor as free */
124 clear_bit(vdev
->num
, video_nums
[vdev
->vfl_type
]);
126 mutex_unlock(&videodev_lock
);
128 /* Release video_device and perform other
129 cleanups as needed. */
133 static struct class video_class
= {
135 .dev_attrs
= video_device_attrs
,
138 struct video_device
*video_devdata(struct file
*file
)
140 return video_device
[iminor(file
->f_path
.dentry
->d_inode
)];
142 EXPORT_SYMBOL(video_devdata
);
144 static ssize_t
v4l2_read(struct file
*filp
, char __user
*buf
,
145 size_t sz
, loff_t
*off
)
147 struct video_device
*vdev
= video_devdata(filp
);
149 if (!vdev
->fops
->read
)
151 if (video_is_unregistered(vdev
))
153 return vdev
->fops
->read(filp
, buf
, sz
, off
);
156 static ssize_t
v4l2_write(struct file
*filp
, const char __user
*buf
,
157 size_t sz
, loff_t
*off
)
159 struct video_device
*vdev
= video_devdata(filp
);
161 if (!vdev
->fops
->write
)
163 if (video_is_unregistered(vdev
))
165 return vdev
->fops
->write(filp
, buf
, sz
, off
);
168 static unsigned int v4l2_poll(struct file
*filp
, struct poll_table_struct
*poll
)
170 struct video_device
*vdev
= video_devdata(filp
);
172 if (!vdev
->fops
->poll
|| video_is_unregistered(vdev
))
173 return DEFAULT_POLLMASK
;
174 return vdev
->fops
->poll(filp
, poll
);
177 static int v4l2_ioctl(struct inode
*inode
, struct file
*filp
,
178 unsigned int cmd
, unsigned long arg
)
180 struct video_device
*vdev
= video_devdata(filp
);
182 if (!vdev
->fops
->ioctl
)
184 /* Allow ioctl to continue even if the device was unregistered.
185 Things like dequeueing buffers might still be useful. */
186 return vdev
->fops
->ioctl(filp
, cmd
, arg
);
189 static long v4l2_unlocked_ioctl(struct file
*filp
,
190 unsigned int cmd
, unsigned long arg
)
192 struct video_device
*vdev
= video_devdata(filp
);
194 if (!vdev
->fops
->unlocked_ioctl
)
196 /* Allow ioctl to continue even if the device was unregistered.
197 Things like dequeueing buffers might still be useful. */
198 return vdev
->fops
->unlocked_ioctl(filp
, cmd
, arg
);
202 #define v4l2_get_unmapped_area NULL
204 static unsigned long v4l2_get_unmapped_area(struct file
*filp
,
205 unsigned long addr
, unsigned long len
, unsigned long pgoff
,
208 struct video_device
*vdev
= video_devdata(filp
);
210 if (!vdev
->fops
->get_unmapped_area
)
212 if (video_is_unregistered(vdev
))
214 return vdev
->fops
->get_unmapped_area(filp
, addr
, len
, pgoff
, flags
);
218 static int v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vm
)
220 struct video_device
*vdev
= video_devdata(filp
);
222 if (!vdev
->fops
->mmap
||
223 video_is_unregistered(vdev
))
225 return vdev
->fops
->mmap(filp
, vm
);
228 /* Override for the open function */
229 static int v4l2_open(struct inode
*inode
, struct file
*filp
)
231 struct video_device
*vdev
;
234 /* Check if the video device is available */
235 mutex_lock(&videodev_lock
);
236 vdev
= video_devdata(filp
);
237 /* return ENODEV if the video device has been removed
238 already or if it is not registered anymore. */
239 if (vdev
== NULL
|| video_is_unregistered(vdev
)) {
240 mutex_unlock(&videodev_lock
);
243 /* and increase the device refcount */
245 mutex_unlock(&videodev_lock
);
246 if (vdev
->fops
->open
)
247 ret
= vdev
->fops
->open(filp
);
249 /* decrease the refcount in case of an error */
255 /* Override for the release function */
256 static int v4l2_release(struct inode
*inode
, struct file
*filp
)
258 struct video_device
*vdev
= video_devdata(filp
);
261 if (vdev
->fops
->release
)
262 vdev
->fops
->release(filp
);
264 /* decrease the refcount unconditionally since the release()
265 return value is ignored. */
270 static const struct file_operations v4l2_unlocked_fops
= {
271 .owner
= THIS_MODULE
,
275 .get_unmapped_area
= v4l2_get_unmapped_area
,
277 .unlocked_ioctl
= v4l2_unlocked_ioctl
,
279 .compat_ioctl
= v4l2_compat_ioctl32
,
281 .release
= v4l2_release
,
286 static const struct file_operations v4l2_fops
= {
287 .owner
= THIS_MODULE
,
291 .get_unmapped_area
= v4l2_get_unmapped_area
,
295 .compat_ioctl
= v4l2_compat_ioctl32
,
297 .release
= v4l2_release
,
303 * get_index - assign stream number based on parent device
304 * @vdev: video_device to assign index number to, vdev->parent should be assigned
305 * @num: -1 if auto assign, requested number otherwise
307 * Note that when this is called the new device has not yet been registered
308 * in the video_device array.
310 * Returns -ENFILE if num is already in use, a free index number if
313 static int get_index(struct video_device
*vdev
, int num
)
315 /* This can be static since this function is called with the global
316 videodev_lock held. */
317 static DECLARE_BITMAP(used
, VIDEO_NUM_DEVICES
);
320 if (num
>= VIDEO_NUM_DEVICES
) {
321 printk(KERN_ERR
"videodev: %s num is too large\n", __func__
);
325 /* Some drivers do not set the parent. In that case always return
327 if (vdev
->parent
== NULL
)
328 return num
>= 0 ? num
: 0;
330 bitmap_zero(used
, VIDEO_NUM_DEVICES
);
332 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++) {
333 if (video_device
[i
] != NULL
&&
334 video_device
[i
]->parent
== vdev
->parent
) {
335 set_bit(video_device
[i
]->index
, used
);
340 if (test_bit(num
, used
))
345 i
= find_first_zero_bit(used
, VIDEO_NUM_DEVICES
);
346 return i
== VIDEO_NUM_DEVICES
? -ENFILE
: i
;
349 int video_register_device(struct video_device
*vdev
, int type
, int nr
)
351 return video_register_device_index(vdev
, type
, nr
, -1);
353 EXPORT_SYMBOL(video_register_device
);
356 * video_register_device_index - register video4linux devices
357 * @vdev: video device structure we want to register
358 * @type: type of device to register
359 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
361 * @index: stream number based on parent device;
362 * -1 if auto assign, requested number otherwise
364 * The registration code assigns minor numbers based on the type
365 * requested. -ENFILE is returned in all the device slots for this
366 * category are full. If not then the minor field is set and the
367 * driver initialize function is called (if non %NULL).
369 * Zero is returned on success.
373 * %VFL_TYPE_GRABBER - A frame grabber
375 * %VFL_TYPE_VTX - A teletext device
377 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
379 * %VFL_TYPE_RADIO - A radio card
381 int video_register_device_index(struct video_device
*vdev
, int type
, int nr
,
386 int minor_offset
= 0;
387 int minor_cnt
= VIDEO_NUM_DEVICES
;
388 const char *name_base
;
389 void *priv
= video_get_drvdata(vdev
);
391 /* A minor value of -1 marks this video device as never
392 having been registered */
395 /* the release callback MUST be present */
396 WARN_ON(!vdev
->release
);
400 /* Part 1: check device type */
402 case VFL_TYPE_GRABBER
:
415 printk(KERN_ERR
"%s called with unknown type: %d\n",
420 vdev
->vfl_type
= type
;
422 if (vdev
->v4l2_dev
&& vdev
->v4l2_dev
->dev
)
423 vdev
->parent
= vdev
->v4l2_dev
->dev
;
425 /* Part 2: find a free minor, kernel number and device index. */
426 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
427 /* Keep the ranges for the first four types for historical
429 * Newer devices (not yet in place) should use the range
430 * of 128-191 and just pick the first free minor there
433 case VFL_TYPE_GRABBER
:
456 /* Pick a minor number */
457 mutex_lock(&videodev_lock
);
458 nr
= find_next_zero_bit(video_nums
[type
], minor_cnt
, nr
== -1 ? 0 : nr
);
460 nr
= find_first_zero_bit(video_nums
[type
], minor_cnt
);
461 if (nr
== minor_cnt
) {
462 printk(KERN_ERR
"could not get a free kernel number\n");
463 mutex_unlock(&videodev_lock
);
466 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
467 /* 1-on-1 mapping of kernel number to minor number */
470 /* The kernel number and minor numbers are independent */
471 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++)
472 if (video_device
[i
] == NULL
)
474 if (i
== VIDEO_NUM_DEVICES
) {
475 mutex_unlock(&videodev_lock
);
476 printk(KERN_ERR
"could not get a free minor\n");
480 vdev
->minor
= i
+ minor_offset
;
482 set_bit(nr
, video_nums
[type
]);
483 /* Should not happen since we thought this minor was free */
484 WARN_ON(video_device
[vdev
->minor
] != NULL
);
485 ret
= vdev
->index
= get_index(vdev
, index
);
486 mutex_unlock(&videodev_lock
);
489 printk(KERN_ERR
"%s: get_index failed\n", __func__
);
493 /* Part 3: Initialize the character device */
494 vdev
->cdev
= cdev_alloc();
495 if (vdev
->cdev
== NULL
) {
499 if (vdev
->fops
->unlocked_ioctl
)
500 vdev
->cdev
->ops
= &v4l2_unlocked_fops
;
502 vdev
->cdev
->ops
= &v4l2_fops
;
503 vdev
->cdev
->owner
= vdev
->fops
->owner
;
504 ret
= cdev_add(vdev
->cdev
, MKDEV(VIDEO_MAJOR
, vdev
->minor
), 1);
506 printk(KERN_ERR
"%s: cdev_add failed\n", __func__
);
512 /* Part 4: register the device with sysfs */
513 memset(&vdev
->dev
, 0, sizeof(vdev
->dev
));
514 /* The memset above cleared the device's drvdata, so
515 put back the copy we made earlier. */
516 video_set_drvdata(vdev
, priv
);
517 vdev
->dev
.class = &video_class
;
518 vdev
->dev
.devt
= MKDEV(VIDEO_MAJOR
, vdev
->minor
);
520 vdev
->dev
.parent
= vdev
->parent
;
521 dev_set_name(&vdev
->dev
, "%s%d", name_base
, nr
);
522 ret
= device_register(&vdev
->dev
);
524 printk(KERN_ERR
"%s: device_register failed\n", __func__
);
527 /* Register the release callback that will be called when the last
528 reference to the device goes away. */
529 vdev
->dev
.release
= v4l2_device_release
;
531 /* Part 5: Activate this minor. The char device can now be used. */
532 mutex_lock(&videodev_lock
);
533 video_device
[vdev
->minor
] = vdev
;
534 mutex_unlock(&videodev_lock
);
538 mutex_lock(&videodev_lock
);
540 cdev_del(vdev
->cdev
);
541 clear_bit(vdev
->num
, video_nums
[type
]);
542 mutex_unlock(&videodev_lock
);
543 /* Mark this video device as never having been registered. */
547 EXPORT_SYMBOL(video_register_device_index
);
550 * video_unregister_device - unregister a video4linux device
551 * @vdev: the device to unregister
553 * This unregisters the passed device. Future open calls will
554 * be met with errors.
556 void video_unregister_device(struct video_device
*vdev
)
558 /* Check if vdev was ever registered at all */
559 if (!vdev
|| vdev
->minor
< 0)
562 mutex_lock(&videodev_lock
);
563 set_bit(V4L2_FL_UNREGISTERED
, &vdev
->flags
);
564 mutex_unlock(&videodev_lock
);
565 device_unregister(&vdev
->dev
);
567 EXPORT_SYMBOL(video_unregister_device
);
570 * Initialise video for linux
572 static int __init
videodev_init(void)
574 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
577 printk(KERN_INFO
"Linux video capture interface: v2.00\n");
578 ret
= register_chrdev_region(dev
, VIDEO_NUM_DEVICES
, VIDEO_NAME
);
580 printk(KERN_WARNING
"videodev: unable to get major %d\n",
585 ret
= class_register(&video_class
);
587 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
588 printk(KERN_WARNING
"video_dev: class_register failed\n");
595 static void __exit
videodev_exit(void)
597 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
599 class_unregister(&video_class
);
600 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
603 module_init(videodev_init
)
604 module_exit(videodev_exit
)
606 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
607 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
608 MODULE_LICENSE("GPL");
609 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR
);