Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Video capture interface for Linux version 2
0004  *
0005  *  A generic video device interface for the LINUX operating system
0006  *  using a set of device structures/vectors for low level operations.
0007  *
0008  * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
0009  *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
0010  *
0011  * Fixes:   20000516  Claudio Matsuoka <claudio@conectiva.com>
0012  *      - Added procfs support
0013  */
0014 
0015 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0016 
0017 #include <linux/debugfs.h>
0018 #include <linux/module.h>
0019 #include <linux/types.h>
0020 #include <linux/kernel.h>
0021 #include <linux/mm.h>
0022 #include <linux/string.h>
0023 #include <linux/errno.h>
0024 #include <linux/init.h>
0025 #include <linux/kmod.h>
0026 #include <linux/slab.h>
0027 #include <linux/uaccess.h>
0028 
0029 #include <media/v4l2-common.h>
0030 #include <media/v4l2-device.h>
0031 #include <media/v4l2-ioctl.h>
0032 #include <media/v4l2-event.h>
0033 
0034 #define VIDEO_NUM_DEVICES   256
0035 #define VIDEO_NAME              "video4linux"
0036 
0037 #define dprintk(fmt, arg...) do {                   \
0038         printk(KERN_DEBUG pr_fmt("%s: " fmt),           \
0039                __func__, ##arg);                \
0040 } while (0)
0041 
0042 /*
0043  *  sysfs stuff
0044  */
0045 
0046 static ssize_t index_show(struct device *cd,
0047               struct device_attribute *attr, char *buf)
0048 {
0049     struct video_device *vdev = to_video_device(cd);
0050 
0051     return sprintf(buf, "%i\n", vdev->index);
0052 }
0053 static DEVICE_ATTR_RO(index);
0054 
0055 static ssize_t dev_debug_show(struct device *cd,
0056               struct device_attribute *attr, char *buf)
0057 {
0058     struct video_device *vdev = to_video_device(cd);
0059 
0060     return sprintf(buf, "%i\n", vdev->dev_debug);
0061 }
0062 
0063 static ssize_t dev_debug_store(struct device *cd, struct device_attribute *attr,
0064               const char *buf, size_t len)
0065 {
0066     struct video_device *vdev = to_video_device(cd);
0067     int res = 0;
0068     u16 value;
0069 
0070     res = kstrtou16(buf, 0, &value);
0071     if (res)
0072         return res;
0073 
0074     vdev->dev_debug = value;
0075     return len;
0076 }
0077 static DEVICE_ATTR_RW(dev_debug);
0078 
0079 static ssize_t name_show(struct device *cd,
0080              struct device_attribute *attr, char *buf)
0081 {
0082     struct video_device *vdev = to_video_device(cd);
0083 
0084     return sprintf(buf, "%.*s\n", (int)sizeof(vdev->name), vdev->name);
0085 }
0086 static DEVICE_ATTR_RO(name);
0087 
0088 static struct attribute *video_device_attrs[] = {
0089     &dev_attr_name.attr,
0090     &dev_attr_dev_debug.attr,
0091     &dev_attr_index.attr,
0092     NULL,
0093 };
0094 ATTRIBUTE_GROUPS(video_device);
0095 
0096 /*
0097  *  Active devices
0098  */
0099 static struct video_device *video_devices[VIDEO_NUM_DEVICES];
0100 static DEFINE_MUTEX(videodev_lock);
0101 static DECLARE_BITMAP(devnode_nums[VFL_TYPE_MAX], VIDEO_NUM_DEVICES);
0102 
0103 /* Device node utility functions */
0104 
0105 /* Note: these utility functions all assume that vfl_type is in the range
0106    [0, VFL_TYPE_MAX-1]. */
0107 
0108 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
0109 /* Return the bitmap corresponding to vfl_type. */
0110 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
0111 {
0112     /* Any types not assigned to fixed minor ranges must be mapped to
0113        one single bitmap for the purposes of finding a free node number
0114        since all those unassigned types use the same minor range. */
0115     int idx = (vfl_type > VFL_TYPE_RADIO) ? VFL_TYPE_MAX - 1 : vfl_type;
0116 
0117     return devnode_nums[idx];
0118 }
0119 #else
0120 /* Return the bitmap corresponding to vfl_type. */
0121 static inline unsigned long *devnode_bits(enum vfl_devnode_type vfl_type)
0122 {
0123     return devnode_nums[vfl_type];
0124 }
0125 #endif
0126 
0127 /* Mark device node number vdev->num as used */
0128 static inline void devnode_set(struct video_device *vdev)
0129 {
0130     set_bit(vdev->num, devnode_bits(vdev->vfl_type));
0131 }
0132 
0133 /* Mark device node number vdev->num as unused */
0134 static inline void devnode_clear(struct video_device *vdev)
0135 {
0136     clear_bit(vdev->num, devnode_bits(vdev->vfl_type));
0137 }
0138 
0139 /* Try to find a free device node number in the range [from, to> */
0140 static inline int devnode_find(struct video_device *vdev, int from, int to)
0141 {
0142     return find_next_zero_bit(devnode_bits(vdev->vfl_type), to, from);
0143 }
0144 
0145 struct video_device *video_device_alloc(void)
0146 {
0147     return kzalloc(sizeof(struct video_device), GFP_KERNEL);
0148 }
0149 EXPORT_SYMBOL(video_device_alloc);
0150 
0151 void video_device_release(struct video_device *vdev)
0152 {
0153     kfree(vdev);
0154 }
0155 EXPORT_SYMBOL(video_device_release);
0156 
0157 void video_device_release_empty(struct video_device *vdev)
0158 {
0159     /* Do nothing */
0160     /* Only valid when the video_device struct is a static. */
0161 }
0162 EXPORT_SYMBOL(video_device_release_empty);
0163 
0164 static inline void video_get(struct video_device *vdev)
0165 {
0166     get_device(&vdev->dev);
0167 }
0168 
0169 static inline void video_put(struct video_device *vdev)
0170 {
0171     put_device(&vdev->dev);
0172 }
0173 
0174 /* Called when the last user of the video device exits. */
0175 static void v4l2_device_release(struct device *cd)
0176 {
0177     struct video_device *vdev = to_video_device(cd);
0178     struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
0179 
0180     mutex_lock(&videodev_lock);
0181     if (WARN_ON(video_devices[vdev->minor] != vdev)) {
0182         /* should not happen */
0183         mutex_unlock(&videodev_lock);
0184         return;
0185     }
0186 
0187     /* Free up this device for reuse */
0188     video_devices[vdev->minor] = NULL;
0189 
0190     /* Delete the cdev on this minor as well */
0191     cdev_del(vdev->cdev);
0192     /* Just in case some driver tries to access this from
0193        the release() callback. */
0194     vdev->cdev = NULL;
0195 
0196     /* Mark device node number as free */
0197     devnode_clear(vdev);
0198 
0199     mutex_unlock(&videodev_lock);
0200 
0201 #if defined(CONFIG_MEDIA_CONTROLLER)
0202     if (v4l2_dev->mdev && vdev->vfl_dir != VFL_DIR_M2M) {
0203         /* Remove interfaces and interface links */
0204         media_devnode_remove(vdev->intf_devnode);
0205         if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
0206             media_device_unregister_entity(&vdev->entity);
0207     }
0208 #endif
0209 
0210     /* Do not call v4l2_device_put if there is no release callback set.
0211      * Drivers that have no v4l2_device release callback might free the
0212      * v4l2_dev instance in the video_device release callback below, so we
0213      * must perform this check here.
0214      *
0215      * TODO: In the long run all drivers that use v4l2_device should use the
0216      * v4l2_device release callback. This check will then be unnecessary.
0217      */
0218     if (v4l2_dev->release == NULL)
0219         v4l2_dev = NULL;
0220 
0221     /* Release video_device and perform other
0222        cleanups as needed. */
0223     vdev->release(vdev);
0224 
0225     /* Decrease v4l2_device refcount */
0226     if (v4l2_dev)
0227         v4l2_device_put(v4l2_dev);
0228 }
0229 
0230 static struct class video_class = {
0231     .name = VIDEO_NAME,
0232     .dev_groups = video_device_groups,
0233 };
0234 
0235 struct video_device *video_devdata(struct file *file)
0236 {
0237     return video_devices[iminor(file_inode(file))];
0238 }
0239 EXPORT_SYMBOL(video_devdata);
0240 
0241 
0242 /* Priority handling */
0243 
0244 static inline bool prio_is_valid(enum v4l2_priority prio)
0245 {
0246     return prio == V4L2_PRIORITY_BACKGROUND ||
0247            prio == V4L2_PRIORITY_INTERACTIVE ||
0248            prio == V4L2_PRIORITY_RECORD;
0249 }
0250 
0251 void v4l2_prio_init(struct v4l2_prio_state *global)
0252 {
0253     memset(global, 0, sizeof(*global));
0254 }
0255 EXPORT_SYMBOL(v4l2_prio_init);
0256 
0257 int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
0258              enum v4l2_priority new)
0259 {
0260     if (!prio_is_valid(new))
0261         return -EINVAL;
0262     if (*local == new)
0263         return 0;
0264 
0265     atomic_inc(&global->prios[new]);
0266     if (prio_is_valid(*local))
0267         atomic_dec(&global->prios[*local]);
0268     *local = new;
0269     return 0;
0270 }
0271 EXPORT_SYMBOL(v4l2_prio_change);
0272 
0273 void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local)
0274 {
0275     v4l2_prio_change(global, local, V4L2_PRIORITY_DEFAULT);
0276 }
0277 EXPORT_SYMBOL(v4l2_prio_open);
0278 
0279 void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local)
0280 {
0281     if (prio_is_valid(local))
0282         atomic_dec(&global->prios[local]);
0283 }
0284 EXPORT_SYMBOL(v4l2_prio_close);
0285 
0286 enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global)
0287 {
0288     if (atomic_read(&global->prios[V4L2_PRIORITY_RECORD]) > 0)
0289         return V4L2_PRIORITY_RECORD;
0290     if (atomic_read(&global->prios[V4L2_PRIORITY_INTERACTIVE]) > 0)
0291         return V4L2_PRIORITY_INTERACTIVE;
0292     if (atomic_read(&global->prios[V4L2_PRIORITY_BACKGROUND]) > 0)
0293         return V4L2_PRIORITY_BACKGROUND;
0294     return V4L2_PRIORITY_UNSET;
0295 }
0296 EXPORT_SYMBOL(v4l2_prio_max);
0297 
0298 int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local)
0299 {
0300     return (local < v4l2_prio_max(global)) ? -EBUSY : 0;
0301 }
0302 EXPORT_SYMBOL(v4l2_prio_check);
0303 
0304 
0305 static ssize_t v4l2_read(struct file *filp, char __user *buf,
0306         size_t sz, loff_t *off)
0307 {
0308     struct video_device *vdev = video_devdata(filp);
0309     int ret = -ENODEV;
0310 
0311     if (!vdev->fops->read)
0312         return -EINVAL;
0313     if (video_is_registered(vdev))
0314         ret = vdev->fops->read(filp, buf, sz, off);
0315     if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
0316         (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
0317         dprintk("%s: read: %zd (%d)\n",
0318             video_device_node_name(vdev), sz, ret);
0319     return ret;
0320 }
0321 
0322 static ssize_t v4l2_write(struct file *filp, const char __user *buf,
0323         size_t sz, loff_t *off)
0324 {
0325     struct video_device *vdev = video_devdata(filp);
0326     int ret = -ENODEV;
0327 
0328     if (!vdev->fops->write)
0329         return -EINVAL;
0330     if (video_is_registered(vdev))
0331         ret = vdev->fops->write(filp, buf, sz, off);
0332     if ((vdev->dev_debug & V4L2_DEV_DEBUG_FOP) &&
0333         (vdev->dev_debug & V4L2_DEV_DEBUG_STREAMING))
0334         dprintk("%s: write: %zd (%d)\n",
0335             video_device_node_name(vdev), sz, ret);
0336     return ret;
0337 }
0338 
0339 static __poll_t v4l2_poll(struct file *filp, struct poll_table_struct *poll)
0340 {
0341     struct video_device *vdev = video_devdata(filp);
0342     __poll_t res = EPOLLERR | EPOLLHUP | EPOLLPRI;
0343 
0344     if (video_is_registered(vdev)) {
0345         if (!vdev->fops->poll)
0346             res = DEFAULT_POLLMASK;
0347         else
0348             res = vdev->fops->poll(filp, poll);
0349     }
0350     if (vdev->dev_debug & V4L2_DEV_DEBUG_POLL)
0351         dprintk("%s: poll: %08x %08x\n",
0352             video_device_node_name(vdev), res,
0353             poll_requested_events(poll));
0354     return res;
0355 }
0356 
0357 static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0358 {
0359     struct video_device *vdev = video_devdata(filp);
0360     int ret = -ENODEV;
0361 
0362     if (vdev->fops->unlocked_ioctl) {
0363         if (video_is_registered(vdev))
0364             ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
0365     } else
0366         ret = -ENOTTY;
0367 
0368     return ret;
0369 }
0370 
0371 #ifdef CONFIG_MMU
0372 #define v4l2_get_unmapped_area NULL
0373 #else
0374 static unsigned long v4l2_get_unmapped_area(struct file *filp,
0375         unsigned long addr, unsigned long len, unsigned long pgoff,
0376         unsigned long flags)
0377 {
0378     struct video_device *vdev = video_devdata(filp);
0379     int ret;
0380 
0381     if (!vdev->fops->get_unmapped_area)
0382         return -ENOSYS;
0383     if (!video_is_registered(vdev))
0384         return -ENODEV;
0385     ret = vdev->fops->get_unmapped_area(filp, addr, len, pgoff, flags);
0386     if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
0387         dprintk("%s: get_unmapped_area (%d)\n",
0388             video_device_node_name(vdev), ret);
0389     return ret;
0390 }
0391 #endif
0392 
0393 static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
0394 {
0395     struct video_device *vdev = video_devdata(filp);
0396     int ret = -ENODEV;
0397 
0398     if (!vdev->fops->mmap)
0399         return -ENODEV;
0400     if (video_is_registered(vdev))
0401         ret = vdev->fops->mmap(filp, vm);
0402     if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
0403         dprintk("%s: mmap (%d)\n",
0404             video_device_node_name(vdev), ret);
0405     return ret;
0406 }
0407 
0408 /* Override for the open function */
0409 static int v4l2_open(struct inode *inode, struct file *filp)
0410 {
0411     struct video_device *vdev;
0412     int ret = 0;
0413 
0414     /* Check if the video device is available */
0415     mutex_lock(&videodev_lock);
0416     vdev = video_devdata(filp);
0417     /* return ENODEV if the video device has already been removed. */
0418     if (vdev == NULL || !video_is_registered(vdev)) {
0419         mutex_unlock(&videodev_lock);
0420         return -ENODEV;
0421     }
0422     /* and increase the device refcount */
0423     video_get(vdev);
0424     mutex_unlock(&videodev_lock);
0425     if (vdev->fops->open) {
0426         if (video_is_registered(vdev))
0427             ret = vdev->fops->open(filp);
0428         else
0429             ret = -ENODEV;
0430     }
0431 
0432     if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
0433         dprintk("%s: open (%d)\n",
0434             video_device_node_name(vdev), ret);
0435     /* decrease the refcount in case of an error */
0436     if (ret)
0437         video_put(vdev);
0438     return ret;
0439 }
0440 
0441 /* Override for the release function */
0442 static int v4l2_release(struct inode *inode, struct file *filp)
0443 {
0444     struct video_device *vdev = video_devdata(filp);
0445     int ret = 0;
0446 
0447     /*
0448      * We need to serialize the release() with queueing new requests.
0449      * The release() may trigger the cancellation of a streaming
0450      * operation, and that should not be mixed with queueing a new
0451      * request at the same time.
0452      */
0453     if (vdev->fops->release) {
0454         if (v4l2_device_supports_requests(vdev->v4l2_dev)) {
0455             mutex_lock(&vdev->v4l2_dev->mdev->req_queue_mutex);
0456             ret = vdev->fops->release(filp);
0457             mutex_unlock(&vdev->v4l2_dev->mdev->req_queue_mutex);
0458         } else {
0459             ret = vdev->fops->release(filp);
0460         }
0461     }
0462 
0463     if (vdev->dev_debug & V4L2_DEV_DEBUG_FOP)
0464         dprintk("%s: release\n",
0465             video_device_node_name(vdev));
0466 
0467     /* decrease the refcount unconditionally since the release()
0468        return value is ignored. */
0469     video_put(vdev);
0470     return ret;
0471 }
0472 
0473 static const struct file_operations v4l2_fops = {
0474     .owner = THIS_MODULE,
0475     .read = v4l2_read,
0476     .write = v4l2_write,
0477     .open = v4l2_open,
0478     .get_unmapped_area = v4l2_get_unmapped_area,
0479     .mmap = v4l2_mmap,
0480     .unlocked_ioctl = v4l2_ioctl,
0481 #ifdef CONFIG_COMPAT
0482     .compat_ioctl = v4l2_compat_ioctl32,
0483 #endif
0484     .release = v4l2_release,
0485     .poll = v4l2_poll,
0486     .llseek = no_llseek,
0487 };
0488 
0489 /**
0490  * get_index - assign stream index number based on v4l2_dev
0491  * @vdev: video_device to assign index number to, vdev->v4l2_dev should be assigned
0492  *
0493  * Note that when this is called the new device has not yet been registered
0494  * in the video_device array, but it was able to obtain a minor number.
0495  *
0496  * This means that we can always obtain a free stream index number since
0497  * the worst case scenario is that there are VIDEO_NUM_DEVICES - 1 slots in
0498  * use of the video_device array.
0499  *
0500  * Returns a free index number.
0501  */
0502 static int get_index(struct video_device *vdev)
0503 {
0504     /* This can be static since this function is called with the global
0505        videodev_lock held. */
0506     static DECLARE_BITMAP(used, VIDEO_NUM_DEVICES);
0507     int i;
0508 
0509     bitmap_zero(used, VIDEO_NUM_DEVICES);
0510 
0511     for (i = 0; i < VIDEO_NUM_DEVICES; i++) {
0512         if (video_devices[i] != NULL &&
0513             video_devices[i]->v4l2_dev == vdev->v4l2_dev) {
0514             __set_bit(video_devices[i]->index, used);
0515         }
0516     }
0517 
0518     return find_first_zero_bit(used, VIDEO_NUM_DEVICES);
0519 }
0520 
0521 #define SET_VALID_IOCTL(ops, cmd, op) \
0522     do { if ((ops)->op) __set_bit(_IOC_NR(cmd), valid_ioctls); } while (0)
0523 
0524 /* This determines which ioctls are actually implemented in the driver.
0525    It's a one-time thing which simplifies video_ioctl2 as it can just do
0526    a bit test.
0527 
0528    Note that drivers can override this by setting bits to 1 in
0529    vdev->valid_ioctls. If an ioctl is marked as 1 when this function is
0530    called, then that ioctl will actually be marked as unimplemented.
0531 
0532    It does that by first setting up the local valid_ioctls bitmap, and
0533    at the end do a:
0534 
0535    vdev->valid_ioctls = valid_ioctls & ~(vdev->valid_ioctls)
0536  */
0537 static void determine_valid_ioctls(struct video_device *vdev)
0538 {
0539     const u32 vid_caps = V4L2_CAP_VIDEO_CAPTURE |
0540                  V4L2_CAP_VIDEO_CAPTURE_MPLANE |
0541                  V4L2_CAP_VIDEO_OUTPUT |
0542                  V4L2_CAP_VIDEO_OUTPUT_MPLANE |
0543                  V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
0544     const u32 meta_caps = V4L2_CAP_META_CAPTURE |
0545                   V4L2_CAP_META_OUTPUT;
0546     DECLARE_BITMAP(valid_ioctls, BASE_VIDIOC_PRIVATE);
0547     const struct v4l2_ioctl_ops *ops = vdev->ioctl_ops;
0548     bool is_vid = vdev->vfl_type == VFL_TYPE_VIDEO &&
0549               (vdev->device_caps & vid_caps);
0550     bool is_vbi = vdev->vfl_type == VFL_TYPE_VBI;
0551     bool is_radio = vdev->vfl_type == VFL_TYPE_RADIO;
0552     bool is_sdr = vdev->vfl_type == VFL_TYPE_SDR;
0553     bool is_tch = vdev->vfl_type == VFL_TYPE_TOUCH;
0554     bool is_meta = vdev->vfl_type == VFL_TYPE_VIDEO &&
0555                (vdev->device_caps & meta_caps);
0556     bool is_rx = vdev->vfl_dir != VFL_DIR_TX;
0557     bool is_tx = vdev->vfl_dir != VFL_DIR_RX;
0558     bool is_io_mc = vdev->device_caps & V4L2_CAP_IO_MC;
0559 
0560     bitmap_zero(valid_ioctls, BASE_VIDIOC_PRIVATE);
0561 
0562     /* vfl_type and vfl_dir independent ioctls */
0563 
0564     SET_VALID_IOCTL(ops, VIDIOC_QUERYCAP, vidioc_querycap);
0565     __set_bit(_IOC_NR(VIDIOC_G_PRIORITY), valid_ioctls);
0566     __set_bit(_IOC_NR(VIDIOC_S_PRIORITY), valid_ioctls);
0567 
0568     /* Note: the control handler can also be passed through the filehandle,
0569        and that can't be tested here. If the bit for these control ioctls
0570        is set, then the ioctl is valid. But if it is 0, then it can still
0571        be valid if the filehandle passed the control handler. */
0572     if (vdev->ctrl_handler || ops->vidioc_queryctrl)
0573         __set_bit(_IOC_NR(VIDIOC_QUERYCTRL), valid_ioctls);
0574     if (vdev->ctrl_handler || ops->vidioc_query_ext_ctrl)
0575         __set_bit(_IOC_NR(VIDIOC_QUERY_EXT_CTRL), valid_ioctls);
0576     if (vdev->ctrl_handler || ops->vidioc_g_ctrl || ops->vidioc_g_ext_ctrls)
0577         __set_bit(_IOC_NR(VIDIOC_G_CTRL), valid_ioctls);
0578     if (vdev->ctrl_handler || ops->vidioc_s_ctrl || ops->vidioc_s_ext_ctrls)
0579         __set_bit(_IOC_NR(VIDIOC_S_CTRL), valid_ioctls);
0580     if (vdev->ctrl_handler || ops->vidioc_g_ext_ctrls)
0581         __set_bit(_IOC_NR(VIDIOC_G_EXT_CTRLS), valid_ioctls);
0582     if (vdev->ctrl_handler || ops->vidioc_s_ext_ctrls)
0583         __set_bit(_IOC_NR(VIDIOC_S_EXT_CTRLS), valid_ioctls);
0584     if (vdev->ctrl_handler || ops->vidioc_try_ext_ctrls)
0585         __set_bit(_IOC_NR(VIDIOC_TRY_EXT_CTRLS), valid_ioctls);
0586     if (vdev->ctrl_handler || ops->vidioc_querymenu)
0587         __set_bit(_IOC_NR(VIDIOC_QUERYMENU), valid_ioctls);
0588     if (!is_tch) {
0589         SET_VALID_IOCTL(ops, VIDIOC_G_FREQUENCY, vidioc_g_frequency);
0590         SET_VALID_IOCTL(ops, VIDIOC_S_FREQUENCY, vidioc_s_frequency);
0591     }
0592     SET_VALID_IOCTL(ops, VIDIOC_LOG_STATUS, vidioc_log_status);
0593 #ifdef CONFIG_VIDEO_ADV_DEBUG
0594     __set_bit(_IOC_NR(VIDIOC_DBG_G_CHIP_INFO), valid_ioctls);
0595     __set_bit(_IOC_NR(VIDIOC_DBG_G_REGISTER), valid_ioctls);
0596     __set_bit(_IOC_NR(VIDIOC_DBG_S_REGISTER), valid_ioctls);
0597 #endif
0598     /* yes, really vidioc_subscribe_event */
0599     SET_VALID_IOCTL(ops, VIDIOC_DQEVENT, vidioc_subscribe_event);
0600     SET_VALID_IOCTL(ops, VIDIOC_SUBSCRIBE_EVENT, vidioc_subscribe_event);
0601     SET_VALID_IOCTL(ops, VIDIOC_UNSUBSCRIBE_EVENT, vidioc_unsubscribe_event);
0602     if (ops->vidioc_enum_freq_bands || ops->vidioc_g_tuner || ops->vidioc_g_modulator)
0603         __set_bit(_IOC_NR(VIDIOC_ENUM_FREQ_BANDS), valid_ioctls);
0604 
0605     if (is_vid) {
0606         /* video specific ioctls */
0607         if ((is_rx && (ops->vidioc_enum_fmt_vid_cap ||
0608                    ops->vidioc_enum_fmt_vid_overlay)) ||
0609             (is_tx && ops->vidioc_enum_fmt_vid_out))
0610             __set_bit(_IOC_NR(VIDIOC_ENUM_FMT), valid_ioctls);
0611         if ((is_rx && (ops->vidioc_g_fmt_vid_cap ||
0612                    ops->vidioc_g_fmt_vid_cap_mplane ||
0613                    ops->vidioc_g_fmt_vid_overlay)) ||
0614             (is_tx && (ops->vidioc_g_fmt_vid_out ||
0615                    ops->vidioc_g_fmt_vid_out_mplane ||
0616                    ops->vidioc_g_fmt_vid_out_overlay)))
0617             __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
0618         if ((is_rx && (ops->vidioc_s_fmt_vid_cap ||
0619                    ops->vidioc_s_fmt_vid_cap_mplane ||
0620                    ops->vidioc_s_fmt_vid_overlay)) ||
0621             (is_tx && (ops->vidioc_s_fmt_vid_out ||
0622                    ops->vidioc_s_fmt_vid_out_mplane ||
0623                    ops->vidioc_s_fmt_vid_out_overlay)))
0624             __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
0625         if ((is_rx && (ops->vidioc_try_fmt_vid_cap ||
0626                    ops->vidioc_try_fmt_vid_cap_mplane ||
0627                    ops->vidioc_try_fmt_vid_overlay)) ||
0628             (is_tx && (ops->vidioc_try_fmt_vid_out ||
0629                    ops->vidioc_try_fmt_vid_out_mplane ||
0630                    ops->vidioc_try_fmt_vid_out_overlay)))
0631             __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
0632         SET_VALID_IOCTL(ops, VIDIOC_OVERLAY, vidioc_overlay);
0633         SET_VALID_IOCTL(ops, VIDIOC_G_FBUF, vidioc_g_fbuf);
0634         SET_VALID_IOCTL(ops, VIDIOC_S_FBUF, vidioc_s_fbuf);
0635         SET_VALID_IOCTL(ops, VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp);
0636         SET_VALID_IOCTL(ops, VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp);
0637         SET_VALID_IOCTL(ops, VIDIOC_G_ENC_INDEX, vidioc_g_enc_index);
0638         SET_VALID_IOCTL(ops, VIDIOC_ENCODER_CMD, vidioc_encoder_cmd);
0639         SET_VALID_IOCTL(ops, VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd);
0640         SET_VALID_IOCTL(ops, VIDIOC_DECODER_CMD, vidioc_decoder_cmd);
0641         SET_VALID_IOCTL(ops, VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd);
0642         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
0643         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
0644         if (ops->vidioc_g_selection) {
0645             __set_bit(_IOC_NR(VIDIOC_G_CROP), valid_ioctls);
0646             __set_bit(_IOC_NR(VIDIOC_CROPCAP), valid_ioctls);
0647         }
0648         if (ops->vidioc_s_selection)
0649             __set_bit(_IOC_NR(VIDIOC_S_CROP), valid_ioctls);
0650         SET_VALID_IOCTL(ops, VIDIOC_G_SELECTION, vidioc_g_selection);
0651         SET_VALID_IOCTL(ops, VIDIOC_S_SELECTION, vidioc_s_selection);
0652     }
0653     if (is_meta && is_rx) {
0654         /* metadata capture specific ioctls */
0655         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_cap);
0656         SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_cap);
0657         SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_cap);
0658         SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_cap);
0659     } else if (is_meta && is_tx) {
0660         /* metadata output specific ioctls */
0661         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_meta_out);
0662         SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_meta_out);
0663         SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_meta_out);
0664         SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_meta_out);
0665     }
0666     if (is_vbi) {
0667         /* vbi specific ioctls */
0668         if ((is_rx && (ops->vidioc_g_fmt_vbi_cap ||
0669                    ops->vidioc_g_fmt_sliced_vbi_cap)) ||
0670             (is_tx && (ops->vidioc_g_fmt_vbi_out ||
0671                    ops->vidioc_g_fmt_sliced_vbi_out)))
0672             __set_bit(_IOC_NR(VIDIOC_G_FMT), valid_ioctls);
0673         if ((is_rx && (ops->vidioc_s_fmt_vbi_cap ||
0674                    ops->vidioc_s_fmt_sliced_vbi_cap)) ||
0675             (is_tx && (ops->vidioc_s_fmt_vbi_out ||
0676                    ops->vidioc_s_fmt_sliced_vbi_out)))
0677             __set_bit(_IOC_NR(VIDIOC_S_FMT), valid_ioctls);
0678         if ((is_rx && (ops->vidioc_try_fmt_vbi_cap ||
0679                    ops->vidioc_try_fmt_sliced_vbi_cap)) ||
0680             (is_tx && (ops->vidioc_try_fmt_vbi_out ||
0681                    ops->vidioc_try_fmt_sliced_vbi_out)))
0682             __set_bit(_IOC_NR(VIDIOC_TRY_FMT), valid_ioctls);
0683         SET_VALID_IOCTL(ops, VIDIOC_G_SLICED_VBI_CAP, vidioc_g_sliced_vbi_cap);
0684     } else if (is_tch) {
0685         /* touch specific ioctls */
0686         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_vid_cap);
0687         SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_vid_cap);
0688         SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_vid_cap);
0689         SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_vid_cap);
0690         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes);
0691         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals);
0692         SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
0693         SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
0694         SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
0695         SET_VALID_IOCTL(ops, VIDIOC_G_PARM, vidioc_g_parm);
0696         SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
0697     } else if (is_sdr && is_rx) {
0698         /* SDR receiver specific ioctls */
0699         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_cap);
0700         SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_cap);
0701         SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_cap);
0702         SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_cap);
0703     } else if (is_sdr && is_tx) {
0704         /* SDR transmitter specific ioctls */
0705         SET_VALID_IOCTL(ops, VIDIOC_ENUM_FMT, vidioc_enum_fmt_sdr_out);
0706         SET_VALID_IOCTL(ops, VIDIOC_G_FMT, vidioc_g_fmt_sdr_out);
0707         SET_VALID_IOCTL(ops, VIDIOC_S_FMT, vidioc_s_fmt_sdr_out);
0708         SET_VALID_IOCTL(ops, VIDIOC_TRY_FMT, vidioc_try_fmt_sdr_out);
0709     }
0710 
0711     if (is_vid || is_vbi || is_sdr || is_tch || is_meta) {
0712         /* ioctls valid for video, vbi, sdr, touch and metadata */
0713         SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
0714         SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
0715         SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
0716         SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
0717         SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
0718         SET_VALID_IOCTL(ops, VIDIOC_CREATE_BUFS, vidioc_create_bufs);
0719         SET_VALID_IOCTL(ops, VIDIOC_PREPARE_BUF, vidioc_prepare_buf);
0720         SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
0721         SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
0722     }
0723 
0724     if (is_vid || is_vbi || is_meta) {
0725         /* ioctls valid for video, vbi and metadata */
0726         if (ops->vidioc_s_std)
0727             __set_bit(_IOC_NR(VIDIOC_ENUMSTD), valid_ioctls);
0728         SET_VALID_IOCTL(ops, VIDIOC_S_STD, vidioc_s_std);
0729         SET_VALID_IOCTL(ops, VIDIOC_G_STD, vidioc_g_std);
0730         if (is_rx) {
0731             SET_VALID_IOCTL(ops, VIDIOC_QUERYSTD, vidioc_querystd);
0732             if (is_io_mc) {
0733                 __set_bit(_IOC_NR(VIDIOC_ENUMINPUT), valid_ioctls);
0734                 __set_bit(_IOC_NR(VIDIOC_G_INPUT), valid_ioctls);
0735                 __set_bit(_IOC_NR(VIDIOC_S_INPUT), valid_ioctls);
0736             } else {
0737                 SET_VALID_IOCTL(ops, VIDIOC_ENUMINPUT, vidioc_enum_input);
0738                 SET_VALID_IOCTL(ops, VIDIOC_G_INPUT, vidioc_g_input);
0739                 SET_VALID_IOCTL(ops, VIDIOC_S_INPUT, vidioc_s_input);
0740             }
0741             SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDIO, vidioc_enumaudio);
0742             SET_VALID_IOCTL(ops, VIDIOC_G_AUDIO, vidioc_g_audio);
0743             SET_VALID_IOCTL(ops, VIDIOC_S_AUDIO, vidioc_s_audio);
0744             SET_VALID_IOCTL(ops, VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings);
0745             SET_VALID_IOCTL(ops, VIDIOC_S_EDID, vidioc_s_edid);
0746         }
0747         if (is_tx) {
0748             if (is_io_mc) {
0749                 __set_bit(_IOC_NR(VIDIOC_ENUMOUTPUT), valid_ioctls);
0750                 __set_bit(_IOC_NR(VIDIOC_G_OUTPUT), valid_ioctls);
0751                 __set_bit(_IOC_NR(VIDIOC_S_OUTPUT), valid_ioctls);
0752             } else {
0753                 SET_VALID_IOCTL(ops, VIDIOC_ENUMOUTPUT, vidioc_enum_output);
0754                 SET_VALID_IOCTL(ops, VIDIOC_G_OUTPUT, vidioc_g_output);
0755                 SET_VALID_IOCTL(ops, VIDIOC_S_OUTPUT, vidioc_s_output);
0756             }
0757             SET_VALID_IOCTL(ops, VIDIOC_ENUMAUDOUT, vidioc_enumaudout);
0758             SET_VALID_IOCTL(ops, VIDIOC_G_AUDOUT, vidioc_g_audout);
0759             SET_VALID_IOCTL(ops, VIDIOC_S_AUDOUT, vidioc_s_audout);
0760         }
0761         if (ops->vidioc_g_parm || ops->vidioc_g_std)
0762             __set_bit(_IOC_NR(VIDIOC_G_PARM), valid_ioctls);
0763         SET_VALID_IOCTL(ops, VIDIOC_S_PARM, vidioc_s_parm);
0764         SET_VALID_IOCTL(ops, VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings);
0765         SET_VALID_IOCTL(ops, VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings);
0766         SET_VALID_IOCTL(ops, VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings);
0767         SET_VALID_IOCTL(ops, VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap);
0768         SET_VALID_IOCTL(ops, VIDIOC_G_EDID, vidioc_g_edid);
0769     }
0770     if (is_tx && (is_radio || is_sdr)) {
0771         /* radio transmitter only ioctls */
0772         SET_VALID_IOCTL(ops, VIDIOC_G_MODULATOR, vidioc_g_modulator);
0773         SET_VALID_IOCTL(ops, VIDIOC_S_MODULATOR, vidioc_s_modulator);
0774     }
0775     if (is_rx && !is_tch) {
0776         /* receiver only ioctls */
0777         SET_VALID_IOCTL(ops, VIDIOC_G_TUNER, vidioc_g_tuner);
0778         SET_VALID_IOCTL(ops, VIDIOC_S_TUNER, vidioc_s_tuner);
0779         SET_VALID_IOCTL(ops, VIDIOC_S_HW_FREQ_SEEK, vidioc_s_hw_freq_seek);
0780     }
0781 
0782     bitmap_andnot(vdev->valid_ioctls, valid_ioctls, vdev->valid_ioctls,
0783             BASE_VIDIOC_PRIVATE);
0784 }
0785 
0786 static int video_register_media_controller(struct video_device *vdev)
0787 {
0788 #if defined(CONFIG_MEDIA_CONTROLLER)
0789     u32 intf_type;
0790     int ret;
0791 
0792     /* Memory-to-memory devices are more complex and use
0793      * their own function to register its mc entities.
0794      */
0795     if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M)
0796         return 0;
0797 
0798     vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE;
0799     vdev->entity.function = MEDIA_ENT_F_UNKNOWN;
0800 
0801     switch (vdev->vfl_type) {
0802     case VFL_TYPE_VIDEO:
0803         intf_type = MEDIA_INTF_T_V4L_VIDEO;
0804         vdev->entity.function = MEDIA_ENT_F_IO_V4L;
0805         break;
0806     case VFL_TYPE_VBI:
0807         intf_type = MEDIA_INTF_T_V4L_VBI;
0808         vdev->entity.function = MEDIA_ENT_F_IO_VBI;
0809         break;
0810     case VFL_TYPE_SDR:
0811         intf_type = MEDIA_INTF_T_V4L_SWRADIO;
0812         vdev->entity.function = MEDIA_ENT_F_IO_SWRADIO;
0813         break;
0814     case VFL_TYPE_TOUCH:
0815         intf_type = MEDIA_INTF_T_V4L_TOUCH;
0816         vdev->entity.function = MEDIA_ENT_F_IO_V4L;
0817         break;
0818     case VFL_TYPE_RADIO:
0819         intf_type = MEDIA_INTF_T_V4L_RADIO;
0820         /*
0821          * Radio doesn't have an entity at the V4L2 side to represent
0822          * radio input or output. Instead, the audio input/output goes
0823          * via either physical wires or ALSA.
0824          */
0825         break;
0826     case VFL_TYPE_SUBDEV:
0827         intf_type = MEDIA_INTF_T_V4L_SUBDEV;
0828         /* Entity will be created via v4l2_device_register_subdev() */
0829         break;
0830     default:
0831         return 0;
0832     }
0833 
0834     if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
0835         vdev->entity.name = vdev->name;
0836 
0837         /* Needed just for backward compatibility with legacy MC API */
0838         vdev->entity.info.dev.major = VIDEO_MAJOR;
0839         vdev->entity.info.dev.minor = vdev->minor;
0840 
0841         ret = media_device_register_entity(vdev->v4l2_dev->mdev,
0842                            &vdev->entity);
0843         if (ret < 0) {
0844             pr_warn("%s: media_device_register_entity failed\n",
0845                 __func__);
0846             return ret;
0847         }
0848     }
0849 
0850     vdev->intf_devnode = media_devnode_create(vdev->v4l2_dev->mdev,
0851                           intf_type,
0852                           0, VIDEO_MAJOR,
0853                           vdev->minor);
0854     if (!vdev->intf_devnode) {
0855         media_device_unregister_entity(&vdev->entity);
0856         return -ENOMEM;
0857     }
0858 
0859     if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) {
0860         struct media_link *link;
0861 
0862         link = media_create_intf_link(&vdev->entity,
0863                           &vdev->intf_devnode->intf,
0864                           MEDIA_LNK_FL_ENABLED |
0865                           MEDIA_LNK_FL_IMMUTABLE);
0866         if (!link) {
0867             media_devnode_remove(vdev->intf_devnode);
0868             media_device_unregister_entity(&vdev->entity);
0869             return -ENOMEM;
0870         }
0871     }
0872 
0873     /* FIXME: how to create the other interface links? */
0874 
0875 #endif
0876     return 0;
0877 }
0878 
0879 int __video_register_device(struct video_device *vdev,
0880                 enum vfl_devnode_type type,
0881                 int nr, int warn_if_nr_in_use,
0882                 struct module *owner)
0883 {
0884     int i = 0;
0885     int ret;
0886     int minor_offset = 0;
0887     int minor_cnt = VIDEO_NUM_DEVICES;
0888     const char *name_base;
0889 
0890     /* A minor value of -1 marks this video device as never
0891        having been registered */
0892     vdev->minor = -1;
0893 
0894     /* the release callback MUST be present */
0895     if (WARN_ON(!vdev->release))
0896         return -EINVAL;
0897     /* the v4l2_dev pointer MUST be present */
0898     if (WARN_ON(!vdev->v4l2_dev))
0899         return -EINVAL;
0900     /* the device_caps field MUST be set for all but subdevs */
0901     if (WARN_ON(type != VFL_TYPE_SUBDEV && !vdev->device_caps))
0902         return -EINVAL;
0903 
0904     /* v4l2_fh support */
0905     spin_lock_init(&vdev->fh_lock);
0906     INIT_LIST_HEAD(&vdev->fh_list);
0907 
0908     /* Part 1: check device type */
0909     switch (type) {
0910     case VFL_TYPE_VIDEO:
0911         name_base = "video";
0912         break;
0913     case VFL_TYPE_VBI:
0914         name_base = "vbi";
0915         break;
0916     case VFL_TYPE_RADIO:
0917         name_base = "radio";
0918         break;
0919     case VFL_TYPE_SUBDEV:
0920         name_base = "v4l-subdev";
0921         break;
0922     case VFL_TYPE_SDR:
0923         /* Use device name 'swradio' because 'sdr' was already taken. */
0924         name_base = "swradio";
0925         break;
0926     case VFL_TYPE_TOUCH:
0927         name_base = "v4l-touch";
0928         break;
0929     default:
0930         pr_err("%s called with unknown type: %d\n",
0931                __func__, type);
0932         return -EINVAL;
0933     }
0934 
0935     vdev->vfl_type = type;
0936     vdev->cdev = NULL;
0937     if (vdev->dev_parent == NULL)
0938         vdev->dev_parent = vdev->v4l2_dev->dev;
0939     if (vdev->ctrl_handler == NULL)
0940         vdev->ctrl_handler = vdev->v4l2_dev->ctrl_handler;
0941     /* If the prio state pointer is NULL, then use the v4l2_device
0942        prio state. */
0943     if (vdev->prio == NULL)
0944         vdev->prio = &vdev->v4l2_dev->prio;
0945 
0946     /* Part 2: find a free minor, device node number and device index. */
0947 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
0948     /* Keep the ranges for the first four types for historical
0949      * reasons.
0950      * Newer devices (not yet in place) should use the range
0951      * of 128-191 and just pick the first free minor there
0952      * (new style). */
0953     switch (type) {
0954     case VFL_TYPE_VIDEO:
0955         minor_offset = 0;
0956         minor_cnt = 64;
0957         break;
0958     case VFL_TYPE_RADIO:
0959         minor_offset = 64;
0960         minor_cnt = 64;
0961         break;
0962     case VFL_TYPE_VBI:
0963         minor_offset = 224;
0964         minor_cnt = 32;
0965         break;
0966     default:
0967         minor_offset = 128;
0968         minor_cnt = 64;
0969         break;
0970     }
0971 #endif
0972 
0973     /* Pick a device node number */
0974     mutex_lock(&videodev_lock);
0975     nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
0976     if (nr == minor_cnt)
0977         nr = devnode_find(vdev, 0, minor_cnt);
0978     if (nr == minor_cnt) {
0979         pr_err("could not get a free device node number\n");
0980         mutex_unlock(&videodev_lock);
0981         return -ENFILE;
0982     }
0983 #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
0984     /* 1-on-1 mapping of device node number to minor number */
0985     i = nr;
0986 #else
0987     /* The device node number and minor numbers are independent, so
0988        we just find the first free minor number. */
0989     for (i = 0; i < VIDEO_NUM_DEVICES; i++)
0990         if (video_devices[i] == NULL)
0991             break;
0992     if (i == VIDEO_NUM_DEVICES) {
0993         mutex_unlock(&videodev_lock);
0994         pr_err("could not get a free minor\n");
0995         return -ENFILE;
0996     }
0997 #endif
0998     vdev->minor = i + minor_offset;
0999     vdev->num = nr;
1000 
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");
1005         return -ENFILE;
1006     }
1007     devnode_set(vdev);
1008     vdev->index = get_index(vdev);
1009     video_devices[vdev->minor] = vdev;
1010     mutex_unlock(&videodev_lock);
1011 
1012     if (vdev->ioctl_ops)
1013         determine_valid_ioctls(vdev);
1014 
1015     /* Part 3: Initialize the character device */
1016     vdev->cdev = cdev_alloc();
1017     if (vdev->cdev == NULL) {
1018         ret = -ENOMEM;
1019         goto cleanup;
1020     }
1021     vdev->cdev->ops = &v4l2_fops;
1022     vdev->cdev->owner = owner;
1023     ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
1024     if (ret < 0) {
1025         pr_err("%s: cdev_add failed\n", __func__);
1026         kfree(vdev->cdev);
1027         vdev->cdev = NULL;
1028         goto cleanup;
1029     }
1030 
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);
1037     if (ret < 0) {
1038         pr_err("%s: device_register failed\n", __func__);
1039         goto cleanup;
1040     }
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;
1044 
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));
1048 
1049     /* Increase v4l2_device refcount */
1050     v4l2_device_get(vdev->v4l2_dev);
1051 
1052     /* Part 5: Register the entity. */
1053     ret = video_register_media_controller(vdev);
1054 
1055     /* Part 6: Activate this minor. The char device can now be used. */
1056     set_bit(V4L2_FL_REGISTERED, &vdev->flags);
1057 
1058     return 0;
1059 
1060 cleanup:
1061     mutex_lock(&videodev_lock);
1062     if (vdev->cdev)
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. */
1068     vdev->minor = -1;
1069     return ret;
1070 }
1071 EXPORT_SYMBOL(__video_register_device);
1072 
1073 /**
1074  *  video_unregister_device - unregister a video4linux device
1075  *  @vdev: the device to unregister
1076  *
1077  *  This unregisters the passed device. Future open calls will
1078  *  be met with errors.
1079  */
1080 void video_unregister_device(struct video_device *vdev)
1081 {
1082     /* Check if vdev was ever registered at all */
1083     if (!vdev || !video_is_registered(vdev))
1084         return;
1085 
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.
1089      */
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);
1095 }
1096 EXPORT_SYMBOL(video_unregister_device);
1097 
1098 /*
1099  *  Initialise video for linux
1100  */
1101 static int __init videodev_init(void)
1102 {
1103     dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1104     int ret;
1105 
1106     pr_info("Linux video capture interface: v2.00\n");
1107     ret = register_chrdev_region(dev, VIDEO_NUM_DEVICES, VIDEO_NAME);
1108     if (ret < 0) {
1109         pr_warn("videodev: unable to get major %d\n",
1110                 VIDEO_MAJOR);
1111         return ret;
1112     }
1113 
1114     ret = class_register(&video_class);
1115     if (ret < 0) {
1116         unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1117         pr_warn("video_dev: class_register failed\n");
1118         return -EIO;
1119     }
1120 
1121     return 0;
1122 }
1123 
1124 static void __exit videodev_exit(void)
1125 {
1126     dev_t dev = MKDEV(VIDEO_MAJOR, 0);
1127 
1128     class_unregister(&video_class);
1129     unregister_chrdev_region(dev, VIDEO_NUM_DEVICES);
1130 }
1131 
1132 subsys_initcall(videodev_init);
1133 module_exit(videodev_exit)
1134 
1135 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@kernel.org>, Bill Dirks, Justin Schoeman, Gerd Knorr");
1136 MODULE_DESCRIPTION("Video4Linux2 core driver");
1137 MODULE_LICENSE("GPL");
1138 MODULE_ALIAS_CHARDEV_MAJOR(VIDEO_MAJOR);