rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: andrew@lunn.ch, benno.lossin@proton.me, tmgross@umich.edu
Subject: [RFC PATCH v2 2/2] rust: net::phy support for C45 genphy helpers
Date: Sat,  1 Jun 2024 13:35:35 +0900	[thread overview]
Message-ID: <20240601043535.53545-3-fujita.tomonori@gmail.com> (raw)
In-Reply-To: <20240601043535.53545-1-fujita.tomonori@gmail.com>

Add support for genphy_c45_* helper functions. Instead of adding
genphy_c45_ methods, this unifies the API for genphy functions with
trait.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/net/phy.rs | 49 +++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 286fd5a7ec91..aabd714ae3f6 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -248,16 +248,8 @@ pub fn genphy_suspend(&mut self) -> Result {
     }
 
     /// Checks the link status and updates current link state.
-    pub fn genphy_read_status(&mut self) -> Result<u16> {
-        let phydev = self.0.get();
-        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
-        // So it's just an FFI call.
-        let ret = unsafe { bindings::genphy_read_status(phydev) };
-        if ret < 0 {
-            Err(Error::from_errno(ret))
-        } else {
-            Ok(ret as u16)
-        }
+    pub fn genphy_read_status<G: reg::GenPhyOps>(&mut self) -> Result<u16> {
+        G::read_status(self)
     }
 
     /// Updates the link status.
@@ -446,6 +438,43 @@ fn write(&self, dev: &mut Device, val: u16) -> Result {
             })
         }
     }
+
+    /// PHY helper functions.
+    ///
+    /// This trait is used to implement some helper functions executed either
+    /// via C22 or C45 registers.
+    pub trait GenPhyOps {
+        /// Checks the link status and updates current link state.
+        fn read_status(dev: &mut Device) -> Result<u16>;
+    }
+
+    impl GenPhyOps for C22 {
+        fn read_status(dev: &mut Device) -> Result<u16> {
+            let phydev = dev.0.get();
+            // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
+            // So it's just an FFI call.
+            let ret = unsafe { bindings::genphy_read_status(phydev) };
+            if ret < 0 {
+                Err(Error::from_errno(ret))
+            } else {
+                Ok(ret as u16)
+            }
+        }
+    }
+
+    impl GenPhyOps for C45 {
+        fn read_status(dev: &mut Device) -> Result<u16> {
+            let phydev = dev.0.get();
+            // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
+            // So it's just an FFI call.
+            let ret = unsafe { bindings::genphy_c45_read_status(phydev) };
+            if ret < 0 {
+                Err(Error::from_errno(ret))
+            } else {
+                Ok(ret as u16)
+            }
+        }
+    }
 }
 
 /// Defines certain other features this PHY supports (like interrupts).
-- 
2.34.1


  parent reply	other threads:[~2024-06-01  4:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-01  4:35 [RFC PATCH v2 0/2] net::phy support for C45 FUJITA Tomonori
2024-06-01  4:35 ` [RFC PATCH v2 1/2] rust: net::phy support for C45 register access FUJITA Tomonori
2024-06-01 12:21   ` Benno Lossin
2024-06-02  7:24     ` FUJITA Tomonori
2024-06-02  9:59       ` Benno Lossin
2024-06-01  4:35 ` FUJITA Tomonori [this message]
2024-06-01 12:23   ` [RFC PATCH v2 2/2] rust: net::phy support for C45 genphy helpers Benno Lossin
2024-06-02  7:36     ` FUJITA Tomonori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240601043535.53545-3-fujita.tomonori@gmail.com \
    --to=fujita.tomonori@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=benno.lossin@proton.me \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).