9618061c3fcc5ceda903a49808ce2406a583a96b
[bertos.git] / bertos / net / lwip / src / netif / ethernetif.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Ethernet driver glue for lwIP
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 /*
39  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without modification,
43  * are permitted provided that the following conditions are met:
44  *
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.
52  *
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
62  * OF SUCH DAMAGE.
63  *
64  * This file is part of the lwIP TCP/IP stack.
65  *
66  * Author: Adam Dunkels <adam@sics.se>
67  *
68  */
69
70 /*
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.
75  */
76
77 #include "cfg/cfg_lwip.h"
78
79 #include <drv/eth.h>
80
81 #include <cpu/irq.h>
82
83 #include <kern/proc.h>
84
85 #include <lwip/def.h>
86 #include <lwip/mem.h>
87 #include <lwip/pbuf.h>
88 #include <lwip/sys.h>
89 #include <lwip/stats.h>
90 #include <lwip/snmp.h>
91
92 #include <netif/etharp.h>
93 #include <netif/ppp_oe.h>
94
95 #include <netif/ethernetif.h>
96
97 /* Define those to better describe your network interface. */
98 #define IFNAME0 'e'
99 #define IFNAME1 '0'
100
101 /**
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...
106  */
107 struct ethernetif
108 {
109         struct eth_addr *ethaddr;
110         /* Add whatever per-interface state that is needed here. */
111 };
112
113 /**
114  * In this function, the hardware should be initialized.
115  * Called from ethernetif_init().
116  *
117  * @param netif the already initialized lwip network interface structure
118  *        for this ethernetif
119  */
120 static void low_level_init(struct netif *netif)
121 {
122         /* set MAC hardware address length */
123         netif->hwaddr_len = ETHARP_HWADDR_LEN;
124
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];
132
133         /* maximum transfer unit */
134         netif->mtu = 1500;
135
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;
139
140         eth_init();
141 }
142
143 /**
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
146  * might be chained.
147  *
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
152  *
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).
157  */
158
159 static err_t low_level_output(UNUSED_ARG(struct netif *, netif), struct pbuf *p)
160 {
161         struct pbuf *q;
162
163         #if ETH_PAD_SIZE
164                 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
165         #endif
166
167         proc_forbid();
168         for (q = p; q != NULL; q = q->next)
169                 eth_putFrame(q->payload, q->len);
170         eth_sendFrame();
171
172         #if ETH_PAD_SIZE
173                 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
174         #endif
175
176         LINK_STATS_INC(link.xmit);
177         proc_permit();
178
179         return ERR_OK;
180 }
181
182 /**
183  * Should allocate a pbuf and transfer the bytes of the incoming
184  * packet from the interface into the pbuf.
185  *
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
189  */
190 static struct pbuf *low_level_input(UNUSED_ARG(struct netif *, netif))
191 {
192         struct pbuf *p, *q;
193         size_t len;
194
195         len = eth_getFrameLen();
196         if (UNLIKELY(len <= 0))
197                 return NULL;
198
199         #if ETH_PAD_SIZE
200                 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
201         #endif
202
203         proc_forbid();
204         /* We allocate a pbuf chain of pbufs from the pool. */
205         p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
206         if (p != NULL)
207         {
208                 #if ETH_PAD_SIZE
209                         pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
210                 #endif
211
212                 for (q = p; q != NULL; q = q->next)
213                         eth_getFrame(q->payload, q->len);
214
215                 #if ETH_PAD_SIZE
216                         pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
217                 #endif
218
219                 LINK_STATS_INC(link.recv);
220         }
221         else
222         {
223                 LINK_STATS_INC(link.memerr);
224                 LINK_STATS_INC(link.drop);
225         }
226         proc_permit();
227
228         return p;
229 }
230
231 /**
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.
237  *
238  * @param netif the lwip network interface structure for this ethernetif
239  */
240 static void ethernetif_input(struct netif *netif)
241 {
242         struct ethernetif *ethernetif;
243         struct eth_hdr *ethhdr;
244         struct pbuf *p;
245
246         ethernetif = netif->state;
247
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;
252
253         ethhdr = p->payload;
254
255         switch (htons(ethhdr->type))
256         {
257         case ETHTYPE_ARP:
258                 etharp_arp_input(netif, ethernetif->ethaddr, p);
259                 break;
260
261         case ETHTYPE_IP:
262 #if DHCP_DOES_ARP_CHECK
263                 etharp_ip_input(netif, p);
264 #endif
265                 pbuf_header(p, (int16_t) - sizeof(struct eth_hdr));
266
267                 if (netif->input(p, netif) != ERR_OK)
268                 {
269                         LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
270                         pbuf_free(p);
271                         p = NULL;
272                 }
273                 break;
274         default:
275                 pbuf_free(p);
276                 p = NULL;
277                 break;
278         }
279 }
280
281 static NORETURN void ethernetif_loop(void *arg)
282 {
283         struct netif *netif = (struct netif *)arg;
284         while (1)
285                 ethernetif_input(netif);
286 }
287
288 /**
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.
292  *
293  * This function should be passed as a parameter to netif_add().
294  *
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
299  */
300 err_t ethernetif_init(struct netif *netif)
301 {
302         struct ethernetif *ethernetif;
303
304         LWIP_ASSERT("netif != NULL", (netif != NULL));
305
306         ethernetif = mem_malloc(sizeof(struct ethernetif));
307         if (ethernetif == NULL)
308         {
309                 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
310                 return ERR_MEM;
311         }
312
313         /*
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.
317          */
318         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
319
320         netif->state = ethernetif;
321
322         netif->hwaddr_len = 6;
323         netif->name[0] = IFNAME0;
324         netif->name[1] = IFNAME1;
325         netif->mtu = 1500;
326
327
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
331          * is available...)
332          */
333         netif->output = etharp_output;
334         netif->linkoutput = low_level_output;
335
336         ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
337
338         /* initialize the hardware */
339         low_level_init(netif);
340         etharp_init();
341
342         if (!sys_thread_new((char *)"eth_thread", ethernetif_loop, netif,
343                         DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO))
344         {
345                 LWIP_DEBUGF(NETIF_DEBUG,
346                         ("ethernetif_init: max number of threads exceeded\n"));
347                 mem_free(ethernetif);
348                 return ERR_MEM;
349         }
350         return ERR_OK;
351 }