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