1 // SPDX-License-Identifier: GPL-2.0-or-later
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  *
8  * Authors:	Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
9  *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
10  *
11  * Fixes:	20000516  Claudio Matsuoka <claudio@conectiva.com>
12  *		- Added procfs support
13  */
14 
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 
17 #include <linux/debugfs.h>
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>
27 #include <linux/uaccess.h>
28 
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-event.h>
33 
34 #define VIDEO_NUM_DEVICES	256
35 #define VIDEO_NAME              "video4linux"
36 
37 #define dprintk(fmt, arg...) do {					\
38 		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
39 		       __func__, ##arg);				\
40 } while (0)
41 
42 /*
43  *	sysfs stuff
44  */
45 
index_show(struct device * cd,struct device_attribute * attr,char * buf)46 static ssize_t index_show(struct device *cd,
47 			  struct device_attribute *attr, char *buf)
48 {
49 	struct video_device *vdev = to_video_device(cd);
50 
51 	return sprintf(buf, "%i\n", vdev->index);
52 }
53 static DEVICE_ATTR_RO(index);
54 
dev_debug_show(struct device * cd,struct device_attribute * attr,char * buf)55 static ssize_t dev_debug_show(struct device *cd,
56 			  struct device_attribute *attr, char *buf)
57 {
58 	struct video_device *vdev = to_video_device(cd);
59 
60 	return sprintf(buf, "%i\n", vdev->dev_debug);
61 }
62 
dev_debug_store(struct device * cd,struct device_attribute * attr,const char * buf,size_t len)63 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
64 			  const char *buf, size_t len)
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 
74 	vdev->dev_debug = value;
75 	return len;
76 }
77 static DEVICE_ATTR_RW(dev_debug);
78 
name_show(struct device * cd,struct device_attribute * attr,char * buf)79 static ssize_t name_show(struct device *cd,
80 			 struct device_attribute *attr, char *buf)
81 {
82 	struct video_device *vdev = to_video_device(cd);
83 
84 	return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
85 }
86 static DEVICE_ATTR_RO(name);
87 
88 static struct attribute *video_device_attrs[] = {
89 	&dev_attr_name.attr,
90 	&dev_attr_dev_debug.attr,
91 	&dev_attr_index.attr,
92 	NULL,
93 };
94 ATTRIBUTE_GROUPS(video_device);
95 
96 /*
97  *	Active devices
98  */
99 static struct video_device *video_devices[VIDEO_NUM_DEVICES];
100 static DEFINE_MUTEX(videodev_lock);
101 static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
102 
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. */
devnode_bits(enum vfl_devnode_type vfl_type)110 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
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. */
115 	int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
116 
117 	return devnode_nums[idx];
118 }
119 #else
120 /* Return the bitmap corresponding to vfl_type. */
devnode_bits(enum vfl_devnode_type vfl_type)121 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
122 {
123 	return devnode_nums[vfl_type];
124 }
125 #endif
126 
127 /* Mark device node number vdev->num as used */
devnode_set(struct video_device * vdev)128 static 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 */
devnode_clear(struct video_device * vdev)134 static 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> */
devnode_find(struct video_device * vdev,int from,int to)140 static 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 
video_device_alloc(void)145 struct video_device *video_device_alloc(void)
146 {
147 	return kzalloc(sizeof(struct video_device), GFP_KERNEL);
148 }
149 EXPORT_SYMBOL(video_device_alloc);
150 
video_device_release(struct video_device * vdev)151 void video_device_release(struct video_device *vdev)
152 {
153 	kfree(vdev);
154 }
155 EXPORT_SYMBOL(video_device_release);
156 
video_device_release_empty(struct video_device * vdev)157 void video_device_release_empty(struct video_device *vdev)
158 {
159 	/* Do nothing */
160 	/* Only valid when the video_device struct is a static. */
161 }
162 EXPORT_SYMBOL(video_device_release_empty);
163 
video_get(struct video_device * vdev)164 static inline void video_get(struct video_device *vdev)
165 {
166 	get_device(&vdev->dev);
167 }
168 
video_put(struct video_device * vdev)169 static 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. */
v4l2_device_release(struct device * cd)175 static void v4l2_device_release(struct device *cd)
176 {
177 	struct video_device *vdev = to_video_device(cd);
178 	struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
179 
180 	mutex_lock(&videodev_lock);
181 	if (WARN_ON(video_devices[vdev->minor] != vdev)) {
182 		/* should not happen */
183 		mutex_unlock(&videodev_lock);
184 		return;
185 	}
186 
187 	/* Free up this device for reuse */
188 	video_devices[vdev->minor] = NULL;
189 
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;
195 
196 	/* Mark device node number as free */
197 	devnode_clear(vdev);
198 
199 	mutex_unlock(&videodev_lock);
200 
201 #if defined(CONFIG_MEDIA_CONTROLLER)
202 	if (v4l2_dev->mdev && vdev->vfl_dir != VFL_DIR_M2M) {
203 		/* Remove interfaces and interface links */
204 		media_devnode_remove(vdev->intf_devnode);
205 		if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
206 			media_device_unregister_entity(&vdev->entity);
207 	}
208 #endif
209 
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 	 */
218 	if (v4l2_dev->release == NULL)
219 		v4l2_dev = NULL;
220 
221 	/* Release video_device and perform other
222 	   cleanups as needed. */
223 	vdev->release(vdev);
224 
225 	/* Decrease v4l2_device refcount */
226 	if (v4l2_dev)
227 		v4l2_device_put(v4l2_dev);
228 }
229 
230 static struct class video_class = {
231 	.name = VIDEO_NAME,
232 	.dev_groups = video_device_groups,
233 };
234 
video_devdata(struct file * file)235 struct video_device *video_devdata(struct file *file)
236 {
237 	return video_devices[iminor(file_inode(file))];
238 }
239 EXPORT_SYMBOL(video_devdata);
240 
241 
242 /* Priority handling */
243 
prio_is_valid(enum v4l2_priority prio)244 static 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 
v4l2_prio_init(struct v4l2_prio_state * global)251 void v4l2_prio_init(struct v4l2_prio_state *global)
252 {
253 	memset(global, 0, sizeof(*global));
254 }
255 EXPORT_SYMBOL(v4l2_prio_init);
256 
v4l2_prio_change(struct v4l2_prio_state * global,enum v4l2_priority * local,enum v4l2_priority new)257 int 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 }
271 EXPORT_SYMBOL(v4l2_prio_change);
272 
v4l2_prio_open(struct v4l2_prio_state * global,enum v4l2_priority * local)273 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
274 {
275 	v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
276 }
277 EXPORT_SYMBOL(v4l2_prio_open);
278 
v4l2_prio_close(struct v4l2_prio_state * global,enum v4l2_priority local)279 void 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 }
284 EXPORT_SYMBOL(v4l2_prio_close);
285 
v4l2_prio_max(struct v4l2_prio_state * global)286 enum 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 }
296 EXPORT_SYMBOL(v4l2_prio_max);
297 
v4l2_prio_check(struct v4l2_prio_state * global,enum v4l2_priority local)298 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
299 {
300 	return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
301 }
302 EXPORT_SYMBOL(v4l2_prio_check);
303 
304 
v4l2_read(struct file * filp,char __user * buf,size_t sz,loff_t * off)305 static 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);
309 	int ret = -ENODEV;
310 
311 	if (!vdev->fops->read)
312 		return -EINVAL;
313 	if (video_is_registered(vdev))
314 		ret = vdev->fops->read(filp, buf, sz, off);
315 	if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
316 	    (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
317 		dprintk("%s: read: %zd (%d)\n",
318 			video_device_node_name(vdev), sz, ret);
319 	return ret;
320 }
321 
v4l2_write(struct file * filp,const char __user * buf,size_t sz,loff_t * off)322 static 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);
326 	int ret = -ENODEV;
327 
328 	if (!vdev->fops->write)
329 		return -EINVAL;
330 	if (video_is_registered(vdev))
331 		ret = vdev->fops->write(filp, buf, sz, off);
332 	if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
333 	    (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
334 		dprintk("%s: write: %zd (%d)\n",
335 			video_device_node_name(vdev), sz, ret);
336 	return ret;
337 }
338 
v4l2_poll(struct file * filp,struct poll_table_struct * poll)339 static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
340 {
341 	struct video_device *vdev = video_devdata(filp);
342 	__poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;
343 
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 	}
350 	if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
351 		dprintk("%s: poll: %08x %08x\n",
352 			video_device_node_name(vdev), res,
353 			poll_requested_events(poll));
354 	return res;
355 }
356 
v4l2_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)357 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
358 {
359 	struct video_device *vdev = video_devdata(filp);
360 	int ret = -ENODEV;
361 
362 	if (vdev->fops->unlocked_ioctl) {
363 		if (video_is_registered(vdev))
364 			ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
365 	} else
366 		ret = -ENOTTY;
367 
368 	return ret;
369 }
370 
371 #ifdef CONFIG_MMU
372 #define v4l2_get_unmapped_area NULL
373 #else
v4l2_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)374 static 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);
379 	int ret;
380 
381 	if (!vdev->fops->get_unmapped_area)
382 		return -ENOSYS;
383 	if (!video_is_registered(vdev))
384 		return -ENODEV;
385 	ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
386 	if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
387 		dprintk("%s: get_unmapped_area (%d)\n",
388 			video_device_node_name(vdev), ret);
389 	return ret;
390 }
391 #endif
392 
v4l2_mmap(struct file * filp,struct vm_area_struct * vm)393 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
394 {
395 	struct video_device *vdev = video_devdata(filp);
396 	int ret = -ENODEV;
397 
398 	if (!vdev->fops->mmap)
399 		return -ENODEV;
400 	if (video_is_registered(vdev))
401 		ret = vdev->fops->mmap(filp, vm);
402 	if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
403 		dprintk("%s: mmap (%d)\n",
404 			video_device_node_name(vdev), ret);
405 	return ret;
406 }
407 
408 /* Override for the open function */
v4l2_open(struct inode * inode,struct file * filp)409 static int v4l2_open(struct inode *inode, struct file *filp)
410 {
411 	struct video_device *vdev;
412 	int ret = 0;
413 
414 	/* Check if the video device is available */
415 	mutex_lock(&videodev_lock);
416 	vdev = video_devdata(filp);
417 	/* return ENODEV if the video device has already been removed. */
418 	if (vdev == NULL || !video_is_registered(vdev)) {
419 		mutex_unlock(&videodev_lock);
420 		return -ENODEV;
421 	}
422 	/* and increase the device refcount */
423 	video_get(vdev);
424 	mutex_unlock(&videodev_lock);
425 	if (vdev->fops->open) {
426 		if (video_is_registered(vdev))
427 			ret = vdev->fops->open(filp);
428 		else
429 			ret = -ENODEV;
430 	}
431 
432 	if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
433 		dprintk("%s: open (%d)\n",
434 			video_device_node_name(vdev), ret);
435 	/* decrease the refcount in case of an error */
436 	if (ret)
437 		video_put(vdev);
438 	return ret;
439 }
440 
441 /* Override for the release function */
v4l2_release(struct inode * inode,struct file * filp)442 static int v4l2_release(struct inode *inode, struct file *filp)
443 {
444 	struct video_device *vdev = video_devdata(filp);
445 	int ret = 0;
446 
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 
463 	if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
464 		dprintk("%s: release\n",
465 			video_device_node_name(vdev));
466 
467 	/* decrease the refcount unconditionally since the release()
468 	   return value is ignored. */
469 	video_put(vdev);
470 	return ret;
471 }
472 
473 static const struct file_operations v4l2_fops = {
474 	.owner = THIS_MODULE,
475 	.read = v4l2_read,
476 	.write = v4l2_write,
477 	.open = v4l2_open,
478 	.get_unmapped_area = v4l2_get_unmapped_area,
479 	.mmap = v4l2_mmap,
480 	.unlocked_ioctl = v4l2_ioctl,
481 #ifdef CONFIG_COMPAT
482 	.compat_ioctl = v4l2_compat_ioctl32,
483 #endif
484 	.release = v4l2_release,
485 	.poll = v4l2_poll,
486 };
487 
488 /**
489  * get_index - assign stream index number based on v4l2_dev
490  * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
491  *
492  * Note that when this is called the new device has not yet been registered
493  * in the video_device array, but it was able to obtain a minor number.
494  *
495  * This means that we can always obtain a free stream index number since
496  * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
497  * use of the video_device array.
498  *
499  * Returns a free index number.
500  */
get_index(struct video_device * vdev)501 static int get_index(struct video_device *vdev)
502 {
503 	/* This can be static since this function is called with the global
504 	   videodev_lock held. */
505 	static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
506 	int i;
507 
508 	bitmap_zero(used, VIDEO_NUM_DEVICES);
509 
510 	for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
511 		if (video_devices[i] != NULL &&
512 		    video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
513 			__set_bit(video_devices[i]->index, used);
514 		}
515 	}
516 
517 	return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
518 }
519 
520 #define SET_VALID_IOCTL(ops, cmd, op) \
521 	do { if ((ops)->op) __set_bit(_IOC_NR(cmd), valid_ioctls); } while (0)
522 
523 /* This determines which ioctls are actually implemented in the driver.
524    It's a one-time thing which simplifies video_ioctl2 as it can just do
525    a bit test.
526 
527    Note that drivers can override this by setting bits to 1 in
528    vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
529    called, then that ioctl will actually be marked as unimplemented.
530 
531    It does that by first setting up the local valid_ioctls bitmap, and
532    at the end do a:
533 
534    vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
535  */
determine_valid_ioctls(struct video_device * vdev)536 static void determine_valid_ioctls(struct video_device *vdev)
537 {
538 	const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
539 			     V4L2_CAP_VIDEO_CAPTURE_MPLANE |
540 			     V4L2_CAP_VIDEO_OUTPUT |
541 			     V4L2_CAP_VIDEO_OUTPUT_MPLANE |
542 			     V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
543 	const u32 meta_caps = V4L2_CAP_META_CAPTURE |
544 			      V4L2_CAP_META_OUTPUT;
545 	DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
546 	const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
547 	bool is_vid = vdev->vfl_type == VFL_TYPE_VIDEO &&
548 		      (vdev->device_caps & vid_caps);
549 	bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
550 	bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
551 	bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
552 	bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
553 	bool is_meta = vdev->vfl_type == VFL_TYPE_VIDEO &&
554 		       (vdev->device_caps & meta_caps);
555 	bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
556 	bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
557 	bool is_io_mc = vdev->device_caps & V4L2_CAP_IO_MC;
558 	bool has_streaming = vdev->device_caps & V4L2_CAP_STREAMING;
559 	bool is_edid =  vdev->device_caps & V4L2_CAP_EDID;
560 
561 	bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
562 
563 	/* vfl_type and vfl_dir independent ioctls */
564 
565 	SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
566 	__set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
567 	__set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
568 
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)
574 		__set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
575 	if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
576 		__set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
577 	if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
578 		__set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
579 	if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
580 		__set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
581 	if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
582 		__set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
583 	if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
584 		__set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
585 	if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
586 		__set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
587 	if (vdev->ctrl_handler || ops->vidioc_querymenu)
588 		__set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
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 	}
593 	SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
594 #ifdef CONFIG_VIDEO_ADV_DEBUG
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);
598 #endif
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);
603 	if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
604 		__set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
605 
606 	if (is_vid) {
607 		/* video specific ioctls */
608 		if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
609 			       ops->vidioc_enum_fmt_vid_overlay)) ||
610 		    (is_tx && ops->vidioc_enum_fmt_vid_out))
611 			__set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
612 		if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
613 			       ops->vidioc_g_fmt_vid_cap_mplane ||
614 			       ops->vidioc_g_fmt_vid_overlay)) ||
615 		    (is_tx && (ops->vidioc_g_fmt_vid_out ||
616 			       ops->vidioc_g_fmt_vid_out_mplane ||
617 			       ops->vidioc_g_fmt_vid_out_overlay)))
618 			__set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
619 		if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
620 			       ops->vidioc_s_fmt_vid_cap_mplane ||
621 			       ops->vidioc_s_fmt_vid_overlay)) ||
622 		    (is_tx && (ops->vidioc_s_fmt_vid_out ||
623 			       ops->vidioc_s_fmt_vid_out_mplane ||
624 			       ops->vidioc_s_fmt_vid_out_overlay)))
625 			__set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
626 		if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
627 			       ops->vidioc_try_fmt_vid_cap_mplane ||
628 			       ops->vidioc_try_fmt_vid_overlay)) ||
629 		    (is_tx && (ops->vidioc_try_fmt_vid_out ||
630 			       ops->vidioc_try_fmt_vid_out_mplane ||
631 			       ops->vidioc_try_fmt_vid_out_overlay)))
632 			__set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
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);
645 		if (ops->vidioc_g_selection &&
646 		    !test_bit(_IOC_NR(VIDIOC_G_SELECTION), vdev->valid_ioctls)) {
647 			__set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
648 			__set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
649 		}
650 		if (ops->vidioc_s_selection &&
651 		    !test_bit(_IOC_NR(VIDIOC_S_SELECTION), vdev->valid_ioctls))
652 			__set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
653 		SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
654 		SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
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) {
670 		/* vbi specific ioctls */
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)))
675 			__set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
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)))
680 			__set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
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)))
685 			__set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
686 		SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
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);
700 	} else if (is_sdr && is_rx) {
701 		/* SDR receiver specific ioctls */
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);
706 	} else if (is_sdr && is_tx) {
707 		/* SDR transmitter specific ioctls */
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);
712 	}
713 
714 	if (has_streaming) {
715 		/* ioctls valid for streaming I/O */
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);
723 		SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
724 		SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
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);
728 	}
729 
730 	if (is_vid || is_vbi || is_meta) {
731 		/* ioctls valid for video, vbi and metadata */
732 		if (ops->vidioc_s_std)
733 			__set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
734 		SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
735 		SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
736 		if (is_rx) {
737 			SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
738 			if (is_io_mc) {
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);
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 			}
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);
750 			SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
751 			SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
752 		}
753 		if (is_tx) {
754 			if (is_io_mc) {
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);
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 			}
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 		}
767 		if (ops->vidioc_g_parm || ops->vidioc_g_std)
768 			__set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
769 		SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
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);
773 		SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
774 		SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
775 	}
776 	if (is_tx && (is_radio || is_sdr)) {
777 		/* radio transmitter only ioctls */
778 		SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
779 		SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
780 	}
781 	if (is_rx && !is_tch) {
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 	if (is_edid) {
788 		SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
789 		if (is_tx) {
790 			SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
791 			SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
792 			SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
793 		}
794 		if (is_rx) {
795 			SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
796 			SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
797 			SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
798 			SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
799 		}
800 	}
801 
802 	bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
803 			BASE_VIDIOC_PRIVATE);
804 }
805 
video_register_media_controller(struct video_device * vdev)806 static int video_register_media_controller(struct video_device *vdev)
807 {
808 #if defined(CONFIG_MEDIA_CONTROLLER)
809 	u32 intf_type;
810 	int ret;
811 
812 	/* Memory-to-memory devices are more complex and use
813 	 * their own function to register its mc entities.
814 	 */
815 	if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
816 		return 0;
817 
818 	vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
819 	vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
820 
821 	switch (vdev->vfl_type) {
822 	case VFL_TYPE_VIDEO:
823 		intf_type = MEDIA_INTF_T_V4L_VIDEO;
824 		vdev->entity.function = MEDIA_ENT_F_IO_V4L;
825 		break;
826 	case VFL_TYPE_VBI:
827 		intf_type = MEDIA_INTF_T_V4L_VBI;
828 		vdev->entity.function = MEDIA_ENT_F_IO_VBI;
829 		break;
830 	case VFL_TYPE_SDR:
831 		intf_type = MEDIA_INTF_T_V4L_SWRADIO;
832 		vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
833 		break;
834 	case VFL_TYPE_TOUCH:
835 		intf_type = MEDIA_INTF_T_V4L_TOUCH;
836 		vdev->entity.function = MEDIA_ENT_F_IO_V4L;
837 		break;
838 	case VFL_TYPE_RADIO:
839 		intf_type = MEDIA_INTF_T_V4L_RADIO;
840 		/*
841 		 * Radio doesn't have an entity at the V4L2 side to represent
842 		 * radio input or output. Instead, the audio input/output goes
843 		 * via either physical wires or ALSA.
844 		 */
845 		break;
846 	case VFL_TYPE_SUBDEV:
847 		intf_type = MEDIA_INTF_T_V4L_SUBDEV;
848 		/* Entity will be created via v4l2_device_register_subdev() */
849 		break;
850 	default:
851 		return 0;
852 	}
853 
854 	if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
855 		vdev->entity.name = vdev->name;
856 
857 		/* Needed just for backward compatibility with legacy MC API */
858 		vdev->entity.info.dev.major = VIDEO_MAJOR;
859 		vdev->entity.info.dev.minor = vdev->minor;
860 
861 		ret = media_device_register_entity(vdev->v4l2_dev->mdev,
862 						   &vdev->entity);
863 		if (ret < 0) {
864 			pr_warn("%s: media_device_register_entity failed\n",
865 				__func__);
866 			return ret;
867 		}
868 	}
869 
870 	vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
871 						  intf_type,
872 						  0, VIDEO_MAJOR,
873 						  vdev->minor);
874 	if (!vdev->intf_devnode) {
875 		media_device_unregister_entity(&vdev->entity);
876 		return -ENOMEM;
877 	}
878 
879 	if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
880 		struct media_link *link;
881 
882 		link = media_create_intf_link(&vdev->entity,
883 					      &vdev->intf_devnode->intf,
884 					      MEDIA_LNK_FL_ENABLED |
885 					      MEDIA_LNK_FL_IMMUTABLE);
886 		if (!link) {
887 			media_devnode_remove(vdev->intf_devnode);
888 			media_device_unregister_entity(&vdev->entity);
889 			return -ENOMEM;
890 		}
891 	}
892 
893 	/* FIXME: how to create the other interface links? */
894 
895 #endif
896 	return 0;
897 }
898 
__video_register_device(struct video_device * vdev,enum vfl_devnode_type type,int nr,int warn_if_nr_in_use,struct module * owner)899 int __video_register_device(struct video_device *vdev,
900 			    enum vfl_devnode_type type,
901 			    int nr, int warn_if_nr_in_use,
902 			    struct module *owner)
903 {
904 	int i = 0;
905 	int ret;
906 	int minor_offset = 0;
907 	int minor_cnt = VIDEO_NUM_DEVICES;
908 	const char *name_base;
909 
910 	/* A minor value of -1 marks this video device as never
911 	   having been registered */
912 	vdev->minor = -1;
913 
914 	/* the release callback MUST be present */
915 	if (WARN_ON(!vdev->release))
916 		return -EINVAL;
917 	/* the v4l2_dev pointer MUST be present */
918 	if (WARN_ON(!vdev->v4l2_dev))
919 		return -EINVAL;
920 	/* the device_caps field MUST be set for all but subdevs */
921 	if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
922 		return -EINVAL;
923 
924 	/* v4l2_fh support */
925 	spin_lock_init(&vdev->fh_lock);
926 	INIT_LIST_HEAD(&vdev->fh_list);
927 
928 	/* Part 1: check device type */
929 	switch (type) {
930 	case VFL_TYPE_VIDEO:
931 		name_base = "video";
932 		break;
933 	case VFL_TYPE_VBI:
934 		name_base = "vbi";
935 		break;
936 	case VFL_TYPE_RADIO:
937 		name_base = "radio";
938 		break;
939 	case VFL_TYPE_SUBDEV:
940 		name_base = "v4l-subdev";
941 		break;
942 	case VFL_TYPE_SDR:
943 		/* Use device name 'swradio' because 'sdr' was already taken. */
944 		name_base = "swradio";
945 		break;
946 	case VFL_TYPE_TOUCH:
947 		name_base = "v4l-touch";
948 		break;
949 	default:
950 		pr_err("%s called with unknown type: %d\n",
951 		       __func__, type);
952 		return -EINVAL;
953 	}
954 
955 	vdev->vfl_type = type;
956 	vdev->cdev = NULL;
957 	if (vdev->dev_parent == NULL)
958 		vdev->dev_parent = vdev->v4l2_dev->dev;
959 	if (vdev->ctrl_handler == NULL)
960 		vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
961 	/* If the prio state pointer is NULL, then use the v4l2_device
962 	   prio state. */
963 	if (vdev->prio == NULL)
964 		vdev->prio = &vdev->v4l2_dev->prio;
965 
966 	/* Part 2: find a free minor, device node number and device index. */
967 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
968 	/* Keep the ranges for the first four types for historical
969 	 * reasons.
970 	 * Newer devices (not yet in place) should use the range
971 	 * of 128-191 and just pick the first free minor there
972 	 * (new style). */
973 	switch (type) {
974 	case VFL_TYPE_VIDEO:
975 		minor_offset = 0;
976 		minor_cnt = 64;
977 		break;
978 	case VFL_TYPE_RADIO:
979 		minor_offset = 64;
980 		minor_cnt = 64;
981 		break;
982 	case VFL_TYPE_VBI:
983 		minor_offset = 224;
984 		minor_cnt = 32;
985 		break;
986 	default:
987 		minor_offset = 128;
988 		minor_cnt = 64;
989 		break;
990 	}
991 #endif
992 
993 	/* Pick a device node number */
994 	mutex_lock(&videodev_lock);
995 	nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
996 	if (nr == minor_cnt)
997 		nr = devnode_find(vdev, 0, minor_cnt);
998 	if (nr == minor_cnt) {
999 		pr_err("could not get a free device node number\n");
1000 		mutex_unlock(&videodev_lock);
1001 		return -ENFILE;
1002 	}
1003 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
1004 	/* 1-on-1 mapping of device node number to minor number */
1005 	i = nr;
1006 #else
1007 	/* The device node number and minor numbers are independent, so
1008 	   we just find the first free minor number. */
1009 	for (i = 0; i < VIDEO_NUM_DEVICES; i++)
1010 		if (video_devices[i] == NULL)
1011 			break;
1012 	if (i == VIDEO_NUM_DEVICES) {
1013 		mutex_unlock(&videodev_lock);
1014 		pr_err("could not get a free minor\n");
1015 		return -ENFILE;
1016 	}
1017 #endif
1018 	vdev->minor = i + minor_offset;
1019 	vdev->num = nr;
1020 
1021 	/* Should not happen since we thought this minor was free */
1022 	if (WARN_ON(video_devices[vdev->minor])) {
1023 		mutex_unlock(&videodev_lock);
1024 		pr_err("video_device not empty!\n");
1025 		return -ENFILE;
1026 	}
1027 	devnode_set(vdev);
1028 	vdev->index = get_index(vdev);
1029 	video_devices[vdev->minor] = vdev;
1030 	mutex_unlock(&videodev_lock);
1031 
1032 	if (vdev->ioctl_ops)
1033 		determine_valid_ioctls(vdev);
1034 
1035 	/* Part 3: Initialize the character device */
1036 	vdev->cdev = cdev_alloc();
1037 	if (vdev->cdev == NULL) {
1038 		ret = -ENOMEM;
1039 		goto cleanup;
1040 	}
1041 	vdev->cdev->ops = &v4l2_fops;
1042 	vdev->cdev->owner = owner;
1043 	ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
1044 	if (ret < 0) {
1045 		pr_err("%s: cdev_add failed\n", __func__);
1046 		kfree(vdev->cdev);
1047 		vdev->cdev = NULL;
1048 		goto cleanup;
1049 	}
1050 
1051 	/* Part 4: register the device with sysfs */
1052 	vdev->dev.class = &video_class;
1053 	vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
1054 	vdev->dev.parent = vdev->dev_parent;
1055 	dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
1056 	mutex_lock(&videodev_lock);
1057 	ret = device_register(&vdev->dev);
1058 	if (ret < 0) {
1059 		mutex_unlock(&videodev_lock);
1060 		pr_err("%s: device_register failed\n", __func__);
1061 		goto cleanup;
1062 	}
1063 	/* Register the release callback that will be called when the last
1064 	   reference to the device goes away. */
1065 	vdev->dev.release = v4l2_device_release;
1066 
1067 	if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
1068 		pr_warn("%s: requested %s%d, got %s\n", __func__,
1069 			name_base, nr, video_device_node_name(vdev));
1070 
1071 	/* Increase v4l2_device refcount */
1072 	v4l2_device_get(vdev->v4l2_dev);
1073 
1074 	/* Part 5: Register the entity. */
1075 	ret = video_register_media_controller(vdev);
1076 
1077 	/* Part 6: Activate this minor. The char device can now be used. */
1078 	set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1079 	mutex_unlock(&videodev_lock);
1080 
1081 	return 0;
1082 
1083 cleanup:
1084 	mutex_lock(&videodev_lock);
1085 	if (vdev->cdev)
1086 		cdev_del(vdev->cdev);
1087 	video_devices[vdev->minor] = NULL;
1088 	devnode_clear(vdev);
1089 	mutex_unlock(&videodev_lock);
1090 	/* Mark this video device as never having been registered. */
1091 	vdev->minor = -1;
1092 	return ret;
1093 }
1094 EXPORT_SYMBOL(__video_register_device);
1095 
1096 /**
1097  *	video_unregister_device - unregister a video4linux device
1098  *	@vdev: the device to unregister
1099  *
1100  *	This unregisters the passed device. Future open calls will
1101  *	be met with errors.
1102  */
video_unregister_device(struct video_device * vdev)1103 void video_unregister_device(struct video_device *vdev)
1104 {
1105 	/* Check if vdev was ever registered at all */
1106 	if (!vdev || !video_is_registered(vdev))
1107 		return;
1108 
1109 	mutex_lock(&videodev_lock);
1110 	/* This must be in a critical section to prevent a race with v4l2_open.
1111 	 * Once this bit has been cleared video_get may never be called again.
1112 	 */
1113 	clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
1114 	mutex_unlock(&videodev_lock);
1115 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
1116 		v4l2_event_wake_all(vdev);
1117 	device_unregister(&vdev->dev);
1118 }
1119 EXPORT_SYMBOL(video_unregister_device);
1120 
1121 #if defined(CONFIG_MEDIA_CONTROLLER)
1122 
video_device_pipeline_start(struct video_device * vdev,struct media_pipeline * pipe)1123 __must_check int video_device_pipeline_start(struct video_device *vdev,
1124 					     struct media_pipeline *pipe)
1125 {
1126 	struct media_entity *entity = &vdev->entity;
1127 
1128 	if (entity->num_pads != 1)
1129 		return -ENODEV;
1130 
1131 	return media_pipeline_start(&entity->pads[0], pipe);
1132 }
1133 EXPORT_SYMBOL_GPL(video_device_pipeline_start);
1134 
__video_device_pipeline_start(struct video_device * vdev,struct media_pipeline * pipe)1135 __must_check int __video_device_pipeline_start(struct video_device *vdev,
1136 					       struct media_pipeline *pipe)
1137 {
1138 	struct media_entity *entity = &vdev->entity;
1139 
1140 	if (entity->num_pads != 1)
1141 		return -ENODEV;
1142 
1143 	return __media_pipeline_start(&entity->pads[0], pipe);
1144 }
1145 EXPORT_SYMBOL_GPL(__video_device_pipeline_start);
1146 
video_device_pipeline_stop(struct video_device * vdev)1147 void video_device_pipeline_stop(struct video_device *vdev)
1148 {
1149 	struct media_entity *entity = &vdev->entity;
1150 
1151 	if (WARN_ON(entity->num_pads != 1))
1152 		return;
1153 
1154 	return media_pipeline_stop(&entity->pads[0]);
1155 }
1156 EXPORT_SYMBOL_GPL(video_device_pipeline_stop);
1157 
__video_device_pipeline_stop(struct video_device * vdev)1158 void __video_device_pipeline_stop(struct video_device *vdev)
1159 {
1160 	struct media_entity *entity = &vdev->entity;
1161 
1162 	if (WARN_ON(entity->num_pads != 1))
1163 		return;
1164 
1165 	return __media_pipeline_stop(&entity->pads[0]);
1166 }
1167 EXPORT_SYMBOL_GPL(__video_device_pipeline_stop);
1168 
video_device_pipeline_alloc_start(struct video_device * vdev)1169 __must_check int video_device_pipeline_alloc_start(struct video_device *vdev)
1170 {
1171 	struct media_entity *entity = &vdev->entity;
1172 
1173 	if (entity->num_pads != 1)
1174 		return -ENODEV;
1175 
1176 	return media_pipeline_alloc_start(&entity->pads[0]);
1177 }
1178 EXPORT_SYMBOL_GPL(video_device_pipeline_alloc_start);
1179 
video_device_pipeline(struct video_device * vdev)1180 struct media_pipeline *video_device_pipeline(struct video_device *vdev)
1181 {
1182 	struct media_entity *entity = &vdev->entity;
1183 
1184 	if (WARN_ON(entity->num_pads != 1))
1185 		return NULL;
1186 
1187 	return media_pad_pipeline(&entity->pads[0]);
1188 }
1189 EXPORT_SYMBOL_GPL(video_device_pipeline);
1190 
1191 #endif /* CONFIG_MEDIA_CONTROLLER */
1192 
1193 /*
1194  *	Initialise video for linux
1195  */
videodev_init(void)1196 static int __init videodev_init(void)
1197 {
1198 	dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1199 	int ret;
1200 
1201 	pr_info("Linux video capture interface: v2.00\n");
1202 	ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1203 	if (ret < 0) {
1204 		pr_warn("videodev: unable to get major %d\n",
1205 				VIDEO_MAJOR);
1206 		return ret;
1207 	}
1208 
1209 	ret = class_register(&video_class);
1210 	if (ret < 0) {
1211 		unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1212 		pr_warn("video_dev: class_register failed\n");
1213 		return -EIO;
1214 	}
1215 
1216 	return 0;
1217 }
1218 
videodev_exit(void)1219 static void __exit videodev_exit(void)
1220 {
1221 	dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1222 
1223 	class_unregister(&video_class);
1224 	unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1225 }
1226 
1227 subsys_initcall(videodev_init);
1228 module_exit(videodev_exit)
1229 
1230 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@kernel.org>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1231 MODULE_DESCRIPTION("Video4Linux2 core driver");
1232 MODULE_LICENSE("GPL");
1233 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);
1234