Device: Raspberry Pi Zero W v1.1

State: Pi Zero is running in gadget mode, acting as a USB Mass Storage device (seen by / mounted on a MacBook Pro).

Question: What is the best way to detect whether or not the MacBook Pro is still connected / using the Pi as a Mass Storage device?

Ideally, either by polling or event trigger, I would like to know (within about ~5 seconds) if the User has either ejected the USB device or shut down the MacBook Pro ... at which point, I'd be able to execute some shell commands.

There is a similar question here, but the author asks about collecting information about the connected host, which is unnecessary in my use-case.

I've tried using:

cat /sys/class/udc/20980000.usb/state ... and, while it seems to pick up the change from not attached ==> configured (when I plug Pi into my MacBook), it doesn't pick up the change when I eject/disconnect (it seems to stay stuck in configured and never "drops back down" to not attached). This seems to be related to a known issue (mentioned in here).

  • Would the change to the state file not require some input from your Mac?
    – Seamus
    Apr 2, 2019 at 15:35
  • @Seamus, I’m not sure what your asking. If you look at the article I reference, the behavior (not toggling back to “not attached”) is / was not correct. I contacted the author and he did eventually create a patch, but I don’t know how to apply it to the Linux kernel (currently working to learn how).
    – Dan
    Apr 2, 2019 at 15:37
  • I was mostly curious; never really thought much about how the 'plugged in device` sees this. Initial thought was that only the 'plugged into device` had visibility of status, but apparently that's not so! Wrt a patch finding its way into the RPi kernel anytime soon, my advice is "don't hold your breath". I submitted a bug report once & it seems a black hole from my perspective. But perhaps @dougie has better insights? You might try reaching out to him.
    – Seamus
    Apr 2, 2019 at 15:56

1 Answer 1


Run udevamdin monitor on your Raspverry Pi Zero.

You can monitor the event.

So it would help if you did research udev things...

  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 18, 2022 at 12:07

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.