blob: 9aa530a8bea93d102e0b686da01f4ede427a8cc4 [file] [log] [blame]
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001/*
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 Coxd9b01442008-10-27 15:13:47 -030012 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030013 * 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 Verkuil27a5e6d2008-07-20 08:43:17 -030028#include <asm/uaccess.h>
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030029
30#include <media/v4l2-common.h>
Hans Verkuil9bea3512008-12-23 07:35:17 -030031#include <media/v4l2-device.h>
Hans Verkuilbec43662008-12-30 06:58:20 -030032#include <media/v4l2-ioctl.h>
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030033
34#define VIDEO_NUM_DEVICES 256
35#define VIDEO_NAME "video4linux"
36
37/*
38 * sysfs stuff
39 */
40
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070041static ssize_t index_show(struct device *cd,
42 struct device_attribute *attr, char *buf)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030043{
Hans Verkuildc93a702008-12-19 21:28:27 -030044 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030045
Hans Verkuildc93a702008-12-19 21:28:27 -030046 return sprintf(buf, "%i\n", vdev->index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030047}
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070048static DEVICE_ATTR_RO(index);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030049
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070050static ssize_t debug_show(struct device *cd,
51 struct device_attribute *attr, char *buf)
Hans Verkuil80131fe02012-06-22 06:37:38 -030052{
53 struct video_device *vdev = to_video_device(cd);
54
55 return sprintf(buf, "%i\n", vdev->debug);
56}
57
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070058static ssize_t debug_store(struct device *cd, struct device_attribute *attr,
59 const char *buf, size_t len)
Hans Verkuil80131fe02012-06-22 06:37:38 -030060{
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-Hartman13e22372013-07-24 15:05:14 -070072static DEVICE_ATTR_RW(debug);
Hans Verkuil80131fe02012-06-22 06:37:38 -030073
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070074static ssize_t name_show(struct device *cd,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030075 struct device_attribute *attr, char *buf)
76{
Hans Verkuildc93a702008-12-19 21:28:27 -030077 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbfa8a272008-08-23 07:48:38 -030078
Hans Verkuildc93a702008-12-19 21:28:27 -030079 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030080}
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070081static DEVICE_ATTR_RO(name);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030082
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070083static struct attribute *video_device_attrs[] = {
84 &dev_attr_name.attr,
85 &dev_attr_debug.attr,
86 &dev_attr_index.attr,
87 NULL,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030088};
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070089ATTRIBUTE_GROUPS(video_device);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -030090
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030091/*
92 * Active devices
93 */
94static struct video_device *video_device[VIDEO_NUM_DEVICES];
95static DEFINE_MUTEX(videodev_lock);
Hans Verkuil22e22122009-09-06 07:13:14 -030096static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -030097
Hans Verkuil5062cb72009-09-07 03:40:24 -030098/* 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. */
105static 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 Verkuil226c0ee2010-08-06 12:48:00 -0300110 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
Hans Verkuil5062cb72009-09-07 03:40:24 -0300111
112 return devnode_nums[idx];
113}
114#else
115/* Return the bitmap corresponding to vfl_type. */
116static 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 */
123static 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 */
129static 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> */
135static 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 Verkuil27a5e6d2008-07-20 08:43:17 -0300140struct video_device *video_device_alloc(void)
141{
Hans Verkuilbfa8a272008-08-23 07:48:38 -0300142 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300143}
144EXPORT_SYMBOL(video_device_alloc);
145
Hans Verkuildc93a702008-12-19 21:28:27 -0300146void video_device_release(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300147{
Hans Verkuildc93a702008-12-19 21:28:27 -0300148 kfree(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300149}
150EXPORT_SYMBOL(video_device_release);
151
Hans Verkuildc93a702008-12-19 21:28:27 -0300152void video_device_release_empty(struct video_device *vdev)
Hans Verkuilf9e86b52008-08-23 05:47:41 -0300153{
154 /* Do nothing */
155 /* Only valid when the video_device struct is a static. */
156}
157EXPORT_SYMBOL(video_device_release_empty);
158
Hans Verkuildc93a702008-12-19 21:28:27 -0300159static inline void video_get(struct video_device *vdev)
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300160{
Hans Verkuildc93a702008-12-19 21:28:27 -0300161 get_device(&vdev->dev);
162}
163
164static 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. */
170static void v4l2_device_release(struct device *cd)
171{
172 struct video_device *vdev = to_video_device(cd);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300173 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300174
175 mutex_lock(&videodev_lock);
Guennadi Liakhovetski1fc2b5f2011-12-06 05:33:03 -0300176 if (WARN_ON(video_device[vdev->minor] != vdev)) {
Hans Verkuildc93a702008-12-19 21:28:27 -0300177 /* should not happen */
Guennadi Liakhovetski1fc2b5f2011-12-06 05:33:03 -0300178 mutex_unlock(&videodev_lock);
Hans Verkuil6ea9a182008-08-30 09:40:47 -0300179 return;
180 }
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300181
182 /* Free up this device for reuse */
Hans Verkuildc93a702008-12-19 21:28:27 -0300183 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 Verkuil22e22122009-09-06 07:13:14 -0300191 /* Mark device node number as free */
Hans Verkuil5062cb72009-09-07 03:40:24 -0300192 devnode_clear(vdev);
Hans Verkuildc93a702008-12-19 21:28:27 -0300193
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300194 mutex_unlock(&videodev_lock);
195
Laurent Pinchartc064b8e2011-06-15 05:20:34 -0300196#if defined(CONFIG_MEDIA_CONTROLLER)
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300197 if (v4l2_dev->mdev &&
Laurent Pinchartc064b8e2011-06-15 05:20:34 -0300198 vdev->vfl_type != VFL_TYPE_SUBDEV)
199 media_device_unregister_entity(&vdev->entity);
200#endif
201
Hans Verkuil8280b662011-09-06 10:23:18 -0300202 /* 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 */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300210 if (v4l2_dev->release == NULL)
Hans Verkuil8280b662011-09-06 10:23:18 -0300211 v4l2_dev = NULL;
212
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300213 /* Release video_device and perform other
214 cleanups as needed. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300215 vdev->release(vdev);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300216
217 /* Decrease v4l2_device refcount */
218 if (v4l2_dev)
219 v4l2_device_put(v4l2_dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300220}
221
222static struct class video_class = {
223 .name = VIDEO_NAME,
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -0700224 .dev_groups = video_device_groups,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300225};
226
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300227struct video_device *video_devdata(struct file *file)
228{
Al Viro496ad9a2013-01-23 17:07:38 -0500229 return video_device[iminor(file_inode(file))];
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300230}
231EXPORT_SYMBOL(video_devdata);
232
Hans Verkuil02265492010-12-29 10:05:02 -0300233
234/* Priority handling */
235
236static 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
243void v4l2_prio_init(struct v4l2_prio_state *global)
244{
245 memset(global, 0, sizeof(*global));
246}
247EXPORT_SYMBOL(v4l2_prio_init);
248
249int 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}
263EXPORT_SYMBOL(v4l2_prio_change);
264
265void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
266{
267 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
268}
269EXPORT_SYMBOL(v4l2_prio_open);
270
271void 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}
276EXPORT_SYMBOL(v4l2_prio_close);
277
278enum 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}
288EXPORT_SYMBOL(v4l2_prio_max);
289
290int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
291{
292 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
293}
294EXPORT_SYMBOL(v4l2_prio_check);
295
296
Hans Verkuildc93a702008-12-19 21:28:27 -0300297static 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 Verkuil28778422010-11-26 06:43:51 -0300301 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300302
303 if (!vdev->fops->read)
304 return -EINVAL;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300305 if (video_is_registered(vdev))
306 ret = vdev->fops->read(filp, buf, sz, off);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300307 if (vdev->debug)
308 printk(KERN_DEBUG "%s: read: %zd (%d)\n",
309 video_device_node_name(vdev), sz, ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300310 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300311}
312
313static 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 Verkuil28778422010-11-26 06:43:51 -0300317 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300318
319 if (!vdev->fops->write)
320 return -EINVAL;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300321 if (video_is_registered(vdev))
322 ret = vdev->fops->write(filp, buf, sz, off);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300323 if (vdev->debug)
324 printk(KERN_DEBUG "%s: write: %zd (%d)\n",
325 video_device_node_name(vdev), sz, ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300326 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300327}
328
329static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
330{
331 struct video_device *vdev = video_devdata(filp);
Hans Verkuilcf533732012-07-31 04:02:25 -0300332 unsigned int res = POLLERR | POLLHUP;
Hans Verkuildc93a702008-12-19 21:28:27 -0300333
Hans Verkuilee6869a2010-09-26 08:47:38 -0300334 if (!vdev->fops->poll)
Hans Verkuil28778422010-11-26 06:43:51 -0300335 return DEFAULT_POLLMASK;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300336 if (video_is_registered(vdev))
Hans Verkuilcf533732012-07-31 04:02:25 -0300337 res = vdev->fops->poll(filp, poll);
Hans Verkuil57fc8eb2014-03-03 06:47:38 -0300338 if (vdev->debug > 2)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300339 printk(KERN_DEBUG "%s: poll: %08x\n",
Hans Verkuilcf533732012-07-31 04:02:25 -0300340 video_device_node_name(vdev), res);
341 return res;
Hans Verkuildc93a702008-12-19 21:28:27 -0300342}
343
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200344static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Hans Verkuildc93a702008-12-19 21:28:27 -0300345{
346 struct video_device *vdev = video_devdata(filp);
Mauro Carvalho Chehab72420632010-10-09 16:43:40 -0300347 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300348
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200349 if (vdev->fops->unlocked_ioctl) {
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300350 struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
Hans Verkuil8ab75e32012-05-10 02:51:31 -0300351
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300352 if (lock && mutex_lock_interruptible(lock))
353 return -ERESTARTSYS;
Mauro Carvalho Chehab72420632010-10-09 16:43:40 -0300354 if (video_is_registered(vdev))
355 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300356 if (lock)
357 mutex_unlock(lock);
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200358 } else if (vdev->fops->ioctl) {
Hans Verkuil879aa242010-11-26 06:47:28 -0300359 /* 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 *
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300363 * All drivers implement struct v4l2_device, so we use the
364 * lock defined there to serialize the ioctls.
Hans Verkuil879aa242010-11-26 06:47:28 -0300365 *
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300366 * However, if the driver sleeps, then it blocks all ioctls
367 * since the lock is still held. This is very common for
368 * VIDIOC_DQBUF since that normally waits for a frame to arrive.
369 * As a result any other ioctl calls will proceed very, very
370 * slowly since each call will have to wait for the VIDIOC_QBUF
371 * to finish. Things that should take 0.01s may now take 10-20
372 * seconds.
Hans Verkuil879aa242010-11-26 06:47:28 -0300373 *
374 * The workaround is to *not* take the lock for VIDIOC_DQBUF.
375 * This actually works OK for videobuf-based drivers, since
376 * videobuf will take its own internal lock.
377 */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300378 struct mutex *m = &vdev->v4l2_dev->ioctl_lock;
Arnd Bergmann0edf2e52010-10-27 09:30:32 -0300379
Hans Verkuil879aa242010-11-26 06:47:28 -0300380 if (cmd != VIDIOC_DQBUF && mutex_lock_interruptible(m))
381 return -ERESTARTSYS;
Mauro Carvalho Chehab72420632010-10-09 16:43:40 -0300382 if (video_is_registered(vdev))
383 ret = vdev->fops->ioctl(filp, cmd, arg);
Hans Verkuil879aa242010-11-26 06:47:28 -0300384 if (cmd != VIDIOC_DQBUF)
385 mutex_unlock(m);
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200386 } else
387 ret = -ENOTTY;
Hans Verkuildc93a702008-12-19 21:28:27 -0300388
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200389 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300390}
391
Bob Liuecc65172011-05-06 05:20:09 -0300392#ifdef CONFIG_MMU
393#define v4l2_get_unmapped_area NULL
394#else
395static unsigned long v4l2_get_unmapped_area(struct file *filp,
396 unsigned long addr, unsigned long len, unsigned long pgoff,
397 unsigned long flags)
398{
399 struct video_device *vdev = video_devdata(filp);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300400 int ret;
Bob Liuecc65172011-05-06 05:20:09 -0300401
402 if (!vdev->fops->get_unmapped_area)
403 return -ENOSYS;
404 if (!video_is_registered(vdev))
405 return -ENODEV;
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300406 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
407 if (vdev->debug)
408 printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
409 video_device_node_name(vdev), ret);
410 return ret;
Bob Liuecc65172011-05-06 05:20:09 -0300411}
412#endif
413
Hans Verkuildc93a702008-12-19 21:28:27 -0300414static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
415{
416 struct video_device *vdev = video_devdata(filp);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300417 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300418
Hans Verkuilee6869a2010-09-26 08:47:38 -0300419 if (!vdev->fops->mmap)
Hans Verkuilcf533732012-07-31 04:02:25 -0300420 return -ENODEV;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300421 if (video_is_registered(vdev))
422 ret = vdev->fops->mmap(filp, vm);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300423 if (vdev->debug)
424 printk(KERN_DEBUG "%s: mmap (%d)\n",
425 video_device_node_name(vdev), ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300426 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300427}
428
429/* Override for the open function */
430static int v4l2_open(struct inode *inode, struct file *filp)
431{
432 struct video_device *vdev;
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300433 int ret = 0;
Hans Verkuildc93a702008-12-19 21:28:27 -0300434
435 /* Check if the video device is available */
436 mutex_lock(&videodev_lock);
437 vdev = video_devdata(filp);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300438 /* return ENODEV if the video device has already been removed. */
Hans Verkuilca9afe62010-11-26 06:54:53 -0300439 if (vdev == NULL || !video_is_registered(vdev)) {
Hans Verkuildc93a702008-12-19 21:28:27 -0300440 mutex_unlock(&videodev_lock);
441 return -ENODEV;
442 }
443 /* and increase the device refcount */
444 video_get(vdev);
445 mutex_unlock(&videodev_lock);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300446 if (vdev->fops->open) {
Hans Verkuilee6869a2010-09-26 08:47:38 -0300447 if (video_is_registered(vdev))
448 ret = vdev->fops->open(filp);
449 else
450 ret = -ENODEV;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300451 }
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300452
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300453 if (vdev->debug)
454 printk(KERN_DEBUG "%s: open (%d)\n",
455 video_device_node_name(vdev), ret);
Ezequiel García8f695d32012-07-26 07:59:04 -0300456 /* decrease the refcount in case of an error */
457 if (ret)
458 video_put(vdev);
Hans Verkuildc93a702008-12-19 21:28:27 -0300459 return ret;
460}
461
462/* Override for the release function */
463static int v4l2_release(struct inode *inode, struct file *filp)
464{
465 struct video_device *vdev = video_devdata(filp);
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300466 int ret = 0;
467
Hans Verkuilcf533732012-07-31 04:02:25 -0300468 if (vdev->fops->release)
469 ret = vdev->fops->release(filp);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300470 if (vdev->debug)
471 printk(KERN_DEBUG "%s: release\n",
472 video_device_node_name(vdev));
Hans Verkuilcf533732012-07-31 04:02:25 -0300473
Ezequiel García8f695d32012-07-26 07:59:04 -0300474 /* decrease the refcount unconditionally since the release()
475 return value is ignored. */
476 video_put(vdev);
Hans Verkuildc93a702008-12-19 21:28:27 -0300477 return ret;
478}
479
Hans Verkuildc93a702008-12-19 21:28:27 -0300480static const struct file_operations v4l2_fops = {
481 .owner = THIS_MODULE,
482 .read = v4l2_read,
483 .write = v4l2_write,
484 .open = v4l2_open,
Bob Liuecc65172011-05-06 05:20:09 -0300485 .get_unmapped_area = v4l2_get_unmapped_area,
Hans Verkuildc93a702008-12-19 21:28:27 -0300486 .mmap = v4l2_mmap,
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200487 .unlocked_ioctl = v4l2_ioctl,
Hans Verkuildc93a702008-12-19 21:28:27 -0300488#ifdef CONFIG_COMPAT
Hans Verkuil9bb7cde2008-12-30 06:42:40 -0300489 .compat_ioctl = v4l2_compat_ioctl32,
Hans Verkuildc93a702008-12-19 21:28:27 -0300490#endif
491 .release = v4l2_release,
492 .poll = v4l2_poll,
493 .llseek = no_llseek,
494};
495
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300496/**
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300497 * get_index - assign stream index number based on v4l2_dev
498 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300499 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300500 * Note that when this is called the new device has not yet been registered
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300501 * in the video_device array, but it was able to obtain a minor number.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300502 *
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300503 * This means that we can always obtain a free stream index number since
504 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
505 * use of the video_device array.
506 *
507 * Returns a free index number.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300508 */
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300509static int get_index(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300510{
Hans Verkuil775a05d2009-02-14 11:31:01 -0300511 /* This can be static since this function is called with the global
512 videodev_lock held. */
513 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300514 int i;
515
Hans Verkuil775a05d2009-02-14 11:31:01 -0300516 bitmap_zero(used, VIDEO_NUM_DEVICES);
Hans Verkuil806e5b72008-12-19 09:10:56 -0300517
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300518 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
519 if (video_device[i] != NULL &&
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300520 video_device[i]->v4l2_dev == vdev->v4l2_dev) {
Hans Verkuil775a05d2009-02-14 11:31:01 -0300521 set_bit(video_device[i]->index, used);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300522 }
523 }
524
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300525 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300526}
527
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300528#define SET_VALID_IOCTL(ops, cmd, op) \
529 if (ops->op) \
530 set_bit(_IOC_NR(cmd), valid_ioctls)
531
532/* This determines which ioctls are actually implemented in the driver.
533 It's a one-time thing which simplifies video_ioctl2 as it can just do
534 a bit test.
535
536 Note that drivers can override this by setting bits to 1 in
537 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
538 called, then that ioctl will actually be marked as unimplemented.
539
540 It does that by first setting up the local valid_ioctls bitmap, and
541 at the end do a:
542
543 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
544 */
545static void determine_valid_ioctls(struct video_device *vdev)
546{
547 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
548 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
Hans Verkuil4b202592012-09-14 07:03:35 -0300549 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
550 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
Hans Verkuilbfffd742013-12-20 02:32:21 -0300551 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300552 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -0300553 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
554 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300555
556 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
557
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300558 /* vfl_type and vfl_dir independent ioctls */
559
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300560 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -0300561 if (ops->vidioc_g_priority)
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300562 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -0300563 if (ops->vidioc_s_priority)
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300564 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300565 /* Note: the control handler can also be passed through the filehandle,
566 and that can't be tested here. If the bit for these control ioctls
567 is set, then the ioctl is valid. But if it is 0, then it can still
568 be valid if the filehandle passed the control handler. */
569 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
570 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
Hans Verkuile6bee362014-06-10 04:22:06 -0300571 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
572 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300573 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
574 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
575 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
576 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
577 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
578 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
579 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
580 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
581 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
582 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
583 if (vdev->ctrl_handler || ops->vidioc_querymenu)
584 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
585 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
586 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
587 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
588#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -0300589 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
Hans Verkuil79b0c642013-03-18 12:16:34 -0300590 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
591 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300592#endif
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300593 /* yes, really vidioc_subscribe_event */
594 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
595 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
596 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300597 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
598 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
599
Hans Verkuil4b202592012-09-14 07:03:35 -0300600 if (is_vid) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300601 /* video specific ioctls */
Hans Verkuil4b202592012-09-14 07:03:35 -0300602 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
603 ops->vidioc_enum_fmt_vid_cap_mplane ||
604 ops->vidioc_enum_fmt_vid_overlay)) ||
605 (is_tx && (ops->vidioc_enum_fmt_vid_out ||
606 ops->vidioc_enum_fmt_vid_out_mplane)))
607 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
608 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
609 ops->vidioc_g_fmt_vid_cap_mplane ||
610 ops->vidioc_g_fmt_vid_overlay)) ||
611 (is_tx && (ops->vidioc_g_fmt_vid_out ||
612 ops->vidioc_g_fmt_vid_out_mplane ||
613 ops->vidioc_g_fmt_vid_out_overlay)))
614 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
615 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
616 ops->vidioc_s_fmt_vid_cap_mplane ||
617 ops->vidioc_s_fmt_vid_overlay)) ||
618 (is_tx && (ops->vidioc_s_fmt_vid_out ||
619 ops->vidioc_s_fmt_vid_out_mplane ||
620 ops->vidioc_s_fmt_vid_out_overlay)))
621 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
622 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
623 ops->vidioc_try_fmt_vid_cap_mplane ||
624 ops->vidioc_try_fmt_vid_overlay)) ||
625 (is_tx && (ops->vidioc_try_fmt_vid_out ||
626 ops->vidioc_try_fmt_vid_out_mplane ||
627 ops->vidioc_try_fmt_vid_out_overlay)))
628 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300629 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
630 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
631 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
632 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
633 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
634 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
635 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
636 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
637 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
638 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
639 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
640 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
Hans Verkuil4b202592012-09-14 07:03:35 -0300641 } else if (is_vbi) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300642 /* vbi specific ioctls */
Hans Verkuil4b202592012-09-14 07:03:35 -0300643 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
644 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
645 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
646 ops->vidioc_g_fmt_sliced_vbi_out)))
647 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
648 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
649 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
650 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
651 ops->vidioc_s_fmt_sliced_vbi_out)))
652 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
653 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
654 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
655 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
656 ops->vidioc_try_fmt_sliced_vbi_out)))
657 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300658 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
Antti Palosaari582c52c2013-12-12 13:44:14 -0300659 } else if (is_sdr) {
660 /* SDR specific ioctls */
661 if (ops->vidioc_enum_fmt_sdr_cap)
662 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
663 if (ops->vidioc_g_fmt_sdr_cap)
664 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
665 if (ops->vidioc_s_fmt_sdr_cap)
666 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
667 if (ops->vidioc_try_fmt_sdr_cap)
668 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300669 }
Antti Palosaari582c52c2013-12-12 13:44:14 -0300670
671 if (is_vid || is_vbi || is_sdr) {
672 /* ioctls valid for video, vbi or sdr */
Frank Schaefer5815d0c2013-01-22 15:51:55 -0300673 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
674 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
675 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
676 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
677 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
678 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
679 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
Hans Verkuil72be89c2014-07-10 19:51:26 -0300680 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
681 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
Antti Palosaari582c52c2013-12-12 13:44:14 -0300682 }
683
684 if (is_vid || is_vbi) {
685 /* ioctls valid for video or vbi */
Hans Verkuil4b202592012-09-14 07:03:35 -0300686 if (ops->vidioc_s_std)
687 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300688 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
Hans Verkuilca371572013-06-03 05:36:50 -0300689 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
Hans Verkuil4b202592012-09-14 07:03:35 -0300690 if (is_rx) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300691 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
Hans Verkuil4b202592012-09-14 07:03:35 -0300692 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
693 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
694 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
695 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
696 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
697 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300698 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
Hans Verkuildd519bb2014-03-07 07:18:37 -0300699 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
Hans Verkuil4b202592012-09-14 07:03:35 -0300700 }
701 if (is_tx) {
702 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
703 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
704 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
705 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
706 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
707 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
708 }
Hans Verkuil4b202592012-09-14 07:03:35 -0300709 if (ops->vidioc_g_crop || ops->vidioc_g_selection)
710 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
711 if (ops->vidioc_s_crop || ops->vidioc_s_selection)
712 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
713 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
714 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
715 if (ops->vidioc_cropcap || ops->vidioc_g_selection)
716 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300717 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
Hans Verkuilca371572013-06-03 05:36:50 -0300718 ops->vidioc_g_std))
Hans Verkuil4b202592012-09-14 07:03:35 -0300719 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
720 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
Hans Verkuil4b202592012-09-14 07:03:35 -0300721 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
722 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
723 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
Hans Verkuil4b202592012-09-14 07:03:35 -0300724 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
Hans Verkuildd519bb2014-03-07 07:18:37 -0300725 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
Hans Verkuil4b202592012-09-14 07:03:35 -0300726 }
Hans Verkuilbfffd742013-12-20 02:32:21 -0300727 if (is_tx && (is_radio || is_sdr)) {
728 /* radio transmitter only ioctls */
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300729 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
730 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
731 }
732 if (is_rx) {
733 /* receiver only ioctls */
734 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
735 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
736 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
737 }
738
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300739 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
740 BASE_VIDIOC_PRIVATE);
741}
742
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300743/**
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300744 * __video_register_device - register video4linux devices
Hans Verkuildc93a702008-12-19 21:28:27 -0300745 * @vdev: video device structure we want to register
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300746 * @type: type of device to register
Hans Verkuil22e22122009-09-06 07:13:14 -0300747 * @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300748 * -1 == first free)
Hans Verkuil6b5270d2009-09-06 07:54:00 -0300749 * @warn_if_nr_in_use: warn if the desired device node number
750 * was already in use and another number was chosen instead.
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300751 * @owner: module that owns the video device node
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300752 *
Hans Verkuil22e22122009-09-06 07:13:14 -0300753 * The registration code assigns minor numbers and device node numbers
754 * based on the requested type and registers the new device node with
755 * the kernel.
Hans Verkuil46b63372010-12-31 11:47:23 -0300756 *
757 * This function assumes that struct video_device was zeroed when it
758 * was allocated and does not contain any stale date.
759 *
Hans Verkuil22e22122009-09-06 07:13:14 -0300760 * An error is returned if no free minor or device node number could be
761 * found, or if the registration of the device node failed.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300762 *
763 * Zero is returned on success.
764 *
765 * Valid types are
766 *
767 * %VFL_TYPE_GRABBER - A frame grabber
768 *
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300769 * %VFL_TYPE_VBI - Vertical blank data (undecoded)
770 *
771 * %VFL_TYPE_RADIO - A radio card
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300772 *
773 * %VFL_TYPE_SUBDEV - A subdevice
Antti Palosaarid42626b2013-12-11 20:03:07 -0300774 *
775 * %VFL_TYPE_SDR - Software Defined Radio
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300776 */
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300777int __video_register_device(struct video_device *vdev, int type, int nr,
778 int warn_if_nr_in_use, struct module *owner)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300779{
780 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300781 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300782 int minor_offset = 0;
783 int minor_cnt = VIDEO_NUM_DEVICES;
784 const char *name_base;
Hans Verkuildc93a702008-12-19 21:28:27 -0300785
786 /* A minor value of -1 marks this video device as never
787 having been registered */
Trent Piepho428c8d12009-03-03 18:51:52 -0300788 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300789
Hans Verkuild6e74972008-08-23 06:27:59 -0300790 /* the release callback MUST be present */
Guennadi Liakhovetski1fc2b5f2011-12-06 05:33:03 -0300791 if (WARN_ON(!vdev->release))
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300792 return -EINVAL;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300793 /* the v4l2_dev pointer MUST be present */
794 if (WARN_ON(!vdev->v4l2_dev))
795 return -EINVAL;
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300796
Sakari Ailus1babcb42010-03-23 09:25:26 -0300797 /* v4l2_fh support */
798 spin_lock_init(&vdev->fh_lock);
799 INIT_LIST_HEAD(&vdev->fh_list);
800
Hans Verkuildc93a702008-12-19 21:28:27 -0300801 /* Part 1: check device type */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300802 switch (type) {
803 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300804 name_base = "video";
805 break;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300806 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300807 name_base = "vbi";
808 break;
809 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300810 name_base = "radio";
811 break;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300812 case VFL_TYPE_SUBDEV:
813 name_base = "v4l-subdev";
814 break;
Antti Palosaarid42626b2013-12-11 20:03:07 -0300815 case VFL_TYPE_SDR:
816 /* Use device name 'swradio' because 'sdr' was already taken. */
817 name_base = "swradio";
818 break;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300819 default:
820 printk(KERN_ERR "%s called with unknown type: %d\n",
821 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300822 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300823 }
824
Hans Verkuildc93a702008-12-19 21:28:27 -0300825 vdev->vfl_type = type;
826 vdev->cdev = NULL;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300827 if (vdev->dev_parent == NULL)
828 vdev->dev_parent = vdev->v4l2_dev->dev;
829 if (vdev->ctrl_handler == NULL)
830 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
831 /* If the prio state pointer is NULL, then use the v4l2_device
832 prio state. */
833 if (vdev->prio == NULL)
834 vdev->prio = &vdev->v4l2_dev->prio;
Hans Verkuildd896012008-10-04 08:36:54 -0300835
Hans Verkuil22e22122009-09-06 07:13:14 -0300836 /* Part 2: find a free minor, device node number and device index. */
Hans Verkuildd896012008-10-04 08:36:54 -0300837#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
838 /* Keep the ranges for the first four types for historical
839 * reasons.
840 * Newer devices (not yet in place) should use the range
841 * of 128-191 and just pick the first free minor there
842 * (new style). */
843 switch (type) {
844 case VFL_TYPE_GRABBER:
845 minor_offset = 0;
846 minor_cnt = 64;
847 break;
848 case VFL_TYPE_RADIO:
849 minor_offset = 64;
850 minor_cnt = 64;
851 break;
Hans Verkuildd896012008-10-04 08:36:54 -0300852 case VFL_TYPE_VBI:
853 minor_offset = 224;
854 minor_cnt = 32;
855 break;
856 default:
857 minor_offset = 128;
858 minor_cnt = 64;
859 break;
860 }
861#endif
862
Hans Verkuil22e22122009-09-06 07:13:14 -0300863 /* Pick a device node number */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300864 mutex_lock(&videodev_lock);
Hans Verkuil5062cb72009-09-07 03:40:24 -0300865 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
Hans Verkuildd896012008-10-04 08:36:54 -0300866 if (nr == minor_cnt)
Hans Verkuil5062cb72009-09-07 03:40:24 -0300867 nr = devnode_find(vdev, 0, minor_cnt);
Hans Verkuildd896012008-10-04 08:36:54 -0300868 if (nr == minor_cnt) {
Hans Verkuil22e22122009-09-06 07:13:14 -0300869 printk(KERN_ERR "could not get a free device node number\n");
Hans Verkuildd896012008-10-04 08:36:54 -0300870 mutex_unlock(&videodev_lock);
871 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300872 }
Hans Verkuildd896012008-10-04 08:36:54 -0300873#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
Hans Verkuil22e22122009-09-06 07:13:14 -0300874 /* 1-on-1 mapping of device node number to minor number */
Hans Verkuildd896012008-10-04 08:36:54 -0300875 i = nr;
876#else
Hans Verkuil22e22122009-09-06 07:13:14 -0300877 /* The device node number and minor numbers are independent, so
878 we just find the first free minor number. */
Hans Verkuildd896012008-10-04 08:36:54 -0300879 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
880 if (video_device[i] == NULL)
881 break;
882 if (i == VIDEO_NUM_DEVICES) {
883 mutex_unlock(&videodev_lock);
884 printk(KERN_ERR "could not get a free minor\n");
885 return -ENFILE;
886 }
887#endif
Hans Verkuildc93a702008-12-19 21:28:27 -0300888 vdev->minor = i + minor_offset;
889 vdev->num = nr;
Hans Verkuil5062cb72009-09-07 03:40:24 -0300890 devnode_set(vdev);
891
Hans Verkuildc93a702008-12-19 21:28:27 -0300892 /* Should not happen since we thought this minor was free */
893 WARN_ON(video_device[vdev->minor] != NULL);
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300894 vdev->index = get_index(vdev);
Marek Szyprowski6c3df5d2013-12-03 10:14:29 -0300895 video_device[vdev->minor] = vdev;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300896 mutex_unlock(&videodev_lock);
897
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300898 if (vdev->ioctl_ops)
899 determine_valid_ioctls(vdev);
900
Hans Verkuildc93a702008-12-19 21:28:27 -0300901 /* Part 3: Initialize the character device */
902 vdev->cdev = cdev_alloc();
903 if (vdev->cdev == NULL) {
904 ret = -ENOMEM;
905 goto cleanup;
906 }
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200907 vdev->cdev->ops = &v4l2_fops;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300908 vdev->cdev->owner = owner;
Hans Verkuildc93a702008-12-19 21:28:27 -0300909 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300910 if (ret < 0) {
911 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300912 kfree(vdev->cdev);
913 vdev->cdev = NULL;
914 goto cleanup;
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300915 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300916
917 /* Part 4: register the device with sysfs */
Hans Verkuildc93a702008-12-19 21:28:27 -0300918 vdev->dev.class = &video_class;
919 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300920 vdev->dev.parent = vdev->dev_parent;
Hans Verkuil22e22122009-09-06 07:13:14 -0300921 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
Hans Verkuildc93a702008-12-19 21:28:27 -0300922 ret = device_register(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300923 if (ret < 0) {
924 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300925 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300926 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300927 /* Register the release callback that will be called when the last
928 reference to the device goes away. */
929 vdev->dev.release = v4l2_device_release;
930
Hans Verkuil6b5270d2009-09-06 07:54:00 -0300931 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
Laurent Pincharteac8ea52009-11-27 13:56:50 -0300932 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
933 name_base, nr, video_device_node_name(vdev));
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300934
935 /* Increase v4l2_device refcount */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300936 v4l2_device_get(vdev->v4l2_dev);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300937
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300938#if defined(CONFIG_MEDIA_CONTROLLER)
939 /* Part 5: Register the entity. */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300940 if (vdev->v4l2_dev->mdev &&
Laurent Pinchartc4f0b782011-04-08 09:04:06 -0300941 vdev->vfl_type != VFL_TYPE_SUBDEV) {
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300942 vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
943 vdev->entity.name = vdev->name;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300944 vdev->entity.info.v4l.major = VIDEO_MAJOR;
945 vdev->entity.info.v4l.minor = vdev->minor;
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300946 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
947 &vdev->entity);
948 if (ret < 0)
949 printk(KERN_WARNING
950 "%s: media_device_register_entity failed\n",
951 __func__);
952 }
953#endif
954 /* Part 6: Activate this minor. The char device can now be used. */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300955 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300956
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300957 return 0;
958
Hans Verkuildc93a702008-12-19 21:28:27 -0300959cleanup:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300960 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300961 if (vdev->cdev)
962 cdev_del(vdev->cdev);
Ricardo Ribalda1056e432013-08-13 11:04:06 -0300963 video_device[vdev->minor] = NULL;
Hans Verkuil5062cb72009-09-07 03:40:24 -0300964 devnode_clear(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300965 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300966 /* Mark this video device as never having been registered. */
967 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300968 return ret;
969}
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300970EXPORT_SYMBOL(__video_register_device);
Hans Verkuil6b5270d2009-09-06 07:54:00 -0300971
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300972/**
973 * video_unregister_device - unregister a video4linux device
Hans Verkuildc93a702008-12-19 21:28:27 -0300974 * @vdev: the device to unregister
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300975 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300976 * This unregisters the passed device. Future open calls will
977 * be met with errors.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300978 */
Hans Verkuildc93a702008-12-19 21:28:27 -0300979void video_unregister_device(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300980{
Hans Verkuildc93a702008-12-19 21:28:27 -0300981 /* Check if vdev was ever registered at all */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300982 if (!vdev || !video_is_registered(vdev))
Hans Verkuildc93a702008-12-19 21:28:27 -0300983 return;
984
Hans Verkuilca9afe62010-11-26 06:54:53 -0300985 mutex_lock(&videodev_lock);
986 /* This must be in a critical section to prevent a race with v4l2_open.
987 * Once this bit has been cleared video_get may never be called again.
988 */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300989 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
Hans Verkuilca9afe62010-11-26 06:54:53 -0300990 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300991 device_unregister(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300992}
993EXPORT_SYMBOL(video_unregister_device);
994
995/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300996 * Initialise video for linux
997 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300998static int __init videodev_init(void)
999{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001000 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001001 int ret;
1002
1003 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001004 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1005 if (ret < 0) {
1006 printk(KERN_WARNING "videodev: unable to get major %d\n",
1007 VIDEO_MAJOR);
1008 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001009 }
1010
1011 ret = class_register(&video_class);
1012 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001013 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001014 printk(KERN_WARNING "video_dev: class_register failed\n");
1015 return -EIO;
1016 }
1017
1018 return 0;
1019}
1020
1021static void __exit videodev_exit(void)
1022{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001023 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1024
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001025 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001026 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001027}
1028
Bhupesh Sharmaee981c62012-03-12 05:09:02 -03001029subsys_initcall(videodev_init);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001030module_exit(videodev_exit)
1031
1032MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1033MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1034MODULE_LICENSE("GPL");
Scott James Remnantcbb72b02009-03-02 15:40:57 -03001035MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001036
1037
1038/*
1039 * Local variables:
1040 * c-basic-offset: 8
1041 * End:
1042 */