Hasty Briefsbeta

Bilingual

Rust in the Linux kernel: part 2

10 months ago
  • #Rust
  • #Driver Development
  • #Linux Kernel
  • Fujita Tomonori wrote a Rust version of the Asix AX88796B embedded Ethernet controller driver in 2023, serving as a simple example for comparing Rust and C in the kernel.
  • The Rust driver starts with legalities and documentation comments, followed by a `use` statement for importing necessary modules from the kernel crate.
  • A macro `kernel::module_phy_driver!` is used to set up symbols for the PHY driver, similar to C's `module_phy_driver()` macro.
  • Constants `BMCR_SPEED100` and `BMCR_FULLDPLX` are defined in Rust, with explicit type annotations and casts from C-defined constants.
  • A shared function `asix_soft_reset` demonstrates Rust's error handling with `Result` and the try operator `?`, contrasting with C's error propagation.
  • The main driver for AX88796B is implemented as an empty structure `PhyAX88796B` with a `Driver` trait implementation, using `#[vtable]` to interface with C's PHY driver infrastructure.
  • The article concludes with a summary comparing steps to implement a PHY driver in C versus Rust, highlighting Rust's trait system and macro usage.