Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / drivers / media / video / videodev.c
1 /*
2  * Video capture interface for Linux
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  * Author:      Alan Cox, <alan@redhat.com>
13  *
14  * Fixes:       20000516  Claudio Matsuoka <claudio@conectiva.com>
15  *              - Added procfs support
16  */
17
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/smp_lock.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmod.h>
28 #include <linux/slab.h>
29 #include <linux/devfs_fs_kernel.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/semaphore.h>
33
34 #include <linux/videodev.h>
35
36 #define VIDEO_NUM_DEVICES       256
37 #define VIDEO_NAME              "video4linux"
38
39 /*
40  *      sysfs stuff
41  */
42
43 static ssize_t show_name(struct class_device *cd, char *buf)
44 {
45         struct video_device *vfd = container_of(cd, struct video_device, class_dev);
46         return sprintf(buf,"%.*s\n",(int)sizeof(vfd->name),vfd->name);
47 }
48
49 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
50
51 struct video_device *video_device_alloc(void)
52 {
53         struct video_device *vfd;
54
55         vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
56         return vfd;
57 }
58
59 void video_device_release(struct video_device *vfd)
60 {
61         kfree(vfd);
62 }
63
64 static void video_release(struct class_device *cd)
65 {
66         struct video_device *vfd = container_of(cd, struct video_device, class_dev);
67
68 #if 1
69         /* needed until all drivers are fixed */
70         if (!vfd->release)
71                 return;
72 #endif
73         vfd->release(vfd);
74 }
75
76 static struct class video_class = {
77         .name    = VIDEO_NAME,
78         .release = video_release,
79 };
80
81 /*
82  *      Active devices
83  */
84
85 static struct video_device *video_device[VIDEO_NUM_DEVICES];
86 static DECLARE_MUTEX(videodev_lock);
87
88 struct video_device* video_devdata(struct file *file)
89 {
90         return video_device[iminor(file->f_dentry->d_inode)];
91 }
92
93 /*
94  *      Open a video device.
95  */
96 static int video_open(struct inode *inode, struct file *file)
97 {
98         unsigned int minor = iminor(inode);
99         int err = 0;
100         struct video_device *vfl;
101         struct file_operations *old_fops;
102
103         if(minor>=VIDEO_NUM_DEVICES)
104                 return -ENODEV;
105         down(&videodev_lock);
106         vfl=video_device[minor];
107         if(vfl==NULL) {
108                 up(&videodev_lock);
109                 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
110                 down(&videodev_lock);
111                 vfl=video_device[minor];
112                 if (vfl==NULL) {
113                         up(&videodev_lock);
114                         return -ENODEV;
115                 }
116         }
117         old_fops = file->f_op;
118         file->f_op = fops_get(vfl->fops);
119         if(file->f_op->open)
120                 err = file->f_op->open(inode,file);
121         if (err) {
122                 fops_put(file->f_op);
123                 file->f_op = fops_get(old_fops);
124         }
125         fops_put(old_fops);
126         up(&videodev_lock);
127         return err;
128 }
129
130 /*
131  * helper function -- handles userspace copying for ioctl arguments
132  */
133
134 static unsigned int
135 video_fix_command(unsigned int cmd)
136 {
137         switch (cmd) {
138         case VIDIOC_OVERLAY_OLD:
139                 cmd = VIDIOC_OVERLAY;
140                 break;
141         case VIDIOC_S_PARM_OLD:
142                 cmd = VIDIOC_S_PARM;
143                 break;
144         case VIDIOC_S_CTRL_OLD:
145                 cmd = VIDIOC_S_CTRL;
146                 break;
147         case VIDIOC_G_AUDIO_OLD:
148                 cmd = VIDIOC_G_AUDIO;
149                 break;
150         case VIDIOC_G_AUDOUT_OLD:
151                 cmd = VIDIOC_G_AUDOUT;
152                 break;
153         case VIDIOC_CROPCAP_OLD:
154                 cmd = VIDIOC_CROPCAP;
155                 break;
156         }
157         return cmd;
158 }
159
160 int
161 video_usercopy(struct inode *inode, struct file *file,
162                unsigned int cmd, unsigned long arg,
163                int (*func)(struct inode *inode, struct file *file,
164                            unsigned int cmd, void *arg))
165 {
166         char    sbuf[128];
167         void    *mbuf = NULL;
168         void    *parg = NULL;
169         int     err  = -EINVAL;
170
171         cmd = video_fix_command(cmd);
172
173         /*  Copy arguments into temp kernel buffer  */
174         switch (_IOC_DIR(cmd)) {
175         case _IOC_NONE:
176                 parg = NULL;
177                 break;
178         case _IOC_READ:
179         case _IOC_WRITE:
180         case (_IOC_WRITE | _IOC_READ):
181                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
182                         parg = sbuf;
183                 } else {
184                         /* too big to allocate from stack */
185                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
186                         if (NULL == mbuf)
187                                 return -ENOMEM;
188                         parg = mbuf;
189                 }
190
191                 err = -EFAULT;
192                 if (_IOC_DIR(cmd) & _IOC_WRITE)
193                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
194                                 goto out;
195                 break;
196         }
197
198         /* call driver */
199         err = func(inode, file, cmd, parg);
200         if (err == -ENOIOCTLCMD)
201                 err = -EINVAL;
202         if (err < 0)
203                 goto out;
204
205         /*  Copy results into user buffer  */
206         switch (_IOC_DIR(cmd))
207         {
208         case _IOC_READ:
209         case (_IOC_WRITE | _IOC_READ):
210                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
211                         err = -EFAULT;
212                 break;
213         }
214
215 out:
216         kfree(mbuf);
217         return err;
218 }
219
220 /*
221  * open/release helper functions -- handle exclusive opens
222  */
223 int video_exclusive_open(struct inode *inode, struct file *file)
224 {
225         struct  video_device *vfl = video_devdata(file);
226         int retval = 0;
227
228         down(&vfl->lock);
229         if (vfl->users) {
230                 retval = -EBUSY;
231         } else {
232                 vfl->users++;
233         }
234         up(&vfl->lock);
235         return retval;
236 }
237
238 int video_exclusive_release(struct inode *inode, struct file *file)
239 {
240         struct  video_device *vfl = video_devdata(file);
241
242         vfl->users--;
243         return 0;
244 }
245
246 static struct file_operations video_fops;
247
248 /**
249  *      video_register_device - register video4linux devices
250  *      @vfd:  video device structure we want to register
251  *      @type: type of device to register
252  *      @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
253  *             -1 == first free)
254  *
255  *      The registration code assigns minor numbers based on the type
256  *      requested. -ENFILE is returned in all the device slots for this
257  *      category are full. If not then the minor field is set and the
258  *      driver initialize function is called (if non %NULL).
259  *
260  *      Zero is returned on success.
261  *
262  *      Valid types are
263  *
264  *      %VFL_TYPE_GRABBER - A frame grabber
265  *
266  *      %VFL_TYPE_VTX - A teletext device
267  *
268  *      %VFL_TYPE_VBI - Vertical blank data (undecoded)
269  *
270  *      %VFL_TYPE_RADIO - A radio card
271  */
272
273 int video_register_device(struct video_device *vfd, int type, int nr)
274 {
275         int i=0;
276         int base;
277         int end;
278         char *name_base;
279
280         switch(type)
281         {
282                 case VFL_TYPE_GRABBER:
283                         base=0;
284                         end=64;
285                         name_base = "video";
286                         break;
287                 case VFL_TYPE_VTX:
288                         base=192;
289                         end=224;
290                         name_base = "vtx";
291                         break;
292                 case VFL_TYPE_VBI:
293                         base=224;
294                         end=256;
295                         name_base = "vbi";
296                         break;
297                 case VFL_TYPE_RADIO:
298                         base=64;
299                         end=128;
300                         name_base = "radio";
301                         break;
302                 default:
303                         return -1;
304         }
305
306         /* pick a minor number */
307         down(&videodev_lock);
308         if (nr >= 0  &&  nr < end-base) {
309                 /* use the one the driver asked for */
310                 i = base+nr;
311                 if (NULL != video_device[i]) {
312                         up(&videodev_lock);
313                         return -ENFILE;
314                 }
315         } else {
316                 /* use first free */
317                 for(i=base;i<end;i++)
318                         if (NULL == video_device[i])
319                                 break;
320                 if (i == end) {
321                         up(&videodev_lock);
322                         return -ENFILE;
323                 }
324         }
325         video_device[i]=vfd;
326         vfd->minor=i;
327         up(&videodev_lock);
328
329         sprintf(vfd->devfs_name, "v4l/%s%d", name_base, i - base);
330         devfs_mk_cdev(MKDEV(VIDEO_MAJOR, vfd->minor),
331                         S_IFCHR | S_IRUSR | S_IWUSR, vfd->devfs_name);
332         init_MUTEX(&vfd->lock);
333
334         /* sysfs class */
335         memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
336         if (vfd->dev)
337                 vfd->class_dev.dev = vfd->dev;
338         vfd->class_dev.class       = &video_class;
339         vfd->class_dev.devt        = MKDEV(VIDEO_MAJOR, vfd->minor);
340         strlcpy(vfd->class_dev.class_id, vfd->devfs_name + 4, BUS_ID_SIZE);
341         class_device_register(&vfd->class_dev);
342         class_device_create_file(&vfd->class_dev,
343                                 &class_device_attr_name);
344
345 #if 1
346         /* needed until all drivers are fixed */
347         if (!vfd->release)
348                 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
349                        "Please fix your driver for proper sysfs support, see "
350                        "http://lwn.net/Articles/36850/\n", vfd->name);
351 #endif
352         return 0;
353 }
354
355 /**
356  *      video_unregister_device - unregister a video4linux device
357  *      @vfd: the device to unregister
358  *
359  *      This unregisters the passed device and deassigns the minor
360  *      number. Future open calls will be met with errors.
361  */
362
363 void video_unregister_device(struct video_device *vfd)
364 {
365         down(&videodev_lock);
366         if(video_device[vfd->minor]!=vfd)
367                 panic("videodev: bad unregister");
368
369         devfs_remove(vfd->devfs_name);
370         video_device[vfd->minor]=NULL;
371         class_device_unregister(&vfd->class_dev);
372         up(&videodev_lock);
373 }
374
375
376 static struct file_operations video_fops=
377 {
378         .owner          = THIS_MODULE,
379         .llseek         = no_llseek,
380         .open           = video_open,
381 };
382
383 /*
384  *      Initialise video for linux
385  */
386
387 static int __init videodev_init(void)
388 {
389         int ret;
390
391         printk(KERN_INFO "Linux video capture interface: v1.00\n");
392         if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
393                 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
394                 return -EIO;
395         }
396
397         ret = class_register(&video_class);
398         if (ret < 0) {
399                 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
400                 printk(KERN_WARNING "video_dev: class_register failed\n");
401                 return -EIO;
402         }
403
404         return 0;
405 }
406
407 static void __exit videodev_exit(void)
408 {
409         class_unregister(&video_class);
410         unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
411 }
412
413 module_init(videodev_init)
414 module_exit(videodev_exit)
415
416 EXPORT_SYMBOL(video_register_device);
417 EXPORT_SYMBOL(video_unregister_device);
418 EXPORT_SYMBOL(video_devdata);
419 EXPORT_SYMBOL(video_usercopy);
420 EXPORT_SYMBOL(video_exclusive_open);
421 EXPORT_SYMBOL(video_exclusive_release);
422 EXPORT_SYMBOL(video_device_alloc);
423 EXPORT_SYMBOL(video_device_release);
424
425 MODULE_AUTHOR("Alan Cox");
426 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
427 MODULE_LICENSE("GPL");
428
429
430 /*
431  * Local variables:
432  * c-basic-offset: 8
433  * End:
434  */