|
| 1 | +--- |
| 2 | +title: "Supporting USB Monitor Control in the Linux kernel - Initial RFC and Updates" |
| 3 | +tags: ["kernel", "display", "hid", "usb monitor control"] |
| 4 | +date: 2025-03-27T20:50:10-07:00 |
| 5 | +draft: false |
| 6 | +--- |
| 7 | + |
| 8 | +I know my last post was on March 2nd, 2025 and this post is now at the end of |
| 9 | +the month. My dayjob has been busy, I am planning a small backpacking trip for |
| 10 | +April 5-6, and I will be in Japan most of May. However, a lot has happened with |
| 11 | +my work for implementing USB Monitor Control Class support in the Linux kernel |
| 12 | +and architecting support in general for external monitor blacklight support in |
| 13 | +the DRM subsystem. |
| 14 | + |
| 15 | +First of all, I have posted a functional RFC for the HID Rust binding work on |
| 16 | +the Linux kernel mailing list. |
| 17 | + |
| 18 | + [[https://lore.kernel.org/rust-for-linux/ [email protected]/]] |
| 19 | + |
| 20 | +This was pretty exciting. I mostly just probed and traced the remove callback |
| 21 | +for the NVIDIA Shield Controller (2017). I chose this device for testing for the |
| 22 | +simple reason of "it was the HID device nearest to me on my messy desk." While |
| 23 | +small, it's a nice first step. |
| 24 | + |
| 25 | +I have the work in its messier form before I squashed changes for a proper patch |
| 26 | +series. |
| 27 | + |
| 28 | + [[https://github.com/torvalds/linux/compare/master...Binary-Eater:linux:8c0e25d500e3d3031b2d60d208704c14cbaa8891]] |
| 29 | + |
| 30 | +I also have a branch which is cleaned up and contains the patches I posted on |
| 31 | +the mailing list. I plan to reset the ~rust-hid~ branch on top of the |
| 32 | +~rust-hid-rfc~ branch and continue the development on the ~rust-hid~ branch |
| 33 | +based on the feedback I got on the mailing list. |
| 34 | + |
| 35 | + [[https://github.com/Binary-Eater/linux/tree/rust-hid-rfc]] |
| 36 | + |
| 37 | +Let me quickly discuss the feedback and the next steps that then need to be |
| 38 | +taken. |
| 39 | + |
| 40 | +All the feedback I received on the mailing list was very helpful. Benjamin |
| 41 | +Tissoires gave some great suggestions for general direction for the Rust HID |
| 42 | +abstractions. |
| 43 | + |
| 44 | +In general, starting with a "simple" HID driver and then moving to implementing |
| 45 | +something like USB Monitor Control Class that will need DRM wiring seems to be |
| 46 | +preferable for the Rust HID bindings. |
| 47 | + |
| 48 | +This "simple" driver would only make use of very simple HID API callbacks like |
| 49 | +~.report_fixup()~ and ~.raw_event()~ ideally. |
| 50 | + |
| 51 | +I am looking at implementing a Rust reference driver for the Glorious PC Gaming |
| 52 | +Race mice. One issue though is that all the modern Glorious mouse models that I |
| 53 | +can buy are "version 2". I wouldn't be surprised if the bugs in their HID |
| 54 | +reports were fixed in the newer revisions. It would actually be really helpful |
| 55 | +for me if anyone who already owns a Glorious Model O 2, O- 2, or I 2 mouse can |
| 56 | +share the HID Report Descriptor using ~hid-recorder~ from [[https://gitlab.freedesktop.org/libevdev/hid-tools][hid-tools]] or using |
| 57 | +~hid-decode~ just to collect the parsed report descriptor. More information can |
| 58 | +be found in the [[https://docs.kernel.org/hid/hidintro.html#parsing-hid-report-descriptors][Parsing HID report descriptors section of the Linux kernel |
| 59 | +documentation]]. |
| 60 | + |
| 61 | + 1. You can make a public GitHub Gist with this information and mention |
| 62 | + ~@Binary-Eater~ in a comment, so I get a notification about it. |
| 63 | + 2. You can reach me over [[https://matrix.to/#/@binary-eater:beater.town][Matrix]]. |
| 64 | + |
| 65 | +One issue with this reference driver is it does not exercise the need for a |
| 66 | +~.remove()~ callback equivalent. This might be a blessing and a curse. With |
| 67 | +Rust's borrow checker, there is a lot of interest in the kernel community to |
| 68 | +architect paths for automatic resource cleanup. In typical C Linux drivers, a |
| 69 | +common mistake is failing to clean up resources when a fatal error is |
| 70 | +encountered. Being able to somehow "automagically" handle this while still |
| 71 | +giving developers control over allocations and resource management to a degree |
| 72 | +would be the perfect balance for kernel-mode drivers. Benjamin Tissoires gave |
| 73 | +the following feedback in the review. |
| 74 | + |
| 75 | +#+BEGIN_QUOTE |
| 76 | +I wonder however how and if we could enforce the remove() to be |
| 77 | +automated by the binding or rust, to not have to deal with resource |
| 78 | +leaking. Because the removal part, especially on failure, is the hardest |
| 79 | +part. |
| 80 | +#+END_QUOTE |
| 81 | + |
| 82 | +The reason I hesitated with doing this initially is due to some limitations in |
| 83 | +Rust's ~Drop~ trait. One of the biggest caveats is it's not well suited for |
| 84 | +ordering resource cleanup and describing a chain of dependencies. Here is an |
| 85 | +example I provide on the mailing list. |
| 86 | + |
| 87 | + [[https://lore.kernel.org/rust-for-linux/ [email protected]/]] |
| 88 | + |
| 89 | +I did get some useful feedback from Daniel Brooks on this topic. I haven't |
| 90 | +responded yet because I am exploring the kernel's [[https://rust.docs.kernel.org/kernel/devres/struct.Devres.html][Devres]] and [[https://rust.docs.kernel.org/kernel/revocable/struct.Revocable.html][Revocable]] support |
| 91 | +in Rust and seeing if that can provide a solution. |
| 92 | + |
| 93 | +I learned from Danilo Krummrich that, on the Rust for Linux side, there are a |
| 94 | +lot more generalized in-tree abstractions for many of the utilities I |
| 95 | +implemented for the Rust HID bindings myself. I will be making use of the |
| 96 | +in-tree helpers in my next revision. |
| 97 | + |
| 98 | +Anyways, I am hoping to make more progress hopefully by the weekend of April |
| 99 | +11-12. I am also hoping to have additional time I normally would not have to |
| 100 | +work on this during nights I am on vacation in May. |
0 commit comments