Contiki 2.5
ethernet.c
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  * @(#)$Id: ethernet.c,v 1.7 2010/09/28 23:02:16 oliverschmidt Exp $
34  */
35 
36 #include <modload.h>
37 
38 #include "contiki-net.h"
39 #include "cfs/cfs.h"
40 #include "sys/log.h"
41 #include "lib/error.h"
42 #include "net/ethernet-drv.h"
43 
44 #include "net/ethernet.h"
45 
46 struct {
47  char signature[4];
48  struct uip_eth_addr ethernet_address;
49  u8_t *buffer;
50  u16_t buffer_size;
51  void __fastcall__ (* init)(u16_t reg);
52  u16_t (* poll)(void);
53  void __fastcall__ (* send)(u16_t len);
54  void (* exit)(void);
55 } *module;
56 
57 /*---------------------------------------------------------------------------*/
58 void CC_FASTCALL
59 ethernet_init(struct ethernet_config *config)
60 {
61  static const char signature[4] = {0x65, 0x74, 0x68, 0x01};
62 
63 #ifndef ETHERNET
64 
65  struct mod_ctrl module_control = {cfs_read};
66  u8_t byte;
67 
68  module_control.callerdata = cfs_open(config->name, CFS_READ);
69  if(module_control.callerdata < 0) {
70  log_message(config->name, ": File not found");
71  error_exit();
72  }
73 
74  byte = mod_load(&module_control);
75  if(byte != MLOAD_OK) {
76  log_message(config->name, byte == MLOAD_ERR_MEM? ": Out of memory":
77  ": No module");
78  error_exit();
79  }
80 
81  cfs_close(module_control.callerdata);
82  module = module_control.module;
83 
84  for(byte = 0; byte < 4; ++byte) {
85  if(module->signature[byte] != signature[byte]) {
86  log_message(config->name, ": No ETH driver");
87  error_exit();
88  }
89  }
90 
91 #else /* !ETHERNET */
92 
93  extern void ETHERNET;
94 
95  module = &ETHERNET;
96 
97 #endif /* !ETHERNET */
98 
99  module->buffer = uip_buf;
100  module->buffer_size = UIP_BUFSIZE;
101  module->init(config->addr);
102 
103  uip_setethaddr(module->ethernet_address);
104 }
105 /*---------------------------------------------------------------------------*/
106 u16_t
107 ethernet_poll(void)
108 {
109  return module->poll();
110 }
111 /*---------------------------------------------------------------------------*/
112 void
113 ethernet_send(void)
114 {
115  module->send(uip_len);
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 ethernet_exit(void)
120 {
121  module->exit();
122 
123 #ifndef ETHERNET
124  mod_free(module);
125 #endif /* !ETHERNET */
126 }
127 /*---------------------------------------------------------------------------*/