2 * Video capture interface for Linux
4 * A generic video device interface for the LINUX operating system
5 * using a set of device structures/vectors for low level operations.
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.
12 * Author: Alan Cox, <alan@redhat.com>
14 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
15 * - Added procfs support
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>
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>
34 #include <linux/videodev.h>
36 #define VIDEO_NUM_DEVICES 256
37 #define VIDEO_NAME "video4linux"
43 static ssize_t
show_name(struct class_device
*cd
, char *buf
)
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
);
49 static CLASS_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
51 struct video_device
*video_device_alloc(void)
53 struct video_device
*vfd
;
55 vfd
= kmalloc(sizeof(*vfd
),GFP_KERNEL
);
58 memset(vfd
,0,sizeof(*vfd
));
62 void video_device_release(struct video_device
*vfd
)
67 static void video_release(struct class_device
*cd
)
69 struct video_device
*vfd
= container_of(cd
, struct video_device
, class_dev
);
71 #if 1 /* needed until all drivers are fixed */
78 static struct class video_class
= {
80 .release
= video_release
,
87 static struct video_device
*video_device
[VIDEO_NUM_DEVICES
];
88 static DECLARE_MUTEX(videodev_lock
);
90 struct video_device
* video_devdata(struct file
*file
)
92 return video_device
[iminor(file
->f_dentry
->d_inode
)];
96 * Open a video device.
98 static int video_open(struct inode
*inode
, struct file
*file
)
100 unsigned int minor
= iminor(inode
);
102 struct video_device
*vfl
;
103 struct file_operations
*old_fops
;
105 if(minor
>=VIDEO_NUM_DEVICES
)
107 down(&videodev_lock
);
108 vfl
=video_device
[minor
];
111 request_module("char-major-%d-%d", VIDEO_MAJOR
, minor
);
112 down(&videodev_lock
);
113 vfl
=video_device
[minor
];
119 old_fops
= file
->f_op
;
120 file
->f_op
= fops_get(vfl
->fops
);
122 err
= file
->f_op
->open(inode
,file
);
124 fops_put(file
->f_op
);
125 file
->f_op
= fops_get(old_fops
);
133 * helper function -- handles userspace copying for ioctl arguments
137 video_fix_command(unsigned int cmd
)
140 case VIDIOC_OVERLAY_OLD
:
141 cmd
= VIDIOC_OVERLAY
;
143 case VIDIOC_S_PARM_OLD
:
146 case VIDIOC_S_CTRL_OLD
:
149 case VIDIOC_G_AUDIO_OLD
:
150 cmd
= VIDIOC_G_AUDIO
;
152 case VIDIOC_G_AUDOUT_OLD
:
153 cmd
= VIDIOC_G_AUDOUT
;
155 case VIDIOC_CROPCAP_OLD
:
156 cmd
= VIDIOC_CROPCAP
;
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
))
173 cmd
= video_fix_command(cmd
);
175 /* Copy arguments into temp kernel buffer */
176 switch (_IOC_DIR(cmd
)) {
182 case (_IOC_WRITE
| _IOC_READ
):
183 if (_IOC_SIZE(cmd
) <= sizeof(sbuf
)) {
186 /* too big to allocate from stack */
187 mbuf
= kmalloc(_IOC_SIZE(cmd
),GFP_KERNEL
);
194 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
195 if (copy_from_user(parg
, (void __user
*)arg
, _IOC_SIZE(cmd
)))
201 err
= func(inode
, file
, cmd
, parg
);
202 if (err
== -ENOIOCTLCMD
)
207 /* Copy results into user buffer */
208 switch (_IOC_DIR(cmd
))
211 case (_IOC_WRITE
| _IOC_READ
):
212 if (copy_to_user((void __user
*)arg
, parg
, _IOC_SIZE(cmd
)))
224 * open/release helper functions -- handle exclusive opens
226 int video_exclusive_open(struct inode
*inode
, struct file
*file
)
228 struct video_device
*vfl
= video_devdata(file
);
241 int video_exclusive_release(struct inode
*inode
, struct file
*file
)
243 struct video_device
*vfl
= video_devdata(file
);
249 static struct file_operations video_fops
;
252 * video_register_device - register video4linux devices
253 * @vfd: video device structure we want to register
254 * @type: type of device to register
255 * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ...
258 * The registration code assigns minor numbers based on the type
259 * requested. -ENFILE is returned in all the device slots for this
260 * category are full. If not then the minor field is set and the
261 * driver initialize function is called (if non %NULL).
263 * Zero is returned on success.
267 * %VFL_TYPE_GRABBER - A frame grabber
269 * %VFL_TYPE_VTX - A teletext device
271 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
273 * %VFL_TYPE_RADIO - A radio card
276 int video_register_device(struct video_device
*vfd
, int type
, int nr
)
285 case VFL_TYPE_GRABBER
:
309 /* pick a minor number */
310 down(&videodev_lock
);
311 if (nr
>= 0 && nr
< end
-base
) {
312 /* use the one the driver asked for */
314 if (NULL
!= video_device
[i
]) {
320 for(i
=base
;i
<end
;i
++)
321 if (NULL
== video_device
[i
])
332 sprintf(vfd
->devfs_name
, "v4l/%s%d", name_base
, i
- base
);
333 devfs_mk_cdev(MKDEV(VIDEO_MAJOR
, vfd
->minor
),
334 S_IFCHR
| S_IRUSR
| S_IWUSR
, vfd
->devfs_name
);
335 init_MUTEX(&vfd
->lock
);
338 memset(&vfd
->class_dev
, 0x00, sizeof(vfd
->class_dev
));
340 vfd
->class_dev
.dev
= vfd
->dev
;
341 vfd
->class_dev
.class = &video_class
;
342 vfd
->class_dev
.devt
= MKDEV(VIDEO_MAJOR
, vfd
->minor
);
343 strlcpy(vfd
->class_dev
.class_id
, vfd
->devfs_name
+ 4, BUS_ID_SIZE
);
344 class_device_register(&vfd
->class_dev
);
345 class_device_create_file(&vfd
->class_dev
,
346 &class_device_attr_name
);
348 #if 1 /* needed until all drivers are fixed */
350 printk(KERN_WARNING
"videodev: \"%s\" has no release callback. "
351 "Please fix your driver for proper sysfs support, see "
352 "http://lwn.net/Articles/36850/\n", vfd
->name
);
358 * video_unregister_device - unregister a video4linux device
359 * @vfd: the device to unregister
361 * This unregisters the passed device and deassigns the minor
362 * number. Future open calls will be met with errors.
365 void video_unregister_device(struct video_device
*vfd
)
367 down(&videodev_lock
);
368 if(video_device
[vfd
->minor
]!=vfd
)
369 panic("videodev: bad unregister");
371 devfs_remove(vfd
->devfs_name
);
372 video_device
[vfd
->minor
]=NULL
;
373 class_device_unregister(&vfd
->class_dev
);
378 static struct file_operations video_fops
=
380 .owner
= THIS_MODULE
,
386 * Initialise video for linux
389 static int __init
videodev_init(void)
393 printk(KERN_INFO
"Linux video capture interface: v1.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
);
399 ret
= class_register(&video_class
);
401 unregister_chrdev(VIDEO_MAJOR
, VIDEO_NAME
);
402 printk(KERN_WARNING
"video_dev: class_register failed\n");
409 static void __exit
videodev_exit(void)
411 class_unregister(&video_class
);
412 unregister_chrdev(VIDEO_MAJOR
, VIDEO_NAME
);
415 module_init(videodev_init
)
416 module_exit(videodev_exit
)
418 EXPORT_SYMBOL(video_register_device
);
419 EXPORT_SYMBOL(video_unregister_device
);
420 EXPORT_SYMBOL(video_devdata
);
421 EXPORT_SYMBOL(video_usercopy
);
422 EXPORT_SYMBOL(video_exclusive_open
);
423 EXPORT_SYMBOL(video_exclusive_release
);
424 EXPORT_SYMBOL(video_device_alloc
);
425 EXPORT_SYMBOL(video_device_release
);
427 MODULE_AUTHOR("Alan Cox");
428 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
429 MODULE_LICENSE("GPL");