linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique
@ 2025-06-02 18:26 Charalampos Mitrodimas
  2025-06-02 18:48 ` Bastien Nocera
  0 siblings, 1 reply; 3+ messages in thread
From: Charalampos Mitrodimas @ 2025-06-02 18:26 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman, Alan Stern
  Cc: linux-usb, linux-kernel, Charalampos Mitrodimas

When multiple Apple devices are connected concurrently, the
apple-mfi-fastcharge driver fails to probe the subsequent devices with
the following error:

    sysfs: cannot create duplicate filename '/class/power_supply/apple_mfi_fastcharge'
    apple-mfi-fastcharge 5-2.4.3.3: probe of 5-2.4.3.3 failed with error -17

This happens because the driver uses a fixed power supply name
("apple_mfi_fastcharge") for all devices, causing a sysfs name
conflict when a second device is connected.

Fix this by generating unique names using the USB bus and device
number (e.g., "apple_mfi_fastcharge_5-12"). This ensures each
connected device gets a unique power supply entry in sysfs.

The change requires storing a copy of the power_supply_desc structure
in the per-device mfi_device struct, since the name pointer needs to
remain valid for the lifetime of the power supply registration.

Fixes: 249fa8217b84 ("USB: Add driver to control USB fast charge for iOS devices")
Signed-off-by: Charalampos Mitrodimas <charmitro@posteo.net>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index ac8695195c13c8752076e4391ac81a9da3780c44..8e852f4b8262e6e8fcd33883be8c5696f19b9ee9 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -44,6 +44,7 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
 struct mfi_device {
 	struct usb_device *udev;
 	struct power_supply *battery;
+	struct power_supply_desc battery_desc;
 	int charge_type;
 };
 
@@ -178,6 +179,7 @@ static int mfi_fc_probe(struct usb_device *udev)
 {
 	struct power_supply_config battery_cfg = {};
 	struct mfi_device *mfi = NULL;
+	char *battery_name;
 	int err;
 
 	if (!mfi_fc_match(udev))
@@ -187,23 +189,38 @@ static int mfi_fc_probe(struct usb_device *udev)
 	if (!mfi)
 		return -ENOMEM;
 
+	battery_name = kasprintf(GFP_KERNEL, "apple_mfi_fastcharge_%d-%d",
+				 udev->bus->busnum, udev->devnum);
+	if (!battery_name) {
+		err = -ENOMEM;
+		goto err_free_mfi;
+	}
+
+	mfi->battery_desc = apple_mfi_fc_desc;
+	mfi->battery_desc.name = battery_name;
+
 	battery_cfg.drv_data = mfi;
 
 	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
 	mfi->battery = power_supply_register(&udev->dev,
-						&apple_mfi_fc_desc,
+						&mfi->battery_desc,
 						&battery_cfg);
 	if (IS_ERR(mfi->battery)) {
 		dev_err(&udev->dev, "Can't register battery\n");
 		err = PTR_ERR(mfi->battery);
-		kfree(mfi);
-		return err;
+		goto err_free_name;
 	}
 
 	mfi->udev = usb_get_dev(udev);
 	dev_set_drvdata(&udev->dev, mfi);
 
 	return 0;
+
+err_free_name:
+	kfree(battery_name);
+err_free_mfi:
+	kfree(mfi);
+	return err;
 }
 
 static void mfi_fc_disconnect(struct usb_device *udev)
@@ -213,6 +230,7 @@ static void mfi_fc_disconnect(struct usb_device *udev)
 	mfi = dev_get_drvdata(&udev->dev);
 	if (mfi->battery)
 		power_supply_unregister(mfi->battery);
+	kfree(mfi->battery_desc.name);
 	dev_set_drvdata(&udev->dev, NULL);
 	usb_put_dev(mfi->udev);
 	kfree(mfi);

---
base-commit: cd2e103d57e5615f9bb027d772f93b9efd567224
change-id: 20250602-apple-mfi-fastcharge-duplicate-sysfs-0ef3864b21f8

Best regards,
-- 
Charalampos Mitrodimas <charmitro@posteo.net>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique
  2025-06-02 18:26 [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique Charalampos Mitrodimas
@ 2025-06-02 18:48 ` Bastien Nocera
  2025-06-03 11:12   ` Charalampos Mitrodimas
  0 siblings, 1 reply; 3+ messages in thread
From: Bastien Nocera @ 2025-06-02 18:48 UTC (permalink / raw)
  To: Charalampos Mitrodimas, Greg Kroah-Hartman, Alan Stern
  Cc: linux-usb, linux-kernel

On Mon, 2025-06-02 at 18:26 +0000, Charalampos Mitrodimas wrote:
> When multiple Apple devices are connected concurrently, the
> apple-mfi-fastcharge driver fails to probe the subsequent devices
> with
> the following error:
> 
>     sysfs: cannot create duplicate filename
> '/class/power_supply/apple_mfi_fastcharge'
>     apple-mfi-fastcharge 5-2.4.3.3: probe of 5-2.4.3.3 failed with
> error -17
> 
> This happens because the driver uses a fixed power supply name
> ("apple_mfi_fastcharge") for all devices, causing a sysfs name
> conflict when a second device is connected.
> 
> Fix this by generating unique names using the USB bus and device
> number (e.g., "apple_mfi_fastcharge_5-12"). This ensures each
> connected device gets a unique power supply entry in sysfs.
> 
> The change requires storing a copy of the power_supply_desc structure
> in the per-device mfi_device struct, since the name pointer needs to
> remain valid for the lifetime of the power supply registration.
> 
> Fixes: 249fa8217b84 ("USB: Add driver to control USB fast charge for
> iOS devices")
> Signed-off-by: Charalampos Mitrodimas <charmitro@posteo.net>
> ---
>  drivers/usb/misc/apple-mfi-fastcharge.c | 24 +++++++++++++++++++++--
> -
>  1 file changed, 21 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c
> b/drivers/usb/misc/apple-mfi-fastcharge.c
> index
> ac8695195c13c8752076e4391ac81a9da3780c44..8e852f4b8262e6e8fcd33883be8
> c5696f19b9ee9 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> @@ -44,6 +44,7 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
>  struct mfi_device {
>  	struct usb_device *udev;
>  	struct power_supply *battery;
> +	struct power_supply_desc battery_desc;
>  	int charge_type;
>  };
>  
> @@ -178,6 +179,7 @@ static int mfi_fc_probe(struct usb_device *udev)
>  {
>  	struct power_supply_config battery_cfg = {};
>  	struct mfi_device *mfi = NULL;
> +	char *battery_name;
>  	int err;
>  
>  	if (!mfi_fc_match(udev))
> @@ -187,23 +189,38 @@ static int mfi_fc_probe(struct usb_device
> *udev)
>  	if (!mfi)
>  		return -ENOMEM;
>  
> +	battery_name = kasprintf(GFP_KERNEL,
> "apple_mfi_fastcharge_%d-%d",
> +				 udev->bus->busnum, udev->devnum);

Looks fine to me although I don't know how common this construct is.

If others think this won't work, you can use the ever increasing id as
used in drivers/hid/hid-steelseries.c

Cheers

> +	if (!battery_name) {
> +		err = -ENOMEM;
> +		goto err_free_mfi;
> +	}
> +
> +	mfi->battery_desc = apple_mfi_fc_desc;
> +	mfi->battery_desc.name = battery_name;
> +
>  	battery_cfg.drv_data = mfi;
>  
>  	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
>  	mfi->battery = power_supply_register(&udev->dev,
> -						&apple_mfi_fc_desc,
> +						&mfi->battery_desc,
>  						&battery_cfg);
>  	if (IS_ERR(mfi->battery)) {
>  		dev_err(&udev->dev, "Can't register battery\n");
>  		err = PTR_ERR(mfi->battery);
> -		kfree(mfi);
> -		return err;
> +		goto err_free_name;
>  	}
>  
>  	mfi->udev = usb_get_dev(udev);
>  	dev_set_drvdata(&udev->dev, mfi);
>  
>  	return 0;
> +
> +err_free_name:
> +	kfree(battery_name);
> +err_free_mfi:
> +	kfree(mfi);
> +	return err;
>  }
>  
>  static void mfi_fc_disconnect(struct usb_device *udev)
> @@ -213,6 +230,7 @@ static void mfi_fc_disconnect(struct usb_device
> *udev)
>  	mfi = dev_get_drvdata(&udev->dev);
>  	if (mfi->battery)
>  		power_supply_unregister(mfi->battery);
> +	kfree(mfi->battery_desc.name);
>  	dev_set_drvdata(&udev->dev, NULL);
>  	usb_put_dev(mfi->udev);
>  	kfree(mfi);
> 
> ---
> base-commit: cd2e103d57e5615f9bb027d772f93b9efd567224
> change-id: 20250602-apple-mfi-fastcharge-duplicate-sysfs-0ef3864b21f8
> 
> Best regards,

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique
  2025-06-02 18:48 ` Bastien Nocera
@ 2025-06-03 11:12   ` Charalampos Mitrodimas
  0 siblings, 0 replies; 3+ messages in thread
From: Charalampos Mitrodimas @ 2025-06-03 11:12 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: Greg Kroah-Hartman, Alan Stern, linux-usb, linux-kernel

Bastien Nocera <hadess@hadess.net> writes:

> On Mon, 2025-06-02 at 18:26 +0000, Charalampos Mitrodimas wrote:
>> When multiple Apple devices are connected concurrently, the
>> apple-mfi-fastcharge driver fails to probe the subsequent devices
>> with
>> the following error:
>> 
>>     sysfs: cannot create duplicate filename
>> '/class/power_supply/apple_mfi_fastcharge'
>>     apple-mfi-fastcharge 5-2.4.3.3: probe of 5-2.4.3.3 failed with
>> error -17
>> 
>> This happens because the driver uses a fixed power supply name
>> ("apple_mfi_fastcharge") for all devices, causing a sysfs name
>> conflict when a second device is connected.
>> 
>> Fix this by generating unique names using the USB bus and device
>> number (e.g., "apple_mfi_fastcharge_5-12"). This ensures each
>> connected device gets a unique power supply entry in sysfs.
>> 
>> The change requires storing a copy of the power_supply_desc structure
>> in the per-device mfi_device struct, since the name pointer needs to
>> remain valid for the lifetime of the power supply registration.
>> 
>> Fixes: 249fa8217b84 ("USB: Add driver to control USB fast charge for
>> iOS devices")
>> Signed-off-by: Charalampos Mitrodimas <charmitro@posteo.net>
>> ---
>>  drivers/usb/misc/apple-mfi-fastcharge.c | 24 +++++++++++++++++++++--
>> -
>>  1 file changed, 21 insertions(+), 3 deletions(-)
>> 
>> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c
>> b/drivers/usb/misc/apple-mfi-fastcharge.c
>> index
>> ac8695195c13c8752076e4391ac81a9da3780c44..8e852f4b8262e6e8fcd33883be8
>> c5696f19b9ee9 100644
>> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
>> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
>> @@ -44,6 +44,7 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
>>  struct mfi_device {
>>  	struct usb_device *udev;
>>  	struct power_supply *battery;
>> +	struct power_supply_desc battery_desc;
>>  	int charge_type;
>>  };
>>  
>> @@ -178,6 +179,7 @@ static int mfi_fc_probe(struct usb_device *udev)
>>  {
>>  	struct power_supply_config battery_cfg = {};
>>  	struct mfi_device *mfi = NULL;
>> +	char *battery_name;
>>  	int err;
>>  
>>  	if (!mfi_fc_match(udev))
>> @@ -187,23 +189,38 @@ static int mfi_fc_probe(struct usb_device
>> *udev)
>>  	if (!mfi)
>>  		return -ENOMEM;
>>  
>> +	battery_name = kasprintf(GFP_KERNEL,
>> "apple_mfi_fastcharge_%d-%d",
>> +				 udev->bus->busnum, udev->devnum);
>
> Looks fine to me although I don't know how common this construct is.
>
> If others think this won't work, you can use the ever increasing id as
> used in drivers/hid/hid-steelseries.c

Hi Bastien,

Thanks for the review!

FWIW, this has been tested in QEMU via,

      -device usb-host,bus=ehci.0,vendorid=0x05ac,productid=0x12a8
      -device usb-host,bus=ehci.0,vendorid=0x05ac,productid=0x12ab

And works as expected,

$ ls /sys/class/power_supply
apple_mfi_fastcharge_2_2  apple_mfi_fastcharge_2_3

C. Mitrodimas

>
> Cheers
>
>> +	if (!battery_name) {
>> +		err = -ENOMEM;
>> +		goto err_free_mfi;
>> +	}
>> +
>> +	mfi->battery_desc = apple_mfi_fc_desc;
>> +	mfi->battery_desc.name = battery_name;
>> +
>>  	battery_cfg.drv_data = mfi;
>>  
>>  	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
>>  	mfi->battery = power_supply_register(&udev->dev,
>> -						&apple_mfi_fc_desc,
>> +						&mfi->battery_desc,
>>  						&battery_cfg);
>>  	if (IS_ERR(mfi->battery)) {
>>  		dev_err(&udev->dev, "Can't register battery\n");
>>  		err = PTR_ERR(mfi->battery);
>> -		kfree(mfi);
>> -		return err;
>> +		goto err_free_name;
>>  	}
>>  
>>  	mfi->udev = usb_get_dev(udev);
>>  	dev_set_drvdata(&udev->dev, mfi);
>>  
>>  	return 0;
>> +
>> +err_free_name:
>> +	kfree(battery_name);
>> +err_free_mfi:
>> +	kfree(mfi);
>> +	return err;
>>  }
>>  
>>  static void mfi_fc_disconnect(struct usb_device *udev)
>> @@ -213,6 +230,7 @@ static void mfi_fc_disconnect(struct usb_device
>> *udev)
>>  	mfi = dev_get_drvdata(&udev->dev);
>>  	if (mfi->battery)
>>  		power_supply_unregister(mfi->battery);
>> +	kfree(mfi->battery_desc.name);
>>  	dev_set_drvdata(&udev->dev, NULL);
>>  	usb_put_dev(mfi->udev);
>>  	kfree(mfi);
>> 
>> ---
>> base-commit: cd2e103d57e5615f9bb027d772f93b9efd567224
>> change-id: 20250602-apple-mfi-fastcharge-duplicate-sysfs-0ef3864b21f8
>> 
>> Best regards,

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-03 11:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-02 18:26 [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique Charalampos Mitrodimas
2025-06-02 18:48 ` Bastien Nocera
2025-06-03 11:12   ` Charalampos Mitrodimas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).