blob: c5ca51a9020a9a1b42da4e91925bb2113a0411c1 [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
Hans Verkuil806e5b72008-12-19 09:10:56 -0300165 /* Some drivers do not set the parent. In that case always return 0. */
166 if (vdev->parent == NULL)
167 return 0;
168
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300169 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
170 if (video_device[i] != NULL &&
171 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300172 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300173 used |= 1 << video_device[i]->index;
174 }
175 }
176
177 if (num >= 0) {
178 if (used & (1 << num))
179 return -ENFILE;
180 return num;
181 }
182
183 i = ffz(used);
184 return i > max_index ? -ENFILE : i;
185}
186
187static const struct file_operations video_fops;
188
189int video_register_device(struct video_device *vfd, int type, int nr)
190{
191 return video_register_device_index(vfd, type, nr, -1);
192}
193EXPORT_SYMBOL(video_register_device);
194
195/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300196 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300197 * @vfd: video device structure we want to register
198 * @type: type of device to register
199 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
200 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300201 * @index: stream number based on parent device;
202 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300203 *
204 * The registration code assigns minor numbers based on the type
205 * requested. -ENFILE is returned in all the device slots for this
206 * category are full. If not then the minor field is set and the
207 * driver initialize function is called (if non %NULL).
208 *
209 * Zero is returned on success.
210 *
211 * Valid types are
212 *
213 * %VFL_TYPE_GRABBER - A frame grabber
214 *
215 * %VFL_TYPE_VTX - A teletext device
216 *
217 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
218 *
219 * %VFL_TYPE_RADIO - A radio card
220 */
221
222int video_register_device_index(struct video_device *vfd, int type, int nr,
223 int index)
224{
225 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300226 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300227 int minor_offset = 0;
228 int minor_cnt = VIDEO_NUM_DEVICES;
229 const char *name_base;
Hans Verkuil9d95af92008-08-24 11:18:47 -0300230 void *priv = video_get_drvdata(vfd);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300231
Hans Verkuild6e74972008-08-23 06:27:59 -0300232 /* the release callback MUST be present */
233 BUG_ON(!vfd->release);
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300234
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300235 if (vfd == NULL)
236 return -EINVAL;
237
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300238 switch (type) {
239 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300240 name_base = "video";
241 break;
242 case VFL_TYPE_VTX:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300243 name_base = "vtx";
244 break;
245 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300246 name_base = "vbi";
247 break;
248 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300249 name_base = "radio";
250 break;
251 default:
252 printk(KERN_ERR "%s called with unknown type: %d\n",
253 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300254 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300255 }
256
Hans Verkuildd896012008-10-04 08:36:54 -0300257 vfd->vfl_type = type;
258
259#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
260 /* Keep the ranges for the first four types for historical
261 * reasons.
262 * Newer devices (not yet in place) should use the range
263 * of 128-191 and just pick the first free minor there
264 * (new style). */
265 switch (type) {
266 case VFL_TYPE_GRABBER:
267 minor_offset = 0;
268 minor_cnt = 64;
269 break;
270 case VFL_TYPE_RADIO:
271 minor_offset = 64;
272 minor_cnt = 64;
273 break;
274 case VFL_TYPE_VTX:
275 minor_offset = 192;
276 minor_cnt = 32;
277 break;
278 case VFL_TYPE_VBI:
279 minor_offset = 224;
280 minor_cnt = 32;
281 break;
282 default:
283 minor_offset = 128;
284 minor_cnt = 64;
285 break;
286 }
287#endif
288
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300289 /* Initialize the character device */
290 cdev_init(&vfd->cdev, vfd->fops);
291 vfd->cdev.owner = vfd->fops->owner;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300292 /* pick a minor number */
293 mutex_lock(&videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -0300294 nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
295 if (nr == minor_cnt)
296 nr = find_first_zero_bit(video_nums[type], minor_cnt);
297 if (nr == minor_cnt) {
298 printk(KERN_ERR "could not get a free kernel number\n");
299 mutex_unlock(&videodev_lock);
300 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300301 }
Hans Verkuildd896012008-10-04 08:36:54 -0300302#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
303 /* 1-on-1 mapping of kernel number to minor number */
304 i = nr;
305#else
306 /* The kernel number and minor numbers are independent */
307 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
308 if (video_device[i] == NULL)
309 break;
310 if (i == VIDEO_NUM_DEVICES) {
311 mutex_unlock(&videodev_lock);
312 printk(KERN_ERR "could not get a free minor\n");
313 return -ENFILE;
314 }
315#endif
316 vfd->minor = i + minor_offset;
317 vfd->num = nr;
318 set_bit(nr, video_nums[type]);
319 BUG_ON(video_device[vfd->minor]);
320 video_device[vfd->minor] = vfd;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300321
322 ret = get_index(vfd, index);
323 vfd->index = ret;
324
325 mutex_unlock(&videodev_lock);
326
327 if (ret < 0) {
328 printk(KERN_ERR "%s: get_index failed\n", __func__);
329 goto fail_minor;
330 }
331
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300332 ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1);
333 if (ret < 0) {
334 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
335 goto fail_minor;
336 }
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300337 /* sysfs class */
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300338 memset(&vfd->dev, 0, sizeof(vfd->dev));
Hans Verkuil9d95af92008-08-24 11:18:47 -0300339 /* The memset above cleared the device's drvdata, so
340 put back the copy we made earlier. */
341 video_set_drvdata(vfd, priv);
Hans Verkuil22a04f12008-07-20 06:35:02 -0300342 vfd->dev.class = &video_class;
343 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300344 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300345 vfd->dev.parent = vfd->parent;
Kay Sieversaf128a12008-10-30 00:51:46 -0300346 dev_set_name(&vfd->dev, "%s%d", name_base, nr);
Hans Verkuil22a04f12008-07-20 06:35:02 -0300347 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300348 if (ret < 0) {
349 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300350 goto del_cdev;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300351 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300352 /* Remember the cdev's release function */
353 vfd->cdev_release = vfd->cdev.kobj.ktype->release;
354 /* Install our own */
355 vfd->cdev.kobj.ktype = &v4l2_ktype_cdev_default;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300356 return 0;
357
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300358del_cdev:
359 cdev_del(&vfd->cdev);
360
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300361fail_minor:
362 mutex_lock(&videodev_lock);
363 video_device[vfd->minor] = NULL;
Hans Verkuildd896012008-10-04 08:36:54 -0300364 clear_bit(vfd->num, video_nums[type]);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300365 mutex_unlock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300366 vfd->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300367 return ret;
368}
369EXPORT_SYMBOL(video_register_device_index);
370
371/**
372 * video_unregister_device - unregister a video4linux device
373 * @vfd: the device to unregister
374 *
375 * This unregisters the passed device and deassigns the minor
376 * number. Future open calls will be met with errors.
377 */
378
379void video_unregister_device(struct video_device *vfd)
380{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300381 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300382}
383EXPORT_SYMBOL(video_unregister_device);
384
385/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300386 * Initialise video for linux
387 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300388static int __init videodev_init(void)
389{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300390 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300391 int ret;
392
393 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300394 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
395 if (ret < 0) {
396 printk(KERN_WARNING "videodev: unable to get major %d\n",
397 VIDEO_MAJOR);
398 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300399 }
400
401 ret = class_register(&video_class);
402 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300403 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300404 printk(KERN_WARNING "video_dev: class_register failed\n");
405 return -EIO;
406 }
407
408 return 0;
409}
410
411static void __exit videodev_exit(void)
412{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300413 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
414
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300415 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300416 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300417}
418
419module_init(videodev_init)
420module_exit(videodev_exit)
421
422MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
423MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
424MODULE_LICENSE("GPL");
425
426
427/*
428 * Local variables:
429 * c-basic-offset: 8
430 * End:
431 */