OSDN Git Service

media: v4l2-subdev: add release() internal op
authorHans Verkuil <hverkuil-cisco@xs4all.nl>
Thu, 21 Feb 2019 13:37:42 +0000 (08:37 -0500)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Tue, 19 Mar 2019 17:24:28 +0000 (13:24 -0400)
If the subdevice created a device node, then the v4l2_subdev cannot
be freed until the last user of the device node closes it.

This means that we need a release() callback in v4l2_subdev_internal_ops
that is called from the video_device release function so the subdevice
driver can postpone freeing memory until the that callback is called.

If no video device node was created then the release callback can
be called immediately when the subdev is unregistered.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
drivers/media/v4l2-core/v4l2-device.c
include/media/v4l2-subdev.h

index e0ddb9a..7cca0de 100644 (file)
@@ -216,10 +216,18 @@ error_module:
 }
 EXPORT_SYMBOL_GPL(v4l2_device_register_subdev);
 
+static void v4l2_subdev_release(struct v4l2_subdev *sd)
+{
+       struct module *owner = !sd->owner_v4l2_dev ? sd->owner : NULL;
+
+       if (sd->internal_ops && sd->internal_ops->release)
+               sd->internal_ops->release(sd);
+       module_put(owner);
+}
+
 static void v4l2_device_release_subdev_node(struct video_device *vdev)
 {
-       struct v4l2_subdev *sd = video_get_drvdata(vdev);
-       sd->devnode = NULL;
+       v4l2_subdev_release(video_get_drvdata(vdev));
        kfree(vdev);
 }
 
@@ -318,8 +326,9 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
                media_device_unregister_entity(&sd->entity);
        }
 #endif
-       video_unregister_device(sd->devnode);
-       if (!sd->owner_v4l2_dev)
-               module_put(sd->owner);
+       if (sd->devnode)
+               video_unregister_device(sd->devnode);
+       else
+               v4l2_subdev_release(sd);
 }
 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);
index 349e1c1..e807aa9 100644 (file)
@@ -755,7 +755,17 @@ struct v4l2_subdev_ops {
  *
  * @open: called when the subdev device node is opened by an application.
  *
- * @close: called when the subdev device node is closed.
+ * @close: called when the subdev device node is closed. Please note that
+ *     it is possible for @close to be called after @unregistered!
+ *
+ * @release: called when the last user of the subdev device is gone. This
+ *     happens after the @unregistered callback and when the last open
+ *     filehandle to the v4l-subdevX device node was closed. If no device
+ *     node was created for this sub-device, then the @release callback
+ *     is called right after the @unregistered callback.
+ *     The @release callback is typically used to free the memory containing
+ *     the v4l2_subdev structure. It is almost certainly required for any
+ *     sub-device that sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag.
  *
  * .. note::
  *     Never call this from drivers, only the v4l2 framework can call
@@ -766,6 +776,7 @@ struct v4l2_subdev_internal_ops {
        void (*unregistered)(struct v4l2_subdev *sd);
        int (*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
        int (*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
+       void (*release)(struct v4l2_subdev *sd);
 };
 
 #define V4L2_SUBDEV_NAME_SIZE 32