]> Pileus Git - ~andy/linux/blobdiff - drivers/media/video/v4l2-dev.c
[media] v4l: Make video_device inherit from media_entity
[~andy/linux] / drivers / media / video / v4l2-dev.c
index 359e23290a7e99ae0301a6d6844a445343cb900e..e405b8094b94036730cdfcf457c2d85fa7c48229 100644 (file)
@@ -303,6 +303,9 @@ static int v4l2_mmap(struct file *filp, struct vm_area_struct *vm)
 static int v4l2_open(struct inode *inode, struct file *filp)
 {
        struct video_device *vdev;
+#if defined(CONFIG_MEDIA_CONTROLLER)
+       struct media_entity *entity = NULL;
+#endif
        int ret = 0;
 
        /* Check if the video device is available */
@@ -316,6 +319,16 @@ static int v4l2_open(struct inode *inode, struct file *filp)
        /* and increase the device refcount */
        video_get(vdev);
        mutex_unlock(&videodev_lock);
+#if defined(CONFIG_MEDIA_CONTROLLER)
+       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) {
+               entity = media_entity_get(&vdev->entity);
+               if (!entity) {
+                       ret = -EBUSY;
+                       video_put(vdev);
+                       return ret;
+               }
+       }
+#endif
        if (vdev->fops->open) {
                if (vdev->lock && mutex_lock_interruptible(vdev->lock)) {
                        ret = -ERESTARTSYS;
@@ -331,8 +344,13 @@ static int v4l2_open(struct inode *inode, struct file *filp)
 
 err:
        /* decrease the refcount in case of an error */
-       if (ret)
+       if (ret) {
+#if defined(CONFIG_MEDIA_CONTROLLER)
+               if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
+                       media_entity_put(entity);
+#endif
                video_put(vdev);
+       }
        return ret;
 }
 
@@ -349,7 +367,10 @@ static int v4l2_release(struct inode *inode, struct file *filp)
                if (vdev->lock)
                        mutex_unlock(vdev->lock);
        }
-
+#if defined(CONFIG_MEDIA_CONTROLLER)
+       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
+               media_entity_put(&vdev->entity);
+#endif
        /* decrease the refcount unconditionally since the release()
           return value is ignored. */
        video_put(vdev);
@@ -408,17 +429,22 @@ static int get_index(struct video_device *vdev)
 }
 
 /**
- *     video_register_device - register video4linux devices
+ *     __video_register_device - register video4linux devices
  *     @vdev: video device structure we want to register
  *     @type: type of device to register
  *     @nr:   which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  *             -1 == first free)
  *     @warn_if_nr_in_use: warn if the desired device node number
  *            was already in use and another number was chosen instead.
+ *     @owner: module that owns the video device node
  *
  *     The registration code assigns minor numbers and device node numbers
  *     based on the requested type and registers the new device node with
  *     the kernel.
+ *
+ *     This function assumes that struct video_device was zeroed when it
+ *     was allocated and does not contain any stale date.
+ *
  *     An error is returned if no free minor or device node number could be
  *     found, or if the registration of the device node failed.
  *
@@ -431,16 +457,17 @@ static int get_index(struct video_device *vdev)
  *     %VFL_TYPE_VBI - Vertical blank data (undecoded)
  *
  *     %VFL_TYPE_RADIO - A radio card
+ *
+ *     %VFL_TYPE_SUBDEV - A subdevice
  */
-static int __video_register_device(struct video_device *vdev, int type, int nr,
-               int warn_if_nr_in_use)
+int __video_register_device(struct video_device *vdev, int type, int nr,
+               int warn_if_nr_in_use, struct module *owner)
 {
        int i = 0;
        int ret;
        int minor_offset = 0;
        int minor_cnt = VIDEO_NUM_DEVICES;
        const char *name_base;
-       void *priv = vdev->dev.p;
 
        /* A minor value of -1 marks this video device as never
           having been registered */
@@ -466,6 +493,9 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
        case VFL_TYPE_RADIO:
                name_base = "radio";
                break;
+       case VFL_TYPE_SUBDEV:
+               name_base = "v4l-subdev";
+               break;
        default:
                printk(KERN_ERR "%s called with unknown type: %d\n",
                       __func__, type);
@@ -549,7 +579,7 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
                goto cleanup;
        }
        vdev->cdev->ops = &v4l2_fops;
-       vdev->cdev->owner = vdev->fops->owner;
+       vdev->cdev->owner = owner;
        ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
        if (ret < 0) {
                printk(KERN_ERR "%s: cdev_add failed\n", __func__);
@@ -559,10 +589,6 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
        }
 
        /* Part 4: register the device with sysfs */
-       memset(&vdev->dev, 0, sizeof(vdev->dev));
-       /* The memset above cleared the device's device_private, so
-          put back the copy we made earlier. */
-       vdev->dev.p = priv;
        vdev->dev.class = &video_class;
        vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
        if (vdev->parent)
@@ -580,12 +606,27 @@ static int __video_register_device(struct video_device *vdev, int type, int nr,
        if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
                printk(KERN_WARNING "%s: requested %s%d, got %s\n", __func__,
                        name_base, nr, video_device_node_name(vdev));
-
-       /* Part 5: Activate this minor. The char device can now be used. */
+#if defined(CONFIG_MEDIA_CONTROLLER)
+       /* Part 5: Register the entity. */
+       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev) {
+               vdev->entity.type = MEDIA_ENT_T_DEVNODE_V4L;
+               vdev->entity.name = vdev->name;
+               vdev->entity.v4l.major = VIDEO_MAJOR;
+               vdev->entity.v4l.minor = vdev->minor;
+               ret = media_device_register_entity(vdev->v4l2_dev->mdev,
+                       &vdev->entity);
+               if (ret < 0)
+                       printk(KERN_WARNING
+                              "%s: media_device_register_entity failed\n",
+                              __func__);
+       }
+#endif
+       /* Part 6: Activate this minor. The char device can now be used. */
        set_bit(V4L2_FL_REGISTERED, &vdev->flags);
        mutex_lock(&videodev_lock);
        video_device[vdev->minor] = vdev;
        mutex_unlock(&videodev_lock);
+
        return 0;
 
 cleanup:
@@ -598,18 +639,7 @@ cleanup:
        vdev->minor = -1;
        return ret;
 }
-
-int video_register_device(struct video_device *vdev, int type, int nr)
-{
-       return __video_register_device(vdev, type, nr, 1);
-}
-EXPORT_SYMBOL(video_register_device);
-
-int video_register_device_no_warn(struct video_device *vdev, int type, int nr)
-{
-       return __video_register_device(vdev, type, nr, 0);
-}
-EXPORT_SYMBOL(video_register_device_no_warn);
+EXPORT_SYMBOL(__video_register_device);
 
 /**
  *     video_unregister_device - unregister a video4linux device
@@ -624,6 +654,11 @@ void video_unregister_device(struct video_device *vdev)
        if (!vdev || !video_is_registered(vdev))
                return;
 
+#if defined(CONFIG_MEDIA_CONTROLLER)
+       if (vdev->v4l2_dev && vdev->v4l2_dev->mdev)
+               media_device_unregister_entity(&vdev->entity);
+#endif
+
        mutex_lock(&videodev_lock);
        /* This must be in a critical section to prevent a race with v4l2_open.
         * Once this bit has been cleared video_get may never be called again.