Lines Matching +full:phy +full:- +full:bindings

1 // SPDX-License-Identifier: GPL-2.0
5 //! Network PHY device.
7 //! C headers: [`include/linux/phy.h`](../../../../../../../include/linux/phy.h).
9 use crate::{bindings, error::*, prelude::*, str::CStr, types::Opaque};
13 /// PHY state machine states.
17 /// Some of PHY drivers access to the state of PHY's software state machine.
19 /// [`enum phy_state`]: ../../../../../../../include/linux/phy.h
22 /// PHY device and driver are not ready for anything.
24 /// PHY is ready to send and receive packets.
26 /// PHY is up, but no polling or interrupts are done.
28 /// PHY is up, but is in an error state.
30 /// PHY and attached device are ready to do work.
32 /// PHY is currently running.
34 /// PHY is up, but not currently plugged in.
36 /// PHY is performing a cable test.
42 /// PHY drivers get duplex information from hardware and update the current state.
44 /// PHY is in full-duplex mode.
46 /// PHY is in half-duplex mode.
48 /// PHY is in unknown duplex mode.
52 /// An instance of a PHY device.
56 /// A [`Device`] instance is created when a callback in [`Driver`] is executed. A PHY driver
64 /// [`struct phy_device`]: ../../../../../../../include/linux/phy.h
72 pub struct Device(Opaque<bindings::phy_device>);
82 unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self { in from_raw()
83 // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::phy_device`. in from_raw()
90 /// Gets the id of the PHY.
91 pub fn phy_id(&self) -> u32 { in phy_id()
98 /// Gets the state of PHY state machine states.
99 pub fn state(&self) -> DeviceState { in state()
107 bindings::phy_state_PHY_DOWN => DeviceState::Down, in state()
108 bindings::phy_state_PHY_READY => DeviceState::Ready, in state()
109 bindings::phy_state_PHY_HALTED => DeviceState::Halted, in state()
110 bindings::phy_state_PHY_ERROR => DeviceState::Error, in state()
111 bindings::phy_state_PHY_UP => DeviceState::Up, in state()
112 bindings::phy_state_PHY_RUNNING => DeviceState::Running, in state()
113 bindings::phy_state_PHY_NOLINK => DeviceState::NoLink, in state()
114 bindings::phy_state_PHY_CABLETEST => DeviceState::CableTest, in state()
122 pub fn is_link_up(&self) -> bool { in is_link_up()
132 /// Gets the current auto-negotiation configuration.
134 /// It returns true if auto-negotiation is enabled.
135 pub fn is_autoneg_enabled(&self) -> bool { in is_autoneg_enabled()
141 bit_field.get(13, 1) == bindings::AUTONEG_ENABLE as u64 in is_autoneg_enabled()
144 /// Gets the current auto-negotiation state.
146 /// It returns true if auto-negotiation is completed.
147 pub fn is_autoneg_completed(&self) -> bool { in is_autoneg_completed()
157 /// Sets the speed of the PHY.
169 DuplexMode::Full => bindings::DUPLEX_FULL as i32, in set_duplex()
170 DuplexMode::Half => bindings::DUPLEX_HALF as i32, in set_duplex()
171 DuplexMode::Unknown => bindings::DUPLEX_UNKNOWN as i32, in set_duplex()
178 /// Reads a given C22 PHY register.
180 pub fn read(&mut self, regnum: u16) -> Result<u16> { in read()
186 bindings::mdiobus_read((*phydev).mdio.bus, (*phydev).mdio.addr, regnum.into()) in read()
195 /// Writes a given C22 PHY register.
196 pub fn write(&mut self, regnum: u16, val: u16) -> Result { in write()
202 bindings::mdiobus_write((*phydev).mdio.bus, (*phydev).mdio.addr, regnum.into(), val) in write()
207 pub fn read_paged(&mut self, page: u16, regnum: u16) -> Result<u16> { in read_paged()
211 let ret = unsafe { bindings::phy_read_paged(phydev, page.into(), regnum.into()) }; in read_paged()
219 /// Resolves the advertisements into PHY settings.
224 unsafe { bindings::phy_resolve_aneg_linkmode(phydev) }; in resolve_aneg_linkmode()
227 /// Executes software reset the PHY via `BMCR_RESET` bit.
228 pub fn genphy_soft_reset(&mut self) -> Result { in genphy_soft_reset()
232 to_result(unsafe { bindings::genphy_soft_reset(phydev) }) in genphy_soft_reset()
235 /// Initializes the PHY.
236 pub fn init_hw(&mut self) -> Result { in init_hw()
240 to_result(unsafe { bindings::phy_init_hw(phydev) }) in init_hw()
243 /// Starts auto-negotiation.
244 pub fn start_aneg(&mut self) -> Result { in start_aneg()
248 to_result(unsafe { bindings::_phy_start_aneg(phydev) }) in start_aneg()
251 /// Resumes the PHY via `BMCR_PDOWN` bit.
252 pub fn genphy_resume(&mut self) -> Result { in genphy_resume()
256 to_result(unsafe { bindings::genphy_resume(phydev) }) in genphy_resume()
259 /// Suspends the PHY via `BMCR_PDOWN` bit.
260 pub fn genphy_suspend(&mut self) -> Result { in genphy_suspend()
264 to_result(unsafe { bindings::genphy_suspend(phydev) }) in genphy_suspend()
268 pub fn genphy_read_status(&mut self) -> Result<u16> { in genphy_read_status()
272 let ret = unsafe { bindings::genphy_read_status(phydev) }; in genphy_read_status()
281 pub fn genphy_update_link(&mut self) -> Result { in genphy_update_link()
285 to_result(unsafe { bindings::genphy_update_link(phydev) }) in genphy_update_link()
289 pub fn genphy_read_lpa(&mut self) -> Result { in genphy_read_lpa()
293 to_result(unsafe { bindings::genphy_read_lpa(phydev) }) in genphy_read_lpa()
296 /// Reads PHY abilities.
297 pub fn genphy_read_abilities(&mut self) -> Result { in genphy_read_abilities()
301 to_result(unsafe { bindings::genphy_read_abilities(phydev) }) in genphy_read_abilities()
305 /// Defines certain other features this PHY supports (like interrupts).
309 /// PHY is internal.
310 pub const IS_INTERNAL: u32 = bindings::PHY_IS_INTERNAL;
311 /// PHY needs to be reset after the refclk is enabled.
312 pub const RST_AFTER_CLK_EN: u32 = bindings::PHY_RST_AFTER_CLK_EN;
313 /// Polling is used to detect PHY status changes.
314 pub const POLL_CABLE_TEST: u32 = bindings::PHY_POLL_CABLE_TEST;
316 pub const ALWAYS_CALL_SUSPEND: u32 = bindings::PHY_ALWAYS_CALL_SUSPEND;
319 /// An adapter for the registration of a PHY driver.
329 phydev: *mut bindings::phy_device, in soft_reset_callback()
330 ) -> core::ffi::c_int { in soft_reset_callback()
333 // where we hold `phy_device->lock`, so the accessors on in soft_reset_callback()
345 phydev: *mut bindings::phy_device, in get_features_callback()
346 ) -> core::ffi::c_int { in get_features_callback()
349 // where we hold `phy_device->lock`, so the accessors on in get_features_callback()
360 unsafe extern "C" fn suspend_callback(phydev: *mut bindings::phy_device) -> core::ffi::c_int { in suspend_callback()
363 // `Device` are okay to call even though `phy_device->lock` in suspend_callback()
374 unsafe extern "C" fn resume_callback(phydev: *mut bindings::phy_device) -> core::ffi::c_int { in resume_callback()
377 // `Device` are okay to call even though `phy_device->lock` in resume_callback()
389 phydev: *mut bindings::phy_device, in config_aneg_callback()
390 ) -> core::ffi::c_int { in config_aneg_callback()
393 // where we hold `phy_device->lock`, so the accessors on in config_aneg_callback()
405 phydev: *mut bindings::phy_device, in read_status_callback()
406 ) -> core::ffi::c_int { in read_status_callback()
409 // where we hold `phy_device->lock`, so the accessors on in read_status_callback()
421 phydev: *mut bindings::phy_device, in match_phy_device_callback()
422 ) -> core::ffi::c_int { in match_phy_device_callback()
424 // where we hold `phy_device->lock`, so the accessors on in match_phy_device_callback()
434 phydev: *mut bindings::phy_device, in read_mmd_callback()
437 ) -> i32 { in read_mmd_callback()
440 // where we hold `phy_device->lock`, so the accessors on in read_mmd_callback()
453 phydev: *mut bindings::phy_device, in write_mmd_callback()
457 ) -> i32 { in write_mmd_callback()
460 // where we hold `phy_device->lock`, so the accessors on in write_mmd_callback()
471 unsafe extern "C" fn link_change_notify_callback(phydev: *mut bindings::phy_device) { in link_change_notify_callback()
473 // where we hold `phy_device->lock`, so the accessors on in link_change_notify_callback()
480 /// Driver structure for a particular PHY type.
483 /// This is used to register a driver for a particular PHY type with the kernel.
489 /// [`struct phy_driver`]: ../../../../../../../include/linux/phy.h
491 pub struct DriverVTable(Opaque<bindings::phy_driver>);
502 pub const fn create_phy_driver<T: Driver>() -> DriverVTable { in create_phy_driver()
504 DriverVTable(Opaque::new(bindings::phy_driver { in create_phy_driver()
561 ..unsafe { core::mem::MaybeUninit::<bindings::phy_driver>::zeroed().assume_init() } in create_phy_driver()
565 /// Driver implementation for a particular PHY type.
570 /// Defines certain other features this PHY supports.
574 /// The friendly name of this PHY type.
581 /// Issues a PHY software reset.
582 fn soft_reset(_dev: &mut Device) -> Result { in soft_reset()
587 fn get_features(_dev: &mut Device) -> Result { in get_features()
593 fn match_phy_device(_dev: &Device) -> bool { in match_phy_device()
597 /// Configures the advertisement and resets auto-negotiation
598 /// if auto-negotiation is enabled.
599 fn config_aneg(_dev: &mut Device) -> Result { in config_aneg()
604 fn read_status(_dev: &mut Device) -> Result<u16> { in read_status()
609 fn suspend(_dev: &mut Device) -> Result { in suspend()
614 fn resume(_dev: &mut Device) -> Result { in resume()
619 fn read_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16) -> Result<u16> { in read_mmd()
624 fn write_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16, _val: u16) -> Result { in write_mmd()
632 /// Registration structure for PHY drivers.
644 /// Registers a PHY driver.
648 ) -> Result<Self> { in register()
656 bindings::phy_drivers_register(drivers[0].0.get(), drivers.len().try_into()?, module.0) in register()
668 bindings::phy_drivers_unregister(self.drivers[0].0.get(), self.drivers.len() as i32) in drop()
673 /// An identifier for PHY devices on an MDIO/MII bus.
676 /// PHY driver.
684 pub const fn new_with_exact_mask(id: u32) -> Self { in new_with_exact_mask()
692 pub const fn new_with_model_mask(id: u32) -> Self { in new_with_model_mask()
700 pub const fn new_with_vendor_mask(id: u32) -> Self { in new_with_vendor_mask()
708 pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self { in new_with_custom_mask()
716 pub const fn new_with_driver<T: Driver>() -> Self { in new_with_driver()
721 pub const fn mask_as_int(&self) -> u32 { in mask_as_int()
727 pub const fn mdio_device_id(&self) -> bindings::mdio_device_id { in mdio_device_id()
728 bindings::mdio_device_id { in mdio_device_id()
747 const fn as_int(&self) -> u32 { in as_int()
768 /// use kernel::net::phy::{self, DeviceId};
785 /// impl phy::Driver for PhySample {
787 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
796 /// use kernel::net::phy::{self, DeviceId};
800 /// _reg: ::kernel::net::phy::Registration,
814 /// impl phy::Driver for PhySample {
816 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
820 /// static mut DRIVERS: [::kernel::net::phy::DriverVTable; 1] =
821 /// [::kernel::net::phy::create_phy_driver::<PhySample>()];
824 /// fn init(module: &'static ThisModule) -> Result<Self> {
826 /// let mut reg = ::kernel::net::phy::Registration::register(
837 /// static __mod_mdio__phydev_device_table: [::kernel::bindings::mdio_device_id; 2] = [
838 /// ::kernel::bindings::mdio_device_id {
842 /// ::kernel::bindings::mdio_device_id {
860 static __mod_mdio__phydev_device_table: [$crate::bindings::mdio_device_id;
863 $crate::bindings::mdio_device_id {
872 _reg: $crate::net::phy::Registration,
881 static mut DRIVERS: [$crate::net::phy::DriverVTable;
883 [$($crate::net::phy::create_phy_driver::<$driver>()),+];
886 fn init(module: &'static ThisModule) -> Result<Self> {
890 let mut reg = $crate::net::phy::Registration::register(