WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline pieric

  • Newbie
  • *
  • Posts: 7
Re: USB gadget kernel module with RPi zero
« Reply #60 on: October 21, 2020, 05:19:57 PM »
Hi all,

I have a problem getting mass storage mode to work. I went for putting all required kernel modules in extensions (dwc2 & gadget) and modprobing them upon boot.
As far as I can tell all dependencies are present and g_ether works perfectly fine.

However if I run:

Code: [Select]
sudo dd if=/dev/zero of=/tmp/usb.img bs=1M count=10  #Without any fs, just for testing
sudo modprobe g_mass_storage file=/tmp/usb.img stall=0

I get following error:
Code: [Select]
modprobe: can't load module g_mass_storage (kernel.tclocal(drivers/usb/gadget/legacy/g_mass_storage.ko): Invalid argument
Same with g_multi. Also with params "removable=1", "ro=0" I get above error. dmesg shows
Code: [Select]
LUN: removable file: (no medium)
no file given for LUN0
g_mass_storage 20980000.usb: failed to start g_mass_storage: -22

Any hints apreciated.

I run latest piCore release 12.0, untouched kernel on a pi zero, non W.

BR
Eric
BR
Eric

Offline pieric

  • Newbie
  • *
  • Posts: 7
Re: USB gadget kernel module with RPi zero
« Reply #61 on: October 22, 2020, 04:28:56 PM »
Hi,

solved. Needed to put required parameters in /etc/modprobe.d/g_mass_storage.conf. Did that by:
Code: [Select]
sudo mkdir /etc/modprobe.d
echo "options g_mass_storage file=/mnt/mmcblk0p2/piusb.img removable=1 ro=0 stall=0 iSerialNumber=123456"

This is what I did to get it running on piZero with piCore12.0*:

* Extensions squashfs-tools and optionally nano need to be in place. I loaded them on demand.
* Existing wifi is an advantage - otherwise detour by card reader/pendrive
 
Enable dwc2 overlay
Code: [Select]
sudo mount /dev/mmcblk0p1
sudo echo dtoverlay=dwc2 >> /mnt/mmcblk0p1/config.txt         #In my case the very end of the file was section [all].
sudo umount /dev/mmcblk0p1


Create module extensions and make them load at start
Code: [Select]
mkdir -p /tmp/dwc2/package/usr/local/lib/modules/5.4.51-piCore/kernel/drivers/usb
mkdir -p /tmp/gadget/package/usr/local/lib/modules/5.4.51-piCore/kernel/drivers/usb

cd /tmp
wget http://tinycorelinux.net/12.x/armv6/releases/RPi/src/kernel/modules-5.4.51-piCore.tar.xz
tar xf modules-5.4.51-piCore.tar.xz

cp -a lib/modules/5.4.51-piCore/kernel/drivers/usb/dwc2 /tmp/dwc2/package/usr/local/lib/modules/5.4.51-piCore/kernel/drivers/usb
cp -a lib/modules/5.4.51-piCore/kernel/drivers/usb/gadget /tmp/gadget/package/usr/local/lib/modules/5.4.51-piCore/kernel/drivers/usb

mksquashfs /tmp/dwc2/package/ /tmp/dwc2-5.4.51-piCore.tcz
mksquashfs /tmp/gadget/package /tmp/gadget-5.4.51-piCore.tcz

cp /tmp/dwc2-5.4.51-piCore.tcz /mnt/mmcblk0p2/tce/optional/
cp /tmp/gadget-5.4.51-piCore.tcz /mnt/mmcblk0p2/tce/optional/

chmod 664 /mnt/mmcblk0p2/tce/optional/dwc2-5.4.51-piCore.tcz
chmod 664 /mnt/mmcblk0p2/tce/optional/gadget-5.4.51-piCore.tcz

sudo echo "dwc2-5.4.51-piCore.tcz" >> /mnt/mmcblk0p2/tce/onboot.lst
sudo echo "gadget-5.4.51-piCore.tcz" >> /mnt/mmcblk0p2/tce/onboot.lst
As I understand, neither is it required to add modules.dep, nor is it needed to run depmod -a, since tinycore does that automatically when kernel modules are included in an extension.

Create container file for a quick test
Code: [Select]
sudo dd if=/dev/zero of=/mnt/mmcblk0p2/piusb.img bs=1M count=1024   #Create container of file 1GB size

Modify bootlocal.sh to load kernel modules on start (Did that with nano)
Added follwoing in "Load modules..." section, where an out-commented "/sbin/modprobe i2c-dev" used to be
Code: [Select]
/sbin/modprobe dwc2    #Load dwc2 module
sleep 5                #Make sure enumeration on host works properly

Added follwoing below for g_ether mode*
Code: [Select]
/sbin/modprobe g_ether stall=0          #Load g_ether module which will be RNDIS by default
/sbin/ifconfig usb0 192.168.100.2 up    #Bring up usb0 ethernet device and assign fixed IP

Added follwoing below for g_mass_storage mode*
Code: [Select]
sudo mkdir /etc/modprobe.d              #Create folder for module configs
#Create config for g_mass_storage to shair container file in read+write mode
echo "options g_mass_storage file=/mnt/mmcblk0p2/piusb.img removable=1 ro=0 stall=0 iSerialNumber=123456" > /etc/modprobe.d/g_mass_storage.conf
/sbin/modprobe g_mass_storage           #Load g_mass_storage module
*Only load either g_ether or g_mass_storage.

Commit changes and reboot
Code: [Select]
filetool.sh -b
sudo reboot
BR
Eric

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #62 on: November 29, 2020, 10:54:46 PM »
pieric newbie puts my newbie to shame. I'll have to reasd him again to try to absorb some of it. Two months ago I found some random bits of advice on mass storage mode, but clearly not though to help. Glad he fixed it himself.

Anyway, its been two months since I flogged this particular pi horse, and I am ready to give it another try. October 4th Rich gave me the answer, but I made the mistake of trying to extend my mmcblkp02 partition, and some error got me stuck with a larger partition but a filesytem that didn't expand to fill it. At least, I think that is what the error said / meant.

I have new microSD cards, and I will just start fresh. If anybody knows why I would get that error, let me know, becuase I am about to try the "readme" advice again, on a fresh card with a fresh OS, on a pi zero.

"1) Start fdisk partitioning tool as root:

   sudo fdisk -u /dev/mmcblk0

   Now list partitions with 'p' command and write down the starting and
   ending sectors of the second partition.

2) Delete second partition with 'd' than recreate it with 'n' command.
   Use the same starting sector as deleted had and provide end
   sectore or size greater than deleted had having enough free space
   for Mounted Mode. When finished, exit fdisk with 'w' command. Now
   partition size increased but file system size is not yet changed.

3) Reboot piCore. It is necessary to make Kernel aware of changes.

4) After reboot expand file system to the new partition boundaries with
   typing the following command as root:

   resize2fs /dev/mmcblk0p2

Now you are ready to use the bigger partition."

If I recall correctly, the "resize2fs" command spit an error at me. I will try again tonight, and do as Rich said in his post, to get my USB gadget working and get the modules automatically loaded on each fresh boot. Probably get TC.tcz GUI extension put on there too. And of course, my hobbyhorse of the MSSAID, artifical intelligence folders, for my raspbery pi cluster computer.

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #63 on: October 03, 2022, 08:59:19 PM »
Hi,

solved. Needed to put required parameters in /etc/modprobe.d/g_mass_storage.conf.

...

Hi pieric,

I'm trying to replicate your steps on piCore 13.1.  Most of your steps seem to have worked for me, except for loading the dwc2 module, and the ifconfig step. 

when I reboot, dwc2 doesn't get loaded, and if I try to do "modprobe dwc2" myself from a command line, I get:

Code: [Select]
modprobe: can't load module dwc2 (kernel.tclocal/drivers/usb/dwc2/dwc2.ko): unknown symbol in module, or unknown parameter

I've checked, and the module is being loaded into the filesystem where it should, based on my tcz, so the os can find it.  Do you know if anything has changed in this module in 13.1?

The ifconfig line is failing with

Code: [Select]
ifconfig: SIOCSIFADDR: No such device

I'm assuming this is happening because the dwc2 module isn't being loaded.  Presumably, it creates the "usb0" device?

Oh, and one last thing, when I reboot with these changes, my usb keyboard that is plugged into a usb hub on the pi (wired via gpio), no longer seems to work.  Is this to be expected?

I'm not quite sure where to go from here.

Thanks in advance for any help you can give me,

Chris

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11635
Re: USB gadget kernel module with RPi zero
« Reply #64 on: October 03, 2022, 09:21:23 PM »
Hi creitzel
I think you may be missing:
Code: [Select]
kernel/drivers/usb/gadget/udc/udc-core.ko
What does this return:
Code: [Select]
modinfo dwc2

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #65 on: October 03, 2022, 10:10:17 PM »
You were definitely on the right track Rich.  I was just going to post that I figured out how to get the dwc2 module to load.

After I posted this, I found another post that was having the same problem with dwc2 on 13.x, and the missing module was "roles.ko", so I included that in my tcz as well, and now it seems to be loading fine.  As for the "udc_core.ko", I'm pretty sure I already have that one, because I copied the entire "gadgets" sub folder into my tcz, and it is in there.  I found the need for "roles.ko" using your "modinfo dwc2" command.   :)

So, now it appears to load the modules correctly on boot, and it creates the usb0 device, and the ifconfig statement works to assign the ip, and bring the interface up. 

Now I'm stuck where I can't seem to ping it or ssh into it, from my windows 10 machine.  I've tried scanning for new hardware on the Win10 box, but that doesn't seem to do anything.  Do I have to somehow point ssh to the usb ip or something?  Although, the fact that ping doesn't work either, is kinda telling me that the problem may be on the Win10 side.

Sorry, for my lack of knowledge, I'm fairly new to this part of linux/piCore.

Thanks for any help you can give in advance,

Chris

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11635
Re: USB gadget kernel module with RPi zero
« Reply #66 on: October 03, 2022, 10:50:25 PM »
Hi creitzel
I think the way to connect to the pi remotely goes something like:
Code: [Select]
ssh tc@aaa.bbb.ccc.dddwhere  aaa.bbb.ccc.ddd  is the IP address of your pi.

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #67 on: October 03, 2022, 11:25:27 PM »
yeah, that would be the way I would ssh from a command line in win10.  I also have putty installed, which allows ssh connections as well. 

The problem I have right now, is the win10 box doesn't seem to recognize the fixed ip that I gave to the pi, it just keeps timing out whenever I try to access it. 

I think I'll give it a go from my linux laptop, and see if that can recognize it.

Thanks for all you help, it's much appreciated. 

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11635
Re: USB gadget kernel module with RPi zero
« Reply #68 on: October 03, 2022, 11:35:51 PM »
Hi creitzel
Are both machines on the same subnet? i.e. Are they both  192.168.1.xxx  or 10.10.1.xxx.

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #69 on: October 04, 2022, 12:47:02 AM »
Just for completeness, I've tried both with it on my subnet, and with it on a different subnet.  Either way, if I try to ping to the ip that I've assigned to it, it times out, like it would if there was no machine at that ip.

It is also connecting via wifi on my lan's subnet, which is currently how I'm able to work on it, because once I enabled dwc2, my usb hub HAT that is connected via the gpio pins has stopped working, so my usb keyboard isn't working right now.

I also now have tried it with my linux laptop, with the same results.  i.e. ping times out when I try to ping the ip assigned to the pi.

I'm not sure what else to check on the win10/linux box side.  There must be something I'm still missing on the pi side.

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11635
Re: USB gadget kernel module with RPi zero
« Reply #70 on: October 04, 2022, 01:01:17 AM »
Hi creitzel
... It is also connecting via wifi on my lan's subnet, ...
Is the other machine also connected to the same wifi router?

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #71 on: October 04, 2022, 10:11:10 AM »
Yes, my win10 box is connected via ethernet cable to the router, and my linux laptop is connected via wifi to the same router.  I don't know if the problem is being caused by that though, I've had the win10 box connected to the network (via the same router) both through ethernet cable, and a wifi dongle at the same time before, and it just gets 2 different ip addresses, one for the cable, and one for the wifi connection.

The interesting thing I noticed last night, when I tried using the pi plugged in to usb on my linux laptop, is it doesn't seem to show up at all when I try doing a "lsusb" at the command prompt.  This makes me think that something must be missing in the pi's configuration.  Every other network/usb type device I've ever used would at least show up in that list, whether I was having issues with it's configuration or not.  This is consistent with how it is behaving on the win10 box too, the usb device doesn't show up in device manager at all.

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #72 on: October 04, 2022, 10:59:48 AM »
You know what?  I just thought of something.  I just grabbed a random cable out of my box o' cables when I started playing around with the pi this time.  I bet it's a charging only cable, which would totally explain why the host computers aren't seeing any device when I plug it in.  I'll check this first thing when I get home this evening, after work.

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #73 on: October 04, 2022, 06:05:25 PM »
Ok, so I did in fact have just a charging cable, and not a data cable.  That was why it wasn't showing up in Windows at all.  I've now switched to a data cable, and now I've got to figure out how to get windows to use it's internal RDNIS driver for the pi.  It currently sees it as an unknown usb device.

Thanks again for all your help Rich. 

Offline creitzel

  • Newbie
  • *
  • Posts: 41
Re: USB gadget kernel module with RPi zero
« Reply #74 on: October 04, 2022, 07:35:44 PM »
Ok, so does anyone have any idea where the device descriptor for g_ether is on the pi 0? 

Now that I have a data cable, when I plug it into a Win10 box, it reports it as an undknown usb device, specifically stating that it couldn't get a device descriptor.  When I plug it into my ubuntu laptop, it gets reported as a usb hub.  This is definitely pointing towards the pi 0 misreporting it's device descriptor to the hosts.

Again, any help would be appreciated.   :)

Thanks in advance,

Chris