WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: USB gadget kernel module with RPi zero  (Read 49868 times)

Offline johannkraus

  • Newbie
  • *
  • Posts: 6
USB gadget kernel module with RPi zero
« on: July 29, 2016, 11:29:05 AM »
Hi there,
I'm using Tiny Core Linux on my different RPis for quite a while now. One of my projects is miniaturising an RFID-based attendance system currently running on a Raspberry Pi B. I bought a couple of RPi zeros and now I am experimenting with some new hardware, i.e. OLED vs LED, capacitive touch vs button, battery power vs power supply, ... AND ...

USB OTG vs. wlan/usblan/serial

The RPi zero supports gadget mode, i.e. one can program the RPi from another computer using only a single USB OTG cable. The available gadget modules enable mass storage mode, ethernet, serial, etc. I'm most interested in the mass storage mode, as it would enable mounting the gadget on a host computer and transferring some data without the need for LAN or a serial connection.
I was already able to do this with Raspbian following this guide http://blog.gbaman.info/?p=699

But I do have some issues with Tiny Core Linux and need your help. This is what I tried so far:
- I installed the newest beta as it has Kernel 4.4.15
- I added dtoverlay=dwc2 to config.txt
- trying modprobe dwc2 tells me that there is no module to load
- I downloaded http://tinycorelinux.net/8.x/armv6/releases/RPi/src/kernel/4.4.15-piCore+_modules.tar.xz and there it is
- I copied dwc2 and the gadget modules to lib/modules/.../usb and also copied modules.dep
- finally, I was able to modprobe dwc2

Then if I modprobe g_mass_storage it tells me that there is no such device. Shouldn't dtoverlay do it's magic?
I would really appreciate any help! I built my last kernel ~10 years ago in my Gentoo times and just learned that something like device tree exists.

To make some advertisement for USB OTG:
- one would not need a serial connection to the GPIO - you can use usb
- the Pi zero can be powered from it's usb port, i.e. the gadget mode one :)
- you can have ethernet over usb without additional hardware, i.e. wlan stick or breakout board
- and lot more fun stuff :)
- the obvious downside is that you loose the only usb port on the RPi zero

Best Johann
Nowhere, Nothing, Never

Offline johannkraus

  • Newbie
  • *
  • Posts: 6
Re: USB gadget kernel module with RPi zero
« Reply #1 on: August 09, 2016, 05:35:26 AM »
Okay - I finally had some time to compare Raspbian to piCore:
  • after adding dwc2 overlay to config.txt and modprobe dwc2 in Raspbian there is another tty in dev -> ttyGS0. I would assume this is the device the gadget modules use. And this device is not present in piCore. Remind that adding the overlay and modprobe dwc2 just works and does not give any error in piCore. But all the messages in dmesg about enabling the new usb (non host) device are missing.
  • using Raspbian one can dynamically load overlays via dtoverlay in the bash. If I do so, the device is also not created. However, I think this is unrelated to my problem here.
  • in piCore I can also modprobe further modules that seem to be needed by the gadget modules, i.e. libcomposite and usb_f_acm.

It seems that dtoverlay cannot handle the dwc2 overlay. Well, sit back and ... RTFM -> https://searchcode.com/codesearch/view/37919024/
Quote
To use the gadget serial driver you must configure the Linux gadget side kernel for "Support for USB Gadgets", for a "USB Peripheral Controller" (for example, net2280), and for the "Serial Gadget" driver.  All this are listed under "USB Gadget Support" when configuring the kernel.  Then rebuild and install the kernel or modules.

I assume that the gadget side kernel was not configured. I will have a try rebuilding the kernel - will take some time  ;D

and
- I copied dwc2 and the gadget modules to lib/modules/.../usb and also copied modules.dep
I assume depmod -a should be better than copying modules.dep
Nowhere, Nothing, Never

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: USB gadget kernel module with RPi zero
« Reply #2 on: August 09, 2016, 06:38:04 AM »
piCore kernel configuration is the same as Raspbian, except few more features enabled, like zswap, etc. Nothing removed compared to Raspbian.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline johannkraus

  • Newbie
  • *
  • Posts: 6
Re: USB gadget kernel module with RPi zero
« Reply #3 on: September 09, 2016, 06:33:11 AM »
Thanks Béla. I tried it once again now with piCore-8.0rc1 and succeeded. Probably last time I forgot to make the changes from depmod persistent. Gadget mode via g_multi (which includes mass storage, serial, and ether) now runs perfectly.
This is how I succeeded:
  • Copy dwc2 and the gadget modules to lib/modules/.../usb
  • Call depmod -a and make the result persistent
  • Add /sbin/modprobe dwc2 to /opt/bootlocal.sh -> again don't forget to call backup
  • Add dtoverlay=dwc2 to config.txt. Don't do that before your system loads dwc2 module at startup as your usb port will then probably not accept your keyboard anymore (as dwc_otg is replaced by dwc2).
  • Create a container to share via g_multi, e.g. dd if=/dev/zero of=/home/tc/piusb.bin bs=512 count=2880 and mkdosfs piusb.bin
  • After reboot call modprobe g_multi file=/home/tc/piusb.bin stall=0
You now can add the g_multi modprobe to bootlocal. I needed a short sleep between modprobing dwc2 and g_multi, e.g. sleep 5. Otherwise I had strange errors on enumerating usbs on the host computer.
For a working ether over usb I just set up the usb0 interface via ifconfig as described elsewhere in this forum for static eth0.
For a working serial via /dev/ttyGS0 I modified cmdline.txt to use GS0 and the /usr/sbin/startserialtty script.

@bmarkus -> I would be interested in having the usb modules builtin the image. They're less than 1MB in size and really provide a benefit for Pi zero users. Any more Pi zero users here?
If the modules could be builtin - Is there a way to modprobe dwc2 and g_multi and setting up usb0 ethernet automatically iff dtoverlay=dwc2 was added to config.txt? This would make an out-of-the-box pi zero gadget with serial or ethernet via usb.

Best
Johann
Nowhere, Nothing, Never

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1244
Re: USB gadget kernel module with RPi zero
« Reply #4 on: September 09, 2016, 10:01:45 AM »
No two users will use the same hardware and driver.  The base image is fine.   My USB hub/ethernet uses the usbip-KERNEL.tcz package.   After I burn the image, I mount the partition and add that extension to the drive, and edit onboot.lst     Takes a minute to do.


Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: USB gadget kernel module with RPi zero
« Reply #5 on: September 10, 2016, 05:43:52 AM »

@bmarkus -> I would be interested in having the usb modules builtin the image. They're less than 1MB in size and really provide a benefit for Pi zero users. Any more Pi zero users here?
If the modules could be builtin - Is there a way to modprobe dwc2 and g_multi and setting up usb0 ethernet automatically iff dtoverlay=dwc2 was added to config.txt? This would make an out-of-the-box pi zero gadget with serial or ethernet via usb.


gadget is not an essential functionality, it will not go to the base. However it can be useful to have when no ethernet is available. What we can do to make  kernel modules and support programs if any as tcz, test and add to the repo. If it works fine, we can decide to preinstall same way as openssh.

@johannkraus

Can you provide list of kernel modules and user space programs, files needed?
« Last Edit: September 10, 2016, 06:24:02 AM by bmarkus »
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline johannkraus

  • Newbie
  • *
  • Posts: 6
Re: USB gadget kernel module with RPi zero
« Reply #6 on: September 13, 2016, 12:47:33 PM »
gadget is not an essential functionality, it will not go to the base.
Okay -  g_ether and g_mass_storage are not. But you currently support a serial connection via ACM0 or S0. Having gadget mode in PiZero one can have a serial connection without any additional hardware or cable (despite a power usb cable connected to the usb port, which is needed anyway for power). To me this seems more essential than connecting GPIO pins and sharing ACM0  ;) But I understand your point - ACM0 is available anyway ;D

@Paul_123 My point is that gadget is built into PiZero, therefore no need to buy any additional hardware nor to make software configurations. All PiZero users get this for free.

However it can be useful to have when no ethernet is available. What we can do to make  kernel modules and support programs if any as tcz, test and add to the repo. If it works fine, we can decide to preinstall same way as openssh.
@johannkraus
Can you provide list of kernel modules and user space programs, files needed?
That would be fine. I'll have a look at the the minimal set of kernel modules needed for serial/ether/storage configurations and come back to you.
For now I can only tell what lsmod tells me
usb_f_acm               6023  1
u_serial               11369  1 usb_f_acm
usb_f_rndis            17023  1
g_multi                 5631  0
usb_f_mass_storage     39135  2 g_multi
libcomposite           49319  4 usb_f_acm,usb_f_rndis,g_multi,usb_f_mass_storage
u_ether                14023  2 usb_f_rndis,g_multi
dwc2                  109939  0
udc_core               12750  2 libcomposite,dwc2

Being lazy for serialtty I have:
Having cmdline0.txt (like cmdline3.txt) as
console=ttyGS0,115200 ...
my change to /usr/sbin/startserialtty is at line7
elif [ "${model:0:17}" = "Raspberry Pi Zero" ]; then
    port=ttyGS0
However, this does not work if dwc2, libcomposite, usb_f_acm and u_serial are not available and modprobed before. And it should only be made iff the dwc2 overlay is used. I don't find infos about used overlays in /proc/device-tree otherwise I would just add this to the elif condition above.

And being even more lazy - for ether and mass storage I have in /opt/bootlocal.sh
# ------ Put other system startup commands below this line
/sbin/modprobe dwc2 # <- but this is too late for /usr/sbin/startserialtty and had to be moved to line 2
/bin/sleep 5 # <- and this is killing performance if moved to line 2 :) Currently searching a way to avoid it.
/sbin/modprobe g_multi file=/home/tc/piusb.bin stall=0
/sbin/ifconfig usb0 192.168.100.2 up
Nowhere, Nothing, Never

Offline Linkan

  • Newbie
  • *
  • Posts: 27
Re: USB gadget kernel module with RPi zero
« Reply #7 on: November 29, 2016, 04:36:13 PM »
Any updates on this? Is there a tcz package available to setup u_ether? I'm using a pi zero and I haven't got a USB ethernet adapter so I'd really like to use this feature.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: USB gadget kernel module with RPi zero
« Reply #8 on: November 29, 2016, 04:46:46 PM »
Any updates on this? Is there a tcz package available to setup u_ether? I'm using a pi zero and I haven't got a USB ethernet adapter so I'd really like to use this feature.

What are you missing?
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline Linkan

  • Newbie
  • *
  • Posts: 27
Re: USB gadget kernel module with RPi zero
« Reply #9 on: November 29, 2016, 05:41:57 PM »
Not sure where I get the dwc2 overlay and how to set it up. Running modprobe dwc2 gives me "modprobe: module dwc2 not found in modules.dep". Do I need to download the linux modules package (http://tinycorelinux.net/8.x/armv6/releases/RPi/src/kernel/4.4.20-piCore+_modules.tar.xz)?
« Last Edit: November 29, 2016, 05:46:12 PM by Linkan »

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: USB gadget kernel module with RPi zero
« Reply #10 on: November 29, 2016, 11:58:57 PM »
dwc2 overlay is shipped with piCore. Enable it in /mnt/mmcblk0p1/config.txt For more info read /mnt/mmcblk0p1/overlays/README

Kernel modules in the archive

http://tinycorelinux.net/8.x/armv6/releases/RPi/src/kernel/4.4.20-piCore%2B_modules.tar.xz

Extract modules you need and create a personal tcz with them or add to backup, tcz is preferred.

« Last Edit: November 30, 2016, 12:01:55 AM by bmarkus »
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline tehfink

  • Newbie
  • *
  • Posts: 11
Re: USB gadget kernel module with RPi zero
« Reply #11 on: November 30, 2016, 04:14:56 PM »
gadget is not an essential functionality, it will not go to the base. However it can be useful to have when no ethernet is available.

It's also quite useful because the power usage via USB is about half of that with a USB/Ethernet adaptor, on a Pi Zero.

Offline Linkan

  • Newbie
  • *
  • Posts: 27
Re: USB gadget kernel module with RPi zero
« Reply #12 on: December 15, 2016, 02:57:39 PM »
Sorry but I just can't figure it out. I have enabled the `dwc2` overlay in config.txt and I created a tcz package with the following modules added:
Code: [Select]
4.4.20-piCore+/kernel/drivers/usb/gadget/legacy/g_ether.ko
4.4.20-piCore+/kernel/drivers/usb/gadget/libcomposite.ko
4.4.20-piCore+/kernel/drivers/usb/gadget/function/usb_f_rndis.ko
4.4.20-piCore+/kernel/drivers/usb/gadget/function/u_ether.ko
4.4.20-piCore+/kernel/drivers/usb/gadget/udc/udc-core.ko
4.4.20-piCore+/kernel/drivers/usb/dwc2/dwc2.ko
I also put `modules-load=dwc2,g_ether` after `rootwait` in cmdline.txt and then I reboot my rpi. It doesn't show up as a usb device on my machine. Is there something I forgot to do?

I also tried the method described above with loading dwc2 and g_ether in /opt/bootlocal.sh with a 5 second delay and setting the ip with ifconfig but I can't ping it from my main machine.

Doing `ip link` doesn't show `usb0` and I see nothing in dmesg or a difference in `lsusb` before and after plugging in my RPi. I have tried with multiple microusb cables too. When I plug in my phone I see a new usb device was connected so I know that works.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 714
Re: USB gadget kernel module with RPi zero
« Reply #13 on: December 15, 2016, 06:16:35 PM »
Have you tried to run
Code: (bash) [Select]
sudo lsmod, to see if the modules have been loaded.

Offline Linkan

  • Newbie
  • *
  • Posts: 27
Re: USB gadget kernel module with RPi zero
« Reply #14 on: December 15, 2016, 06:20:36 PM »
How can I do that without a keyboard? I can plug in a monitor but I wont be able to use a keyboard because the only USB port on the pi zero will be occupied by my OTG cable.