Writing a basic Linux device driver when you know nothing about Linux drivers
10 months ago
- #Linux
- #USB
- #Rust
- The author purchased a Nanoleaf Pegboard Desk Dock, which only supports Windows and macOS, prompting the development of a Linux driver.
- After reverse-engineering the device's protocol and receiving official documentation from Nanoleaf, the author decided to write a Linux driver.
- The device was identified using `lsusb`, revealing it as a Human Interface Device (HID) with a generic USB driver in the Linux kernel.
- Two options were considered for the driver: a kernel driver or a userspace driver via `libusb`. The author chose the latter for simplicity.
- A `udev` rule was created to allow non-root users to access the device, with specific instructions for NixOS users.
- The driver was written in Rust using the `rusb` crate, starting with basic device enumeration and interface claiming.
- The author encountered and resolved issues with the kernel driver holding the device open, requiring manual detachment.
- Data was sent to the device using interrupt endpoints, with initial success in changing the device's LED color.
- The driver was improved to handle interrupts properly, including polling for device responses in a background thread.
- The final proof-of-concept demonstrated that writing a simple USB device driver is feasible with minimal code.