3

I'm wondering if it's possible to make a Linux system appear like an USB peripheral.

Like smartphones which can switch from master to slave depending on the device they are connected to, I would like to know if it's possible to have the same behavior with a Linux system.

For example, I have an embedded Linux on a card with usb connectors, when I plug a usb key to this card, the usb key is detected as a slave device and mounted on the file system. Now if I connect my card to my computer I would like to have it recognized as usb slave device too.

Do you think it's possible?

I found a similar question asked but not answered Use a Linux directory as a USB-OTG device to an Android phone?


I finaly chose to try to configure the system as MTP device instead of presenting it as mass storage for those reasons :

  • Protect against concurrent file access
  • Protect system against crash or corruption due to concurrent file access or bad mounting/unmounting
  • Possibility to expose root file system without unmounting or stopping it
  • Possibility to share multiple devices

As I have some problems configuring it I oppened a new question here for those interested.

  • 1
    The USB hardware on the 'card' must support OTG as well. From personal experience the modules are available when compiling the kernel IF the hardware supports it. – jc__ Aug 18 '17 at 14:23
  • Oh and on my embedded system the USB hardware DID support OTG, but it was not wired up on the the circuit board so I was unable to use it. – jc__ Aug 18 '17 at 14:25
2

Yes, you can, but it is not easy. You need at least a little work with it.

Working as an USB slave is supported in Linux since around the 2.4 or 2.6 times. You have to find a compatible chip, then a device having it, and then buying one, somewhere (typically, to rent it on the Internet).

Specifically for USB gadgets, there is support for peripherial devices and also for block devices.

Your Google search: Linux USB gadget . You may also have a little dig task in the kernel sources, this slave-side mode is likely not included in most distribution kernels, so you will have to recompile it.

Here is an old, but still actual reference about this. Other may be useful answer.

  • 1
    Thanks for the links, it makes it a lot clearer for me. The Mass Storage Gadget seems interesting but it need a separated file or partition when I need to access the root file system. Maybe it's possible to makes the system act like an MTP device to the host? – Arkaik Aug 18 '17 at 13:56
  • @Arkaik You can give your root filesystem to it, although it is a little bit dirty. If you won't crash your root fs, I suggest to export it only readonly (for example, losetup -r /dev/loop0 /dev/sda1, and then exporting /dev/loop0). It will be buggy, but it will mainly work. For a filesystem-level sharing, this may be interesting (will work only with Android). Search for wtp linux gadget on the google. – peterh Aug 18 '17 at 14:02
  • @Arkaik There is even a user space USB gadget api in Linux. – peterh Aug 18 '17 at 14:03
  • Mount it as a read-only loop device could be nice but I will not be able to write files on the card. I'll dig around this USB gadget API and come back ;) – Arkaik Aug 18 '17 at 14:40
  • @Arkaik Android can do this, so the gadget exists. I am not sure if it is in kernel space. – peterh Aug 18 '17 at 14:45

Your Answer

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

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