blob: fa2124cb31bd380436734211319a291ad135146e [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/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
Hans Verkuil17028cd2014-12-01 10:10:44 -030050static ssize_t dev_debug_show(struct device *cd,
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070051 struct device_attribute *attr, char *buf)
Hans Verkuil80131fe02012-06-22 06:37:38 -030052{
53 struct video_device *vdev = to_video_device(cd);
54
Hans Verkuil17028cd2014-12-01 10:10:44 -030055 return sprintf(buf, "%i\n", vdev->dev_debug);
Hans Verkuil80131fe02012-06-22 06:37:38 -030056}
57
Hans Verkuil17028cd2014-12-01 10:10:44 -030058static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070059 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
Hans Verkuil17028cd2014-12-01 10:10:44 -030069 vdev->dev_debug = value;
Hans Verkuil80131fe02012-06-22 06:37:38 -030070 return len;
71}
Hans Verkuil17028cd2014-12-01 10:10:44 -030072static DEVICE_ATTR_RW(dev_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,
Hans Verkuil17028cd2014-12-01 10:10:44 -030085 &dev_attr_dev_debug.attr,
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -070086 &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)
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300197 if (v4l2_dev->mdev) {
198 /* Remove interfaces and interface links */
199 media_devnode_remove(vdev->intf_devnode);
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200200 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300201 media_device_unregister_entity(&vdev->entity);
202 }
Laurent Pinchartc064b8e2011-06-15 05:20:34 -0300203#endif
204
Hans Verkuil8280b662011-09-06 10:23:18 -0300205 /* Do not call v4l2_device_put if there is no release callback set.
206 * Drivers that have no v4l2_device release callback might free the
207 * v4l2_dev instance in the video_device release callback below, so we
208 * must perform this check here.
209 *
210 * TODO: In the long run all drivers that use v4l2_device should use the
211 * v4l2_device release callback. This check will then be unnecessary.
212 */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300213 if (v4l2_dev->release == NULL)
Hans Verkuil8280b662011-09-06 10:23:18 -0300214 v4l2_dev = NULL;
215
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300216 /* Release video_device and perform other
217 cleanups as needed. */
Hans Verkuildc93a702008-12-19 21:28:27 -0300218 vdev->release(vdev);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300219
220 /* Decrease v4l2_device refcount */
221 if (v4l2_dev)
222 v4l2_device_put(v4l2_dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300223}
224
225static struct class video_class = {
226 .name = VIDEO_NAME,
Greg Kroah-Hartman13e22372013-07-24 15:05:14 -0700227 .dev_groups = video_device_groups,
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300228};
229
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300230struct video_device *video_devdata(struct file *file)
231{
Al Viro496ad9a2013-01-23 17:07:38 -0500232 return video_device[iminor(file_inode(file))];
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300233}
234EXPORT_SYMBOL(video_devdata);
235
Hans Verkuil02265492010-12-29 10:05:02 -0300236
237/* Priority handling */
238
239static inline bool prio_is_valid(enum v4l2_priority prio)
240{
241 return prio == V4L2_PRIORITY_BACKGROUND ||
242 prio == V4L2_PRIORITY_INTERACTIVE ||
243 prio == V4L2_PRIORITY_RECORD;
244}
245
246void v4l2_prio_init(struct v4l2_prio_state *global)
247{
248 memset(global, 0, sizeof(*global));
249}
250EXPORT_SYMBOL(v4l2_prio_init);
251
252int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
253 enum v4l2_priority new)
254{
255 if (!prio_is_valid(new))
256 return -EINVAL;
257 if (*local == new)
258 return 0;
259
260 atomic_inc(&global->prios[new]);
261 if (prio_is_valid(*local))
262 atomic_dec(&global->prios[*local]);
263 *local = new;
264 return 0;
265}
266EXPORT_SYMBOL(v4l2_prio_change);
267
268void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
269{
270 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
271}
272EXPORT_SYMBOL(v4l2_prio_open);
273
274void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
275{
276 if (prio_is_valid(local))
277 atomic_dec(&global->prios[local]);
278}
279EXPORT_SYMBOL(v4l2_prio_close);
280
281enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
282{
283 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
284 return V4L2_PRIORITY_RECORD;
285 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
286 return V4L2_PRIORITY_INTERACTIVE;
287 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
288 return V4L2_PRIORITY_BACKGROUND;
289 return V4L2_PRIORITY_UNSET;
290}
291EXPORT_SYMBOL(v4l2_prio_max);
292
293int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
294{
295 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
296}
297EXPORT_SYMBOL(v4l2_prio_check);
298
299
Hans Verkuildc93a702008-12-19 21:28:27 -0300300static ssize_t v4l2_read(struct file *filp, char __user *buf,
301 size_t sz, loff_t *off)
302{
303 struct video_device *vdev = video_devdata(filp);
Hans Verkuil28778422010-11-26 06:43:51 -0300304 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300305
306 if (!vdev->fops->read)
307 return -EINVAL;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300308 if (video_is_registered(vdev))
309 ret = vdev->fops->read(filp, buf, sz, off);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300310 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
311 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300312 printk(KERN_DEBUG "%s: read: %zd (%d)\n",
313 video_device_node_name(vdev), sz, ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300314 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300315}
316
317static ssize_t v4l2_write(struct file *filp, const char __user *buf,
318 size_t sz, loff_t *off)
319{
320 struct video_device *vdev = video_devdata(filp);
Hans Verkuil28778422010-11-26 06:43:51 -0300321 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300322
323 if (!vdev->fops->write)
324 return -EINVAL;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300325 if (video_is_registered(vdev))
326 ret = vdev->fops->write(filp, buf, sz, off);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300327 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
328 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300329 printk(KERN_DEBUG "%s: write: %zd (%d)\n",
330 video_device_node_name(vdev), sz, ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300331 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300332}
333
334static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
335{
336 struct video_device *vdev = video_devdata(filp);
Hans Verkuilcf533732012-07-31 04:02:25 -0300337 unsigned int res = POLLERR | POLLHUP;
Hans Verkuildc93a702008-12-19 21:28:27 -0300338
Hans Verkuilee6869a2010-09-26 08:47:38 -0300339 if (!vdev->fops->poll)
Hans Verkuil28778422010-11-26 06:43:51 -0300340 return DEFAULT_POLLMASK;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300341 if (video_is_registered(vdev))
Hans Verkuilcf533732012-07-31 04:02:25 -0300342 res = vdev->fops->poll(filp, poll);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300343 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300344 printk(KERN_DEBUG "%s: poll: %08x\n",
Hans Verkuilcf533732012-07-31 04:02:25 -0300345 video_device_node_name(vdev), res);
346 return res;
Hans Verkuildc93a702008-12-19 21:28:27 -0300347}
348
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200349static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Hans Verkuildc93a702008-12-19 21:28:27 -0300350{
351 struct video_device *vdev = video_devdata(filp);
Mauro Carvalho Chehab72420632010-10-09 16:43:40 -0300352 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300353
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200354 if (vdev->fops->unlocked_ioctl) {
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300355 struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
Hans Verkuil8ab75e32012-05-10 02:51:31 -0300356
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300357 if (lock && mutex_lock_interruptible(lock))
358 return -ERESTARTSYS;
Mauro Carvalho Chehab72420632010-10-09 16:43:40 -0300359 if (video_is_registered(vdev))
360 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
Hans Verkuil5a5adf62012-06-22 07:29:35 -0300361 if (lock)
362 mutex_unlock(lock);
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200363 } else
364 ret = -ENOTTY;
Hans Verkuildc93a702008-12-19 21:28:27 -0300365
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200366 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300367}
368
Bob Liuecc65172011-05-06 05:20:09 -0300369#ifdef CONFIG_MMU
370#define v4l2_get_unmapped_area NULL
371#else
372static unsigned long v4l2_get_unmapped_area(struct file *filp,
373 unsigned long addr, unsigned long len, unsigned long pgoff,
374 unsigned long flags)
375{
376 struct video_device *vdev = video_devdata(filp);
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300377 int ret;
Bob Liuecc65172011-05-06 05:20:09 -0300378
379 if (!vdev->fops->get_unmapped_area)
380 return -ENOSYS;
381 if (!video_is_registered(vdev))
382 return -ENODEV;
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300383 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300384 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300385 printk(KERN_DEBUG "%s: get_unmapped_area (%d)\n",
386 video_device_node_name(vdev), ret);
387 return ret;
Bob Liuecc65172011-05-06 05:20:09 -0300388}
389#endif
390
Hans Verkuildc93a702008-12-19 21:28:27 -0300391static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
392{
393 struct video_device *vdev = video_devdata(filp);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300394 int ret = -ENODEV;
Hans Verkuildc93a702008-12-19 21:28:27 -0300395
Hans Verkuilee6869a2010-09-26 08:47:38 -0300396 if (!vdev->fops->mmap)
Hans Verkuilcf533732012-07-31 04:02:25 -0300397 return -ENODEV;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300398 if (video_is_registered(vdev))
399 ret = vdev->fops->mmap(filp, vm);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300400 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300401 printk(KERN_DEBUG "%s: mmap (%d)\n",
402 video_device_node_name(vdev), ret);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300403 return ret;
Hans Verkuildc93a702008-12-19 21:28:27 -0300404}
405
406/* Override for the open function */
407static int v4l2_open(struct inode *inode, struct file *filp)
408{
409 struct video_device *vdev;
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300410 int ret = 0;
Hans Verkuildc93a702008-12-19 21:28:27 -0300411
412 /* Check if the video device is available */
413 mutex_lock(&videodev_lock);
414 vdev = video_devdata(filp);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300415 /* return ENODEV if the video device has already been removed. */
Hans Verkuilca9afe62010-11-26 06:54:53 -0300416 if (vdev == NULL || !video_is_registered(vdev)) {
Hans Verkuildc93a702008-12-19 21:28:27 -0300417 mutex_unlock(&videodev_lock);
418 return -ENODEV;
419 }
420 /* and increase the device refcount */
421 video_get(vdev);
422 mutex_unlock(&videodev_lock);
Hans Verkuilee6869a2010-09-26 08:47:38 -0300423 if (vdev->fops->open) {
Hans Verkuilee6869a2010-09-26 08:47:38 -0300424 if (video_is_registered(vdev))
425 ret = vdev->fops->open(filp);
426 else
427 ret = -ENODEV;
Hans Verkuilee6869a2010-09-26 08:47:38 -0300428 }
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300429
Hans Verkuil17028cd2014-12-01 10:10:44 -0300430 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300431 printk(KERN_DEBUG "%s: open (%d)\n",
432 video_device_node_name(vdev), ret);
Ezequiel García8f695d32012-07-26 07:59:04 -0300433 /* decrease the refcount in case of an error */
434 if (ret)
435 video_put(vdev);
Hans Verkuildc93a702008-12-19 21:28:27 -0300436 return ret;
437}
438
439/* Override for the release function */
440static int v4l2_release(struct inode *inode, struct file *filp)
441{
442 struct video_device *vdev = video_devdata(filp);
Hans Verkuil65d9ff92009-03-29 20:19:38 -0300443 int ret = 0;
444
Hans Verkuilcf533732012-07-31 04:02:25 -0300445 if (vdev->fops->release)
446 ret = vdev->fops->release(filp);
Hans Verkuil17028cd2014-12-01 10:10:44 -0300447 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
Hans Verkuilcc4b7e72012-06-09 12:13:29 -0300448 printk(KERN_DEBUG "%s: release\n",
449 video_device_node_name(vdev));
Hans Verkuilcf533732012-07-31 04:02:25 -0300450
Ezequiel García8f695d32012-07-26 07:59:04 -0300451 /* decrease the refcount unconditionally since the release()
452 return value is ignored. */
453 video_put(vdev);
Hans Verkuildc93a702008-12-19 21:28:27 -0300454 return ret;
455}
456
Hans Verkuildc93a702008-12-19 21:28:27 -0300457static const struct file_operations v4l2_fops = {
458 .owner = THIS_MODULE,
459 .read = v4l2_read,
460 .write = v4l2_write,
461 .open = v4l2_open,
Bob Liuecc65172011-05-06 05:20:09 -0300462 .get_unmapped_area = v4l2_get_unmapped_area,
Hans Verkuildc93a702008-12-19 21:28:27 -0300463 .mmap = v4l2_mmap,
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200464 .unlocked_ioctl = v4l2_ioctl,
Hans Verkuildc93a702008-12-19 21:28:27 -0300465#ifdef CONFIG_COMPAT
Hans Verkuil9bb7cde2008-12-30 06:42:40 -0300466 .compat_ioctl = v4l2_compat_ioctl32,
Hans Verkuildc93a702008-12-19 21:28:27 -0300467#endif
468 .release = v4l2_release,
469 .poll = v4l2_poll,
470 .llseek = no_llseek,
471};
472
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300473/**
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300474 * get_index - assign stream index number based on v4l2_dev
475 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300476 *
Hans Verkuildc93a702008-12-19 21:28:27 -0300477 * Note that when this is called the new device has not yet been registered
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300478 * in the video_device array, but it was able to obtain a minor number.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300479 *
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300480 * This means that we can always obtain a free stream index number since
481 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
482 * use of the video_device array.
483 *
484 * Returns a free index number.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300485 */
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300486static int get_index(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300487{
Hans Verkuil775a05d2009-02-14 11:31:01 -0300488 /* This can be static since this function is called with the global
489 videodev_lock held. */
490 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300491 int i;
492
Hans Verkuil775a05d2009-02-14 11:31:01 -0300493 bitmap_zero(used, VIDEO_NUM_DEVICES);
Hans Verkuil806e5b72008-12-19 09:10:56 -0300494
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300495 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
496 if (video_device[i] != NULL &&
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300497 video_device[i]->v4l2_dev == vdev->v4l2_dev) {
Hans Verkuil775a05d2009-02-14 11:31:01 -0300498 set_bit(video_device[i]->index, used);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300499 }
500 }
501
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300502 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300503}
504
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300505#define SET_VALID_IOCTL(ops, cmd, op) \
506 if (ops->op) \
507 set_bit(_IOC_NR(cmd), valid_ioctls)
508
509/* This determines which ioctls are actually implemented in the driver.
510 It's a one-time thing which simplifies video_ioctl2 as it can just do
511 a bit test.
512
513 Note that drivers can override this by setting bits to 1 in
514 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
515 called, then that ioctl will actually be marked as unimplemented.
516
517 It does that by first setting up the local valid_ioctls bitmap, and
518 at the end do a:
519
520 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
521 */
522static void determine_valid_ioctls(struct video_device *vdev)
523{
524 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
525 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
Hans Verkuil4b202592012-09-14 07:03:35 -0300526 bool is_vid = vdev->vfl_type == VFL_TYPE_GRABBER;
527 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
Hans Verkuilbfffd742013-12-20 02:32:21 -0300528 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300529 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300530 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
Hans Verkuil4b202592012-09-14 07:03:35 -0300531 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
532 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300533
534 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
535
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300536 /* vfl_type and vfl_dir independent ioctls */
537
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300538 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
Hans Verkuil2438e782015-02-03 10:46:56 -0300539 set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
540 set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
541
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300542 /* Note: the control handler can also be passed through the filehandle,
543 and that can't be tested here. If the bit for these control ioctls
544 is set, then the ioctl is valid. But if it is 0, then it can still
545 be valid if the filehandle passed the control handler. */
546 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
547 set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
Hans Verkuile6bee362014-06-10 04:22:06 -0300548 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
549 set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300550 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
551 set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
552 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
553 set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
554 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
555 set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
556 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
557 set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
558 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
559 set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
560 if (vdev->ctrl_handler || ops->vidioc_querymenu)
561 set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
562 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
563 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
564 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
565#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil96b03d22013-04-06 06:16:58 -0300566 set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
Hans Verkuil79b0c642013-03-18 12:16:34 -0300567 set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
568 set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300569#endif
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300570 /* yes, really vidioc_subscribe_event */
571 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
572 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
573 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300574 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
575 set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
576
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300577 if (is_vid || is_tch) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300578 /* video specific ioctls */
Hans Verkuil4b202592012-09-14 07:03:35 -0300579 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
580 ops->vidioc_enum_fmt_vid_cap_mplane ||
581 ops->vidioc_enum_fmt_vid_overlay)) ||
582 (is_tx && (ops->vidioc_enum_fmt_vid_out ||
583 ops->vidioc_enum_fmt_vid_out_mplane)))
584 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
585 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
586 ops->vidioc_g_fmt_vid_cap_mplane ||
587 ops->vidioc_g_fmt_vid_overlay)) ||
588 (is_tx && (ops->vidioc_g_fmt_vid_out ||
589 ops->vidioc_g_fmt_vid_out_mplane ||
590 ops->vidioc_g_fmt_vid_out_overlay)))
591 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
592 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
593 ops->vidioc_s_fmt_vid_cap_mplane ||
594 ops->vidioc_s_fmt_vid_overlay)) ||
595 (is_tx && (ops->vidioc_s_fmt_vid_out ||
596 ops->vidioc_s_fmt_vid_out_mplane ||
597 ops->vidioc_s_fmt_vid_out_overlay)))
598 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
599 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
600 ops->vidioc_try_fmt_vid_cap_mplane ||
601 ops->vidioc_try_fmt_vid_overlay)) ||
602 (is_tx && (ops->vidioc_try_fmt_vid_out ||
603 ops->vidioc_try_fmt_vid_out_mplane ||
604 ops->vidioc_try_fmt_vid_out_overlay)))
605 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300606 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
607 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
608 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
609 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
610 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
611 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
612 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
613 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
614 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
615 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
616 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
617 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
Hans Verkuil2c9fc462015-03-13 13:25:53 -0300618 if (ops->vidioc_g_crop || ops->vidioc_g_selection)
619 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
620 if (ops->vidioc_s_crop || ops->vidioc_s_selection)
621 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
622 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
623 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
624 if (ops->vidioc_cropcap || ops->vidioc_g_selection)
625 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300626 } else if (is_vbi) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300627 /* vbi specific ioctls */
Hans Verkuil4b202592012-09-14 07:03:35 -0300628 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
629 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
630 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
631 ops->vidioc_g_fmt_sliced_vbi_out)))
632 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
633 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
634 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
635 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
636 ops->vidioc_s_fmt_sliced_vbi_out)))
637 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
638 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
639 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
640 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
641 ops->vidioc_try_fmt_sliced_vbi_out)))
642 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300643 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
Antti Palosaari9effc722015-10-10 13:51:00 -0300644 } else if (is_sdr && is_rx) {
645 /* SDR receiver specific ioctls */
Antti Palosaari582c52c2013-12-12 13:44:14 -0300646 if (ops->vidioc_enum_fmt_sdr_cap)
647 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
648 if (ops->vidioc_g_fmt_sdr_cap)
649 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
650 if (ops->vidioc_s_fmt_sdr_cap)
651 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
652 if (ops->vidioc_try_fmt_sdr_cap)
653 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Antti Palosaari9effc722015-10-10 13:51:00 -0300654 } else if (is_sdr && is_tx) {
655 /* SDR transmitter specific ioctls */
656 if (ops->vidioc_enum_fmt_sdr_out)
657 set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
658 if (ops->vidioc_g_fmt_sdr_out)
659 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
660 if (ops->vidioc_s_fmt_sdr_out)
661 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
662 if (ops->vidioc_try_fmt_sdr_out)
663 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300664 }
Antti Palosaari582c52c2013-12-12 13:44:14 -0300665
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300666 if (is_vid || is_vbi || is_sdr || is_tch) {
Antti Palosaari582c52c2013-12-12 13:44:14 -0300667 /* ioctls valid for video, vbi or sdr */
Frank Schaefer5815d0c2013-01-22 15:51:55 -0300668 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
669 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
670 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
671 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
672 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
673 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
674 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
Hans Verkuil72be89c2014-07-10 19:51:26 -0300675 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
676 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
Antti Palosaari582c52c2013-12-12 13:44:14 -0300677 }
678
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300679 if (is_vid || is_vbi || is_tch) {
Antti Palosaari582c52c2013-12-12 13:44:14 -0300680 /* ioctls valid for video or vbi */
Hans Verkuil4b202592012-09-14 07:03:35 -0300681 if (ops->vidioc_s_std)
682 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
Hans Verkuil4b202592012-09-14 07:03:35 -0300683 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
Hans Verkuilca371572013-06-03 05:36:50 -0300684 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
Hans Verkuil4b202592012-09-14 07:03:35 -0300685 if (is_rx) {
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300686 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
Hans Verkuil4b202592012-09-14 07:03:35 -0300687 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
688 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
689 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
690 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
691 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
692 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300693 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
Hans Verkuildd519bb2014-03-07 07:18:37 -0300694 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
Hans Verkuil4b202592012-09-14 07:03:35 -0300695 }
696 if (is_tx) {
697 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
698 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
699 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
700 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
701 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
702 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
703 }
Hans Verkuil4b202592012-09-14 07:03:35 -0300704 if (ops->vidioc_g_parm || (vdev->vfl_type == VFL_TYPE_GRABBER &&
Hans Verkuilca371572013-06-03 05:36:50 -0300705 ops->vidioc_g_std))
Hans Verkuil4b202592012-09-14 07:03:35 -0300706 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
707 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
Hans Verkuil4b202592012-09-14 07:03:35 -0300708 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
709 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
710 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
Hans Verkuil4b202592012-09-14 07:03:35 -0300711 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
Hans Verkuildd519bb2014-03-07 07:18:37 -0300712 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
Hans Verkuil4b202592012-09-14 07:03:35 -0300713 }
Hans Verkuilbfffd742013-12-20 02:32:21 -0300714 if (is_tx && (is_radio || is_sdr)) {
715 /* radio transmitter only ioctls */
Hans Verkuil90d0fc42012-09-14 07:06:08 -0300716 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
717 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
718 }
719 if (is_rx) {
720 /* receiver only ioctls */
721 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
722 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
723 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
724 }
725
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300726 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
727 BASE_VIDIOC_PRIVATE);
728}
729
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300730static int video_register_media_controller(struct video_device *vdev, int type)
731{
732#if defined(CONFIG_MEDIA_CONTROLLER)
733 u32 intf_type;
734 int ret;
735
736 if (!vdev->v4l2_dev->mdev)
737 return 0;
738
Laurent Pinchartb76a2a82016-02-29 08:45:44 -0300739 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200740 vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300741
742 switch (type) {
743 case VFL_TYPE_GRABBER:
744 intf_type = MEDIA_INTF_T_V4L_VIDEO;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200745 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300746 break;
747 case VFL_TYPE_VBI:
748 intf_type = MEDIA_INTF_T_V4L_VBI;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200749 vdev->entity.function = MEDIA_ENT_F_IO_VBI;
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300750 break;
751 case VFL_TYPE_SDR:
752 intf_type = MEDIA_INTF_T_V4L_SWRADIO;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200753 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300754 break;
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300755 case VFL_TYPE_TOUCH:
756 intf_type = MEDIA_INTF_T_V4L_TOUCH;
757 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
758 break;
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300759 case VFL_TYPE_RADIO:
760 intf_type = MEDIA_INTF_T_V4L_RADIO;
761 /*
762 * Radio doesn't have an entity at the V4L2 side to represent
763 * radio input or output. Instead, the audio input/output goes
764 * via either physical wires or ALSA.
765 */
766 break;
767 case VFL_TYPE_SUBDEV:
768 intf_type = MEDIA_INTF_T_V4L_SUBDEV;
769 /* Entity will be created via v4l2_device_register_subdev() */
770 break;
771 default:
772 return 0;
773 }
774
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200775 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300776 vdev->entity.name = vdev->name;
777
778 /* Needed just for backward compatibility with legacy MC API */
779 vdev->entity.info.dev.major = VIDEO_MAJOR;
780 vdev->entity.info.dev.minor = vdev->minor;
781
782 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
783 &vdev->entity);
784 if (ret < 0) {
785 printk(KERN_WARNING
786 "%s: media_device_register_entity failed\n",
787 __func__);
788 return ret;
789 }
790 }
791
792 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
793 intf_type,
794 0, VIDEO_MAJOR,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300795 vdev->minor);
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300796 if (!vdev->intf_devnode) {
797 media_device_unregister_entity(&vdev->entity);
798 return -ENOMEM;
799 }
800
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200801 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300802 struct media_link *link;
803
804 link = media_create_intf_link(&vdev->entity,
Mauro Carvalho Chehab13f6e882015-09-04 15:39:43 -0300805 &vdev->intf_devnode->intf,
806 MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300807 if (!link) {
808 media_devnode_remove(vdev->intf_devnode);
809 media_device_unregister_entity(&vdev->entity);
810 return -ENOMEM;
811 }
812 }
813
814 /* FIXME: how to create the other interface links? */
815
816#endif
817 return 0;
818}
819
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300820int __video_register_device(struct video_device *vdev, int type, int nr,
821 int warn_if_nr_in_use, struct module *owner)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300822{
823 int i = 0;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300824 int ret;
Hans Verkuildd896012008-10-04 08:36:54 -0300825 int minor_offset = 0;
826 int minor_cnt = VIDEO_NUM_DEVICES;
827 const char *name_base;
Hans Verkuildc93a702008-12-19 21:28:27 -0300828
829 /* A minor value of -1 marks this video device as never
830 having been registered */
Trent Piepho428c8d12009-03-03 18:51:52 -0300831 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300832
Hans Verkuild6e74972008-08-23 06:27:59 -0300833 /* the release callback MUST be present */
Guennadi Liakhovetski1fc2b5f2011-12-06 05:33:03 -0300834 if (WARN_ON(!vdev->release))
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300835 return -EINVAL;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300836 /* the v4l2_dev pointer MUST be present */
837 if (WARN_ON(!vdev->v4l2_dev))
838 return -EINVAL;
Henrik Kretzschmaree7aa9f2008-08-22 16:41:03 -0300839
Sakari Ailus1babcb42010-03-23 09:25:26 -0300840 /* v4l2_fh support */
841 spin_lock_init(&vdev->fh_lock);
842 INIT_LIST_HEAD(&vdev->fh_list);
843
Hans Verkuildc93a702008-12-19 21:28:27 -0300844 /* Part 1: check device type */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300845 switch (type) {
846 case VFL_TYPE_GRABBER:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300847 name_base = "video";
848 break;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300849 case VFL_TYPE_VBI:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300850 name_base = "vbi";
851 break;
852 case VFL_TYPE_RADIO:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300853 name_base = "radio";
854 break;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300855 case VFL_TYPE_SUBDEV:
856 name_base = "v4l-subdev";
857 break;
Antti Palosaarid42626b2013-12-11 20:03:07 -0300858 case VFL_TYPE_SDR:
859 /* Use device name 'swradio' because 'sdr' was already taken. */
860 name_base = "swradio";
861 break;
Nick Dyerb2fe22d2016-07-18 18:10:30 -0300862 case VFL_TYPE_TOUCH:
863 name_base = "v4l-touch";
864 break;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300865 default:
866 printk(KERN_ERR "%s called with unknown type: %d\n",
867 __func__, type);
Henrik Kretzschmar46f2c212008-09-03 16:47:39 -0300868 return -EINVAL;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300869 }
870
Hans Verkuildc93a702008-12-19 21:28:27 -0300871 vdev->vfl_type = type;
872 vdev->cdev = NULL;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300873 if (vdev->dev_parent == NULL)
874 vdev->dev_parent = vdev->v4l2_dev->dev;
875 if (vdev->ctrl_handler == NULL)
876 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
877 /* If the prio state pointer is NULL, then use the v4l2_device
878 prio state. */
879 if (vdev->prio == NULL)
880 vdev->prio = &vdev->v4l2_dev->prio;
Hans Verkuildd896012008-10-04 08:36:54 -0300881
Hans Verkuil22e22122009-09-06 07:13:14 -0300882 /* Part 2: find a free minor, device node number and device index. */
Hans Verkuildd896012008-10-04 08:36:54 -0300883#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
884 /* Keep the ranges for the first four types for historical
885 * reasons.
886 * Newer devices (not yet in place) should use the range
887 * of 128-191 and just pick the first free minor there
888 * (new style). */
889 switch (type) {
890 case VFL_TYPE_GRABBER:
891 minor_offset = 0;
892 minor_cnt = 64;
893 break;
894 case VFL_TYPE_RADIO:
895 minor_offset = 64;
896 minor_cnt = 64;
897 break;
Hans Verkuildd896012008-10-04 08:36:54 -0300898 case VFL_TYPE_VBI:
899 minor_offset = 224;
900 minor_cnt = 32;
901 break;
902 default:
903 minor_offset = 128;
904 minor_cnt = 64;
905 break;
906 }
907#endif
908
Hans Verkuil22e22122009-09-06 07:13:14 -0300909 /* Pick a device node number */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300910 mutex_lock(&videodev_lock);
Hans Verkuil5062cb72009-09-07 03:40:24 -0300911 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
Hans Verkuildd896012008-10-04 08:36:54 -0300912 if (nr == minor_cnt)
Hans Verkuil5062cb72009-09-07 03:40:24 -0300913 nr = devnode_find(vdev, 0, minor_cnt);
Hans Verkuildd896012008-10-04 08:36:54 -0300914 if (nr == minor_cnt) {
Hans Verkuil22e22122009-09-06 07:13:14 -0300915 printk(KERN_ERR "could not get a free device node number\n");
Hans Verkuildd896012008-10-04 08:36:54 -0300916 mutex_unlock(&videodev_lock);
917 return -ENFILE;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300918 }
Hans Verkuildd896012008-10-04 08:36:54 -0300919#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
Hans Verkuil22e22122009-09-06 07:13:14 -0300920 /* 1-on-1 mapping of device node number to minor number */
Hans Verkuildd896012008-10-04 08:36:54 -0300921 i = nr;
922#else
Hans Verkuil22e22122009-09-06 07:13:14 -0300923 /* The device node number and minor numbers are independent, so
924 we just find the first free minor number. */
Hans Verkuildd896012008-10-04 08:36:54 -0300925 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
926 if (video_device[i] == NULL)
927 break;
928 if (i == VIDEO_NUM_DEVICES) {
929 mutex_unlock(&videodev_lock);
930 printk(KERN_ERR "could not get a free minor\n");
931 return -ENFILE;
932 }
933#endif
Hans Verkuildc93a702008-12-19 21:28:27 -0300934 vdev->minor = i + minor_offset;
935 vdev->num = nr;
Hans Verkuil5062cb72009-09-07 03:40:24 -0300936 devnode_set(vdev);
937
Hans Verkuildc93a702008-12-19 21:28:27 -0300938 /* Should not happen since we thought this minor was free */
939 WARN_ON(video_device[vdev->minor] != NULL);
Hans Verkuil7ae0cd92009-06-19 11:32:56 -0300940 vdev->index = get_index(vdev);
Marek Szyprowski6c3df5d2013-12-03 10:14:29 -0300941 video_device[vdev->minor] = vdev;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300942 mutex_unlock(&videodev_lock);
943
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300944 if (vdev->ioctl_ops)
945 determine_valid_ioctls(vdev);
946
Hans Verkuildc93a702008-12-19 21:28:27 -0300947 /* Part 3: Initialize the character device */
948 vdev->cdev = cdev_alloc();
949 if (vdev->cdev == NULL) {
950 ret = -ENOMEM;
951 goto cleanup;
952 }
Arnd Bergmann86a5ef72010-07-04 00:15:09 +0200953 vdev->cdev->ops = &v4l2_fops;
Laurent Pinchart2096a5d2009-12-09 08:38:49 -0300954 vdev->cdev->owner = owner;
Hans Verkuildc93a702008-12-19 21:28:27 -0300955 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300956 if (ret < 0) {
957 printk(KERN_ERR "%s: cdev_add failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300958 kfree(vdev->cdev);
959 vdev->cdev = NULL;
960 goto cleanup;
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -0300961 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300962
963 /* Part 4: register the device with sysfs */
Hans Verkuildc93a702008-12-19 21:28:27 -0300964 vdev->dev.class = &video_class;
965 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
Hans Verkuil1c1d86a2013-06-12 11:15:12 -0300966 vdev->dev.parent = vdev->dev_parent;
Hans Verkuil22e22122009-09-06 07:13:14 -0300967 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
Hans Verkuildc93a702008-12-19 21:28:27 -0300968 ret = device_register(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300969 if (ret < 0) {
970 printk(KERN_ERR "%s: device_register failed\n", __func__);
Hans Verkuildc93a702008-12-19 21:28:27 -0300971 goto cleanup;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300972 }
Hans Verkuildc93a702008-12-19 21:28:27 -0300973 /* Register the release callback that will be called when the last
974 reference to the device goes away. */
975 vdev->dev.release = v4l2_device_release;
976
Hans Verkuil6b5270d2009-09-06 07:54:00 -0300977 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
Laurent Pincharteac8ea52009-11-27 13:56:50 -0300978 printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
979 name_base, nr, video_device_node_name(vdev));
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300980
981 /* Increase v4l2_device refcount */
Hans Verkuild9bfbcc2014-11-23 09:14:01 -0300982 v4l2_device_get(vdev->v4l2_dev);
Hans Verkuilbedf8bc2011-03-12 06:37:19 -0300983
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300984 /* Part 5: Register the entity. */
Mauro Carvalho Chehabd9c21e32015-08-24 08:47:54 -0300985 ret = video_register_media_controller(vdev, type);
986
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300987 /* Part 6: Activate this minor. The char device can now be used. */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -0300988 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
Laurent Pinchart2c0ab672009-12-09 08:40:10 -0300989
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300990 return 0;
991
Hans Verkuildc93a702008-12-19 21:28:27 -0300992cleanup:
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300993 mutex_lock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300994 if (vdev->cdev)
995 cdev_del(vdev->cdev);
Ricardo Ribalda1056e432013-08-13 11:04:06 -0300996 video_device[vdev->minor] = NULL;
Hans Verkuil5062cb72009-09-07 03:40:24 -0300997 devnode_clear(vdev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -0300998 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -0300999 /* Mark this video device as never having been registered. */
1000 vdev->minor = -1;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001001 return ret;
1002}
Laurent Pinchart2096a5d2009-12-09 08:38:49 -03001003EXPORT_SYMBOL(__video_register_device);
Hans Verkuil6b5270d2009-09-06 07:54:00 -03001004
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001005/**
1006 * video_unregister_device - unregister a video4linux device
Hans Verkuildc93a702008-12-19 21:28:27 -03001007 * @vdev: the device to unregister
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001008 *
Hans Verkuildc93a702008-12-19 21:28:27 -03001009 * This unregisters the passed device. Future open calls will
1010 * be met with errors.
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001011 */
Hans Verkuildc93a702008-12-19 21:28:27 -03001012void video_unregister_device(struct video_device *vdev)
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001013{
Hans Verkuildc93a702008-12-19 21:28:27 -03001014 /* Check if vdev was ever registered at all */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -03001015 if (!vdev || !video_is_registered(vdev))
Hans Verkuildc93a702008-12-19 21:28:27 -03001016 return;
1017
Hans Verkuilca9afe62010-11-26 06:54:53 -03001018 mutex_lock(&videodev_lock);
1019 /* This must be in a critical section to prevent a race with v4l2_open.
1020 * Once this bit has been cleared video_get may never be called again.
1021 */
Laurent Pinchart957b4aa2009-11-27 13:57:22 -03001022 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
Hans Verkuilca9afe62010-11-26 06:54:53 -03001023 mutex_unlock(&videodev_lock);
Hans Verkuildc93a702008-12-19 21:28:27 -03001024 device_unregister(&vdev->dev);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001025}
1026EXPORT_SYMBOL(video_unregister_device);
1027
1028/*
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001029 * Initialise video for linux
1030 */
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001031static int __init videodev_init(void)
1032{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001033 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001034 int ret;
1035
1036 printk(KERN_INFO "Linux video capture interface: v2.00\n");
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001037 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1038 if (ret < 0) {
1039 printk(KERN_WARNING "videodev: unable to get major %d\n",
1040 VIDEO_MAJOR);
1041 return ret;
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001042 }
1043
1044 ret = class_register(&video_class);
1045 if (ret < 0) {
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001046 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001047 printk(KERN_WARNING "video_dev: class_register failed\n");
1048 return -EIO;
1049 }
1050
1051 return 0;
1052}
1053
1054static void __exit videodev_exit(void)
1055{
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001056 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1057
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001058 class_unregister(&video_class);
Hans Verkuil7f8ecfa2008-08-29 17:31:35 -03001059 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001060}
1061
Bhupesh Sharmaee981c62012-03-12 05:09:02 -03001062subsys_initcall(videodev_init);
Hans Verkuil27a5e6d2008-07-20 08:43:17 -03001063module_exit(videodev_exit)
1064
1065MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
1066MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
1067MODULE_LICENSE("GPL");
Scott James Remnantcbb72b02009-03-02 15:40:57 -03001068MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);