All of lore.kernel.org
 help / color / mirror / Atom feed
From: Charalampos Mitrodimas <charmitro@posteo.net>
To: Bastien Nocera <hadess@hadess.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Alan Stern <stern@rowland.harvard.edu>,
	 linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: misc: apple-mfi-fastcharge: Make power supply names unique
Date: Tue, 03 Jun 2025 11:12:04 +0000	[thread overview]
Message-ID: <87r001uliz.fsf@posteo.net> (raw)
In-Reply-To: <5a610d364c8a8148e48d5bf7a95e73fc81650e5a.camel@hadess.net>

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,

      reply	other threads:[~2025-06-03 11:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r001uliz.fsf@posteo.net \
    --to=charmitro@posteo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=hadess@hadess.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.