Lines Matching +full:c +full:- +full:phy

1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2023 FUJITA Tomonori <fujita.tomonori@gmail.com>
5 //! Network PHY device.
7 //! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h).
14 /// PHY state machine states.
18 /// Some of PHY drivers access to the state of PHY's software state machine.
20 /// [`enum phy_state`]: srctree/include/linux/phy.h
23 /// PHY device and driver are not ready for anything.
25 /// PHY is ready to send and receive packets.
27 /// PHY is up, but no polling or interrupts are done.
29 /// PHY is up, but is in an error state.
31 /// PHY and attached device are ready to do work.
33 /// PHY is currently running.
35 /// PHY is up, but not currently plugged in.
37 /// PHY is performing a cable test.
43 /// PHY drivers get duplex information from hardware and update the current state.
45 /// PHY is in full-duplex mode.
47 /// PHY is in half-duplex mode.
49 /// PHY is in unknown duplex mode.
53 /// An instance of a PHY device.
57 /// A [`Device`] instance is created when a callback in [`Driver`] is executed. A PHY driver
62 /// - Referencing a `phy_device` using this struct asserts that you are in
64 /// - This struct always has a valid `self.0.mdio.dev`.
66 /// [`struct phy_device`]: srctree/include/linux/phy.h
67 // During the calls to most functions in [`Driver`], the C side (`PHYLIB`) holds a lock that is
82 /// - the pointer must point at a valid `phy_device`, and the caller
85 /// - `(*ptr).mdio.dev` must be a valid.
86 unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
94 /// Gets the id of the PHY.
95 pub fn phy_id(&self) -> u32 {
102 /// Gets the state of PHY state machine states.
103 pub fn state(&self) -> DeviceState {
126 pub fn is_link_up(&self) -> bool {
136 /// Gets the current auto-negotiation configuration.
138 /// It returns true if auto-negotiation is enabled.
139 pub fn is_autoneg_enabled(&self) -> bool {
148 /// Gets the current auto-negotiation state.
150 /// It returns true if auto-negotiation is completed.
151 pub fn is_autoneg_completed(&self) -> bool {
161 /// Sets the speed of the PHY.
182 /// Reads a PHY register.
184 pub fn read<R: reg::Register>(&mut self, reg: R) -> Result<u16> {
188 /// Writes a PHY register.
189 pub fn write<R: reg::Register>(&mut self, reg: R, val: u16) -> Result {
194 pub fn read_paged(&mut self, page: u16, regnum: u16) -> Result<u16> {
203 /// Resolves the advertisements into PHY settings.
211 /// Executes software reset the PHY via `BMCR_RESET` bit.
212 pub fn genphy_soft_reset(&mut self) -> Result {
219 /// Initializes the PHY.
220 pub fn init_hw(&mut self) -> Result {
227 /// Starts auto-negotiation.
228 pub fn start_aneg(&mut self) -> Result {
235 /// Resumes the PHY via `BMCR_PDOWN` bit.
236 pub fn genphy_resume(&mut self) -> Result {
243 /// Suspends the PHY via `BMCR_PDOWN` bit.
244 pub fn genphy_suspend(&mut self) -> Result {
252 pub fn genphy_read_status<R: reg::Register>(&mut self) -> Result<u16> {
257 pub fn genphy_update_link(&mut self) -> Result {
265 pub fn genphy_read_lpa(&mut self) -> Result {
272 /// Reads PHY abilities.
273 pub fn genphy_read_abilities(&mut self) -> Result {
282 fn as_ref(&self) -> &kernel::device::Device {
289 /// Defines certain other features this PHY supports (like interrupts).
293 /// PHY is internal.
295 /// PHY needs to be reset after the refclk is enabled.
297 /// Polling is used to detect PHY status changes.
303 /// An adapter for the registration of a PHY driver.
312 unsafe extern "C" fn soft_reset_callback(phydev: *mut bindings::phy_device) -> c_int {
315 // where we hold `phy_device->lock`, so the accessors on
326 unsafe extern "C" fn probe_callback(phydev: *mut bindings::phy_device) -> c_int {
341 unsafe extern "C" fn get_features_callback(phydev: *mut bindings::phy_device) -> c_int {
344 // where we hold `phy_device->lock`, so the accessors on
355 unsafe extern "C" fn suspend_callback(phydev: *mut bindings::phy_device) -> c_int {
357 // SAFETY: The C core code ensures that the accessors on
358 // `Device` are okay to call even though `phy_device->lock`
369 unsafe extern "C" fn resume_callback(phydev: *mut bindings::phy_device) -> c_int {
371 // SAFETY: The C core code ensures that the accessors on
372 // `Device` are okay to call even though `phy_device->lock`
383 unsafe extern "C" fn config_aneg_callback(phydev: *mut bindings::phy_device) -> c_int {
386 // where we hold `phy_device->lock`, so the accessors on
397 unsafe extern "C" fn read_status_callback(phydev: *mut bindings::phy_device) -> c_int {
400 // where we hold `phy_device->lock`, so the accessors on
411 unsafe extern "C" fn match_phy_device_callback(
414 ) -> c_int {
416 // where we hold `phy_device->lock`, so the accessors on
425 unsafe extern "C" fn read_mmd_callback(
429 ) -> i32 {
432 // where we hold `phy_device->lock`, so the accessors on
435 // CAST: the C side verifies devnum < 32.
444 unsafe extern "C" fn write_mmd_callback(
449 ) -> i32 {
452 // where we hold `phy_device->lock`, so the accessors on
463 unsafe extern "C" fn link_change_notify_callback(phydev: *mut bindings::phy_device) {
465 // where we hold `phy_device->lock`, so the accessors on
472 /// Driver structure for a particular PHY type.
475 /// This is used to register a driver for a particular PHY type with the kernel.
481 /// [`struct phy_driver`]: srctree/include/linux/phy.h
494 pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
562 /// Driver implementation for a particular PHY type.
567 /// Defines certain other features this PHY supports.
571 /// The friendly name of this PHY type.
578 /// Issues a PHY software reset.
579 fn soft_reset(_dev: &mut Device) -> Result {
583 /// Sets up device-specific structures during discovery.
584 fn probe(_dev: &mut Device) -> Result {
589 fn get_features(_dev: &mut Device) -> Result {
595 fn match_phy_device(_dev: &Device) -> bool {
599 /// Configures the advertisement and resets auto-negotiation
600 /// if auto-negotiation is enabled.
601 fn config_aneg(_dev: &mut Device) -> Result {
606 fn read_status(_dev: &mut Device) -> Result<u16> {
611 fn suspend(_dev: &mut Device) -> Result {
616 fn resume(_dev: &mut Device) -> Result {
621 fn read_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16) -> Result<u16> {
626 fn write_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16, _val: u16) -> Result {
634 /// Registration structure for PHY drivers.
650 /// Registers a PHY driver.
654 ) -> Result<Self> {
679 /// An identifier for PHY devices on an MDIO/MII bus.
682 /// PHY driver.
689 pub const fn new_with_exact_mask(id: u32) -> Self {
697 pub const fn new_with_model_mask(id: u32) -> Self {
705 pub const fn new_with_vendor_mask(id: u32) -> Self {
713 pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
721 pub const fn new_with_driver<T: Driver>() -> Self {
725 /// Get the MDIO device's PHY ID.
726 pub const fn id(&self) -> u32 {
731 pub const fn mask_as_int(&self) -> u32 {
737 pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
760 const fn as_int(&self) -> u32 {
781 /// use kernel::net::phy::{self, DeviceId};
798 /// impl phy::Driver for PhySample {
800 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
809 /// use kernel::net::phy::{self, DeviceId};
813 /// _reg: ::kernel::net::phy::Registration,
827 /// impl phy::Driver for PhySample {
829 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
833 /// static mut DRIVERS: [::kernel::net::phy::DriverVTable; 1] =
834 /// [::kernel::net::phy::create_phy_driver::<PhySample>()];
837 /// fn init(module: &'static ::kernel::ThisModule) -> Result<Self> {
839 /// let mut reg = ::kernel::net::phy::Registration::register(
850 /// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId, (), N> =
852 /// ::kernel::net::phy::DeviceId(
872 const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId, (), N> =
880 _reg: $crate::net::phy::Registration,
889 static mut DRIVERS: [$crate::net::phy::DriverVTable;
891 [$($crate::net::phy::create_phy_driver::<$driver>()),+];
894 fn init(module: &'static $crate::ThisModule) -> Result<Self> {
896 // the `DRIVERS` static. The array is used only in the C side.
898 let mut reg = $crate::net::phy::Registration::register(