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