First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darran 0:55a05330f8cc 1 /**
darran 0:55a05330f8cc 2 * @file
darran 0:55a05330f8cc 3 * Ethernet Interface Skeleton
darran 0:55a05330f8cc 4 *
darran 0:55a05330f8cc 5 */
darran 0:55a05330f8cc 6
darran 0:55a05330f8cc 7 /*
darran 0:55a05330f8cc 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
darran 0:55a05330f8cc 9 * All rights reserved.
darran 0:55a05330f8cc 10 *
darran 0:55a05330f8cc 11 * Redistribution and use in source and binary forms, with or without modification,
darran 0:55a05330f8cc 12 * are permitted provided that the following conditions are met:
darran 0:55a05330f8cc 13 *
darran 0:55a05330f8cc 14 * 1. Redistributions of source code must retain the above copyright notice,
darran 0:55a05330f8cc 15 * this list of conditions and the following disclaimer.
darran 0:55a05330f8cc 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
darran 0:55a05330f8cc 17 * this list of conditions and the following disclaimer in the documentation
darran 0:55a05330f8cc 18 * and/or other materials provided with the distribution.
darran 0:55a05330f8cc 19 * 3. The name of the author may not be used to endorse or promote products
darran 0:55a05330f8cc 20 * derived from this software without specific prior written permission.
darran 0:55a05330f8cc 21 *
darran 0:55a05330f8cc 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
darran 0:55a05330f8cc 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
darran 0:55a05330f8cc 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
darran 0:55a05330f8cc 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
darran 0:55a05330f8cc 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
darran 0:55a05330f8cc 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
darran 0:55a05330f8cc 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
darran 0:55a05330f8cc 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
darran 0:55a05330f8cc 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
darran 0:55a05330f8cc 31 * OF SUCH DAMAGE.
darran 0:55a05330f8cc 32 *
darran 0:55a05330f8cc 33 * This file is part of the lwIP TCP/IP stack.
darran 0:55a05330f8cc 34 *
darran 0:55a05330f8cc 35 * Author: Adam Dunkels <adam@sics.se>
darran 0:55a05330f8cc 36 *
darran 0:55a05330f8cc 37 */
darran 0:55a05330f8cc 38
darran 0:55a05330f8cc 39 /*
darran 0:55a05330f8cc 40 * This file is a skeleton for developing Ethernet network interface
darran 0:55a05330f8cc 41 * drivers for lwIP. Add code to the low_level functions and do a
darran 0:55a05330f8cc 42 * search-and-replace for the word "ethernetif" to replace it with
darran 0:55a05330f8cc 43 * something that better describes your network interface.
darran 0:55a05330f8cc 44 */
darran 0:55a05330f8cc 45
darran 0:55a05330f8cc 46 #include "lwip/opt.h"
darran 0:55a05330f8cc 47
darran 0:55a05330f8cc 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
darran 0:55a05330f8cc 49
darran 0:55a05330f8cc 50 #include "lwip/def.h"
darran 0:55a05330f8cc 51 #include "lwip/mem.h"
darran 0:55a05330f8cc 52 #include "lwip/pbuf.h"
darran 0:55a05330f8cc 53 #include "lwip/sys.h"
darran 0:55a05330f8cc 54 #include <lwip/stats.h>
darran 0:55a05330f8cc 55 #include <lwip/snmp.h>
darran 0:55a05330f8cc 56 #include "netif/etharp.h"
darran 0:55a05330f8cc 57 #include "netif/ppp_oe.h"
darran 0:55a05330f8cc 58
darran 0:55a05330f8cc 59 /* Define those to better describe your network interface. */
darran 0:55a05330f8cc 60 #define IFNAME0 'e'
darran 0:55a05330f8cc 61 #define IFNAME1 'n'
darran 0:55a05330f8cc 62
darran 0:55a05330f8cc 63 /**
darran 0:55a05330f8cc 64 * Helper struct to hold private data used to operate your ethernet interface.
darran 0:55a05330f8cc 65 * Keeping the ethernet address of the MAC in this struct is not necessary
darran 0:55a05330f8cc 66 * as it is already kept in the struct netif.
darran 0:55a05330f8cc 67 * But this is only an example, anyway...
darran 0:55a05330f8cc 68 */
darran 0:55a05330f8cc 69 struct ethernetif {
darran 0:55a05330f8cc 70 struct eth_addr *ethaddr;
darran 0:55a05330f8cc 71 /* Add whatever per-interface state that is needed here. */
darran 0:55a05330f8cc 72 };
darran 0:55a05330f8cc 73
darran 0:55a05330f8cc 74 /* Forward declarations. */
darran 0:55a05330f8cc 75 static void ethernetif_input(struct netif *netif);
darran 0:55a05330f8cc 76
darran 0:55a05330f8cc 77 /**
darran 0:55a05330f8cc 78 * In this function, the hardware should be initialized.
darran 0:55a05330f8cc 79 * Called from ethernetif_init().
darran 0:55a05330f8cc 80 *
darran 0:55a05330f8cc 81 * @param netif the already initialized lwip network interface structure
darran 0:55a05330f8cc 82 * for this ethernetif
darran 0:55a05330f8cc 83 */
darran 0:55a05330f8cc 84 static void
darran 0:55a05330f8cc 85 low_level_init(struct netif *netif)
darran 0:55a05330f8cc 86 {
darran 0:55a05330f8cc 87 struct ethernetif *ethernetif = netif->state;
darran 0:55a05330f8cc 88
darran 0:55a05330f8cc 89 /* set MAC hardware address length */
darran 0:55a05330f8cc 90 netif->hwaddr_len = ETHARP_HWADDR_LEN;
darran 0:55a05330f8cc 91
darran 0:55a05330f8cc 92 /* set MAC hardware address */
darran 0:55a05330f8cc 93 netif->hwaddr[0] = ;
darran 0:55a05330f8cc 94 ...
darran 0:55a05330f8cc 95 netif->hwaddr[5] = ;
darran 0:55a05330f8cc 96
darran 0:55a05330f8cc 97 /* maximum transfer unit */
darran 0:55a05330f8cc 98 netif->mtu = 1500;
darran 0:55a05330f8cc 99
darran 0:55a05330f8cc 100 /* device capabilities */
darran 0:55a05330f8cc 101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
darran 0:55a05330f8cc 102 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
darran 0:55a05330f8cc 103
darran 0:55a05330f8cc 104 /* Do whatever else is needed to initialize interface. */
darran 0:55a05330f8cc 105 }
darran 0:55a05330f8cc 106
darran 0:55a05330f8cc 107 /**
darran 0:55a05330f8cc 108 * This function should do the actual transmission of the packet. The packet is
darran 0:55a05330f8cc 109 * contained in the pbuf that is passed to the function. This pbuf
darran 0:55a05330f8cc 110 * might be chained.
darran 0:55a05330f8cc 111 *
darran 0:55a05330f8cc 112 * @param netif the lwip network interface structure for this ethernetif
darran 0:55a05330f8cc 113 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
darran 0:55a05330f8cc 114 * @return ERR_OK if the packet could be sent
darran 0:55a05330f8cc 115 * an err_t value if the packet couldn't be sent
darran 0:55a05330f8cc 116 *
darran 0:55a05330f8cc 117 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
darran 0:55a05330f8cc 118 * strange results. You might consider waiting for space in the DMA queue
darran 0:55a05330f8cc 119 * to become availale since the stack doesn't retry to send a packet
darran 0:55a05330f8cc 120 * dropped because of memory failure (except for the TCP timers).
darran 0:55a05330f8cc 121 */
darran 0:55a05330f8cc 122
darran 0:55a05330f8cc 123 static err_t
darran 0:55a05330f8cc 124 low_level_output(struct netif *netif, struct pbuf *p)
darran 0:55a05330f8cc 125 {
darran 0:55a05330f8cc 126 struct ethernetif *ethernetif = netif->state;
darran 0:55a05330f8cc 127 struct pbuf *q;
darran 0:55a05330f8cc 128
darran 0:55a05330f8cc 129 initiate transfer();
darran 0:55a05330f8cc 130
darran 0:55a05330f8cc 131 #if ETH_PAD_SIZE
darran 0:55a05330f8cc 132 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
darran 0:55a05330f8cc 133 #endif
darran 0:55a05330f8cc 134
darran 0:55a05330f8cc 135 for(q = p; q != NULL; q = q->next) {
darran 0:55a05330f8cc 136 /* Send the data from the pbuf to the interface, one pbuf at a
darran 0:55a05330f8cc 137 time. The size of the data in each pbuf is kept in the ->len
darran 0:55a05330f8cc 138 variable. */
darran 0:55a05330f8cc 139 send data from(q->payload, q->len);
darran 0:55a05330f8cc 140 }
darran 0:55a05330f8cc 141
darran 0:55a05330f8cc 142 signal that packet should be sent();
darran 0:55a05330f8cc 143
darran 0:55a05330f8cc 144 #if ETH_PAD_SIZE
darran 0:55a05330f8cc 145 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
darran 0:55a05330f8cc 146 #endif
darran 0:55a05330f8cc 147
darran 0:55a05330f8cc 148 LINK_STATS_INC(link.xmit);
darran 0:55a05330f8cc 149
darran 0:55a05330f8cc 150 return ERR_OK;
darran 0:55a05330f8cc 151 }
darran 0:55a05330f8cc 152
darran 0:55a05330f8cc 153 /**
darran 0:55a05330f8cc 154 * Should allocate a pbuf and transfer the bytes of the incoming
darran 0:55a05330f8cc 155 * packet from the interface into the pbuf.
darran 0:55a05330f8cc 156 *
darran 0:55a05330f8cc 157 * @param netif the lwip network interface structure for this ethernetif
darran 0:55a05330f8cc 158 * @return a pbuf filled with the received packet (including MAC header)
darran 0:55a05330f8cc 159 * NULL on memory error
darran 0:55a05330f8cc 160 */
darran 0:55a05330f8cc 161 static struct pbuf *
darran 0:55a05330f8cc 162 low_level_input(struct netif *netif)
darran 0:55a05330f8cc 163 {
darran 0:55a05330f8cc 164 struct ethernetif *ethernetif = netif->state;
darran 0:55a05330f8cc 165 struct pbuf *p, *q;
darran 0:55a05330f8cc 166 u16_t len;
darran 0:55a05330f8cc 167
darran 0:55a05330f8cc 168 /* Obtain the size of the packet and put it into the "len"
darran 0:55a05330f8cc 169 variable. */
darran 0:55a05330f8cc 170 len = ;
darran 0:55a05330f8cc 171
darran 0:55a05330f8cc 172 #if ETH_PAD_SIZE
darran 0:55a05330f8cc 173 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
darran 0:55a05330f8cc 174 #endif
darran 0:55a05330f8cc 175
darran 0:55a05330f8cc 176 /* We allocate a pbuf chain of pbufs from the pool. */
darran 0:55a05330f8cc 177 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
darran 0:55a05330f8cc 178
darran 0:55a05330f8cc 179 if (p != NULL) {
darran 0:55a05330f8cc 180
darran 0:55a05330f8cc 181 #if ETH_PAD_SIZE
darran 0:55a05330f8cc 182 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
darran 0:55a05330f8cc 183 #endif
darran 0:55a05330f8cc 184
darran 0:55a05330f8cc 185 /* We iterate over the pbuf chain until we have read the entire
darran 0:55a05330f8cc 186 * packet into the pbuf. */
darran 0:55a05330f8cc 187 for(q = p; q != NULL; q = q->next) {
darran 0:55a05330f8cc 188 /* Read enough bytes to fill this pbuf in the chain. The
darran 0:55a05330f8cc 189 * available data in the pbuf is given by the q->len
darran 0:55a05330f8cc 190 * variable.
darran 0:55a05330f8cc 191 * This does not necessarily have to be a memcpy, you can also preallocate
darran 0:55a05330f8cc 192 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
darran 0:55a05330f8cc 193 * actually received size. In this case, ensure the tot_len member of the
darran 0:55a05330f8cc 194 * pbuf is the sum of the chained pbuf len members.
darran 0:55a05330f8cc 195 */
darran 0:55a05330f8cc 196 read data into(q->payload, q->len);
darran 0:55a05330f8cc 197 }
darran 0:55a05330f8cc 198 acknowledge that packet has been read();
darran 0:55a05330f8cc 199
darran 0:55a05330f8cc 200 #if ETH_PAD_SIZE
darran 0:55a05330f8cc 201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
darran 0:55a05330f8cc 202 #endif
darran 0:55a05330f8cc 203
darran 0:55a05330f8cc 204 LINK_STATS_INC(link.recv);
darran 0:55a05330f8cc 205 } else {
darran 0:55a05330f8cc 206 drop packet();
darran 0:55a05330f8cc 207 LINK_STATS_INC(link.memerr);
darran 0:55a05330f8cc 208 LINK_STATS_INC(link.drop);
darran 0:55a05330f8cc 209 }
darran 0:55a05330f8cc 210
darran 0:55a05330f8cc 211 return p;
darran 0:55a05330f8cc 212 }
darran 0:55a05330f8cc 213
darran 0:55a05330f8cc 214 /**
darran 0:55a05330f8cc 215 * This function should be called when a packet is ready to be read
darran 0:55a05330f8cc 216 * from the interface. It uses the function low_level_input() that
darran 0:55a05330f8cc 217 * should handle the actual reception of bytes from the network
darran 0:55a05330f8cc 218 * interface. Then the type of the received packet is determined and
darran 0:55a05330f8cc 219 * the appropriate input function is called.
darran 0:55a05330f8cc 220 *
darran 0:55a05330f8cc 221 * @param netif the lwip network interface structure for this ethernetif
darran 0:55a05330f8cc 222 */
darran 0:55a05330f8cc 223 static void
darran 0:55a05330f8cc 224 ethernetif_input(struct netif *netif)
darran 0:55a05330f8cc 225 {
darran 0:55a05330f8cc 226 struct ethernetif *ethernetif;
darran 0:55a05330f8cc 227 struct eth_hdr *ethhdr;
darran 0:55a05330f8cc 228 struct pbuf *p;
darran 0:55a05330f8cc 229
darran 0:55a05330f8cc 230 ethernetif = netif->state;
darran 0:55a05330f8cc 231
darran 0:55a05330f8cc 232 /* move received packet into a new pbuf */
darran 0:55a05330f8cc 233 p = low_level_input(netif);
darran 0:55a05330f8cc 234 /* no packet could be read, silently ignore this */
darran 0:55a05330f8cc 235 if (p == NULL) return;
darran 0:55a05330f8cc 236 /* points to packet payload, which starts with an Ethernet header */
darran 0:55a05330f8cc 237 ethhdr = p->payload;
darran 0:55a05330f8cc 238
darran 0:55a05330f8cc 239 switch (htons(ethhdr->type)) {
darran 0:55a05330f8cc 240 /* IP or ARP packet? */
darran 0:55a05330f8cc 241 case ETHTYPE_IP:
darran 0:55a05330f8cc 242 case ETHTYPE_ARP:
darran 0:55a05330f8cc 243 #if PPPOE_SUPPORT
darran 0:55a05330f8cc 244 /* PPPoE packet? */
darran 0:55a05330f8cc 245 case ETHTYPE_PPPOEDISC:
darran 0:55a05330f8cc 246 case ETHTYPE_PPPOE:
darran 0:55a05330f8cc 247 #endif /* PPPOE_SUPPORT */
darran 0:55a05330f8cc 248 /* full packet send to tcpip_thread to process */
darran 0:55a05330f8cc 249 if (netif->input(p, netif)!=ERR_OK)
darran 0:55a05330f8cc 250 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
darran 0:55a05330f8cc 251 pbuf_free(p);
darran 0:55a05330f8cc 252 p = NULL;
darran 0:55a05330f8cc 253 }
darran 0:55a05330f8cc 254 break;
darran 0:55a05330f8cc 255
darran 0:55a05330f8cc 256 default:
darran 0:55a05330f8cc 257 pbuf_free(p);
darran 0:55a05330f8cc 258 p = NULL;
darran 0:55a05330f8cc 259 break;
darran 0:55a05330f8cc 260 }
darran 0:55a05330f8cc 261 }
darran 0:55a05330f8cc 262
darran 0:55a05330f8cc 263 /**
darran 0:55a05330f8cc 264 * Should be called at the beginning of the program to set up the
darran 0:55a05330f8cc 265 * network interface. It calls the function low_level_init() to do the
darran 0:55a05330f8cc 266 * actual setup of the hardware.
darran 0:55a05330f8cc 267 *
darran 0:55a05330f8cc 268 * This function should be passed as a parameter to netif_add().
darran 0:55a05330f8cc 269 *
darran 0:55a05330f8cc 270 * @param netif the lwip network interface structure for this ethernetif
darran 0:55a05330f8cc 271 * @return ERR_OK if the loopif is initialized
darran 0:55a05330f8cc 272 * ERR_MEM if private data couldn't be allocated
darran 0:55a05330f8cc 273 * any other err_t on error
darran 0:55a05330f8cc 274 */
darran 0:55a05330f8cc 275 err_t
darran 0:55a05330f8cc 276 ethernetif_init(struct netif *netif)
darran 0:55a05330f8cc 277 {
darran 0:55a05330f8cc 278 struct ethernetif *ethernetif;
darran 0:55a05330f8cc 279
darran 0:55a05330f8cc 280 LWIP_ASSERT("netif != NULL", (netif != NULL));
darran 0:55a05330f8cc 281
darran 0:55a05330f8cc 282 ethernetif = mem_malloc(sizeof(struct ethernetif));
darran 0:55a05330f8cc 283 if (ethernetif == NULL) {
darran 0:55a05330f8cc 284 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
darran 0:55a05330f8cc 285 return ERR_MEM;
darran 0:55a05330f8cc 286 }
darran 0:55a05330f8cc 287
darran 0:55a05330f8cc 288 #if LWIP_NETIF_HOSTNAME
darran 0:55a05330f8cc 289 /* Initialize interface hostname */
darran 0:55a05330f8cc 290 netif->hostname = "lwip";
darran 0:55a05330f8cc 291 #endif /* LWIP_NETIF_HOSTNAME */
darran 0:55a05330f8cc 292
darran 0:55a05330f8cc 293 /*
darran 0:55a05330f8cc 294 * Initialize the snmp variables and counters inside the struct netif.
darran 0:55a05330f8cc 295 * The last argument should be replaced with your link speed, in units
darran 0:55a05330f8cc 296 * of bits per second.
darran 0:55a05330f8cc 297 */
darran 0:55a05330f8cc 298 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
darran 0:55a05330f8cc 299
darran 0:55a05330f8cc 300 netif->state = ethernetif;
darran 0:55a05330f8cc 301 netif->name[0] = IFNAME0;
darran 0:55a05330f8cc 302 netif->name[1] = IFNAME1;
darran 0:55a05330f8cc 303 /* We directly use etharp_output() here to save a function call.
darran 0:55a05330f8cc 304 * You can instead declare your own function an call etharp_output()
darran 0:55a05330f8cc 305 * from it if you have to do some checks before sending (e.g. if link
darran 0:55a05330f8cc 306 * is available...) */
darran 0:55a05330f8cc 307 netif->output = etharp_output;
darran 0:55a05330f8cc 308 netif->linkoutput = low_level_output;
darran 0:55a05330f8cc 309
darran 0:55a05330f8cc 310 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
darran 0:55a05330f8cc 311
darran 0:55a05330f8cc 312 /* initialize the hardware */
darran 0:55a05330f8cc 313 low_level_init(netif);
darran 0:55a05330f8cc 314
darran 0:55a05330f8cc 315 return ERR_OK;
darran 0:55a05330f8cc 316 }
darran 0:55a05330f8cc 317
darran 0:55a05330f8cc 318 #endif /* 0 */