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> |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 28 | #include <asm/uaccess.h> |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 29 | |
| 30 | #include <media/v4l2-common.h> |
Hans Verkuil | 9bea351 | 2008-12-23 07:35:17 -0300 | [diff] [blame] | 31 | #include <media/v4l2-device.h> |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 32 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 33 | |
| 34 | #define VIDEO_NUM_DEVICES 256 |
| 35 | #define VIDEO_NAME "video4linux" |
| 36 | |
| 37 | /* |
| 38 | * sysfs stuff |
| 39 | */ |
| 40 | |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 41 | static ssize_t index_show(struct device *cd, |
| 42 | struct device_attribute *attr, char *buf) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 43 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 44 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 45 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 46 | return sprintf(buf, "%i\n", vdev->index); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 47 | } |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 48 | static DEVICE_ATTR_RO(index); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 49 | |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 50 | static ssize_t debug_show(struct device *cd, |
| 51 | struct device_attribute *attr, char *buf) |
Hans Verkuil | 80131fe0 | 2012-06-22 06:37:38 -0300 | [diff] [blame] | 52 | { |
| 53 | struct video_device *vdev = to_video_device(cd); |
| 54 | |
| 55 | return sprintf(buf, "%i\n", vdev->debug); |
| 56 | } |
| 57 | |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 58 | static ssize_t debug_store(struct device *cd, struct device_attribute *attr, |
| 59 | const char *buf, size_t len) |
Hans Verkuil | 80131fe0 | 2012-06-22 06:37:38 -0300 | [diff] [blame] | 60 | { |
| 61 | struct video_device *vdev = to_video_device(cd); |
| 62 | int res = 0; |
| 63 | u16 value; |
| 64 | |
| 65 | res = kstrtou16(buf, 0, &value); |
| 66 | if (res) |
| 67 | return res; |
| 68 | |
| 69 | vdev->debug = value; |
| 70 | return len; |
| 71 | } |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 72 | static DEVICE_ATTR_RW(debug); |
Hans Verkuil | 80131fe0 | 2012-06-22 06:37:38 -0300 | [diff] [blame] | 73 | |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 74 | static ssize_t name_show(struct device *cd, |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 75 | struct device_attribute *attr, char *buf) |
| 76 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 77 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 78 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 79 | return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 80 | } |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 81 | static DEVICE_ATTR_RO(name); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 82 | |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 83 | static struct attribute *video_device_attrs[] = { |
| 84 | &dev_attr_name.attr, |
| 85 | &dev_attr_debug.attr, |
| 86 | &dev_attr_index.attr, |
| 87 | NULL, |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 88 | }; |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 89 | ATTRIBUTE_GROUPS(video_device); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 90 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 91 | /* |
| 92 | * Active devices |
| 93 | */ |
| 94 | static struct video_device *video_device[VIDEO_NUM_DEVICES]; |
| 95 | static DEFINE_MUTEX(videodev_lock); |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 96 | static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 97 | |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 98 | /* Device node utility functions */ |
| 99 | |
| 100 | /* Note: these utility functions all assume that vfl_type is in the range |
| 101 | [0, VFL_TYPE_MAX-1]. */ |
| 102 | |
| 103 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
| 104 | /* Return the bitmap corresponding to vfl_type. */ |
| 105 | static inline unsigned long *devnode_bits(int vfl_type) |
| 106 | { |
| 107 | /* Any types not assigned to fixed minor ranges must be mapped to |
| 108 | one single bitmap for the purposes of finding a free node number |
| 109 | since all those unassigned types use the same minor range. */ |
Hans Verkuil | 226c0ee | 2010-08-06 12:48:00 -0300 | [diff] [blame] | 110 | int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type; |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 111 | |
| 112 | return devnode_nums[idx]; |
| 113 | } |
| 114 | #else |
| 115 | /* Return the bitmap corresponding to vfl_type. */ |
| 116 | static inline unsigned long *devnode_bits(int vfl_type) |
| 117 | { |
| 118 | return devnode_nums[vfl_type]; |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | /* Mark device node number vdev->num as used */ |
| 123 | static inline void devnode_set(struct video_device *vdev) |
| 124 | { |
| 125 | set_bit(vdev->num, devnode_bits(vdev->vfl_type)); |
| 126 | } |
| 127 | |
| 128 | /* Mark device node number vdev->num as unused */ |
| 129 | static inline void devnode_clear(struct video_device *vdev) |
| 130 | { |
| 131 | clear_bit(vdev->num, devnode_bits(vdev->vfl_type)); |
| 132 | } |
| 133 | |
| 134 | /* Try to find a free device node number in the range [from, to> */ |
| 135 | static inline int devnode_find(struct video_device *vdev, int from, int to) |
| 136 | { |
| 137 | return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from); |
| 138 | } |
| 139 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 140 | struct video_device *video_device_alloc(void) |
| 141 | { |
Hans Verkuil | bfa8a27 | 2008-08-23 07:48:38 -0300 | [diff] [blame] | 142 | return kzalloc(sizeof(struct video_device), GFP_KERNEL); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 143 | } |
| 144 | EXPORT_SYMBOL(video_device_alloc); |
| 145 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 146 | void video_device_release(struct video_device *vdev) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 147 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 148 | kfree(vdev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 149 | } |
| 150 | EXPORT_SYMBOL(video_device_release); |
| 151 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 152 | void video_device_release_empty(struct video_device *vdev) |
Hans Verkuil | f9e86b5 | 2008-08-23 05:47:41 -0300 | [diff] [blame] | 153 | { |
| 154 | /* Do nothing */ |
| 155 | /* Only valid when the video_device struct is a static. */ |
| 156 | } |
| 157 | EXPORT_SYMBOL(video_device_release_empty); |
| 158 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 159 | static inline void video_get(struct video_device *vdev) |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 160 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 161 | get_device(&vdev->dev); |
| 162 | } |
| 163 | |
| 164 | static inline void video_put(struct video_device *vdev) |
| 165 | { |
| 166 | put_device(&vdev->dev); |
| 167 | } |
| 168 | |
| 169 | /* Called when the last user of the video device exits. */ |
| 170 | static void v4l2_device_release(struct device *cd) |
| 171 | { |
| 172 | struct video_device *vdev = to_video_device(cd); |
Hans Verkuil | bedf8bc | 2011-03-12 06:37:19 -0300 | [diff] [blame] | 173 | struct v4l2_device *v4l2_dev = vdev->v4l2_dev; |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 174 | |
| 175 | mutex_lock(&videodev_lock); |
Guennadi Liakhovetski | 1fc2b5f | 2011-12-06 05:33:03 -0300 | [diff] [blame] | 176 | if (WARN_ON(video_device[vdev->minor] != vdev)) { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 177 | /* should not happen */ |
Guennadi Liakhovetski | 1fc2b5f | 2011-12-06 05:33:03 -0300 | [diff] [blame] | 178 | mutex_unlock(&videodev_lock); |
Hans Verkuil | 6ea9a18 | 2008-08-30 09:40:47 -0300 | [diff] [blame] | 179 | return; |
| 180 | } |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 181 | |
| 182 | /* Free up this device for reuse */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 183 | video_device[vdev->minor] = NULL; |
| 184 | |
| 185 | /* Delete the cdev on this minor as well */ |
| 186 | cdev_del(vdev->cdev); |
| 187 | /* Just in case some driver tries to access this from |
| 188 | the release() callback. */ |
| 189 | vdev->cdev = NULL; |
| 190 | |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 191 | /* Mark device node number as free */ |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 192 | devnode_clear(vdev); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 193 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 194 | mutex_unlock(&videodev_lock); |
| 195 | |
Laurent Pinchart | c064b8e | 2011-06-15 05:20:34 -0300 | [diff] [blame] | 196 | #if defined(CONFIG_MEDIA_CONTROLLER) |
Guennadi Liakhovetski | 1fc2b5f | 2011-12-06 05:33:03 -0300 | [diff] [blame] | 197 | if (v4l2_dev && v4l2_dev->mdev && |
Laurent Pinchart | c064b8e | 2011-06-15 05:20:34 -0300 | [diff] [blame] | 198 | vdev->vfl_type != VFL_TYPE_SUBDEV) |
| 199 | media_device_unregister_entity(&vdev->entity); |
| 200 | #endif |
| 201 | |
Hans Verkuil | 8280b66 | 2011-09-06 10:23:18 -0300 | [diff] [blame] | 202 | /* Do not call v4l2_device_put if there is no release callback set. |
| 203 | * Drivers that have no v4l2_device release callback might free the |
| 204 | * v4l2_dev instance in the video_device release callback below, so we |
| 205 | * must perform this check here. |
| 206 | * |
| 207 | * TODO: In the long run all drivers that use v4l2_device should use the |
| 208 | * v4l2_device release callback. This check will then be unnecessary. |
| 209 | */ |
Antonio Ospite | e58fced | 2011-10-12 17:59:26 -0300 | [diff] [blame] | 210 | if (v4l2_dev && v4l2_dev->release == NULL) |
Hans Verkuil | 8280b66 | 2011-09-06 10:23:18 -0300 | [diff] [blame] | 211 | v4l2_dev = NULL; |
| 212 | |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 213 | /* Release video_device and perform other |
| 214 | cleanups as needed. */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 215 | vdev->release(vdev); |
Hans Verkuil | bedf8bc | 2011-03-12 06:37:19 -0300 | [diff] [blame] | 216 | |
| 217 | /* Decrease v4l2_device refcount */ |
| 218 | if (v4l2_dev) |
| 219 | v4l2_device_put(v4l2_dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static struct class video_class = { |
| 223 | .name = VIDEO_NAME, |
Greg Kroah-Hartman | 13e2237 | 2013-07-24 15:05:14 -0700 | [diff] [blame] | 224 | .dev_groups = video_device_groups, |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 225 | }; |
| 226 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 227 | struct video_device *video_devdata(struct file *file) |
| 228 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 229 | return video_device[iminor(file_inode(file))]; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 230 | } |
| 231 | EXPORT_SYMBOL(video_devdata); |
| 232 | |
Hans Verkuil | 0226549 | 2010-12-29 10:05:02 -0300 | [diff] [blame] | 233 | |
| 234 | /* Priority handling */ |
| 235 | |
| 236 | static inline bool prio_is_valid(enum v4l2_priority prio) |
| 237 | { |
| 238 | return prio == V4L2_PRIORITY_BACKGROUND || |
| 239 | prio == V4L2_PRIORITY_INTERACTIVE || |
| 240 | prio == V4L2_PRIORITY_RECORD; |
| 241 | } |
| 242 | |
| 243 | void v4l2_prio_init(struct v4l2_prio_state *global) |
| 244 | { |
| 245 | memset(global, 0, sizeof(*global)); |
| 246 | } |
| 247 | EXPORT_SYMBOL(v4l2_prio_init); |
| 248 | |
| 249 | int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local, |
| 250 | enum v4l2_priority new) |
| 251 | { |
| 252 | if (!prio_is_valid(new)) |
| 253 | return -EINVAL; |
| 254 | if (*local == new) |
| 255 | return 0; |
| 256 | |
| 257 | atomic_inc(&global->prios[new]); |
| 258 | if (prio_is_valid(*local)) |
| 259 | atomic_dec(&global->prios[*local]); |
| 260 | *local = new; |
| 261 | return 0; |
| 262 | } |
| 263 | EXPORT_SYMBOL(v4l2_prio_change); |
| 264 | |
| 265 | void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local) |
| 266 | { |
| 267 | v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT); |
| 268 | } |
| 269 | EXPORT_SYMBOL(v4l2_prio_open); |
| 270 | |
| 271 | void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local) |
| 272 | { |
| 273 | if (prio_is_valid(local)) |
| 274 | atomic_dec(&global->prios[local]); |
| 275 | } |
| 276 | EXPORT_SYMBOL(v4l2_prio_close); |
| 277 | |
| 278 | enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global) |
| 279 | { |
| 280 | if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0) |
| 281 | return V4L2_PRIORITY_RECORD; |
| 282 | if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0) |
| 283 | return V4L2_PRIORITY_INTERACTIVE; |
| 284 | if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0) |
| 285 | return V4L2_PRIORITY_BACKGROUND; |
| 286 | return V4L2_PRIORITY_UNSET; |
| 287 | } |
| 288 | EXPORT_SYMBOL(v4l2_prio_max); |
| 289 | |
| 290 | int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local) |
| 291 | { |
| 292 | return (local < v4l2_prio_max(global)) ? -EBUSY : 0; |
| 293 | } |
| 294 | EXPORT_SYMBOL(v4l2_prio_check); |
| 295 | |
| 296 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 297 | static ssize_t v4l2_read(struct file *filp, char __user *buf, |
| 298 | size_t sz, loff_t *off) |
| 299 | { |
| 300 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | 2877842 | 2010-11-26 06:43:51 -0300 | [diff] [blame] | 301 | int ret = -ENODEV; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 302 | |
| 303 | if (!vdev->fops->read) |
| 304 | return -EINVAL; |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 305 | if (video_is_registered(vdev)) |
| 306 | ret = vdev->fops->read(filp, buf, sz, off); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 307 | if (vdev->debug) |
| 308 | printk(KERN_DEBUG "%s: read: %zd (%d)\n", |
| 309 | video_device_node_name(vdev), sz, ret); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 310 | return ret; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static ssize_t v4l2_write(struct file *filp, const char __user *buf, |
| 314 | size_t sz, loff_t *off) |
| 315 | { |
| 316 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | 2877842 | 2010-11-26 06:43:51 -0300 | [diff] [blame] | 317 | int ret = -ENODEV; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 318 | |
| 319 | if (!vdev->fops->write) |
| 320 | return -EINVAL; |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 321 | if (video_is_registered(vdev)) |
| 322 | ret = vdev->fops->write(filp, buf, sz, off); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 323 | if (vdev->debug) |
| 324 | printk(KERN_DEBUG "%s: write: %zd (%d)\n", |
| 325 | video_device_node_name(vdev), sz, ret); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 326 | return ret; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll) |
| 330 | { |
| 331 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 332 | unsigned int res = POLLERR | POLLHUP; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 333 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 334 | if (!vdev->fops->poll) |
Hans Verkuil | 2877842 | 2010-11-26 06:43:51 -0300 | [diff] [blame] | 335 | return DEFAULT_POLLMASK; |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 336 | if (video_is_registered(vdev)) |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 337 | res = vdev->fops->poll(filp, poll); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 338 | if (vdev->debug) |
| 339 | printk(KERN_DEBUG "%s: poll: %08x\n", |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 340 | video_device_node_name(vdev), res); |
| 341 | return res; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 342 | } |
| 343 | |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 344 | static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 345 | { |
| 346 | struct video_device *vdev = video_devdata(filp); |
Mauro Carvalho Chehab | 7242063 | 2010-10-09 16:43:40 -0300 | [diff] [blame] | 347 | int ret = -ENODEV; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 348 | |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 349 | if (vdev->fops->unlocked_ioctl) { |
Hans Verkuil | 5a5adf6 | 2012-06-22 07:29:35 -0300 | [diff] [blame] | 350 | struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd); |
Hans Verkuil | 8ab75e3 | 2012-05-10 02:51:31 -0300 | [diff] [blame] | 351 | |
Hans Verkuil | 5a5adf6 | 2012-06-22 07:29:35 -0300 | [diff] [blame] | 352 | if (lock && mutex_lock_interruptible(lock)) |
| 353 | return -ERESTARTSYS; |
Mauro Carvalho Chehab | 7242063 | 2010-10-09 16:43:40 -0300 | [diff] [blame] | 354 | if (video_is_registered(vdev)) |
| 355 | ret = vdev->fops->unlocked_ioctl(filp, cmd, arg); |
Hans Verkuil | 5a5adf6 | 2012-06-22 07:29:35 -0300 | [diff] [blame] | 356 | if (lock) |
| 357 | mutex_unlock(lock); |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 358 | } else if (vdev->fops->ioctl) { |
Hans Verkuil | 879aa24 | 2010-11-26 06:47:28 -0300 | [diff] [blame] | 359 | /* This code path is a replacement for the BKL. It is a major |
| 360 | * hack but it will have to do for those drivers that are not |
| 361 | * yet converted to use unlocked_ioctl. |
| 362 | * |
| 363 | * There are two options: if the driver implements struct |
| 364 | * v4l2_device, then the lock defined there is used to |
| 365 | * serialize the ioctls. Otherwise the v4l2 core lock defined |
| 366 | * below is used. This lock is really bad since it serializes |
| 367 | * completely independent devices. |
| 368 | * |
| 369 | * Both variants suffer from the same problem: if the driver |
| 370 | * sleeps, then it blocks all ioctls since the lock is still |
| 371 | * held. This is very common for VIDIOC_DQBUF since that |
| 372 | * normally waits for a frame to arrive. As a result any other |
| 373 | * ioctl calls will proceed very, very slowly since each call |
| 374 | * will have to wait for the VIDIOC_QBUF to finish. Things that |
| 375 | * should take 0.01s may now take 10-20 seconds. |
| 376 | * |
| 377 | * The workaround is to *not* take the lock for VIDIOC_DQBUF. |
| 378 | * This actually works OK for videobuf-based drivers, since |
| 379 | * videobuf will take its own internal lock. |
| 380 | */ |
Arnd Bergmann | 0edf2e5 | 2010-10-27 09:30:32 -0300 | [diff] [blame] | 381 | static DEFINE_MUTEX(v4l2_ioctl_mutex); |
Hans Verkuil | 879aa24 | 2010-11-26 06:47:28 -0300 | [diff] [blame] | 382 | struct mutex *m = vdev->v4l2_dev ? |
| 383 | &vdev->v4l2_dev->ioctl_lock : &v4l2_ioctl_mutex; |
Arnd Bergmann | 0edf2e5 | 2010-10-27 09:30:32 -0300 | [diff] [blame] | 384 | |
Hans Verkuil | 879aa24 | 2010-11-26 06:47:28 -0300 | [diff] [blame] | 385 | if (cmd != VIDIOC_DQBUF && mutex_lock_interruptible(m)) |
| 386 | return -ERESTARTSYS; |
Mauro Carvalho Chehab | 7242063 | 2010-10-09 16:43:40 -0300 | [diff] [blame] | 387 | if (video_is_registered(vdev)) |
| 388 | ret = vdev->fops->ioctl(filp, cmd, arg); |
Hans Verkuil | 879aa24 | 2010-11-26 06:47:28 -0300 | [diff] [blame] | 389 | if (cmd != VIDIOC_DQBUF) |
| 390 | mutex_unlock(m); |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 391 | } else |
| 392 | ret = -ENOTTY; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 393 | |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 394 | return ret; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 395 | } |
| 396 | |
Bob Liu | ecc6517 | 2011-05-06 05:20:09 -0300 | [diff] [blame] | 397 | #ifdef CONFIG_MMU |
| 398 | #define v4l2_get_unmapped_area NULL |
| 399 | #else |
| 400 | static unsigned long v4l2_get_unmapped_area(struct file *filp, |
| 401 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 402 | unsigned long flags) |
| 403 | { |
| 404 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 405 | int ret; |
Bob Liu | ecc6517 | 2011-05-06 05:20:09 -0300 | [diff] [blame] | 406 | |
| 407 | if (!vdev->fops->get_unmapped_area) |
| 408 | return -ENOSYS; |
| 409 | if (!video_is_registered(vdev)) |
| 410 | return -ENODEV; |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 411 | ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags); |
| 412 | if (vdev->debug) |
| 413 | printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n", |
| 414 | video_device_node_name(vdev), ret); |
| 415 | return ret; |
Bob Liu | ecc6517 | 2011-05-06 05:20:09 -0300 | [diff] [blame] | 416 | } |
| 417 | #endif |
| 418 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 419 | static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm) |
| 420 | { |
| 421 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 422 | int ret = -ENODEV; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 423 | |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 424 | if (!vdev->fops->mmap) |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 425 | return -ENODEV; |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 426 | if (video_is_registered(vdev)) |
| 427 | ret = vdev->fops->mmap(filp, vm); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 428 | if (vdev->debug) |
| 429 | printk(KERN_DEBUG "%s: mmap (%d)\n", |
| 430 | video_device_node_name(vdev), ret); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 431 | return ret; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Override for the open function */ |
| 435 | static int v4l2_open(struct inode *inode, struct file *filp) |
| 436 | { |
| 437 | struct video_device *vdev; |
Hans Verkuil | 65d9ff9 | 2009-03-29 20:19:38 -0300 | [diff] [blame] | 438 | int ret = 0; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 439 | |
| 440 | /* Check if the video device is available */ |
| 441 | mutex_lock(&videodev_lock); |
| 442 | vdev = video_devdata(filp); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 443 | /* return ENODEV if the video device has already been removed. */ |
Hans Verkuil | ca9afe6 | 2010-11-26 06:54:53 -0300 | [diff] [blame] | 444 | if (vdev == NULL || !video_is_registered(vdev)) { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 445 | mutex_unlock(&videodev_lock); |
| 446 | return -ENODEV; |
| 447 | } |
| 448 | /* and increase the device refcount */ |
| 449 | video_get(vdev); |
| 450 | mutex_unlock(&videodev_lock); |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 451 | if (vdev->fops->open) { |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 452 | if (video_is_registered(vdev)) |
| 453 | ret = vdev->fops->open(filp); |
| 454 | else |
| 455 | ret = -ENODEV; |
Hans Verkuil | ee6869a | 2010-09-26 08:47:38 -0300 | [diff] [blame] | 456 | } |
Hans Verkuil | 65d9ff9 | 2009-03-29 20:19:38 -0300 | [diff] [blame] | 457 | |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 458 | if (vdev->debug) |
| 459 | printk(KERN_DEBUG "%s: open (%d)\n", |
| 460 | video_device_node_name(vdev), ret); |
Ezequiel GarcÃa | 8f695d3 | 2012-07-26 07:59:04 -0300 | [diff] [blame] | 461 | /* decrease the refcount in case of an error */ |
| 462 | if (ret) |
| 463 | video_put(vdev); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | /* Override for the release function */ |
| 468 | static int v4l2_release(struct inode *inode, struct file *filp) |
| 469 | { |
| 470 | struct video_device *vdev = video_devdata(filp); |
Hans Verkuil | 65d9ff9 | 2009-03-29 20:19:38 -0300 | [diff] [blame] | 471 | int ret = 0; |
| 472 | |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 473 | if (vdev->fops->release) |
| 474 | ret = vdev->fops->release(filp); |
Hans Verkuil | cc4b7e7 | 2012-06-09 12:13:29 -0300 | [diff] [blame] | 475 | if (vdev->debug) |
| 476 | printk(KERN_DEBUG "%s: release\n", |
| 477 | video_device_node_name(vdev)); |
Hans Verkuil | cf53373 | 2012-07-31 04:02:25 -0300 | [diff] [blame] | 478 | |
Ezequiel GarcÃa | 8f695d3 | 2012-07-26 07:59:04 -0300 | [diff] [blame] | 479 | /* decrease the refcount unconditionally since the release() |
| 480 | return value is ignored. */ |
| 481 | video_put(vdev); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 482 | return ret; |
| 483 | } |
| 484 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 485 | static const struct file_operations v4l2_fops = { |
| 486 | .owner = THIS_MODULE, |
| 487 | .read = v4l2_read, |
| 488 | .write = v4l2_write, |
| 489 | .open = v4l2_open, |
Bob Liu | ecc6517 | 2011-05-06 05:20:09 -0300 | [diff] [blame] | 490 | .get_unmapped_area = v4l2_get_unmapped_area, |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 491 | .mmap = v4l2_mmap, |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 492 | .unlocked_ioctl = v4l2_ioctl, |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 493 | #ifdef CONFIG_COMPAT |
Hans Verkuil | 9bb7cde | 2008-12-30 06:42:40 -0300 | [diff] [blame] | 494 | .compat_ioctl = v4l2_compat_ioctl32, |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 495 | #endif |
| 496 | .release = v4l2_release, |
| 497 | .poll = v4l2_poll, |
| 498 | .llseek = no_llseek, |
| 499 | }; |
| 500 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 501 | /** |
Hans Verkuil | 1c1d86a | 2013-06-12 11:15:12 -0300 | [diff] [blame] | 502 | * get_index - assign stream index number based on v4l2_dev |
| 503 | * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 504 | * |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 505 | * Note that when this is called the new device has not yet been registered |
Hans Verkuil | 7ae0cd9 | 2009-06-19 11:32:56 -0300 | [diff] [blame] | 506 | * in the video_device array, but it was able to obtain a minor number. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 507 | * |
Hans Verkuil | 7ae0cd9 | 2009-06-19 11:32:56 -0300 | [diff] [blame] | 508 | * This means that we can always obtain a free stream index number since |
| 509 | * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in |
| 510 | * use of the video_device array. |
| 511 | * |
| 512 | * Returns a free index number. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 513 | */ |
Hans Verkuil | 7ae0cd9 | 2009-06-19 11:32:56 -0300 | [diff] [blame] | 514 | static int get_index(struct video_device *vdev) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 515 | { |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 516 | /* This can be static since this function is called with the global |
| 517 | videodev_lock held. */ |
| 518 | static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 519 | int i; |
| 520 | |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 521 | bitmap_zero(used, VIDEO_NUM_DEVICES); |
Hans Verkuil | 806e5b7 | 2008-12-19 09:10:56 -0300 | [diff] [blame] | 522 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 523 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) { |
| 524 | if (video_device[i] != NULL && |
Hans Verkuil | 1c1d86a | 2013-06-12 11:15:12 -0300 | [diff] [blame] | 525 | video_device[i]->v4l2_dev == vdev->v4l2_dev) { |
Hans Verkuil | 775a05d | 2009-02-14 11:31:01 -0300 | [diff] [blame] | 526 | set_bit(video_device[i]->index, used); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Hans Verkuil | 7ae0cd9 | 2009-06-19 11:32:56 -0300 | [diff] [blame] | 530 | return find_first_zero_bit(used, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 531 | } |
| 532 | |
Hans Verkuil | 48ea0be | 2012-05-10 05:36:00 -0300 | [diff] [blame] | 533 | #define SET_VALID_IOCTL(ops, cmd, op) \ |
| 534 | if (ops->op) \ |
| 535 | set_bit(_IOC_NR(cmd), valid_ioctls) |
| 536 | |
| 537 | /* This determines which ioctls are actually implemented in the driver. |
| 538 | It's a one-time thing which simplifies video_ioctl2 as it can just do |
| 539 | a bit test. |
| 540 | |
| 541 | Note that drivers can override this by setting bits to 1 in |
| 542 | vdev->valid_ioctls. If an ioctl is marked as 1 when this function is |
| 543 | called, then that ioctl will actually be marked as unimplemented. |
| 544 | |
| 545 | It does that by first setting up the local valid_ioctls bitmap, and |
| 546 | at the end do a: |
| 547 | |
| 548 | vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls) |
| 549 | */ |
| 550 | static void determine_valid_ioctls(struct video_device *vdev) |
| 551 | { |
| 552 | DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE); |
| 553 | const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops; |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 554 | bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER; |
| 555 | bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI; |
Hans Verkuil | bfffd74 | 2013-12-20 02:32:21 -0300 | [diff] [blame] | 556 | bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO; |
Antti Palosaari | 582c52c | 2013-12-12 13:44:14 -0300 | [diff] [blame] | 557 | bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR; |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 558 | bool is_rx = vdev->vfl_dir != VFL_DIR_TX; |
| 559 | bool is_tx = vdev->vfl_dir != VFL_DIR_RX; |
Hans Verkuil | 48ea0be | 2012-05-10 05:36:00 -0300 | [diff] [blame] | 560 | |
| 561 | bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE); |
| 562 | |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 563 | /* vfl_type and vfl_dir independent ioctls */ |
| 564 | |
Hans Verkuil | 48ea0be | 2012-05-10 05:36:00 -0300 | [diff] [blame] | 565 | SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap); |
| 566 | if (ops->vidioc_g_priority || |
| 567 | test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags)) |
| 568 | set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls); |
| 569 | if (ops->vidioc_s_priority || |
| 570 | test_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags)) |
| 571 | set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 572 | SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon); |
| 573 | SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff); |
| 574 | /* Note: the control handler can also be passed through the filehandle, |
| 575 | and that can't be tested here. If the bit for these control ioctls |
| 576 | is set, then the ioctl is valid. But if it is 0, then it can still |
| 577 | be valid if the filehandle passed the control handler. */ |
| 578 | if (vdev->ctrl_handler || ops->vidioc_queryctrl) |
| 579 | set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls); |
| 580 | if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls) |
| 581 | set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls); |
| 582 | if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls) |
| 583 | set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls); |
| 584 | if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls) |
| 585 | set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls); |
| 586 | if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls) |
| 587 | set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls); |
| 588 | if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls) |
| 589 | set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls); |
| 590 | if (vdev->ctrl_handler || ops->vidioc_querymenu) |
| 591 | set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls); |
| 592 | SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency); |
| 593 | SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency); |
| 594 | SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status); |
| 595 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Hans Verkuil | 96b03d2 | 2013-04-06 06:16:58 -0300 | [diff] [blame] | 596 | set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls); |
Hans Verkuil | 79b0c64 | 2013-03-18 12:16:34 -0300 | [diff] [blame] | 597 | set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls); |
| 598 | set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 599 | #endif |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 600 | /* yes, really vidioc_subscribe_event */ |
| 601 | SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event); |
| 602 | SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event); |
| 603 | SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 604 | if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator) |
| 605 | set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls); |
| 606 | |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 607 | if (is_vid) { |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 608 | /* video specific ioctls */ |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 609 | if ((is_rx && (ops->vidioc_enum_fmt_vid_cap || |
| 610 | ops->vidioc_enum_fmt_vid_cap_mplane || |
| 611 | ops->vidioc_enum_fmt_vid_overlay)) || |
| 612 | (is_tx && (ops->vidioc_enum_fmt_vid_out || |
| 613 | ops->vidioc_enum_fmt_vid_out_mplane))) |
| 614 | set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls); |
| 615 | if ((is_rx && (ops->vidioc_g_fmt_vid_cap || |
| 616 | ops->vidioc_g_fmt_vid_cap_mplane || |
| 617 | ops->vidioc_g_fmt_vid_overlay)) || |
| 618 | (is_tx && (ops->vidioc_g_fmt_vid_out || |
| 619 | ops->vidioc_g_fmt_vid_out_mplane || |
| 620 | ops->vidioc_g_fmt_vid_out_overlay))) |
| 621 | set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); |
| 622 | if ((is_rx && (ops->vidioc_s_fmt_vid_cap || |
| 623 | ops->vidioc_s_fmt_vid_cap_mplane || |
| 624 | ops->vidioc_s_fmt_vid_overlay)) || |
| 625 | (is_tx && (ops->vidioc_s_fmt_vid_out || |
| 626 | ops->vidioc_s_fmt_vid_out_mplane || |
| 627 | ops->vidioc_s_fmt_vid_out_overlay))) |
| 628 | set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); |
| 629 | if ((is_rx && (ops->vidioc_try_fmt_vid_cap || |
| 630 | ops->vidioc_try_fmt_vid_cap_mplane || |
| 631 | ops->vidioc_try_fmt_vid_overlay)) || |
| 632 | (is_tx && (ops->vidioc_try_fmt_vid_out || |
| 633 | ops->vidioc_try_fmt_vid_out_mplane || |
| 634 | ops->vidioc_try_fmt_vid_out_overlay))) |
| 635 | set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 636 | SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay); |
| 637 | SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf); |
| 638 | SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf); |
| 639 | SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp); |
| 640 | SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp); |
| 641 | SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index); |
| 642 | SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd); |
| 643 | SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd); |
| 644 | SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd); |
| 645 | SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd); |
| 646 | SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes); |
| 647 | SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 648 | } else if (is_vbi) { |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 649 | /* vbi specific ioctls */ |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 650 | if ((is_rx && (ops->vidioc_g_fmt_vbi_cap || |
| 651 | ops->vidioc_g_fmt_sliced_vbi_cap)) || |
| 652 | (is_tx && (ops->vidioc_g_fmt_vbi_out || |
| 653 | ops->vidioc_g_fmt_sliced_vbi_out))) |
| 654 | set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); |
| 655 | if ((is_rx && (ops->vidioc_s_fmt_vbi_cap || |
| 656 | ops->vidioc_s_fmt_sliced_vbi_cap)) || |
| 657 | (is_tx && (ops->vidioc_s_fmt_vbi_out || |
| 658 | ops->vidioc_s_fmt_sliced_vbi_out))) |
| 659 | set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); |
| 660 | if ((is_rx && (ops->vidioc_try_fmt_vbi_cap || |
| 661 | ops->vidioc_try_fmt_sliced_vbi_cap)) || |
| 662 | (is_tx && (ops->vidioc_try_fmt_vbi_out || |
| 663 | ops->vidioc_try_fmt_sliced_vbi_out))) |
| 664 | set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 665 | SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap); |
Antti Palosaari | 582c52c | 2013-12-12 13:44:14 -0300 | [diff] [blame] | 666 | } else if (is_sdr) { |
| 667 | /* SDR specific ioctls */ |
| 668 | if (ops->vidioc_enum_fmt_sdr_cap) |
| 669 | set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls); |
| 670 | if (ops->vidioc_g_fmt_sdr_cap) |
| 671 | set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls); |
| 672 | if (ops->vidioc_s_fmt_sdr_cap) |
| 673 | set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls); |
| 674 | if (ops->vidioc_try_fmt_sdr_cap) |
| 675 | set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 676 | } |
Antti Palosaari | 582c52c | 2013-12-12 13:44:14 -0300 | [diff] [blame] | 677 | |
| 678 | if (is_vid || is_vbi || is_sdr) { |
| 679 | /* ioctls valid for video, vbi or sdr */ |
Frank Schaefer | 5815d0c | 2013-01-22 15:51:55 -0300 | [diff] [blame] | 680 | SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs); |
| 681 | SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf); |
| 682 | SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf); |
| 683 | SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf); |
| 684 | SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf); |
| 685 | SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs); |
| 686 | SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf); |
Antti Palosaari | 582c52c | 2013-12-12 13:44:14 -0300 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | if (is_vid || is_vbi) { |
| 690 | /* ioctls valid for video or vbi */ |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 691 | if (ops->vidioc_s_std) |
| 692 | set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 693 | SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std); |
Hans Verkuil | ca37157 | 2013-06-03 05:36:50 -0300 | [diff] [blame] | 694 | SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 695 | if (is_rx) { |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 696 | SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 697 | SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input); |
| 698 | SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input); |
| 699 | SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input); |
| 700 | SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio); |
| 701 | SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio); |
| 702 | SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio); |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 703 | SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings); |
Hans Verkuil | dd519bb | 2014-03-07 07:18:37 -0300 | [diff] [blame] | 704 | SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 705 | } |
| 706 | if (is_tx) { |
| 707 | SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output); |
| 708 | SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output); |
| 709 | SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output); |
| 710 | SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout); |
| 711 | SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout); |
| 712 | SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout); |
| 713 | } |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 714 | if (ops->vidioc_g_crop || ops->vidioc_g_selection) |
| 715 | set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls); |
| 716 | if (ops->vidioc_s_crop || ops->vidioc_s_selection) |
| 717 | set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls); |
| 718 | SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection); |
| 719 | SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection); |
| 720 | if (ops->vidioc_cropcap || ops->vidioc_g_selection) |
| 721 | set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 722 | if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER && |
Hans Verkuil | ca37157 | 2013-06-03 05:36:50 -0300 | [diff] [blame] | 723 | ops->vidioc_g_std)) |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 724 | set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls); |
| 725 | SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 726 | SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings); |
| 727 | SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings); |
| 728 | SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 729 | SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap); |
Hans Verkuil | dd519bb | 2014-03-07 07:18:37 -0300 | [diff] [blame] | 730 | SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid); |
Hans Verkuil | 4b20259 | 2012-09-14 07:03:35 -0300 | [diff] [blame] | 731 | } |
Hans Verkuil | bfffd74 | 2013-12-20 02:32:21 -0300 | [diff] [blame] | 732 | if (is_tx && (is_radio || is_sdr)) { |
| 733 | /* radio transmitter only ioctls */ |
Hans Verkuil | 90d0fc4 | 2012-09-14 07:06:08 -0300 | [diff] [blame] | 734 | SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator); |
| 735 | SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator); |
| 736 | } |
| 737 | if (is_rx) { |
| 738 | /* receiver only ioctls */ |
| 739 | SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner); |
| 740 | SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner); |
| 741 | SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek); |
| 742 | } |
| 743 | |
Hans Verkuil | 48ea0be | 2012-05-10 05:36:00 -0300 | [diff] [blame] | 744 | bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls, |
| 745 | BASE_VIDIOC_PRIVATE); |
| 746 | } |
| 747 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 748 | /** |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 749 | * __video_register_device - register video4linux devices |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 750 | * @vdev: video device structure we want to register |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 751 | * @type: type of device to register |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 752 | * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ... |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 753 | * -1 == first free) |
Hans Verkuil | 6b5270d | 2009-09-06 07:54:00 -0300 | [diff] [blame] | 754 | * @warn_if_nr_in_use: warn if the desired device node number |
| 755 | * was already in use and another number was chosen instead. |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 756 | * @owner: module that owns the video device node |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 757 | * |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 758 | * The registration code assigns minor numbers and device node numbers |
| 759 | * based on the requested type and registers the new device node with |
| 760 | * the kernel. |
Hans Verkuil | 46b6337 | 2010-12-31 11:47:23 -0300 | [diff] [blame] | 761 | * |
| 762 | * This function assumes that struct video_device was zeroed when it |
| 763 | * was allocated and does not contain any stale date. |
| 764 | * |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 765 | * An error is returned if no free minor or device node number could be |
| 766 | * found, or if the registration of the device node failed. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 767 | * |
| 768 | * Zero is returned on success. |
| 769 | * |
| 770 | * Valid types are |
| 771 | * |
| 772 | * %VFL_TYPE_GRABBER - A frame grabber |
| 773 | * |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 774 | * %VFL_TYPE_VBI - Vertical blank data (undecoded) |
| 775 | * |
| 776 | * %VFL_TYPE_RADIO - A radio card |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 777 | * |
| 778 | * %VFL_TYPE_SUBDEV - A subdevice |
Antti Palosaari | d42626b | 2013-12-11 20:03:07 -0300 | [diff] [blame] | 779 | * |
| 780 | * %VFL_TYPE_SDR - Software Defined Radio |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 781 | */ |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 782 | int __video_register_device(struct video_device *vdev, int type, int nr, |
| 783 | int warn_if_nr_in_use, struct module *owner) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 784 | { |
| 785 | int i = 0; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 786 | int ret; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 787 | int minor_offset = 0; |
| 788 | int minor_cnt = VIDEO_NUM_DEVICES; |
| 789 | const char *name_base; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 790 | |
| 791 | /* A minor value of -1 marks this video device as never |
| 792 | having been registered */ |
Trent Piepho | 428c8d1 | 2009-03-03 18:51:52 -0300 | [diff] [blame] | 793 | vdev->minor = -1; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 794 | |
Hans Verkuil | d6e7497 | 2008-08-23 06:27:59 -0300 | [diff] [blame] | 795 | /* the release callback MUST be present */ |
Guennadi Liakhovetski | 1fc2b5f | 2011-12-06 05:33:03 -0300 | [diff] [blame] | 796 | if (WARN_ON(!vdev->release)) |
Henrik Kretzschmar | ee7aa9f | 2008-08-22 16:41:03 -0300 | [diff] [blame] | 797 | return -EINVAL; |
Hans Verkuil | 1c1d86a | 2013-06-12 11:15:12 -0300 | [diff] [blame] | 798 | /* the v4l2_dev pointer MUST be present */ |
| 799 | if (WARN_ON(!vdev->v4l2_dev)) |
| 800 | return -EINVAL; |
Henrik Kretzschmar | ee7aa9f | 2008-08-22 16:41:03 -0300 | [diff] [blame] | 801 | |
Sakari Ailus | 1babcb4 | 2010-03-23 09:25:26 -0300 | [diff] [blame] | 802 | /* v4l2_fh support */ |
| 803 | spin_lock_init(&vdev->fh_lock); |
| 804 | INIT_LIST_HEAD(&vdev->fh_list); |
| 805 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 806 | /* Part 1: check device type */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 807 | switch (type) { |
| 808 | case VFL_TYPE_GRABBER: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 809 | name_base = "video"; |
| 810 | break; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 811 | case VFL_TYPE_VBI: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 812 | name_base = "vbi"; |
| 813 | break; |
| 814 | case VFL_TYPE_RADIO: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 815 | name_base = "radio"; |
| 816 | break; |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 817 | case VFL_TYPE_SUBDEV: |
| 818 | name_base = "v4l-subdev"; |
| 819 | break; |
Antti Palosaari | d42626b | 2013-12-11 20:03:07 -0300 | [diff] [blame] | 820 | case VFL_TYPE_SDR: |
| 821 | /* Use device name 'swradio' because 'sdr' was already taken. */ |
| 822 | name_base = "swradio"; |
| 823 | break; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 824 | default: |
| 825 | printk(KERN_ERR "%s called with unknown type: %d\n", |
| 826 | __func__, type); |
Henrik Kretzschmar | 46f2c21 | 2008-09-03 16:47:39 -0300 | [diff] [blame] | 827 | return -EINVAL; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 828 | } |
| 829 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 830 | vdev->vfl_type = type; |
| 831 | vdev->cdev = NULL; |
Hans Verkuil | 1c1d86a | 2013-06-12 11:15:12 -0300 | [diff] [blame] | 832 | if (vdev->dev_parent == NULL) |
| 833 | vdev->dev_parent = vdev->v4l2_dev->dev; |
| 834 | if (vdev->ctrl_handler == NULL) |
| 835 | vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler; |
| 836 | /* If the prio state pointer is NULL, then use the v4l2_device |
| 837 | prio state. */ |
| 838 | if (vdev->prio == NULL) |
| 839 | vdev->prio = &vdev->v4l2_dev->prio; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 840 | |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 841 | /* Part 2: find a free minor, device node number and device index. */ |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 842 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
| 843 | /* Keep the ranges for the first four types for historical |
| 844 | * reasons. |
| 845 | * Newer devices (not yet in place) should use the range |
| 846 | * of 128-191 and just pick the first free minor there |
| 847 | * (new style). */ |
| 848 | switch (type) { |
| 849 | case VFL_TYPE_GRABBER: |
| 850 | minor_offset = 0; |
| 851 | minor_cnt = 64; |
| 852 | break; |
| 853 | case VFL_TYPE_RADIO: |
| 854 | minor_offset = 64; |
| 855 | minor_cnt = 64; |
| 856 | break; |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 857 | case VFL_TYPE_VBI: |
| 858 | minor_offset = 224; |
| 859 | minor_cnt = 32; |
| 860 | break; |
| 861 | default: |
| 862 | minor_offset = 128; |
| 863 | minor_cnt = 64; |
| 864 | break; |
| 865 | } |
| 866 | #endif |
| 867 | |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 868 | /* Pick a device node number */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 869 | mutex_lock(&videodev_lock); |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 870 | nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt); |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 871 | if (nr == minor_cnt) |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 872 | nr = devnode_find(vdev, 0, minor_cnt); |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 873 | if (nr == minor_cnt) { |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 874 | printk(KERN_ERR "could not get a free device node number\n"); |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 875 | mutex_unlock(&videodev_lock); |
| 876 | return -ENFILE; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 877 | } |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 878 | #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 879 | /* 1-on-1 mapping of device node number to minor number */ |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 880 | i = nr; |
| 881 | #else |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 882 | /* The device node number and minor numbers are independent, so |
| 883 | we just find the first free minor number. */ |
Hans Verkuil | dd89601 | 2008-10-04 08:36:54 -0300 | [diff] [blame] | 884 | for (i = 0; i < VIDEO_NUM_DEVICES; i++) |
| 885 | if (video_device[i] == NULL) |
| 886 | break; |
| 887 | if (i == VIDEO_NUM_DEVICES) { |
| 888 | mutex_unlock(&videodev_lock); |
| 889 | printk(KERN_ERR "could not get a free minor\n"); |
| 890 | return -ENFILE; |
| 891 | } |
| 892 | #endif |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 893 | vdev->minor = i + minor_offset; |
| 894 | vdev->num = nr; |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 895 | devnode_set(vdev); |
| 896 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 897 | /* Should not happen since we thought this minor was free */ |
| 898 | WARN_ON(video_device[vdev->minor] != NULL); |
Hans Verkuil | 7ae0cd9 | 2009-06-19 11:32:56 -0300 | [diff] [blame] | 899 | vdev->index = get_index(vdev); |
Marek Szyprowski | 6c3df5d | 2013-12-03 10:14:29 -0300 | [diff] [blame] | 900 | video_device[vdev->minor] = vdev; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 901 | mutex_unlock(&videodev_lock); |
| 902 | |
Hans Verkuil | 48ea0be | 2012-05-10 05:36:00 -0300 | [diff] [blame] | 903 | if (vdev->ioctl_ops) |
| 904 | determine_valid_ioctls(vdev); |
| 905 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 906 | /* Part 3: Initialize the character device */ |
| 907 | vdev->cdev = cdev_alloc(); |
| 908 | if (vdev->cdev == NULL) { |
| 909 | ret = -ENOMEM; |
| 910 | goto cleanup; |
| 911 | } |
Arnd Bergmann | 86a5ef7 | 2010-07-04 00:15:09 +0200 | [diff] [blame] | 912 | vdev->cdev->ops = &v4l2_fops; |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 913 | vdev->cdev->owner = owner; |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 914 | ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 915 | if (ret < 0) { |
| 916 | printk(KERN_ERR "%s: cdev_add failed\n", __func__); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 917 | kfree(vdev->cdev); |
| 918 | vdev->cdev = NULL; |
| 919 | goto cleanup; |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 920 | } |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 921 | |
| 922 | /* Part 4: register the device with sysfs */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 923 | vdev->dev.class = &video_class; |
| 924 | vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor); |
Hans Verkuil | 1c1d86a | 2013-06-12 11:15:12 -0300 | [diff] [blame] | 925 | vdev->dev.parent = vdev->dev_parent; |
Hans Verkuil | 22e2212 | 2009-09-06 07:13:14 -0300 | [diff] [blame] | 926 | dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 927 | ret = device_register(&vdev->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 928 | if (ret < 0) { |
| 929 | printk(KERN_ERR "%s: device_register failed\n", __func__); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 930 | goto cleanup; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 931 | } |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 932 | /* Register the release callback that will be called when the last |
| 933 | reference to the device goes away. */ |
| 934 | vdev->dev.release = v4l2_device_release; |
| 935 | |
Hans Verkuil | 6b5270d | 2009-09-06 07:54:00 -0300 | [diff] [blame] | 936 | if (nr != -1 && nr != vdev->num && warn_if_nr_in_use) |
Laurent Pinchart | eac8ea5 | 2009-11-27 13:56:50 -0300 | [diff] [blame] | 937 | printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__, |
| 938 | name_base, nr, video_device_node_name(vdev)); |
Hans Verkuil | bedf8bc | 2011-03-12 06:37:19 -0300 | [diff] [blame] | 939 | |
| 940 | /* Increase v4l2_device refcount */ |
| 941 | if (vdev->v4l2_dev) |
| 942 | v4l2_device_get(vdev->v4l2_dev); |
| 943 | |
Laurent Pinchart | 2c0ab67 | 2009-12-09 08:40:10 -0300 | [diff] [blame] | 944 | #if defined(CONFIG_MEDIA_CONTROLLER) |
| 945 | /* Part 5: Register the entity. */ |
Laurent Pinchart | c4f0b78 | 2011-04-08 09:04:06 -0300 | [diff] [blame] | 946 | if (vdev->v4l2_dev && vdev->v4l2_dev->mdev && |
| 947 | vdev->vfl_type != VFL_TYPE_SUBDEV) { |
Laurent Pinchart | 2c0ab67 | 2009-12-09 08:40:10 -0300 | [diff] [blame] | 948 | vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L; |
| 949 | vdev->entity.name = vdev->name; |
Clemens Ladisch | fa5034c | 2011-11-05 18:42:01 -0300 | [diff] [blame] | 950 | vdev->entity.info.v4l.major = VIDEO_MAJOR; |
| 951 | vdev->entity.info.v4l.minor = vdev->minor; |
Laurent Pinchart | 2c0ab67 | 2009-12-09 08:40:10 -0300 | [diff] [blame] | 952 | ret = media_device_register_entity(vdev->v4l2_dev->mdev, |
| 953 | &vdev->entity); |
| 954 | if (ret < 0) |
| 955 | printk(KERN_WARNING |
| 956 | "%s: media_device_register_entity failed\n", |
| 957 | __func__); |
| 958 | } |
| 959 | #endif |
| 960 | /* Part 6: Activate this minor. The char device can now be used. */ |
Laurent Pinchart | 957b4aa | 2009-11-27 13:57:22 -0300 | [diff] [blame] | 961 | set_bit(V4L2_FL_REGISTERED, &vdev->flags); |
Laurent Pinchart | 2c0ab67 | 2009-12-09 08:40:10 -0300 | [diff] [blame] | 962 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 963 | return 0; |
| 964 | |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 965 | cleanup: |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 966 | mutex_lock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 967 | if (vdev->cdev) |
| 968 | cdev_del(vdev->cdev); |
Ricardo Ribalda | 1056e43 | 2013-08-13 11:04:06 -0300 | [diff] [blame] | 969 | video_device[vdev->minor] = NULL; |
Hans Verkuil | 5062cb7 | 2009-09-07 03:40:24 -0300 | [diff] [blame] | 970 | devnode_clear(vdev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 971 | mutex_unlock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 972 | /* Mark this video device as never having been registered. */ |
| 973 | vdev->minor = -1; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 974 | return ret; |
| 975 | } |
Laurent Pinchart | 2096a5d | 2009-12-09 08:38:49 -0300 | [diff] [blame] | 976 | EXPORT_SYMBOL(__video_register_device); |
Hans Verkuil | 6b5270d | 2009-09-06 07:54:00 -0300 | [diff] [blame] | 977 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 978 | /** |
| 979 | * video_unregister_device - unregister a video4linux device |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 980 | * @vdev: the device to unregister |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 981 | * |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 982 | * This unregisters the passed device. Future open calls will |
| 983 | * be met with errors. |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 984 | */ |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 985 | void video_unregister_device(struct video_device *vdev) |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 986 | { |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 987 | /* Check if vdev was ever registered at all */ |
Laurent Pinchart | 957b4aa | 2009-11-27 13:57:22 -0300 | [diff] [blame] | 988 | if (!vdev || !video_is_registered(vdev)) |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 989 | return; |
| 990 | |
Hans Verkuil | ca9afe6 | 2010-11-26 06:54:53 -0300 | [diff] [blame] | 991 | mutex_lock(&videodev_lock); |
| 992 | /* This must be in a critical section to prevent a race with v4l2_open. |
| 993 | * Once this bit has been cleared video_get may never be called again. |
| 994 | */ |
Laurent Pinchart | 957b4aa | 2009-11-27 13:57:22 -0300 | [diff] [blame] | 995 | clear_bit(V4L2_FL_REGISTERED, &vdev->flags); |
Hans Verkuil | ca9afe6 | 2010-11-26 06:54:53 -0300 | [diff] [blame] | 996 | mutex_unlock(&videodev_lock); |
Hans Verkuil | dc93a70 | 2008-12-19 21:28:27 -0300 | [diff] [blame] | 997 | device_unregister(&vdev->dev); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 998 | } |
| 999 | EXPORT_SYMBOL(video_unregister_device); |
| 1000 | |
| 1001 | /* |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1002 | * Initialise video for linux |
| 1003 | */ |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1004 | static int __init videodev_init(void) |
| 1005 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 1006 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1007 | int ret; |
| 1008 | |
| 1009 | printk(KERN_INFO "Linux video capture interface: v2.00\n"); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 1010 | ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME); |
| 1011 | if (ret < 0) { |
| 1012 | printk(KERN_WARNING "videodev: unable to get major %d\n", |
| 1013 | VIDEO_MAJOR); |
| 1014 | return ret; |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | ret = class_register(&video_class); |
| 1018 | if (ret < 0) { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 1019 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1020 | printk(KERN_WARNING "video_dev: class_register failed\n"); |
| 1021 | return -EIO; |
| 1022 | } |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static void __exit videodev_exit(void) |
| 1028 | { |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 1029 | dev_t dev = MKDEV(VIDEO_MAJOR, 0); |
| 1030 | |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1031 | class_unregister(&video_class); |
Hans Verkuil | 7f8ecfa | 2008-08-29 17:31:35 -0300 | [diff] [blame] | 1032 | unregister_chrdev_region(dev, VIDEO_NUM_DEVICES); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1033 | } |
| 1034 | |
Bhupesh Sharma | ee981c6 | 2012-03-12 05:09:02 -0300 | [diff] [blame] | 1035 | subsys_initcall(videodev_init); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1036 | module_exit(videodev_exit) |
| 1037 | |
| 1038 | MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 1039 | MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2"); |
| 1040 | MODULE_LICENSE("GPL"); |
Scott James Remnant | cbb72b0 | 2009-03-02 15:40:57 -0300 | [diff] [blame] | 1041 | MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR); |
Hans Verkuil | 27a5e6d | 2008-07-20 08:43:17 -0300 | [diff] [blame] | 1042 | |
| 1043 | |
| 1044 | /* |
| 1045 | * Local variables: |
| 1046 | * c-basic-offset: 8 |
| 1047 | * End: |
| 1048 | */ |