creating a USB gadget
from muusemuuse@lemm.ee to linux@lemmy.ml on 19 Apr 04:36
https://lemm.ee/post/61772628
from muusemuuse@lemm.ee to linux@lemmy.ml on 19 Apr 04:36
https://lemm.ee/post/61772628
I want to create a USB gadget with a raspberry pi zero 2W. I’m starting with imitating a webcam I already have to see how much of this I can figure out. I’ve used the online documentation and a couple AI bots to get this far quickly, but I’m hung up on a ln command. It’s telling me “ln: failed to create symbolic link ‘configs/c.1/uvc.usb0’: No such file or directory” when trying to create the link. This makes no sense to me though. I’m trying to create the link, of course it doesn’t exist yet. That’s what that command is supposed to do.
I’ve confirmed this problem in alpine linux and raspbian lite.
Below is the little script I have so far just to create the device:
#!/bin/bash modprobe libcomposite cd /sys/kernel/config/usb_gadget/ mkdir -p fauxcam cd fauxcam echo 0x046d > idVendor # Logitech Vendor ID echo 0x094b > idProduct # Brio 105 Product ID echo 0x0200 > bcdUSB echo 0x9914 > bcdDevice mkdir -p strings/0x409 echo "111111111111" > strings/0x409/serialnumber echo "Brio 105" > strings/0x409/product mkdir -p configs/c.1/strings/0x409 echo "UVC Configuration" > configs/c.1/strings/0x409/configuration echo 250 > configs/c.1/MaxPower mkdir -p functions/uvc.usb0 ln -s functions/uvc.usb0 configs/c.1/ echo "usb0" > UDC
threaded - newest
Aren’t sysfs entries supposed to be created by kernel modules?
Hey, you need to also
modprobe usb_f_uvc
at the beggining, right aftermodprobe libcomposite
no change
Hmm, not sure then. It seems correct to me. Check out this repo, it has systemd services for all the USB gadgets, you can run uvc very easily with this: github.com/BigfootACA/systemd-gadget
I can write specific instructions how to get this working later today, if you’ll need them.
Looking at those units I noticed something… ExecStart=/usr/bin/ln -s ${GADGET}/functions/uvc.0 ${GADGET}/configs/a.1/
It’s an a.1 instead of a c.1. Surely it couldn’t be as simple as just using a different letter, could it?
No, unfortunately not :/
That’s just the “name” of the configuration, for instance in my USB ethernet gadget I use both c.1 and c.2 as config names, and not a.1.
Once I crack this, I think it could be fun to create a script that could spoof devices plugged into it with the gadget framework. Could save me a lot of trouble creating known-working usb configs down the road. Instead of starting from scratch each time, I would just have to tweak an existing profile.
Hey, if you ever create that, give me a shoutout, I’d love to see it.
Honestly it’s just an idea to suck up time at this point. I do NOT have the skills for this shit, as we can see here. I can correct what ChatGPT gets wrong though and just run with that.
You should definitely try with the systemd-gadgets I linked earlier. It makes all the configuration really easy, you just need to enable the relevant services, so in your case
usbgadget-func-uvc.service
andgadget-start.service
. You also need to copy them beforehand to/etc/systemd/system
, includinggadget-init.service
, and you need to copygadget
to/etc/default/gadget
, and the scriptsgadget-start.sh
andgadget-init.sh
to/etc/systemd/scripts
. Edit/etc/default/gadget
to edit the configs and names of the gadget, and then startgadget-start.service
. No need to enablegadget-init.service
, it’s called as a dependency from other services.There’s an install script in the repo that you can use as well,
setup.sh
, and a PKGBUILD so you can create an Arch package. After installing with either method, just change/etc/default/gadget
, enable the uvc and gadget start services, and then just start the gadget start service.there is no setup and the instructions for a manual install keep getting me weird complaints from systemd about dependancies. It occurs to me that tackling this is error doesn’t make sense because it requires systemd to work and ultimately I want to run this on a diskless alpine linux install which doesnt use systemd.
Sorry, my bad, I forgot that the setup script isn’t part of the default repo and is something I added in my own fork. But yeah, if there’s no systemd, it’s no use.
I can whip up a quick script that should work, I’ll test it out on my own hardware, and post it here for you sometime tomorrow.
woot! let me know what you come up with.
IDK how all that works but I will say that the result of this would (in a normal filesystem) create a link named
configs/c.1/uvc.usb0
yes, but that link would point tofunctions/uvc.usb0
relative to its own dir. This doesn’t exist and your symlink would be broken, presumably the special file system there doesn’t like that.Edit: Apparently that’s not the problem and you’re totally supposed to run an ln command that should logically result in a broken symlink, thanks to kernel driver sysfs abuse.
Normally, you’d be right, but this special filesystem doesn’t care about that. I’ve done similar with different gadgets so many times, always with relativr symlinks, sometimes directly from the root of the special filesystem, sometimes from the gadgets own subdirectory, and it just works.
Do you mean you intend to create a broken symlink?
No, this is ConfigFS, the linux kernel’s special filesystem for configuring kernel stuff, and for some reason that I don’t really know, it doesn’t matter if the symlink is relative or not. The kernel documentation even creates a relative symlink from the same directory as OP did here.
Edit: OK, I looked at the docs, and they sure do make a broken symlink there. I still think it’s worth a try to create a non-broken link, maybe the docs are wrong. I would expect they would put a little note there, that yes, you really do want to create a broken symlink (if so, why not a regular file?), but then again its kernel docs and those aren’t the most friendly.
I also thought you were OP for some reason, sorry.
Edit2: If you look at the file listing later in the docs, you can see this:
Which does look like a real non-broken symlink, so I maintain the docs are wrong and you’re not supposed to make a broken symlink.
Original comment, silightly edited:
You misunderstand. I suspect OP cannot create the symlink, because it would be a broken symlink, not because the symlink is relative. Maybe you cannot create broken symlinks in the sysfs for some reason.
I was just trying to explain that a relative symlink is relative to the directory in which it resides. The target to the symlink should point to
…/…/functions/uvc.usb0
if you want it to point to something that exists. The ln command in OP’s listing would result in a broken symlink, since the specified path is not relative to the c.1 directory. It is relative to the working directory, but that’s wrong, that’s not what ln expects you to put there.Maybe it needs to be a correct symlink, maybe that will solve the problem.
I’ve got some experience with Linux USB gadgets, and can confirm that the docs definitely aren’t wrong, as I’ve used them myself to write gadgets using the same symlink commands that the docs mention and that OP used here.
I’ve got a working USB ethernet gadget and MTP gadget for the Steam Deck that make the same link that should be broken, and here’s a repo that implements every USB gadget with almost every gadget making the same symlink, one directory up from where OP does it. I’ve tested all the gadgets from that repo and they all work, and because they work, I’ve forked the repo here for future use in the above mentioned Steam Deck plugin.
I can pretty confidently say that it’s not a broken symlink. I’m not 100% sure why this doesn’t work for OP, but I think it’s most likely an issue with not loading the correct driver for the UVC function.
OK I’m convinced, that’s apparently how that works. I wonder why it’s designed that way.
Yeah, it’s a bit weird that it works like that. I’ll be honest, in the last three or four years that I’ve been tinkering with USB gadgets, I’ve never noticed that those symlinks you create should be broken until you mentioned it. I just kinda took it for granted that it works (probably because I never had issues with this part of the process).
You can check this project : github.com/showmewebcam/showmewebcam
It’s uses buildroot and it’s easy to change the sources for your need (I’ve added the wifi support)