1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Video capture interface for Linux version 2
5 * A generic video device interface for the LINUX operating system
6 * using a set of device structures/vectors for low level operations.
12 * - Added procfs support
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/debugfs.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.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>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-event.h>
34 #define VIDEO_NUM_DEVICES 256
35 #define VIDEO_NAME "video4linux"
37 #define dprintk(fmt, arg...) do { \
38 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
46 static ssize_t index_show(struct device *cd,
47 struct device_attribute *attr, char *buf)
49 struct video_device *vdev = to_video_device(cd);
51 return sprintf(buf, "%i\n", vdev->index);
53 static DEVICE_ATTR_RO(index);
55 static ssize_t dev_debug_show(struct device *cd,
56 struct device_attribute *attr, char *buf)
58 struct video_device *vdev = to_video_device(cd);
60 return sprintf(buf, "%i\n", vdev->dev_debug);
63 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
64 const char *buf, size_t len)
66 struct video_device *vdev = to_video_device(cd);
70 res = kstrtou16(buf, 0, &value);
74 vdev->dev_debug = value;
77 static DEVICE_ATTR_RW(dev_debug);
79 static ssize_t name_show(struct device *cd,
80 struct device_attribute *attr, char *buf)
82 struct video_device *vdev = to_video_device(cd);
84 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
86 static DEVICE_ATTR_RO(name);
88 static struct attribute *video_device_attrs[] = {
90 &dev_attr_dev_debug.attr,
94 ATTRIBUTE_GROUPS(video_device);
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);
103 /* Device node utility functions */
105 /* Note: these utility functions all assume that vfl_type is in the range
106 [0, VFL_TYPE_MAX-1]. */
108 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
109 /* Return the bitmap corresponding to vfl_type. */
110 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
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;
117 return devnode_nums[idx];
120 /* Return the bitmap corresponding to vfl_type. */
121 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
123 return devnode_nums[vfl_type];
127 /* Mark device node number vdev->num as used */
128 static inline void devnode_set(struct video_device *vdev)
130 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
133 /* Mark device node number vdev->num as unused */
134 static inline void devnode_clear(struct video_device *vdev)
136 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
139 /* Try to find a free device node number in the range [from, to> */
140 static inline int devnode_find(struct video_device *vdev, int from, int to)
142 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
145 struct video_device *video_device_alloc(void)
147 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
149 EXPORT_SYMBOL(video_device_alloc);
151 void video_device_release(struct video_device *vdev)
155 EXPORT_SYMBOL(video_device_release);
157 void video_device_release_empty(struct video_device *vdev)
160 /* Only valid when the video_device struct is a static. */
162 EXPORT_SYMBOL(video_device_release_empty);
164 static inline void video_get(struct video_device *vdev)
166 get_device(&vdev->dev);
169 static inline void video_put(struct video_device *vdev)
171 put_device(&vdev->dev);
174 /* Called when the last user of the video device exits. */
175 static void v4l2_device_release(struct device *cd)
177 struct video_device *vdev = to_video_device(cd);
178 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
180 mutex_lock(&videodev_lock);
181 if (WARN_ON(video_devices[vdev->minor] != vdev)) {
182 /* should not happen */
183 mutex_unlock(&videodev_lock);
187 /* Free up this device for reuse */
188 video_devices[vdev->minor] = NULL;
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. */
196 /* Mark device node number as free */
199 mutex_unlock(&videodev_lock);
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);
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.
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.
218 if (v4l2_dev->release == NULL)
221 /* Release video_device and perform other
222 cleanups as needed. */
225 /* Decrease v4l2_device refcount */
227 v4l2_device_put(v4l2_dev);
230 static struct class video_class = {
232 .dev_groups = video_device_groups,
235 struct video_device *video_devdata(struct file *file)
237 return video_devices[iminor(file_inode(file))];
239 EXPORT_SYMBOL(video_devdata);
242 /* Priority handling */
244 static inline bool prio_is_valid(enum v4l2_priority prio)
246 return prio == V4L2_PRIORITY_BACKGROUND ||
247 prio == V4L2_PRIORITY_INTERACTIVE ||
248 prio == V4L2_PRIORITY_RECORD;
251 void v4l2_prio_init(struct v4l2_prio_state *global)
253 memset(global, 0, sizeof(*global));
255 EXPORT_SYMBOL(v4l2_prio_init);
257 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
258 enum v4l2_priority new)
260 if (!prio_is_valid(new))
265 atomic_inc(&global->prios[new]);
266 if (prio_is_valid(*local))
267 atomic_dec(&global->prios[*local]);
271 EXPORT_SYMBOL(v4l2_prio_change);
273 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
275 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
277 EXPORT_SYMBOL(v4l2_prio_open);
279 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
281 if (prio_is_valid(local))
282 atomic_dec(&global->prios[local]);
284 EXPORT_SYMBOL(v4l2_prio_close);
286 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
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;
296 EXPORT_SYMBOL(v4l2_prio_max);
298 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
300 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
302 EXPORT_SYMBOL(v4l2_prio_check);
305 static ssize_t v4l2_read(struct file *filp, char __user *buf,
306 size_t sz, loff_t *off)
308 struct video_device *vdev = video_devdata(filp);
311 if (!vdev->fops->read)
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);
322 static ssize_t v4l2_write(struct file *filp, const char __user *buf,
323 size_t sz, loff_t *off)
325 struct video_device *vdev = video_devdata(filp);
328 if (!vdev->fops->write)
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);
339 static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
341 struct video_device *vdev = video_devdata(filp);
342 __poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;
344 if (video_is_registered(vdev)) {
345 if (!vdev->fops->poll)
346 res = DEFAULT_POLLMASK;
348 res = vdev->fops->poll(filp, poll);
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));
357 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
359 struct video_device *vdev = video_devdata(filp);
362 if (vdev->fops->unlocked_ioctl) {
363 if (video_is_registered(vdev))
364 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
372 #define v4l2_get_unmapped_area NULL
374 static unsigned long v4l2_get_unmapped_area(struct file *filp,
375 unsigned long addr, unsigned long len, unsigned long pgoff,
378 struct video_device *vdev = video_devdata(filp);
381 if (!vdev->fops->get_unmapped_area)
383 if (!video_is_registered(vdev))
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);
393 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
395 struct video_device *vdev = video_devdata(filp);
398 if (!vdev->fops->mmap)
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);
408 /* Override for the open function */
409 static int v4l2_open(struct inode *inode, struct file *filp)
411 struct video_device *vdev;
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);
422 /* and increase the device refcount */
424 mutex_unlock(&videodev_lock);
425 if (vdev->fops->open) {
426 if (video_is_registered(vdev))
427 ret = vdev->fops->open(filp);
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 */
441 /* Override for the release function */
442 static int v4l2_release(struct inode *inode, struct file *filp)
444 struct video_device *vdev = video_devdata(filp);
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.
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);
459 ret = vdev->fops->release(filp);
463 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
464 dprintk("%s: release\n",
465 video_device_node_name(vdev));
467 /* decrease the refcount unconditionally since the release()
468 return value is ignored. */
473 static const struct file_operations v4l2_fops = {
474 .owner = THIS_MODULE,
478 .get_unmapped_area = v4l2_get_unmapped_area,
480 .unlocked_ioctl = v4l2_ioctl,
482 .compat_ioctl = v4l2_compat_ioctl32,
484 .release = v4l2_release,
490 * get_index - assign stream index number based on v4l2_dev
491 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
493 * Note that when this is called the new device has not yet been registered
494 * in the video_device array, but it was able to obtain a minor number.
496 * This means that we can always obtain a free stream index number since
497 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
498 * use of the video_device array.
500 * Returns a free index number.
502 static int get_index(struct video_device *vdev)
504 /* This can be static since this function is called with the global
505 videodev_lock held. */
506 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
509 bitmap_zero(used, VIDEO_NUM_DEVICES);
511 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
512 if (video_devices[i] != NULL &&
513 video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
514 __set_bit(video_devices[i]->index, used);
518 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
521 #define SET_VALID_IOCTL(ops, cmd, op) \
522 do { if ((ops)->op) __set_bit(_IOC_NR(cmd), valid_ioctls); } while (0)
524 /* This determines which ioctls are actually implemented in the driver.
525 It's a one-time thing which simplifies video_ioctl2 as it can just do
528 Note that drivers can override this by setting bits to 1 in
529 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
530 called, then that ioctl will actually be marked as unimplemented.
532 It does that by first setting up the local valid_ioctls bitmap, and
535 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
537 static void determine_valid_ioctls(struct video_device *vdev)
539 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
540 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
541 V4L2_CAP_VIDEO_OUTPUT |
542 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
543 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
544 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
545 V4L2_CAP_META_OUTPUT;
546 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
547 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
548 bool is_vid = vdev->vfl_type == VFL_TYPE_VIDEO &&
549 (vdev->device_caps & vid_caps);
550 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
551 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
552 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
553 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
554 bool is_meta = vdev->vfl_type == VFL_TYPE_VIDEO &&
555 (vdev->device_caps & meta_caps);
556 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
557 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
558 bool is_io_mc = vdev->device_caps & V4L2_CAP_IO_MC;
560 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
562 /* vfl_type and vfl_dir independent ioctls */
564 SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
565 __set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
566 __set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
568 /* Note: the control handler can also be passed through the filehandle,
569 and that can't be tested here. If the bit for these control ioctls
570 is set, then the ioctl is valid. But if it is 0, then it can still
571 be valid if the filehandle passed the control handler. */
572 if (vdev->ctrl_handler || ops->vidioc_queryctrl)
573 __set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
574 if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
575 __set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
576 if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
577 __set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
578 if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
579 __set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
580 if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
581 __set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
582 if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
583 __set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
584 if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
585 __set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
586 if (vdev->ctrl_handler || ops->vidioc_querymenu)
587 __set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
589 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
590 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
592 SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
593 #ifdef CONFIG_VIDEO_ADV_DEBUG
594 __set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
595 __set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
596 __set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
598 /* yes, really vidioc_subscribe_event */
599 SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
600 SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
601 SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
602 if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
603 __set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
606 /* video specific ioctls */
607 if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
608 ops->vidioc_enum_fmt_vid_overlay)) ||
609 (is_tx && ops->vidioc_enum_fmt_vid_out))
610 __set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
611 if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
612 ops->vidioc_g_fmt_vid_cap_mplane ||
613 ops->vidioc_g_fmt_vid_overlay)) ||
614 (is_tx && (ops->vidioc_g_fmt_vid_out ||
615 ops->vidioc_g_fmt_vid_out_mplane ||
616 ops->vidioc_g_fmt_vid_out_overlay)))
617 __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
618 if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
619 ops->vidioc_s_fmt_vid_cap_mplane ||
620 ops->vidioc_s_fmt_vid_overlay)) ||
621 (is_tx && (ops->vidioc_s_fmt_vid_out ||
622 ops->vidioc_s_fmt_vid_out_mplane ||
623 ops->vidioc_s_fmt_vid_out_overlay)))
624 __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
625 if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
626 ops->vidioc_try_fmt_vid_cap_mplane ||
627 ops->vidioc_try_fmt_vid_overlay)) ||
628 (is_tx && (ops->vidioc_try_fmt_vid_out ||
629 ops->vidioc_try_fmt_vid_out_mplane ||
630 ops->vidioc_try_fmt_vid_out_overlay)))
631 __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
632 SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
633 SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
634 SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
635 SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
636 SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
637 SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
638 SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
639 SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
640 SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
641 SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
642 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
643 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
644 if (ops->vidioc_g_selection) {
645 __set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
646 __set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
648 if (ops->vidioc_s_selection)
649 __set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
650 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
651 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
653 if (is_meta && is_rx) {
654 /* metadata capture specific ioctls */
655 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_cap);
656 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_cap);
657 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_cap);
658 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_cap);
659 } else if (is_meta && is_tx) {
660 /* metadata output specific ioctls */
661 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_out);
662 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_out);
663 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_out);
664 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_out);
667 /* vbi specific ioctls */
668 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
669 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
670 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
671 ops->vidioc_g_fmt_sliced_vbi_out)))
672 __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
673 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
674 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
675 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
676 ops->vidioc_s_fmt_sliced_vbi_out)))
677 __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
678 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
679 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
680 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
681 ops->vidioc_try_fmt_sliced_vbi_out)))
682 __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
683 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
685 /* touch specific ioctls */
686 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_vid_cap);
687 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_vid_cap);
688 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_vid_cap);
689 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_vid_cap);
690 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
691 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
692 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
693 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
694 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
695 SET_VALID_IOCTL(ops, VIDIOC_G_PARM, vidioc_g_parm);
696 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
697 } else if (is_sdr && is_rx) {
698 /* SDR receiver specific ioctls */
699 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_cap);
700 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_cap);
701 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_cap);
702 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_cap);
703 } else if (is_sdr && is_tx) {
704 /* SDR transmitter specific ioctls */
705 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_out);
706 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_out);
707 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_out);
708 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_out);
711 if (is_vid || is_vbi || is_sdr || is_tch || is_meta) {
712 /* ioctls valid for video, vbi, sdr, touch and metadata */
713 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
714 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
715 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
716 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
717 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
718 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
719 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
720 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
721 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
724 if (is_vid || is_vbi || is_meta) {
725 /* ioctls valid for video, vbi and metadata */
726 if (ops->vidioc_s_std)
727 __set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
728 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
729 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
731 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
733 __set_bit(_IOC_NR(VIDIOC_ENUMINPUT), valid_ioctls);
734 __set_bit(_IOC_NR(VIDIOC_G_INPUT), valid_ioctls);
735 __set_bit(_IOC_NR(VIDIOC_S_INPUT), valid_ioctls);
737 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
738 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
739 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
741 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
742 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
743 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
744 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
745 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
749 __set_bit(_IOC_NR(VIDIOC_ENUMOUTPUT), valid_ioctls);
750 __set_bit(_IOC_NR(VIDIOC_G_OUTPUT), valid_ioctls);
751 __set_bit(_IOC_NR(VIDIOC_S_OUTPUT), valid_ioctls);
753 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
754 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
755 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
757 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
758 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
759 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
761 if (ops->vidioc_g_parm || ops->vidioc_g_std)
762 __set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
763 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
764 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
765 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
766 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
767 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
768 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
770 if (is_tx && (is_radio || is_sdr)) {
771 /* radio transmitter only ioctls */
772 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
773 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
775 if (is_rx && !is_tch) {
776 /* receiver only ioctls */
777 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
778 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
779 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
782 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
783 BASE_VIDIOC_PRIVATE);
786 static int video_register_media_controller(struct video_device *vdev)
788 #if defined(CONFIG_MEDIA_CONTROLLER)
792 /* Memory-to-memory devices are more complex and use
793 * their own function to register its mc entities.
795 if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
798 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
799 vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
801 switch (vdev->vfl_type) {
803 intf_type = MEDIA_INTF_T_V4L_VIDEO;
804 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
807 intf_type = MEDIA_INTF_T_V4L_VBI;
808 vdev->entity.function = MEDIA_ENT_F_IO_VBI;
811 intf_type = MEDIA_INTF_T_V4L_SWRADIO;
812 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
815 intf_type = MEDIA_INTF_T_V4L_TOUCH;
816 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
819 intf_type = MEDIA_INTF_T_V4L_RADIO;
821 * Radio doesn't have an entity at the V4L2 side to represent
822 * radio input or output. Instead, the audio input/output goes
823 * via either physical wires or ALSA.
826 case VFL_TYPE_SUBDEV:
827 intf_type = MEDIA_INTF_T_V4L_SUBDEV;
828 /* Entity will be created via v4l2_device_register_subdev() */
834 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
835 vdev->entity.name = vdev->name;
837 /* Needed just for backward compatibility with legacy MC API */
838 vdev->entity.info.dev.major = VIDEO_MAJOR;
839 vdev->entity.info.dev.minor = vdev->minor;
841 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
844 pr_warn("%s: media_device_register_entity failed\n",
850 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
854 if (!vdev->intf_devnode) {
855 media_device_unregister_entity(&vdev->entity);
859 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
860 struct media_link *link;
862 link = media_create_intf_link(&vdev->entity,
863 &vdev->intf_devnode->intf,
864 MEDIA_LNK_FL_ENABLED |
865 MEDIA_LNK_FL_IMMUTABLE);
867 media_devnode_remove(vdev->intf_devnode);
868 media_device_unregister_entity(&vdev->entity);
873 /* FIXME: how to create the other interface links? */
879 int __video_register_device(struct video_device *vdev,
880 enum vfl_devnode_type type,
881 int nr, int warn_if_nr_in_use,
882 struct module *owner)
886 int minor_offset = 0;
887 int minor_cnt = VIDEO_NUM_DEVICES;
888 const char *name_base;
890 /* A minor value of -1 marks this video device as never
891 having been registered */
894 /* the release callback MUST be present */
895 if (WARN_ON(!vdev->release))
897 /* the v4l2_dev pointer MUST be present */
898 if (WARN_ON(!vdev->v4l2_dev))
900 /* the device_caps field MUST be set for all but subdevs */
901 if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
904 /* v4l2_fh support */
905 spin_lock_init(&vdev->fh_lock);
906 INIT_LIST_HEAD(&vdev->fh_list);
908 /* Part 1: check device type */
919 case VFL_TYPE_SUBDEV:
920 name_base = "v4l-subdev";
923 /* Use device name 'swradio' because 'sdr' was already taken. */
924 name_base = "swradio";
927 name_base = "v4l-touch";
930 pr_err("%s called with unknown type: %d\n",
935 vdev->vfl_type = type;
937 if (vdev->dev_parent == NULL)
938 vdev->dev_parent = vdev->v4l2_dev->dev;
939 if (vdev->ctrl_handler == NULL)
940 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
941 /* If the prio state pointer is NULL, then use the v4l2_device
943 if (vdev->prio == NULL)
944 vdev->prio = &vdev->v4l2_dev->prio;
946 /* Part 2: find a free minor, device node number and device index. */
947 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
948 /* Keep the ranges for the first four types for historical
950 * Newer devices (not yet in place) should use the range
951 * of 128-191 and just pick the first free minor there
973 /* Pick a device node number */
974 mutex_lock(&videodev_lock);
975 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
977 nr = devnode_find(vdev, 0, minor_cnt);
978 if (nr == minor_cnt) {
979 pr_err("could not get a free device node number\n");
980 mutex_unlock(&videodev_lock);
983 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
984 /* 1-on-1 mapping of device node number to minor number */
987 /* The device node number and minor numbers are independent, so
988 we just find the first free minor number. */
989 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
990 if (video_devices[i] == NULL)
992 if (i == VIDEO_NUM_DEVICES) {
993 mutex_unlock(&videodev_lock);
994 pr_err("could not get a free minor\n");
998 vdev->minor = i + minor_offset;
1001 /* Should not happen since we thought this minor was free */
1002 if (WARN_ON(video_devices[vdev->minor])) {
1003 mutex_unlock(&videodev_lock);
1004 pr_err("video_device not empty!\n");
1008 vdev->index = get_index(vdev);
1009 video_devices[vdev->minor] = vdev;
1010 mutex_unlock(&videodev_lock);
1012 if (vdev->ioctl_ops)
1013 determine_valid_ioctls(vdev);
1015 /* Part 3: Initialize the character device */
1016 vdev->cdev = cdev_alloc();
1017 if (vdev->cdev == NULL) {
1021 vdev->cdev->ops = &v4l2_fops;
1022 vdev->cdev->owner = owner;
1023 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
1025 pr_err("%s: cdev_add failed\n", __func__);
1031 /* Part 4: register the device with sysfs */
1032 vdev->dev.class = &video_class;
1033 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
1034 vdev->dev.parent = vdev->dev_parent;
1035 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
1036 ret = device_register(&vdev->dev);
1038 pr_err("%s: device_register failed\n", __func__);
1041 /* Register the release callback that will be called when the last
1042 reference to the device goes away. */
1043 vdev->dev.release = v4l2_device_release;
1045 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
1046 pr_warn("%s: requested %s%d, got %s\n", __func__,
1047 name_base, nr, video_device_node_name(vdev));
1049 /* Increase v4l2_device refcount */
1050 v4l2_device_get(vdev->v4l2_dev);
1052 /* Part 5: Register the entity. */
1053 ret = video_register_media_controller(vdev);
1055 /* Part 6: Activate this minor. The char device can now be used. */
1056 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1061 mutex_lock(&videodev_lock);
1063 cdev_del(vdev->cdev);
1064 video_devices[vdev->minor] = NULL;
1065 devnode_clear(vdev);
1066 mutex_unlock(&videodev_lock);
1067 /* Mark this video device as never having been registered. */
1071 EXPORT_SYMBOL(__video_register_device);
1074 * video_unregister_device - unregister a video4linux device
1075 * @vdev: the device to unregister
1077 * This unregisters the passed device. Future open calls will
1078 * be met with errors.
1080 void video_unregister_device(struct video_device *vdev)
1082 /* Check if vdev was ever registered at all */
1083 if (!vdev || !video_is_registered(vdev))
1086 mutex_lock(&videodev_lock);
1087 /* This must be in a critical section to prevent a race with v4l2_open.
1088 * Once this bit has been cleared video_get may never be called again.
1090 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
1091 mutex_unlock(&videodev_lock);
1092 if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
1093 v4l2_event_wake_all(vdev);
1094 device_unregister(&vdev->dev);
1096 EXPORT_SYMBOL(video_unregister_device);
1099 * Initialise video for linux
1101 static int __init videodev_init(void)
1103 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1106 pr_info("Linux video capture interface: v2.00\n");
1107 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1109 pr_warn("videodev: unable to get major %d\n",
1114 ret = class_register(&video_class);
1116 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1117 pr_warn("video_dev: class_register failed\n");
1124 static void __exit videodev_exit(void)
1126 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1128 class_unregister(&video_class);
1129 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1132 subsys_initcall(videodev_init);
1133 module_exit(videodev_exit)
1135 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <
[email protected]>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1136 MODULE_DESCRIPTION("Video4Linux2 core driver");
1137 MODULE_LICENSE("GPL");
1138 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);