usb_set_device_state(9)
π usb_set_device_state(9) π
okay bestie, welcome to the Kernel API Manual β yes we're in the kernel today, and yes this function is lowkey iconic. here's the whole vibe check on usb_set_device_state, a usbcore/hcds situation that changes a device's current state π¦
β¨ NAME β¨
usb_set_device_state β change a device's current state (usbcore, hcds) π
π SYNOPSIS π
void usb_set_device_state (struct usb_device *udev , enum
usb_device_state new_state );
πΈ ARGUMENTS πΈ
udevβ pointer to the device whose state should be changed. like, this one, right here π«Άnew_stateβ the new state value to be stored. the fresh fit it's gonna be wearing π
π DESCRIPTION π
okay so this is the part where you gotta pay attention, because it's giving "safety goggles on" energy π
udev->state is not fully protected by the device lock. like, at all. no cap. even though most transitions are made only while holding the lock, the state can change to USB_STATE_NOTATTACHED at almost any time β° β and that's on purpose! it's so devices can be marked as disconnected as soon as possible, without having to wait for any semaphores to be released. iconic efficiency, honestly β¨
but the consequence?? as a result, all changes to any device's state must be protected by the device_state_lock spinlock π. every single one. periodt.
once a device has been added to the device tree, all changes to its state should be made using this routine. the state should not be set directly. don't do it. i'm literally begging π
here's the actual logic, bestie:
- if
udev->stateis alreadyUSB_STATE_NOTATTACHEDthen no change is made β nothing happens, we move on π - otherwise
udev->stateis set tonew_stateβ slay π - and if
new_stateisUSB_STATE_NOTATTACHED, then all ofudev's descendants' states are also set toUSB_STATE_NOTATTACHEDβ the whole family gets the news at once, rip π«‘
no cap: setting state directly instead of going through this routine after the device is in the device tree is a don't. the lock is not optional. the whole descendant-update cascade is not optional either π₯
π§ SEE ALSO π§
kernel file ./drivers/usb/core/hub.c and its little friend group π
usb_hub_set_port_power(9)usb_hub_clear_tt_buffer(9)usb_remove_device(9)usb_disconnect(9)usb_enumerate_device_otg(9)usb_enumerate_device(9)usb_new_device(9)usb_deauthorize_device(9)get_port_ssp_rate(9)usb_root_hub_lost_power(9)hub_hc_release_resources(9)usb_reset_and_verify_device(9)usb_reset_device(9)usb_queue_reset_device(9)usb_hub_find_child(9)usb_get_hub_port_acpi_handle(9)
and that's the page!! π brought to you by ./drivers/usb/core, dated October 2026, and yeah β usb_set_device_state(9) is the main character here, obviously ππ go forth and guard that state with the spinlock, bestie π¦πΈ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms