blob: 31eac66411d736e4330119900e111aa921e841e4 [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>
Hans Verkuil9bea3512008-12-23 07:35:17 -030033#include <media/v4l2-device.h>
Hans Verkuilbec43662008-12-30 06:58:20 -030034#include <media/v4l2-ioctl.h>
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030035
36#define VIDEO_NUM_DEVICES 256
37#define VIDEO_NAME "video4linux"
38
39/*
40 * sysfs stuff
41 */
42
43static ssize_t show_index(struct device *cd,
44 struct device_attribute *attr, char *buf)
45{
Hans Verkuildc93a702008-12-19 21:28:27 -030046 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030047
Hans Verkuildc93a702008-12-19 21:28:27 -030048 return sprintf(buf, "%i\n", vdev->index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030049}
50
51static ssize_t show_name(struct device *cd,
52 struct device_attribute *attr, char *buf)
53{
Hans Verkuildc93a702008-12-19 21:28:27 -030054 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030055
Hans Verkuildc93a702008-12-19 21:28:27 -030056 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030057}
58
59static struct device_attribute video_device_attrs[] = {
60 __ATTR(name, S_IRUGO, show_name, NULL),
61 __ATTR(index, S_IRUGO, show_index, NULL),
62 __ATTR_NULL
63};
64
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030065/*
66 * Active devices
67 */
68static struct video_device *video_device[VIDEO_NUM_DEVICES];
69static DEFINE_MUTEX(videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -030070static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030071
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030072struct video_device *video_device_alloc(void)
73{
Hans Verkuilbfa8a272008-08-23 07:48:38 -030074 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030075}
76EXPORT_SYMBOL(video_device_alloc);
77
Hans Verkuildc93a702008-12-19 21:28:27 -030078void video_device_release(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030079{
Hans Verkuildc93a702008-12-19 21:28:27 -030080 kfree(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030081}
82EXPORT_SYMBOL(video_device_release);
83
Hans Verkuildc93a702008-12-19 21:28:27 -030084void video_device_release_empty(struct video_device *vdev)
Hans Verkuilf9e86b52008-08-23 05:47:41 -030085{
86 /* Do nothing */
87 /* Only valid when the video_device struct is a static. */
88}
89EXPORT_SYMBOL(video_device_release_empty);
90
Hans Verkuildc93a702008-12-19 21:28:27 -030091static inline void video_get(struct video_device *vdev)
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030092{
Hans Verkuildc93a702008-12-19 21:28:27 -030093 get_device(&vdev->dev);
94}
95
96static inline void video_put(struct video_device *vdev)
97{
98 put_device(&vdev->dev);
99}
100
101/* Called when the last user of the video device exits. */
102static void v4l2_device_release(struct device *cd)
103{
104 struct video_device *vdev = to_video_device(cd);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300105
106 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300107 if (video_device[vdev->minor] != vdev) {
Hans Verkuil6ea9a182008-08-30 09:40:47 -0300108 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300109 /* should not happen */
110 WARN_ON(1);
Hans Verkuil6ea9a182008-08-30 09:40:47 -0300111 return;
112 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300113
114 /* Free up this device for reuse */
Hans Verkuildc93a702008-12-19 21:28:27 -0300115 video_device[vdev->minor] = NULL;
116
117 /* Delete the cdev on this minor as well */
118 cdev_del(vdev->cdev);
119 /* Just in case some driver tries to access this from
120 the release() callback. */
121 vdev->cdev = NULL;
122
123 /* Mark minor as free */
124 clear_bit(vdev->num, video_nums[vdev->vfl_type]);
125
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300126 mutex_unlock(&videodev_lock);
127
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300128 /* Release video_device and perform other
129 cleanups as needed. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300130 vdev->release(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300131}
132
133static struct class video_class = {
134 .name = VIDEO_NAME,
135 .dev_attrs = video_device_attrs,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300136};
137
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300138struct video_device *video_devdata(struct file *file)
139{
140 return video_device[iminor(file->f_path.dentry->d_inode)];
141}
142EXPORT_SYMBOL(video_devdata);
143
Hans Verkuildc93a702008-12-19 21:28:27 -0300144static ssize_t v4l2_read(struct file *filp, char __user *buf,
145 size_t sz, loff_t *off)
146{
147 struct video_device *vdev = video_devdata(filp);
148
149 if (!vdev->fops->read)
150 return -EINVAL;
151 if (video_is_unregistered(vdev))
152 return -EIO;
153 return vdev->fops->read(filp, buf, sz, off);
154}
155
156static ssize_t v4l2_write(struct file *filp, const char __user *buf,
157 size_t sz, loff_t *off)
158{
159 struct video_device *vdev = video_devdata(filp);
160
161 if (!vdev->fops->write)
162 return -EINVAL;
163 if (video_is_unregistered(vdev))
164 return -EIO;
165 return vdev->fops->write(filp, buf, sz, off);
166}
167
168static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
169{
170 struct video_device *vdev = video_devdata(filp);
171
172 if (!vdev->fops->poll || video_is_unregistered(vdev))
173 return DEFAULT_POLLMASK;
174 return vdev->fops->poll(filp, poll);
175}
176
177static int v4l2_ioctl(struct inode *inode, struct file *filp,
178 unsigned int cmd, unsigned long arg)
179{
180 struct video_device *vdev = video_devdata(filp);
181
182 if (!vdev->fops->ioctl)
183 return -ENOTTY;
184 /* Allow ioctl to continue even if the device was unregistered.
185 Things like dequeueing buffers might still be useful. */
Hans Verkuilbec43662008-12-30 06:58:20 -0300186 return vdev->fops->ioctl(filp, cmd, arg);
Hans Verkuildc93a702008-12-19 21:28:27 -0300187}
188
189static long v4l2_unlocked_ioctl(struct file *filp,
190 unsigned int cmd, unsigned long arg)
191{
192 struct video_device *vdev = video_devdata(filp);
193
194 if (!vdev->fops->unlocked_ioctl)
195 return -ENOTTY;
196 /* Allow ioctl to continue even if the device was unregistered.
197 Things like dequeueing buffers might still be useful. */
198 return vdev->fops->unlocked_ioctl(filp, cmd, arg);
199}
200
Daniel Glöcknerc01f1a52009-03-26 11:31:08 -0300201#ifdef CONFIG_MMU
202#define v4l2_get_unmapped_area NULL
203#else
204static unsigned long v4l2_get_unmapped_area(struct file *filp,
205 unsigned long addr, unsigned long len, unsigned long pgoff,
206 unsigned long flags)
207{
208 struct video_device *vdev = video_devdata(filp);
209
210 if (!vdev->fops->get_unmapped_area)
211 return -ENOSYS;
212 if (video_is_unregistered(vdev))
213 return -ENODEV;
214 return vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
215}
216#endif
217
Hans Verkuildc93a702008-12-19 21:28:27 -0300218static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
219{
220 struct video_device *vdev = video_devdata(filp);
221
222 if (!vdev->fops->mmap ||
223 video_is_unregistered(vdev))
224 return -ENODEV;
225 return vdev->fops->mmap(filp, vm);
226}
227
228/* Override for the open function */
229static int v4l2_open(struct inode *inode, struct file *filp)
230{
231 struct video_device *vdev;
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300232 int ret = 0;
Hans Verkuildc93a702008-12-19 21:28:27 -0300233
234 /* Check if the video device is available */
235 mutex_lock(&videodev_lock);
236 vdev = video_devdata(filp);
237 /* return ENODEV if the video device has been removed
238 already or if it is not registered anymore. */
239 if (vdev == NULL || video_is_unregistered(vdev)) {
240 mutex_unlock(&videodev_lock);
241 return -ENODEV;
242 }
243 /* and increase the device refcount */
244 video_get(vdev);
245 mutex_unlock(&videodev_lock);
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300246 if (vdev->fops->open)
247 ret = vdev->fops->open(filp);
248
Hans Verkuildc93a702008-12-19 21:28:27 -0300249 /* decrease the refcount in case of an error */
250 if (ret)
251 video_put(vdev);
252 return ret;
253}
254
255/* Override for the release function */
256static int v4l2_release(struct inode *inode, struct file *filp)
257{
258 struct video_device *vdev = video_devdata(filp);
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300259 int ret = 0;
260
261 if (vdev->fops->release)
262 vdev->fops->release(filp);
Hans Verkuildc93a702008-12-19 21:28:27 -0300263
264 /* decrease the refcount unconditionally since the release()
265 return value is ignored. */
266 video_put(vdev);
267 return ret;
268}
269
270static const struct file_operations v4l2_unlocked_fops = {
271 .owner = THIS_MODULE,
272 .read = v4l2_read,
273 .write = v4l2_write,
274 .open = v4l2_open,
Daniel Glöcknerc01f1a52009-03-26 11:31:08 -0300275 .get_unmapped_area = v4l2_get_unmapped_area,
Hans Verkuildc93a702008-12-19 21:28:27 -0300276 .mmap = v4l2_mmap,
277 .unlocked_ioctl = v4l2_unlocked_ioctl,
278#ifdef CONFIG_COMPAT
Hans Verkuil9bb7cde2008-12-30 06:42:40 -0300279 .compat_ioctl = v4l2_compat_ioctl32,
Hans Verkuildc93a702008-12-19 21:28:27 -0300280#endif
281 .release = v4l2_release,
282 .poll = v4l2_poll,
283 .llseek = no_llseek,
284};
285
286static const struct file_operations v4l2_fops = {
287 .owner = THIS_MODULE,
288 .read = v4l2_read,
289 .write = v4l2_write,
290 .open = v4l2_open,
Daniel Glöcknerc01f1a52009-03-26 11:31:08 -0300291 .get_unmapped_area = v4l2_get_unmapped_area,
Hans Verkuildc93a702008-12-19 21:28:27 -0300292 .mmap = v4l2_mmap,
293 .ioctl = v4l2_ioctl,
294#ifdef CONFIG_COMPAT
Hans Verkuil9bb7cde2008-12-30 06:42:40 -0300295 .compat_ioctl = v4l2_compat_ioctl32,
Hans Verkuildc93a702008-12-19 21:28:27 -0300296#endif
297 .release = v4l2_release,
298 .poll = v4l2_poll,
299 .llseek = no_llseek,
300};
301
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300302/**
303 * get_index - assign stream number based on parent device
Hans Verkuildc93a702008-12-19 21:28:27 -0300304 * @vdev: video_device to assign index number to, vdev->parent should be assigned
305 * @num: -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300306 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300307 * Note that when this is called the new device has not yet been registered
308 * in the video_device array.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300309 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300310 * Returns -ENFILE if num is already in use, a free index number if
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300311 * successful.
312 */
313static int get_index(struct video_device *vdev, int num)
314{
Hans Verkuil775a05d2009-02-14 11:31:01 -0300315 /* This can be static since this function is called with the global
316 videodev_lock held. */
317 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300318 int i;
319
Hans Verkuil775a05d2009-02-14 11:31:01 -0300320 if (num >= VIDEO_NUM_DEVICES) {
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300321 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
322 return -EINVAL;
323 }
324
Hans Verkuil775a05d2009-02-14 11:31:01 -0300325 /* Some drivers do not set the parent. In that case always return
326 num or 0. */
Hans Verkuil806e5b72008-12-19 09:10:56 -0300327 if (vdev->parent == NULL)
Hans Verkuil775a05d2009-02-14 11:31:01 -0300328 return num >= 0 ? num : 0;
329
330 bitmap_zero(used, VIDEO_NUM_DEVICES);
Hans Verkuil806e5b72008-12-19 09:10:56 -0300331
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300332 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
333 if (video_device[i] != NULL &&
Hans Verkuil5e85e732008-07-20 06:31:39 -0300334 video_device[i]->parent == vdev->parent) {
Hans Verkuil775a05d2009-02-14 11:31:01 -0300335 set_bit(video_device[i]->index, used);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300336 }
337 }
338
339 if (num >= 0) {
Hans Verkuil775a05d2009-02-14 11:31:01 -0300340 if (test_bit(num, used))
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300341 return -ENFILE;
342 return num;
343 }
344
Hans Verkuil775a05d2009-02-14 11:31:01 -0300345 i = find_first_zero_bit(used, VIDEO_NUM_DEVICES);
346 return i == VIDEO_NUM_DEVICES ? -ENFILE : i;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300347}
348
Hans Verkuildc93a702008-12-19 21:28:27 -0300349int video_register_device(struct video_device *vdev, int type, int nr)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300350{
Hans Verkuildc93a702008-12-19 21:28:27 -0300351 return video_register_device_index(vdev, type, nr, -1);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300352}
353EXPORT_SYMBOL(video_register_device);
354
355/**
Randy Dunlapedc91892008-07-28 15:39:38 -0300356 * video_register_device_index - register video4linux devices
Hans Verkuildc93a702008-12-19 21:28:27 -0300357 * @vdev: video device structure we want to register
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300358 * @type: type of device to register
359 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
360 * -1 == first free)
Randy Dunlapedc91892008-07-28 15:39:38 -0300361 * @index: stream number based on parent device;
362 * -1 if auto assign, requested number otherwise
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300363 *
364 * The registration code assigns minor numbers based on the type
365 * requested. -ENFILE is returned in all the device slots for this
366 * category are full. If not then the minor field is set and the
367 * driver initialize function is called (if non %NULL).
368 *
369 * Zero is returned on success.
370 *
371 * Valid types are
372 *
373 * %VFL_TYPE_GRABBER - A frame grabber
374 *
375 * %VFL_TYPE_VTX - A teletext device
376 *
377 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
378 *
379 * %VFL_TYPE_RADIO - A radio card
380 */
Hans Verkuildc93a702008-12-19 21:28:27 -0300381int video_register_device_index(struct video_device *vdev, int type, int nr,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300382 int index)
383{
384 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300385 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300386 int minor_offset = 0;
387 int minor_cnt = VIDEO_NUM_DEVICES;
388 const char *name_base;
Hans Verkuildc93a702008-12-19 21:28:27 -0300389 void *priv = video_get_drvdata(vdev);
390
391 /* A minor value of -1 marks this video device as never
392 having been registered */
Trent Piepho428c8d12009-03-03 18:51:52 -0300393 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300394
Hans Verkuild6e74972008-08-23 06:27:59 -0300395 /* the release callback MUST be present */
Trent Piepho428c8d12009-03-03 18:51:52 -0300396 WARN_ON(!vdev->release);
397 if (!vdev->release)
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300398 return -EINVAL;
399
Hans Verkuildc93a702008-12-19 21:28:27 -0300400 /* Part 1: check device type */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300401 switch (type) {
402 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300403 name_base = "video";
404 break;
405 case VFL_TYPE_VTX:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300406 name_base = "vtx";
407 break;
408 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300409 name_base = "vbi";
410 break;
411 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300412 name_base = "radio";
413 break;
414 default:
415 printk(KERN_ERR "%s called with unknown type: %d\n",
416 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300417 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300418 }
419
Hans Verkuildc93a702008-12-19 21:28:27 -0300420 vdev->vfl_type = type;
421 vdev->cdev = NULL;
Hans Verkuil00575962009-03-13 10:03:04 -0300422 if (vdev->v4l2_dev && vdev->v4l2_dev->dev)
Hans Verkuil9bea3512008-12-23 07:35:17 -0300423 vdev->parent = vdev->v4l2_dev->dev;
Hans Verkuildd896012008-10-04 08:36:54 -0300424
Hans Verkuildc93a702008-12-19 21:28:27 -0300425 /* Part 2: find a free minor, kernel number and device index. */
Hans Verkuildd896012008-10-04 08:36:54 -0300426#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
427 /* Keep the ranges for the first four types for historical
428 * reasons.
429 * Newer devices (not yet in place) should use the range
430 * of 128-191 and just pick the first free minor there
431 * (new style). */
432 switch (type) {
433 case VFL_TYPE_GRABBER:
434 minor_offset = 0;
435 minor_cnt = 64;
436 break;
437 case VFL_TYPE_RADIO:
438 minor_offset = 64;
439 minor_cnt = 64;
440 break;
441 case VFL_TYPE_VTX:
442 minor_offset = 192;
443 minor_cnt = 32;
444 break;
445 case VFL_TYPE_VBI:
446 minor_offset = 224;
447 minor_cnt = 32;
448 break;
449 default:
450 minor_offset = 128;
451 minor_cnt = 64;
452 break;
453 }
454#endif
455
Hans Verkuildc93a702008-12-19 21:28:27 -0300456 /* Pick a minor number */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300457 mutex_lock(&videodev_lock);
Hans Verkuildd896012008-10-04 08:36:54 -0300458 nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr);
459 if (nr == minor_cnt)
460 nr = find_first_zero_bit(video_nums[type], minor_cnt);
461 if (nr == minor_cnt) {
462 printk(KERN_ERR "could not get a free kernel number\n");
463 mutex_unlock(&videodev_lock);
464 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300465 }
Hans Verkuildd896012008-10-04 08:36:54 -0300466#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
467 /* 1-on-1 mapping of kernel number to minor number */
468 i = nr;
469#else
470 /* The kernel number and minor numbers are independent */
471 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
472 if (video_device[i] == NULL)
473 break;
474 if (i == VIDEO_NUM_DEVICES) {
475 mutex_unlock(&videodev_lock);
476 printk(KERN_ERR "could not get a free minor\n");
477 return -ENFILE;
478 }
479#endif
Hans Verkuildc93a702008-12-19 21:28:27 -0300480 vdev->minor = i + minor_offset;
481 vdev->num = nr;
Hans Verkuildd896012008-10-04 08:36:54 -0300482 set_bit(nr, video_nums[type]);
Hans Verkuildc93a702008-12-19 21:28:27 -0300483 /* Should not happen since we thought this minor was free */
484 WARN_ON(video_device[vdev->minor] != NULL);
485 ret = vdev->index = get_index(vdev, index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300486 mutex_unlock(&videodev_lock);
487
488 if (ret < 0) {
489 printk(KERN_ERR "%s: get_index failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300490 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300491 }
492
Hans Verkuildc93a702008-12-19 21:28:27 -0300493 /* Part 3: Initialize the character device */
494 vdev->cdev = cdev_alloc();
495 if (vdev->cdev == NULL) {
496 ret = -ENOMEM;
497 goto cleanup;
498 }
499 if (vdev->fops->unlocked_ioctl)
500 vdev->cdev->ops = &v4l2_unlocked_fops;
501 else
502 vdev->cdev->ops = &v4l2_fops;
503 vdev->cdev->owner = vdev->fops->owner;
504 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300505 if (ret < 0) {
506 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300507 kfree(vdev->cdev);
508 vdev->cdev = NULL;
509 goto cleanup;
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300510 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300511
512 /* Part 4: register the device with sysfs */
513 memset(&vdev->dev, 0, sizeof(vdev->dev));
Hans Verkuil9d95af92008-08-24 11:18:47 -0300514 /* The memset above cleared the device's drvdata, so
515 put back the copy we made earlier. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300516 video_set_drvdata(vdev, priv);
517 vdev->dev.class = &video_class;
518 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
519 if (vdev->parent)
520 vdev->dev.parent = vdev->parent;
521 dev_set_name(&vdev->dev, "%s%d", name_base, nr);
522 ret = device_register(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300523 if (ret < 0) {
524 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300525 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300526 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300527 /* Register the release callback that will be called when the last
528 reference to the device goes away. */
529 vdev->dev.release = v4l2_device_release;
530
531 /* Part 5: Activate this minor. The char device can now be used. */
532 mutex_lock(&videodev_lock);
533 video_device[vdev->minor] = vdev;
534 mutex_unlock(&videodev_lock);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300535 return 0;
536
Hans Verkuildc93a702008-12-19 21:28:27 -0300537cleanup:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300538 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300539 if (vdev->cdev)
540 cdev_del(vdev->cdev);
541 clear_bit(vdev->num, video_nums[type]);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300542 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300543 /* Mark this video device as never having been registered. */
544 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300545 return ret;
546}
547EXPORT_SYMBOL(video_register_device_index);
548
549/**
550 * video_unregister_device - unregister a video4linux device
Hans Verkuildc93a702008-12-19 21:28:27 -0300551 * @vdev: the device to unregister
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300552 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300553 * This unregisters the passed device. Future open calls will
554 * be met with errors.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300555 */
Hans Verkuildc93a702008-12-19 21:28:27 -0300556void video_unregister_device(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300557{
Hans Verkuildc93a702008-12-19 21:28:27 -0300558 /* Check if vdev was ever registered at all */
559 if (!vdev || vdev->minor < 0)
560 return;
561
562 mutex_lock(&videodev_lock);
563 set_bit(V4L2_FL_UNREGISTERED, &vdev->flags);
564 mutex_unlock(&videodev_lock);
565 device_unregister(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300566}
567EXPORT_SYMBOL(video_unregister_device);
568
569/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300570 * Initialise video for linux
571 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300572static int __init videodev_init(void)
573{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300574 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300575 int ret;
576
577 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300578 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
579 if (ret < 0) {
580 printk(KERN_WARNING "videodev: unable to get major %d\n",
581 VIDEO_MAJOR);
582 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300583 }
584
585 ret = class_register(&video_class);
586 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300587 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300588 printk(KERN_WARNING "video_dev: class_register failed\n");
589 return -EIO;
590 }
591
592 return 0;
593}
594
595static void __exit videodev_exit(void)
596{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300597 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
598
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300599 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300600 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300601}
602
603module_init(videodev_init)
604module_exit(videodev_exit)
605
606MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
607MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
608MODULE_LICENSE("GPL");
Scott James Remnantcbb72b02009-03-02 15:40:57 -0300609MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300610
611
612/*
613 * Local variables:
614 * c-basic-offset: 8
615 * End:
616 */