Merge tag 'trace-v6.10-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[linux-2.6-block.git] / drivers / media / v4l2-core / v4l2-dev.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
27a5e6d3
HV
2/*
3 * Video capture interface for Linux version 2
4 *
5 * A generic video device interface for the LINUX operating system
6 * using a set of device structures/vectors for low level operations.
7 *
d9b01449 8 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
32590819 9 * Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
27a5e6d3
HV
10 *
11 * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com>
12 * - Added procfs support
13 */
14
baa057e2
MCC
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
517fd2b6 17#include <linux/debugfs.h>
27a5e6d3
HV
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/string.h>
23#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/kmod.h>
26#include <linux/slab.h>
7c0f6ba6 27#include <linux/uaccess.h>
27a5e6d3
HV
28
29#include <media/v4l2-common.h>
9bea3514 30#include <media/v4l2-device.h>
bec43661 31#include <media/v4l2-ioctl.h>
28955a61 32#include <media/v4l2-event.h>
27a5e6d3
HV
33
34#define VIDEO_NUM_DEVICES 256
35#define VIDEO_NAME "video4linux"
36
baa057e2
MCC
37#define dprintk(fmt, arg...) do { \
38 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
39 __func__, ##arg); \
40} while (0)
41
27a5e6d3
HV
42/*
43 * sysfs stuff
44 */
45
13e2237f
GKH
46static ssize_t index_show(struct device *cd,
47 struct device_attribute *attr, char *buf)
27a5e6d3 48{
dc93a70c 49 struct video_device *vdev = to_video_device(cd);
bfa8a273 50
dc93a70c 51 return sprintf(buf, "%i\n", vdev->index);
27a5e6d3 52}
13e2237f 53static DEVICE_ATTR_RO(index);
27a5e6d3 54
17028cdb 55static ssize_t dev_debug_show(struct device *cd,
13e2237f 56 struct device_attribute *attr, char *buf)
80131fe0
HV
57{
58 struct video_device *vdev = to_video_device(cd);
59
17028cdb 60 return sprintf(buf, "%i\n", vdev->dev_debug);
80131fe0
HV
61}
62
17028cdb 63static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
13e2237f 64 const char *buf, size_t len)
80131fe0
HV
65{
66 struct video_device *vdev = to_video_device(cd);
67 int res = 0;
68 u16 value;
69
70 res = kstrtou16(buf, 0, &value);
71 if (res)
72 return res;
73
17028cdb 74 vdev->dev_debug = value;
80131fe0
HV
75 return len;
76}
17028cdb 77static DEVICE_ATTR_RW(dev_debug);
80131fe0 78
13e2237f 79static ssize_t name_show(struct device *cd,
27a5e6d3
HV
80 struct device_attribute *attr, char *buf)
81{
dc93a70c 82 struct video_device *vdev = to_video_device(cd);
bfa8a273 83
dc93a70c 84 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
27a5e6d3 85}
13e2237f 86static DEVICE_ATTR_RO(name);
27a5e6d3 87
13e2237f
GKH
88static struct attribute *video_device_attrs[] = {
89 &dev_attr_name.attr,
17028cdb 90 &dev_attr_dev_debug.attr,
13e2237f
GKH
91 &dev_attr_index.attr,
92 NULL,
27a5e6d3 93};
13e2237f 94ATTRIBUTE_GROUPS(video_device);
27a5e6d3 95
7f8ecfab
HV
96/*
97 * Active devices
98 */
ee758243 99static struct video_device *video_devices[VIDEO_NUM_DEVICES];
7f8ecfab 100static DEFINE_MUTEX(videodev_lock);
22e22125 101static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
7f8ecfab 102
5062cb70
HV
103/* Device node utility functions */
104
105/* Note: these utility functions all assume that vfl_type is in the range
106 [0, VFL_TYPE_MAX-1]. */
107
108#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
109/* Return the bitmap corresponding to vfl_type. */
4839c58f 110static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
5062cb70
HV
111{
112 /* Any types not assigned to fixed minor ranges must be mapped to
113 one single bitmap for the purposes of finding a free node number
114 since all those unassigned types use the same minor range. */
226c0eea 115 int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
5062cb70
HV
116
117 return devnode_nums[idx];
118}
119#else
120/* Return the bitmap corresponding to vfl_type. */
4839c58f 121static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
5062cb70
HV
122{
123 return devnode_nums[vfl_type];
124}
125#endif
126
127/* Mark device node number vdev->num as used */
128static inline void devnode_set(struct video_device *vdev)
129{
130 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
131}
132
133/* Mark device node number vdev->num as unused */
134static inline void devnode_clear(struct video_device *vdev)
135{
136 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
137}
138
139/* Try to find a free device node number in the range [from, to> */
140static inline int devnode_find(struct video_device *vdev, int from, int to)
141{
142 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
143}
144
27a5e6d3
HV
145struct video_device *video_device_alloc(void)
146{
bfa8a273 147 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
27a5e6d3
HV
148}
149EXPORT_SYMBOL(video_device_alloc);
150
dc93a70c 151void video_device_release(struct video_device *vdev)
27a5e6d3 152{
dc93a70c 153 kfree(vdev);
27a5e6d3
HV
154}
155EXPORT_SYMBOL(video_device_release);
156
dc93a70c 157void video_device_release_empty(struct video_device *vdev)
f9e86b5e
HV
158{
159 /* Do nothing */
160 /* Only valid when the video_device struct is a static. */
161}
162EXPORT_SYMBOL(video_device_release_empty);
163
dc93a70c 164static inline void video_get(struct video_device *vdev)
7f8ecfab 165{
dc93a70c
HV
166 get_device(&vdev->dev);
167}
168
169static inline void video_put(struct video_device *vdev)
170{
171 put_device(&vdev->dev);
172}
173
174/* Called when the last user of the video device exits. */
175static void v4l2_device_release(struct device *cd)
176{
177 struct video_device *vdev = to_video_device(cd);
bedf8bcf 178 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
7f8ecfab
HV
179
180 mutex_lock(&videodev_lock);
ee758243 181 if (WARN_ON(video_devices[vdev->minor] != vdev)) {
dc93a70c 182 /* should not happen */
1fc2b5f7 183 mutex_unlock(&videodev_lock);
6ea9a182
HV
184 return;
185 }
7f8ecfab
HV
186
187 /* Free up this device for reuse */
ee758243 188 video_devices[vdev->minor] = NULL;
7f8ecfab 189
dc93a70c
HV
190 /* Delete the cdev on this minor as well */
191 cdev_del(vdev->cdev);
192 /* Just in case some driver tries to access this from
193 the release() callback. */
194 vdev->cdev = NULL;
7f8ecfab 195
22e22125 196 /* Mark device node number as free */
5062cb70 197 devnode_clear(vdev);
7f8ecfab 198
dc93a70c 199 mutex_unlock(&videodev_lock);
27a5e6d3 200
c064b8ea 201#if defined(CONFIG_MEDIA_CONTROLLER)
be2fff65 202 if (v4l2_dev->mdev && vdev->vfl_dir != VFL_DIR_M2M) {
d9c21e3e
MCC
203 /* Remove interfaces and interface links */
204 media_devnode_remove(vdev->intf_devnode);
4ca72efa 205 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
d9c21e3e
MCC
206 media_device_unregister_entity(&vdev->entity);
207 }
c064b8ea
LP
208#endif
209
8280b662
HV
210 /* Do not call v4l2_device_put if there is no release callback set.
211 * Drivers that have no v4l2_device release callback might free the
212 * v4l2_dev instance in the video_device release callback below, so we
213 * must perform this check here.
214 *
215 * TODO: In the long run all drivers that use v4l2_device should use the
216 * v4l2_device release callback. This check will then be unnecessary.
217 */
d9bfbcc0 218 if (v4l2_dev->release == NULL)
8280b662
HV
219 v4l2_dev = NULL;
220
dc93a70c
HV
221 /* Release video_device and perform other
222 cleanups as needed. */
223 vdev->release(vdev);
bedf8bcf
HV
224
225 /* Decrease v4l2_device refcount */
226 if (v4l2_dev)
227 v4l2_device_put(v4l2_dev);
27a5e6d3
HV
228}
229
230static struct class video_class = {
231 .name = VIDEO_NAME,
13e2237f 232 .dev_groups = video_device_groups,
27a5e6d3
HV
233};
234
27a5e6d3
HV
235struct video_device *video_devdata(struct file *file)
236{
ee758243 237 return video_devices[iminor(file_inode(file))];
27a5e6d3
HV
238}
239EXPORT_SYMBOL(video_devdata);
240
02265493
HV
241
242/* Priority handling */
243
244static inline bool prio_is_valid(enum v4l2_priority prio)
245{
246 return prio == V4L2_PRIORITY_BACKGROUND ||
247 prio == V4L2_PRIORITY_INTERACTIVE ||
248 prio == V4L2_PRIORITY_RECORD;
249}
250
251void v4l2_prio_init(struct v4l2_prio_state *global)
252{
253 memset(global, 0, sizeof(*global));
254}
255EXPORT_SYMBOL(v4l2_prio_init);
256
257int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
258 enum v4l2_priority new)
259{
260 if (!prio_is_valid(new))
261 return -EINVAL;
262 if (*local == new)
263 return 0;
264
265 atomic_inc(&global->prios[new]);
266 if (prio_is_valid(*local))
267 atomic_dec(&global->prios[*local]);
268 *local = new;
269 return 0;
270}
271EXPORT_SYMBOL(v4l2_prio_change);
272
273void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
274{
275 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
276}
277EXPORT_SYMBOL(v4l2_prio_open);
278
279void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
280{
281 if (prio_is_valid(local))
282 atomic_dec(&global->prios[local]);
283}
284EXPORT_SYMBOL(v4l2_prio_close);
285
286enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
287{
288 if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
289 return V4L2_PRIORITY_RECORD;
290 if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
291 return V4L2_PRIORITY_INTERACTIVE;
292 if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
293 return V4L2_PRIORITY_BACKGROUND;
294 return V4L2_PRIORITY_UNSET;
295}
296EXPORT_SYMBOL(v4l2_prio_max);
297
298int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
299{
300 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
301}
302EXPORT_SYMBOL(v4l2_prio_check);
303
304
dc93a70c
HV
305static ssize_t v4l2_read(struct file *filp, char __user *buf,
306 size_t sz, loff_t *off)
307{
308 struct video_device *vdev = video_devdata(filp);
2877842d 309 int ret = -ENODEV;
dc93a70c
HV
310
311 if (!vdev->fops->read)
312 return -EINVAL;
ee6869af
HV
313 if (video_is_registered(vdev))
314 ret = vdev->fops->read(filp, buf, sz, off);
17028cdb
HV
315 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
316 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
baa057e2 317 dprintk("%s: read: %zd (%d)\n",
cc4b7e7f 318 video_device_node_name(vdev), sz, ret);
ee6869af 319 return ret;
dc93a70c
HV
320}
321
322static ssize_t v4l2_write(struct file *filp, const char __user *buf,
323 size_t sz, loff_t *off)
324{
325 struct video_device *vdev = video_devdata(filp);
2877842d 326 int ret = -ENODEV;
dc93a70c
HV
327
328 if (!vdev->fops->write)
329 return -EINVAL;
ee6869af
HV
330 if (video_is_registered(vdev))
331 ret = vdev->fops->write(filp, buf, sz, off);
17028cdb
HV
332 if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
333 (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
baa057e2 334 dprintk("%s: write: %zd (%d)\n",
cc4b7e7f 335 video_device_node_name(vdev), sz, ret);
ee6869af 336 return ret;
dc93a70c
HV
337}
338
c23e0cb8 339static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
dc93a70c
HV
340{
341 struct video_device *vdev = video_devdata(filp);
5cb0a64e 342 __poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;
ee6869af 343
5cb0a64e
HV
344 if (video_is_registered(vdev)) {
345 if (!vdev->fops->poll)
346 res = DEFAULT_POLLMASK;
347 else
348 res = vdev->fops->poll(filp, poll);
349 }
17028cdb 350 if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
bea7515d
HV
351 dprintk("%s: poll: %08x %08x\n",
352 video_device_node_name(vdev), res,
353 poll_requested_events(poll));
cf533735 354 return res;
dc93a70c
HV
355}
356
86a5ef7d 357static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
dc93a70c
HV
358{
359 struct video_device *vdev = video_devdata(filp);
72420630 360 int ret = -ENODEV;
dc93a70c 361
86a5ef7d 362 if (vdev->fops->unlocked_ioctl) {
72420630
MCC
363 if (video_is_registered(vdev))
364 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
86a5ef7d
AB
365 } else
366 ret = -ENOTTY;
dc93a70c 367
86a5ef7d 368 return ret;
dc93a70c
HV
369}
370
ecc6517d
BL
371#ifdef CONFIG_MMU
372#define v4l2_get_unmapped_area NULL
373#else
374static unsigned long v4l2_get_unmapped_area(struct file *filp,
375 unsigned long addr, unsigned long len, unsigned long pgoff,
376 unsigned long flags)
377{
378 struct video_device *vdev = video_devdata(filp);
cc4b7e7f 379 int ret;
ecc6517d
BL
380
381 if (!vdev->fops->get_unmapped_area)
382 return -ENOSYS;
383 if (!video_is_registered(vdev))
384 return -ENODEV;
cc4b7e7f 385 ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
17028cdb 386 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
baa057e2 387 dprintk("%s: get_unmapped_area (%d)\n",
cc4b7e7f
HV
388 video_device_node_name(vdev), ret);
389 return ret;
ecc6517d
BL
390}
391#endif
392
dc93a70c
HV
393static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
394{
395 struct video_device *vdev = video_devdata(filp);
ee6869af
HV
396 int ret = -ENODEV;
397
398 if (!vdev->fops->mmap)
cf533735 399 return -ENODEV;
ee6869af
HV
400 if (video_is_registered(vdev))
401 ret = vdev->fops->mmap(filp, vm);
17028cdb 402 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
baa057e2 403 dprintk("%s: mmap (%d)\n",
cc4b7e7f 404 video_device_node_name(vdev), ret);
ee6869af 405 return ret;
dc93a70c
HV
406}
407
408/* Override for the open function */
409static int v4l2_open(struct inode *inode, struct file *filp)
410{
411 struct video_device *vdev;
65d9ff9c 412 int ret = 0;
dc93a70c
HV
413
414 /* Check if the video device is available */
415 mutex_lock(&videodev_lock);
416 vdev = video_devdata(filp);
ee6869af 417 /* return ENODEV if the video device has already been removed. */
ca9afe6f 418 if (vdev == NULL || !video_is_registered(vdev)) {
dc93a70c
HV
419 mutex_unlock(&videodev_lock);
420 return -ENODEV;
421 }
422 /* and increase the device refcount */
423 video_get(vdev);
424 mutex_unlock(&videodev_lock);
ee6869af 425 if (vdev->fops->open) {
ee6869af
HV
426 if (video_is_registered(vdev))
427 ret = vdev->fops->open(filp);
428 else
429 ret = -ENODEV;
ee6869af 430 }
65d9ff9c 431
17028cdb 432 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
baa057e2 433 dprintk("%s: open (%d)\n",
cc4b7e7f 434 video_device_node_name(vdev), ret);
8f695d3f
EG
435 /* decrease the refcount in case of an error */
436 if (ret)
437 video_put(vdev);
dc93a70c
HV
438 return ret;
439}
440
441/* Override for the release function */
442static int v4l2_release(struct inode *inode, struct file *filp)
443{
444 struct video_device *vdev = video_devdata(filp);
65d9ff9c
HV
445 int ret = 0;
446
cc6eddcd
HV
447 /*
448 * We need to serialize the release() with queueing new requests.
449 * The release() may trigger the cancellation of a streaming
450 * operation, and that should not be mixed with queueing a new
451 * request at the same time.
452 */
453 if (vdev->fops->release) {
454 if (v4l2_device_supports_requests(vdev->v4l2_dev)) {
455 mutex_lock(&vdev->v4l2_dev->mdev->req_queue_mutex);
456 ret = vdev->fops->release(filp);
457 mutex_unlock(&vdev->v4l2_dev->mdev->req_queue_mutex);
458 } else {
459 ret = vdev->fops->release(filp);
460 }
461 }
462
17028cdb 463 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
baa057e2 464 dprintk("%s: release\n",
cc4b7e7f 465 video_device_node_name(vdev));
cf533735 466
8f695d3f
EG
467 /* decrease the refcount unconditionally since the release()
468 return value is ignored. */
469 video_put(vdev);
dc93a70c
HV
470 return ret;
471}
472
dc93a70c
HV
473static const struct file_operations v4l2_fops = {
474 .owner = THIS_MODULE,
475 .read = v4l2_read,
476 .write = v4l2_write,
477 .open = v4l2_open,
ecc6517d 478 .get_unmapped_area = v4l2_get_unmapped_area,
dc93a70c 479 .mmap = v4l2_mmap,
86a5ef7d 480 .unlocked_ioctl = v4l2_ioctl,
dc93a70c 481#ifdef CONFIG_COMPAT
9bb7cde7 482 .compat_ioctl = v4l2_compat_ioctl32,
dc93a70c
HV
483#endif
484 .release = v4l2_release,
485 .poll = v4l2_poll,
486 .llseek = no_llseek,
487};
488
27a5e6d3 489/**
1c1d86a1
HV
490 * get_index - assign stream index number based on v4l2_dev
491 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
27a5e6d3 492 *
dc93a70c 493 * Note that when this is called the new device has not yet been registered
7ae0cd9b 494 * in the video_device array, but it was able to obtain a minor number.
27a5e6d3 495 *
7ae0cd9b
HV
496 * This means that we can always obtain a free stream index number since
497 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
498 * use of the video_device array.
499 *
500 * Returns a free index number.
27a5e6d3 501 */
7ae0cd9b 502static int get_index(struct video_device *vdev)
27a5e6d3 503{
775a05dd
HV
504 /* This can be static since this function is called with the global
505 videodev_lock held. */
506 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
27a5e6d3
HV
507 int i;
508
775a05dd 509 bitmap_zero(used, VIDEO_NUM_DEVICES);
806e5b7c 510
27a5e6d3 511 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
ee758243
SQ
512 if (video_devices[i] != NULL &&
513 video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
0533d173 514 __set_bit(video_devices[i]->index, used);
27a5e6d3
HV
515 }
516 }
517
7ae0cd9b 518 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
27a5e6d3
HV
519}
520
35037eab 521#define SET_VALID_IOCTL(ops, cmd, op) \
0533d173 522 do { if ((ops)->op) __set_bit(_IOC_NR(cmd), valid_ioctls); } while (0)
48ea0be0
HV
523
524/* This determines which ioctls are actually implemented in the driver.
525 It's a one-time thing which simplifies video_ioctl2 as it can just do
526 a bit test.
527
528 Note that drivers can override this by setting bits to 1 in
529 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
530 called, then that ioctl will actually be marked as unimplemented.
531
532 It does that by first setting up the local valid_ioctls bitmap, and
533 at the end do a:
534
535 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
536 */
537static void determine_valid_ioctls(struct video_device *vdev)
538{
96f49c1a
VB
539 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
540 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
541 V4L2_CAP_VIDEO_OUTPUT |
542 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
543 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
544 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
545 V4L2_CAP_META_OUTPUT;
48ea0be0
HV
546 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
547 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
238e4a5b 548 bool is_vid = vdev->vfl_type == VFL_TYPE_VIDEO &&
96f49c1a 549 (vdev->device_caps & vid_caps);
4b20259f 550 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
bfffd743 551 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
582c52cb 552 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
b2fe22d0 553 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
238e4a5b 554 bool is_meta = vdev->vfl_type == VFL_TYPE_VIDEO &&
96f49c1a 555 (vdev->device_caps & meta_caps);
4b20259f
HV
556 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
557 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
f645e625 558 bool is_io_mc = vdev->device_caps & V4L2_CAP_IO_MC;
5f225889 559 bool has_streaming = vdev->device_caps & V4L2_CAP_STREAMING;
48ea0be0
HV
560
561 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
562
90d0fc49
HV
563 /* vfl_type and vfl_dir independent ioctls */
564
48ea0be0 565 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
0533d173
CJ
566 __set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
567 __set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
2438e78a 568
90d0fc49
HV
569 /* Note: the control handler can also be passed through the filehandle,
570 and that can't be tested here. If the bit for these control ioctls
571 is set, then the ioctl is valid. But if it is 0, then it can still
572 be valid if the filehandle passed the control handler. */
573 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
0533d173 574 __set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
e6bee368 575 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
0533d173 576 __set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
90d0fc49 577 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
0533d173 578 __set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
90d0fc49 579 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
0533d173 580 __set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
90d0fc49 581 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
0533d173 582 __set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
90d0fc49 583 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
0533d173 584 __set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
90d0fc49 585 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
0533d173 586 __set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
90d0fc49 587 if (vdev->ctrl_handler || ops->vidioc_querymenu)
0533d173 588 __set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
8669d847
HV
589 if (!is_tch) {
590 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
591 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
592 }
90d0fc49
HV
593 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
594#ifdef CONFIG_VIDEO_ADV_DEBUG
0533d173
CJ
595 __set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
596 __set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
597 __set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
90d0fc49 598#endif
90d0fc49
HV
599 /* yes, really vidioc_subscribe_event */
600 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
601 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
602 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
90d0fc49 603 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
0533d173 604 __set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
90d0fc49 605
4fbd54bb
HV
606 if (is_vid) {
607 /* video specific ioctls */
4b20259f 608 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
96f49c1a
VB
609 ops->vidioc_enum_fmt_vid_overlay)) ||
610 (is_tx && ops->vidioc_enum_fmt_vid_out))
0533d173 611 __set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
4b20259f
HV
612 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
613 ops->vidioc_g_fmt_vid_cap_mplane ||
96f49c1a 614 ops->vidioc_g_fmt_vid_overlay)) ||
4b20259f
HV
615 (is_tx && (ops->vidioc_g_fmt_vid_out ||
616 ops->vidioc_g_fmt_vid_out_mplane ||
96f49c1a 617 ops->vidioc_g_fmt_vid_out_overlay)))
0533d173 618 __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
4b20259f
HV
619 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
620 ops->vidioc_s_fmt_vid_cap_mplane ||
96f49c1a 621 ops->vidioc_s_fmt_vid_overlay)) ||
4b20259f
HV
622 (is_tx && (ops->vidioc_s_fmt_vid_out ||
623 ops->vidioc_s_fmt_vid_out_mplane ||
96f49c1a 624 ops->vidioc_s_fmt_vid_out_overlay)))
0533d173 625 __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
4b20259f
HV
626 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
627 ops->vidioc_try_fmt_vid_cap_mplane ||
96f49c1a 628 ops->vidioc_try_fmt_vid_overlay)) ||
4b20259f
HV
629 (is_tx && (ops->vidioc_try_fmt_vid_out ||
630 ops->vidioc_try_fmt_vid_out_mplane ||
96f49c1a 631 ops->vidioc_try_fmt_vid_out_overlay)))
0533d173 632 __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
90d0fc49
HV
633 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
634 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
635 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
636 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
637 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
638 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
639 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
640 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
641 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
642 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
643 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
644 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
dd4229fa
PK
645 if (ops->vidioc_g_selection &&
646 !test_bit(_IOC_NR(VIDIOC_G_SELECTION), vdev->valid_ioctls)) {
0533d173
CJ
647 __set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
648 __set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
5200ab6a 649 }
dd4229fa
PK
650 if (ops->vidioc_s_selection &&
651 !test_bit(_IOC_NR(VIDIOC_S_SELECTION), vdev->valid_ioctls))
0533d173 652 __set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
2c9fc463
HV
653 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
654 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
96f49c1a
VB
655 }
656 if (is_meta && is_rx) {
657 /* metadata capture specific ioctls */
658 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_cap);
659 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_cap);
660 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_cap);
661 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_cap);
662 } else if (is_meta && is_tx) {
663 /* metadata output specific ioctls */
664 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_out);
665 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_out);
666 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_out);
667 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_out);
668 }
669 if (is_vbi) {
90d0fc49 670 /* vbi specific ioctls */
4b20259f
HV
671 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
672 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
673 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
674 ops->vidioc_g_fmt_sliced_vbi_out)))
0533d173 675 __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
4b20259f
HV
676 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
677 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
678 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
679 ops->vidioc_s_fmt_sliced_vbi_out)))
0533d173 680 __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
4b20259f
HV
681 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
682 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
683 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
684 ops->vidioc_try_fmt_sliced_vbi_out)))
0533d173 685 __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
90d0fc49 686 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
4fbd54bb
HV
687 } else if (is_tch) {
688 /* touch specific ioctls */
689 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_vid_cap);
690 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_vid_cap);
691 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_vid_cap);
692 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_vid_cap);
693 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
694 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
695 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
696 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
697 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
698 SET_VALID_IOCTL(ops, VIDIOC_G_PARM, vidioc_g_parm);
699 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
9effc72f
AP
700 } else if (is_sdr && is_rx) {
701 /* SDR receiver specific ioctls */
8e72244b
HV
702 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_cap);
703 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_cap);
704 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_cap);
705 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_cap);
9effc72f
AP
706 } else if (is_sdr && is_tx) {
707 /* SDR transmitter specific ioctls */
8e72244b
HV
708 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_out);
709 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_out);
710 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_out);
711 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_out);
4b20259f 712 }
582c52cb 713
5f225889
HV
714 if (has_streaming) {
715 /* ioctls valid for streaming I/O */
5815d0c4
FS
716 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
717 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
718 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
719 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
720 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
721 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
722 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
72be89c8
HV
723 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
724 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
a3293a85
BG
725 /* VIDIOC_CREATE_BUFS support is mandatory to enable VIDIOC_REMOVE_BUFS */
726 if (ops->vidioc_create_bufs)
727 SET_VALID_IOCTL(ops, VIDIOC_REMOVE_BUFS, vidioc_remove_bufs);
582c52cb
AP
728 }
729
4fbd54bb
HV
730 if (is_vid || is_vbi || is_meta) {
731 /* ioctls valid for video, vbi and metadata */
4b20259f 732 if (ops->vidioc_s_std)
0533d173 733 __set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
4b20259f 734 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
ca371575 735 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
4b20259f 736 if (is_rx) {
90d0fc49 737 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
f645e625 738 if (is_io_mc) {
0533d173
CJ
739 __set_bit(_IOC_NR(VIDIOC_ENUMINPUT), valid_ioctls);
740 __set_bit(_IOC_NR(VIDIOC_G_INPUT), valid_ioctls);
741 __set_bit(_IOC_NR(VIDIOC_S_INPUT), valid_ioctls);
f645e625
NS
742 } else {
743 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
744 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
745 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
746 }
4b20259f
HV
747 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
748 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
749 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
90d0fc49 750 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
dd519bb3 751 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
4b20259f
HV
752 }
753 if (is_tx) {
f645e625 754 if (is_io_mc) {
0533d173
CJ
755 __set_bit(_IOC_NR(VIDIOC_ENUMOUTPUT), valid_ioctls);
756 __set_bit(_IOC_NR(VIDIOC_G_OUTPUT), valid_ioctls);
757 __set_bit(_IOC_NR(VIDIOC_S_OUTPUT), valid_ioctls);
f645e625
NS
758 } else {
759 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
760 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
761 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
762 }
4b20259f
HV
763 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
764 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
765 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
766 }
4fbd54bb 767 if (ops->vidioc_g_parm || ops->vidioc_g_std)
0533d173 768 __set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
4b20259f 769 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
4b20259f
HV
770 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
771 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
772 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
4b20259f 773 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
dd519bb3 774 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
4b20259f 775 }
bfffd743
HV
776 if (is_tx && (is_radio || is_sdr)) {
777 /* radio transmitter only ioctls */
90d0fc49
HV
778 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
779 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
780 }
8669d847 781 if (is_rx && !is_tch) {
90d0fc49
HV
782 /* receiver only ioctls */
783 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
784 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
785 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
786 }
787
48ea0be0
HV
788 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
789 BASE_VIDIOC_PRIVATE);
790}
791
be2fff65 792static int video_register_media_controller(struct video_device *vdev)
d9c21e3e
MCC
793{
794#if defined(CONFIG_MEDIA_CONTROLLER)
795 u32 intf_type;
796 int ret;
797
be2fff65
EG
798 /* Memory-to-memory devices are more complex and use
799 * their own function to register its mc entities.
800 */
801 if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
d9c21e3e
MCC
802 return 0;
803
b76a2a8c 804 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
4ca72efa 805 vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
d9c21e3e 806
be2fff65 807 switch (vdev->vfl_type) {
238e4a5b 808 case VFL_TYPE_VIDEO:
d9c21e3e 809 intf_type = MEDIA_INTF_T_V4L_VIDEO;
4ca72efa 810 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
d9c21e3e
MCC
811 break;
812 case VFL_TYPE_VBI:
813 intf_type = MEDIA_INTF_T_V4L_VBI;
4ca72efa 814 vdev->entity.function = MEDIA_ENT_F_IO_VBI;
d9c21e3e
MCC
815 break;
816 case VFL_TYPE_SDR:
817 intf_type = MEDIA_INTF_T_V4L_SWRADIO;
4ca72efa 818 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
d9c21e3e 819 break;
b2fe22d0
ND
820 case VFL_TYPE_TOUCH:
821 intf_type = MEDIA_INTF_T_V4L_TOUCH;
822 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
823 break;
d9c21e3e
MCC
824 case VFL_TYPE_RADIO:
825 intf_type = MEDIA_INTF_T_V4L_RADIO;
826 /*
827 * Radio doesn't have an entity at the V4L2 side to represent
828 * radio input or output. Instead, the audio input/output goes
829 * via either physical wires or ALSA.
830 */
831 break;
832 case VFL_TYPE_SUBDEV:
833 intf_type = MEDIA_INTF_T_V4L_SUBDEV;
834 /* Entity will be created via v4l2_device_register_subdev() */
835 break;
836 default:
837 return 0;
838 }
839
4ca72efa 840 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
d9c21e3e
MCC
841 vdev->entity.name = vdev->name;
842
843 /* Needed just for backward compatibility with legacy MC API */
844 vdev->entity.info.dev.major = VIDEO_MAJOR;
845 vdev->entity.info.dev.minor = vdev->minor;
846
847 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
848 &vdev->entity);
849 if (ret < 0) {
baa057e2 850 pr_warn("%s: media_device_register_entity failed\n",
d9c21e3e
MCC
851 __func__);
852 return ret;
853 }
854 }
855
856 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
857 intf_type,
858 0, VIDEO_MAJOR,
0b3b72df 859 vdev->minor);
d9c21e3e
MCC
860 if (!vdev->intf_devnode) {
861 media_device_unregister_entity(&vdev->entity);
862 return -ENOMEM;
863 }
864
4ca72efa 865 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
d9c21e3e
MCC
866 struct media_link *link;
867
868 link = media_create_intf_link(&vdev->entity,
13f6e888 869 &vdev->intf_devnode->intf,
4d1e4545
HV
870 MEDIA_LNK_FL_ENABLED |
871 MEDIA_LNK_FL_IMMUTABLE);
d9c21e3e
MCC
872 if (!link) {
873 media_devnode_remove(vdev->intf_devnode);
874 media_device_unregister_entity(&vdev->entity);
875 return -ENOMEM;
876 }
877 }
878
879 /* FIXME: how to create the other interface links? */
880
881#endif
882 return 0;
883}
884
4839c58f
MCC
885int __video_register_device(struct video_device *vdev,
886 enum vfl_devnode_type type,
887 int nr, int warn_if_nr_in_use,
888 struct module *owner)
27a5e6d3
HV
889{
890 int i = 0;
27a5e6d3 891 int ret;
dd89601d
HV
892 int minor_offset = 0;
893 int minor_cnt = VIDEO_NUM_DEVICES;
894 const char *name_base;
27a5e6d3 895
dc93a70c
HV
896 /* A minor value of -1 marks this video device as never
897 having been registered */
428c8d19 898 vdev->minor = -1;
ee7aa9f8 899
dc93a70c 900 /* the release callback MUST be present */
1fc2b5f7 901 if (WARN_ON(!vdev->release))
f3b9f50e 902 return -EINVAL;
1c1d86a1
HV
903 /* the v4l2_dev pointer MUST be present */
904 if (WARN_ON(!vdev->v4l2_dev))
905 return -EINVAL;
049e684f
HV
906 /* the device_caps field MUST be set for all but subdevs */
907 if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
3c135050 908 return -EINVAL;
f3b9f50e 909
1babcb46
SA
910 /* v4l2_fh support */
911 spin_lock_init(&vdev->fh_lock);
912 INIT_LIST_HEAD(&vdev->fh_list);
913
dc93a70c 914 /* Part 1: check device type */
27a5e6d3 915 switch (type) {
238e4a5b 916 case VFL_TYPE_VIDEO:
27a5e6d3
HV
917 name_base = "video";
918 break;
27a5e6d3 919 case VFL_TYPE_VBI:
27a5e6d3
HV
920 name_base = "vbi";
921 break;
922 case VFL_TYPE_RADIO:
27a5e6d3
HV
923 name_base = "radio";
924 break;
2096a5dc
LP
925 case VFL_TYPE_SUBDEV:
926 name_base = "v4l-subdev";
927 break;
d42626bd
AP
928 case VFL_TYPE_SDR:
929 /* Use device name 'swradio' because 'sdr' was already taken. */
930 name_base = "swradio";
931 break;
b2fe22d0
ND
932 case VFL_TYPE_TOUCH:
933 name_base = "v4l-touch";
934 break;
27a5e6d3 935 default:
baa057e2 936 pr_err("%s called with unknown type: %d\n",
27a5e6d3 937 __func__, type);
46f2c21c 938 return -EINVAL;
27a5e6d3
HV
939 }
940
dc93a70c
HV
941 vdev->vfl_type = type;
942 vdev->cdev = NULL;
1c1d86a1
HV
943 if (vdev->dev_parent == NULL)
944 vdev->dev_parent = vdev->v4l2_dev->dev;
945 if (vdev->ctrl_handler == NULL)
946 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
947 /* If the prio state pointer is NULL, then use the v4l2_device
948 prio state. */
949 if (vdev->prio == NULL)
950 vdev->prio = &vdev->v4l2_dev->prio;
dd89601d 951
22e22125 952 /* Part 2: find a free minor, device node number and device index. */
dd89601d
HV
953#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
954 /* Keep the ranges for the first four types for historical
955 * reasons.
956 * Newer devices (not yet in place) should use the range
957 * of 128-191 and just pick the first free minor there
958 * (new style). */
959 switch (type) {
238e4a5b 960 case VFL_TYPE_VIDEO:
dd89601d
HV
961 minor_offset = 0;
962 minor_cnt = 64;
963 break;
964 case VFL_TYPE_RADIO:
965 minor_offset = 64;
966 minor_cnt = 64;
967 break;
dd89601d
HV
968 case VFL_TYPE_VBI:
969 minor_offset = 224;
970 minor_cnt = 32;
971 break;
972 default:
973 minor_offset = 128;
974 minor_cnt = 64;
975 break;
976 }
977#endif
978
22e22125 979 /* Pick a device node number */
27a5e6d3 980 mutex_lock(&videodev_lock);
5062cb70 981 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
dd89601d 982 if (nr == minor_cnt)
5062cb70 983 nr = devnode_find(vdev, 0, minor_cnt);
dd89601d 984 if (nr == minor_cnt) {
baa057e2 985 pr_err("could not get a free device node number\n");
dd89601d
HV
986 mutex_unlock(&videodev_lock);
987 return -ENFILE;
27a5e6d3 988 }
dd89601d 989#ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
22e22125 990 /* 1-on-1 mapping of device node number to minor number */
dd89601d
HV
991 i = nr;
992#else
22e22125
HV
993 /* The device node number and minor numbers are independent, so
994 we just find the first free minor number. */
dd89601d 995 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
ee758243 996 if (video_devices[i] == NULL)
dd89601d
HV
997 break;
998 if (i == VIDEO_NUM_DEVICES) {
999 mutex_unlock(&videodev_lock);
baa057e2 1000 pr_err("could not get a free minor\n");
dd89601d
HV
1001 return -ENFILE;
1002 }
1003#endif
dc93a70c
HV
1004 vdev->minor = i + minor_offset;
1005 vdev->num = nr;
5062cb70 1006
dc93a70c 1007 /* Should not happen since we thought this minor was free */
ee758243 1008 if (WARN_ON(video_devices[vdev->minor])) {
a95845ba 1009 mutex_unlock(&videodev_lock);
baa057e2 1010 pr_err("video_device not empty!\n");
a95845ba
MCC
1011 return -ENFILE;
1012 }
1013 devnode_set(vdev);
7ae0cd9b 1014 vdev->index = get_index(vdev);
ee758243 1015 video_devices[vdev->minor] = vdev;
27a5e6d3
HV
1016 mutex_unlock(&videodev_lock);
1017
48ea0be0
HV
1018 if (vdev->ioctl_ops)
1019 determine_valid_ioctls(vdev);
1020
dc93a70c
HV
1021 /* Part 3: Initialize the character device */
1022 vdev->cdev = cdev_alloc();
1023 if (vdev->cdev == NULL) {
1024 ret = -ENOMEM;
1025 goto cleanup;
1026 }
86a5ef7d 1027 vdev->cdev->ops = &v4l2_fops;
2096a5dc 1028 vdev->cdev->owner = owner;
dc93a70c 1029 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
7f8ecfab 1030 if (ret < 0) {
baa057e2 1031 pr_err("%s: cdev_add failed\n", __func__);
dc93a70c
HV
1032 kfree(vdev->cdev);
1033 vdev->cdev = NULL;
1034 goto cleanup;
7f8ecfab 1035 }
dc93a70c
HV
1036
1037 /* Part 4: register the device with sysfs */
dc93a70c
HV
1038 vdev->dev.class = &video_class;
1039 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
1c1d86a1 1040 vdev->dev.parent = vdev->dev_parent;
22e22125 1041 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
1ed4477f 1042 mutex_lock(&videodev_lock);
dc93a70c 1043 ret = device_register(&vdev->dev);
27a5e6d3 1044 if (ret < 0) {
1ed4477f 1045 mutex_unlock(&videodev_lock);
baa057e2 1046 pr_err("%s: device_register failed\n", __func__);
dc93a70c 1047 goto cleanup;
27a5e6d3 1048 }
dc93a70c
HV
1049 /* Register the release callback that will be called when the last
1050 reference to the device goes away. */
1051 vdev->dev.release = v4l2_device_release;
27a5e6d3 1052
6b5270d2 1053 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
baa057e2 1054 pr_warn("%s: requested %s%d, got %s\n", __func__,
eac8ea53 1055 name_base, nr, video_device_node_name(vdev));
bedf8bcf
HV
1056
1057 /* Increase v4l2_device refcount */
d9bfbcc0 1058 v4l2_device_get(vdev->v4l2_dev);
bedf8bcf 1059
2c0ab67b 1060 /* Part 5: Register the entity. */
be2fff65 1061 ret = video_register_media_controller(vdev);
d9c21e3e 1062
2c0ab67b 1063 /* Part 6: Activate this minor. The char device can now be used. */
957b4aa9 1064 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1ed4477f 1065 mutex_unlock(&videodev_lock);
2c0ab67b 1066
dc93a70c 1067 return 0;
7f8ecfab 1068
dc93a70c 1069cleanup:
27a5e6d3 1070 mutex_lock(&videodev_lock);
dc93a70c
HV
1071 if (vdev->cdev)
1072 cdev_del(vdev->cdev);
ee758243 1073 video_devices[vdev->minor] = NULL;
5062cb70 1074 devnode_clear(vdev);
27a5e6d3 1075 mutex_unlock(&videodev_lock);
dc93a70c
HV
1076 /* Mark this video device as never having been registered. */
1077 vdev->minor = -1;
27a5e6d3
HV
1078 return ret;
1079}
2096a5dc 1080EXPORT_SYMBOL(__video_register_device);
6b5270d2 1081
27a5e6d3
HV
1082/**
1083 * video_unregister_device - unregister a video4linux device
dc93a70c 1084 * @vdev: the device to unregister
27a5e6d3 1085 *
dc93a70c
HV
1086 * This unregisters the passed device. Future open calls will
1087 * be met with errors.
27a5e6d3 1088 */
dc93a70c 1089void video_unregister_device(struct video_device *vdev)
27a5e6d3 1090{
dc93a70c 1091 /* Check if vdev was ever registered at all */
957b4aa9 1092 if (!vdev || !video_is_registered(vdev))
dc93a70c
HV
1093 return;
1094
ca9afe6f
HV
1095 mutex_lock(&videodev_lock);
1096 /* This must be in a critical section to prevent a race with v4l2_open.
1097 * Once this bit has been cleared video_get may never be called again.
1098 */
957b4aa9 1099 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
ca9afe6f 1100 mutex_unlock(&videodev_lock);
28955a61
HV
1101 if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
1102 v4l2_event_wake_all(vdev);
dc93a70c 1103 device_unregister(&vdev->dev);
27a5e6d3
HV
1104}
1105EXPORT_SYMBOL(video_unregister_device);
1106
340eba47
TV
1107#if defined(CONFIG_MEDIA_CONTROLLER)
1108
1109__must_check int video_device_pipeline_start(struct video_device *vdev,
1110 struct media_pipeline *pipe)
1111{
1112 struct media_entity *entity = &vdev->entity;
1113
1114 if (entity->num_pads != 1)
1115 return -ENODEV;
1116
9e3576a1 1117 return media_pipeline_start(&entity->pads[0], pipe);
340eba47
TV
1118}
1119EXPORT_SYMBOL_GPL(video_device_pipeline_start);
1120
1121__must_check int __video_device_pipeline_start(struct video_device *vdev,
1122 struct media_pipeline *pipe)
1123{
1124 struct media_entity *entity = &vdev->entity;
1125
1126 if (entity->num_pads != 1)
1127 return -ENODEV;
1128
9e3576a1 1129 return __media_pipeline_start(&entity->pads[0], pipe);
340eba47
TV
1130}
1131EXPORT_SYMBOL_GPL(__video_device_pipeline_start);
1132
1133void video_device_pipeline_stop(struct video_device *vdev)
1134{
1135 struct media_entity *entity = &vdev->entity;
1136
1137 if (WARN_ON(entity->num_pads != 1))
1138 return;
1139
9e3576a1 1140 return media_pipeline_stop(&entity->pads[0]);
340eba47
TV
1141}
1142EXPORT_SYMBOL_GPL(video_device_pipeline_stop);
1143
1144void __video_device_pipeline_stop(struct video_device *vdev)
1145{
1146 struct media_entity *entity = &vdev->entity;
1147
1148 if (WARN_ON(entity->num_pads != 1))
1149 return;
1150
9e3576a1 1151 return __media_pipeline_stop(&entity->pads[0]);
340eba47
TV
1152}
1153EXPORT_SYMBOL_GPL(__video_device_pipeline_stop);
1154
d9f44345
TV
1155__must_check int video_device_pipeline_alloc_start(struct video_device *vdev)
1156{
1157 struct media_entity *entity = &vdev->entity;
1158
1159 if (entity->num_pads != 1)
1160 return -ENODEV;
1161
9e3576a1 1162 return media_pipeline_alloc_start(&entity->pads[0]);
d9f44345
TV
1163}
1164EXPORT_SYMBOL_GPL(video_device_pipeline_alloc_start);
1165
340eba47
TV
1166struct media_pipeline *video_device_pipeline(struct video_device *vdev)
1167{
1168 struct media_entity *entity = &vdev->entity;
1169
1170 if (WARN_ON(entity->num_pads != 1))
1171 return NULL;
1172
9e3576a1 1173 return media_pad_pipeline(&entity->pads[0]);
340eba47
TV
1174}
1175EXPORT_SYMBOL_GPL(video_device_pipeline);
1176
1177#endif /* CONFIG_MEDIA_CONTROLLER */
1178
27a5e6d3
HV
1179/*
1180 * Initialise video for linux
1181 */
27a5e6d3
HV
1182static int __init videodev_init(void)
1183{
7f8ecfab 1184 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
27a5e6d3
HV
1185 int ret;
1186
baa057e2 1187 pr_info("Linux video capture interface: v2.00\n");
7f8ecfab
HV
1188 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1189 if (ret < 0) {
baa057e2 1190 pr_warn("videodev: unable to get major %d\n",
7f8ecfab
HV
1191 VIDEO_MAJOR);
1192 return ret;
27a5e6d3
HV
1193 }
1194
1195 ret = class_register(&video_class);
1196 if (ret < 0) {
7f8ecfab 1197 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
baa057e2 1198 pr_warn("video_dev: class_register failed\n");
27a5e6d3
HV
1199 return -EIO;
1200 }
1201
1202 return 0;
1203}
1204
1205static void __exit videodev_exit(void)
1206{
7f8ecfab
HV
1207 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1208
27a5e6d3 1209 class_unregister(&video_class);
7f8ecfab 1210 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
27a5e6d3
HV
1211}
1212
ee981c6f 1213subsys_initcall(videodev_init);
27a5e6d3
HV
1214module_exit(videodev_exit)
1215
ff35213f
EG
1216MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@kernel.org>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1217MODULE_DESCRIPTION("Video4Linux2 core driver");
27a5e6d3 1218MODULE_LICENSE("GPL");
cbb72b0f 1219MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);