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 | * |
Alan Cox | d9b0144 | 2008-10-27 15:13:47 -0300 | [diff] [blame] | 12 | * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 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> |
Hans Verkuil | 9bea351 | 2008-12-23 07:35:17 -0300 | [diff] [blame] | 33 | #include <media/v4l2-device.h> |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 34 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 35 | |
| 36 | #define VIDEO_NUM_DEVICES 256 |
| 37 | #define VIDEO_NAME "video4linux" |
| 38 | |
| 39 | /* |
| 40 | * sysfs stuff |
| 41 | */ |
| 42 | |
| 43 | static ssize_t show_index(struct device *cd, |
| 44 | struct device_attribute *attr, char *buf) |
| 45 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 46 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 47 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 48 | return sprintf(buf, "%i\n", vdev->index); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static ssize_t show_name(struct device *cd, |
| 52 | struct device_attribute *attr, char *buf) |
| 53 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 54 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 55 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 56 | return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static struct device_attribute video_device_attrs[] = { |
| 60 | __ATTR(name, S_IRUGO, show_name, NULL), |
| 61 | __ATTR(index, S_IRUGO, show_index, NULL), |
| 62 | __ATTR_NULL |
| 63 | }; |
| 64 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 65 | /* |
| 66 | * Active devices |
| 67 | */ |
| 68 | static struct video_device *video_device[VIDEO_NUM_DEVICES]; |
| 69 | static DEFINE_MUTEX(videodev_lock); |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 70 | static DECLARE_BITMAP(video_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 71 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 72 | struct video_device *video_device_alloc(void) |
| 73 | { |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 74 | return kzalloc(sizeof(struct video_device), GFP_KERNEL); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL(video_device_alloc); |
| 77 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 78 | void video_device_release(struct video_device *vdev) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 79 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 80 | kfree(vdev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 81 | } |
| 82 | EXPORT_SYMBOL(video_device_release); |
| 83 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 84 | void video_device_release_empty(struct video_device *vdev) |
Hans Verkuil | f9e86b5 | 2008-08-23 05:47:41 -0300 | [diff] [blame] | 85 | { |
| 86 | /* Do nothing */ |
| 87 | /* Only valid when the video_device struct is a static. */ |
| 88 | } |
| 89 | EXPORT_SYMBOL(video_device_release_empty); |
| 90 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 91 | static inline void video_get(struct video_device *vdev) |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 92 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 93 | get_device(&vdev->dev); |
| 94 | } |
| 95 | |
| 96 | static inline void video_put(struct video_device *vdev) |
| 97 | { |
| 98 | put_device(&vdev->dev); |
| 99 | } |
| 100 | |
| 101 | /* Called when the last user of the video device exits. */ |
| 102 | static void v4l2_device_release(struct device *cd) |
| 103 | { |
| 104 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 105 | |
| 106 | mutex_lock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 107 | if (video_device[vdev->minor] != vdev) { |
Hans Verkuil | 6ea9a18 | 2008-08-30 09:40:47 -0300 | [diff] [blame] | 108 | mutex_unlock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 109 | /* should not happen */ |
| 110 | WARN_ON(1); |
Hans Verkuil | 6ea9a18 | 2008-08-30 09:40:47 -0300 | [diff] [blame] | 111 | return; |
| 112 | } |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 113 | |
| 114 | /* Free up this device for reuse */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 115 | video_device[vdev->minor] = NULL; |
| 116 | |
| 117 | /* Delete the cdev on this minor as well */ |
| 118 | cdev_del(vdev->cdev); |
| 119 | /* Just in case some driver tries to access this from |
| 120 | the release() callback. */ |
| 121 | vdev->cdev = NULL; |
| 122 | |
| 123 | /* Mark minor as free */ |
| 124 | clear_bit(vdev->num, video_nums[vdev->vfl_type]); |
| 125 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 126 | mutex_unlock(&videodev_lock); |
| 127 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 128 | /* Release video_device and perform other |
| 129 | cleanups as needed. */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 130 | vdev->release(vdev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static struct class video_class = { |
| 134 | .name = VIDEO_NAME, |
| 135 | .dev_attrs = video_device_attrs, |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 136 | }; |
| 137 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 138 | struct video_device *video_devdata(struct file *file) |
| 139 | { |
| 140 | return video_device[iminor(file->f_path.dentry->d_inode)]; |
| 141 | } |
| 142 | EXPORT_SYMBOL(video_devdata); |
| 143 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 144 | static ssize_t v4l2_read(struct file *filp, char __user *buf, |
| 145 | size_t sz, loff_t *off) |
| 146 | { |
| 147 | struct video_device *vdev = video_devdata(filp); |
| 148 | |
| 149 | if (!vdev->fops->read) |
| 150 | return -EINVAL; |
| 151 | if (video_is_unregistered(vdev)) |
| 152 | return -EIO; |
| 153 | return vdev->fops->read(filp, buf, sz, off); |
| 154 | } |
| 155 | |
| 156 | static ssize_t v4l2_write(struct file *filp, const char __user *buf, |
| 157 | size_t sz, loff_t *off) |
| 158 | { |
| 159 | struct video_device *vdev = video_devdata(filp); |
| 160 | |
| 161 | if (!vdev->fops->write) |
| 162 | return -EINVAL; |
| 163 | if (video_is_unregistered(vdev)) |
| 164 | return -EIO; |
| 165 | return vdev->fops->write(filp, buf, sz, off); |
| 166 | } |
| 167 | |
| 168 | static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll) |
| 169 | { |
| 170 | struct video_device *vdev = video_devdata(filp); |
| 171 | |
| 172 | if (!vdev->fops->poll || video_is_unregistered(vdev)) |
| 173 | return DEFAULT_POLLMASK; |
| 174 | return vdev->fops->poll(filp, poll); |
| 175 | } |
| 176 | |
| 177 | static int v4l2_ioctl(struct inode *inode, struct file *filp, |
| 178 | unsigned int cmd, unsigned long arg) |
| 179 | { |
| 180 | struct video_device *vdev = video_devdata(filp); |
| 181 | |
| 182 | if (!vdev->fops->ioctl) |
| 183 | return -ENOTTY; |
| 184 | /* Allow ioctl to continue even if the device was unregistered. |
| 185 | Things like dequeueing buffers might still be useful. */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 186 | return vdev->fops->ioctl(filp, cmd, arg); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static long v4l2_unlocked_ioctl(struct file *filp, |
| 190 | unsigned int cmd, unsigned long arg) |
| 191 | { |
| 192 | struct video_device *vdev = video_devdata(filp); |
| 193 | |
| 194 | if (!vdev->fops->unlocked_ioctl) |
| 195 | return -ENOTTY; |
| 196 | /* Allow ioctl to continue even if the device was unregistered. |
| 197 | Things like dequeueing buffers might still be useful. */ |
| 198 | return vdev->fops->unlocked_ioctl(filp, cmd, arg); |
| 199 | } |
| 200 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 201 | static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm) |
| 202 | { |
| 203 | struct video_device *vdev = video_devdata(filp); |
| 204 | |
| 205 | if (!vdev->fops->mmap || |
| 206 | video_is_unregistered(vdev)) |
| 207 | return -ENODEV; |
| 208 | return vdev->fops->mmap(filp, vm); |
| 209 | } |
| 210 | |
| 211 | /* Override for the open function */ |
| 212 | static int v4l2_open(struct inode *inode, struct file *filp) |
| 213 | { |
| 214 | struct video_device *vdev; |
| 215 | int ret; |
| 216 | |
| 217 | /* Check if the video device is available */ |
| 218 | mutex_lock(&videodev_lock); |
| 219 | vdev = video_devdata(filp); |
| 220 | /* return ENODEV if the video device has been removed |
| 221 | already or if it is not registered anymore. */ |
| 222 | if (vdev == NULL || video_is_unregistered(vdev)) { |
| 223 | mutex_unlock(&videodev_lock); |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | /* and increase the device refcount */ |
| 227 | video_get(vdev); |
| 228 | mutex_unlock(&videodev_lock); |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 229 | ret = vdev->fops->open(filp); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 230 | /* decrease the refcount in case of an error */ |
| 231 | if (ret) |
| 232 | video_put(vdev); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /* Override for the release function */ |
| 237 | static int v4l2_release(struct inode *inode, struct file *filp) |
| 238 | { |
| 239 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 240 | int ret = vdev->fops->release(filp); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 241 | |
| 242 | /* decrease the refcount unconditionally since the release() |
| 243 | return value is ignored. */ |
| 244 | video_put(vdev); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static const struct file_operations v4l2_unlocked_fops = { |
| 249 | .owner = THIS_MODULE, |
| 250 | .read = v4l2_read, |
| 251 | .write = v4l2_write, |
| 252 | .open = v4l2_open, |
| 253 | .mmap = v4l2_mmap, |
| 254 | .unlocked_ioctl = v4l2_unlocked_ioctl, |
| 255 | #ifdef CONFIG_COMPAT |
Hans Verkuil | 9bb7cde | 2008-12-30 06:42:40 -0300 | [diff] [blame] | 256 | .compat_ioctl = v4l2_compat_ioctl32, |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 257 | #endif |
| 258 | .release = v4l2_release, |
| 259 | .poll = v4l2_poll, |
| 260 | .llseek = no_llseek, |
| 261 | }; |
| 262 | |
| 263 | static const struct file_operations v4l2_fops = { |
| 264 | .owner = THIS_MODULE, |
| 265 | .read = v4l2_read, |
| 266 | .write = v4l2_write, |
| 267 | .open = v4l2_open, |
| 268 | .mmap = v4l2_mmap, |
| 269 | .ioctl = v4l2_ioctl, |
| 270 | #ifdef CONFIG_COMPAT |
Hans Verkuil | 9bb7cde | 2008-12-30 06:42:40 -0300 | [diff] [blame] | 271 | .compat_ioctl = v4l2_compat_ioctl32, |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 272 | #endif |
| 273 | .release = v4l2_release, |
| 274 | .poll = v4l2_poll, |
| 275 | .llseek = no_llseek, |
| 276 | }; |
| 277 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 278 | /** |
| 279 | * get_index - assign stream number based on parent device |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 280 | * @vdev: video_device to assign index number to, vdev->parent should be assigned |
| 281 | * @num: -1 if auto assign, requested number otherwise |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 282 | * |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 283 | * Note that when this is called the new device has not yet been registered |
| 284 | * in the video_device array. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 285 | * |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 286 | * Returns -ENFILE if num is already in use, a free index number if |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 287 | * successful. |
| 288 | */ |
| 289 | static int get_index(struct video_device *vdev, int num) |
| 290 | { |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 291 | /* This can be static since this function is called with the global |
| 292 | videodev_lock held. */ |
| 293 | static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 294 | int i; |
| 295 | |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 296 | if (num >= VIDEO_NUM_DEVICES) { |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 297 | printk(KERN_ERR "videodev: %s num is too large\n", __func__); |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 301 | /* Some drivers do not set the parent. In that case always return |
| 302 | num or 0. */ |
Hans Verkuil | 806e5b7 | 2008-12-19 09:10:56 -0300 | [diff] [blame] | 303 | if (vdev->parent == NULL) |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 304 | return num >= 0 ? num : 0; |
| 305 | |
| 306 | bitmap_zero(used, VIDEO_NUM_DEVICES); |
Hans Verkuil | 806e5b7 | 2008-12-19 09:10:56 -0300 | [diff] [blame] | 307 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 308 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) { |
| 309 | if (video_device[i] != NULL && |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 310 | video_device[i]->parent == vdev->parent) { |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 311 | set_bit(video_device[i]->index, used); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | if (num >= 0) { |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 316 | if (test_bit(num, used)) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 317 | return -ENFILE; |
| 318 | return num; |
| 319 | } |
| 320 | |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 321 | i = find_first_zero_bit(used, VIDEO_NUM_DEVICES); |
| 322 | return i == VIDEO_NUM_DEVICES ? -ENFILE : i; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 323 | } |
| 324 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 325 | int video_register_device(struct video_device *vdev, int type, int nr) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 326 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 327 | return video_register_device_index(vdev, type, nr, -1); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 328 | } |
| 329 | EXPORT_SYMBOL(video_register_device); |
| 330 | |
| 331 | /** |
Randy Dunlap | edc9189 | 2008-07-28 15:39:38 -0300 | [diff] [blame] | 332 | * video_register_device_index - register video4linux devices |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 333 | * @vdev: video device structure we want to register |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 334 | * @type: type of device to register |
| 335 | * @nr: which device number (0 == /dev/video0, 1 == /dev/video1, ... |
| 336 | * -1 == first free) |
Randy Dunlap | edc9189 | 2008-07-28 15:39:38 -0300 | [diff] [blame] | 337 | * @index: stream number based on parent device; |
| 338 | * -1 if auto assign, requested number otherwise |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 339 | * |
| 340 | * The registration code assigns minor numbers based on the type |
| 341 | * requested. -ENFILE is returned in all the device slots for this |
| 342 | * category are full. If not then the minor field is set and the |
| 343 | * driver initialize function is called (if non %NULL). |
| 344 | * |
| 345 | * Zero is returned on success. |
| 346 | * |
| 347 | * Valid types are |
| 348 | * |
| 349 | * %VFL_TYPE_GRABBER - A frame grabber |
| 350 | * |
| 351 | * %VFL_TYPE_VTX - A teletext device |
| 352 | * |
| 353 | * %VFL_TYPE_VBI - Vertical blank data (undecoded) |
| 354 | * |
| 355 | * %VFL_TYPE_RADIO - A radio card |
| 356 | */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 357 | int video_register_device_index(struct video_device *vdev, int type, int nr, |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 358 | int index) |
| 359 | { |
| 360 | int i = 0; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 361 | int ret; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 362 | int minor_offset = 0; |
| 363 | int minor_cnt = VIDEO_NUM_DEVICES; |
| 364 | const char *name_base; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 365 | void *priv = video_get_drvdata(vdev); |
| 366 | |
| 367 | /* A minor value of -1 marks this video device as never |
| 368 | having been registered */ |
Trent Piepho | 428c8d1 | 2009-03-03 18:51:52 -0300 | [diff] [blame^] | 369 | vdev->minor = -1; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 370 | |
Hans Verkuil | d6e7497 | 2008-08-23 06:27:59 -0300 | [diff] [blame] | 371 | /* the release callback MUST be present */ |
Trent Piepho | 428c8d1 | 2009-03-03 18:51:52 -0300 | [diff] [blame^] | 372 | WARN_ON(!vdev->release); |
| 373 | if (!vdev->release) |
Henrik Kretzschmar | ee7aa9f | 2008-08-22 16:41:03 -0300 | [diff] [blame] | 374 | return -EINVAL; |
| 375 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 376 | /* Part 1: check device type */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 377 | switch (type) { |
| 378 | case VFL_TYPE_GRABBER: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 379 | name_base = "video"; |
| 380 | break; |
| 381 | case VFL_TYPE_VTX: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 382 | name_base = "vtx"; |
| 383 | break; |
| 384 | case VFL_TYPE_VBI: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 385 | name_base = "vbi"; |
| 386 | break; |
| 387 | case VFL_TYPE_RADIO: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 388 | name_base = "radio"; |
| 389 | break; |
| 390 | default: |
| 391 | printk(KERN_ERR "%s called with unknown type: %d\n", |
| 392 | __func__, type); |
Henrik Kretzschmar | 46f2c21 | 2008-09-03 16:47:39 -0300 | [diff] [blame] | 393 | return -EINVAL; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 394 | } |
| 395 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 396 | vdev->vfl_type = type; |
| 397 | vdev->cdev = NULL; |
Hans Verkuil | 9bea351 | 2008-12-23 07:35:17 -0300 | [diff] [blame] | 398 | if (vdev->v4l2_dev) |
| 399 | vdev->parent = vdev->v4l2_dev->dev; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 400 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 401 | /* Part 2: find a free minor, kernel number and device index. */ |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 402 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
| 403 | /* Keep the ranges for the first four types for historical |
| 404 | * reasons. |
| 405 | * Newer devices (not yet in place) should use the range |
| 406 | * of 128-191 and just pick the first free minor there |
| 407 | * (new style). */ |
| 408 | switch (type) { |
| 409 | case VFL_TYPE_GRABBER: |
| 410 | minor_offset = 0; |
| 411 | minor_cnt = 64; |
| 412 | break; |
| 413 | case VFL_TYPE_RADIO: |
| 414 | minor_offset = 64; |
| 415 | minor_cnt = 64; |
| 416 | break; |
| 417 | case VFL_TYPE_VTX: |
| 418 | minor_offset = 192; |
| 419 | minor_cnt = 32; |
| 420 | break; |
| 421 | case VFL_TYPE_VBI: |
| 422 | minor_offset = 224; |
| 423 | minor_cnt = 32; |
| 424 | break; |
| 425 | default: |
| 426 | minor_offset = 128; |
| 427 | minor_cnt = 64; |
| 428 | break; |
| 429 | } |
| 430 | #endif |
| 431 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 432 | /* Pick a minor number */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 433 | mutex_lock(&videodev_lock); |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 434 | nr = find_next_zero_bit(video_nums[type], minor_cnt, nr == -1 ? 0 : nr); |
| 435 | if (nr == minor_cnt) |
| 436 | nr = find_first_zero_bit(video_nums[type], minor_cnt); |
| 437 | if (nr == minor_cnt) { |
| 438 | printk(KERN_ERR "could not get a free kernel number\n"); |
| 439 | mutex_unlock(&videodev_lock); |
| 440 | return -ENFILE; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 441 | } |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 442 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
| 443 | /* 1-on-1 mapping of kernel number to minor number */ |
| 444 | i = nr; |
| 445 | #else |
| 446 | /* The kernel number and minor numbers are independent */ |
| 447 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) |
| 448 | if (video_device[i] == NULL) |
| 449 | break; |
| 450 | if (i == VIDEO_NUM_DEVICES) { |
| 451 | mutex_unlock(&videodev_lock); |
| 452 | printk(KERN_ERR "could not get a free minor\n"); |
| 453 | return -ENFILE; |
| 454 | } |
| 455 | #endif |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 456 | vdev->minor = i + minor_offset; |
| 457 | vdev->num = nr; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 458 | set_bit(nr, video_nums[type]); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 459 | /* Should not happen since we thought this minor was free */ |
| 460 | WARN_ON(video_device[vdev->minor] != NULL); |
| 461 | ret = vdev->index = get_index(vdev, index); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 462 | mutex_unlock(&videodev_lock); |
| 463 | |
| 464 | if (ret < 0) { |
| 465 | printk(KERN_ERR "%s: get_index failed\n", __func__); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 466 | goto cleanup; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 467 | } |
| 468 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 469 | /* Part 3: Initialize the character device */ |
| 470 | vdev->cdev = cdev_alloc(); |
| 471 | if (vdev->cdev == NULL) { |
| 472 | ret = -ENOMEM; |
| 473 | goto cleanup; |
| 474 | } |
| 475 | if (vdev->fops->unlocked_ioctl) |
| 476 | vdev->cdev->ops = &v4l2_unlocked_fops; |
| 477 | else |
| 478 | vdev->cdev->ops = &v4l2_fops; |
| 479 | vdev->cdev->owner = vdev->fops->owner; |
| 480 | ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 481 | if (ret < 0) { |
| 482 | printk(KERN_ERR "%s: cdev_add failed\n", __func__); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 483 | kfree(vdev->cdev); |
| 484 | vdev->cdev = NULL; |
| 485 | goto cleanup; |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 486 | } |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 487 | |
| 488 | /* Part 4: register the device with sysfs */ |
| 489 | memset(&vdev->dev, 0, sizeof(vdev->dev)); |
Hans Verkuil | 9d95af9 | 2008-08-24 11:18:47 -0300 | [diff] [blame] | 490 | /* The memset above cleared the device's drvdata, so |
| 491 | put back the copy we made earlier. */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 492 | video_set_drvdata(vdev, priv); |
| 493 | vdev->dev.class = &video_class; |
| 494 | vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor); |
| 495 | if (vdev->parent) |
| 496 | vdev->dev.parent = vdev->parent; |
| 497 | dev_set_name(&vdev->dev, "%s%d", name_base, nr); |
| 498 | ret = device_register(&vdev->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 499 | if (ret < 0) { |
| 500 | printk(KERN_ERR "%s: device_register failed\n", __func__); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 501 | goto cleanup; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 502 | } |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 503 | /* Register the release callback that will be called when the last |
| 504 | reference to the device goes away. */ |
| 505 | vdev->dev.release = v4l2_device_release; |
| 506 | |
| 507 | /* Part 5: Activate this minor. The char device can now be used. */ |
| 508 | mutex_lock(&videodev_lock); |
| 509 | video_device[vdev->minor] = vdev; |
| 510 | mutex_unlock(&videodev_lock); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 511 | return 0; |
| 512 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 513 | cleanup: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 514 | mutex_lock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 515 | if (vdev->cdev) |
| 516 | cdev_del(vdev->cdev); |
| 517 | clear_bit(vdev->num, video_nums[type]); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 518 | mutex_unlock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 519 | /* Mark this video device as never having been registered. */ |
| 520 | vdev->minor = -1; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 521 | return ret; |
| 522 | } |
| 523 | EXPORT_SYMBOL(video_register_device_index); |
| 524 | |
| 525 | /** |
| 526 | * video_unregister_device - unregister a video4linux device |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 527 | * @vdev: the device to unregister |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 528 | * |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 529 | * This unregisters the passed device. Future open calls will |
| 530 | * be met with errors. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 531 | */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 532 | void video_unregister_device(struct video_device *vdev) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 533 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 534 | /* Check if vdev was ever registered at all */ |
| 535 | if (!vdev || vdev->minor < 0) |
| 536 | return; |
| 537 | |
| 538 | mutex_lock(&videodev_lock); |
| 539 | set_bit(V4L2_FL_UNREGISTERED, &vdev->flags); |
| 540 | mutex_unlock(&videodev_lock); |
| 541 | device_unregister(&vdev->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 542 | } |
| 543 | EXPORT_SYMBOL(video_unregister_device); |
| 544 | |
| 545 | /* |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 546 | * Initialise video for linux |
| 547 | */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 548 | static int __init videodev_init(void) |
| 549 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 550 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 551 | int ret; |
| 552 | |
| 553 | printk(KERN_INFO "Linux video capture interface: v2.00\n"); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 554 | ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME); |
| 555 | if (ret < 0) { |
| 556 | printk(KERN_WARNING "videodev: unable to get major %d\n", |
| 557 | VIDEO_MAJOR); |
| 558 | return ret; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | ret = class_register(&video_class); |
| 562 | if (ret < 0) { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 563 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 564 | printk(KERN_WARNING "video_dev: class_register failed\n"); |
| 565 | return -EIO; |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static void __exit videodev_exit(void) |
| 572 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 573 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
| 574 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 575 | class_unregister(&video_class); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 576 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | module_init(videodev_init) |
| 580 | module_exit(videodev_exit) |
| 581 | |
| 582 | MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 583 | MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2"); |
| 584 | MODULE_LICENSE("GPL"); |
| 585 | |
| 586 | |
| 587 | /* |
| 588 | * Local variables: |
| 589 | * c-basic-offset: 8 |
| 590 | * End: |
| 591 | */ |