drivers/usb/common/usb-conn-gpio.c
Source file repositories/reference/linux-study-clean/drivers/usb/common/usb-conn-gpio.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/common/usb-conn-gpio.c- Extension
.c- Size
- 9351 bytes
- Lines
- 376
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/device.hlinux/gpio/consumer.hlinux/interrupt.hlinux/irq.hlinux/module.hlinux/of.hlinux/pinctrl/consumer.hlinux/platform_device.hlinux/power_supply.hlinux/regulator/consumer.hlinux/string_choices.hlinux/usb/role.hlinux/idr.h
Detected Declarations
struct usb_conn_infofunction usb_conn_detect_cablefunction usb_conn_queue_dworkfunction usb_conn_isrfunction usb_charger_get_propertyfunction usb_conn_psy_registerfunction usb_conn_probefunction usb_conn_removefunction usb_conn_suspendfunction usb_conn_resume
Annotated Snippet
struct usb_conn_info {
struct device *dev;
int conn_id; /* store the IDA-allocated ID */
struct usb_role_switch *role_sw;
enum usb_role last_role;
struct regulator *vbus;
struct delayed_work dw_det;
unsigned long debounce_jiffies;
struct gpio_desc *id_gpiod;
struct gpio_desc *vbus_gpiod;
int id_irq;
int vbus_irq;
struct power_supply_desc desc;
struct power_supply *charger;
bool initial_detection;
};
/*
* "DEVICE" = VBUS and "HOST" = !ID, so we have:
* Both "DEVICE" and "HOST" can't be set as active at the same time
* so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
* even if VBUS is on.
*
* Role | ID | VBUS
* ------------------------------------
* [1] DEVICE | H | H
* [2] NONE | H | L
* [3] HOST | L | H
* [4] HOST | L | L
*
* In case we have only one of these signals:
* - VBUS only - we want to distinguish between [1] and [2], so ID is always 1
* - ID only - we want to distinguish between [1] and [4], so VBUS = ID
*/
static void usb_conn_detect_cable(struct work_struct *work)
{
struct usb_conn_info *info;
enum usb_role role;
int id, vbus, ret;
info = container_of(to_delayed_work(work),
struct usb_conn_info, dw_det);
/* check ID and VBUS */
id = info->id_gpiod ?
gpiod_get_value_cansleep(info->id_gpiod) : 1;
vbus = info->vbus_gpiod ?
gpiod_get_value_cansleep(info->vbus_gpiod) : id;
if (!id)
role = USB_ROLE_HOST;
else if (vbus)
role = USB_ROLE_DEVICE;
else
role = USB_ROLE_NONE;
dev_dbg(info->dev, "role %s -> %s, gpios: id %d, vbus %d\n",
usb_role_string(info->last_role), usb_role_string(role), id, vbus);
if (!info->initial_detection && info->last_role == role) {
dev_warn(info->dev, "repeated role: %s\n", usb_role_string(role));
return;
}
info->initial_detection = false;
if (info->last_role == USB_ROLE_HOST && info->vbus)
regulator_disable(info->vbus);
ret = usb_role_switch_set_role(info->role_sw, role);
if (ret)
dev_err(info->dev, "failed to set role: %d\n", ret);
if (role == USB_ROLE_HOST && info->vbus) {
ret = regulator_enable(info->vbus);
if (ret)
dev_err(info->dev, "enable vbus regulator failed\n");
}
info->last_role = role;
if (info->vbus)
dev_dbg(info->dev, "vbus regulator is %s\n",
str_enabled_disabled(regulator_is_enabled(info->vbus)));
power_supply_changed(info->charger);
}
Annotation
- Immediate include surface: `linux/device.h`, `linux/gpio/consumer.h`, `linux/interrupt.h`, `linux/irq.h`, `linux/module.h`, `linux/of.h`, `linux/pinctrl/consumer.h`, `linux/platform_device.h`.
- Detected declarations: `struct usb_conn_info`, `function usb_conn_detect_cable`, `function usb_conn_queue_dwork`, `function usb_conn_isr`, `function usb_charger_get_property`, `function usb_conn_psy_register`, `function usb_conn_probe`, `function usb_conn_remove`, `function usb_conn_suspend`, `function usb_conn_resume`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.