Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Video capture interface for Linux version 2 |
| 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 | * Authors: Alan Cox, <alan@redhat.com> (version 1) |
| 13 | * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2) |
| 14 | * |
| 15 | * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com> |
| 16 | * - Added procfs support |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/kmod.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/smp_lock.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/system.h> |
| 31 | |
| 32 | #include <media/v4l2-common.h> |
| 33 | |
| 34 | #define VIDEO_NUM_DEVICES 256 |
| 35 | #define VIDEO_NAME "video4linux" |
| 36 | |
| 37 | /* |
| 38 | * sysfs stuff |
| 39 | */ |
| 40 | |
| 41 | static ssize_t show_index(struct device *cd, |
| 42 | struct device_attribute *attr, char *buf) |
| 43 | { |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 44 | struct video_device *vfd = container_of(cd, struct video_device, dev); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 45 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 46 | return sprintf(buf, "%i\n", vfd->index); |
| 47 | } |
| 48 | |
| 49 | static ssize_t show_name(struct device *cd, |
| 50 | struct device_attribute *attr, char *buf) |
| 51 | { |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 52 | struct video_device *vfd = container_of(cd, struct video_device, dev); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 53 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 54 | return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name); |
| 55 | } |
| 56 | |
| 57 | static struct device_attribute video_device_attrs[] = { |
| 58 | __ATTR(name, S_IRUGO, show_name, NULL), |
| 59 | __ATTR(index, S_IRUGO, show_index, NULL), |
| 60 | __ATTR_NULL |
| 61 | }; |
| 62 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 63 | /* |
| 64 | * Active devices |
| 65 | */ |
| 66 | static struct video_device *video_device[VIDEO_NUM_DEVICES]; |
| 67 | static DEFINE_MUTEX(videodev_lock); |
| 68 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 69 | struct video_device *video_device_alloc(void) |
| 70 | { |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 71 | return kzalloc(sizeof(struct video_device), GFP_KERNEL); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 72 | } |
| 73 | EXPORT_SYMBOL(video_device_alloc); |
| 74 | |
| 75 | void video_device_release(struct video_device *vfd) |
| 76 | { |
| 77 | kfree(vfd); |
| 78 | } |
| 79 | EXPORT_SYMBOL(video_device_release); |
| 80 | |
Hans Verkuil | f9e86b5 | 2008-08-23 05:47:41 -0300 | [diff] [blame] | 81 | void video_device_release_empty(struct video_device *vfd) |
| 82 | { |
| 83 | /* Do nothing */ |
| 84 | /* Only valid when the video_device struct is a static. */ |
| 85 | } |
| 86 | EXPORT_SYMBOL(video_device_release_empty); |
| 87 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 88 | /* Called when the last user of the character device is gone. */ |
| 89 | static void v4l2_chardev_release(struct kobject *kobj) |
| 90 | { |
| 91 | struct video_device *vfd = container_of(kobj, struct video_device, cdev.kobj); |
| 92 | |
| 93 | mutex_lock(&videodev_lock); |
| 94 | if (video_device[vfd->minor] != vfd) |
| 95 | panic("videodev: bad release"); |
| 96 | |
| 97 | /* Free up this device for reuse */ |
| 98 | video_device[vfd->minor] = NULL; |
| 99 | mutex_unlock(&videodev_lock); |
| 100 | |
| 101 | /* Release the character device */ |
| 102 | vfd->cdev_release(kobj); |
| 103 | /* Release video_device and perform other |
| 104 | cleanups as needed. */ |
| 105 | if (vfd->release) |
| 106 | vfd->release(vfd); |
| 107 | } |
| 108 | |
| 109 | /* The new kobj_type for the character device */ |
| 110 | static struct kobj_type v4l2_ktype_cdev_default = { |
| 111 | .release = v4l2_chardev_release, |
| 112 | }; |
| 113 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 114 | static void video_release(struct device *cd) |
| 115 | { |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 116 | struct video_device *vfd = container_of(cd, struct video_device, dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 117 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 118 | /* It's now safe to delete the char device. |
| 119 | This will either trigger the v4l2_chardev_release immediately (if |
| 120 | the refcount goes to 0) or later when the last user of the |
| 121 | character device closes it. */ |
| 122 | cdev_del(&vfd->cdev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static struct class video_class = { |
| 126 | .name = VIDEO_NAME, |
| 127 | .dev_attrs = video_device_attrs, |
| 128 | .dev_release = video_release, |
| 129 | }; |
| 130 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 131 | struct video_device *video_devdata(struct file *file) |
| 132 | { |
| 133 | return video_device[iminor(file->f_path.dentry->d_inode)]; |
| 134 | } |
| 135 | EXPORT_SYMBOL(video_devdata); |
| 136 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 137 | /** |
| 138 | * get_index - assign stream number based on parent device |
| 139 | * @vdev: video_device to assign index number to, vdev->dev should be assigned |
| 140 | * @num: -1 if auto assign, requested number otherwise |
| 141 | * |
| 142 | * |
| 143 | * returns -ENFILE if num is already in use, a free index number if |
| 144 | * successful. |
| 145 | */ |
| 146 | static int get_index(struct video_device *vdev, int num) |
| 147 | { |
| 148 | u32 used = 0; |
| 149 | const int max_index = sizeof(used) * 8 - 1; |
| 150 | int i; |
| 151 | |
| 152 | /* Currently a single v4l driver instance cannot create more than |
| 153 | 32 devices. |
| 154 | Increase to u64 or an array of u32 if more are needed. */ |
| 155 | if (num > max_index) { |
| 156 | printk(KERN_ERR "videodev: %s num is too large\n", __func__); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) { |
| 161 | if (video_device[i] != NULL && |
| 162 | video_device[i] != vdev && |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 163 | video_device[i]->parent == vdev->parent) { |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 164 | used |= 1 << video_device[i]->index; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (num >= 0) { |
| 169 | if (used & (1 << num)) |
| 170 | return -ENFILE; |
| 171 | return num; |
| 172 | } |
| 173 | |
| 174 | i = ffz(used); |
| 175 | return i > max_index ? -ENFILE : i; |
| 176 | } |
| 177 | |
| 178 | static const struct file_operations video_fops; |
| 179 | |
| 180 | int video_register_device(struct video_device *vfd, int type, int nr) |
| 181 | { |
| 182 | return video_register_device_index(vfd, type, nr, -1); |
| 183 | } |
| 184 | EXPORT_SYMBOL(video_register_device); |
| 185 | |
| 186 | /** |
Randy Dunlap | edc9189 | 2008-07-28 15:39:38 -0300 | [diff] [blame] | 187 | * video_register_device_index - register video4linux devices |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 188 | * @vfd: video device structure we want to register |
| 189 | * @type: type of device to register |
| 190 | * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ... |
| 191 | * -1 == first free) |
Randy Dunlap | edc9189 | 2008-07-28 15:39:38 -0300 | [diff] [blame] | 192 | * @index: stream number based on parent device; |
| 193 | * -1 if auto assign, requested number otherwise |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 194 | * |
| 195 | * The registration code assigns minor numbers based on the type |
| 196 | * requested. -ENFILE is returned in all the device slots for this |
| 197 | * category are full. If not then the minor field is set and the |
| 198 | * driver initialize function is called (if non %NULL). |
| 199 | * |
| 200 | * Zero is returned on success. |
| 201 | * |
| 202 | * Valid types are |
| 203 | * |
| 204 | * %VFL_TYPE_GRABBER - A frame grabber |
| 205 | * |
| 206 | * %VFL_TYPE_VTX - A teletext device |
| 207 | * |
| 208 | * %VFL_TYPE_VBI - Vertical blank data (undecoded) |
| 209 | * |
| 210 | * %VFL_TYPE_RADIO - A radio card |
| 211 | */ |
| 212 | |
| 213 | int video_register_device_index(struct video_device *vfd, int type, int nr, |
| 214 | int index) |
| 215 | { |
| 216 | int i = 0; |
| 217 | int base; |
| 218 | int end; |
| 219 | int ret; |
| 220 | char *name_base; |
Hans Verkuil | 9d95af9 | 2008-08-24 11:18:47 -0300 | [diff] [blame] | 221 | void *priv = video_get_drvdata(vfd); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 222 | |
Hans Verkuil | d6e7497 | 2008-08-23 06:27:59 -0300 | [diff] [blame] | 223 | /* the release callback MUST be present */ |
| 224 | BUG_ON(!vfd->release); |
Henrik Kretzschmar | f3b9f50 | 2008-09-03 17:11:53 -0300 | [diff] [blame] | 225 | |
Henrik Kretzschmar | ee7aa9f | 2008-08-22 16:41:03 -0300 | [diff] [blame] | 226 | if (vfd == NULL) |
| 227 | return -EINVAL; |
| 228 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 229 | switch (type) { |
| 230 | case VFL_TYPE_GRABBER: |
| 231 | base = MINOR_VFL_TYPE_GRABBER_MIN; |
| 232 | end = MINOR_VFL_TYPE_GRABBER_MAX+1; |
| 233 | name_base = "video"; |
| 234 | break; |
| 235 | case VFL_TYPE_VTX: |
| 236 | base = MINOR_VFL_TYPE_VTX_MIN; |
| 237 | end = MINOR_VFL_TYPE_VTX_MAX+1; |
| 238 | name_base = "vtx"; |
| 239 | break; |
| 240 | case VFL_TYPE_VBI: |
| 241 | base = MINOR_VFL_TYPE_VBI_MIN; |
| 242 | end = MINOR_VFL_TYPE_VBI_MAX+1; |
| 243 | name_base = "vbi"; |
| 244 | break; |
| 245 | case VFL_TYPE_RADIO: |
| 246 | base = MINOR_VFL_TYPE_RADIO_MIN; |
| 247 | end = MINOR_VFL_TYPE_RADIO_MAX+1; |
| 248 | name_base = "radio"; |
| 249 | break; |
| 250 | default: |
| 251 | printk(KERN_ERR "%s called with unknown type: %d\n", |
| 252 | __func__, type); |
Henrik Kretzschmar | 46f2c21 | 2008-09-03 16:47:39 -0300 | [diff] [blame] | 253 | return -EINVAL; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 254 | } |
| 255 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 256 | /* Initialize the character device */ |
| 257 | cdev_init(&vfd->cdev, vfd->fops); |
| 258 | vfd->cdev.owner = vfd->fops->owner; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 259 | /* pick a minor number */ |
| 260 | mutex_lock(&videodev_lock); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 261 | if (nr >= 0 && nr < end-base) { |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 262 | /* use the one the driver asked for */ |
| 263 | i = base + nr; |
| 264 | if (NULL != video_device[i]) { |
| 265 | mutex_unlock(&videodev_lock); |
| 266 | return -ENFILE; |
| 267 | } |
| 268 | } else { |
| 269 | /* use first free */ |
| 270 | for (i = base; i < end; i++) |
| 271 | if (NULL == video_device[i]) |
| 272 | break; |
| 273 | if (i == end) { |
| 274 | mutex_unlock(&videodev_lock); |
| 275 | return -ENFILE; |
| 276 | } |
| 277 | } |
| 278 | video_device[i] = vfd; |
Hans Verkuil | 0ea6bc8 | 2008-07-26 08:26:43 -0300 | [diff] [blame] | 279 | vfd->vfl_type = type; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 280 | vfd->minor = i; |
| 281 | |
| 282 | ret = get_index(vfd, index); |
| 283 | vfd->index = ret; |
| 284 | |
| 285 | mutex_unlock(&videodev_lock); |
| 286 | |
| 287 | if (ret < 0) { |
| 288 | printk(KERN_ERR "%s: get_index failed\n", __func__); |
| 289 | goto fail_minor; |
| 290 | } |
| 291 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 292 | ret = cdev_add(&vfd->cdev, MKDEV(VIDEO_MAJOR, vfd->minor), 1); |
| 293 | if (ret < 0) { |
| 294 | printk(KERN_ERR "%s: cdev_add failed\n", __func__); |
| 295 | goto fail_minor; |
| 296 | } |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 297 | /* sysfs class */ |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 298 | memset(&vfd->dev, 0, sizeof(vfd->dev)); |
Hans Verkuil | 9d95af9 | 2008-08-24 11:18:47 -0300 | [diff] [blame] | 299 | /* The memset above cleared the device's drvdata, so |
| 300 | put back the copy we made earlier. */ |
| 301 | video_set_drvdata(vfd, priv); |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 302 | vfd->dev.class = &video_class; |
| 303 | vfd->dev.devt = MKDEV(VIDEO_MAJOR, vfd->minor); |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 304 | if (vfd->parent) |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 305 | vfd->dev.parent = vfd->parent; |
| 306 | sprintf(vfd->dev.bus_id, "%s%d", name_base, i - base); |
| 307 | ret = device_register(&vfd->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 308 | if (ret < 0) { |
| 309 | printk(KERN_ERR "%s: device_register failed\n", __func__); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 310 | goto del_cdev; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 311 | } |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 312 | /* Remember the cdev's release function */ |
| 313 | vfd->cdev_release = vfd->cdev.kobj.ktype->release; |
| 314 | /* Install our own */ |
| 315 | vfd->cdev.kobj.ktype = &v4l2_ktype_cdev_default; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 316 | return 0; |
| 317 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 318 | del_cdev: |
| 319 | cdev_del(&vfd->cdev); |
| 320 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 321 | fail_minor: |
| 322 | mutex_lock(&videodev_lock); |
| 323 | video_device[vfd->minor] = NULL; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 324 | mutex_unlock(&videodev_lock); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 325 | vfd->minor = -1; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 326 | return ret; |
| 327 | } |
| 328 | EXPORT_SYMBOL(video_register_device_index); |
| 329 | |
| 330 | /** |
| 331 | * video_unregister_device - unregister a video4linux device |
| 332 | * @vfd: the device to unregister |
| 333 | * |
| 334 | * This unregisters the passed device and deassigns the minor |
| 335 | * number. Future open calls will be met with errors. |
| 336 | */ |
| 337 | |
| 338 | void video_unregister_device(struct video_device *vfd) |
| 339 | { |
Hans Verkuil | 22a04f1 | 2008-07-20 06:35:02 -0300 | [diff] [blame] | 340 | device_unregister(&vfd->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 341 | } |
| 342 | EXPORT_SYMBOL(video_unregister_device); |
| 343 | |
| 344 | /* |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 345 | * Initialise video for linux |
| 346 | */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 347 | static int __init videodev_init(void) |
| 348 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 349 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 350 | int ret; |
| 351 | |
| 352 | printk(KERN_INFO "Linux video capture interface: v2.00\n"); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 353 | ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME); |
| 354 | if (ret < 0) { |
| 355 | printk(KERN_WARNING "videodev: unable to get major %d\n", |
| 356 | VIDEO_MAJOR); |
| 357 | return ret; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | ret = class_register(&video_class); |
| 361 | if (ret < 0) { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 362 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 363 | printk(KERN_WARNING "video_dev: class_register failed\n"); |
| 364 | return -EIO; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static void __exit videodev_exit(void) |
| 371 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 372 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
| 373 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 374 | class_unregister(&video_class); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame^] | 375 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | module_init(videodev_init) |
| 379 | module_exit(videodev_exit) |
| 380 | |
| 381 | MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 382 | MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2"); |
| 383 | MODULE_LICENSE("GPL"); |
| 384 | |
| 385 | |
| 386 | /* |
| 387 | * Local variables: |
| 388 | * c-basic-offset: 8 |
| 389 | * End: |
| 390 | */ |