Skip to contentSkip to conversation

usb: apple-mfi-fastcharge: Convert to devm resources

PATCH In review Rosen Penev 2 participants 4 messages Jun 13 – Jun 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: apple-mfi-fastcharge: Convert to devm resources

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
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 61 ++++++-------------------
 1 file changed, 13 insertions(+), 48 deletions(-)
drivers/usb/misc/apple-mfi-fastcharge.c +13 -48
index af266e19f2fd..c1064f672d6d 100644
@@ -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,
Signature · 1 line
2.54.0
#1 lore raw
Quoted 3 lines · Rosen Penev

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.

Quoted 2 lines

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

#2 lore raw
Quoted 8 lines · Greg Kroah-Hartman
Quoted 3 lines · Rosen Penev

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.

Quoted 5 lines
Quoted 3 lines

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.

Quoted 4 lines

thanks,

greg k-h

#3 lore raw
Quoted 11 lines · Rosen Penev
Quoted 8 lines · Greg Kroah-Hartman
Quoted 3 lines · Rosen Penev

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