Commit | Line | Data |
---|---|---|
27a5e6d3 HV |
1 | /* |
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 | * | |
d9b01449 | 12 | * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1) |
27a5e6d3 HV |
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> | |
9bea3514 | 33 | #include <media/v4l2-device.h> |
bec43661 | 34 | #include <media/v4l2-ioctl.h> |
27a5e6d3 HV |
35 | |
36 | #define VIDEO_NUM_DEVICES 256 | |
37 | #define VIDEO_NAME "video4linux" | |
38 | ||
39 | /* | |
40 | * sysfs stuff | |
41 | */ | |
42 | ||
43 | static ssize_t show_index(struct device *cd, | |
44 | struct device_attribute *attr, char *buf) | |
45 | { | |
dc93a70c | 46 | struct video_device *vdev = to_video_device(cd); |
bfa8a273 | 47 | |
dc93a70c | 48 | return sprintf(buf, "%i\n", vdev->index); |
27a5e6d3 HV |
49 | } |
50 | ||
51 | static ssize_t show_name(struct device *cd, | |
52 | struct device_attribute *attr, char *buf) | |
53 | { | |
dc93a70c | 54 | struct video_device *vdev = to_video_device(cd); |
bfa8a273 | 55 | |
dc93a70c | 56 | return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name); |
27a5e6d3 HV |
57 | } |
58 | ||
59 | static 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 | ||
7f8ecfab HV |
65 | /* |
66 | * Active devices | |
67 | */ | |
68 | static struct video_device *video_device[VIDEO_NUM_DEVICES]; | |
69 | static DEFINE_MUTEX(videodev_lock); | |
dd89601d | 70 | static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES); |
7f8ecfab | 71 | |
27a5e6d3 HV |
72 | struct video_device *video_device_alloc(void) |
73 | { | |
bfa8a273 | 74 | return kzalloc(sizeof(struct video_device), GFP_KERNEL); |
27a5e6d3 HV |
75 | } |
76 | EXPORT_SYMBOL(video_device_alloc); | |
77 | ||
dc93a70c | 78 | void video_device_release(struct video_device *vdev) |
27a5e6d3 | 79 | { |
dc93a70c | 80 | kfree(vdev); |
27a5e6d3 HV |
81 | } |
82 | EXPORT_SYMBOL(video_device_release); | |
83 | ||
dc93a70c | 84 | void video_device_release_empty(struct video_device *vdev) |
f9e86b5e HV |
85 | { |
86 | /* Do nothing */ | |
87 | /* Only valid when the video_device struct is a static. */ | |
88 | } | |
89 | EXPORT_SYMBOL(video_device_release_empty); | |
90 | ||
dc93a70c | 91 | static inline void video_get(struct video_device *vdev) |
7f8ecfab | 92 | { |
dc93a70c HV |
93 | get_device(&vdev->dev); |
94 | } | |
95 | ||
96 | static 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. */ | |
102 | static void v4l2_device_release(struct device *cd) | |
103 | { | |
104 | struct video_device *vdev = to_video_device(cd); | |
7f8ecfab HV |
105 | |
106 | mutex_lock(&videodev_lock); | |
dc93a70c | 107 | if (video_device[vdev->minor] != vdev) { |
6ea9a182 | 108 | mutex_unlock(&videodev_lock); |
dc93a70c HV |
109 | /* should not happen */ |
110 | WARN_ON(1); | |
6ea9a182 HV |
111 | return; |
112 | } | |
7f8ecfab HV |
113 | |
114 | /* Free up this device for reuse */ | |
dc93a70c | 115 | video_device[vdev->minor] = NULL; |
7f8ecfab | 116 | |
dc93a70c HV |
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; | |
7f8ecfab | 122 | |
dc93a70c HV |
123 | /* Mark minor as free */ |
124 | clear_bit(vdev->num, video_nums[vdev->vfl_type]); | |
7f8ecfab | 125 | |
dc93a70c | 126 | mutex_unlock(&videodev_lock); |
27a5e6d3 | 127 | |
dc93a70c HV |
128 | /* Release video_device and perform other |
129 | cleanups as needed. */ | |
130 | vdev->release(vdev); | |
27a5e6d3 HV |
131 | } |
132 | ||
133 | static struct class video_class = { | |
134 | .name = VIDEO_NAME, | |
135 | .dev_attrs = video_device_attrs, | |
27a5e6d3 HV |
136 | }; |
137 | ||
27a5e6d3 HV |
138 | struct video_device *video_devdata(struct file *file) |
139 | { | |
140 | return video_device[iminor(file->f_path.dentry->d_inode)]; | |
141 | } | |
142 | EXPORT_SYMBOL(video_devdata); | |
143 | ||
dc93a70c HV |
144 | static 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 | ||
156 | static 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 | ||
168 | static 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 | ||
177 | static 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. */ | |
bec43661 | 186 | return vdev->fops->ioctl(filp, cmd, arg); |
dc93a70c HV |
187 | } |
188 | ||
189 | static 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 | ||
c01f1a5a DG |
201 | #ifdef CONFIG_MMU |
202 | #define v4l2_get_unmapped_area NULL | |
203 | #else | |
204 | static 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 | ||
dc93a70c HV |
218 | static 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 */ | |
229 | static int v4l2_open(struct inode *inode, struct file *filp) | |
230 | { | |
231 | struct video_device *vdev; | |
232 | int ret; | |
233 | ||
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); | |
bec43661 | 246 | ret = vdev->fops->open(filp); |
dc93a70c HV |
247 | /* decrease the refcount in case of an error */ |
248 | if (ret) | |
249 | video_put(vdev); | |
250 | return ret; | |
251 | } | |
252 | ||
253 | /* Override for the release function */ | |
254 | static int v4l2_release(struct inode *inode, struct file *filp) | |
255 | { | |
256 | struct video_device *vdev = video_devdata(filp); | |
bec43661 | 257 | int ret = vdev->fops->release(filp); |
dc93a70c HV |
258 | |
259 | /* decrease the refcount unconditionally since the release() | |
260 | return value is ignored. */ | |
261 | video_put(vdev); | |
262 | return ret; | |
263 | } | |
264 | ||
265 | static const struct file_operations v4l2_unlocked_fops = { | |
266 | .owner = THIS_MODULE, | |
267 | .read = v4l2_read, | |
268 | .write = v4l2_write, | |
269 | .open = v4l2_open, | |
c01f1a5a | 270 | .get_unmapped_area = v4l2_get_unmapped_area, |
dc93a70c HV |
271 | .mmap = v4l2_mmap, |
272 | .unlocked_ioctl = v4l2_unlocked_ioctl, | |
273 | #ifdef CONFIG_COMPAT | |
9bb7cde7 | 274 | .compat_ioctl = v4l2_compat_ioctl32, |
dc93a70c HV |
275 | #endif |
276 | .release = v4l2_release, | |
277 | .poll = v4l2_poll, | |
278 | .llseek = no_llseek, | |
279 | }; | |
280 | ||
281 | static const struct file_operations v4l2_fops = { | |
282 | .owner = THIS_MODULE, | |
283 | .read = v4l2_read, | |
284 | .write = v4l2_write, | |
285 | .open = v4l2_open, | |
c01f1a5a | 286 | .get_unmapped_area = v4l2_get_unmapped_area, |
dc93a70c HV |
287 | .mmap = v4l2_mmap, |
288 | .ioctl = v4l2_ioctl, | |
289 | #ifdef CONFIG_COMPAT | |
9bb7cde7 | 290 | .compat_ioctl = v4l2_compat_ioctl32, |
dc93a70c HV |
291 | #endif |
292 | .release = v4l2_release, | |
293 | .poll = v4l2_poll, | |
294 | .llseek = no_llseek, | |
295 | }; | |
296 | ||
27a5e6d3 HV |
297 | /** |
298 | * get_index - assign stream number based on parent device | |
dc93a70c HV |
299 | * @vdev: video_device to assign index number to, vdev->parent should be assigned |
300 | * @num: -1 if auto assign, requested number otherwise | |
27a5e6d3 | 301 | * |
dc93a70c HV |
302 | * Note that when this is called the new device has not yet been registered |
303 | * in the video_device array. | |
27a5e6d3 | 304 | * |
dc93a70c | 305 | * Returns -ENFILE if num is already in use, a free index number if |
27a5e6d3 HV |
306 | * successful. |
307 | */ | |
308 | static int get_index(struct video_device *vdev, int num) | |
309 | { | |
775a05dd HV |
310 | /* This can be static since this function is called with the global |
311 | videodev_lock held. */ | |
312 | static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES); | |
27a5e6d3 HV |
313 | int i; |
314 | ||
775a05dd | 315 | if (num >= VIDEO_NUM_DEVICES) { |
27a5e6d3 HV |
316 | printk(KERN_ERR "videodev: %s num is too large\n", __func__); |
317 | return -EINVAL; | |
318 | } | |
319 | ||
775a05dd HV |
320 | /* Some drivers do not set the parent. In that case always return |
321 | num or 0. */ | |
806e5b7c | 322 | if (vdev->parent == NULL) |
775a05dd HV |
323 | return num >= 0 ? num : 0; |
324 | ||
325 | bitmap_zero(used, VIDEO_NUM_DEVICES); | |
806e5b7c | 326 | |
27a5e6d3 HV |
327 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) { |
328 | if (video_device[i] != NULL && | |
5e85e732 | 329 | video_device[i]->parent == vdev->parent) { |
775a05dd | 330 | set_bit(video_device[i]->index, used); |
27a5e6d3 HV |
331 | } |
332 | } | |
333 | ||
334 | if (num >= 0) { | |
775a05dd | 335 | if (test_bit(num, used)) |
27a5e6d3 HV |
336 | return -ENFILE; |
337 | return num; | |
338 | } | |
339 | ||
775a05dd HV |
340 | i = find_first_zero_bit(used, VIDEO_NUM_DEVICES); |
341 | return i == VIDEO_NUM_DEVICES ? -ENFILE : i; | |
27a5e6d3 HV |
342 | } |
343 | ||
dc93a70c | 344 | int video_register_device(struct video_device *vdev, int type, int nr) |
27a5e6d3 | 345 | { |
dc93a70c | 346 | return video_register_device_index(vdev, type, nr, -1); |
27a5e6d3 HV |
347 | } |
348 | EXPORT_SYMBOL(video_register_device); | |
349 | ||
350 | /** | |
edc9189c | 351 | * video_register_device_index - register video4linux devices |
dc93a70c | 352 | * @vdev: video device structure we want to register |
27a5e6d3 HV |
353 | * @type: type of device to register |
354 | * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ... | |
355 | * -1 == first free) | |
edc9189c RD |
356 | * @index: stream number based on parent device; |
357 | * -1 if auto assign, requested number otherwise | |
27a5e6d3 HV |
358 | * |
359 | * The registration code assigns minor numbers based on the type | |
360 | * requested. -ENFILE is returned in all the device slots for this | |
361 | * category are full. If not then the minor field is set and the | |
362 | * driver initialize function is called (if non %NULL). | |
363 | * | |
364 | * Zero is returned on success. | |
365 | * | |
366 | * Valid types are | |
367 | * | |
368 | * %VFL_TYPE_GRABBER - A frame grabber | |
369 | * | |
370 | * %VFL_TYPE_VTX - A teletext device | |
371 | * | |
372 | * %VFL_TYPE_VBI - Vertical blank data (undecoded) | |
373 | * | |
374 | * %VFL_TYPE_RADIO - A radio card | |
375 | */ | |
dc93a70c | 376 | int video_register_device_index(struct video_device *vdev, int type, int nr, |
27a5e6d3 HV |
377 | int index) |
378 | { | |
379 | int i = 0; | |
27a5e6d3 | 380 | int ret; |
dd89601d HV |
381 | int minor_offset = 0; |
382 | int minor_cnt = VIDEO_NUM_DEVICES; | |
383 | const char *name_base; | |
dc93a70c | 384 | void *priv = video_get_drvdata(vdev); |
27a5e6d3 | 385 | |
dc93a70c HV |
386 | /* A minor value of -1 marks this video device as never |
387 | having been registered */ | |
428c8d19 | 388 | vdev->minor = -1; |
ee7aa9f8 | 389 | |
dc93a70c | 390 | /* the release callback MUST be present */ |
428c8d19 TP |
391 | WARN_ON(!vdev->release); |
392 | if (!vdev->release) | |
f3b9f50e HK |
393 | return -EINVAL; |
394 | ||
dc93a70c | 395 | /* Part 1: check device type */ |
27a5e6d3 HV |
396 | switch (type) { |
397 | case VFL_TYPE_GRABBER: | |
27a5e6d3 HV |
398 | name_base = "video"; |
399 | break; | |
400 | case VFL_TYPE_VTX: | |
27a5e6d3 HV |
401 | name_base = "vtx"; |
402 | break; | |
403 | case VFL_TYPE_VBI: | |
27a5e6d3 HV |
404 | name_base = "vbi"; |
405 | break; | |
406 | case VFL_TYPE_RADIO: | |
27a5e6d3 HV |
407 | name_base = "radio"; |
408 | break; | |
409 | default: | |
410 | printk(KERN_ERR "%s called with unknown type: %d\n", | |
411 | __func__, type); | |
46f2c21c | 412 | return -EINVAL; |
27a5e6d3 HV |
413 | } |
414 | ||
dc93a70c HV |
415 | vdev->vfl_type = type; |
416 | vdev->cdev = NULL; | |
00575961 | 417 | if (vdev->v4l2_dev && vdev->v4l2_dev->dev) |
9bea3514 | 418 | vdev->parent = vdev->v4l2_dev->dev; |
dd89601d | 419 | |
dc93a70c | 420 | /* Part 2: find a free minor, kernel number and device index. */ |
dd89601d HV |
421 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
422 | /* Keep the ranges for the first four types for historical | |
423 | * reasons. | |
424 | * Newer devices (not yet in place) should use the range | |
425 | * of 128-191 and just pick the first free minor there | |
426 | * (new style). */ | |
427 | switch (type) { | |
428 | case VFL_TYPE_GRABBER: | |
429 | minor_offset = 0; | |
430 | minor_cnt = 64; | |
431 | break; | |
432 | case VFL_TYPE_RADIO: | |
433 | minor_offset = 64; | |
434 | minor_cnt = 64; | |
435 | break; | |
436 | case VFL_TYPE_VTX: | |
437 | minor_offset = 192; | |
438 | minor_cnt = 32; | |
439 | break; | |
440 | case VFL_TYPE_VBI: | |
441 | minor_offset = 224; | |
442 | minor_cnt = 32; | |
443 | break; | |
444 | default: | |
445 | minor_offset = 128; | |
446 | minor_cnt = 64; | |
447 | break; | |
448 | } | |
449 | #endif | |
450 | ||
dc93a70c | 451 | /* Pick a minor number */ |
27a5e6d3 | 452 | mutex_lock(&videodev_lock); |
dd89601d HV |
453 | nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr); |
454 | if (nr == minor_cnt) | |
455 | nr = find_first_zero_bit(video_nums[type], minor_cnt); | |
456 | if (nr == minor_cnt) { | |
457 | printk(KERN_ERR "could not get a free kernel number\n"); | |
458 | mutex_unlock(&videodev_lock); | |
459 | return -ENFILE; | |
27a5e6d3 | 460 | } |
dd89601d HV |
461 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
462 | /* 1-on-1 mapping of kernel number to minor number */ | |
463 | i = nr; | |
464 | #else | |
465 | /* The kernel number and minor numbers are independent */ | |
466 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) | |
467 | if (video_device[i] == NULL) | |
468 | break; | |
469 | if (i == VIDEO_NUM_DEVICES) { | |
470 | mutex_unlock(&videodev_lock); | |
471 | printk(KERN_ERR "could not get a free minor\n"); | |
472 | return -ENFILE; | |
473 | } | |
474 | #endif | |
dc93a70c HV |
475 | vdev->minor = i + minor_offset; |
476 | vdev->num = nr; | |
dd89601d | 477 | set_bit(nr, video_nums[type]); |
dc93a70c HV |
478 | /* Should not happen since we thought this minor was free */ |
479 | WARN_ON(video_device[vdev->minor] != NULL); | |
480 | ret = vdev->index = get_index(vdev, index); | |
27a5e6d3 HV |
481 | mutex_unlock(&videodev_lock); |
482 | ||
483 | if (ret < 0) { | |
484 | printk(KERN_ERR "%s: get_index failed\n", __func__); | |
dc93a70c | 485 | goto cleanup; |
27a5e6d3 HV |
486 | } |
487 | ||
dc93a70c HV |
488 | /* Part 3: Initialize the character device */ |
489 | vdev->cdev = cdev_alloc(); | |
490 | if (vdev->cdev == NULL) { | |
491 | ret = -ENOMEM; | |
492 | goto cleanup; | |
493 | } | |
494 | if (vdev->fops->unlocked_ioctl) | |
495 | vdev->cdev->ops = &v4l2_unlocked_fops; | |
496 | else | |
497 | vdev->cdev->ops = &v4l2_fops; | |
498 | vdev->cdev->owner = vdev->fops->owner; | |
499 | ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1); | |
7f8ecfab HV |
500 | if (ret < 0) { |
501 | printk(KERN_ERR "%s: cdev_add failed\n", __func__); | |
dc93a70c HV |
502 | kfree(vdev->cdev); |
503 | vdev->cdev = NULL; | |
504 | goto cleanup; | |
7f8ecfab | 505 | } |
dc93a70c HV |
506 | |
507 | /* Part 4: register the device with sysfs */ | |
508 | memset(&vdev->dev, 0, sizeof(vdev->dev)); | |
9d95af9d HV |
509 | /* The memset above cleared the device's drvdata, so |
510 | put back the copy we made earlier. */ | |
dc93a70c HV |
511 | video_set_drvdata(vdev, priv); |
512 | vdev->dev.class = &video_class; | |
513 | vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor); | |
514 | if (vdev->parent) | |
515 | vdev->dev.parent = vdev->parent; | |
516 | dev_set_name(&vdev->dev, "%s%d", name_base, nr); | |
517 | ret = device_register(&vdev->dev); | |
27a5e6d3 HV |
518 | if (ret < 0) { |
519 | printk(KERN_ERR "%s: device_register failed\n", __func__); | |
dc93a70c | 520 | goto cleanup; |
27a5e6d3 | 521 | } |
dc93a70c HV |
522 | /* Register the release callback that will be called when the last |
523 | reference to the device goes away. */ | |
524 | vdev->dev.release = v4l2_device_release; | |
27a5e6d3 | 525 | |
dc93a70c HV |
526 | /* Part 5: Activate this minor. The char device can now be used. */ |
527 | mutex_lock(&videodev_lock); | |
528 | video_device[vdev->minor] = vdev; | |
529 | mutex_unlock(&videodev_lock); | |
530 | return 0; | |
7f8ecfab | 531 | |
dc93a70c | 532 | cleanup: |
27a5e6d3 | 533 | mutex_lock(&videodev_lock); |
dc93a70c HV |
534 | if (vdev->cdev) |
535 | cdev_del(vdev->cdev); | |
536 | clear_bit(vdev->num, video_nums[type]); | |
27a5e6d3 | 537 | mutex_unlock(&videodev_lock); |
dc93a70c HV |
538 | /* Mark this video device as never having been registered. */ |
539 | vdev->minor = -1; | |
27a5e6d3 HV |
540 | return ret; |
541 | } | |
542 | EXPORT_SYMBOL(video_register_device_index); | |
543 | ||
544 | /** | |
545 | * video_unregister_device - unregister a video4linux device | |
dc93a70c | 546 | * @vdev: the device to unregister |
27a5e6d3 | 547 | * |
dc93a70c HV |
548 | * This unregisters the passed device. Future open calls will |
549 | * be met with errors. | |
27a5e6d3 | 550 | */ |
dc93a70c | 551 | void video_unregister_device(struct video_device *vdev) |
27a5e6d3 | 552 | { |
dc93a70c HV |
553 | /* Check if vdev was ever registered at all */ |
554 | if (!vdev || vdev->minor < 0) | |
555 | return; | |
556 | ||
557 | mutex_lock(&videodev_lock); | |
558 | set_bit(V4L2_FL_UNREGISTERED, &vdev->flags); | |
559 | mutex_unlock(&videodev_lock); | |
560 | device_unregister(&vdev->dev); | |
27a5e6d3 HV |
561 | } |
562 | EXPORT_SYMBOL(video_unregister_device); | |
563 | ||
27a5e6d3 HV |
564 | /* |
565 | * Initialise video for linux | |
566 | */ | |
27a5e6d3 HV |
567 | static int __init videodev_init(void) |
568 | { | |
7f8ecfab | 569 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
27a5e6d3 HV |
570 | int ret; |
571 | ||
572 | printk(KERN_INFO "Linux video capture interface: v2.00\n"); | |
7f8ecfab HV |
573 | ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME); |
574 | if (ret < 0) { | |
575 | printk(KERN_WARNING "videodev: unable to get major %d\n", | |
576 | VIDEO_MAJOR); | |
577 | return ret; | |
27a5e6d3 HV |
578 | } |
579 | ||
580 | ret = class_register(&video_class); | |
581 | if (ret < 0) { | |
7f8ecfab | 582 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
27a5e6d3 HV |
583 | printk(KERN_WARNING "video_dev: class_register failed\n"); |
584 | return -EIO; | |
585 | } | |
586 | ||
587 | return 0; | |
588 | } | |
589 | ||
590 | static void __exit videodev_exit(void) | |
591 | { | |
7f8ecfab HV |
592 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
593 | ||
27a5e6d3 | 594 | class_unregister(&video_class); |
7f8ecfab | 595 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
27a5e6d3 HV |
596 | } |
597 | ||
598 | module_init(videodev_init) | |
599 | module_exit(videodev_exit) | |
600 | ||
601 | MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>"); | |
602 | MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2"); | |
603 | MODULE_LICENSE("GPL"); | |
cbb72b0f | 604 | MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR); |
27a5e6d3 HV |
605 | |
606 | ||
607 | /* | |
608 | * Local variables: | |
609 | * c-basic-offset: 8 | |
610 | * End: | |
611 | */ |