Rust Drives A Linux USB Device

In theory, writing a Linux device driver shouldn’t be that hard, but it is harder than it looks. However, using libusb, you can easily deal with USB devices from user space, which, for many purposes, is fine.  [Crescentrose] didn’t know anything about writing user-space USB drivers until they wrote one and documented it for us. Oh, the code is in Rust, for which there aren’t as many examples.

The device in question was a USB hub with some extra lights and gadgets. So the real issue, it seems to us, wasn’t the code, but figuring out the protocol and the USB stack. The post covers that, too, explaining configurations, interfaces, and endpoints.

There are other ancillary topics, too, like setting up udev. This lets you load things when a USB device (or something else) plugs in.

Of course, you came for the main code. The Rust program is fairly straightforward once you have the preliminaries out of the way. The libusb library helps a lot. By the end, the code kicks off some threads, handles interrupts, and does other device-driver-like things.

So if you like Rust and you ever thought about a user space device driver for a USB device, this is your chance to see it done. It didn’t take years. However, you can do a lot in user space.

7 thoughts on “Rust Drives A Linux USB Device

      1. Reminds me of the diagnostic tool Nvidia uses for all of its testing and debugging. It is called MODS which either stands for “MOdular Diagnostic System” or “MOnolithic Diagnostic System” depending on who you ask.

      2. I consider it an hybrid kernel, because it allows to create device drivers in user space too (you can create character and block devices, use FUSE for file systems, libusb for usb devices… And what is more important: they are being used a lot). Also, the trend is to move to user space as much as possible.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.