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