blob: 9a9902c56ae72f81d06ebf58088baf73e05cc28a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
43static 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
49static CLASS_DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
50
51struct 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
62void video_device_release(struct video_device *vfd)
63{
64 kfree(vfd);
65}
66
67static void video_release(struct class_device *cd)
68{
69 struct video_device *vfd = container_of(cd, struct video_device, class_dev);
70
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -020071#if 1
Michael Krufky50c25ff2006-01-09 15:25:34 -020072 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!vfd->release)
74 return;
75#endif
76 vfd->release(vfd);
77}
78
79static struct class video_class = {
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080080 .name = VIDEO_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .release = video_release,
82};
83
84/*
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080085 * Active devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -080087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct video_device *video_device[VIDEO_NUM_DEVICES];
89static DECLARE_MUTEX(videodev_lock);
90
91struct video_device* video_devdata(struct file *file)
92{
93 return video_device[iminor(file->f_dentry->d_inode)];
94}
95
96/*
97 * Open a video device.
98 */
99static int video_open(struct inode *inode, struct file *file)
100{
101 unsigned int minor = iminor(inode);
102 int err = 0;
103 struct video_device *vfl;
104 struct file_operations *old_fops;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if(minor>=VIDEO_NUM_DEVICES)
107 return -ENODEV;
108 down(&videodev_lock);
109 vfl=video_device[minor];
110 if(vfl==NULL) {
111 up(&videodev_lock);
112 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
113 down(&videodev_lock);
114 vfl=video_device[minor];
115 if (vfl==NULL) {
116 up(&videodev_lock);
117 return -ENODEV;
118 }
119 }
120 old_fops = file->f_op;
121 file->f_op = fops_get(vfl->fops);
122 if(file->f_op->open)
123 err = file->f_op->open(inode,file);
124 if (err) {
125 fops_put(file->f_op);
126 file->f_op = fops_get(old_fops);
127 }
128 fops_put(old_fops);
129 up(&videodev_lock);
130 return err;
131}
132
133/*
134 * helper function -- handles userspace copying for ioctl arguments
135 */
136
137static unsigned int
138video_fix_command(unsigned int cmd)
139{
140 switch (cmd) {
141 case VIDIOC_OVERLAY_OLD:
142 cmd = VIDIOC_OVERLAY;
143 break;
144 case VIDIOC_S_PARM_OLD:
145 cmd = VIDIOC_S_PARM;
146 break;
147 case VIDIOC_S_CTRL_OLD:
148 cmd = VIDIOC_S_CTRL;
149 break;
150 case VIDIOC_G_AUDIO_OLD:
151 cmd = VIDIOC_G_AUDIO;
152 break;
153 case VIDIOC_G_AUDOUT_OLD:
154 cmd = VIDIOC_G_AUDOUT;
155 break;
156 case VIDIOC_CROPCAP_OLD:
157 cmd = VIDIOC_CROPCAP;
158 break;
159 }
160 return cmd;
161}
162
163int
164video_usercopy(struct inode *inode, struct file *file,
165 unsigned int cmd, unsigned long arg,
166 int (*func)(struct inode *inode, struct file *file,
167 unsigned int cmd, void *arg))
168{
169 char sbuf[128];
170 void *mbuf = NULL;
171 void *parg = NULL;
172 int err = -EINVAL;
173
174 cmd = video_fix_command(cmd);
175
176 /* Copy arguments into temp kernel buffer */
177 switch (_IOC_DIR(cmd)) {
178 case _IOC_NONE:
179 parg = NULL;
180 break;
181 case _IOC_READ:
182 case _IOC_WRITE:
183 case (_IOC_WRITE | _IOC_READ):
184 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
185 parg = sbuf;
186 } else {
187 /* too big to allocate from stack */
188 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
189 if (NULL == mbuf)
190 return -ENOMEM;
191 parg = mbuf;
192 }
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 err = -EFAULT;
195 if (_IOC_DIR(cmd) & _IOC_WRITE)
196 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
197 goto out;
198 break;
199 }
200
201 /* call driver */
202 err = func(inode, file, cmd, parg);
203 if (err == -ENOIOCTLCMD)
204 err = -EINVAL;
205 if (err < 0)
206 goto out;
207
208 /* Copy results into user buffer */
209 switch (_IOC_DIR(cmd))
210 {
211 case _IOC_READ:
212 case (_IOC_WRITE | _IOC_READ):
213 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
214 err = -EFAULT;
215 break;
216 }
217
218out:
Jesper Juhl2ea75332005-11-07 01:01:31 -0800219 kfree(mbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
221}
222
223/*
224 * open/release helper functions -- handle exclusive opens
225 */
226int video_exclusive_open(struct inode *inode, struct file *file)
227{
228 struct video_device *vfl = video_devdata(file);
229 int retval = 0;
230
231 down(&vfl->lock);
232 if (vfl->users) {
233 retval = -EBUSY;
234 } else {
235 vfl->users++;
236 }
237 up(&vfl->lock);
238 return retval;
239}
240
241int video_exclusive_release(struct inode *inode, struct file *file)
242{
243 struct video_device *vfl = video_devdata(file);
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 vfl->users--;
246 return 0;
247}
248
249static struct file_operations video_fops;
250
251/**
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, ...
256 * -1 == first free)
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * 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).
262 *
263 * Zero is returned on success.
264 *
265 * Valid types are
266 *
267 * %VFL_TYPE_GRABBER - A frame grabber
268 *
269 * %VFL_TYPE_VTX - A teletext device
270 *
271 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
272 *
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800273 * %VFL_TYPE_RADIO - A radio card
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
275
276int video_register_device(struct video_device *vfd, int type, int nr)
277{
278 int i=0;
279 int base;
280 int end;
281 char *name_base;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 switch(type)
284 {
285 case VFL_TYPE_GRABBER:
286 base=0;
287 end=64;
288 name_base = "video";
289 break;
290 case VFL_TYPE_VTX:
291 base=192;
292 end=224;
293 name_base = "vtx";
294 break;
295 case VFL_TYPE_VBI:
296 base=224;
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800297 end=256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 name_base = "vbi";
299 break;
300 case VFL_TYPE_RADIO:
301 base=64;
302 end=128;
303 name_base = "radio";
304 break;
305 default:
306 return -1;
307 }
308
309 /* pick a minor number */
310 down(&videodev_lock);
311 if (nr >= 0 && nr < end-base) {
312 /* use the one the driver asked for */
313 i = base+nr;
314 if (NULL != video_device[i]) {
315 up(&videodev_lock);
316 return -ENFILE;
317 }
318 } else {
319 /* use first free */
320 for(i=base;i<end;i++)
321 if (NULL == video_device[i])
322 break;
323 if (i == end) {
324 up(&videodev_lock);
325 return -ENFILE;
326 }
327 }
328 video_device[i]=vfd;
329 vfd->minor=i;
330 up(&videodev_lock);
331
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);
336
337 /* sysfs class */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800338 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (vfd->dev)
340 vfd->class_dev.dev = vfd->dev;
341 vfd->class_dev.class = &video_class;
Michael Krufky50c25ff2006-01-09 15:25:34 -0200342 vfd->class_dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200346 &class_device_attr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Mauro Carvalho Chehabd21838d2006-01-09 15:25:21 -0200348#if 1
349 /* needed until all drivers are fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!vfd->release)
351 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
352 "Please fix your driver for proper sysfs support, see "
353 "http://lwn.net/Articles/36850/\n", vfd->name);
354#endif
355 return 0;
356}
357
358/**
359 * video_unregister_device - unregister a video4linux device
360 * @vfd: the device to unregister
361 *
362 * This unregisters the passed device and deassigns the minor
363 * number. Future open calls will be met with errors.
364 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366void video_unregister_device(struct video_device *vfd)
367{
368 down(&videodev_lock);
369 if(video_device[vfd->minor]!=vfd)
370 panic("videodev: bad unregister");
371
372 devfs_remove(vfd->devfs_name);
373 video_device[vfd->minor]=NULL;
374 class_device_unregister(&vfd->class_dev);
375 up(&videodev_lock);
376}
377
378
379static struct file_operations video_fops=
380{
381 .owner = THIS_MODULE,
382 .llseek = no_llseek,
383 .open = video_open,
384};
385
386/*
387 * Initialise video for linux
388 */
Sigmund Augdal Helberg938606b2005-12-01 00:51:19 -0800389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static int __init videodev_init(void)
391{
392 int ret;
393
394 printk(KERN_INFO "Linux video capture interface: v1.00\n");
395 if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
396 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
397 return -EIO;
398 }
399
400 ret = class_register(&video_class);
401 if (ret < 0) {
402 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
403 printk(KERN_WARNING "video_dev: class_register failed\n");
404 return -EIO;
405 }
406
407 return 0;
408}
409
410static void __exit videodev_exit(void)
411{
412 class_unregister(&video_class);
413 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
414}
415
416module_init(videodev_init)
417module_exit(videodev_exit)
418
419EXPORT_SYMBOL(video_register_device);
420EXPORT_SYMBOL(video_unregister_device);
421EXPORT_SYMBOL(video_devdata);
422EXPORT_SYMBOL(video_usercopy);
423EXPORT_SYMBOL(video_exclusive_open);
424EXPORT_SYMBOL(video_exclusive_release);
425EXPORT_SYMBOL(video_device_alloc);
426EXPORT_SYMBOL(video_device_release);
427
428MODULE_AUTHOR("Alan Cox");
429MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
430MODULE_LICENSE("GPL");
431
432
433/*
434 * Local variables:
435 * c-basic-offset: 8
436 * End:
437 */