Understanding USB callback events and sequence

From: Farhan Khan <farhan_at_farhan.codes>
Date: Tue, 22 Mar 2022 18:05:19 UTC
Hi all,

I am trying to understand how the structure of the USB callback and have a few questions. The relevant manual page is usbdi(9).

The manual talks about the 3 states of the callback: USB_ST_SETUP, USB_ST_TRANSFERRED, and default (error). I am not clear on the difference between USB_ST_SETUP and USB_ST_TRANSFERRED. The manual seems to suggest that USB_ST_SETUP is where the USB packet is sent, which leads me to think USB_ST_TRANSFERRED is cleanup? From looking at examples, this appears to be how thinks are done.

What triggers the event? From testing, it seems that usbd_transfer_start(9) triggers the callback with USB_ST_SETUP. If so, what triggers USB_ST_TRANSFERRED. Why is it necessary?

Also, when I read examples I see that in some places the expected `break` in the `select` is removed with a note saying "fall through". Wouldn't this double-send the USB packet, or at least sent another packet during the USB_ST_TRANSFERRED stage? For example here:

Finally, I have been looking at USB traffic under wireshark (interpreted from usbdump -w file.pcap) and I see that every USB packet has a response from the device. Is this response packet handled by one of the RX callbacks? Or is this the same as TX's USB_ST_TRANSFERRED?

Thank you and pardon the multiple questions!

Farhan Khan
PGP Fingerprint: 1312 89CE 663E 1EB2 179C 1C83 C41D 2281 F8DA C0DE