hwmon: (corsair-psu) Fix probe when built-in

Message ID 20231207210723.222552-1-W_Armin@gmx.de
State New
Headers
Series hwmon: (corsair-psu) Fix probe when built-in |

Commit Message

Armin Wolf Dec. 7, 2023, 9:07 p.m. UTC
  It seems that when the driver is built-in, the HID bus is
initialized after the driver is loaded, which whould cause
module_hid_driver() to fail.
Fix this by registering the driver after the HID bus using
late_initcall() in accordance with other hwmon HID drivers.

Compile-tested only.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
 drivers/hwmon/corsair-psu.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

--
2.39.2
  

Comments

Wilken Gottwalt Dec. 8, 2023, 6:48 a.m. UTC | #1
On Thu,  7 Dec 2023 22:07:23 +0100
Armin Wolf <W_Armin@gmx.de> wrote:

> It seems that when the driver is built-in, the HID bus is
> initialized after the driver is loaded, which whould cause
> module_hid_driver() to fail.
> Fix this by registering the driver after the HID bus using
> late_initcall() in accordance with other hwmon HID drivers.
> 
> Compile-tested only.

So you did not test this? Well, I did.

[    2.225831] Driver 'corsair-psu' was unable to register with bus_type 'hid' because the bus was not initialized.
[    2.225835] amd_pstate: driver load is disabled, boot with specific mode to enable this
[    2.226363] ledtrig-cpu: registered to indicate activity on CPUs
[    2.226679] hid: raw HID events driver (C) Jiri Kosina

You are right, it is a timing issue and this can actually happen. I'm fine with
the fix.

Though, this could even be a bigger issue. There are currently 104 HID drivers
using the module_hid_driver macro. Maybe it would be a better idea to change the
module_hid_driver macro to use the lateinit calls instead of the plain init/exit
calls.

greetings,
Will

> Signed-off-by: Armin Wolf <W_Armin@gmx.de>
> ---
>  drivers/hwmon/corsair-psu.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index 904890598c11..2c7c92272fe3 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -899,7 +899,23 @@ static struct hid_driver corsairpsu_driver = {
>  	.reset_resume	= corsairpsu_resume,
>  #endif
>  };
> -module_hid_driver(corsairpsu_driver);
> +
> +static int __init corsair_init(void)
> +{
> +	return hid_register_driver(&corsairpsu_driver);
> +}
> +
> +static void __exit corsair_exit(void)
> +{
> +	hid_unregister_driver(&corsairpsu_driver);
> +}
> +
> +/*
> + * With module_init() the driver would load before the HID bus when
> + * built-in, so use late_initcall() instead.
> + */
> +late_initcall(corsair_init);
> +module_exit(corsair_exit);
> 
>  MODULE_LICENSE("GPL");
>  MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
> --
> 2.39.2
>
  
Guenter Roeck Dec. 8, 2023, 6:38 p.m. UTC | #2
On Thu, Dec 07, 2023 at 10:07:23PM +0100, Armin Wolf wrote:
> It seems that when the driver is built-in, the HID bus is
> initialized after the driver is loaded, which whould cause
> module_hid_driver() to fail.
> Fix this by registering the driver after the HID bus using
> late_initcall() in accordance with other hwmon HID drivers.
> 
> Compile-tested only.
> 
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>

Applied. I dropped the "Compile-tested only" comment and added a note
that the patch was tested but the tested did not provide a Tested-by: tag.

Thanks,
Guenter
  
Wilken Gottwalt Dec. 8, 2023, 6:44 p.m. UTC | #3
On Thu,  7 Dec 2023 22:07:23 +0100
Armin Wolf <W_Armin@gmx.de> wrote:

Just forgot. Did both, works just fine. Is it okay that way? This is my first
time.

Tested-by: Wilken Gottwalt <wilken.gottwalt@posteo.net>
Reviewed-by: Wilken Gottwalt <wilken.gottwalt@posteo.net>

> It seems that when the driver is built-in, the HID bus is
> initialized after the driver is loaded, which whould cause
> module_hid_driver() to fail.
> Fix this by registering the driver after the HID bus using
> late_initcall() in accordance with other hwmon HID drivers.
> 
> Compile-tested only.
> 
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>
> ---
>  drivers/hwmon/corsair-psu.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index 904890598c11..2c7c92272fe3 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -899,7 +899,23 @@ static struct hid_driver corsairpsu_driver = {
>  	.reset_resume	= corsairpsu_resume,
>  #endif
>  };
> -module_hid_driver(corsairpsu_driver);
> +
> +static int __init corsair_init(void)
> +{
> +	return hid_register_driver(&corsairpsu_driver);
> +}
> +
> +static void __exit corsair_exit(void)
> +{
> +	hid_unregister_driver(&corsairpsu_driver);
> +}
> +
> +/*
> + * With module_init() the driver would load before the HID bus when
> + * built-in, so use late_initcall() instead.
> + */
> +late_initcall(corsair_init);
> +module_exit(corsair_exit);
> 
>  MODULE_LICENSE("GPL");
>  MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
> --
> 2.39.2
>
  
Guenter Roeck Dec. 8, 2023, 7:12 p.m. UTC | #4
On 12/8/23 10:44, Wilken Gottwalt wrote:
> On Thu,  7 Dec 2023 22:07:23 +0100
> Armin Wolf <W_Armin@gmx.de> wrote:
> 
> Just forgot. Did both, works just fine. Is it okay that way? This is my first
> time.
> 
> Tested-by: Wilken Gottwalt <wilken.gottwalt@posteo.net>
> Reviewed-by: Wilken Gottwalt <wilken.gottwalt@posteo.net>
> 

Yes, it is perfect, just a bit late. I sent the pull request pretty much in parallel
(I didn't want to wait to make sure the patch makes it into v6.7-rc5).

Thanks,
Guenter
  

Patch

diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
index 904890598c11..2c7c92272fe3 100644
--- a/drivers/hwmon/corsair-psu.c
+++ b/drivers/hwmon/corsair-psu.c
@@ -899,7 +899,23 @@  static struct hid_driver corsairpsu_driver = {
 	.reset_resume	= corsairpsu_resume,
 #endif
 };
-module_hid_driver(corsairpsu_driver);
+
+static int __init corsair_init(void)
+{
+	return hid_register_driver(&corsairpsu_driver);
+}
+
+static void __exit corsair_exit(void)
+{
+	hid_unregister_driver(&corsairpsu_driver);
+}
+
+/*
+ * With module_init() the driver would load before the HID bus when
+ * built-in, so use late_initcall() instead.
+ */
+late_initcall(corsair_init);
+module_exit(corsair_exit);

 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");