* drivers/net/skeleton.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <arpa/inet.h>
#include <nuttx/arch.h>
#include <nuttx/spinlock.h>
#include <nuttx/wdog.h>
#include <nuttx/wqueue.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netdev.h>
#ifdef CONFIG_NET_PKT
# include <nuttx/net/pkt.h>
#endif
#ifdef CONFIG_NET_SKELETON
* Pre-processor Definitions
****************************************************************************/
#if !defined(CONFIG_SCHED_WORKQUEUE)
# error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE)
#else
* will be the same as HPWORK.
*
* NOTE: However, the network should NEVER run on the high priority work
* queue! That queue is intended only to service short back end interrupt
* processing that never suspends. Suspending the high priority work queue
* may bring the system to its knees!
*/
#define ETHWORK LPWORK
* physical interfaces that will be supported.
*/
#ifndef CONFIG_NET_SKELETON_NINTERFACES
# define CONFIG_NET_SKELETON_NINTERFACES 1
#endif
#define SKELETON_TXTIMEOUT (60*CLK_TCK)
#define PKTBUF_SIZE (MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE)
#define BUF ((FAR struct eth_hdr_s *)priv->sk_dev.d_buf)
* Private Types
****************************************************************************/
* interface
*/
struct skel_driver_s
{
bool sk_bifup;
struct wdog_s sk_txtimeout;
struct work_s sk_irqwork;
struct work_s sk_pollwork;
spinlock_t sk_lock;
struct net_driver_s sk_dev;
};
* Private Data
****************************************************************************/
* instance of the device could be supported. In order to support multiple
* devices instances, this data would have to be allocated dynamically.
*/
* be multiple packet buffers in a more complex, pipelined design. Many
* contemporary Ethernet interfaces, for example, use multiple, linked DMA
* descriptors in rings to implement such a pipeline. This example assumes
* much simpler hardware that simply handles one packet at a time.
*
* NOTE that if CONFIG_NET_SKELETON_NINTERFACES were greater than 1,
* you would need a minimum on one packet buffer per instance.
* Much better to be allocated dynamically in cases where more than
* one are needed.
*/
static uint16_t
g_pktbuf[CONFIG_NET_SKELETON_NINTERFACES][(PKTBUF_SIZE + 1) / 2];
static struct skel_driver_s g_skel[CONFIG_NET_SKELETON_NINTERFACES];
* Private Function Prototypes
****************************************************************************/
static int skel_transmit(FAR struct skel_driver_s *priv);
static int skel_txpoll(FAR struct net_driver_s *dev);
static void skel_reply(struct skel_driver_s *priv);
static void skel_receive(FAR struct skel_driver_s *priv);
static void skel_txdone(FAR struct skel_driver_s *priv);
static void skel_interrupt_work(FAR void *arg);
static int skel_interrupt(int irq, FAR void *context, FAR void *arg);
static void skel_txtimeout_work(FAR void *arg);
static void skel_txtimeout_expiry(wdparm_t arg);
static int skel_ifup(FAR struct net_driver_s *dev);
static int skel_ifdown(FAR struct net_driver_s *dev);
static void skel_txavail_work(FAR void *arg);
static int skel_txavail(FAR struct net_driver_s *dev);
#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6)
static int skel_addmac(FAR struct net_driver_s *dev,
FAR const uint8_t *mac);
#ifdef CONFIG_NET_MCASTGROUP
static int skel_rmmac(FAR struct net_driver_s *dev,
FAR const uint8_t *mac);
#endif
#endif
#ifdef CONFIG_NETDEV_IOCTL
static int skel_ioctl(FAR struct net_driver_s *dev, int cmd,
unsigned long arg);
#endif
* Private Functions
****************************************************************************/
* Name: skel_transmit
*
* Description:
* Start hardware transmission. Called either from the txdone interrupt
* handling or from watchdog based polling.
*
* Input Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* OK on success; a negated errno on failure
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static int skel_transmit(FAR struct skel_driver_s *priv)
{
* here, then we are committed to sending a packet; Higher level logic
* must have assured that there is no transmission in progress.
*/
NETDEV_TXPACKETS(priv->sk_dev);
wd_start(&priv->sk_txtimeout, SKELETON_TXTIMEOUT,
skel_txtimeout_expiry, (wdparm_t)priv);
return OK;
}
* Name: skel_txpoll
*
* Description:
* The transmitter is available, check if the network has any outgoing
* packets ready to send. This is a callback from devif_poll().
* devif_poll() may be called:
*
* 1. When the preceding TX packet send is complete,
* 2. When the preceding TX packet send timesout and the interface is reset
* 3. During normal TX polling
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* OK on success; a negated errno on failure
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static int skel_txpoll(FAR struct net_driver_s *dev)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
skel_transmit(priv);
* have been examined.
*/
return 0;
}
* Name: skel_reply
*
* Description:
* After a packet has been received and dispatched to the network, it
* may return return with an outgoing packet. This function checks for
* that case and performs the transmission if necessary.
*
* Input Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static void skel_reply(struct skel_driver_s *priv)
{
* network, the field d_len will set to a value > 0.
*/
if (priv->sk_dev.d_len > 0)
{
skel_transmit(priv);
}
}
* Name: skel_receive
*
* Description:
* An interrupt was received indicating the availability of a new RX packet
*
* Input Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static void skel_receive(FAR struct skel_driver_s *priv)
{
do
{
* configuration.
*/
* amount of data in priv->sk_dev.d_len
*/
#ifdef CONFIG_NET_PKT
pkt_input(&priv->sk_dev);
#endif
#ifdef CONFIG_NET_IPv4
if (BUF->type == HTONS(ETHTYPE_IP))
{
ninfo("IPv4 frame\n");
NETDEV_RXIPV4(&priv->sk_dev);
ipv4_input(&priv->sk_dev);
skel_reply(priv);
}
else
#endif
#ifdef CONFIG_NET_IPv6
if (BUF->type == HTONS(ETHTYPE_IP6))
{
ninfo("IPv6 frame\n");
NETDEV_RXIPV6(&priv->sk_dev);
ipv6_input(&priv->sk_dev);
skel_reply(priv);
}
else
#endif
#ifdef CONFIG_NET_ARP
if (BUF->type == HTONS(ETHTYPE_ARP))
{
arp_input(&priv->sk_dev);
NETDEV_RXARP(&priv->sk_dev);
* sent out on the network, the field d_len will set to a value
* > 0.
*/
if (priv->sk_dev.d_len > 0)
{
skel_transmit(priv);
}
}
else
#endif
{
NETDEV_RXDROPPED(&priv->sk_dev);
}
}
while (true);
}
* Name: skel_txdone
*
* Description:
* An interrupt was received indicating that the last TX packet(s) is done
*
* Input Parameters:
* priv - Reference to the driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static void skel_txdone(FAR struct skel_driver_s *priv)
{
NETDEV_TXDONE(priv->sk_dev);
* disable further Tx interrupts.
*/
wd_cancel(&priv->sk_txtimeout);
devif_poll(&priv->sk_dev, skel_txpoll);
}
* Name: skel_interrupt_work
*
* Description:
* Perform interrupt related work from the worker thread
*
* Input Parameters:
* arg - The argument passed when work_queue() was called.
*
* Returned Value:
* OK on success
*
* Assumptions:
* Runs on a worker thread.
*
****************************************************************************/
static void skel_interrupt_work(FAR void *arg)
{
FAR struct skel_driver_s *priv = (FAR struct skel_driver_s *)arg;
* NOTE: Serialization is only required in the case where the driver work
* is performed on an LP worker thread and where more than one LP worker
* thread has been configured.
*/
net_lock();
skel_receive(priv);
* This may disable further Tx interrupts if there are no pending
* transmissions.
*/
skel_txdone(priv);
net_unlock();
up_enable_irq(CONFIG_NET_SKELETON_IRQ);
}
* Name: skel_interrupt
*
* Description:
* Hardware interrupt handler
*
* Input Parameters:
* irq - Number of the IRQ that generated the interrupt
* context - Interrupt register state save info (architecture-specific)
*
* Returned Value:
* OK on success
*
* Assumptions:
* Runs in the context of a the Ethernet interrupt handler. Local
* interrupts are disabled by the interrupt logic.
*
****************************************************************************/
static int skel_interrupt(int irq, FAR void *context, FAR void *arg)
{
FAR struct skel_driver_s *priv = (FAR struct skel_driver_s *)arg;
DEBUGASSERT(priv != NULL);
* also disabled if the TX timeout event occurs, there can be no race
* condition here.
*/
up_disable_irq(CONFIG_NET_SKELETON_IRQ);
{
* there will be no race condition between any subsequent timeout
* expiration and the deferred interrupt processing.
*/
wd_cancel(&priv->sk_txtimeout);
}
work_queue(ETHWORK, &priv->sk_irqwork, skel_interrupt_work, priv, 0);
return OK;
}
* Name: skel_txtimeout_work
*
* Description:
* Perform TX timeout related work from the worker thread
*
* Input Parameters:
* arg - The argument passed when work_queue() as called.
*
* Returned Value:
* OK on success
*
****************************************************************************/
static void skel_txtimeout_work(FAR void *arg)
{
FAR struct skel_driver_s *priv = (FAR struct skel_driver_s *)arg;
* NOTE: Serialization is only required in the case where the driver work
* is performed on an LP worker thread and where more than one LP worker
* thread has been configured.
*/
net_lock();
NETDEV_TXTIMEOUTS(priv->sk_dev);
devif_poll(&priv->sk_dev, skel_txpoll);
net_unlock();
}
* Name: skel_txtimeout_expiry
*
* Description:
* Our TX watchdog timed out. Called from the timer interrupt handler.
* The last TX never completed. Reset the hardware and start again.
*
* Input Parameters:
* arg - The argument
*
* Returned Value:
* None
*
* Assumptions:
* Runs in the context of a the timer interrupt handler. Local
* interrupts are disabled by the interrupt logic.
*
****************************************************************************/
static void skel_txtimeout_expiry(wdparm_t arg)
{
FAR struct skel_driver_s *priv = (FAR struct skel_driver_s *)arg;
* conditions with interrupt work. There is still a potential race
* condition with interrupt work that is already queued and in progress.
*/
up_disable_irq(CONFIG_NET_SKELETON_IRQ);
work_queue(ETHWORK, &priv->sk_irqwork, skel_txtimeout_work, priv, 0);
}
* Name: skel_ifup
*
* Description:
* NuttX Callback: Bring up the Ethernet interface when an IP address is
* provided
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static int skel_ifup(FAR struct net_driver_s *dev)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
irqstate_t flags;
#ifdef CONFIG_NET_IPv4
ninfo("Bringing up: %u.%u.%u.%u\n",
ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr),
ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr));
#endif
#ifdef CONFIG_NET_IPv6
ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2],
dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5],
dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
#endif
flags = spin_lock_irqsave(&priv->sk_lock);
priv->sk_bifup = true;
up_enable_irq(CONFIG_NET_SKELETON_IRQ);
spin_unlock_irqrestore(&priv->sk_lock, flags);
return OK;
}
* Name: skel_ifdown
*
* Description:
* NuttX Callback: Stop the interface.
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static int skel_ifdown(FAR struct net_driver_s *dev)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
irqstate_t flags;
flags = spin_lock_irqsave(&priv->sk_lock);
up_disable_irq(CONFIG_NET_SKELETON_IRQ);
wd_cancel(&priv->sk_txtimeout);
* a known configuration that will guarantee the skel_ifup() always
* successfully brings the interface back up.
*/
priv->sk_bifup = false;
spin_unlock_irqrestore(&priv->sk_lock, flags);
return OK;
}
* Name: skel_txavail_work
*
* Description:
* Perform an out-of-cycle poll on the worker thread.
*
* Input Parameters:
* arg - Reference to the NuttX driver state structure (cast to void*)
*
* Returned Value:
* None
*
* Assumptions:
* Runs on a work queue thread.
*
****************************************************************************/
static void skel_txavail_work(FAR void *arg)
{
FAR struct skel_driver_s *priv = (FAR struct skel_driver_s *)arg;
* NOTE: Serialization is only required in the case where the driver work
* is performed on an LP worker thread and where more than one LP worker
* thread has been configured.
*/
net_lock();
if (priv->sk_bifup)
{
devif_poll(&priv->sk_dev, skel_txpoll);
}
net_unlock();
}
* Name: skel_txavail
*
* Description:
* Driver callback invoked when new TX data is available. This is a
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
* latency.
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
*
* Returned Value:
* None
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static int skel_txavail(FAR struct net_driver_s *dev)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
* pending interrupt actions and we will have to ignore the Tx
* availability action.
*/
if (work_available(&priv->sk_pollwork))
{
work_queue(ETHWORK, &priv->sk_pollwork, skel_txavail_work, priv, 0);
}
return OK;
}
* Name: skel_addmac
*
* Description:
* NuttX Callback: Add the specified MAC address to the hardware multicast
* address filtering
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
* mac - The MAC address to be added
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#if defined(CONFIG_NET_MCASTGROUP) || defined(CONFIG_NET_ICMPv6)
static int skel_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
UNUSED(priv);
return OK;
}
#endif
* Name: skel_rmmac
*
* Description:
* NuttX Callback: Remove the specified MAC address from the hardware
* multicast address filtering
*
* Input Parameters:
* dev - Reference to the NuttX driver state structure
* mac - The MAC address to be removed
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_NET_MCASTGROUP
static int skel_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
UNUSED(priv);
return OK;
}
#endif
* Name: skel_ioctl
*
* Description:
* Handle network IOCTL commands directed to this device.
*
* Input 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 skel_ioctl(FAR struct net_driver_s *dev, int cmd,
unsigned long arg)
{
FAR struct skel_driver_s *priv =
(FAR struct skel_driver_s *)dev->d_private;
int ret;
switch (cmd)
{
default:
nerr("ERROR: Unrecognized IOCTL command: %d\n", cmd);
return -ENOTTY;
}
return OK;
}
#endif
* Public Functions
****************************************************************************/
* Name: skel_initialize
*
* Description:
* Initialize the Ethernet controller and driver
*
* Input Parameters:
* intf - In the case where there are multiple EMACs, this value
* identifies which EMAC is to be initialized.
*
* Returned Value:
* OK on success; Negated errno on failure.
*
* Assumptions:
* Called early in initialization before multi-tasking is initiated.
*
****************************************************************************/
int skel_initialize(int intf)
{
FAR struct skel_driver_s *priv;
DEBUGASSERT(intf < CONFIG_NET_SKELETON_NINTERFACES);
priv = &g_skel[intf];
if (irq_attach(CONFIG_NET_SKELETON_IRQ, skel_interrupt, priv))
{
return -EAGAIN;
}
memset(priv, 0, sizeof(struct skel_driver_s));
priv->sk_dev.d_buf = (FAR uint8_t *)g_pktbuf[intf];
priv->sk_dev.d_ifup = skel_ifup;
priv->sk_dev.d_ifdown = skel_ifdown;
priv->sk_dev.d_txavail = skel_txavail;
#ifdef CONFIG_NET_MCASTGROUP
priv->sk_dev.d_addmac = skel_addmac;
priv->sk_dev.d_rmmac = skel_rmmac;
#endif
#ifdef CONFIG_NETDEV_IOCTL
priv->sk_dev.d_ioctl = skel_ioctl;
#endif
priv->sk_dev.d_private = g_skel;
spin_lock_init(&priv->sk_lock);
* the device and/or calling skel_ifdown().
*/
* priv->sk_dev.d_mac.ether.ether_addr_octet
* Applies only if the Ethernet MAC has its own internal address.
*/
netdev_register(&priv->sk_dev, NET_LL_ETHERNET);
return OK;
}
#endif
#endif