* [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources
@ 2026-06-13 20:18 Rosen Penev
2026-06-14 3:40 ` Greg Kroah-Hartman
0 siblings, 1 reply; 4+ messages in thread
From: Rosen Penev @ 2026-06-13 20:18 UTC (permalink / raw)
To: linux-usb; +Cc: Bastien Nocera, Greg Kroah-Hartman, open list
Use devm_kzalloc(), devm_kasprintf(), and devm_power_supply_register()
to let the driver core handle resource cleanup. This allows removing
the mfi_fc_disconnect() function and the error labels in probe.
Also take the opportunity to shrink the private struct and move the
variables in probe as devm doesn't need them in the struct.
Assisted-by: opencode:big-pickle
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/usb/misc/apple-mfi-fastcharge.c | 61 ++++++-------------------
1 file changed, 13 insertions(+), 48 deletions(-)
diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index af266e19f2fd..c1064f672d6d 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -43,8 +43,6 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
/* Driver-local specific stuff */
struct mfi_device {
struct usb_device *udev;
- struct power_supply *battery;
- struct power_supply_desc battery_desc;
int charge_type;
};
@@ -177,67 +175,34 @@ static bool mfi_fc_match(struct usb_device *udev)
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))
- return -ENODEV;
+ struct power_supply_desc battery_desc;
+ struct power_supply *battery;
+ struct mfi_device *mfi;
- mfi = kzalloc_obj(struct mfi_device);
+ mfi = devm_kzalloc(&udev->dev, sizeof(*mfi), GFP_KERNEL);
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_desc = apple_mfi_fc_desc;
+ battery_desc.name = devm_kasprintf(&udev->dev, GFP_KERNEL,
+ "apple_mfi_fastcharge_%d-%d",
+ udev->bus->busnum,
+ udev->devnum);
+ if (!battery_desc.name)
+ return -ENOMEM;
battery_cfg.drv_data = mfi;
mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
- mfi->battery = power_supply_register(&udev->dev,
- &mfi->battery_desc,
- &battery_cfg);
- if (IS_ERR(mfi->battery)) {
- dev_err(&udev->dev, "Can't register battery\n");
- err = PTR_ERR(mfi->battery);
- goto err_free_name;
- }
-
mfi->udev = 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)
-{
- struct mfi_device *mfi;
- 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);
- kfree(mfi);
+ battery = devm_power_supply_register(&udev->dev, &battery_desc, &battery_cfg);
+ return PTR_ERR_OR_ZERO(battery);
}
static struct usb_device_driver mfi_fc_driver = {
.name = "apple-mfi-fastcharge",
.probe = mfi_fc_probe,
- .disconnect = mfi_fc_disconnect,
.id_table = mfi_fc_id_table,
.match = mfi_fc_match,
.generic_subclass = 1,
--
2.54.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources
2026-06-13 20:18 [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
@ 2026-06-14 3:40 ` Greg Kroah-Hartman
2026-06-14 3:45 ` Rosen Penev
0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-06-14 3:40 UTC (permalink / raw)
To: Rosen Penev; +Cc: linux-usb, Bastien Nocera, open list
On Sat, Jun 13, 2026 at 01:18:51PM -0700, Rosen Penev wrote:
> Use devm_kzalloc(), devm_kasprintf(), and devm_power_supply_register()
> to let the driver core handle resource cleanup. This allows removing
> the mfi_fc_disconnect() function and the error labels in probe.
Please don't do this on old drivers unless you have the hardware to test
that the change worked properly.
> Also take the opportunity to shrink the private struct and move the
> variables in probe as devm doesn't need them in the struct.
Shouldn't this be a separate change?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources
2026-06-14 3:40 ` Greg Kroah-Hartman
@ 2026-06-14 3:45 ` Rosen Penev
2026-06-14 3:56 ` Greg Kroah-Hartman
0 siblings, 1 reply; 4+ messages in thread
From: Rosen Penev @ 2026-06-14 3:45 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-usb, Bastien Nocera, open list
On Sat, Jun 13, 2026 at 8:41 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Sat, Jun 13, 2026 at 01:18:51PM -0700, Rosen Penev wrote:
> > Use devm_kzalloc(), devm_kasprintf(), and devm_power_supply_register()
> > to let the driver core handle resource cleanup. This allows removing
> > the mfi_fc_disconnect() function and the error labels in probe.
>
> Please don't do this on old drivers unless you have the hardware to test
> that the change worked properly.
iPhones? I have those. I also have a USB power meter.
>
> > Also take the opportunity to shrink the private struct and move the
> > variables in probe as devm doesn't need them in the struct.
>
> Shouldn't this be a separate change?
Not really. devm is what obsoletes them. And actually one of them
belongs in the struct. Will fix in v2.
>
> thanks,
>
> greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources
2026-06-14 3:45 ` Rosen Penev
@ 2026-06-14 3:56 ` Greg Kroah-Hartman
0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-06-14 3:56 UTC (permalink / raw)
To: Rosen Penev; +Cc: linux-usb, Bastien Nocera, open list
On Sat, Jun 13, 2026 at 08:45:02PM -0700, Rosen Penev wrote:
> On Sat, Jun 13, 2026 at 8:41 PM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Sat, Jun 13, 2026 at 01:18:51PM -0700, Rosen Penev wrote:
> > > Use devm_kzalloc(), devm_kasprintf(), and devm_power_supply_register()
> > > to let the driver core handle resource cleanup. This allows removing
> > > the mfi_fc_disconnect() function and the error labels in probe.
> >
> > Please don't do this on old drivers unless you have the hardware to test
> > that the change worked properly.
> iPhones? I have those. I also have a USB power meter.
And did you test this change out with all of that? If you do, please
say so.
But again, there is no need to change existing code to use devm_* calls
unless it fixes a bug. devm can be tricky and is not always a good
idea.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-14 3:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-13 20:18 [PATCH] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
2026-06-14 3:40 ` Greg Kroah-Hartman
2026-06-14 3:45 ` Rosen Penev
2026-06-14 3:56 ` Greg Kroah-Hartman
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.