Re: [PATCH v1 1/3] rust: core abstractions for network PHY drivers
From: Greg KH <hidden>
Date: 2023-10-02 09:14:43
Also in:
rust-for-linux
On Mon, Oct 02, 2023 at 05:53:00PM +0900, FUJITA Tomonori wrote:
+/// Corresponds to the kernel's `enum phy_state`.
+#[derive(PartialEq)]
+pub enum DeviceState {
+ /// PHY device and driver are not ready for anything.
+ Down,
+ /// PHY is ready to send and receive packets.
+ Ready,
+ /// PHY is up, but no polling or interrupts are done.
+ Halted,
+ /// PHY is up, but is in an error state.
+ Error,
+ /// PHY and attached device are ready to do work.
+ Up,
+ /// PHY is currently running.
+ Running,
+ /// PHY is up, but not currently plugged in.
+ NoLink,
+ /// PHY is performing a cable test.
+ CableTest,
+}I still think these should come straight from the C code, and that moving them to an enum makes sense to make this possible, but hey, it's not my subsystem to maintain! :)
+/// Wraps the kernel's `struct phy_device`.
+///
+/// # Invariants
+///
+/// `self.0` is always in a valid state.
+#[repr(transparent)]
+pub struct Device(Opaque<bindings::phy_device>);
+
+impl Device {
+ /// Creates a new [`Device`] instance from a raw pointer.
+ ///
+ /// # Safety
+ ///
+ /// For the duration of the lifetime 'a, the pointer must be valid for writing and nobody else
+ /// may read or write to the `phy_device` object.
+ pub unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
+ unsafe { &mut *ptr.cast() }
+ }
+
+ /// Gets the id of the PHY.
+ pub fn id(&mut self) -> u32 {
+ let phydev = self.0.get();
+ // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
+ unsafe { (*phydev).phy_id }
+ }Naming question, why are you making this "id" instead of "phy_id" like the C code has? Same for many of these bindings.
+ /// Returns true if the link is up.
+ pub fn get_link(&mut self) -> bool {
+ const LINK_IS_UP: u32 = 1;
+ let phydev = self.0.get();
+ // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
+ unsafe { (*phydev).link() == LINK_IS_UP }
+ }"get_" normally means to grab a reference, if this isn't matching a C call, why not just call it link_is_up or to match your other state checks "is_link_up"? but hey, I'm just bikeshedding at this point in time, if they maintainer likes these as-is, keep them :) thanks, greg k-h