mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: Bastien Nocera <hadess@hadess.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources
Date: Sat,  5 Dec 2026 18:44:45 -0800	[thread overview]
Message-ID: <cfe9bf3def4e942b0bb9d1211ce65e8f9a837cb6.1796524906.git.rosenp@gmail.com> (raw)
In-Reply-To: <cover.1796524906.git.rosenp@gmail.com>

Use devm_kzalloc_obj(), 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.

Remove the redundant mfi_fc_match() check from probe since the
.match callback already handles filtering.

Also fix the module init/exit: module_usb_driver() is for struct
usb_driver *, not struct usb_device_driver *. Use the correct
usb_register_device_driver() / usb_deregister_device_driver() calls
instead.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 76 +++++++------------------
 1 file changed, 20 insertions(+), 56 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 02adec28d9e6..133dde848dd1 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -43,9 +43,8 @@ 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;
+	struct power_supply_desc battery_desc;
 };
 
 static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
@@ -155,16 +154,6 @@ static enum power_supply_property apple_mfi_fc_properties[] = {
 	POWER_SUPPLY_PROP_SCOPE
 };
 
-static const struct power_supply_desc apple_mfi_fc_desc = {
-	.name                   = "apple_mfi_fastcharge",
-	.type                   = POWER_SUPPLY_TYPE_BATTERY,
-	.properties             = apple_mfi_fc_properties,
-	.num_properties         = ARRAY_SIZE(apple_mfi_fc_properties),
-	.get_property           = apple_mfi_fc_get_property,
-	.set_property           = apple_mfi_fc_set_property,
-	.property_is_writeable  = apple_mfi_fc_property_is_writeable
-};
-
 static bool mfi_fc_match(struct usb_device *udev)
 {
 	int idProduct;
@@ -177,67 +166,42 @@ 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_obj(&udev->dev, *mfi);
 	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;
-	}
+	battery_desc = &mfi->battery_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;
 
-	mfi->battery_desc = apple_mfi_fc_desc;
-	mfi->battery_desc.name = battery_name;
+	battery_desc->type = POWER_SUPPLY_TYPE_BATTERY;
+	battery_desc->properties = apple_mfi_fc_properties;
+	battery_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
+	battery_desc->get_property = apple_mfi_fc_get_property;
+	battery_desc->set_property = apple_mfi_fc_set_property;
+	battery_desc->property_is_writeable = apple_mfi_fc_property_is_writeable;
 
 	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;
-}
+	battery = devm_power_supply_register(&udev->dev, battery_desc, &battery_cfg);
+	if (IS_ERR(battery))
+		return PTR_ERR(battery);
 
-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);
+	return 0;
 }
 
 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.56.1


  reply	other threads:[~2026-12-06  2:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
2026-12-06  2:44 ` Rosen Penev [this message]
2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev

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=cfe9bf3def4e942b0bb9d1211ce65e8f9a837cb6.1796524906.git.rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hadess@hadess.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®