Conceptual
Login

The Driver Trait with probe and unbind

A driver is a type that implements a trait with two moments: probe, which runs when matching hardware appears and either sets everything up or returns an error, and unbind, which runs when the hardware or the driver goes away. Probe returns your driver's state as a pinned, reference-counted value that the kernel stores and hands back on every later callback, so there is no global variable and no manual free. Because that state is owned, its Drop implementation is the real teardown, and unbind is reserved for the few things that must happen while the device is still bound. The C bug this kills is the remove function that forgets one of the eight things probe did.

Questions this Concept answers

  • Why does most teardown in a Rust driver live in the state's `Drop` rather than in `unbind`?