Committer:
mbed714
Date:
Sat Sep 18 23:05:49 2010 +0000
Revision:
0:d616ece2d859

        

Who changed what in which revision?

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