64

USB storage devices, such as USB sticks and harddrives, are common to attach external storage to any kind of computer. How can the Raspberry Pi emulate an USB storage? The easiest application was to directly access the SD card via USB, but one could also provide acccess to files via Ethernet. I thought about the following layout for an "USB cloud stick":

Computer <---USB---> |Raspberry Pi| <---Ethernet---> Cloud, NAS etc.

The computer should only see a normal USB stick to read and write files from. The Raspberry Pi would act as programmable bridge that map directory listings and file accesss to request to a cloud storage. You could access cloud storage hosters with any computer (including black box media players) via USB without having to install any additional software on the computer. The Raspberry Pi USB bridge should also be able to encrypt/decrypt files on the fly, so one could store files encrypted in the cloud and access them on any device like a normal USB drive.

Edit: Existing products with similar but limited functionality include Wireless Media Stick and USB-over-Network. Access to files in the cloud may be possible by mounting virtual storage with cloud drive or similar software and encryption with TrueCrypt or EncFS - passwords would be stored on the SD card of Raspberry Pi only but one could access the storage with any computer without having to type in a passphrase on a untrusted machine.

13
  • 1
    I think what he wants is to have a partition or shared folder on his Pi that is mountable via USB on other machines. The idea being that you could plug the raspi into any computer and that folder/partion acts like a flash drive and can be written to without any additional software.
    – wmarbut
    Commented Jul 16, 2012 at 2:30
  • 4
    The Pi should only act as gateway between any computer, including black boxes, and for instance Amazon cloud. The gateway could also encrypt the files on the fly.
    – Jakob
    Commented Jul 16, 2012 at 6:57
  • 1
    Scrap that - this would be perfect! Commented Jul 17, 2012 at 8:22
  • 1
    @AlexChamberlain - no, you cannot. USB Mass Storage devices have a particular protocol which is supported by the host operating system. You can of course create a driver to make something else look like a sort of storage device to a particular operating system, but it would not be a "USB Mass Storage" device but rather a "custom storage device connected via USB" Commented Aug 1, 2012 at 21:47
  • 1
    @ppumkin: Because "mapping the drive in" or "plugging into the LAN" requires to configure the PC (or media player). Just plugging into an USB port does not require any administration and one does not need to expose any passwords to the PC. Note that I want to share storage from a cloud drive, which always requires passwords or even installation of client software.
    – Jakob
    Commented Nov 13, 2012 at 11:28

10 Answers 10

16

EDIT: This answer only applies to older Raspberry Pi devices. The Raspberry Pi 4 (and the Pi Zero) support USB Gadget Mode which allows what the OP is asking for. The rest of this post only applies to Raspberry Pi 1, 2 and 3 which do not have hardware support for USB Gadget Mode.


The problem is that the Pi's USB connection to the PC doesn't have the data pins connected - only the power pins. So you can't use this to speak USB because it's not wired up.

Your only option with the Pi would be to 'bit bang' USB using the GPIO pins, but this is very slow and potentially unreliable. I suspect you would only really be able to emulate a keyboard or mouse - anything higher bandwidth would probably be too much for the CPU to cope with, given the tight timing requirements of bit banging something like USB.

Another alternative would be to find a device that lets you connect two computers together via USB, to make a kind of network. But then you may as well just use the Ethernet connection...

11
  • 2
    I've previously played with a GP32 which can behave like a host even when plugged in a hub as a client. If the Raspberry can behave like a host on the USB-ports (not the power one) then a similar trick should be possible there too. Perhaps USB-to-go can do it? Commented Aug 1, 2012 at 22:06
  • 3
    Huh? You are mistaken by the micro usb port used for the power adapter. What about the 2 proper USB ports? With some emulation software it would be possible to plug that usb into a PC and emulate Storage - eg - Android phone when you plug in usb. - But there is not reason for that because you plug the Pi into your network, share your "cloud" folder via samba or whatever(wifi or lan)- and also at the same time do your cloud synching whatever it is you want to do. LAN can handle many request on various ports
    – Piotr Kula
    Commented Nov 13, 2012 at 11:08
  • 3
    @ppumkin: Why am I mistaken by the micro USB port? The data pins aren't connected, so you can't send data across it. The downstream "proper" USB ports are 'host' ports, so it is not possible to plug them into a PC without some kind of converter device in the middle. Software emulation is not enough, because of the way the USB protocol works. You might be thinking of Ethernet where you can plug any two devices together, but USB doesn't work like this. If you don't believe me, have a read of the USB specification, especially the parts about USB hosts vs peripherals.
    – Malvineous
    Commented Nov 13, 2012 at 23:49
  • 2
    Just wanted to add that "Bit-banging" USB is not an option. USB is a long, long way from something like I2C which can be bit-banged. Possibly the most practical approach is to use one of those AVR micros that has a USB device port built into it, and then communicate to it via the serial port on the Pi. (e.g. olimex.com/Products/AVR/Proto/AVR-USB-162 )
    – greggo
    Commented Jan 6, 2013 at 18:43
  • 2
    @Malvineous At 12 MHz or just 1.5 ? Clock period is [83.33+/-0.2 us] for 12 Mhz, and [666.6+/-10 us] for 1.5 MHz. Bear in mind you need to recover the clock on receive, which presumably means sampling at least 5 - 10 times the clock rate. Do these implementations allow interrupts while talking to USB devices?
    – greggo
    Commented Jan 17, 2013 at 22:48
9

Based on a quick perusal of the BCM2835 data sheet (http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf) chapter 15, it looks like USB OTG is supported in HW, so theoretically, it might just be a matter of SW to support the proposed scheme. I'm not a USB protocol expert (or even novice, really), but it would be pretty cool to have this functionality.

Looking at a description of the BeagleBone (http://elinux.org/BeagleBone), it appears to support something similar, although there is specific mention of a connector that supports this mode. It would be unfortunate if the Raspberry Pi is limited due only to connector selection.

Hope this is helpful.

1
  • 3
    Even if the SOC supports it, the LAN9512 hub device on a model B probably gets in the way of running the USB port "backwards" in device mode. On a model A (or if you remove that and install R37 & R38) you could get the SOCs USB port to the external jack - but then you would not have ethernet, and would only have the sdcard for potential storage. Commented Jul 31, 2012 at 0:08
8

This now seems to be possible using a PiZero - see https://web.archive.org/web/20210120154409/http://pi.gbaman.info/?p=699 for more.

2
  • 4
    Welcome to Raspberry Pi! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Ghanima
    Commented Jan 13, 2016 at 19:31
  • 1
    Case in point: this link no longer exists.
    – Seiyria
    Commented Feb 9, 2023 at 14:58
5

Unfortunately though this seems simple, as best as I can tell, it hasn't been done. See this thread and very detailed answer for more details

https://unix.stackexchange.com/questions/2683/serve-files-over-usb

And a Pi specific answer here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=8&t=4938

1
  • 2
    Thanks for the links. Looks like this device may do part of what I want, but its proprietary and only works in one direction (virtual read-only USB storage).
    – Jakob
    Commented Jul 17, 2012 at 6:58
4

If you aren't particularly concerned about performance, you could probably get a USB capable microcontroller with USB mass storage code (you could for example reprogram the STM32F103 debug interface on a $10 STM32F0 discovery eval module), carefully connect that to the pi's serial port as a back end, and run at several hundred kilobaud.

Better performance would likely be had by interfacing to the client PC via ethernet, but that would require a custom driver or different presentation to the host operating system - ie, you'd be a network attached storage device or share server.

Off the shelf file transfer cables were I believe already mentioned, but that would require appropriate software for the client PC and source-level linux driver support for the pi end.

1
4

I found out that Arduino can emulate a USB device, the Arduino Leonardo even out of the box. The LUFA USB stack can be used on Arduino and it implements a USB mass storage device driver (see this tutorial). This setup could be used to map USB to SCSI commands such as understood by the SD card reader. I don't know enough about SCSI but it looks like the SD card on Raspberry Pi and the USB-via-Arduino could be used together on the same bus. Sure this idea is not a full solution...

3
  • -1 Since the topic is about the Raspberry Pi and not Arduino.
    – user46
    Commented Aug 1, 2012 at 23:03
  • 2
    I know, this answer is no full solution! I hope that that questions about Raspberry Pi as a tool instead of an an end in itself are on-topic and broadening the view to related project helps. In this case I found this tutorial how to use an SD card as USB mass storage device. Maybe one could use an Arduino or another ATMEGA32U4 to get USB to the SIP bus which is also available on Raspberry Pi?
    – Jakob
    Commented Aug 2, 2012 at 8:13
  • 3
    No, you can't really share access to the sdcard, other than by electrical switching. Only one host can "mount" a given filesystem at a time (for the types of filesystems used on cards - network filesystems are different in this respect). Commented Aug 2, 2012 at 12:47
4

The Pi Zero and Pi Zero W are now available and support the Gadget interface (Kernal.org, linux-sunxi.org) allowing several different profiles, including USB Mass Storage and virtual networking.

The closest solution to the OP's request is to use a networking protocol between the Pi and the host PC, and then some other form of networking protocol from the Pi to the cloud/network provider. The Pi Zero W has builtin Wi-Fi, so could even connect directly to the cloud/network provider. Networking both of those links will be the easiest way to get the job done. There are many networking protocols and cloud providers available.

Trying to use USB Mass Storage would be a bad idea; you would have to write or rewrite a driver-level adaptation between USB Mass Storage and the Pi. You'd either start with MTP, or wind up with something similar. MTP has severe performance issues (Reddit, XDA, HowToGeek, Reddit) so I suggest the networking approach.

There should be lots of programs and projects in that direction. From the Pi's perspective, it's just acting like a fileserver/sync server, so any similar tutorial should get you started. Looking into things like Pi-based Seafile, Syncthing, SugarSync, and OwnCloud servers should get you started.

A few useful guides on the Pi Zero as a USB Gadget:

2

This comes down to how USB works, you see whenever two things are connected with USB there is all ways a USB host and a USB device and never the twain shall switch places. A USB host does all kinds of things that USB devices cannot do mostly to sync up data transfer between all devices connected to the USB bus. Check out the USB wikipedia page for more info.

What you're talking about doing is forcing two USB hosts (the Raspberry Pi and a computer) to communicate which unfortunately is simply not supported by the USB standard. There are some devices which can fake a data transfer between two USB hosts but as Malvineous mentioned you'd be better off using ethernet.

Your question specifically mentions using the Raspberry Pi to emulate USB storage but have you considered setting up the Raspberry Pi as a NAS? Using the Raspberry Pi as a NAS box basically does exactly what you asked for but instead of using USB it would be using your network. Here are great instructions on how to do this if you are interested.

3
  • Thanks for the pointers. So my questions is about how to implement a USB device using a Raspberry Pi. A NAS, however is not an answer to this question.
    – Jakob
    Commented Jul 25, 2012 at 7:12
  • 4
    If you want the Raspberry Pi to have the ability to be a USB device I suggest you investigate the products at ftdichip.com more than likely you'll be able to wire up a board that uses the Pi's GPIO pins to interface with an FTDI chip and that will give you USB device capability. You'll probably also need to write a driver to communicate with the FTDI device.
    – Dan B
    Commented Jul 25, 2012 at 13:36
  • Except that "the twain shall switch places" if they are USB OTG devices... Commented Jul 31, 2012 at 0:10
2

I can do it with my old Nokia N900 Phone using the usb gadget driver included with the stock Nokia kernel. The emulated device behaves exactly like a real one, you can even boot a PC from it.

1
  • 2
    Could you provide some details? You don't talk about just providing the storage at your phone via USB like most other smartphones can do, do you? As far as I understand, a USB gadget driver only provides a mapping from SCSI to USB, so one would still need to map SCSI to some cloud storage API.
    – Jakob
    Commented Nov 13, 2012 at 8:59
1

The USB standard are host based. That means that one device has to controll all communication with the connected devices. Clients can be a simple device or a hub. The hub is a host proxy for the clients connected to the hub, and communicates with the host (or hub) it's connected to.

As the original design of USB looks, you can't connect two hosts together (your PC and the RPi).

There are extentions though, USB On-The-Go, which allow a USB connection to see if it is connected to a host or a client, and adjust it's role depending on that. To this to work, you have to have support in the hardware. I don't belive the RPi is designed to do that.

For more information, look att http://www.usb.org/home or even http://en.wikipedia.org/wiki/Universal_Serial_Bus

In short, I don't belive that it can be done.

4
  • Another answer contradicts your lack of OTG support remark. Commented Jul 26, 2012 at 19:44
  • 1
    @Alex, look at the date stamps. Anders posted this in 2012, before the Pi Zero or A+ were available. Commented Nov 5, 2017 at 2:56
  • @YetAnotherRandomUser , look at the date stamps. I posted my response the day after Anders wrote his comment. Commented Dec 1, 2017 at 12:59
  • @AlexChamberlain and in 2012, there was not support for that for Raspberry and Rasbperian (or any other software I know of). So yes, in THAT time frame, I was right. The RPi Zero came many years later... Now we have support for it for RPi Zero, which is mentioned in another post, as you wrote.
    – Anders
    Commented Dec 4, 2017 at 10:50

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.