driver-core: platform: Provide helpers for multi-driver modules
authorThierry Reding <treding@nvidia.com>
Fri, 25 Sep 2015 15:29:04 +0000 (17:29 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 5 Oct 2015 04:02:40 +0000 (05:02 +0100)
Some modules register several sub-drivers. Provide a helper that makes
it easy to register and unregister a list of sub-drivers, as well as
unwind properly on error.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/driver-model/platform.txt
drivers/base/platform.c
include/linux/platform_device.h

index 07795ec..e456696 100644 (file)
@@ -63,6 +63,20 @@ runtime memory footprint:
        int platform_driver_probe(struct platform_driver *drv,
                          int (*probe)(struct platform_device *))
 
+Kernel modules can be composed of several platform drivers. The platform core
+provides helpers to register and unregister an array of drivers:
+
+       int __platform_register_drivers(struct platform_driver * const *drivers,
+                                     unsigned int count, struct module *owner);
+       void platform_unregister_drivers(struct platform_driver * const *drivers,
+                                        unsigned int count);
+
+If one of the drivers fails to register, all drivers registered up to that
+point will be unregistered in reverse order. Note that there is a convenience
+macro that passes THIS_MODULE as owner parameter:
+
+       #define platform_register_driver(drivers, count)
+
 
 Device Enumeration
 ~~~~~~~~~~~~~~~~~~
index 07cec9b..1dd6d3b 100644 (file)
@@ -710,6 +710,67 @@ err_out:
 }
 EXPORT_SYMBOL_GPL(__platform_create_bundle);
 
+/**
+ * __platform_register_drivers - register an array of platform drivers
+ * @drivers: an array of drivers to register
+ * @count: the number of drivers to register
+ * @owner: module owning the drivers
+ *
+ * Registers platform drivers specified by an array. On failure to register a
+ * driver, all previously registered drivers will be unregistered. Callers of
+ * this API should use platform_unregister_drivers() to unregister drivers in
+ * the reverse order.
+ *
+ * Returns: 0 on success or a negative error code on failure.
+ */
+int __platform_register_drivers(struct platform_driver * const *drivers,
+                               unsigned int count, struct module *owner)
+{
+       unsigned int i;
+       int err;
+
+       for (i = 0; i < count; i++) {
+               pr_debug("registering platform driver %ps\n", drivers[i]);
+
+               err = __platform_driver_register(drivers[i], owner);
+               if (err < 0) {
+                       pr_err("failed to register platform driver %ps: %d\n",
+                              drivers[i], err);
+                       goto error;
+               }
+       }
+
+       return 0;
+
+error:
+       while (i--) {
+               pr_debug("unregistering platform driver %ps\n", drivers[i]);
+               platform_driver_unregister(drivers[i]);
+       }
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(__platform_register_drivers);
+
+/**
+ * platform_unregister_drivers - unregister an array of platform drivers
+ * @drivers: an array of drivers to unregister
+ * @count: the number of drivers to unregister
+ *
+ * Unegisters platform drivers specified by an array. This is typically used
+ * to complement an earlier call to platform_register_drivers(). Drivers are
+ * unregistered in the reverse order in which they were registered.
+ */
+void platform_unregister_drivers(struct platform_driver * const *drivers,
+                                unsigned int count)
+{
+       while (count--) {
+               pr_debug("unregistering platform driver %ps\n", drivers[count]);
+               platform_driver_unregister(drivers[count]);
+       }
+}
+EXPORT_SYMBOL_GPL(platform_unregister_drivers);
+
 /* modalias support enables more hands-off userspace setup:
  * (a) environment variable lets new-style hotplug events work once system is
  *     fully running:  "modprobe $MODALIAS"
index bba08f4..dc777be 100644 (file)
@@ -270,6 +270,14 @@ extern struct platform_device *__platform_create_bundle(
        struct resource *res, unsigned int n_res,
        const void *data, size_t size, struct module *module);
 
+int __platform_register_drivers(struct platform_driver * const *drivers,
+                               unsigned int count, struct module *owner);
+void platform_unregister_drivers(struct platform_driver * const *drivers,
+                                unsigned int count);
+
+#define platform_register_drivers(drivers, count) \
+       __platform_register_drivers(drivers, count, THIS_MODULE)
+
 /* early platform driver interface */
 struct early_platform_driver {
        const char *class_str;