DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

USB device mode on Pi Zero - possible? EDIT: Yes, it is!

Sun Dec 13, 2015 6:50 pm

I've been digging around on the forum for a couple of days now trying to find out if it is possible to get the Pi Zero to act as a USB device instead of a host under Linux.

As far as I can tell, the Zero is capable of doing this at a hardware level but the dwc_otg driver doesn't work in device mode and has been hard coded to operate in host mode only. Up until now this hasn't been an issue because the various "B" models couldn't do device mode anyway as the SOC's USB data lines are connected into the hub/ethernet chip and the "A" models would need a non-standard USB cable. The compute module apparently can do USB device mode but not under Linux.

Can anyone tell me whether I'm right about this? If the problem is with the driver I would be happy to help out with a solution although this would probably be more on the testing side; I've done a bit of Linux driver development a few years ago but USB drivers are a bit beyond me.

EDIT: Yes, device mode is possible - see this thread on github for the details.
Last edited by DaveB on Sun Dec 20, 2015 10:19 pm, edited 2 times in total.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: USB device mode on Pi Zero - possible?

Sun Dec 13, 2015 8:29 pm

As you note, with the advent of the zero this may be possible, and RPiF employee dom appeals here for help to get it working: viewtopic.php?f=63&t=127003&start=50#p850028

On the pimonori youtube video blog it was said recently they'd seen people @ RPiF working on it so fingers crossed. Sorry no link & I ain't watching it all again!

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible?

Sun Dec 13, 2015 9:48 pm

gregeric wrote:On the pimonori youtube video blog it was said recently they'd seen people @ RPiF working on it so fingers crossed. Sorry no link & I ain't watching it all again!
Thanks, I didn't know that. I've found it now so here's a link for anyone else who's interested: https://youtu.be/_PNx3BXTTss?t=12m34s.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: USB device mode on Pi Zero - possible?

Sun Dec 13, 2015 9:58 pm

Good sleuthing!

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible?

Sun Dec 13, 2015 10:23 pm

gregeric wrote:As you note, with the advent of the zero this may be possible, and RPiF employee dom appeals here for help to get it working: viewtopic.php?f=63&t=127003&start=50#p850028
I've done a bit of tinkering with this now. If I revert this commit as suggested by dom in the linked topic, it hangs on boot repeating the message "Mode Mismatch Interrupt: currently in device mode". However, if I also add "dwc_otg.fiq_enable=0 dwc_otg.fiq_fsm_enable=0" to the kernel command line (also hinted at in the linked topic) the Pi boots and the host detects it as a USB device but can't enumerate it.

How far this is actually getting me I don't know but it's interesting.

(I'm accessing the Pi using the serial pins on the header when doing this).

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: USB device mode on Pi Zero - possible?

Sun Dec 13, 2015 10:30 pm

I'm sorry that I can't help you any more, it's far beyond my knowledge, only ever having exploited (as a user) the USB client mode that teensy & mbed offer. Good luck with your endeavors.

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 8:32 pm

gregeric wrote:I'm sorry that I can't help you any more, it's far beyond my knowledge, only ever having exploited (as a user) the USB client mode that teensy & mbed offer. Good luck with your endeavors.
No worries, thanks for the pointer to the Pimoroni blog, at least I know the RPF devs are looking into this. It's out my league and from what I've read, that particular driver is a bit "difficult", to put it politely. ;)

If any RPF devs are reading this, my offer to help with testing still stands.

mattmiller
Posts: 2247
Joined: Thu Feb 05, 2015 11:25 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 8:58 pm

If this can be made to work - it would be great boost for headless working :)
I wish I knew more about low-level stuff to help but by golly - if your working on it - send me your coffee bill :)

Matthew

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 9:19 pm

mattmiller wrote:If this can be made to work - it would be great boost for headless working :)
That was what I was thinking: use the Zero as a sort of super powerful Arduino. It would be great to be able to do this without needing a USB to serial converter which, for the Zero, would double the cost.
mattmiller wrote:I wish I knew more about low-level stuff to help but by golly - if your working on it - send me your coffee bill :)
As I said, I'm out of my depth on this, but apparently the foundation's devs are working on it. Maybe they could send you their coffee bill? :)

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 27444
Joined: Sat Jul 30, 2011 7:41 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 9:37 pm

Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 10:03 pm

jamesh wrote:This may be of use.

https://github.com/raspberrypi/linux/issues/1212
Thanks, that's interesting. I've now tried plugging the Zero in with no SD card and a normal (non-OTG) USB cable plugged into the USB port. The host machine (Linux desktop) detects it as a USB device:

dmesg:

Code: Select all

[ 6764.150505] usb 6-1: new full-speed USB device number 2 using ohci-pci
[ 6764.317549] usb 6-1: New USB device found, idVendor=0a5c, idProduct=2763
[ 6764.317560] usb 6-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6764.317567] usb 6-1: Product: BCM2708 Boot
[ 6764.317573] usb 6-1: Manufacturer: Broadcom
lsusb:

Code: Select all

Bus 006 Device 002: ID 0a5c:2763 Broadcom Corp. 
So this must be boot-over-usb mode and the usbboot program mentioned in the link is used to send a binary to the Pi to boot (I've not tried it).

This shows that the hardware can do device mode but we still need a working device mode Linux driver in order to be able to do anything other than boot the Pi.

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: USB device mode on Pi Zero - possible?

Mon Dec 14, 2015 10:34 pm

When I saw this, I needed to test it. Guess what! It is showing up. :P
That's one step closer to a raspberry pi USB device. :)

Laurens
P.S. I found this post where the compute module has the same usb name as the zero. (Maybe a lead ;) )

gadgetoid
Posts: 152
Joined: Wed Mar 07, 2012 9:58 pm

Re: USB device mode on Pi Zero - possible?

Tue Dec 15, 2015 7:47 pm

I tried using rpiboot to transfer a baremetal binary I compiled. Doesn't seem to do anything yet, but I'm not well enough versed in baremetal to know if it's the upload process at fault, or just a bunk binary that I'm uploading. I also don't know how big a binary can be at this stage, where it's stored, where it's run, how much control it has over the system, what initialisation it might need to do... and so on and so forth. I'd rtfm, but I just don't have time.

Here's the verbose output:

Code: Select all

$> sudo rpiboot -v
Waiting for BCM2835 ...
Found serial = 0: writing file ./usbbootcode.bin
libusb_bulk_transfer returned 0
Writing 65972 bytes
libusb_bulk_transfer returned 0
Successful
Waiting for BCM2835 ...

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible? [EDIT: Yes, it is

Sun Dec 20, 2015 7:40 pm

DaveB wrote:
jamesh wrote:This may be of use.

https://github.com/raspberrypi/linux/issues/1212
It is! The people contributing to that thread (mainly ED6E0F17 and notro) have worked out a way to use the dwc2 driver to get device mode. I've done a quick test and got it to work as a serial gadget.

I'll post some more details when I've done some more testing.


ejolson
Posts: 6032
Joined: Tue Mar 18, 2014 11:47 am

Re: USB device mode on Pi Zero - possible? [EDIT: Yes, it is

Mon Dec 21, 2015 3:42 am

DaveB wrote:I've done a quick test and got it to work as a serial gadget.
What about as a USB Ethernet gadget? Last month I suggested a cluster could be built by connecting a bunch of Pi Zero's running in device mode into a powered USB hub. The hub, in turn, could then be connected to a Pi 2B host which would then serve as a router and head node for the cluster. Standard TCP/IP based cluster software such as slurm, MPI and Hadoop could be deployed by using the gadget driver to make each Pi Zero appear as an Ethernet device connected to the Pi 2B. I don't have any Pi Zero's to experiment with, but would be very interested if someone could setup such a cluster.

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible? [EDIT: Yes, it is

Mon Dec 21, 2015 11:35 am

ejolson wrote:
DaveB wrote:I've done a quick test and got it to work as a serial gadget.
What about as a USB Ethernet gadget?
That should work but I've not tried it yet.

DaveB
Posts: 29
Joined: Wed Sep 14, 2011 8:40 pm

Re: USB device mode on Pi Zero - possible? EDIT: Yes, it is!

Mon Dec 21, 2015 2:33 pm

I've written up how to enable device mode here. I thought that the advanced section was a better fit for it rather than continuing this thread in GD.

mimi123
Posts: 583
Joined: Thu Aug 22, 2013 3:32 pm

Re: USB device mode on Pi Zero - possible?

Mon Dec 21, 2015 4:39 pm

gadgetoid wrote:I tried using rpiboot to transfer a baremetal binary I compiled. Doesn't seem to do anything yet, but I'm not well enough versed in baremetal to know if it's the upload process at fault, or just a bunk binary that I'm uploading. I also don't know how big a binary can be at this stage, where it's stored, where it's run, how much control it has over the system, what initialisation it might need to do... and so on and so forth. I'd rtfm, but I just don't have time.

Here's the verbose output:

Code: Select all

$> sudo rpiboot -v
Waiting for BCM2835 ...
Found serial = 0: writing file ./usbbootcode.bin
libusb_bulk_transfer returned 0
Writing 65972 bytes
libusb_bulk_transfer returned 0
Successful
Waiting for BCM2835 ...
compile VC4 code, not ARM code

Return to “General discussion”