Skip to content

Commit 570ec17

Browse files
Chance Yangopsiff
authored andcommitted
usb: common: usb-conn-gpio: use a unique name for usb connector device
[ Upstream commit d4e5b10c55627e2f3fc9e5b337a28b4e2f02a55e ] The current implementation of the usb-conn-gpio driver uses a fixed "usb-charger" name for all USB connector devices. This causes conflicts in the power supply subsystem when multiple USB connectors are present, as duplicate names are not allowed. Use IDA to manage unique IDs for naming usb connectors (e.g., usb-charger-0, usb-charger-1). Signed-off-by: Chance Yang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Sasha Levin <[email protected]> (cherry picked from commit 4fb6703824793bcdddaf5b4efaa6cba16328878e)
1 parent ea25de2 commit 570ec17

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

drivers/usb/common/usb-conn-gpio.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <linux/power_supply.h>
2121
#include <linux/regulator/consumer.h>
2222
#include <linux/usb/role.h>
23+
#include <linux/idr.h>
24+
25+
static DEFINE_IDA(usb_conn_ida);
2326

2427
#define USB_GPIO_DEB_MS 20 /* ms */
2528
#define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
@@ -29,6 +32,7 @@
2932

3033
struct usb_conn_info {
3134
struct device *dev;
35+
int conn_id; /* store the IDA-allocated ID */
3236
struct usb_role_switch *role_sw;
3337
enum usb_role last_role;
3438
struct regulator *vbus;
@@ -160,16 +164,28 @@ static int usb_conn_psy_register(struct usb_conn_info *info)
160164
.of_node = dev->of_node,
161165
};
162166

163-
desc->name = "usb-charger";
167+
info->conn_id = ida_alloc(&usb_conn_ida, GFP_KERNEL);
168+
if (info->conn_id < 0)
169+
return info->conn_id;
170+
171+
desc->name = devm_kasprintf(dev, GFP_KERNEL, "usb-charger-%d",
172+
info->conn_id);
173+
if (!desc->name) {
174+
ida_free(&usb_conn_ida, info->conn_id);
175+
return -ENOMEM;
176+
}
177+
164178
desc->properties = usb_charger_properties;
165179
desc->num_properties = ARRAY_SIZE(usb_charger_properties);
166180
desc->get_property = usb_charger_get_property;
167181
desc->type = POWER_SUPPLY_TYPE_USB;
168182
cfg.drv_data = info;
169183

170184
info->charger = devm_power_supply_register(dev, desc, &cfg);
171-
if (IS_ERR(info->charger))
172-
dev_err(dev, "Unable to register charger\n");
185+
if (IS_ERR(info->charger)) {
186+
dev_err(dev, "Unable to register charger %d\n", info->conn_id);
187+
ida_free(&usb_conn_ida, info->conn_id);
188+
}
173189

174190
return PTR_ERR_OR_ZERO(info->charger);
175191
}
@@ -277,6 +293,9 @@ static void usb_conn_remove(struct platform_device *pdev)
277293

278294
cancel_delayed_work_sync(&info->dw_det);
279295

296+
if (info->charger)
297+
ida_free(&usb_conn_ida, info->conn_id);
298+
280299
if (info->last_role == USB_ROLE_HOST && info->vbus)
281300
regulator_disable(info->vbus);
282301

0 commit comments

Comments
 (0)