Skip to content

Commit

Permalink
driver/net/lan9250: Add lan9250_ioctl and lan9250_uninitialize APIs
Browse files Browse the repository at this point in the history
    - use 'lan9250_ioctl' to set and get the value of lan9250 phy register
    - use `lan9250_uninitialize` to un-initialize the ethernet driver

Signed-off-by: [email protected] <[email protected]>
  • Loading branch information
cwespressif authored and xiaoxiang781216 committed Jul 15, 2024
1 parent 8bdc975 commit e0b9921
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
121 changes: 121 additions & 0 deletions drivers/net/lan9250.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ static int lan9250_rmmac(FAR struct net_driver_s *dev,
FAR const uint8_t *mac);
#endif

#ifdef CONFIG_NETDEV_IOCTL
static int lan9250_ioctl(FAR struct net_driver_s *dev, int cmd,
unsigned long arg);
#endif

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -2322,6 +2327,79 @@ static int lan9250_rmmac(FAR struct net_driver_s *dev,
}
#endif

/****************************************************************************
* Name: lan9250_ioctl
*
* Description:
* Handle network IOCTL commands directed to this device.
*
* Parameters:
* dev - Reference to the NuttX driver state structure
* cmd - The IOCTL command
* arg - The argument for the IOCTL command
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
* The network is locked.
*
****************************************************************************/

#ifdef CONFIG_NETDEV_IOCTL
static int lan9250_ioctl(FAR struct net_driver_s *dev, int cmd,
unsigned long arg)
{
FAR struct lan9250_driver_s *priv =
(FAR struct lan9250_driver_s *)dev->d_private;
int ret = OK;

/* Lock the SPI bus so that we have exclusive access */

lan9250_lock_spi(priv);

/* Decode and dispatch the driver-specific IOCTL command */

switch (cmd)
{
#ifdef CONFIG_NETDEV_PHY_IOCTL
case SIOCGMIIPHY: /* Get MII PHY address */
{
struct mii_ioctl_data_s *req =
(struct mii_ioctl_data_s *)((uintptr_t)arg);
req->phy_id = 0;
}
break;

case SIOCGMIIREG: /* Get register from MII PHY */
{
struct mii_ioctl_data_s *req =
(struct mii_ioctl_data_s *)((uintptr_t)arg);
req->val_out = lan9250_get_phyreg(priv, req->reg_num);
}
break;

case SIOCSMIIREG: /* Set register in MII PHY */
{
struct mii_ioctl_data_s *req =
(struct mii_ioctl_data_s *)((uintptr_t)arg);
lan9250_set_phyreg(priv, req->reg_num, req->val_in);
}
break;
#endif /* CONFIG_NETDEV_PHY_IOCTL */

default:
nerr("ERROR: Unrecognized IOCTL command: %d\n", cmd);
ret = -ENOTTY; /* Special return value for this case */
}

/* Un-lock the SPI bus */

lan9250_unlock_spi(priv);
return ret;
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -2367,6 +2445,9 @@ int lan9250_initialize(
#ifdef CONFIG_NET_MCASTGROUP
dev->d_addmac = lan9250_addmac;
dev->d_rmmac = lan9250_rmmac;
#endif
#ifdef CONFIG_NETDEV_IOCTL
dev->d_ioctl = lan9250_ioctl; /* Handle network IOCTL commands */
#endif
dev->d_private = priv;

Expand Down Expand Up @@ -2398,3 +2479,43 @@ int lan9250_initialize(

return netdev_register(dev, NET_LL_ETHERNET);
}

/****************************************************************************
* Function: lan9250_uninitialize
*
* Description:
* Un-initialize the Ethernet driver
*
* Input Parameters:
* lower - The MCU-specific interrupt used to control low-level MCU
* functions (i.e., LAN9250 GPIO interrupts).
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
*
****************************************************************************/

int lan9250_uninitialize(FAR const struct lan9250_lower_s *lower)
{
FAR struct lan9250_driver_s *priv = &g_lan9250;
FAR struct net_driver_s *dev = &priv->dev;

int ret = netdev_unregister(dev); /* No such interface yet */
if (ret < 0)
{
nerr("ERROR: netdev_unregister failed: %d\n", ret);
return ret;
}

/* Detach the interrupt to the driver */

if (lower->attach(lower, NULL, NULL) < 0)
{
nerr("ERROR: irq_detach failed: %d\n", ret);
return -EAGAIN;
}

return OK;
}
19 changes: 19 additions & 0 deletions include/nuttx/net/lan9250.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ int lan9250_initialize(
#endif
FAR const struct lan9250_lower_s *lower);

/****************************************************************************
* Function: lan9250_uninitialize
*
* Description:
* Un-initialize the Ethernet driver
*
* Input Parameters:
* lower - The MCU-specific interrupt used to control low-level MCU
* functions (i.e., LAN9250 GPIO interrupts).
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
*
****************************************************************************/

int lan9250_uninitialize(FAR const struct lan9250_lower_s *lower);

#undef EXTERN
#ifdef __cplusplus
}
Expand Down

0 comments on commit e0b9921

Please sign in to comment.