Refactor to use new protocol module and sipo.
[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)
252                 return;
253         /* points to packet payload, which starts with an Ethernet header */
254         ethhdr = p->payload;
255
256         switch (htons(ethhdr->type))
257         {
258         /* IP or ARP packet? */
259         case ETHTYPE_IP:
260         case ETHTYPE_ARP:
261 #if PPPOE_SUPPORT
262         /* PPPoE packet? */
263         case ETHTYPE_PPPOEDISC:
264         case ETHTYPE_PPPOE:
265 #endif /* PPPOE_SUPPORT */
266                 /* full packet send to tcpip_thread to process */
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
275         default:
276                 pbuf_free(p);
277                 p = NULL;
278                 break;
279         }
280 }
281
282
283 static NORETURN void ethernetif_loop(void *arg)
284 {
285         struct netif *netif = (struct netif *)arg;
286         while (1)
287                 ethernetif_input(netif);
288 }
289
290 /**
291  * Should be called at the beginning of the program to set up the
292  * network interface. It calls the function low_level_init() to do the
293  * actual setup of the hardware.
294  *
295  * This function should be passed as a parameter to netif_add().
296  *
297  * @param netif the lwip network interface structure for this ethernetif
298  * @return ERR_OK if the loopif is initialized
299  *         ERR_MEM if private data couldn't be allocated
300  *         any other err_t on error
301  */
302 err_t ethernetif_init(struct netif *netif)
303 {
304         struct ethernetif *ethernetif;
305
306         LWIP_ASSERT("netif != NULL", (netif != NULL));
307
308         ethernetif = mem_malloc(sizeof(struct ethernetif));
309         if (ethernetif == NULL)
310         {
311                 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
312                 return ERR_MEM;
313         }
314
315         /*
316          * Initialize the snmp variables and counters inside the struct netif.
317          * The last argument should be replaced with your link speed, in units
318          * of bits per second.
319          */
320         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
321
322         netif->state = ethernetif;
323
324         netif->hwaddr_len = 6;
325         netif->name[0] = IFNAME0;
326         netif->name[1] = IFNAME1;
327         netif->mtu = 1500;
328
329
330         /* We directly use etharp_output() here to save a function call.
331          * You can instead declare your own function an call etharp_output()
332          * from it if you have to do some checks before sending (e.g. if link
333          * is available...)
334          */
335         netif->output = etharp_output;
336         netif->linkoutput = low_level_output;
337
338         ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
339
340         /* initialize the hardware */
341         low_level_init(netif);
342         etharp_init();
343
344         if (!sys_thread_new((char *)"eth_thread", ethernetif_loop, netif,
345                         DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO))
346         {
347                 LWIP_DEBUGF(NETIF_DEBUG,
348                         ("ethernetif_init: max number of threads exceeded\n"));
349                 mem_free(ethernetif);
350                 return ERR_MEM;
351         }
352         return ERR_OK;
353 }