WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #45 on: October 01, 2020, 02:03:02 PM »
Rich, I will take that under advisement. I am already swimming deep over my head. I better tread water, float, and catch my breath.

Still going over johannkraus' successful attempt, and his and the community's help getting Linkan to have succesful g_ether. Maybe my mistakes will be revealed?

But bot was still having problems when I first popped on this thread, so I am guessing whatever the solution is, its not well documented.

(I wonder if there is a software way to tell which USB port on a pi-zero is drawing the power? Anybody have a command for that? I guess i could use my USB voltmeter-ammeter; ideally, I would need two or four volt-ammeters for proper experiments?)

Anyway, I am on JK's second message, about where he says RTFM; OI will do that. And follow his advice:

Quote
  "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

depmod -a should create the modules.dep file ; maybe copying it into the directory skips an important step? I'll make certain that is done and then make the results persistent. And then read some more!

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #46 on: October 01, 2020, 02:23:44 PM »
OK, apparently I didn't make the loaded modules (all 1900+ .ko files) persistent?

If I run depomod -a, the modules.dep file goes from having 1500+ entries back down to 180+ entries?

When I reboot, modules.dep goes back up to 1500+ entries (I made that persistent, at least!). I guess I can count the ,ko files in the /lib/modules/4.9.22-piCore directory? There's a command for that, if I can remember it / find it?

If there are 1900+ .ko files, I can install/configure them all? Or is this overkill?

modules.dep says g_ether only needs four files: usb_f_rndis.ko , libcomposite.ko , u_ether.ko , udc-core.ko

I dunno. read more manuals? Learn how to configure the kernel? I guess.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #47 on: October 01, 2020, 04:34:33 PM »
I was worried modprobe didn't know the path pr couldn't work from root , from /

This website  https://gist.github.com/gbaman/4c1345c0c4d6d82149d4 , although for Raspbian, says this
Quote
11. To manually load a kernel module, after rebooting use sudo modprobe moduleName. To unload a module, use sudo rmmod moduleName.

That seemed like good advice. Also, the previous 6 items.

6.Then run sudo cp -R /tmp/PiZeroCombined/fat32/* /boot/ (to copy the kernel and device tree stuff)

7. Then run sudo cp -R /tmp/PiZeroCombined/ext4/lib/* /lib/ (to copy the kernel modules themselves)

8. At this point, you have 2 options, you can either set the module you want to run on startup, or dynamically load and unload them.

9. To set a module to load on startup, edit the modules file with sudo nano /etc/modules and add the module name you want to start (e.g. g_ether) to the end on a new line. Also, make sure to the check the "Using the modules" section before rebooting.

10.Then, reboot the Raspberry Pi using sudo reboot.


I assume things are similar for PiCore. "6.Configure kernel? , 7. Load the necessary modules? 8-9. Add sudo modprobe g_ether to onboot.lst ? 10. then, make persistent (filetool.lst ; and "backup") and reboot.

and, of course, 11. modify config.txt with the dtoverlay=dwc2 line.

No warranty on the above methodology. In fact, is there an opposite of a warranty? Indemnification? Warning.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #48 on: October 01, 2020, 06:44:16 PM »
OK, I su-did the following (You like that one? I made that one up myself!)

modprobe g_ether.ko
modprobe udc-core.ko
modprobe libcomposite.ko
modprobe usb_f_rndis.ko
modprobe u_ether.ko

I think That's everything. I should make it persistent.

I forgot to check if I have the 1900+ files of the entire 4.9.22 zipped modules file (I've got them backed up, stored, on my kali laptop; but they won't do much good there! Have to get them persistent and in the right folders on the piCore-slave uSD, and get the kernel to talk to them? config-ed with them? or whatever it is that modules do with kernels?).

I have a notebook around here with simple CLI commands like "How many freakin' MB of data in this folder", and "How many files in this folder; and all the folders beneath it?".

"How many files does this folder collect, before you can call it a man? How many Trees, of devices are here, stored within this silicon we got from sand? Yes and how many times, must the Shebangs fly, before we close it out with a Sh@z@m? The answer my friend, is not in this crappy poem; the answer is not in my bad poems."

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #49 on: October 01, 2020, 07:09:53 PM »
Sorry if this brings you back to Linux preschool, but the repetition helps me.

Count Files using wc :
$ ls | wc -l

Count Files Recursively using find :
$ find <directory> -type f | wc -l

Count Files using tree :
$ tree <directory>
3 directories, 3 files

Check Disk Usage in Linux Using the du Command :
du <options> <location of directory or file>

I DON'T have all 1900+ files persistent on the card. i goofed up somewhere? But maybe don't need them? I modprobed the five .ko's that I KNOW I need.

I bet I have to "configure the kernel" (whatever that means?) ... Yuuuuclk .... uggggh! Onerous tasks!

I guess for tonight, I will depmod -a to update the module.dep file (or not; maybe try it with and without update?); and I will alter config.txt to say dtoverlay=dwc2 ; and I will look for an etc/modules (like in raspbian) to add g_ether as the last line; but picore probably wants it in the onboot.lst, I'll wager? Then try to boot piCore as a g_ether gadget device.

If that doesn't work, I will have to quit for a few days while I read about kernel configuration.Yuck. I mean, closed door=opened window ?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: USB gadget kernel module with RPi zero
« Reply #50 on: October 01, 2020, 08:29:17 PM »
Hi ladnar
You could create a  gadget  extension (squashfs-tools.tcz  required):
Code: [Select]
tc@E310:~/pi$ wget http://tinycorelinux.net/9.x/armv6/releases/RPi/src/kernel/4.9.22-piCore_modules.tar.xz
--2020-10-01 22:58:43--  http://tinycorelinux.net/9.x/armv6/releases/RPi/src/kernel/4.9.22-piCore_modules.tar.xz
Resolving tinycorelinux.net... 89.22.99.37
Connecting to tinycorelinux.net|89.22.99.37|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11161928 (11M) [text/plain]
Saving to: '4.9.22-piCore_modules.tar.xz'

4.9.22-piCore_modules.tar.xz                            100%[============================================================================================================================>]  10.64M  3.30MB/s    in 3.2s   

2020-10-01 22:58:46 (3.30 MB/s) - '4.9.22-piCore_modules.tar.xz' saved [11161928/11161928]

tc@E310:~/pi$ tar xf 4.9.22-piCore_modules.tar.xz
tc@E310:~/pi$ mkdir -p package/usr/local/lib/modules/4.9.22-piCore/kernel/drivers/usb
tc@E310:~/pi$ cp -a lib/modules/4.9.22-piCore/kernel/drivers/usb/gadget package/usr/local/lib/modules/4.9.22-piCore/kernel/drivers/usb
tc@E310:~/pi$ mksquashfs package gadget-4.9.22-piCore.tcz
Parallel mksquashfs: Using 2 processors
Creating 4.0 filesystem on gadget-4.9.22-piCore.tcz, block size 4096.
[============================================================================================================================================================================================================|] 186/186 100%

Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 4096
        compressed data, compressed metadata, compressed fragments, no xattrs
        duplicates are removed
Filesystem size 253.94 Kbytes (0.25 Mbytes)
        36.96% of uncompressed filesystem size (686.99 Kbytes)
Inode table size 946 bytes (0.92 Kbytes)
        44.58% of uncompressed inode table size (2122 bytes)
Directory table size 510 bytes (0.50 Kbytes)
        54.60% of uncompressed directory table size (934 bytes)
Number of duplicate files found 0
Number of inodes 43
Number of files 30
Number of fragments 0
Number of symbolic links  0
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 13
Number of ids (unique uids + gids) 2
Number of uids 1
        tc (1001)
Number of gids 1
        staff (50)
tc@E310:~/pi$

Copy  gadget-4.9.22-piCore.tcz  to your  tce  directory. Then:
Code: [Select]
tce-load -i gadget-4.9.22-piCore
sudo modprobe g_ether

Check if your modules are loaded:
Code: [Select]
lsmod

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #51 on: October 03, 2020, 07:06:07 AM »
Thanks,

I will give it a try, but right now I am "extreme apartment hunting"; I really did not have the time to clog the tinycore/picore pages with my meager experiments; but I did anyway. And now I am paying for it (in stress levels and worry). I'll see you, here, in a week or two when I, like a cat, land on my feet! Maybe I'll have a gadget extension and/or configured-and-rebuilt kernel by then? Maybe I will have some greater grasp of what I am doing, then.

Thanks again for the specific advice / code.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #52 on: October 03, 2020, 08:23:15 PM »
Old link, but I am going to try to absorb two or three chapter tonight.

https://linux.die.net/lkmpg/index.html

this part looks good.

https://linux.die.net/lkmpg/x44.html

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: USB gadget kernel module with RPi zero
« Reply #53 on: October 04, 2020, 06:34:34 AM »
Hi ladnar
Unless you are planning on learning how to program in C and writing your own kernel modules, I don't think that guide will be
of much help to you.

The instructions listed in reply #50 should take well under 5 minutes. The most time consuming step is the download time
of  4.9.22-piCore_modules.tar.xz. The  tce-load  step will even run  depmod  for you since it checks if extensions contain
kernel modules. If you are satisfied it loaded the 5 modules you mentioned in reply #48 loaded, you can:
1. Add  gadget-4.9.22-piCore.tcz  to your /etc/sysconfig/tcedir/onboot.lst  file so the extension gets loaded when you boot.
2. Add  modprobe g_ether.ko  to your  /opt/bootlocal.sh  file so the module and its dependencies get loaded when you boot.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #54 on: October 04, 2020, 11:58:13 AM »
Have started C, but as with most other things in life, have been separated from the book that would teach it. I think I still have the pdf somewhere? Thanks Rich. (I already know a fair amount about the process; headers, pre-compile, compile, assembly, object files, linkers, binary blobs, etc. Studied a bit of Barry Brey's textbook on x86, MASM and TASM, circa 2001; and Rodnay Zaks 6502 early 1980s book; and Ritchie and Kernighans "C" classic textbook. tarted to read Two Tennenbaum books, one on distributed systems, the other one was the one Torvalds read before he wrote Linux. Also had  nice book on Binary dissasembly; I forget the author?  A recent book from stach press, out of Frisco? But you're right. I can't know everything, at least not ALL AT ONCE. So "C" might have  to wait You be happy to see, below, that I have maybe figured out my problem --besides my exuberance outstripping my talent. I think the latter caught up to the former, finally.)

First things first. I looked at my old posts (under "doctor anonymous" handle ) and realized, I already half-solved this problem, about 9 months ago! But I was moving apartments then, too, and so I had lost track of my progress. The good doctor was working with usbboot, and got it half-functional.

Now, I think I have figured out this modprobe stuff, and I think I know why it was working with Raspbian and not with piCore. Its the persistence thing. I guess "persistence" is not the thing I need, per se. (A GOOD shell script FIRST, then persistence of that script.).

I tried putting the following thing into onboot.lst ; It will go there, but its useless ! "sudo modprobe g_ether". But the simple shell scripts I made, also placed in my onboot.lst , for "Meyer's Strong AI Standard Directories" (MSSAID), execute fine. So, something like that is needed here. I think I can probably solve user/member bot's  problem too, with "serial" mode. (for the moment, please ignore the impertinence of a biologist walking in and telling the experts that LSB/FHS has to be changed (to include MMSAID). I know LSB has a history, but its also come a long way, and has much further to go!)

I don't have time today to look into serial, but will write  g_ether script and post it here (if my idea works). Also, depmod -a  stymied me a few time, because the chmod permissions for modules.dep are 755 ; I was going to chmod it to 777, and then back again, but then I remembered just to sudo it.

That link ( https://linux.die.net/lkmpg/x44.html ) helped alot to explain modules, and another page I stumbled on with some good example on how to actually use, lsmod, modprobe, depmod, and modules.dep . It wasn't rocket science, and I think a clutter and junk in my brain just reached critical mass.
It was also important to hear about the difference between insmod and modprobe, and the function of the depmode and modules.dep in making modprobe much easier and convenient than insmod.

Anyhoo, I should go write the script. It will basically just load all the modules that I have been typing in one at a time. I'll put the script in the onboot.lst and it will execute on startup (instead of the modules erasing / reverting, when I shut the power off). That's what was happening. I got all the modprobe stuff done, but then would shut it off to switch the wires around and hook it up to the master board. For whatever reason, I figured its better to turn master and slave b oard on simultaneously, so they both "negotiate the bus" at the same time. Maybe not necessary, but I considered it prudent (funny things can happen with USB and power and hubs and etc? Funny, buggy things!). But in this case, shutting off power was undoing the modules.

Also, anyone stumbling on this post who doesn't want to read the previous one hundred (20 or so) should note that I have placed five modules in the appropriate /lib/modules/4.9.22-piCore/ .../  folders, and placed the path of those folders in the /opt/.filetool,lst , and then typed the backup command. This seems to have worked for these five files.

g_ether.ko ; usb_f_rndis.ko ; u_ether.ko ; udc-core.ko and libcomposite.ko

The script will just be this, I think:

Code: [Select]
#!/bin/sh

sudo modprobe g_ether

exit

I wonder if this could be put in cmdline.txt, because raspbian has that line about "load-modules,g_ether" or whatever? Maybe that command actually works? Maybe all this time, that is all I had to do? I will try it my way first, and if that doesn't work, I will try the raspbain cmdline.txt edit.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #55 on: October 04, 2020, 12:02:14 PM »
OK, Rich, I realize you just gave me the right answer (two right answers! Your previous two posts!), slightly after I figured it out. Thanks again. And thanks for putting up with my slow learning curve, and the volumes I've written about it! Now I  just have to peel off five minutes from my day, but I just spent that time researching and posting. time to go catch a bus. I've got other things to do. Maybe tonight.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #56 on: October 05, 2020, 08:25:44 PM »
Ok my script works, AFTER I boot. It lets me run the script by typed command, in CLI. Successful!  But not when I put it in the onboot.lst? (I changed it to a two line script; it needed a "depmod -a" before the "modprobe g_ether". I think I even put in a "cd /" as the first line.

Must be a permissions thing? Or maybe modprobe doesn't like to execute in an "bootup" shell script? No that seems to silly to be true.

I must have forgotten something about onboot.lst ?

I am going to try the right answer (supplied by Rich, above). Make an extension? I have never done that before. I suppose I could add a desktop, now that I downloaded TC extension?
« Last Edit: October 05, 2020, 08:35:14 PM by ladnar »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: USB gadget kernel module with RPi zero
« Reply #57 on: October 05, 2020, 08:36:47 PM »
Hi ladnar
Ok my script works, AFTER I boot. But not when I put it in the onboot.lst? ...
That's not what  onboot.lst  is for. The only thing that goes into  onboot.lst  is which extensions you want loaded when
you boot.

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #58 on: October 05, 2020, 08:38:16 PM »
Oh!!!   I see. Rich already said. optbootlocal.sh.

Thanks again. I better slow down and read thorough, one more time. This will work tonight!

Offline ladnar

  • Newbie
  • *
  • Posts: 47
Re: USB gadget kernel module with RPi zero
« Reply #59 on: October 05, 2020, 08:40:46 PM »
OK, about onboot.lst. I will learn this extension thing. And only put those there.( I read like 1/3 of the manual, two years ago. If I try again, it might make more sense.)

Maybe tonight's the night I get a desktop, too; TC.tz !