Sebastian Urban's Rust Library Makes It Easier to Experiment with a Raspberry Pi's USB Gadget Mode

Library comes with predefined network, serial, Human Interface Device, and Mass Storage Device functions, and you can add your own too.

Gareth Halfacree
7 months ago β€’ HW101

Developer Sebastian Urban is making it easier to turn a Raspberry Pi 4 into a Rust-powered USB gadget β€” using, appropriately enough, his usb-gadget library.

"I just wanted to share a library I've been working on: usb-gadget," Urban explains of his creation. "If you've ever tinkered with implementing USB gadgets, i.e. USB peripherals, on Linux, I think you might find it handy!"

Typically, a Raspberry Pi single-board computer (SBC) is used as as a USB host: you connect a power cable to its USB Type-C port and then whatever USB devices you want to use to its two USB 2.0 and two USB 3.0 Type-A ports. Sometimes, though, you want things to work differently, and that's where the USB "gadget" mode comes in: turning the SBC into a USB device compatible with any USB host you'd care to name.

"In a nutshell, [usb-gadget can]: configure pre-defined USB functions like network interfaces, serial ports, HID, etc.; create fully custom USB functions using user-mode Rust code; tweak & play with WebUSB and OS-specific descriptors."

Using USB gadgets, it's possible to expose a Raspberry Pi's serial console over USB β€” or have it create a network connection to a host machine. You can tap into the hardware random number generator (HRNG) in the board's system-on-chip and use it as a source of entropy for a server, or use it to interface with the general-purpose input/output (GPIO) pins.

The source code for the library has been published to GitHub under the permissive Apache 2.0 license; documentation is available on crates.io. "A USB device controller (UDC) supported by Linux is require," Urban notes. "Normally, standard PCs do not include an UDC. A Raspberry Pi 4 contains an UDC, which is connected to its USB-C port."

More information is available in Urban's Reddit post.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles