aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/videodev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/videodev.c')
-rw-r--r--drivers/media/video/videodev.c436
1 files changed, 436 insertions, 0 deletions
diff --git a/drivers/media/video/videodev.c b/drivers/media/video/videodev.c
new file mode 100644
index 000000000000..06df15f75de9
--- /dev/null
+++ b/drivers/media/video/videodev.c
@@ -0,0 +1,436 @@
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
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
71#if 1 /* needed until all drivers are fixed */
72 if (!vfd->release)
73 return;
74#endif
75 vfd->release(vfd);
76}
77
78static struct class video_class = {
79 .name = VIDEO_NAME,
80 .release = video_release,
81};
82
83/*
84 * Active devices
85 */
86
87static struct video_device *video_device[VIDEO_NUM_DEVICES];
88static DECLARE_MUTEX(videodev_lock);
89
90struct 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 */
98static 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
136static unsigned int
137video_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
162int
163video_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
217out:
218 if (mbuf)
219 kfree(mbuf);
220 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);
244
245 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)
257 *
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).
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 *
273 * %VFL_TYPE_RADIO - A radio card
274 */
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;
282
283 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;
297 end=240;
298 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 */
338 memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
339 if (vfd->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);
347
348#if 1 /* needed until all drivers are fixed */
349 if (!vfd->release)
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);
353#endif
354 return 0;
355}
356
357/**
358 * video_unregister_device - unregister a video4linux device
359 * @vfd: the device to unregister
360 *
361 * This unregisters the passed device and deassigns the minor
362 * number. Future open calls will be met with errors.
363 */
364
365void video_unregister_device(struct video_device *vfd)
366{
367 down(&videodev_lock);
368 if(video_device[vfd->minor]!=vfd)
369 panic("videodev: bad unregister");
370
371 devfs_remove(vfd->devfs_name);
372 video_device[vfd->minor]=NULL;
373 class_device_unregister(&vfd->class_dev);
374 up(&videodev_lock);
375}
376
377
378static struct file_operations video_fops=
379{
380 .owner = THIS_MODULE,
381 .llseek = no_llseek,
382 .open = video_open,
383};
384
385/*
386 * Initialise video for linux
387 */
388
389static int __init videodev_init(void)
390{
391 int ret;
392
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);
396 return -EIO;
397 }
398
399 ret = class_register(&video_class);
400 if (ret < 0) {
401 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
402 printk(KERN_WARNING "video_dev: class_register failed\n");
403 return -EIO;
404 }
405
406 return 0;
407}
408
409static void __exit videodev_exit(void)
410{
411 class_unregister(&video_class);
412 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
413}
414
415module_init(videodev_init)
416module_exit(videodev_exit)
417
418EXPORT_SYMBOL(video_register_device);
419EXPORT_SYMBOL(video_unregister_device);
420EXPORT_SYMBOL(video_devdata);
421EXPORT_SYMBOL(video_usercopy);
422EXPORT_SYMBOL(video_exclusive_open);
423EXPORT_SYMBOL(video_exclusive_release);
424EXPORT_SYMBOL(video_device_alloc);
425EXPORT_SYMBOL(video_device_release);
426
427MODULE_AUTHOR("Alan Cox");
428MODULE_DESCRIPTION("Device registrar for Video4Linux drivers");
429MODULE_LICENSE("GPL");
430
431
432/*
433 * Local variables:
434 * c-basic-offset: 8
435 * End:
436 */