Contiki-Inga 3.x
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  */
34 
35 #include <modload.h>
36 
37 #include "contiki-net.h"
38 #include "cfs/cfs.h"
39 #include "sys/log.h"
40 #include "lib/error.h"
41 #include "net/ethernet-drv.h"
42 
43 #include "net/ethernet.h"
44 
45 struct {
46  char signature[4];
47  struct uip_eth_addr ethernet_address;
48  uint8_t *buffer;
49  uint16_t buffer_size;
50  void __fastcall__ (* init)(uint16_t reg);
51  uint16_t (* poll)(void);
52  void __fastcall__ (* send)(uint16_t len);
53  void (* exit)(void);
54 } *module;
55 
56 /*---------------------------------------------------------------------------*/
57 void CC_FASTCALL
58 ethernet_init(struct ethernet_config *config)
59 {
60  static const char signature[4] = {0x65, 0x74, 0x68, 0x01};
61 
62 #ifndef ETHERNET
63 
64  struct mod_ctrl module_control = {cfs_read};
65  uint8_t byte;
66 
67  module_control.callerdata = cfs_open(config->name, CFS_READ);
68  if(module_control.callerdata < 0) {
69  log_message(config->name, ": File not found");
70  error_exit();
71  }
72 
73  byte = mod_load(&module_control);
74  if(byte != MLOAD_OK) {
75  log_message(config->name, byte == MLOAD_ERR_MEM? ": Out of memory":
76  ": No module");
77  error_exit();
78  }
79 
80  cfs_close(module_control.callerdata);
81  module = module_control.module;
82 
83  for(byte = 0; byte < 4; ++byte) {
84  if(module->signature[byte] != signature[byte]) {
85  log_message(config->name, ": No ETH driver");
86  error_exit();
87  }
88  }
89 
90 #else /* !ETHERNET */
91 
92  extern void ETHERNET;
93 
94  module = &ETHERNET;
95 
96 #endif /* !ETHERNET */
97 
98  module->buffer = uip_buf;
99  module->buffer_size = UIP_BUFSIZE;
100  module->init(config->addr);
101 
102  uip_setethaddr(module->ethernet_address);
103 }
104 /*---------------------------------------------------------------------------*/
105 uint16_t
106 ethernet_poll(void)
107 {
108  return module->poll();
109 }
110 /*---------------------------------------------------------------------------*/
111 void
112 ethernet_send(void)
113 {
114  module->send(uip_len);
115 }
116 /*---------------------------------------------------------------------------*/
117 void
118 ethernet_exit(void)
119 {
120  module->exit();
121 
122 #ifndef ETHERNET
123  mod_free(module);
124 #endif /* !ETHERNET */
125 }
126 /*---------------------------------------------------------------------------*/