drivers/usb/typec/mode_selection.c
Source file repositories/reference/linux-study-clean/drivers/usb/typec/mode_selection.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/typec/mode_selection.c- Extension
.c- Size
- 7730 bytes
- Lines
- 284
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/types.hlinux/list_sort.hlinux/slab.hlinux/mutex.hlinux/workqueue.hlinux/usb/typec_altmode.hclass.h
Detected Declarations
struct mode_statestruct mode_selectionstruct mode_orderfunction activate_altmodefunction mode_selection_activatefunction mode_list_cleanfunction list_for_each_entry_safefunction mode_selection_work_fnfunction typec_altmode_state_updatefunction compare_prioritiesfunction altmode_add_to_listfunction typec_mode_selection_startfunction typec_mode_selection_deleteexport typec_altmode_state_updateexport typec_mode_selection_startexport typec_mode_selection_delete
Annotated Snippet
struct mode_state {
u16 svid;
u8 priority;
int error;
struct list_head list;
};
/**
* struct mode_selection - Manages the selection and state of Alternate Modes
* @mode_list: Prioritized list of available Alternate Modes
* @lock: Mutex to protect mode_list
* @work: Work structure
* @partner: Handle to the Type-C partner device
* @active_svid: svid of currently active mode
* @timeout: Timeout for a mode entry attempt, ms
* @delay: Delay between mode entry/exit attempts, ms
*/
struct mode_selection {
struct list_head mode_list;
/* Protects the mode_list*/
struct mutex lock;
struct delayed_work work;
struct typec_partner *partner;
u16 active_svid;
unsigned int timeout;
unsigned int delay;
};
/**
* struct mode_order - Mode activation tracking
* @svid: Standard or Vendor ID of the Alternate Mode
* @enter: Flag indicating if the driver is currently attempting to enter or
* exit the mode
* @result: Outcome of the attempt to activate the mode
*/
struct mode_order {
u16 svid;
int enter;
int result;
};
static int activate_altmode(struct device *dev, void *data)
{
if (is_typec_partner_altmode(dev)) {
struct typec_altmode *alt = to_typec_altmode(dev);
struct mode_order *order = (struct mode_order *)data;
if (order->svid == alt->svid) {
if (alt->ops && alt->ops->activate)
order->result = alt->ops->activate(alt, order->enter);
else
order->result = -EOPNOTSUPP;
return 1;
}
}
return 0;
}
static int mode_selection_activate(struct mode_selection *sel,
const u16 svid, const int enter)
__must_hold(&sel->lock)
{
struct mode_order order = {.svid = svid, .enter = enter, .result = -ENODEV};
/*
* The port driver may acquire its internal mutex during alternate mode
* activation. Since this is the same mutex that may be held during the
* execution of typec_altmode_state_update(), it is crucial to release
* sel->mutex before activation to avoid potential deadlock.
* Note that sel->mode_list must remain invariant throughout this unlocked
* interval.
*/
mutex_unlock(&sel->lock);
device_for_each_child(&sel->partner->dev, &order, activate_altmode);
mutex_lock(&sel->lock);
return order.result;
}
static void mode_list_clean(struct mode_selection *sel)
{
struct mode_state *ms, *tmp;
list_for_each_entry_safe(ms, tmp, &sel->mode_list, list) {
list_del(&ms->list);
kfree(ms);
}
}
Annotation
- Immediate include surface: `linux/types.h`, `linux/list_sort.h`, `linux/slab.h`, `linux/mutex.h`, `linux/workqueue.h`, `linux/usb/typec_altmode.h`, `class.h`.
- Detected declarations: `struct mode_state`, `struct mode_selection`, `struct mode_order`, `function activate_altmode`, `function mode_selection_activate`, `function mode_list_clean`, `function list_for_each_entry_safe`, `function mode_selection_work_fn`, `function typec_altmode_state_update`, `function compare_priorities`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.