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), \
42 static struct dentry *v4l2_debugfs_dir;
48 static ssize_t index_show(struct device *cd,
49 struct device_attribute *attr, char *buf)
51 struct video_device *vdev = to_video_device(cd);
53 return sprintf(buf, "%i\n", vdev->index);
55 static DEVICE_ATTR_RO(index);
57 static ssize_t dev_debug_show(struct device *cd,
58 struct device_attribute *attr, char *buf)
60 struct video_device *vdev = to_video_device(cd);
62 return sprintf(buf, "%i\n", vdev->dev_debug);
65 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
66 const char *buf, size_t len)
68 struct video_device *vdev = to_video_device(cd);
72 res = kstrtou16(buf, 0, &value);
76 vdev->dev_debug = value;
79 static DEVICE_ATTR_RW(dev_debug);
81 static ssize_t name_show(struct device *cd,
82 struct device_attribute *attr, char *buf)
84 struct video_device *vdev = to_video_device(cd);
86 return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
88 static DEVICE_ATTR_RO(name);
90 static struct attribute *video_device_attrs[] = {
92 &dev_attr_dev_debug.attr,
96 ATTRIBUTE_GROUPS(video_device);
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);
105 /* Device node utility functions */
107 /* Note: these utility functions all assume that vfl_type is in the range
108 [0, VFL_TYPE_MAX-1]. */
110 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
111 /* Return the bitmap corresponding to vfl_type. */
112 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
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;
119 return devnode_nums[idx];
122 /* Return the bitmap corresponding to vfl_type. */
123 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
125 return devnode_nums[vfl_type];
129 /* Mark device node number vdev->num as used */
130 static inline void devnode_set(struct video_device *vdev)
132 set_bit(vdev->num, devnode_bits(vdev->vfl_type));
135 /* Mark device node number vdev->num as unused */
136 static inline void devnode_clear(struct video_device *vdev)
138 clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
141 /* Try to find a free device node number in the range [from, to> */
142 static inline int devnode_find(struct video_device *vdev, int from, int to)
144 return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
147 struct video_device *video_device_alloc(void)
149 return kzalloc(sizeof(struct video_device), GFP_KERNEL);
151 EXPORT_SYMBOL(video_device_alloc);
153 void video_device_release(struct video_device *vdev)
157 EXPORT_SYMBOL(video_device_release);
159 void video_device_release_empty(struct video_device *vdev)
162 /* Only valid when the video_device struct is a static. */
164 EXPORT_SYMBOL(video_device_release_empty);
166 static inline void video_get(struct video_device *vdev)
168 get_device(&vdev->dev);
171 static inline void video_put(struct video_device *vdev)
173 put_device(&vdev->dev);
176 /* Called when the last user of the video device exits. */
177 static void v4l2_device_release(struct device *cd)
179 struct video_device *vdev = to_video_device(cd);
180 struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
182 mutex_lock(&videodev_lock);
183 if (WARN_ON(video_devices[vdev->minor] != vdev)) {
184 /* should not happen */
185 mutex_unlock(&videodev_lock);
189 /* Free up this device for reuse */
190 video_devices[vdev->minor] = NULL;
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. */
198 /* Mark device node number as free */
201 mutex_unlock(&videodev_lock);
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);
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.
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.
220 if (v4l2_dev->release == NULL)
223 /* Release video_device and perform other
224 cleanups as needed. */
227 /* Decrease v4l2_device refcount */
229 v4l2_device_put(v4l2_dev);
232 static struct class video_class = {
234 .dev_groups = video_device_groups,
237 struct video_device *video_devdata(struct file *file)
239 return video_devices[iminor(file_inode(file))];
241 EXPORT_SYMBOL(video_devdata);
244 /* Priority handling */
246 static inline bool prio_is_valid(enum v4l2_priority prio)
248 return prio == V4L2_PRIORITY_BACKGROUND ||
249 prio == V4L2_PRIORITY_INTERACTIVE ||
250 prio == V4L2_PRIORITY_RECORD;
253 void v4l2_prio_init(struct v4l2_prio_state *global)
255 memset(global, 0, sizeof(*global));
257 EXPORT_SYMBOL(v4l2_prio_init);
259 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
260 enum v4l2_priority new)
262 if (!prio_is_valid(new))
267 atomic_inc(&global->prios[new]);
268 if (prio_is_valid(*local))
269 atomic_dec(&global->prios[*local]);
273 EXPORT_SYMBOL(v4l2_prio_change);
275 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
277 v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
279 EXPORT_SYMBOL(v4l2_prio_open);
281 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
283 if (prio_is_valid(local))
284 atomic_dec(&global->prios[local]);
286 EXPORT_SYMBOL(v4l2_prio_close);
288 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
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;
298 EXPORT_SYMBOL(v4l2_prio_max);
300 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
302 return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
304 EXPORT_SYMBOL(v4l2_prio_check);
307 static ssize_t v4l2_read(struct file *filp, char __user *buf,
308 size_t sz, loff_t *off)
310 struct video_device *vdev = video_devdata(filp);
313 if (!vdev->fops->read)
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);
324 static ssize_t v4l2_write(struct file *filp, const char __user *buf,
325 size_t sz, loff_t *off)
327 struct video_device *vdev = video_devdata(filp);
330 if (!vdev->fops->write)
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);
341 static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
343 struct video_device *vdev = video_devdata(filp);
344 __poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;
346 if (video_is_registered(vdev)) {
347 if (!vdev->fops->poll)
348 res = DEFAULT_POLLMASK;
350 res = vdev->fops->poll(filp, poll);
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));
359 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
361 struct video_device *vdev = video_devdata(filp);
364 if (vdev->fops->unlocked_ioctl) {
365 if (video_is_registered(vdev))
366 ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
374 #define v4l2_get_unmapped_area NULL
376 static unsigned long v4l2_get_unmapped_area(struct file *filp,
377 unsigned long addr, unsigned long len, unsigned long pgoff,
380 struct video_device *vdev = video_devdata(filp);
383 if (!vdev->fops->get_unmapped_area)
385 if (!video_is_registered(vdev))
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);
395 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
397 struct video_device *vdev = video_devdata(filp);
400 if (!vdev->fops->mmap)
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);
410 /* Override for the open function */
411 static int v4l2_open(struct inode *inode, struct file *filp)
413 struct video_device *vdev;
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);
424 /* and increase the device refcount */
426 mutex_unlock(&videodev_lock);
427 if (vdev->fops->open) {
428 if (video_is_registered(vdev))
429 ret = vdev->fops->open(filp);
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 */
443 /* Override for the release function */
444 static int v4l2_release(struct inode *inode, struct file *filp)
446 struct video_device *vdev = video_devdata(filp);
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.
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);
461 ret = vdev->fops->release(filp);
465 if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
466 dprintk("%s: release\n",
467 video_device_node_name(vdev));
469 /* decrease the refcount unconditionally since the release()
470 return value is ignored. */
475 static const struct file_operations v4l2_fops = {
476 .owner = THIS_MODULE,
480 .get_unmapped_area = v4l2_get_unmapped_area,
482 .unlocked_ioctl = v4l2_ioctl,
484 .compat_ioctl = v4l2_compat_ioctl32,
486 .release = v4l2_release,
492 * get_index - assign stream index number based on v4l2_dev
493 * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
495 * Note that when this is called the new device has not yet been registered
496 * in the video_device array, but it was able to obtain a minor number.
498 * This means that we can always obtain a free stream index number since
499 * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
500 * use of the video_device array.
502 * Returns a free index number.
504 static int get_index(struct video_device *vdev)
506 /* This can be static since this function is called with the global
507 videodev_lock held. */
508 static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
511 bitmap_zero(used, VIDEO_NUM_DEVICES);
513 for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
514 if (video_devices[i] != NULL &&
515 video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
516 set_bit(video_devices[i]->index, used);
520 return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
523 #define SET_VALID_IOCTL(ops, cmd, op) \
525 set_bit(_IOC_NR(cmd), valid_ioctls)
527 /* This determines which ioctls are actually implemented in the driver.
528 It's a one-time thing which simplifies video_ioctl2 as it can just do
531 Note that drivers can override this by setting bits to 1 in
532 vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
533 called, then that ioctl will actually be marked as unimplemented.
535 It does that by first setting up the local valid_ioctls bitmap, and
538 vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
540 static void determine_valid_ioctls(struct video_device *vdev)
542 const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
543 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
544 V4L2_CAP_VIDEO_OUTPUT |
545 V4L2_CAP_VIDEO_OUTPUT_MPLANE |
546 V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
547 const u32 meta_caps = V4L2_CAP_META_CAPTURE |
548 V4L2_CAP_META_OUTPUT;
549 DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
550 const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
551 bool is_vid = vdev->vfl_type == VFL_TYPE_VIDEO &&
552 (vdev->device_caps & vid_caps);
553 bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
554 bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
555 bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
556 bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
557 bool is_meta = vdev->vfl_type == VFL_TYPE_VIDEO &&
558 (vdev->device_caps & meta_caps);
559 bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
560 bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
561 bool is_io_mc = vdev->device_caps & V4L2_CAP_IO_MC;
563 bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
565 /* vfl_type and vfl_dir independent ioctls */
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);
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);
592 SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
593 SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
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);
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);
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 set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
649 set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
651 if (ops->vidioc_s_selection)
652 set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
653 SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
654 SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
656 if (is_meta && is_rx) {
657 /* metadata capture specific ioctls */
658 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_cap);
659 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_cap);
660 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_cap);
661 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_cap);
662 } else if (is_meta && is_tx) {
663 /* metadata output specific ioctls */
664 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_out);
665 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_out);
666 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_out);
667 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_out);
670 /* vbi specific ioctls */
671 if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
672 ops->vidioc_g_fmt_sliced_vbi_cap)) ||
673 (is_tx && (ops->vidioc_g_fmt_vbi_out ||
674 ops->vidioc_g_fmt_sliced_vbi_out)))
675 set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
676 if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
677 ops->vidioc_s_fmt_sliced_vbi_cap)) ||
678 (is_tx && (ops->vidioc_s_fmt_vbi_out ||
679 ops->vidioc_s_fmt_sliced_vbi_out)))
680 set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
681 if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
682 ops->vidioc_try_fmt_sliced_vbi_cap)) ||
683 (is_tx && (ops->vidioc_try_fmt_vbi_out ||
684 ops->vidioc_try_fmt_sliced_vbi_out)))
685 set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
686 SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
688 /* touch specific ioctls */
689 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_vid_cap);
690 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_vid_cap);
691 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_vid_cap);
692 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_vid_cap);
693 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
694 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
695 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
696 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
697 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
698 SET_VALID_IOCTL(ops, VIDIOC_G_PARM, vidioc_g_parm);
699 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
700 } else if (is_sdr && is_rx) {
701 /* SDR receiver specific ioctls */
702 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_cap);
703 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_cap);
704 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_cap);
705 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_cap);
706 } else if (is_sdr && is_tx) {
707 /* SDR transmitter specific ioctls */
708 SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_out);
709 SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_out);
710 SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_out);
711 SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_out);
714 if (is_vid || is_vbi || is_sdr || is_tch || is_meta) {
715 /* ioctls valid for video, vbi, sdr, touch and metadata */
716 SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
717 SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
718 SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
719 SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
720 SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
721 SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
722 SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
723 SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
724 SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
727 if (is_vid || is_vbi || is_meta) {
728 /* ioctls valid for video, vbi and metadata */
729 if (ops->vidioc_s_std)
730 set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
731 SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
732 SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
734 SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
736 set_bit(_IOC_NR(VIDIOC_ENUMINPUT), valid_ioctls);
737 set_bit(_IOC_NR(VIDIOC_G_INPUT), valid_ioctls);
738 set_bit(_IOC_NR(VIDIOC_S_INPUT), valid_ioctls);
740 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
741 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
742 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
744 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
745 SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
746 SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
747 SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
748 SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
752 set_bit(_IOC_NR(VIDIOC_ENUMOUTPUT), valid_ioctls);
753 set_bit(_IOC_NR(VIDIOC_G_OUTPUT), valid_ioctls);
754 set_bit(_IOC_NR(VIDIOC_S_OUTPUT), valid_ioctls);
756 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
757 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
758 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
760 SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
761 SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
762 SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
764 if (ops->vidioc_g_parm || ops->vidioc_g_std)
765 set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
766 SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
767 SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
768 SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
769 SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
770 SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
771 SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
773 if (is_tx && (is_radio || is_sdr)) {
774 /* radio transmitter only ioctls */
775 SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
776 SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
778 if (is_rx && !is_tch) {
779 /* receiver only ioctls */
780 SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
781 SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
782 SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
785 bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
786 BASE_VIDIOC_PRIVATE);
789 static int video_register_media_controller(struct video_device *vdev)
791 #if defined(CONFIG_MEDIA_CONTROLLER)
795 /* Memory-to-memory devices are more complex and use
796 * their own function to register its mc entities.
798 if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
801 vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
802 vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
804 switch (vdev->vfl_type) {
806 intf_type = MEDIA_INTF_T_V4L_VIDEO;
807 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
810 intf_type = MEDIA_INTF_T_V4L_VBI;
811 vdev->entity.function = MEDIA_ENT_F_IO_VBI;
814 intf_type = MEDIA_INTF_T_V4L_SWRADIO;
815 vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
818 intf_type = MEDIA_INTF_T_V4L_TOUCH;
819 vdev->entity.function = MEDIA_ENT_F_IO_V4L;
822 intf_type = MEDIA_INTF_T_V4L_RADIO;
824 * Radio doesn't have an entity at the V4L2 side to represent
825 * radio input or output. Instead, the audio input/output goes
826 * via either physical wires or ALSA.
829 case VFL_TYPE_SUBDEV:
830 intf_type = MEDIA_INTF_T_V4L_SUBDEV;
831 /* Entity will be created via v4l2_device_register_subdev() */
837 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
838 vdev->entity.name = vdev->name;
840 /* Needed just for backward compatibility with legacy MC API */
841 vdev->entity.info.dev.major = VIDEO_MAJOR;
842 vdev->entity.info.dev.minor = vdev->minor;
844 ret = media_device_register_entity(vdev->v4l2_dev->mdev,
847 pr_warn("%s: media_device_register_entity failed\n",
853 vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
857 if (!vdev->intf_devnode) {
858 media_device_unregister_entity(&vdev->entity);
862 if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
863 struct media_link *link;
865 link = media_create_intf_link(&vdev->entity,
866 &vdev->intf_devnode->intf,
867 MEDIA_LNK_FL_ENABLED |
868 MEDIA_LNK_FL_IMMUTABLE);
870 media_devnode_remove(vdev->intf_devnode);
871 media_device_unregister_entity(&vdev->entity);
876 /* FIXME: how to create the other interface links? */
882 int __video_register_device(struct video_device *vdev,
883 enum vfl_devnode_type type,
884 int nr, int warn_if_nr_in_use,
885 struct module *owner)
889 int minor_offset = 0;
890 int minor_cnt = VIDEO_NUM_DEVICES;
891 const char *name_base;
893 /* A minor value of -1 marks this video device as never
894 having been registered */
897 /* the release callback MUST be present */
898 if (WARN_ON(!vdev->release))
900 /* the v4l2_dev pointer MUST be present */
901 if (WARN_ON(!vdev->v4l2_dev))
903 /* the device_caps field MUST be set for all but subdevs */
904 if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
907 /* v4l2_fh support */
908 spin_lock_init(&vdev->fh_lock);
909 INIT_LIST_HEAD(&vdev->fh_list);
911 /* Part 1: check device type */
922 case VFL_TYPE_SUBDEV:
923 name_base = "v4l-subdev";
926 /* Use device name 'swradio' because 'sdr' was already taken. */
927 name_base = "swradio";
930 name_base = "v4l-touch";
933 pr_err("%s called with unknown type: %d\n",
938 vdev->vfl_type = type;
940 if (vdev->dev_parent == NULL)
941 vdev->dev_parent = vdev->v4l2_dev->dev;
942 if (vdev->ctrl_handler == NULL)
943 vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
944 /* If the prio state pointer is NULL, then use the v4l2_device
946 if (vdev->prio == NULL)
947 vdev->prio = &vdev->v4l2_dev->prio;
949 /* Part 2: find a free minor, device node number and device index. */
950 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
951 /* Keep the ranges for the first four types for historical
953 * Newer devices (not yet in place) should use the range
954 * of 128-191 and just pick the first free minor there
976 /* Pick a device node number */
977 mutex_lock(&videodev_lock);
978 nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
980 nr = devnode_find(vdev, 0, minor_cnt);
981 if (nr == minor_cnt) {
982 pr_err("could not get a free device node number\n");
983 mutex_unlock(&videodev_lock);
986 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
987 /* 1-on-1 mapping of device node number to minor number */
990 /* The device node number and minor numbers are independent, so
991 we just find the first free minor number. */
992 for (i = 0; i < VIDEO_NUM_DEVICES; i++)
993 if (video_devices[i] == NULL)
995 if (i == VIDEO_NUM_DEVICES) {
996 mutex_unlock(&videodev_lock);
997 pr_err("could not get a free minor\n");
1001 vdev->minor = i + minor_offset;
1004 /* Should not happen since we thought this minor was free */
1005 if (WARN_ON(video_devices[vdev->minor])) {
1006 mutex_unlock(&videodev_lock);
1007 pr_err("video_device not empty!\n");
1011 vdev->index = get_index(vdev);
1012 video_devices[vdev->minor] = vdev;
1013 mutex_unlock(&videodev_lock);
1015 if (vdev->ioctl_ops)
1016 determine_valid_ioctls(vdev);
1018 /* Part 3: Initialize the character device */
1019 vdev->cdev = cdev_alloc();
1020 if (vdev->cdev == NULL) {
1024 vdev->cdev->ops = &v4l2_fops;
1025 vdev->cdev->owner = owner;
1026 ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
1028 pr_err("%s: cdev_add failed\n", __func__);
1034 /* Part 4: register the device with sysfs */
1035 vdev->dev.class = &video_class;
1036 vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
1037 vdev->dev.parent = vdev->dev_parent;
1038 dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
1039 ret = device_register(&vdev->dev);
1041 pr_err("%s: device_register failed\n", __func__);
1044 /* Register the release callback that will be called when the last
1045 reference to the device goes away. */
1046 vdev->dev.release = v4l2_device_release;
1048 if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
1049 pr_warn("%s: requested %s%d, got %s\n", __func__,
1050 name_base, nr, video_device_node_name(vdev));
1052 /* Increase v4l2_device refcount */
1053 v4l2_device_get(vdev->v4l2_dev);
1055 /* Part 5: Register the entity. */
1056 ret = video_register_media_controller(vdev);
1058 /* Part 6: Activate this minor. The char device can now be used. */
1059 set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1064 mutex_lock(&videodev_lock);
1066 cdev_del(vdev->cdev);
1067 video_devices[vdev->minor] = NULL;
1068 devnode_clear(vdev);
1069 mutex_unlock(&videodev_lock);
1070 /* Mark this video device as never having been registered. */
1074 EXPORT_SYMBOL(__video_register_device);
1077 * video_unregister_device - unregister a video4linux device
1078 * @vdev: the device to unregister
1080 * This unregisters the passed device. Future open calls will
1081 * be met with errors.
1083 void video_unregister_device(struct video_device *vdev)
1085 /* Check if vdev was ever registered at all */
1086 if (!vdev || !video_is_registered(vdev))
1089 mutex_lock(&videodev_lock);
1090 /* This must be in a critical section to prevent a race with v4l2_open.
1091 * Once this bit has been cleared video_get may never be called again.
1093 clear_bit(V4L2_FL_REGISTERED, &vdev->flags);
1094 mutex_unlock(&videodev_lock);
1095 if (test_bit(V4L2_FL_USES_V4L2_FH, &vdev->flags))
1096 v4l2_event_wake_all(vdev);
1097 device_unregister(&vdev->dev);
1099 EXPORT_SYMBOL(video_unregister_device);
1102 * Initialise video for linux
1104 static int __init videodev_init(void)
1106 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1109 pr_info("Linux video capture interface: v2.00\n");
1110 ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1112 pr_warn("videodev: unable to get major %d\n",
1117 ret = class_register(&video_class);
1119 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1120 pr_warn("video_dev: class_register failed\n");
1124 v4l2_debugfs_dir = debugfs_create_dir("video4linux", NULL);
1125 v4l2_async_debug_init(v4l2_debugfs_dir);
1129 static void __exit videodev_exit(void)
1131 dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1133 debugfs_remove_recursive(v4l2_debugfs_dir);
1134 class_unregister(&video_class);
1135 unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1138 subsys_initcall(videodev_init);
1139 module_exit(videodev_exit)
1141 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <
[email protected]>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1142 MODULE_DESCRIPTION("Video4Linux2 core driver");
1143 MODULE_LICENSE("GPL");
1144 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);