upstream: [media] v4l: Only get module if it's different than the driver for v4l2_dev
authorSakari Ailus <sakari.ailus@linux.intel.com>
Thu, 12 Dec 2013 12:36:46 +0000 (09:36 -0300)
committerChanho Park <chanho61.park@samsung.com>
Tue, 18 Nov 2014 02:55:15 +0000 (11:55 +0900)
When the sub-device is registered, increment the use count of the sub-device
owner only if it's different from the owner of the driver for the media
device. This avoids increasing the use count by the module itself and thus
making it possible to unload it when it's not in use.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
drivers/media/v4l2-core/v4l2-device.c
include/media/v4l2-subdev.h

index 2dbfebc..036136a 100644 (file)
@@ -153,7 +153,17 @@ int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
        /* Warn if we apparently re-register a subdev */
        WARN_ON(sd->v4l2_dev != NULL);
 
-       if (!try_module_get(sd->owner))
+       /*
+        * The reason to acquire the module here is to avoid unloading
+        * a module of sub-device which is registered to a media
+        * device. To make it possible to unload modules for media
+        * devices that also register sub-devices, do not
+        * try_module_get() such sub-device owners.
+        */
+       sd->owner_v4l2_dev = v4l2_dev->dev && v4l2_dev->dev->driver &&
+               sd->owner == v4l2_dev->dev->driver->owner;
+
+       if (!sd->owner_v4l2_dev && !try_module_get(sd->owner))
                return -ENODEV;
 
        sd->v4l2_dev = v4l2_dev;
@@ -187,7 +197,8 @@ error_unregister:
        if (sd->internal_ops && sd->internal_ops->unregistered)
                sd->internal_ops->unregistered(sd);
 error_module:
-       module_put(sd->owner);
+       if (!sd->owner_v4l2_dev)
+               module_put(sd->owner);
        sd->v4l2_dev = NULL;
        return err;
 }
@@ -275,6 +286,7 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
        }
 #endif
        video_unregister_device(sd->devnode);
-       module_put(sd->owner);
+       if (!sd->owner_v4l2_dev)
+               module_put(sd->owner);
 }
 EXPORT_SYMBOL_GPL(v4l2_device_unregister_subdev);
index 12ebb17..8da9502 100644 (file)
@@ -571,6 +571,7 @@ struct v4l2_subdev {
 #endif
        struct list_head list;
        struct module *owner;
+       bool owner_v4l2_dev;
        u32 flags;
        struct v4l2_device *v4l2_dev;
        const struct v4l2_subdev_ops *ops;