V4L/DVB (8750): V4L: check inval in video_register_device_index()
[safe/jmp/linux-2.6] / 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@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
41 static ssize_t show_index(struct device *cd,
42                          struct device_attribute *attr, char *buf)
43 {
44         struct video_device *vfd = container_of(cd, struct video_device, dev);
45         return sprintf(buf, "%i\n", vfd->index);
46 }
47
48 static ssize_t show_name(struct device *cd,
49                          struct device_attribute *attr, char *buf)
50 {
51         struct video_device *vfd = container_of(cd, struct video_device, dev);
52         return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
53 }
54
55 static struct device_attribute video_device_attrs[] = {
56         __ATTR(name, S_IRUGO, show_name, NULL),
57         __ATTR(index, S_IRUGO, show_index, NULL),
58         __ATTR_NULL
59 };
60
61 struct video_device *video_device_alloc(void)
62 {
63         struct video_device *vfd;
64
65         vfd = kzalloc(sizeof(*vfd), GFP_KERNEL);
66         return vfd;
67 }
68 EXPORT_SYMBOL(video_device_alloc);
69
70 void video_device_release(struct video_device *vfd)
71 {
72         kfree(vfd);
73 }
74 EXPORT_SYMBOL(video_device_release);
75
76 static void video_release(struct device *cd)
77 {
78         struct video_device *vfd = container_of(cd, struct video_device, dev);
79
80 #if 1
81         /* needed until all drivers are fixed */
82         if (!vfd->release)
83                 return;
84 #endif
85         vfd->release(vfd);
86 }
87
88 static struct class video_class = {
89         .name = VIDEO_NAME,
90         .dev_attrs = video_device_attrs,
91         .dev_release = video_release,
92 };
93
94 /*
95  *      Active devices
96  */
97
98 static struct video_device *video_device[VIDEO_NUM_DEVICES];
99 static DEFINE_MUTEX(videodev_lock);
100
101 struct video_device *video_devdata(struct file *file)
102 {
103         return video_device[iminor(file->f_path.dentry->d_inode)];
104 }
105 EXPORT_SYMBOL(video_devdata);
106
107 /*
108  *      Open a video device - FIXME: Obsoleted
109  */
110 static int video_open(struct inode *inode, struct file *file)
111 {
112         unsigned int minor = iminor(inode);
113         int err = 0;
114         struct video_device *vfl;
115         const struct file_operations *old_fops;
116
117         if (minor >= VIDEO_NUM_DEVICES)
118                 return -ENODEV;
119         mutex_lock(&videodev_lock);
120         vfl = video_device[minor];
121         if (vfl == NULL) {
122                 mutex_unlock(&videodev_lock);
123                 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
124                 mutex_lock(&videodev_lock);
125                 vfl = video_device[minor];
126                 if (vfl == NULL) {
127                         mutex_unlock(&videodev_lock);
128                         return -ENODEV;
129                 }
130         }
131         old_fops = file->f_op;
132         file->f_op = fops_get(vfl->fops);
133         if (file->f_op->open)
134                 err = file->f_op->open(inode, file);
135         if (err) {
136                 fops_put(file->f_op);
137                 file->f_op = fops_get(old_fops);
138         }
139         fops_put(old_fops);
140         mutex_unlock(&videodev_lock);
141         return err;
142 }
143
144 /*
145  * open/release helper functions -- handle exclusive opens
146  * Should be removed soon
147  */
148 int video_exclusive_open(struct inode *inode, struct file *file)
149 {
150         struct video_device *vfl = video_devdata(file);
151         int retval = 0;
152
153         mutex_lock(&vfl->lock);
154         if (vfl->users)
155                 retval = -EBUSY;
156         else
157                 vfl->users++;
158         mutex_unlock(&vfl->lock);
159         return retval;
160 }
161 EXPORT_SYMBOL(video_exclusive_open);
162
163 int video_exclusive_release(struct inode *inode, struct file *file)
164 {
165         struct video_device *vfl = video_devdata(file);
166
167         vfl->users--;
168         return 0;
169 }
170 EXPORT_SYMBOL(video_exclusive_release);
171
172 /**
173  * get_index - assign stream number based on parent device
174  * @vdev: video_device to assign index number to, vdev->dev should be assigned
175  * @num: -1 if auto assign, requested number otherwise
176  *
177  *
178  * returns -ENFILE if num is already in use, a free index number if
179  * successful.
180  */
181 static int get_index(struct video_device *vdev, int num)
182 {
183         u32 used = 0;
184         const int max_index = sizeof(used) * 8 - 1;
185         int i;
186
187         /* Currently a single v4l driver instance cannot create more than
188            32 devices.
189            Increase to u64 or an array of u32 if more are needed. */
190         if (num > max_index) {
191                 printk(KERN_ERR "videodev: %s num is too large\n", __func__);
192                 return -EINVAL;
193         }
194
195         for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
196                 if (video_device[i] != NULL &&
197                     video_device[i] != vdev &&
198                     video_device[i]->parent == vdev->parent) {
199                         used |= 1 << video_device[i]->index;
200                 }
201         }
202
203         if (num >= 0) {
204                 if (used & (1 << num))
205                         return -ENFILE;
206                 return num;
207         }
208
209         i = ffz(used);
210         return i > max_index ? -ENFILE : i;
211 }
212
213 static const struct file_operations video_fops;
214
215 int video_register_device(struct video_device *vfd, int type, int nr)
216 {
217         return video_register_device_index(vfd, type, nr, -1);
218 }
219 EXPORT_SYMBOL(video_register_device);
220
221 /**
222  *      video_register_device_index - register video4linux devices
223  *      @vfd:  video device structure we want to register
224  *      @type: type of device to register
225  *      @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
226  *             -1 == first free)
227  *      @index: stream number based on parent device;
228  *              -1 if auto assign, requested number otherwise
229  *
230  *      The registration code assigns minor numbers based on the type
231  *      requested. -ENFILE is returned in all the device slots for this
232  *      category are full. If not then the minor field is set and the
233  *      driver initialize function is called (if non %NULL).
234  *
235  *      Zero is returned on success.
236  *
237  *      Valid types are
238  *
239  *      %VFL_TYPE_GRABBER - A frame grabber
240  *
241  *      %VFL_TYPE_VTX - A teletext device
242  *
243  *      %VFL_TYPE_VBI - Vertical blank data (undecoded)
244  *
245  *      %VFL_TYPE_RADIO - A radio card
246  */
247
248 int video_register_device_index(struct video_device *vfd, int type, int nr,
249                                         int index)
250 {
251         int i = 0;
252         int base;
253         int end;
254         int ret;
255         char *name_base;
256
257         if (vfd == NULL)
258                 return -EINVAL;
259
260         if (vfd == NULL)
261                 return -EINVAL;
262
263         switch (type) {
264         case VFL_TYPE_GRABBER:
265                 base = MINOR_VFL_TYPE_GRABBER_MIN;
266                 end = MINOR_VFL_TYPE_GRABBER_MAX+1;
267                 name_base = "video";
268                 break;
269         case VFL_TYPE_VTX:
270                 base = MINOR_VFL_TYPE_VTX_MIN;
271                 end = MINOR_VFL_TYPE_VTX_MAX+1;
272                 name_base = "vtx";
273                 break;
274         case VFL_TYPE_VBI:
275                 base = MINOR_VFL_TYPE_VBI_MIN;
276                 end = MINOR_VFL_TYPE_VBI_MAX+1;
277                 name_base = "vbi";
278                 break;
279         case VFL_TYPE_RADIO:
280                 base = MINOR_VFL_TYPE_RADIO_MIN;
281                 end = MINOR_VFL_TYPE_RADIO_MAX+1;
282                 name_base = "radio";
283                 break;
284         default:
285                 printk(KERN_ERR "%s called with unknown type: %d\n",
286                        __func__, type);
287                 return -EINVAL;
288         }
289
290         /* pick a minor number */
291         mutex_lock(&videodev_lock);
292         if (nr >= 0  &&  nr < end-base) {
293                 /* use the one the driver asked for */
294                 i = base + nr;
295                 if (NULL != video_device[i]) {
296                         mutex_unlock(&videodev_lock);
297                         return -ENFILE;
298                 }
299         } else {
300                 /* use first free */
301                 for (i = base; i < end; i++)
302                         if (NULL == video_device[i])
303                                 break;
304                 if (i == end) {
305                         mutex_unlock(&videodev_lock);
306                         return -ENFILE;
307                 }
308         }
309         video_device[i] = vfd;
310         vfd->vfl_type = type;
311         vfd->minor = i;
312
313         ret = get_index(vfd, index);
314         vfd->index = ret;
315
316         mutex_unlock(&videodev_lock);
317
318         if (ret < 0) {
319                 printk(KERN_ERR "%s: get_index failed\n", __func__);
320                 goto fail_minor;
321         }
322
323         mutex_init(&vfd->lock);
324
325         /* sysfs class */
326         memset(&vfd->dev, 0x00, sizeof(vfd->dev));
327         vfd->dev.class = &video_class;
328         vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
329         if (vfd->parent)
330                 vfd->dev.parent = vfd->parent;
331         sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base);
332         ret = device_register(&vfd->dev);
333         if (ret < 0) {
334                 printk(KERN_ERR "%s: device_register failed\n", __func__);
335                 goto fail_minor;
336         }
337
338 #if 1
339         /* needed until all drivers are fixed */
340         if (!vfd->release)
341                 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
342                        "Please fix your driver for proper sysfs support, see "
343                        "http://lwn.net/Articles/36850/\n", vfd->name);
344 #endif
345         return 0;
346
347 fail_minor:
348         mutex_lock(&videodev_lock);
349         video_device[vfd->minor] = NULL;
350         vfd->minor = -1;
351         mutex_unlock(&videodev_lock);
352         return ret;
353 }
354 EXPORT_SYMBOL(video_register_device_index);
355
356 /**
357  *      video_unregister_device - unregister a video4linux device
358  *      @vfd: the device to unregister
359  *
360  *      This unregisters the passed device and deassigns the minor
361  *      number. Future open calls will be met with errors.
362  */
363
364 void video_unregister_device(struct video_device *vfd)
365 {
366         mutex_lock(&videodev_lock);
367         if (video_device[vfd->minor] != vfd)
368                 panic("videodev: bad unregister");
369
370         video_device[vfd->minor] = NULL;
371         device_unregister(&vfd->dev);
372         mutex_unlock(&videodev_lock);
373 }
374 EXPORT_SYMBOL(video_unregister_device);
375
376 /*
377  * Video fs operations
378  */
379 static const struct file_operations video_fops = {
380         .owner          = THIS_MODULE,
381         .llseek         = no_llseek,
382         .open           = video_open,
383 };
384
385 /*
386  *      Initialise video for linux
387  */
388
389 static int __init videodev_init(void)
390 {
391         int ret;
392
393         printk(KERN_INFO "Linux video capture interface: v2.00\n");
394         if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
395                 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
396                 return -EIO;
397         }
398
399         ret = class_register(&video_class);
400         if (ret < 0) {
401                 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
402                 printk(KERN_WARNING "video_dev: class_register failed\n");
403                 return -EIO;
404         }
405
406         return 0;
407 }
408
409 static void __exit videodev_exit(void)
410 {
411         class_unregister(&video_class);
412         unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
413 }
414
415 module_init(videodev_init)
416 module_exit(videodev_exit)
417
418 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
419 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
420 MODULE_LICENSE("GPL");
421
422
423 /*
424  * Local variables:
425  * c-basic-offset: 8
426  * End:
427  */