blob: 4e0db8845e043fa5ec6727f0b00124bc6e3a751f [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 Verkuildc93a702008-12-19 21:28:27 -030044 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030045
Hans Verkuildc93a702008-12-19 21:28:27 -030046 return sprintf(buf, "%i\n", vdev->index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030047}
48
49static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
51{
Hans Verkuildc93a702008-12-19 21:28:27 -030052 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030053
Hans Verkuildc93a702008-12-19 21:28:27 -030054 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030055}
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
Hans Verkuildc93a702008-12-19 21:28:27 -030076void video_device_release(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030077{
Hans Verkuildc93a702008-12-19 21:28:27 -030078 kfree(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030079}
80EXPORT_SYMBOL(video_device_release);
81
Hans Verkuildc93a702008-12-19 21:28:27 -030082void video_device_release_empty(struct video_device *vdev)
Hans Verkuilf9e86b52008-08-23 05:47:41 -030083{
84 /* Do nothing */
85 /* Only valid when the video_device struct is a static. */
86}
87EXPORT_SYMBOL(video_device_release_empty);
88
Hans Verkuildc93a702008-12-19 21:28:27 -030089static inline void video_get(struct video_device *vdev)
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030090{
Hans Verkuildc93a702008-12-19 21:28:27 -030091 get_device(&vdev->dev);
92}
93
94static inline void video_put(struct video_device *vdev)
95{
96 put_device(&vdev->dev);
97}
98
99/* Called when the last user of the video device exits. */
100static void v4l2_device_release(struct device *cd)
101{
102 struct video_device *vdev = to_video_device(cd);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300103
104 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300105 if (video_device[vdev->minor] != vdev) {
Hans Verkuil6ea9a182008-08-30 09:40:47 -0300106 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300107 /* should not happen */
108 WARN_ON(1);
Hans Verkuil6ea9a182008-08-30 09:40:47 -0300109 return;
110 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300111
112 /* Free up this device for reuse */
Hans Verkuildc93a702008-12-19 21:28:27 -0300113 video_device[vdev->minor] = NULL;
114
115 /* Delete the cdev on this minor as well */
116 cdev_del(vdev->cdev);
117 /* Just in case some driver tries to access this from
118 the release() callback. */
119 vdev->cdev = NULL;
120
121 /* Mark minor as free */
122 clear_bit(vdev->num, video_nums[vdev->vfl_type]);
123
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300124 mutex_unlock(&videodev_lock);
125
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300126 /* Release video_device and perform other
127 cleanups as needed. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300128 vdev->release(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300129}
130
131static struct class video_class = {
132 .name = VIDEO_NAME,
133 .dev_attrs = video_device_attrs,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300134};
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 Verkuildc93a702008-12-19 21:28:27 -0300142static ssize_t v4l2_read(struct file *filp, char __user *buf,
143 size_t sz, loff_t *off)
144{
145 struct video_device *vdev = video_devdata(filp);
146
147 if (!vdev->fops->read)
148 return -EINVAL;
149 if (video_is_unregistered(vdev))
150 return -EIO;
151 return vdev->fops->read(filp, buf, sz, off);
152}
153
154static ssize_t v4l2_write(struct file *filp, const char __user *buf,
155 size_t sz, loff_t *off)
156{
157 struct video_device *vdev = video_devdata(filp);
158
159 if (!vdev->fops->write)
160 return -EINVAL;
161 if (video_is_unregistered(vdev))
162 return -EIO;
163 return vdev->fops->write(filp, buf, sz, off);
164}
165
166static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
167{
168 struct video_device *vdev = video_devdata(filp);
169
170 if (!vdev->fops->poll || video_is_unregistered(vdev))
171 return DEFAULT_POLLMASK;
172 return vdev->fops->poll(filp, poll);
173}
174
175static int v4l2_ioctl(struct inode *inode, struct file *filp,
176 unsigned int cmd, unsigned long arg)
177{
178 struct video_device *vdev = video_devdata(filp);
179
180 if (!vdev->fops->ioctl)
181 return -ENOTTY;
182 /* Allow ioctl to continue even if the device was unregistered.
183 Things like dequeueing buffers might still be useful. */
184 return vdev->fops->ioctl(inode, filp, cmd, arg);
185}
186
187static long v4l2_unlocked_ioctl(struct file *filp,
188 unsigned int cmd, unsigned long arg)
189{
190 struct video_device *vdev = video_devdata(filp);
191
192 if (!vdev->fops->unlocked_ioctl)
193 return -ENOTTY;
194 /* Allow ioctl to continue even if the device was unregistered.
195 Things like dequeueing buffers might still be useful. */
196 return vdev->fops->unlocked_ioctl(filp, cmd, arg);
197}
198
199#ifdef CONFIG_COMPAT
200static long v4l2_compat_ioctl(struct file *filp,
201 unsigned int cmd, unsigned long arg)
202{
203 struct video_device *vdev = video_devdata(filp);
204
205 if (!vdev->fops->compat_ioctl)
206 return -ENOIOCTLCMD;
207 /* Allow ioctl to continue even if the device was unregistered.
208 Things like dequeueing buffers might still be useful. */
209 return vdev->fops->compat_ioctl(filp, cmd, arg);
210}
211#endif
212
213static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
214{
215 struct video_device *vdev = video_devdata(filp);
216
217 if (!vdev->fops->mmap ||
218 video_is_unregistered(vdev))
219 return -ENODEV;
220 return vdev->fops->mmap(filp, vm);
221}
222
223/* Override for the open function */
224static int v4l2_open(struct inode *inode, struct file *filp)
225{
226 struct video_device *vdev;
227 int ret;
228
229 /* Check if the video device is available */
230 mutex_lock(&videodev_lock);
231 vdev = video_devdata(filp);
232 /* return ENODEV if the video device has been removed
233 already or if it is not registered anymore. */
234 if (vdev == NULL || video_is_unregistered(vdev)) {
235 mutex_unlock(&videodev_lock);
236 return -ENODEV;
237 }
238 /* and increase the device refcount */
239 video_get(vdev);
240 mutex_unlock(&videodev_lock);
241 ret = vdev->fops->open(inode, filp);
242 /* decrease the refcount in case of an error */
243 if (ret)
244 video_put(vdev);
245 return ret;
246}
247
248/* Override for the release function */
249static int v4l2_release(struct inode *inode, struct file *filp)
250{
251 struct video_device *vdev = video_devdata(filp);
252 int ret = vdev->fops->release(inode, filp);
253
254 /* decrease the refcount unconditionally since the release()
255 return value is ignored. */
256 video_put(vdev);
257 return ret;
258}
259
260static const struct file_operations v4l2_unlocked_fops = {
261 .owner = THIS_MODULE,
262 .read = v4l2_read,
263 .write = v4l2_write,
264 .open = v4l2_open,
265 .mmap = v4l2_mmap,
266 .unlocked_ioctl = v4l2_unlocked_ioctl,
267#ifdef CONFIG_COMPAT
268 .compat_ioctl = v4l2_compat_ioctl,
269#endif
270 .release = v4l2_release,
271 .poll = v4l2_poll,
272 .llseek = no_llseek,
273};
274
275static const struct file_operations v4l2_fops = {
276 .owner = THIS_MODULE,
277 .read = v4l2_read,
278 .write = v4l2_write,
279 .open = v4l2_open,
280 .mmap = v4l2_mmap,
281 .ioctl = v4l2_ioctl,
282#ifdef CONFIG_COMPAT
283 .compat_ioctl = v4l2_compat_ioctl,
284#endif
285 .release = v4l2_release,
286 .poll = v4l2_poll,
287 .llseek = no_llseek,
288};
289
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300290/**
291 * get_index - assign stream number based on parent device
Hans Verkuildc93a702008-12-19 21:28:27 -0300292 * @vdev: video_device to assign index number to, vdev->parent should be assigned
293 * @num: -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300294 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300295 * Note that when this is called the new device has not yet been registered
296 * in the video_device array.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300297 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300298 * Returns -ENFILE if num is already in use, a free index number if
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300299 * successful.
300 */
301static int get_index(struct video_device *vdev, int num)
302{
303 u32 used = 0;
304 const int max_index = sizeof(used) * 8 - 1;
305 int i;
306
307 /* Currently a single v4l driver instance cannot create more than
308 32 devices.
309 Increase to u64 or an array of u32 if more are needed. */
310 if (num > max_index) {
311 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
312 return -EINVAL;
313 }
314
Hans Verkuil806e5b72008-12-19 09:10:56 -0300315 /* Some drivers do not set the parent. In that case always return 0. */
316 if (vdev->parent == NULL)
317 return 0;
318
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300319 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
320 if (video_device[i] != NULL &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300321 video_device[i]->parent == vdev->parent) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300322 used |= 1 << video_device[i]->index;
323 }
324 }
325
326 if (num >= 0) {
327 if (used & (1 << num))
328 return -ENFILE;
329 return num;
330 }
331
332 i = ffz(used);
333 return i > max_index ? -ENFILE : i;
334}
335
Hans Verkuildc93a702008-12-19 21:28:27 -0300336int video_register_device(struct video_device *vdev, int type, int nr)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300337{
Hans Verkuildc93a702008-12-19 21:28:27 -0300338 return video_register_device_index(vdev, type, nr, -1);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300339}
340EXPORT_SYMBOL(video_register_device);
341
342/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300343 * video_register_device_index - register video4linux devices
Hans Verkuildc93a702008-12-19 21:28:27 -0300344 * @vdev: video device structure we want to register
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300345 * @type: type of device to register
346 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
347 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300348 * @index: stream number based on parent device;
349 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300350 *
351 * The registration code assigns minor numbers based on the type
352 * requested. -ENFILE is returned in all the device slots for this
353 * category are full. If not then the minor field is set and the
354 * driver initialize function is called (if non %NULL).
355 *
356 * Zero is returned on success.
357 *
358 * Valid types are
359 *
360 * %VFL_TYPE_GRABBER - A frame grabber
361 *
362 * %VFL_TYPE_VTX - A teletext device
363 *
364 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
365 *
366 * %VFL_TYPE_RADIO - A radio card
367 */
Hans Verkuildc93a702008-12-19 21:28:27 -0300368int video_register_device_index(struct video_device *vdev, int type, int nr,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300369 int index)
370{
371 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300372 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300373 int minor_offset = 0;
374 int minor_cnt = VIDEO_NUM_DEVICES;
375 const char *name_base;
Hans Verkuildc93a702008-12-19 21:28:27 -0300376 void *priv = video_get_drvdata(vdev);
377
378 /* A minor value of -1 marks this video device as never
379 having been registered */
380 if (vdev)
381 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300382
Hans Verkuild6e74972008-08-23 06:27:59 -0300383 /* the release callback MUST be present */
Hans Verkuildc93a702008-12-19 21:28:27 -0300384 WARN_ON(!vdev || !vdev->release);
385 if (!vdev || !vdev->release)
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300386 return -EINVAL;
387
Hans Verkuildc93a702008-12-19 21:28:27 -0300388 /* Part 1: check device type */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300389 switch (type) {
390 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300391 name_base = "video";
392 break;
393 case VFL_TYPE_VTX:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300394 name_base = "vtx";
395 break;
396 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300397 name_base = "vbi";
398 break;
399 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300400 name_base = "radio";
401 break;
402 default:
403 printk(KERN_ERR "%s called with unknown type: %d\n",
404 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300405 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300406 }
407
Hans Verkuildc93a702008-12-19 21:28:27 -0300408 vdev->vfl_type = type;
409 vdev->cdev = NULL;
Hans Verkuildd896012008-10-04 08:36:54 -0300410
Hans Verkuildc93a702008-12-19 21:28:27 -0300411 /* Part 2: find a free minor, kernel number and device index. */
Hans Verkuildd896012008-10-04 08:36:54 -0300412#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
413 /* Keep the ranges for the first four types for historical
414 * reasons.
415 * Newer devices (not yet in place) should use the range
416 * of 128-191 and just pick the first free minor there
417 * (new style). */
418 switch (type) {
419 case VFL_TYPE_GRABBER:
420 minor_offset = 0;
421 minor_cnt = 64;
422 break;
423 case VFL_TYPE_RADIO:
424 minor_offset = 64;
425 minor_cnt = 64;
426 break;
427 case VFL_TYPE_VTX:
428 minor_offset = 192;
429 minor_cnt = 32;
430 break;
431 case VFL_TYPE_VBI:
432 minor_offset = 224;
433 minor_cnt = 32;
434 break;
435 default:
436 minor_offset = 128;
437 minor_cnt = 64;
438 break;
439 }
440#endif
441
Hans Verkuildc93a702008-12-19 21:28:27 -0300442 /* Pick a minor number */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300443 mutex_lock(&videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -0300444 nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
445 if (nr == minor_cnt)
446 nr = find_first_zero_bit(video_nums[type], minor_cnt);
447 if (nr == minor_cnt) {
448 printk(KERN_ERR "could not get a free kernel number\n");
449 mutex_unlock(&videodev_lock);
450 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300451 }
Hans Verkuildd896012008-10-04 08:36:54 -0300452#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
453 /* 1-on-1 mapping of kernel number to minor number */
454 i = nr;
455#else
456 /* The kernel number and minor numbers are independent */
457 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
458 if (video_device[i] == NULL)
459 break;
460 if (i == VIDEO_NUM_DEVICES) {
461 mutex_unlock(&videodev_lock);
462 printk(KERN_ERR "could not get a free minor\n");
463 return -ENFILE;
464 }
465#endif
Hans Verkuildc93a702008-12-19 21:28:27 -0300466 vdev->minor = i + minor_offset;
467 vdev->num = nr;
Hans Verkuildd896012008-10-04 08:36:54 -0300468 set_bit(nr, video_nums[type]);
Hans Verkuildc93a702008-12-19 21:28:27 -0300469 /* Should not happen since we thought this minor was free */
470 WARN_ON(video_device[vdev->minor] != NULL);
471 ret = vdev->index = get_index(vdev, index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300472 mutex_unlock(&videodev_lock);
473
474 if (ret < 0) {
475 printk(KERN_ERR "%s: get_index failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300476 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300477 }
478
Hans Verkuildc93a702008-12-19 21:28:27 -0300479 /* Part 3: Initialize the character device */
480 vdev->cdev = cdev_alloc();
481 if (vdev->cdev == NULL) {
482 ret = -ENOMEM;
483 goto cleanup;
484 }
485 if (vdev->fops->unlocked_ioctl)
486 vdev->cdev->ops = &v4l2_unlocked_fops;
487 else
488 vdev->cdev->ops = &v4l2_fops;
489 vdev->cdev->owner = vdev->fops->owner;
490 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300491 if (ret < 0) {
492 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300493 kfree(vdev->cdev);
494 vdev->cdev = NULL;
495 goto cleanup;
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300496 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300497
498 /* Part 4: register the device with sysfs */
499 memset(&vdev->dev, 0, sizeof(vdev->dev));
Hans Verkuil9d95af92008-08-24 11:18:47 -0300500 /* The memset above cleared the device's drvdata, so
501 put back the copy we made earlier. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300502 video_set_drvdata(vdev, priv);
503 vdev->dev.class = &video_class;
504 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
505 if (vdev->parent)
506 vdev->dev.parent = vdev->parent;
507 dev_set_name(&vdev->dev, "%s%d", name_base, nr);
508 ret = device_register(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300509 if (ret < 0) {
510 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300511 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300512 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300513 /* Register the release callback that will be called when the last
514 reference to the device goes away. */
515 vdev->dev.release = v4l2_device_release;
516
517 /* Part 5: Activate this minor. The char device can now be used. */
518 mutex_lock(&videodev_lock);
519 video_device[vdev->minor] = vdev;
520 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300521 return 0;
522
Hans Verkuildc93a702008-12-19 21:28:27 -0300523cleanup:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300524 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300525 if (vdev->cdev)
526 cdev_del(vdev->cdev);
527 clear_bit(vdev->num, video_nums[type]);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300528 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300529 /* Mark this video device as never having been registered. */
530 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300531 return ret;
532}
533EXPORT_SYMBOL(video_register_device_index);
534
535/**
536 * video_unregister_device - unregister a video4linux device
Hans Verkuildc93a702008-12-19 21:28:27 -0300537 * @vdev: the device to unregister
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300538 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300539 * This unregisters the passed device. Future open calls will
540 * be met with errors.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300541 */
Hans Verkuildc93a702008-12-19 21:28:27 -0300542void video_unregister_device(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300543{
Hans Verkuildc93a702008-12-19 21:28:27 -0300544 /* Check if vdev was ever registered at all */
545 if (!vdev || vdev->minor < 0)
546 return;
547
548 mutex_lock(&videodev_lock);
549 set_bit(V4L2_FL_UNREGISTERED, &vdev->flags);
550 mutex_unlock(&videodev_lock);
551 device_unregister(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300552}
553EXPORT_SYMBOL(video_unregister_device);
554
555/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300556 * Initialise video for linux
557 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300558static int __init videodev_init(void)
559{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300560 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300561 int ret;
562
563 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300564 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
565 if (ret < 0) {
566 printk(KERN_WARNING "videodev: unable to get major %d\n",
567 VIDEO_MAJOR);
568 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300569 }
570
571 ret = class_register(&video_class);
572 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300573 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300574 printk(KERN_WARNING "video_dev: class_register failed\n");
575 return -EIO;
576 }
577
578 return 0;
579}
580
581static void __exit videodev_exit(void)
582{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300583 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
584
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300585 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300586 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300587}
588
589module_init(videodev_init)
590module_exit(videodev_exit)
591
592MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
593MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
594MODULE_LICENSE("GPL");
595
596
597/*
598 * Local variables:
599 * c-basic-offset: 8
600 * End:
601 */