V4L/DVB (9973): v4l2-dev: use the release callback from device instead of cdev
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / v4l2-dev.c
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 *
12 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (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
41 static ssize_t show_index(struct device *cd,
42 struct device_attribute *attr, char *buf)
43 {
44 struct video_device *vdev = to_video_device(cd);
45
46 return sprintf(buf, "%i\n", vdev->index);
47 }
48
49 static ssize_t show_name(struct device *cd,
50 struct device_attribute *attr, char *buf)
51 {
52 struct video_device *vdev = to_video_device(cd);
53
54 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
55 }
56
57 static 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
63 /*
64 * Active devices
65 */
66 static struct video_device *video_device[VIDEO_NUM_DEVICES];
67 static DEFINE_MUTEX(videodev_lock);
68 static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
69
70 struct video_device *video_device_alloc(void)
71 {
72 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
73 }
74 EXPORT_SYMBOL(video_device_alloc);
75
76 void video_device_release(struct video_device *vdev)
77 {
78 kfree(vdev);
79 }
80 EXPORT_SYMBOL(video_device_release);
81
82 void video_device_release_empty(struct video_device *vdev)
83 {
84 /* Do nothing */
85 /* Only valid when the video_device struct is a static. */
86 }
87 EXPORT_SYMBOL(video_device_release_empty);
88
89 static inline void video_get(struct video_device *vdev)
90 {
91 get_device(&vdev->dev);
92 }
93
94 static 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. */
100 static void v4l2_device_release(struct device *cd)
101 {
102 struct video_device *vdev = to_video_device(cd);
103
104 mutex_lock(&videodev_lock);
105 if (video_device[vdev->minor] != vdev) {
106 mutex_unlock(&videodev_lock);
107 /* should not happen */
108 WARN_ON(1);
109 return;
110 }
111
112 /* Free up this device for reuse */
113 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
124 mutex_unlock(&videodev_lock);
125
126 /* Release video_device and perform other
127 cleanups as needed. */
128 vdev->release(vdev);
129 }
130
131 static struct class video_class = {
132 .name = VIDEO_NAME,
133 .dev_attrs = video_device_attrs,
134 };
135
136 struct video_device *video_devdata(struct file *file)
137 {
138 return video_device[iminor(file->f_path.dentry->d_inode)];
139 }
140 EXPORT_SYMBOL(video_devdata);
141
142 static 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
154 static 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
166 static 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
175 static 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
187 static 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
200 static 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
213 static 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 */
224 static 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 */
249 static 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
260 static 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
275 static 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
290 /**
291 * get_index - assign stream number based on parent device
292 * @vdev: video_device to assign index number to, vdev->parent should be assigned
293 * @num: -1 if auto assign, requested number otherwise
294 *
295 * Note that when this is called the new device has not yet been registered
296 * in the video_device array.
297 *
298 * Returns -ENFILE if num is already in use, a free index number if
299 * successful.
300 */
301 static 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
315 /* Some drivers do not set the parent. In that case always return 0. */
316 if (vdev->parent == NULL)
317 return 0;
318
319 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
320 if (video_device[i] != NULL &&
321 video_device[i]->parent == vdev->parent) {
322 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
336 int video_register_device(struct video_device *vdev, int type, int nr)
337 {
338 return video_register_device_index(vdev, type, nr, -1);
339 }
340 EXPORT_SYMBOL(video_register_device);
341
342 /**
343 * video_register_device_index - register video4linux devices
344 * @vdev: video device structure we want to register
345 * @type: type of device to register
346 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
347 * -1 == first free)
348 * @index: stream number based on parent device;
349 * -1 if auto assign, requested number otherwise
350 *
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 */
368 int video_register_device_index(struct video_device *vdev, int type, int nr,
369 int index)
370 {
371 int i = 0;
372 int ret;
373 int minor_offset = 0;
374 int minor_cnt = VIDEO_NUM_DEVICES;
375 const char *name_base;
376 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;
382
383 /* the release callback MUST be present */
384 WARN_ON(!vdev || !vdev->release);
385 if (!vdev || !vdev->release)
386 return -EINVAL;
387
388 /* Part 1: check device type */
389 switch (type) {
390 case VFL_TYPE_GRABBER:
391 name_base = "video";
392 break;
393 case VFL_TYPE_VTX:
394 name_base = "vtx";
395 break;
396 case VFL_TYPE_VBI:
397 name_base = "vbi";
398 break;
399 case VFL_TYPE_RADIO:
400 name_base = "radio";
401 break;
402 default:
403 printk(KERN_ERR "%s called with unknown type: %d\n",
404 __func__, type);
405 return -EINVAL;
406 }
407
408 vdev->vfl_type = type;
409 vdev->cdev = NULL;
410
411 /* Part 2: find a free minor, kernel number and device index. */
412 #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
442 /* Pick a minor number */
443 mutex_lock(&videodev_lock);
444 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;
451 }
452 #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
466 vdev->minor = i + minor_offset;
467 vdev->num = nr;
468 set_bit(nr, video_nums[type]);
469 /* 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);
472 mutex_unlock(&videodev_lock);
473
474 if (ret < 0) {
475 printk(KERN_ERR "%s: get_index failed\n", __func__);
476 goto cleanup;
477 }
478
479 /* 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);
491 if (ret < 0) {
492 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
493 kfree(vdev->cdev);
494 vdev->cdev = NULL;
495 goto cleanup;
496 }
497
498 /* Part 4: register the device with sysfs */
499 memset(&vdev->dev, 0, sizeof(vdev->dev));
500 /* The memset above cleared the device's drvdata, so
501 put back the copy we made earlier. */
502 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);
509 if (ret < 0) {
510 printk(KERN_ERR "%s: device_register failed\n", __func__);
511 goto cleanup;
512 }
513 /* 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);
521 return 0;
522
523 cleanup:
524 mutex_lock(&videodev_lock);
525 if (vdev->cdev)
526 cdev_del(vdev->cdev);
527 clear_bit(vdev->num, video_nums[type]);
528 mutex_unlock(&videodev_lock);
529 /* Mark this video device as never having been registered. */
530 vdev->minor = -1;
531 return ret;
532 }
533 EXPORT_SYMBOL(video_register_device_index);
534
535 /**
536 * video_unregister_device - unregister a video4linux device
537 * @vdev: the device to unregister
538 *
539 * This unregisters the passed device. Future open calls will
540 * be met with errors.
541 */
542 void video_unregister_device(struct video_device *vdev)
543 {
544 /* 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);
552 }
553 EXPORT_SYMBOL(video_unregister_device);
554
555 /*
556 * Initialise video for linux
557 */
558 static int __init videodev_init(void)
559 {
560 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
561 int ret;
562
563 printk(KERN_INFO "Linux video capture interface: v2.00\n");
564 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;
569 }
570
571 ret = class_register(&video_class);
572 if (ret < 0) {
573 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
574 printk(KERN_WARNING "video_dev: class_register failed\n");
575 return -EIO;
576 }
577
578 return 0;
579 }
580
581 static void __exit videodev_exit(void)
582 {
583 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
584
585 class_unregister(&video_class);
586 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
587 }
588
589 module_init(videodev_init)
590 module_exit(videodev_exit)
591
592 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
593 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
594 MODULE_LICENSE("GPL");
595
596
597 /*
598 * Local variables:
599 * c-basic-offset: 8
600 * End:
601 */