LCOV - code coverage report
Current view: top level - drivers/media/v4l2-core - v4l2-dev.c (source / functions) Hit Total Coverage
Test: Real Lines: 232 441 52.6 %
Date: 2020-10-17 15:46:16 Functions: 0 29 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14