On Thu, Sep 24, 2015 at 07:02:36PM +0200, Thierry Reding wrote:
From: Thierry Reding treding@nvidia.com
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.
Cc: Greg Kroah-Hartman gregkh@linuxfoundation.org Signed-off-by: Thierry Reding treding@nvidia.com
Documentation/driver-model/platform.txt | 11 ++++++ drivers/base/platform.c | 60 +++++++++++++++++++++++++++++++++ include/linux/platform_device.h | 5 +++ 3 files changed, 76 insertions(+)
diff --git a/Documentation/driver-model/platform.txt b/Documentation/driver-model/platform.txt index 07795ec51cde..e80468738ba9 100644 --- a/Documentation/driver-model/platform.txt +++ b/Documentation/driver-model/platform.txt @@ -63,6 +63,17 @@ 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);
- 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.
Device Enumeration
diff --git a/drivers/base/platform.c b/drivers/base/platform.c index f80aaaf9f610..b7d7987fda97 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -711,6 +711,66 @@ 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 + * + * 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) +{ + 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]);
I notice that this is actually doing the wrong thing because the platform drivers will end up with their .owner field set to NULL because this file is always built-in. I've fixed it up by passing in a struct module *owner into __platform_register_drivers() and pass it on to the __platform_driver_register() function instead.
Thierry