Implement USB Gadgets on Linux Easily with Sebastian’s New Rust Library
Rust developers looking for easier ways to turn a Raspberry Pi 4 into a Rust-powered USB gadget now have a powerful tool at their reach. Developer Sebastian Urban has released details on his new Rust library for USB gadget development on Linux.
Sebastian, who claimed to have been working on this project for a while now, said his “usb-gadget” would be appropriate enough for those who ever tinkered with implementing USB peripherals on Linux.
Normally, we use the Raspberry Pi as a USB host but there are times you just want things to work differently, and you turn the SBC into a USB device compatible with any USB host. This is where USB gadget mode comes in and the Raspberry Pi’s ability to operate in this mode is one of the most useful features that it has. In this mode, you can expose the Pi’s serial console over USB or have it create a network connection to another computer.
“The USB gadget is a new Rust library that allows implementation or development of USB peripherals, so-called USB gadgets, on Linux devices that have a USB device controller (UDC),” the developer explained. “It supports configuring predefined USB functions like network interfaces [such as CDC ECM, CDC ECM (subset), CDC EEM, CDC NCM, and RNDIS]; serial ports [such as CDC ACM and generic], Human Interface Device (HID), and, Mass-Storage Device (MSD). You can also use it to create fully custom USB functions using user-mode Rust code and also tweak & play with WebUSB and OS-specific descriptions,” Sebastian added.
For requirements, Sebastian notes that you will need a USB device controller that is Linux-compatible. “Unlike standard PCs that do not include a UDC, a Raspberry Pi 4 contains a UDC, which is connected to its USB-C port.” He also added that you need a minimum support Rust version.is 1.73.0
Sebastian’s post on Reddit gives more insights into his project. You will also find the link to his GitHub repository where you will find the project files and source code for the library have been published there in case you like to take a friendly peek into it.
Documentation is available here.