[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / media / video / videodev.c
blob06df15f75de94d2fdf34268fd799737e99d7c5d1
1 /*
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>
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>
34 #include <linux/videodev.h>
36 #define VIDEO_NUM_DEVICES 256
37 #define VIDEO_NAME "video4linux"
40 * sysfs stuff
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);
56 if (NULL == vfd)
57 return NULL;
58 memset(vfd,0,sizeof(*vfd));
59 return vfd;
62 void video_device_release(struct video_device *vfd)
64 kfree(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 */
72 if (!vfd->release)
73 return;
74 #endif
75 vfd->release(vfd);
78 static struct class video_class = {
79 .name = VIDEO_NAME,
80 .release = video_release,
84 * Active devices
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);
101 int err = 0;
102 struct video_device *vfl;
103 struct file_operations *old_fops;
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;
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);
127 fops_put(old_fops);
128 up(&videodev_lock);
129 return err;
133 * helper function -- handles userspace copying for ioctl arguments
136 static unsigned int
137 video_fix_command(unsigned int cmd)
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;
159 return cmd;
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))
168 char sbuf[128];
169 void *mbuf = NULL;
170 void *parg = NULL;
171 int err = -EINVAL;
173 cmd = video_fix_command(cmd);
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;
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;
200 /* call driver */
201 err = func(inode, file, cmd, parg);
202 if (err == -ENOIOCTLCMD)
203 err = -EINVAL;
204 if (err < 0)
205 goto out;
207 /* Copy results into user buffer */
208 switch (_IOC_DIR(cmd))
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;
217 out:
218 if (mbuf)
219 kfree(mbuf);
220 return err;
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);
229 int retval = 0;
231 down(&vfl->lock);
232 if (vfl->users) {
233 retval = -EBUSY;
234 } else {
235 vfl->users++;
237 up(&vfl->lock);
238 return retval;
241 int video_exclusive_release(struct inode *inode, struct file *file)
243 struct video_device *vfl = video_devdata(file);
245 vfl->users--;
246 return 0;
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, ...
256 * -1 == first free)
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.
265 * Valid types are
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)
278 int i=0;
279 int base;
280 int end;
281 char *name_base;
283 switch(type)
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;
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;
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;
328 video_device[i]=vfd;
329 vfd->minor=i;
330 up(&videodev_lock);
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);
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);
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;
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);
374 up(&videodev_lock);
378 static struct file_operations video_fops=
380 .owner = THIS_MODULE,
381 .llseek = no_llseek,
382 .open = video_open,
386 * Initialise video for linux
389 static int __init videodev_init(void)
391 int ret;
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;
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;
406 return 0;
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");
433 * Local variables:
434 * c-basic-offset: 8
435 * End: