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 '19 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 '19 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 '19 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.