4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Ethernet driver glue for lwIP
35 * \author Andrea Righi <arighi@develer.com>
39 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without modification,
43 * are permitted provided that the following conditions are met:
45 * 1. Redistributions of source code must retain the above copyright notice,
46 * this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright notice,
48 * this list of conditions and the following disclaimer in the documentation
49 * and/or other materials provided with the distribution.
50 * 3. The name of the author may not be used to endorse or promote products
51 * derived from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
56 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
57 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
58 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
61 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
64 * This file is part of the lwIP TCP/IP stack.
66 * Author: Adam Dunkels <adam@sics.se>
71 * This file is a skeleton for developing Ethernet network interface
72 * drivers for lwIP. Add code to the low_level functions and do a
73 * search-and-replace for the word "ethernetif" to replace it with
74 * something that better describes your network interface.
77 #include "cfg/cfg_lwip.h"
83 #include <kern/proc.h>
87 #include <lwip/pbuf.h>
89 #include <lwip/stats.h>
90 #include <lwip/snmp.h>
92 #include <netif/etharp.h>
93 #include <netif/ppp_oe.h>
95 #include <netif/ethernetif.h>
97 /* Define those to better describe your network interface. */
102 * Helper struct to hold private data used to operate your ethernet interface.
103 * Keeping the ethernet address of the MAC in this struct is not necessary
104 * as it is already kept in the struct netif.
105 * But this is only an example, anyway...
109 struct eth_addr *ethaddr;
110 /* Add whatever per-interface state that is needed here. */
114 * In this function, the hardware should be initialized.
115 * Called from ethernetif_init().
117 * @param netif the already initialized lwip network interface structure
118 * for this ethernetif
120 static void low_level_init(struct netif *netif)
122 /* set MAC hardware address length */
123 netif->hwaddr_len = ETHARP_HWADDR_LEN;
125 /* set MAC hardware address */
126 netif->hwaddr[0] = mac_addr[0];
127 netif->hwaddr[1] = mac_addr[1];
128 netif->hwaddr[2] = mac_addr[2];
129 netif->hwaddr[3] = mac_addr[3];
130 netif->hwaddr[4] = mac_addr[4];
131 netif->hwaddr[5] = mac_addr[5];
133 /* maximum transfer unit */
136 /* device capabilities */
137 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
138 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
144 * This function should do the actual transmission of the packet. The packet is
145 * contained in the pbuf that is passed to the function. This pbuf
148 * @param netif the lwip network interface structure for this ethernetif
149 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
150 * @return ERR_OK if the packet could be sent
151 * an err_t value if the packet couldn't be sent
153 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
154 * strange results. You might consider waiting for space in the DMA queue
155 * to become availale since the stack doesn't retry to send a packet
156 * dropped because of memory failure (except for the TCP timers).
159 static err_t low_level_output(UNUSED_ARG(struct netif *, netif), struct pbuf *p)
164 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
168 for (q = p; q != NULL; q = q->next)
169 eth_putFrame(q->payload, q->len);
173 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
176 LINK_STATS_INC(link.xmit);
183 * Should allocate a pbuf and transfer the bytes of the incoming
184 * packet from the interface into the pbuf.
186 * @param netif the lwip network interface structure for this ethernetif
187 * @return a pbuf filled with the received packet (including MAC header)
188 * NULL on memory error
190 static struct pbuf *low_level_input(UNUSED_ARG(struct netif *, netif))
195 len = eth_getFrameLen();
196 if (UNLIKELY(len <= 0))
200 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
204 /* We allocate a pbuf chain of pbufs from the pool. */
205 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
209 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
212 for (q = p; q != NULL; q = q->next)
213 eth_getFrame(q->payload, q->len);
216 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
219 LINK_STATS_INC(link.recv);
223 LINK_STATS_INC(link.memerr);
224 LINK_STATS_INC(link.drop);
232 * This function should be called when a packet is ready to be read
233 * from the interface. It uses the function low_level_input() that
234 * should handle the actual reception of bytes from the network
235 * interface. Then the type of the received packet is determined and
236 * the appropriate input function is called.
238 * @param netif the lwip network interface structure for this ethernetif
240 static void ethernetif_input(struct netif *netif)
242 struct ethernetif *ethernetif;
243 struct eth_hdr *ethhdr;
246 ethernetif = netif->state;
248 /* move received packet into a new pbuf */
249 p = low_level_input(netif);
250 /* no packet could be read, silently ignore this */
251 if (p == NULL) return;
255 switch (htons(ethhdr->type))
258 etharp_arp_input(netif, ethernetif->ethaddr, p);
262 #if DHCP_DOES_ARP_CHECK
263 etharp_ip_input(netif, p);
265 pbuf_header(p, (int16_t) - sizeof(struct eth_hdr));
267 if (netif->input(p, netif) != ERR_OK)
269 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
281 static NORETURN void ethernetif_loop(void *arg)
283 struct netif *netif = (struct netif *)arg;
285 ethernetif_input(netif);
289 * Should be called at the beginning of the program to set up the
290 * network interface. It calls the function low_level_init() to do the
291 * actual setup of the hardware.
293 * This function should be passed as a parameter to netif_add().
295 * @param netif the lwip network interface structure for this ethernetif
296 * @return ERR_OK if the loopif is initialized
297 * ERR_MEM if private data couldn't be allocated
298 * any other err_t on error
300 err_t ethernetif_init(struct netif *netif)
302 struct ethernetif *ethernetif;
304 LWIP_ASSERT("netif != NULL", (netif != NULL));
306 ethernetif = mem_malloc(sizeof(struct ethernetif));
307 if (ethernetif == NULL)
309 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
314 * Initialize the snmp variables and counters inside the struct netif.
315 * The last argument should be replaced with your link speed, in units
316 * of bits per second.
318 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
320 netif->state = ethernetif;
322 netif->hwaddr_len = 6;
323 netif->name[0] = IFNAME0;
324 netif->name[1] = IFNAME1;
328 /* We directly use etharp_output() here to save a function call.
329 * You can instead declare your own function an call etharp_output()
330 * from it if you have to do some checks before sending (e.g. if link
333 netif->output = etharp_output;
334 netif->linkoutput = low_level_output;
336 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
338 /* initialize the hardware */
339 low_level_init(netif);
342 if (!sys_thread_new((char *)"eth_thread", ethernetif_loop, netif,
343 DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO))
345 LWIP_DEBUGF(NETIF_DEBUG,
346 ("ethernetif_init: max number of threads exceeded\n"));
347 mem_free(ethernetif);