How can the Pi detect if a host device has been connected, and gather information about that device, when being used as a gadget (such as mass storage). It would be useful if I could detect this within a python script.

As an example, if the Pi is to be used as a mass storage device, and I have a python script that's supposed to set that up, it would be useful if I could trigger certain behaviour when a device is actually connected, but not before. Further, it would be useful if the behaviour could depend on certain information about that device. For instance, if the data needed to be refreshed when a host was connected, or if a certain type of host needed a different set of data than another type of host.

  • I have made an answer but it was wrong. I was only on the g_ether path and checked network connection. So I deleted it.
    – Ingo
    Commented Apr 4, 2019 at 21:11

1 Answer 1


Try checking /sys/class/udc/<udc>/state:

Indicates current state of the USB Device Controller. Valid states are: 'not-attached', 'attached', 'powered', 'reconnecting', 'unauthenticated', 'default', 'addressed', 'configured', and 'suspended'; however not all USB Device Controllers support reporting all states.

Apparently, this only changes to configured during the first connection, and then it remains configured even if you disconnect it. But it should at least allow you to detect the initial connection event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.