blob: 7addf2fd55de45cda286735d1f91809b23c7d806 [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 *
12 * Authors: Alan Cox, <alan@redhat.com> (version 1)
13 * 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);
68
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030069struct video_device *video_device_alloc(void)
70{
Hans Verkuilbfa8a272008-08-23 07:48:38 -030071 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030072}
73EXPORT_SYMBOL(video_device_alloc);
74
75void video_device_release(struct video_device *vfd)
76{
77 kfree(vfd);
78}
79EXPORT_SYMBOL(video_device_release);
80
Hans Verkuilf9e86b52008-08-23 05:47:41 -030081void video_device_release_empty(struct video_device *vfd)
82{
83 /* Do nothing */
84 /* Only valid when the video_device struct is a static. */
85}
86EXPORT_SYMBOL(video_device_release_empty);
87
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030088/* Called when the last user of the character device is gone. */
89static void v4l2_chardev_release(struct kobject *kobj)
90{
91 struct video_device *vfd = container_of(kobj, struct video_device, cdev.kobj);
92
93 mutex_lock(&videodev_lock);
Hans Verkuil6ea9a182008-08-30 09:40:47 -030094 if (video_device[vfd->minor] != vfd) {
95 mutex_unlock(&videodev_lock);
96 BUG();
97 return;
98 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030099
100 /* Free up this device for reuse */
101 video_device[vfd->minor] = NULL;
102 mutex_unlock(&videodev_lock);
103
104 /* Release the character device */
105 vfd->cdev_release(kobj);
106 /* Release video_device and perform other
107 cleanups as needed. */
108 if (vfd->release)
109 vfd->release(vfd);
110}
111
112/* The new kobj_type for the character device */
113static struct kobj_type v4l2_ktype_cdev_default = {
114 .release = v4l2_chardev_release,
115};
116
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300117static void video_release(struct device *cd)
118{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300119 struct video_device *vfd = container_of(cd, struct video_device, dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300120
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300121 /* It's now safe to delete the char device.
122 This will either trigger the v4l2_chardev_release immediately (if
123 the refcount goes to 0) or later when the last user of the
124 character device closes it. */
125 cdev_del(&vfd->cdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300126}
127
128static struct class video_class = {
129 .name = VIDEO_NAME,
130 .dev_attrs = video_device_attrs,
131 .dev_release = video_release,
132};
133
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300134struct video_device *video_devdata(struct file *file)
135{
136 return video_device[iminor(file->f_path.dentry->d_inode)];
137}
138EXPORT_SYMBOL(video_devdata);
139
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300140/**
141 * get_index - assign stream number based on parent device
142 * @vdev: video_device to assign index number to, vdev->dev should be assigned
143 * @num: -1 if auto assign, requested number otherwise
144 *
145 *
146 * returns -ENFILE if num is already in use, a free index number if
147 * successful.
148 */
149static int get_index(struct video_device *vdev, int num)
150{
151 u32 used = 0;
152 const int max_index = sizeof(used) * 8 - 1;
153 int i;
154
155 /* Currently a single v4l driver instance cannot create more than
156 32 devices.
157 Increase to u64 or an array of u32 if more are needed. */
158 if (num > max_index) {
159 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
160 return -EINVAL;
161 }
162
163 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
164 if (video_device[i] != NULL &&
165 video_device[i] != vdev &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300166 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300167 used |= 1 << video_device[i]->index;
168 }
169 }
170
171 if (num >= 0) {
172 if (used & (1 << num))
173 return -ENFILE;
174 return num;
175 }
176
177 i = ffz(used);
178 return i > max_index ? -ENFILE : i;
179}
180
181static const struct file_operations video_fops;
182
183int video_register_device(struct video_device *vfd, int type, int nr)
184{
185 return video_register_device_index(vfd, type, nr, -1);
186}
187EXPORT_SYMBOL(video_register_device);
188
189/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300190 * video_register_device_index - register video4linux devices
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300191 * @vfd: video device structure we want to register
192 * @type: type of device to register
193 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
194 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300195 * @index: stream number based on parent device;
196 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300197 *
198 * The registration code assigns minor numbers based on the type
199 * requested. -ENFILE is returned in all the device slots for this
200 * category are full. If not then the minor field is set and the
201 * driver initialize function is called (if non %NULL).
202 *
203 * Zero is returned on success.
204 *
205 * Valid types are
206 *
207 * %VFL_TYPE_GRABBER - A frame grabber
208 *
209 * %VFL_TYPE_VTX - A teletext device
210 *
211 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
212 *
213 * %VFL_TYPE_RADIO - A radio card
214 */
215
216int video_register_device_index(struct video_device *vfd, int type, int nr,
217 int index)
218{
219 int i = 0;
220 int base;
221 int end;
222 int ret;
223 char *name_base;
Hans Verkuil9d95af92008-08-24 11:18:47 -0300224 void *priv = video_get_drvdata(vfd);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300225
Hans Verkuild6e74972008-08-23 06:27:59 -0300226 /* the release callback MUST be present */
227 BUG_ON(!vfd->release);
Henrik Kretzschmarf3b9f502008-09-03 17:11:53 -0300228
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300229 if (vfd == NULL)
230 return -EINVAL;
231
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300232 switch (type) {
233 case VFL_TYPE_GRABBER:
234 base = MINOR_VFL_TYPE_GRABBER_MIN;
235 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
236 name_base = "video";
237 break;
238 case VFL_TYPE_VTX:
239 base = MINOR_VFL_TYPE_VTX_MIN;
240 end = MINOR_VFL_TYPE_VTX_MAX+1;
241 name_base = "vtx";
242 break;
243 case VFL_TYPE_VBI:
244 base = MINOR_VFL_TYPE_VBI_MIN;
245 end = MINOR_VFL_TYPE_VBI_MAX+1;
246 name_base = "vbi";
247 break;
248 case VFL_TYPE_RADIO:
249 base = MINOR_VFL_TYPE_RADIO_MIN;
250 end = MINOR_VFL_TYPE_RADIO_MAX+1;
251 name_base = "radio";
252 break;
253 default:
254 printk(KERN_ERR "%s called with unknown type: %d\n",
255 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300256 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300257 }
258
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300259 /* Initialize the character device */
260 cdev_init(&vfd->cdev, vfd->fops);
261 vfd->cdev.owner = vfd->fops->owner;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300262 /* pick a minor number */
263 mutex_lock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300264 if (nr >= 0 && nr < end-base) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300265 /* use the one the driver asked for */
266 i = base + nr;
267 if (NULL != video_device[i]) {
268 mutex_unlock(&videodev_lock);
269 return -ENFILE;
270 }
271 } else {
272 /* use first free */
273 for (i = base; i < end; i++)
274 if (NULL == video_device[i])
275 break;
276 if (i == end) {
277 mutex_unlock(&videodev_lock);
278 return -ENFILE;
279 }
280 }
281 video_device[i] = vfd;
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300282 vfd->vfl_type = type;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300283 vfd->minor = i;
284
285 ret = get_index(vfd, index);
286 vfd->index = ret;
287
288 mutex_unlock(&videodev_lock);
289
290 if (ret < 0) {
291 printk(KERN_ERR "%s: get_index failed\n", __func__);
292 goto fail_minor;
293 }
294
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300295 ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1);
296 if (ret < 0) {
297 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
298 goto fail_minor;
299 }
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300300 /* sysfs class */
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300301 memset(&vfd->dev, 0, sizeof(vfd->dev));
Hans Verkuil9d95af92008-08-24 11:18:47 -0300302 /* The memset above cleared the device's drvdata, so
303 put back the copy we made earlier. */
304 video_set_drvdata(vfd, priv);
Hans Verkuil22a04f12008-07-20 06:35:02 -0300305 vfd->dev.class = &video_class;
306 vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Hans Verkuil5e85e732008-07-20 06:31:39 -0300307 if (vfd->parent)
Hans Verkuil22a04f12008-07-20 06:35:02 -0300308 vfd->dev.parent = vfd->parent;
309 sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
310 ret = device_register(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300311 if (ret < 0) {
312 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300313 goto del_cdev;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300314 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300315 /* Remember the cdev's release function */
316 vfd->cdev_release = vfd->cdev.kobj.ktype->release;
317 /* Install our own */
318 vfd->cdev.kobj.ktype = &v4l2_ktype_cdev_default;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300319 return 0;
320
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300321del_cdev:
322 cdev_del(&vfd->cdev);
323
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300324fail_minor:
325 mutex_lock(&videodev_lock);
326 video_device[vfd->minor] = NULL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300327 mutex_unlock(&videodev_lock);
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300328 vfd->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300329 return ret;
330}
331EXPORT_SYMBOL(video_register_device_index);
332
333/**
334 * video_unregister_device - unregister a video4linux device
335 * @vfd: the device to unregister
336 *
337 * This unregisters the passed device and deassigns the minor
338 * number. Future open calls will be met with errors.
339 */
340
341void video_unregister_device(struct video_device *vfd)
342{
Hans Verkuil22a04f12008-07-20 06:35:02 -0300343 device_unregister(&vfd->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300344}
345EXPORT_SYMBOL(video_unregister_device);
346
347/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300348 * Initialise video for linux
349 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300350static int __init videodev_init(void)
351{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300352 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300353 int ret;
354
355 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300356 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
357 if (ret < 0) {
358 printk(KERN_WARNING "videodev: unable to get major %d\n",
359 VIDEO_MAJOR);
360 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300361 }
362
363 ret = class_register(&video_class);
364 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300365 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300366 printk(KERN_WARNING "video_dev: class_register failed\n");
367 return -EIO;
368 }
369
370 return 0;
371}
372
373static void __exit videodev_exit(void)
374{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300375 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
376
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300377 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300378 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300379}
380
381module_init(videodev_init)
382module_exit(videodev_exit)
383
384MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
385MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
386MODULE_LICENSE("GPL");
387
388
389/*
390 * Local variables:
391 * c-basic-offset: 8
392 * End:
393 */