Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

USB: document some API requirements on disconnection

A call to usb_set_interface() crashes if the device is deallocated
concurrently, such as due to physical removal or a serious IO error.
It could also interfere with other drivers using the device if the
current driver is unbound before the call is finished.

Document the need to delay driver unbinding until this call returns,
which solves both issues.

Document the same regarding usb_clear_halt(), which is equally known
to be routinely called by drivers.

Explicitly mention finishing pending operations in the documentation
of the driver disconnect callback.

Signed-off-by: Michal Pecio <michal.pecio@gmail.com>
Link: https://lore.kernel.org/r/20240218092515.7635ff8c@foxbook
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Michal Pecio and committed by
Greg Kroah-Hartman
9f2a3933 be95cc6d

+8 -3
+4 -2
Documentation/driver-api/usb/callbacks.rst
··· 99 99 This callback is a signal to break any connection with an interface. 100 100 You are not allowed any IO to a device after returning from this 101 101 callback. You also may not do any other operation that may interfere 102 - with another driver bound the interface, eg. a power management 103 - operation. 102 + with another driver bound to the interface, eg. a power management 103 + operation. Outstanding operations on the device must be completed or 104 + aborted before this callback may return. 105 + 104 106 If you are called due to a physical disconnection, all your URBs will be 105 107 killed by usbcore. Note that in this case disconnect will be called some 106 108 time after the physical disconnection. Thus your driver must be prepared
+4 -1
drivers/usb/core/message.c
··· 1198 1198 * same status code used to report a true stall. 1199 1199 * 1200 1200 * This call is synchronous, and may not be used in an interrupt context. 1201 + * If a thread in your driver uses this call, make sure your disconnect() 1202 + * method can wait for it to complete. 1201 1203 * 1202 1204 * Return: Zero on success, or else the status code returned by the 1203 1205 * underlying usb_control_msg() call. ··· 1518 1516 * This call is synchronous, and may not be used in an interrupt context. 1519 1517 * Also, drivers must not change altsettings while urbs are scheduled for 1520 1518 * endpoints in that interface; all such urbs must first be completed 1521 - * (perhaps forced by unlinking). 1519 + * (perhaps forced by unlinking). If a thread in your driver uses this call, 1520 + * make sure your disconnect() method can wait for it to complete. 1522 1521 * 1523 1522 * Return: Zero on success, or else the status code returned by the 1524 1523 * underlying usb_control_msg() call.