USB – Role Swapped USB OTG A-Device Disconnection Detection

usbusb deviceusb-host

Let's say I have a USB OTG A-device that has negotiated a role swap via HNP, i.e. it is providing VBUS while acting in the device (non-host) role for data.

More commonly USB devices do not provide VBUS and therefore can detect that they have been disconnected from the host when VBUS disappears.
This is differentiated from a suspended state, where activity on the bus ceases but VBUS remains.

In the role swapped example, however, since the device is providing power it can't rely on the disappearance of VBUS to detect when the host is disconnected.
The only other option is to detect when activity on the bus ceases, but then how can this be differentiated from a suspended state?

I'm obviously missing something.

TIA

Best Answer

As Ale..chenski referenced in their comment, section 6.3 of the On-The-Go Supplement to the USB 2.0 Specification outlines the HNP Sequence of Events:

A) A-device finishes using bus and stops all bus activity, (i.e. suspends the bus).

B) B-device detects that bus is idle for more than TA_AIDL_BDIS min and begins HNP by turning off pull-up on D+. This allows the bus to discharge to the SE0 state. If the bus was operating in HS mode, the B- device will first enter the full-speed mode and turn on its D+ pull-up resistor for at least TB_FS_BDIS min before turning off its pull up to start the HNP sequence.

Note: After B-device enters the FS mode and turns on its pull-up resistor; it waits to see if the data line goes high. If the data line does not go high within TWTRSTHS (from Table 7-14 in USB 2.0 specification [USB2.0]), then the B-device shall start its HS chirp. Otherwise, if the D+ line does go high for at least TB_FS_BDIS min, then the B-device may start HNP.

C) The A-device detects the SE0 on the bus and recognizes this as a request from the B-device to become Host. The A-device responds by turning on its D+ pull-up within TA_BDIS_ACON max of first detecting the SE0 on the bus.

D) After waiting long enough to insure that the D+ line cannot be high due to the residual effect of the B- device pull-up, (see Section 5.1.9), the B-device sees that the D+ line is high and D- line is low, (i.e. J state). This indicates that the A-device has recognized the HNP request from the B-device. At this point, the B-device becomes Host and asserts bus reset to start using the bus. The B-device must assert the bus reset (SE0) within TB_ACON_BSE0 max of the time that the A-device turns on its pull-up.

E) When the B-device completes using the bus, it stops all bus activity. (Optionally, the B-device may turn on its D+ pull-up when a FS idle condition is detected on the bus.)

F) A-device detects lack of bus activity for more than TA_BIDL_ADIS min and turns off its D+ pull-up. Alternatively, if the A-device has no further need to communicate with the B-device, the A-device may turn off VBUS and end the session.

G) B-device turns on its pull-up.

H) After waiting long enough to insure that the D+ line cannot be high due to the residual effect of the A-device pull-up, (see Section 5.1.9), the A-device sees that the D+ line is high (and D- line is low) indicating that the B-device is signaling a connect and is ready to respond as a Peripheral. At this point, the A-device becomes Host and asserts bus reset to start using the bus.

When a B-device acting as a host suspends the bus (step E), this indicates that it is finished acting as a host. A disconnect could also be interpreted as step E, however, the occurrence of (or lack of) step G allows the A-device to differentiate between a suspend and a disconnect.

A B-device acting as a host will only suspend the bus when it is finished acting as a host.