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