2014-08-16 20:56:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/phy_notify.c
|
|
|
|
*
|
2020-03-07 18:07:26 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
2020-03-07 18:07:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
2020-03-07 18:07:26 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-08-17 22:07:53 +02:00
|
|
|
/* Force verbose debug on in this file only to support unit-level testing. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_PHY_DEBUG
|
2016-06-11 19:50:18 +02:00
|
|
|
# undef CONFIG_DEBUG_INFO
|
|
|
|
# define CONFIG_DEBUG_INFO 1
|
2014-08-17 22:07:53 +02:00
|
|
|
# undef CONFIG_DEBUG_NET
|
|
|
|
# define CONFIG_DEBUG_NET 1
|
|
|
|
#endif
|
|
|
|
|
2014-11-17 17:22:30 +01:00
|
|
|
#include <stdio.h>
|
2014-08-16 22:08:04 +02:00
|
|
|
#include <unistd.h>
|
2014-08-16 20:56:02 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2020-02-02 08:42:28 +01:00
|
|
|
#include <net/if.h>
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/irq.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-10-07 18:57:09 +02:00
|
|
|
#include <nuttx/signal.h>
|
2014-08-16 20:56:02 +02:00
|
|
|
#include <nuttx/net/phy.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_PHY_INTERRUPT
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-02-24 18:51:25 +01:00
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/* The current design artificially limits the number of notification client.
|
|
|
|
* This is an arbitrary limit. If you exceed it, simply adjust the affected
|
|
|
|
* areas.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_PHY_NOTIFICATION_NCLIENTS > 4
|
|
|
|
# warning Fix me!! Support currently limited to 4 clients
|
|
|
|
# undef CONFIG_PHY_NOTIFICATION_NCLIENTS
|
|
|
|
# define CONFIG_PHY_NOTIFICATION_NCLIENTS 4
|
|
|
|
#endif
|
|
|
|
|
2014-08-17 22:07:53 +02:00
|
|
|
/* Debug ********************************************************************/
|
2019-02-24 18:51:25 +01:00
|
|
|
|
2014-08-17 22:07:53 +02:00
|
|
|
/* Extra, in-depth debug output that is only available if
|
|
|
|
* CONFIG_NETDEV_PHY_DEBUG us defined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_PHY_DEBUG
|
2016-06-16 20:33:32 +02:00
|
|
|
# define phyinfo _info
|
|
|
|
# define phyerr _err
|
2014-08-17 22:07:53 +02:00
|
|
|
#else
|
2016-06-12 16:31:22 +02:00
|
|
|
# define phyinfo(x...)
|
2016-06-11 23:50:49 +02:00
|
|
|
# define phyerr(x...)
|
2014-08-17 22:07:53 +02:00
|
|
|
#endif
|
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2019-02-24 18:51:25 +01:00
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/* This describes the state of one notification. There may be up to
|
|
|
|
* CONFIG_PHY_NOTIFICATION_NCLIENTS such notifications active simultaneously.
|
|
|
|
*
|
|
|
|
* There
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct phy_notify_s
|
|
|
|
{
|
|
|
|
bool assigned;
|
2020-02-02 08:42:28 +01:00
|
|
|
char intf[IFNAMSIZ + 1];
|
2014-08-16 20:56:02 +02:00
|
|
|
pid_t pid;
|
2019-01-27 15:57:23 +01:00
|
|
|
struct sigevent event;
|
2019-01-27 16:28:59 +01:00
|
|
|
struct sigwork_s work;
|
2014-08-18 00:51:56 +02:00
|
|
|
phy_enable_t enable;
|
2014-08-16 20:56:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
static int phy_semtake(void);
|
2017-03-02 15:42:13 +01:00
|
|
|
static FAR struct phy_notify_s *phy_find_unassigned(void);
|
|
|
|
static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf,
|
|
|
|
pid_t pid);
|
|
|
|
static int phy_handler(int irq, FAR void *context, FAR void *arg);
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2019-02-24 18:51:25 +01:00
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/* Serializes access to the g_notify_clients array */
|
|
|
|
|
|
|
|
static sem_t g_notify_clients_sem = SEM_INITIALIZER(1);
|
|
|
|
|
|
|
|
/* This is a array the hold information for each PHY notification client */
|
|
|
|
|
2020-03-07 18:07:26 +01:00
|
|
|
static struct phy_notify_s
|
|
|
|
g_notify_clients[CONFIG_PHY_NOTIFICATION_NCLIENTS];
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: phy_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
static int phy_semtake(void)
|
2014-08-16 20:56:02 +02:00
|
|
|
{
|
2020-03-31 16:05:48 +02:00
|
|
|
return nxsem_wait_uninterruptible(&g_notify_clients_sem);
|
2014-08-16 20:56:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define phy_semgive() nxsem_post(&g_notify_clients_sem);
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: phy_find_unassigned
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct phy_notify_s *phy_find_unassigned(void)
|
|
|
|
{
|
|
|
|
FAR struct phy_notify_s *client;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2014-08-16 20:56:02 +02:00
|
|
|
int i;
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
ret = phy_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
phyerr("ERROR: phy_semtake failed: %d\n", ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
for (i = 0; i < CONFIG_PHY_NOTIFICATION_NCLIENTS; i++)
|
|
|
|
{
|
|
|
|
client = &g_notify_clients[i];
|
|
|
|
if (!client->assigned)
|
|
|
|
{
|
|
|
|
/* Assign and re-initialized the entry */
|
|
|
|
|
|
|
|
client->assigned = true;
|
2014-11-17 17:22:30 +01:00
|
|
|
client->intf[0] = '\0';
|
2014-08-16 20:56:02 +02:00
|
|
|
client->pid = -1;
|
2014-08-18 00:51:56 +02:00
|
|
|
client->enable = NULL;
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
/* Return the client entry assigned to the caller */
|
|
|
|
|
|
|
|
phy_semgive();
|
2016-06-12 16:31:22 +02:00
|
|
|
phyinfo("Returning client %d\n", i);
|
2014-08-16 20:56:02 +02:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ooops... too many */
|
|
|
|
|
2018-03-09 14:11:07 +01:00
|
|
|
phyerr("ERROR: No free client entries\n");
|
2014-08-16 20:56:02 +02:00
|
|
|
phy_semgive();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: phy_find_assigned
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct phy_notify_s *phy_find_assigned(FAR const char *intf,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
FAR struct phy_notify_s *client;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2014-08-16 20:56:02 +02:00
|
|
|
int i;
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
ret = phy_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
phyerr("ERROR: phy_semtake failed: %d\n", ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
for (i = 0; i < CONFIG_PHY_NOTIFICATION_NCLIENTS; i++)
|
|
|
|
{
|
|
|
|
client = &g_notify_clients[i];
|
2017-08-08 22:24:12 +02:00
|
|
|
if (client->assigned && client->pid == pid &&
|
2020-02-02 08:42:28 +01:00
|
|
|
strncmp(client->intf, intf, IFNAMSIZ) == 0)
|
2014-08-16 20:56:02 +02:00
|
|
|
{
|
|
|
|
/* Return the matching client entry to the caller */
|
|
|
|
|
|
|
|
phy_semgive();
|
2016-06-12 16:31:22 +02:00
|
|
|
phyinfo("Returning client %d\n", i);
|
2014-08-16 20:56:02 +02:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ooops... not found */
|
|
|
|
|
|
|
|
phy_semgive();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: phy_handler
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-03-02 15:42:13 +01:00
|
|
|
static int phy_handler(int irq, FAR void *context, FAR void *arg)
|
2014-08-16 20:56:02 +02:00
|
|
|
{
|
2017-03-02 15:42:13 +01:00
|
|
|
FAR struct phy_notify_s *client = (FAR struct phy_notify_s *)arg;
|
2014-08-16 20:56:02 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-03-02 15:42:13 +01:00
|
|
|
DEBUGASSERT(client != NULL && client->assigned && client->enable);
|
2019-01-27 15:57:23 +01:00
|
|
|
phyinfo("Signaling PID=%d with event %p\n", client->pid, &client->event);
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
/* Disable further interrupts */
|
|
|
|
|
|
|
|
client->enable(false);
|
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/* Signal the client that the PHY has something interesting to say to us */
|
|
|
|
|
2019-01-27 16:28:59 +01:00
|
|
|
ret = nxsig_notification(client->pid, &client->event,
|
|
|
|
SI_QUEUE, &client->work);
|
2014-08-16 20:56:02 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-01-27 15:57:23 +01:00
|
|
|
phyerr("ERROR: nxsig_notification failed: %d\n", ret);
|
2014-08-16 20:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: phy_notify_subscribe
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup up to deliver signals to the task identified by 'pid' when
|
|
|
|
* there is any change indicated by an interrupt from the PHY associated
|
|
|
|
* with 'intf'
|
|
|
|
*
|
|
|
|
* NOTE: This function is intended to be called only from an Ethernet
|
|
|
|
* driver in support of the SIOCMIISIG ioctl command. It should never
|
|
|
|
* by called directly by application logic.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-08-16 20:56:02 +02:00
|
|
|
* intf - Provides the name of the network interface, for example, "eth0".
|
2014-08-16 22:08:04 +02:00
|
|
|
* pid - Identifies the task to receive the signal. The special value
|
|
|
|
* of zero means to use the pid of the current task.
|
2019-01-27 15:57:23 +01:00
|
|
|
* event - Describes the way a task is to be notified
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-27 15:57:23 +01:00
|
|
|
int phy_notify_subscribe(FAR const char *intf, pid_t pid,
|
|
|
|
FAR struct sigevent *event)
|
2014-08-16 20:56:02 +02:00
|
|
|
{
|
|
|
|
FAR struct phy_notify_s *client;
|
2017-03-02 15:42:13 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
DEBUGASSERT(intf);
|
|
|
|
|
2019-01-27 15:57:23 +01:00
|
|
|
phyinfo("%s: PID=%d event=%p\n", intf, pid, event);
|
2014-08-17 22:07:53 +02:00
|
|
|
|
2014-08-16 22:08:04 +02:00
|
|
|
/* The special value pid == 0 means to use the pid of the current task. */
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
pid = getpid();
|
2016-06-12 16:31:22 +02:00
|
|
|
phyinfo("Actual PID=%d\n", pid);
|
2014-08-16 22:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 18:07:26 +01:00
|
|
|
/* Unsubscribe if sigev_notify field equals SIGEV_NONE */
|
|
|
|
|
|
|
|
if (event->sigev_notify == SIGEV_NONE)
|
|
|
|
{
|
|
|
|
return phy_notify_unsubscribe(intf, pid);
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
/* Check if this client already exists */
|
|
|
|
|
|
|
|
client = phy_find_assigned(intf, pid);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (client != NULL)
|
2014-08-18 00:51:56 +02:00
|
|
|
{
|
|
|
|
/* Yes.. update the signal number and argument */
|
|
|
|
|
2019-01-27 15:57:23 +01:00
|
|
|
client->event = *event;
|
2014-08-18 00:51:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No, allocate a new slot in the client notification table */
|
|
|
|
|
|
|
|
client = phy_find_unassigned();
|
2020-03-31 16:05:48 +02:00
|
|
|
if (client == NULL)
|
2014-08-18 00:51:56 +02:00
|
|
|
{
|
2018-03-09 14:11:07 +01:00
|
|
|
phyerr("ERROR: Failed to allocate a client entry\n");
|
2014-08-18 00:51:56 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the new client entry */
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
client->pid = pid;
|
2019-01-27 15:57:23 +01:00
|
|
|
client->event = *event;
|
2020-02-02 08:42:28 +01:00
|
|
|
strncpy(client->intf, intf, IFNAMSIZ + 1);
|
|
|
|
client->intf[IFNAMSIZ] = '\0';
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
/* Attach/re-attach the PHY interrupt */
|
|
|
|
|
2017-03-02 15:42:13 +01:00
|
|
|
ret = arch_phy_irq(intf, phy_handler, client, &client->enable);
|
2014-08-18 00:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable/re-enable the PH interrupt */
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
DEBUGASSERT(client->enable);
|
|
|
|
client->enable(true);
|
2017-03-02 15:42:13 +01:00
|
|
|
return ret;
|
2014-08-16 20:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: phy_notify_unsubscribe
|
2014-08-16 20:56:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop the deliver of signals for events from the PHY associated with
|
|
|
|
* 'intf' to the task identified by 'pid'
|
|
|
|
*
|
|
|
|
* NOTE: This function is intended to be called only from an Ethernet
|
|
|
|
* driver in support of the SIOCMIISIG ioctl command. It should never
|
|
|
|
* by called directly by application logic.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-08-16 20:56:02 +02:00
|
|
|
* intf - Provides the name of the network interface, for example, "eth0".
|
|
|
|
* pid - Identifies the task that was receiving notifications.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int phy_notify_unsubscribe(FAR const char *intf, pid_t pid)
|
|
|
|
{
|
|
|
|
FAR struct phy_notify_s *client;
|
2020-03-31 16:05:48 +02:00
|
|
|
int ret;
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2018-03-09 14:11:07 +01:00
|
|
|
phyinfo("%s: PID=%d\n", intf, pid);
|
2014-08-17 22:07:53 +02:00
|
|
|
|
2014-08-16 20:56:02 +02:00
|
|
|
/* Find the client entry for this interface */
|
|
|
|
|
|
|
|
client = phy_find_assigned(intf, pid);
|
2020-03-31 16:05:48 +02:00
|
|
|
if (client == NULL)
|
2014-08-16 20:56:02 +02:00
|
|
|
{
|
2018-03-09 14:11:07 +01:00
|
|
|
phyerr("ERROR: No such client\n");
|
2014-08-16 20:56:02 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detach and disable the PHY interrupt */
|
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
ret = phy_semtake();
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
arch_phy_irq(intf, NULL, NULL, NULL);
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Cancel any pending notification */
|
2019-01-27 16:28:59 +01:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
nxsig_cancel_notification(&client->work);
|
2019-01-27 16:28:59 +01:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
/* Un-initialize the client entry */
|
2014-08-16 20:56:02 +02:00
|
|
|
|
2020-03-31 16:05:48 +02:00
|
|
|
client->assigned = false;
|
|
|
|
client->intf[0] = '\0';
|
|
|
|
client->pid = -1;
|
|
|
|
|
|
|
|
phy_semgive();
|
|
|
|
}
|
2014-08-16 20:56:02 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_ARCH_PHY_INTERRUPT */
|