]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/src/ecos/init.c
129e0a3e1216512c4a8b4b3ec57a930d24247424
[karo-tx-redboot.git] / packages / net / lwip_tcpip / v2_0 / src / ecos / init.c
1 //==========================================================================
2 //####ECOSGPLCOPYRIGHTBEGIN####
3 // -------------------------------------------
4 // This file is part of eCos, the Embedded Configurable Operating System.
5 // Copyright (C) 2004 eCosCentric 
6 //
7 // eCos is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 or (at your option) any later version.
10 //
11 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with eCos; if not, write to the Free Software Foundation, Inc.,
18 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 //
20 // As a special exception, if other files instantiate templates or use macros
21 // or inline functions from this file, or you compile this file and link it
22 // with other works to produce a work based on this file, this file does not
23 // by itself cause the resulting work to be covered by the GNU General Public
24 // License. However the source code for this file must still be made available
25 // in accordance with section (3) of the GNU General Public License.
26 //
27 // This exception does not invalidate any other reasons why a work based on
28 // this file might be covered by the GNU General Public License.
29 // -------------------------------------------
30 //####ECOSGPLCOPYRIGHTEND####
31 //==========================================================================
32
33 /*
34  * init.c - misc lwip ecos glue functions 
35  */
36 #include <pkgconf/system.h>
37 #include "lwip/opt.h"
38 #include "lwip/sys.h"
39 #include "lwip/memp.h"
40 #include "lwip/tcpip.h"
41 #include "lwip/ip_addr.h"
42
43 #if LWIP_DHCP
44 #include "lwip/dhcp.h"
45 #endif
46
47 #if LWIP_SLIP
48 #include "netif/slipif.h"
49 #endif
50
51 #if PPP_SUPPORT
52 #include "netif/ppp/ppp.h"
53 #endif
54
55 #include "netif/loopif.h"
56 #include <cyg/hal/hal_if.h>
57 #include <cyg/infra/diag.h>
58
59 #ifdef CYGPKG_LWIP_ETH
60 #include "netif/etharp.h"
61
62 #include <cyg/io/eth/eth_drv.h>
63 #include <cyg/io/eth/netdev.h>
64
65
66 // Define table boundaries
67 CYG_HAL_TABLE_BEGIN(__NETDEVTAB__, netdev);
68 CYG_HAL_TABLE_END(__NETDEVTAB_END__, netdev);
69 static void ecosglue_init(void);
70 #endif
71
72 void inline IP_ADDR(struct ip_addr *ipaddr, char a, char b, char c, char d)
73 {
74         IP4_ADDR(ipaddr,a,b,c,d);
75 }
76
77
78 void tcpip_init_done(void * arg)
79 {
80         sys_sem_t *sem = arg;
81         sys_sem_signal(*sem);
82 }
83
84 struct netif mynetif, loopif;
85 void lwip_set_addr(struct netif *netif);
86 #if PPP_SUPPORT
87 #define PPP_USER "pppuser"
88 #define PPP_PASS "ppppass"
89
90 void 
91 pppMyCallback(void *a , int e, void * arg)
92 {
93         diag_printf("callback %d \n",e);
94 }
95
96 /* These temporarily here */
97 unsigned long
98 sys_jiffies(void)
99 {
100    return cyg_current_time();
101 }
102
103 void 
104 ppp_trace(int level, const char *format,...)
105 {
106     va_list args;
107
108     (void)level;
109     va_start(args, format);
110     diag_vprintf(format, args);
111     va_end(args);
112 }       
113 #endif
114
115 #if LWIP_HAVE_LOOPIF
116 struct netif ecos_loopif;
117 #endif
118
119 /*
120  * Called by the eCos application at startup
121  * wraps various init calls
122  */
123 int
124 lwip_init(void)
125 {
126         struct ip_addr ipaddr, netmask, gw;
127         static int inited = 0;
128         sys_sem_t sem;
129         if (inited)
130                 return 1;
131         inited++;
132         
133         sys_init();     /* eCos specific initialization */
134         mem_init();     /* heap based memory allocator */
135         memp_init();    /* pool based memory allocator */
136         pbuf_init();    /* packet buffer allocator */
137         netif_init();   /* netif layer */
138         
139         /* Start the stack.It will spawn a new dedicated thread */
140         sem = sys_sem_new(0);
141         tcpip_init(tcpip_init_done,&sem);
142         sys_sem_wait(sem);
143         sys_sem_free(sem);
144
145 #if LWIP_HAVE_LOOPIF
146         IP4_ADDR(&gw, 127,0,0,1);
147         IP4_ADDR(&ipaddr, 127,0,0,1);
148         IP4_ADDR(&netmask, 255,0,0,0);
149   
150         netif_add(&ecos_loopif, &ipaddr, &netmask, &gw, NULL, loopif_init,
151             tcpip_input);
152 #endif
153         
154 #if LWIP_SLIP   
155         lwip_set_addr(&mynetif);
156         slipif_init(&mynetif);
157         netif_set_default(&mynetif);
158 #elif PPP_SUPPORT
159         pppInit();
160 #if PAP_SUPPORT || CHAP_SUPPORT
161         pppSetAuth(PPPAUTHTYPE_PAP, PPP_USER, PPP_PASS);
162 #endif
163         pppOpen(sio_open(2), pppMyCallback, NULL);
164 #else   
165         ecosglue_init();                
166 #endif  
167         return 0;
168 }
169
170 void
171 lwip_set_addr(struct netif *netif)
172 {
173         struct ip_addr ipaddr, netmask, gw;
174
175         IP_ADDR(&gw, CYGPKG_LWIP_SERV_ADDR);
176         IP_ADDR(&ipaddr, CYGPKG_LWIP_MY_ADDR);
177         IP_ADDR(&netmask, CYGPKG_LWIP_NETMASK);
178         netif_set_addr(netif, &ipaddr, &netmask, &gw);
179         netif->next = netif_list;
180         netif_list = netif;
181         
182         netif->input = tcpip_input;
183         //netif->input = ip_input;
184 }
185
186 #ifdef CYGPKG_LWIP_ETH
187 //io eth stuff
188
189 cyg_sem_t delivery;
190
191 void
192 lwip_dsr_stuff(void)
193 {
194   cyg_semaphore_post(&delivery);
195 }
196
197 //Input thread signalled by DSR calls deliver() on low level drivers
198 static void
199 input_thread(void *arg)
200 {
201   cyg_netdevtab_entry_t *t;
202
203   for (;;) {
204     cyg_semaphore_wait(&delivery);
205
206     for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
207       struct eth_drv_sc *sc = (struct eth_drv_sc *)t->device_instance;
208       if (sc->state & ETH_DRV_NEEDS_DELIVERY) {
209 #if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
210         cyg_bool was_ctrlc_int;
211 #endif
212         sc->state &= ~ETH_DRV_NEEDS_DELIVERY;
213 #if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
214         was_ctrlc_int = HAL_CTRLC_CHECK((*sc->funs->int_vector)(sc), (int)sc);
215           if (!was_ctrlc_int) // Fall through and run normal code
216                   
217 #endif
218         (sc->funs->deliver) (sc);
219       }
220     }
221   }
222
223 }
224
225 // Initialize all network devices
226 static void
227 init_hw_drivers(void)
228 {
229   cyg_netdevtab_entry_t *t;
230
231   for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
232     if (t->init(t)) {
233       t->status = CYG_NETDEVTAB_STATUS_AVAIL;
234     } else {
235       // What to do if device init fails?
236       t->status = 0;            // Device not [currently] available
237     }
238   }
239 }
240
241 static void
242 arp_timer(void *arg)
243 {
244   etharp_tmr();
245   sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler) arp_timer, NULL);
246 }
247
248
249 static void
250 ecosglue_init(void)
251 {
252   cyg_semaphore_init(&delivery, 0);
253   init_hw_drivers();
254   sys_thread_new(input_thread, (void*)0, CYGPKG_LWIP_ETH_THREAD_PRIORITY);
255   etharp_init();
256   sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler) arp_timer, NULL);
257 }
258
259 #endif //CYGPKG_LWIP_ETH