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
),
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
*vfd
)
80 EXPORT_SYMBOL(video_device_release
);
82 void video_device_release_empty(struct video_device
*vfd
)
85 /* Only valid when the video_device struct is a static. */
87 EXPORT_SYMBOL(video_device_release_empty
);
89 /* Called when the last user of the character device is gone. */
90 static void v4l2_chardev_release(struct kobject
*kobj
)
92 struct video_device
*vfd
= container_of(kobj
, struct video_device
, cdev
.kobj
);
94 mutex_lock(&videodev_lock
);
95 if (video_device
[vfd
->minor
] != vfd
) {
96 mutex_unlock(&videodev_lock
);
101 /* Free up this device for reuse */
102 video_device
[vfd
->minor
] = NULL
;
103 clear_bit(vfd
->num
, video_nums
[vfd
->vfl_type
]);
104 mutex_unlock(&videodev_lock
);
106 /* Release the character device */
107 vfd
->cdev_release(kobj
);
108 /* Release video_device and perform other
109 cleanups as needed. */
114 /* The new kobj_type for the character device */
115 static struct kobj_type v4l2_ktype_cdev_default
= {
116 .release
= v4l2_chardev_release
,
119 static void video_release(struct device
*cd
)
121 struct video_device
*vfd
= container_of(cd
, struct video_device
, dev
);
123 /* It's now safe to delete the char device.
124 This will either trigger the v4l2_chardev_release immediately (if
125 the refcount goes to 0) or later when the last user of the
126 character device closes it. */
127 cdev_del(&vfd
->cdev
);
130 static struct class video_class
= {
132 .dev_attrs
= video_device_attrs
,
133 .dev_release
= video_release
,
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
);
143 * get_index - assign stream number based on parent device
144 * @vdev: video_device to assign index number to, vdev->dev should be assigned
145 * @num: -1 if auto assign, requested number otherwise
148 * returns -ENFILE if num is already in use, a free index number if
151 static int get_index(struct video_device
*vdev
, int num
)
154 const int max_index
= sizeof(used
) * 8 - 1;
157 /* Currently a single v4l driver instance cannot create more than
159 Increase to u64 or an array of u32 if more are needed. */
160 if (num
> max_index
) {
161 printk(KERN_ERR
"videodev: %s num is too large\n", __func__
);
165 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++) {
166 if (video_device
[i
] != NULL
&&
167 video_device
[i
] != vdev
&&
168 video_device
[i
]->parent
== vdev
->parent
) {
169 used
|= 1 << video_device
[i
]->index
;
174 if (used
& (1 << num
))
180 return i
> max_index
? -ENFILE
: i
;
183 static const struct file_operations video_fops
;
185 int video_register_device(struct video_device
*vfd
, int type
, int nr
)
187 return video_register_device_index(vfd
, type
, nr
, -1);
189 EXPORT_SYMBOL(video_register_device
);
192 * video_register_device_index - register video4linux devices
193 * @vfd: video device structure we want to register
194 * @type: type of device to register
195 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
197 * @index: stream number based on parent device;
198 * -1 if auto assign, requested number otherwise
200 * The registration code assigns minor numbers based on the type
201 * requested. -ENFILE is returned in all the device slots for this
202 * category are full. If not then the minor field is set and the
203 * driver initialize function is called (if non %NULL).
205 * Zero is returned on success.
209 * %VFL_TYPE_GRABBER - A frame grabber
211 * %VFL_TYPE_VTX - A teletext device
213 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
215 * %VFL_TYPE_RADIO - A radio card
218 int video_register_device_index(struct video_device
*vfd
, int type
, int nr
,
223 int minor_offset
= 0;
224 int minor_cnt
= VIDEO_NUM_DEVICES
;
225 const char *name_base
;
226 void *priv
= video_get_drvdata(vfd
);
228 /* the release callback MUST be present */
229 BUG_ON(!vfd
->release
);
235 case VFL_TYPE_GRABBER
:
248 printk(KERN_ERR
"%s called with unknown type: %d\n",
253 vfd
->vfl_type
= type
;
255 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
256 /* Keep the ranges for the first four types for historical
258 * Newer devices (not yet in place) should use the range
259 * of 128-191 and just pick the first free minor there
262 case VFL_TYPE_GRABBER
:
285 /* Initialize the character device */
286 cdev_init(&vfd
->cdev
, vfd
->fops
);
287 vfd
->cdev
.owner
= vfd
->fops
->owner
;
288 /* pick a minor number */
289 mutex_lock(&videodev_lock
);
290 nr
= find_next_zero_bit(video_nums
[type
], minor_cnt
, nr
== -1 ? 0 : nr
);
292 nr
= find_first_zero_bit(video_nums
[type
], minor_cnt
);
293 if (nr
== minor_cnt
) {
294 printk(KERN_ERR
"could not get a free kernel number\n");
295 mutex_unlock(&videodev_lock
);
298 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
299 /* 1-on-1 mapping of kernel number to minor number */
302 /* The kernel number and minor numbers are independent */
303 for (i
= 0; i
< VIDEO_NUM_DEVICES
; i
++)
304 if (video_device
[i
] == NULL
)
306 if (i
== VIDEO_NUM_DEVICES
) {
307 mutex_unlock(&videodev_lock
);
308 printk(KERN_ERR
"could not get a free minor\n");
312 vfd
->minor
= i
+ minor_offset
;
314 set_bit(nr
, video_nums
[type
]);
315 BUG_ON(video_device
[vfd
->minor
]);
316 video_device
[vfd
->minor
] = vfd
;
318 ret
= get_index(vfd
, index
);
321 mutex_unlock(&videodev_lock
);
324 printk(KERN_ERR
"%s: get_index failed\n", __func__
);
328 ret
= cdev_add(&vfd
->cdev
, MKDEV(VIDEO_MAJOR
, vfd
->minor
), 1);
330 printk(KERN_ERR
"%s: cdev_add failed\n", __func__
);
334 memset(&vfd
->dev
, 0, sizeof(vfd
->dev
));
335 /* The memset above cleared the device's drvdata, so
336 put back the copy we made earlier. */
337 video_set_drvdata(vfd
, priv
);
338 vfd
->dev
.class = &video_class
;
339 vfd
->dev
.devt
= MKDEV(VIDEO_MAJOR
, vfd
->minor
);
341 vfd
->dev
.parent
= vfd
->parent
;
342 sprintf(vfd
->dev
.bus_id
, "%s%d", name_base
, nr
);
343 ret
= device_register(&vfd
->dev
);
345 printk(KERN_ERR
"%s: device_register failed\n", __func__
);
348 /* Remember the cdev's release function */
349 vfd
->cdev_release
= vfd
->cdev
.kobj
.ktype
->release
;
350 /* Install our own */
351 vfd
->cdev
.kobj
.ktype
= &v4l2_ktype_cdev_default
;
355 cdev_del(&vfd
->cdev
);
358 mutex_lock(&videodev_lock
);
359 video_device
[vfd
->minor
] = NULL
;
360 clear_bit(vfd
->num
, video_nums
[type
]);
361 mutex_unlock(&videodev_lock
);
365 EXPORT_SYMBOL(video_register_device_index
);
368 * video_unregister_device - unregister a video4linux device
369 * @vfd: the device to unregister
371 * This unregisters the passed device and deassigns the minor
372 * number. Future open calls will be met with errors.
375 void video_unregister_device(struct video_device
*vfd
)
377 device_unregister(&vfd
->dev
);
379 EXPORT_SYMBOL(video_unregister_device
);
382 * Initialise video for linux
384 static int __init
videodev_init(void)
386 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
389 printk(KERN_INFO
"Linux video capture interface: v2.00\n");
390 ret
= register_chrdev_region(dev
, VIDEO_NUM_DEVICES
, VIDEO_NAME
);
392 printk(KERN_WARNING
"videodev: unable to get major %d\n",
397 ret
= class_register(&video_class
);
399 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
400 printk(KERN_WARNING
"video_dev: class_register failed\n");
407 static void __exit
videodev_exit(void)
409 dev_t dev
= MKDEV(VIDEO_MAJOR
, 0);
411 class_unregister(&video_class
);
412 unregister_chrdev_region(dev
, VIDEO_NUM_DEVICES
);
415 module_init(videodev_init
)
416 module_exit(videodev_exit
)
418 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
419 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
420 MODULE_LICENSE("GPL");