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