raspberrypi / linux

Kernel source tree for Raspberry Pi-provided kernel builds. Issues unrelated to the linux kernel should be posted on the community forum at https://forums.raspberrypi.com/
Other
11.14k stars 4.99k forks source link

RPi zero Support for USB Gadget Mode (pi shows up as keyboard, disk, net, camera etc) #1212

Closed hh closed 8 years ago

hh commented 8 years ago

Having a $5 usb gadget that could function as a combination of anything in https://github.com/torvalds/linux/tree/master/drivers/usb/gadget/function would be pretty grand.

Think about your phone, how it works to connect a micro-B to charge and connect to it as a gadget to your computer, then when you connect a micro-A cable you plug mice, keyboard, and thumb drives into your phone.

As a usb host, you can plug a phone into it (just like you do your dekstop or laptop) and flash it. Format a usb drive, or bootstrap embedded devices like intel edison.

As a usb gadet, you can plug it into a computer and have it show up as a usb keyboard, disk, network, and other interesting things. (send some keystrokes to hit F12, and boot to a usb installation disk).

I'm looking to see if we can't get the RPi zero to function as both.

It looks like it may have worked at one point: https://github.com/raspberrypi/linux/issues/881#issuecomment-161411866

I just wanted to gauge the interest.

NicoHood commented 8 years ago

I got this one: http://www.amazon.de/gp/product/B00CES44EO?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s01

TS32GUSDU1

I will try the option. Also is there any difference between the ladyada build and the one from gbaman?

pelwell commented 8 years ago

Yes, that card has been problematic for some people. I have one on my desk (the packaging says TS32GUSDI1E) that so far has been OK, but I have a Verbatim card that fails for me. I'm not saying these are bad cards, but there is something about them that the sdhost driver doesn't like/understand.

procount commented 8 years ago

I tried using Gadget mode for the first time today. My first attempt used Meltwater's Pi-Kitchen to install it with NOOBS, but I got the same SD card corruption as Nico. I think that version used LadyAda's build. AFAIK my card is an Integral 16GB. In Ubuntu disk utility it shows up as a 'Generic STORAGE DEVICE'. The markings on it indicate it is SDHC IU1 (where the 1 is inside the U) So I tried again using this build https://dl.dropboxusercontent.com/u/1122948/temp/PiOTG-Test/PiZeroCombined.tar.gz (from Gbaman?) and it worked. I only loaded the g_ether module, but I got ssh and vnc working.with Ubutu14.04 running in Vmware on a Win7x64 laptop. (I couldn't install the correct g_ether driver under win7x64 for some reason) Another problem I had though was that the Ubuntu usb0 device kept losing its ip address. In a separate terminal I had to type 'sudo ifconfig 192.168.4.1 netmask 255.255.255.0 up' and then the vnc and ssh connections would spring into life again from where they left off. This repeatedly happened at a rate of less than 1 minute intervals.

ED6E0F17 commented 8 years ago

@procount - I had the same issue running Linux 4.3 on Ubuntu; an upgrade to 4.4 fixed it.

NicoHood commented 8 years ago

I tried again with @gbaman s tutorial. It should be noted that you need to add this after step 6 of its tutorial. Otherwise it will overwrite the setting again. it now seems to work and i can start playing with this.

Edit: Works great, I can just use all stuff with my pi. Just open the network options of ubuntu and select "shared network" under ip v4. I used nmap to find the device and now i can ssh into it. only usb is connected. Its also powered via usb. Awesome!

nmap -T4 -F 10.42.0.255/24
ssh pi@10.42.0.x

# or if avahi-daemon is installed:
ssh pi@raspberrypi.local

If i copy a file to my pi via ssh (scp) it transmits at 6mb/s which is totally fine for me.

Another question: If i power the pi zero via extern power supply (like you are used to), does it hurt if the laptop also powers the pi via the OTG usb port? Is there any protection between those two inputs? Otherwise I have to power my pi only via usb otg (meaning i cannot simply replug a keyboard on the fly).

procount commented 8 years ago

@ED6E0F17 - But the Rapbian kernel I downloaded for Gadget mode is already 4.4.0-rc6=#14 and Ubuntu on my PC is 3.3. I also got Nico's SD card errors again after a reboot with this kernel, so it's not totally solved. But after another reboot it worked fine. So it's not SDcard corruption, but an intermittent access problem.

NicoHood commented 8 years ago

Have you addeed the patch dtoverlay=mmc after applying the kernel? Because this will overwrite config.txt. I had to rewrite it again. Now it works fine for some time now. I'd really like to see this (without the sd card patch XD) in the official release.

procount commented 8 years ago

No, but I have now, so we'll see how it goes.

ED6E0F17 commented 8 years ago

I have no issues with the RPI kernel, but I get errors on the host end. (x86-64 Ubuntu 14.04.3 with linux 4.4-rc8) :

[224599.070811] cdc_ether 1-1:1.0 usb0: register 'cdc_ether' at usb-0000:00:1d.7-1, CDC Ethernet Device, 8e:f6:a8:3e:1e:9f
[224599.072720] cdc_acm 1-1:1.2: ttyACM0: USB ACM device
[224599.216681] cdc_ether 1-1:1.0 usb0: kevent 12 may have been dropped
[224599.218452] cdc_ether 1-1:1.0 usb0: kevent 12 may have been dropped

Before upgrading from linux 4.3.0 the dropped events continued after the physical connection, and the network connection was unstable.

procount commented 8 years ago

Pleased to report I fixed my loss of IP address in Ununtu. Despite setting an ip address with ifconfig, dhclient insisted on trying to get another one, and when it didn't get a dhcp response it reset the ip address. Setting a static ip address in interfaces and rebooting fixed it.

NicoHood commented 8 years ago

Is there any information about how the power rails are wired on the PCB?

Because connecting the PI to the usb host only via usb (device mode) is not a good idea, as you might want to unplug the device (but will lose the power then). So you want to self power the raspi. The question is, if the current power circuit protects against that. Connecting both VCCs would be a bad idea.

If not it might be a good idea for @ladyada or @nseidle to sell Micro USB Data Only cables where the 5V VCC is left out (very simple) or broken out as 2nd cable.

With an additional power in selector cable (like on an arduino board) you could connect the 2nd power cable and an external power supply. Like on an Arduino the highest voltage would be used. But the first solution would be more simple (and cheap) for mass production.

Other solutions would be to create a pcb with a 2nd micro usb port that filters vcc as i described it (and adds an external power supply). However it would make more sense to have a simple cable without vcc.

So does anyone has an idea about this?

mindforger commented 8 years ago

as far as i have looked into the board and tested around, both ports vcc are shorted by a 0Ohm resistor but there are pads for a circuitry that may help and i have read about this circuitry already somewhere in the web but can't remember where or what

what exactly would be "bad" when i power the pi zero from a powerbank or a powerplug and then connect it directly to the computer USB ? And by the way i am missing on how to"configure" the g_multi mode ... it's only stated "you can" but not "how" .. sorry i am a noob to this still :)

NicoHood commented 8 years ago

Why it is dangerous? Point 5: http://electronics.stackexchange.com/questions/67149/what-are-the-most-common-ways-to-fry-an-arduino

Because they can have different potential. Or they always have. And this way you destroy either the pi, the pc or the pi PSU. Or all together. It may take some time to see an actual effect, but I did that once. NEVER connect Vccs together.

Thatshwy this question is essential when using the USB Device mode. Currentlz we can only use it, when powered via the PC HOST and not attached to normal PSU. But connecting to the PC does not give us enough current to run the pi stable. At least we should not do it like that.

mindforger commented 8 years ago

soooo if i were going to desolder the 0ohm in this picture right above the printing "PWR IN" https://learn.adafruit.com/assets/28756 the data port should not have an vcc connection, only ground and should be safe?

but will it work without a vcc connection?

Ferroin commented 8 years ago

For what it's worth, I actually have run a Pi 1 Model B fully stable from USB ports on a computer before. They were USB 3.0 ports designed for charging phones (which is part of the USB 3.0 standard, and they can source 2A at 5V usually, occasionally up to 2.1A), which are becoming more and more common these days.

The other problem with running from a PC USB port is that they aren't consistent (just like the USB PSU's). I've seen some that can run a Pi just fine, perfectly stable, 100% load 24/7 for weeks with no issues. I've also seen some that are so strictly compliant with the USB standards (which require devices to draw minimum current until they negotiate higher power consumption) that you can't even hope to boot a Pi powered from them. Most fall somewhere in-between, and start to lose voltage once you get to around 450mA of current draw, but will still let you draw the full 500mA without negotiation (because there are so many USB 'gadgets' that make no data connection and just use the port as a power source).

mindforger commented 8 years ago

i was lucky to have bought a y-cable as i thought this could be usefull .. now it is, i only have to connect both ends when powering it from the PC and 2 seperate wire when powering it from the PSU while connected to the PC

Ferroin commented 8 years ago

Tying the grounds together can be dangerous as well. The computer should have protection to handle this, but some don't, and I'm almost certain the Pi doesn't. Unless the devices are electrically grounded to the same ground plane, they will almost certainly have variance in voltage (usually it's millivolts, but sometimes it's more, and that can be problematic). This is why so many standards for communications require a ground line in addition to the data lines.

NicoHood commented 8 years ago

As I said this will kill your device and/or PC X_x Also Y Cables are bad practice in general too.

Edit: Desoldering the 0 ohm could be an idea though. But this would break usb host devices without a powered hub. But a powered hub should be used anyways. So can anyone verify that this 0ohm really connects those 2 vccs together?

mindforger commented 8 years ago

i measured it with two usb cable and there are several guides in the web stating the same, and also which parts you would have to add to make the OTG regulator part working

also i want to work headless with it, just the data port as an easy access ssh via network in parallel with mass storage for easy file transfer from my laptop or tablet (if i get the driver working) ... hope the next batch of rpi zeros will be deployed soon and i get a hold of one or two more -.-

NicoHood commented 8 years ago

What have you tested? That the lines are correctly separated if you remove the resistor? I don't get the OTG question. OTG is a 5th pin that is grounded by the OTG cable and has nothing to do with the power supply.

Assumed you broke the resistor and it works as described: As Device: It is self powered

As Host: Use an active hub.

mindforger commented 8 years ago

i desoldered the resistor and the data port Vcc wasn't connected anymore in fact i did not find any part (so far) of the board that was connected to the USB data port's Vcc it looks to me that it is completely isolated then

with this rsistorsize and pad distance you can easily bridge it back with just some solder if required

with OTG regulator i mean the left out parts right above the resistor bridge, if you just google it for a second you get the part list containing everythign you need to make the data port a real OTG port with power limitation and short circuit and overcurrent protection, but you also only get the very limited power supply of around 40mA or so, so you still need to use an active hub rendering the whole circuit a bit useless ... or i miss the technical benefit or this circuit in this particular case

NicoHood commented 8 years ago

That sounds nice, if the regulator can detect vcc inputs via usb and then safely disconnects the pis vcc. It would at least work to connect an usb stick (as this is the original intention of otg for smartphones etc). I could not find anything online. It would be highly appreciated if you could post further information on that. It'd be also cool to know what the other unused, exposed pads (on the bottom) do.

mindforger commented 8 years ago

the pads for the connector at the bottom is a jtag interface (J5).. but guess what the site i got my info from is missing or offline, i can't find it anymore XD the other pads are several testing points https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=89522 https://docs.google.com/spreadsheets/d/15kHcow993d_2CS6rZxQMq0wAWlgWNaHU_go1Hyf08ck/edit#gid=0

but the heck i don't find the site anymore for the regulator

just found another intersting source http://hackaday.com/2015/04/06/more-power-for-raspberry-pi-usb-ports/

looks like exactly the same circuit and also named U13

fantom-x commented 8 years ago

I have managed to make Pi Zero work with Windows as an USB Ethernet gadget: I can ssh into it and then from the Pi I can access the internet. I am having much less luck making this work with Ubuntu 16.04: I can SSH into it alright, but it just does not want to connect to the Internet. Is there a way to make it work with Ubuntu just like it works with Windows?

lurch commented 8 years ago

Have a look at http://blog.gbaman.info/?p=791 and https://www.google.co.uk/search?q=linux+ip+forwarding

lurch commented 8 years ago

...and looks like some of the info is out of date, but there's also https://help.ubuntu.com/community/Internet/ConnectionSharing

mindforger commented 8 years ago

DISREGARD MY POST ABOVE when using the 1.3 version with camera connector you can not disconnect em anymore, they are now connected by a pretty fat line

fantom-x commented 8 years ago

@lurch, thank for the suggestions. Here is how I made it work:

  1. NetworkManager in Ubuntu creates a new connection "Wired Connection 1" or another number: on IPv4 tab chose "Link-Local Only". Once a connection is established, the pi can be accessed by ssh pi@raspberry.local
  2. Use ifconfig to get the address of this new connection 169.254.X.X; in my case it was 169.254.202.41
  3. On the host run echo 1 > /proc/sys/net/ipv4/ip_forward
  4. On the host run iptables -t nat -A POSTROUTING -s 169.254.0.0/16 -o br0 -j MASQUERADE. Replace br0 with your interface name.
  5. SSH into pi and add a default route: route add default gw 169.254.202.41 usb0. The gateway is the IP address of the host link (see item 2 above)

After that was done, I was able to ping my router and google servers by IP.

fantom-x commented 8 years ago

Anyone knows how to make DNS work in Pi Zero when it is connected to a Ubuntu PC? I can ping any website from the pi, but cannot figure out how to make name resolution work...

Update: my firewall (ufw) was blocking routed packets.

lurch commented 8 years ago

Anyone knows how to make DNS work in Pi Zero when it is connected to a Ubuntu PC?

I don't know if it's the "proper" way, but I was able to get name resolution working by finding out the IP address of my router's DNS server (I got this from the "Network Manager -> Connection Information" dialog on Ubuntu, and in my case it's 192.168.2.1) and on the Pi Zero (over the SSH connection) I did echo "nameserver 192.168.2.1" | sudo resolvconf -a usb0 and I can then successfully run apt-get update etc. :-)

lurch commented 8 years ago

I've knocked up a simple script to automatically setup internet access for a USB-network-connected PiZero: https://gist.github.com/lurch/ad939bbce48064cffdb215268eac9f62

fantom-x commented 8 years ago

@lurch, once you ssh into the gadget for the first time you can use arp -n to get your host's IP address. In this case your script could become a one liner...

hex007 commented 8 years ago

@lurch Have you tested the script. I am unable to get it to run even when i follow all instructions on your blog post and from your script

  1. Flashed Jesse lite
  2. Edited 2 files in boot partition
  3. from ubuntu 15.10 x64:

    a. Changed the connection to link-local only b. Interface is called: enx96fcc47029dc (strange name) c. Pi connected. Can ssh successfully d. echo 1 > /proc/sys/net/ipv4/ip_forward e. sudo iptables -t nat -A POSTROUTING -s 169.254.0.0/16 -o br0 -j MASQUERADE f. SSH into Pi

  4. From pi:

    a.sudo route add default gw 169.254.9.197 usb0; echo \"nameserver 192.168.0.1\" | sudo resolvconf -a usb0 b. ping 8.8..8.8 ---> nothing, all packets dropped

Am i missing some step?

lurch commented 8 years ago

I only tested on Ubuntu 14.04, but I can see a couple of small errors in what you've done above:

  1. I assume you did step 3d as the root user?
  2. Your iptables command should probably be using eth0 or wlan0 rather than br0
  3. In step 4a make sure you're giving the IP address of the PC-side of the USB-network connection and not the IP address of the Pi-side of the USB-network connection
  4. If running the commands directly on the Pi itself, you don't want to escape the quotes, so change it to echo "nameserver 192.168.0.1" | sudo resolvconf -a usb0

BTW I'm actually a different Andrew than @gbaman (who wrote the blog article) ;-) As you'll see in one of my comments on http://blog.gbaman.info/?p=791 I recommend configuring the Zero to assign static MAC addresses to both sides of the USB-network device, so that you don't have to keep reconfiguring the PC-side network device every time you reboot the Zero. My script makes no 'permanent' changes, so you need to run it each time you reboot your PC or Pi Zero.

Ruffio commented 8 years ago

@hh has your issue been resolved? If so, please close this issue. Thanks.

hh commented 8 years ago

@ladyada do you have info on the kernel you used for https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget/serial-gadget

Do you know if any patches required for it to work were merged into any branches under https://github.com/raspberrypi/linux ?

gbaman commented 8 years ago

@hh that method described in the Adafruit guide is rather old and long out of date now. Raspbian now ships with all you need. Plus, is now even possible to configure everything needed by editing just config.txt and cmdline.txt on the FAT32 partition using another computer without the Pi ever being booted. Wrote it all up a while back after stumbling across the cmdline.txt parameter - http://blog.gbaman.info/?p=791

hh commented 8 years ago

Beautiful! Thanks @gbaman and everyone else! (I still haven't gotten any pizero's yet)

ladyada commented 8 years ago

hey cool @gbaman thx for the notice! the guide has been updated :) https://learn.adafruit.com/turning-your-raspberry-pi-zero-into-a-usb-gadget (serial gadget still requires a tiny bit of commands to kick off the getty service)

MrHamel commented 8 years ago

Can someone test and tell me the performance of the mass storage capability? According to the Tizen documentation, it does support ISO mounting and I'd like to weigh out my options. Thanks!