blob: ead6d18e51adc4a2230f5acf1138c24f9d0ad928 [file] [log] [blame]
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001/*
2 * Video capture interface for Linux version 2
3 *
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
6 *
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.
11 *
Alan Coxd9b01442008-10-27 15:13:47 -030012 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030013 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
14 *
15 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
16 * - Added procfs support
17 */
18
19#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/kernel.h>
22#include <linux/mm.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>
31
32#include <media/v4l2-common.h>
33
34#define VIDEO_NUM_DEVICES 256
35#define VIDEO_NAME "video4linux"
36
37/*
38 * sysfs stuff
39 */
40
41static ssize_t show_index(struct device *cd,
42 struct device_attribute *attr, char *buf)
43{
Hans Verkuil22a04f12008-07-20 06:35:02 -030044 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030045
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030046 return sprintf(buf, "%i\n", vfd->index);
47}
48
49static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
51{
Hans Verkuil22a04f12008-07-20 06:35:02 -030052 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030053
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030054 return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
55}
56
57static struct device_attribute video_device_attrs[] = {
58 __ATTR(name, S_IRUGO, show_name, NULL),
59 __ATTR(index, S_IRUGO, show_index, NULL),
60 __ATTR_NULL
61};
62
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030063/*
64 * Active devices
65 */
66static struct video_device *video_device[VIDEO_NUM_DEVICES];
67static DEFINE_MUTEX(videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -030068static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030069
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030070struct video_device *video_device_alloc(void)
71{
Hans Verkuilbfa8a272008-08-23 07:48:38 -030072 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030073}
74EXPORT_SYMBOL(video_device_alloc);
75
76void video_device_release(struct video_device *vfd)
77{
78 kfree(vfd);
79}
80EXPORT_SYMBOL(video_device_release);
81
Hans Verkuilf9e86b52008-08-23 05:47:41 -030082void video_device_release_empty(struct video_device *vfd)
83{
84 /* Do nothing */
85 /* Only valid when the video_device struct is a static. */
86}
87EXPORT_SYMBOL(video_device_release_empty);
88
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030089/* Called when the last user of the character device is gone. */
90static void v4l2_chardev_release(struct kobject *kobj)
91{
92 struct video_device *vfd = container_of(kobj, struct video_device, cdev.kobj);
93
94 mutex_lock(&videodev_lock);
Hans Verkuil6ea9a182008-08-30 09:40:47 -030095 if (video_device[vfd->minor] != vfd) {
96 mutex_unlock(&videodev_lock);
97 BUG();
98 return;
99 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300100
101 /* Free up this device for reuse */
102 video_device[vfd->minor] = NULL;
Hans Verkuildd896012008-10-04 08:36:54 -0300103 clear_bit(vfd->num, video_nums[vfd->vfl_type]);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300104 mutex_unlock(&videodev_lock);
105
106 /* Release the character device */
107 vfd->cdev_release(kobj);
108 /* Release video_device and perform other
109 cleanups as needed. */
110 if (vfd->release)
111 vfd->release(vfd);
112}
113
114/* The new kobj_type for the character device */
115static struct kobj_type v4l2_ktype_cdev_default = {
116 .release = v4l2_chardev_release,
117};
118
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300119static void video_release(struct device *cd)
120{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300121 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300122
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300123 /* 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);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300128}
129
130static struct class video_class = {
131 .name = VIDEO_NAME,
132 .dev_attrs = video_device_attrs,
133 .dev_release = video_release,
134};
135
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300136struct video_device *video_devdata(struct file *file)
137{
138 return video_device[iminor(file->f_path.dentry->d_inode)];
139}
140EXPORT_SYMBOL(video_devdata);
141
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300142/**
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
146 *
147 *
148 * returns -ENFILE if num is already in use, a free index number if
149 * successful.
150 */
151static int get_index(struct video_device *vdev, int num)
152{
153 u32 used = 0;
154 const int max_index = sizeof(used) * 8 - 1;
155 int i;
156
157 /* Currently a single v4l driver instance cannot create more than
158 32 devices.
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__);
162 return -EINVAL;
163 }
164
165 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
166 if (video_device[i] != NULL &&
167 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300168 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300169 used |= 1 << video_device[i]->index;
170 }
171 }
172
173 if (num >= 0) {
174 if (used & (1 << num))
175 return -ENFILE;
176 return num;
177 }
178
179 i = ffz(used);
180 return i > max_index ? -ENFILE : i;
181}
182
183static const struct file_operations video_fops;
184
185int video_register_device(struct video_device *vfd, int type, int nr)
186{
187 return video_register_device_index(vfd, type, nr, -1);
188}
189EXPORT_SYMBOL(video_register_device);
190
191/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300192 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300193 * @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, ...
196 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300197 * @index: stream number based on parent device;
198 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300199 *
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).
204 *
205 * Zero is returned on success.
206 *
207 * Valid types are
208 *
209 * %VFL_TYPE_GRABBER - A frame grabber
210 *
211 * %VFL_TYPE_VTX - A teletext device
212 *
213 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
214 *
215 * %VFL_TYPE_RADIO - A radio card
216 */
217
218int video_register_device_index(struct video_device *vfd, int type, int nr,
219 int index)
220{
221 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300222 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300223 int minor_offset = 0;
224 int minor_cnt = VIDEO_NUM_DEVICES;
225 const char *name_base;
Hans Verkuil9d95af92008-08-24 11:18:47 -0300226 void *priv = video_get_drvdata(vfd);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300227
Hans Verkuild6e74972008-08-23 06:27:59 -0300228 /* the release callback MUST be present */
229 BUG_ON(!vfd->release);
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300230
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300231 if (vfd == NULL)
232 return -EINVAL;
233
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300234 switch (type) {
235 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300236 name_base = "video";
237 break;
238 case VFL_TYPE_VTX:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300239 name_base = "vtx";
240 break;
241 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300242 name_base = "vbi";
243 break;
244 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300245 name_base = "radio";
246 break;
247 default:
248 printk(KERN_ERR "%s called with unknown type: %d\n",
249 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300250 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300251 }
252
Hans Verkuildd896012008-10-04 08:36:54 -0300253 vfd->vfl_type = type;
254
255#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
256 /* Keep the ranges for the first four types for historical
257 * reasons.
258 * Newer devices (not yet in place) should use the range
259 * of 128-191 and just pick the first free minor there
260 * (new style). */
261 switch (type) {
262 case VFL_TYPE_GRABBER:
263 minor_offset = 0;
264 minor_cnt = 64;
265 break;
266 case VFL_TYPE_RADIO:
267 minor_offset = 64;
268 minor_cnt = 64;
269 break;
270 case VFL_TYPE_VTX:
271 minor_offset = 192;
272 minor_cnt = 32;
273 break;
274 case VFL_TYPE_VBI:
275 minor_offset = 224;
276 minor_cnt = 32;
277 break;
278 default:
279 minor_offset = 128;
280 minor_cnt = 64;
281 break;
282 }
283#endif
284
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300285 /* Initialize the character device */
286 cdev_init(&vfd->cdev, vfd->fops);
287 vfd->cdev.owner = vfd->fops->owner;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300288 /* pick a minor number */
289 mutex_lock(&videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -0300290 nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
291 if (nr == minor_cnt)
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);
296 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300297 }
Hans Verkuildd896012008-10-04 08:36:54 -0300298#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
299 /* 1-on-1 mapping of kernel number to minor number */
300 i = nr;
301#else
302 /* The kernel number and minor numbers are independent */
303 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
304 if (video_device[i] == NULL)
305 break;
306 if (i == VIDEO_NUM_DEVICES) {
307 mutex_unlock(&videodev_lock);
308 printk(KERN_ERR "could not get a free minor\n");
309 return -ENFILE;
310 }
311#endif
312 vfd->minor = i + minor_offset;
313 vfd->num = nr;
314 set_bit(nr, video_nums[type]);
315 BUG_ON(video_device[vfd->minor]);
316 video_device[vfd->minor] = vfd;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300317
318 ret = get_index(vfd, index);
319 vfd->index = ret;
320
321 mutex_unlock(&videodev_lock);
322
323 if (ret < 0) {
324 printk(KERN_ERR "%s: get_index failed\n", __func__);
325 goto fail_minor;
326 }
327
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300328 ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1);
329 if (ret < 0) {
330 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
331 goto fail_minor;
332 }
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300333 /* sysfs class */
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300334 memset(&vfd->dev, 0, sizeof(vfd->dev));
Hans Verkuil9d95af92008-08-24 11:18:47 -0300335 /* The memset above cleared the device's drvdata, so
336 put back the copy we made earlier. */
337 video_set_drvdata(vfd, priv);
Hans Verkuil22a04f12008-07-20 06:35:02 -0300338 vfd->dev.class = &video_class;
339 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300340 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300341 vfd->dev.parent = vfd->parent;
Hans Verkuildd896012008-10-04 08:36:54 -0300342 sprintf(vfd->dev.bus_id, "%s%d", name_base, nr);
Hans Verkuil22a04f12008-07-20 06:35:02 -0300343 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300344 if (ret < 0) {
345 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300346 goto del_cdev;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300347 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300348 /* 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;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300352 return 0;
353
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300354del_cdev:
355 cdev_del(&vfd->cdev);
356
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300357fail_minor:
358 mutex_lock(&videodev_lock);
359 video_device[vfd->minor] = NULL;
Hans Verkuildd896012008-10-04 08:36:54 -0300360 clear_bit(vfd->num, video_nums[type]);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300361 mutex_unlock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300362 vfd->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300363 return ret;
364}
365EXPORT_SYMBOL(video_register_device_index);
366
367/**
368 * video_unregister_device - unregister a video4linux device
369 * @vfd: the device to unregister
370 *
371 * This unregisters the passed device and deassigns the minor
372 * number. Future open calls will be met with errors.
373 */
374
375void video_unregister_device(struct video_device *vfd)
376{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300377 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300378}
379EXPORT_SYMBOL(video_unregister_device);
380
381/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300382 * Initialise video for linux
383 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300384static int __init videodev_init(void)
385{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300386 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300387 int ret;
388
389 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300390 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
391 if (ret < 0) {
392 printk(KERN_WARNING "videodev: unable to get major %d\n",
393 VIDEO_MAJOR);
394 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300395 }
396
397 ret = class_register(&video_class);
398 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300399 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300400 printk(KERN_WARNING "video_dev: class_register failed\n");
401 return -EIO;
402 }
403
404 return 0;
405}
406
407static void __exit videodev_exit(void)
408{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300409 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
410
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300411 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300412 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300413}
414
415module_init(videodev_init)
416module_exit(videodev_exit)
417
418MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
419MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
420MODULE_LICENSE("GPL");
421
422
423/*
424 * Local variables:
425 * c-basic-offset: 8
426 * End:
427 */