Firstly, many thanks to ED6E0F17, notro and others on github who worked this all out. The original thread is here. I am Dave-0 in that thread but I didn't do the clever bits.

This is very new and a bit rough and ready so please chip in with corrections and improvements. Any mistakes are most likely to be mine.

To enable USB device mode (also referred to as peripheral mode or gadget mode) on a Pi Zero follow the steps below. If you are using device mode you can't plug in a keyboard or mouse to control the Pi. Because of this, I advise using a 3.3v USB-serial adapter connected to the 40 pin header when trying this out, that way you can get a shell. The following steps assume you are using a Linux host system and that you are modifying a Raspbian install. I won't be going into detail on compiling kernels as that's already documented here.

1. Download the 4.4.y branch of the Pi kernel.
You get this from github:

There is also a working .config here but this one has all modules switched off so you might not want to use that one.

4. Compile and install
Compile and install your new kernel, bcm2708-rpi-b.dtb and modules on to your Raspbian card. Take a backup first unless you're not bothered about losing anything if it goes wrong. You will also need to replace the standard config.txt with this one:

That's great, and well done getting it going. Does your kernel switch between client & host OK when plugging in an OTG adapter (I'm assuming you will see the old issues we had with USB in host mode, since fixed with the FIQ magic)?

gregeric wrote:Does your kernel switch between client & host OK when plugging in an OTG adapter (I'm assuming you will see the old issues we had with USB in host mode, since fixed with the FIQ magic)?

No, I can only get dwc2 to work in device mode if I compile it with the "Gadget only mode" option, regardless of which type of cable is connected.

I've not had any of the issues with USB lock ups yet but in device mode it's only got to handle the traffic of one device rather than the hub+ethernet+mouse+keyboard+whatever it would be dealing with in host mode.

Yip, and just to add, the top one over on https://gist.github.com/gbaman/50b6cca61dd1c3f88f41 does include full standard USB support which continued to work, even when gadget mode drivers are loaded.
Included is serial, ethernet, mass storage drivers along with a few others like HID and MIDI, just need to load them.

Lead developer of PiNet, a free and opensource centralised user accounts and file storage system for Raspberry Pi classrooms used in over 200 schools across the world.
http://pinet.org.uk

I have a Raspberry Pi Model A (and not Zero or A+) and I then need files that are hard-coded to gadget mode, because the OTG-ID-pin is connected to ground, i.e. always host. Is there any one that has files that work on Model A. I have a suitable cable (i think -- it works for powering): USB AM/AM: http://se.farnell.com/pro-signal/psg900 ... dp/1494745

mob-i-l wrote:I have a Raspberry Pi Model A (and not Zero or A+) and I then need files that are hard-coded to gadget mode, because the OTG-ID-pin is connected to ground, i.e. always host. Is there any one that has files that work on Model A. I have a suitable cable (i think -- it works for powering): USB AM/AM: http://se.farnell.com/pro-signal/psg900 ... dp/1494745

If it's mass-storage you are looking for, then you can use the existing tools published for the Compute Module

I've verified those on a Model A+ with a cable similar-looking to that in your link.

To get it to work, remove the SD card & power cable from the A. Now connect the Pi to the host PC with your cable - your Pi will now be powered from the host PC, do not connect the power cable to the Pi. Next insert your SD card. Run the rpiboot program, & shortly the mass storage device will appear under Windows or Linux PC.

mob-i-l wrote:I have a Raspberry Pi Model A (and not Zero or A+) and I then need files that are hard-coded to gadget mode, because the OTG-ID-pin is connected to ground, i.e. always host. Is there any one that has files that work on Model A. I have a suitable cable (i think -- it works for powering): USB AM/AM: http://se.farnell.com/pro-signal/psg900 ... dp/1494745

If it's mass-storage you are looking for, then you can use the existing tools published for the Compute Module

I've verified those on a Model A+ with a cable similar-looking to that in your link.

To get it to work, remove the SD card & power cable from the A. Now connect the Pi to the host PC with your cable - your Pi will now be powered from the host PC, do not connect the power cable to the Pi. Next insert your SD card. Run the rpiboot program, & shortly the mass storage device will appear under Windows or Linux PC.

Not very convenient, but it works (also on the Zero).

I tried, but it didn't work so far. No SD-card in the Pi A was noted. I tested both Raspberry Pi B+ and 2B as hosts with most updated Raspbian Jessie (I also run sudo rpi-update). I compiled and installed rpiboot on both hosts. (I moved the host microSD between the hosts.) I tested both 8GB and 16GB SD-cards.

@mob-i-l does that uSD have max_usb_current=1 in config.txt, might it be a power issue? It should re-enumerate serial=1, then the msd.elf gets squirted across & executed. Unlike the linux module method, this gives access to the whole SD. I haven't disassembled them but I guess usbbootcode.bin & msd.elf are both threadx/vc4, not running on the arm.

I'm getting confused, aren't we talking about two different things here? The USB device mode that the Pi boots into if no SD card is present is completely separate from USB device mode support under Linux. Even if you use the usbboot program you still need the dwc2 driver and the gadget driver loaded.

Yes, they are two different things, which is why I apologised for the thread derail. I don't believe linux / dwc2 is involved at all - it's all done under threadx, the OS that the VC4 runs hidden behind the scenes. If my hunch is correct, a disassembly of msd.elf would reveal no arm/linux code, instead vc4/threadx.

I've been trying to get the usb gadget stuff working on a model A and A+ using the serial gadget driver. Unfortunately while the driver loads and creates the device, it is not recognised when connected to a PC host (windows & linux). Using the same SD card in Pi Zero it is recognised as a serial device by the host and can be connected to.

The kernek is configured for usb gadget only mode and teh debug options have been enabled. g_serial has been compiled as a module rather than a builtin and loaded via rc.local as follows: