Problem: How can I detect when the gadget is or is not connected to a host from the gadget itself?

Background: The g_mass_storage Linux kernel module can be used to provide a mass storage device via a USB OTG port. I would like to use this to allow for file transfer over a USB to a device running Linux and this module.

Unfortunately this is not directly supported my the module, having both client and host access the storage can be flaky at best. But I could avoid this by controlling the Linux machine (USB client) so that is only reads of writes from the storage when the USB host is not connected.

One very naive solution would be to parse the dmesg output. That at least clearly tells me when it is plugged in, but not when it is removed.

My research shows that somewhere in /sys/ there is a gadget status flag, see this chain, but I'm not sure where.

Going the other way around, plugging the gadget into a Linux machine, I know I could use udev, but udev monitor does not seem to say anything about the state of the gadget on the gadget itself.

1 Answer 1


You may try looking at /sys/class/udc/YOUR_UDC/state

See also https://www.kernel.org/doc/Documentation/ABI/stable/sysfs-class-udc for more details on sysfs API.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .