]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/src/ecos/init.c
unified MX27, MX25, MX37 trees
[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 <pkgconf/net_lwip.h>
38 #include "lwip/opt.h"
39 #include "lwip/sys.h"
40 #include "lwip/memp.h"
41 #include "lwip/tcpip.h"
42 #include "lwip/ip_addr.h"
43
44 #if LWIP_DHCP
45 #include "lwip/dhcp.h"
46 #endif
47
48 #if LWIP_SLIP
49 #include "netif/slipif.h"
50 #endif
51
52 #if PPP_SUPPORT
53 #include "netif/ppp/ppp.h"
54 #endif
55
56 #include "netif/loopif.h"
57 #include <cyg/hal/hal_if.h>
58 #include <cyg/infra/diag.h>
59
60 #ifdef CYGPKG_LWIP_ETH
61 #include "netif/etharp.h"
62
63 #include <cyg/io/eth/eth_drv.h>
64 #include <cyg/io/eth/netdev.h>
65
66
67 // Define table boundaries
68 CYG_HAL_TABLE_BEGIN(__NETDEVTAB__, netdev);
69 CYG_HAL_TABLE_END(__NETDEVTAB_END__, netdev);
70 static void ecosglue_init(void);
71 #endif
72
73 void inline IP_ADDR(struct ip_addr *ipaddr, char a, char b, char c, char d)
74 {
75         IP4_ADDR(ipaddr,a,b,c,d);
76 }
77
78
79 struct netif mynetif, loopif;
80 void lwip_set_addr(struct netif *netif);
81 #if PPP_SUPPORT
82 #define PPP_USER "pppuser"
83 #define PPP_PASS "ppppass"
84
85 void 
86 pppMyCallback(void *a , int e, void * arg)
87 {
88         diag_printf("callback %d \n",e);
89 }
90
91 /* These temporarily here */
92 unsigned long
93 sys_jiffies(void)
94 {
95    return cyg_current_time();
96 }
97
98 void 
99 ppp_trace(int level, const char *format,...)
100 {
101     va_list args;
102
103     (void)level;
104     va_start(args, format);
105     diag_vprintf(format, args);
106     va_end(args);
107 }       
108 #endif
109
110 #if LWIP_HAVE_LOOPIF
111 struct netif ecos_loopif;
112 #endif
113
114 #ifdef CYGPKG_LWIP_ETH
115 static void
116 arp_timer(void *arg)
117 {
118   etharp_tmr();
119   sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler) arp_timer, NULL);
120 }
121 #endif
122
123 #if LWIP_DHCP
124 static void lwip_dhcp_fine_tmr(void *arg)
125 {
126     dhcp_fine_tmr();
127     sys_timeout(500, (sys_timeout_handler) lwip_dhcp_fine_tmr, NULL);
128 }
129
130 static void lwip_dhcp_coarse_tmr(void *arg)
131 {
132     dhcp_coarse_tmr();
133     sys_timeout(60000, (sys_timeout_handler) lwip_dhcp_coarse_tmr, NULL);
134 }
135 #endif
136
137
138 //
139 // This function is called when tcpip thread finished initialisation.
140 // We start several timers here - these timers are all handled in the
141 // tcpip thread. That means that also the DHCP stuff is handled in the
142 // TCPIP thread. If this causes any trouble than it may be necessaray to
143 // use an own DHCP thread insted.
144 //
145 void tcpip_init_done(void * arg)
146 {
147 #ifdef CYGPKG_LWIP_ETH
148     sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler) arp_timer, NULL);
149 #endif
150 #ifdef CYGOPT_LWIP_DHCP_MANAGEMENT
151         sys_timeout(500, (sys_timeout_handler) lwip_dhcp_fine_tmr, NULL);
152         sys_timeout(60000, (sys_timeout_handler) lwip_dhcp_coarse_tmr, NULL);
153 #endif
154         sys_sem_t *sem = arg;
155         sys_sem_signal(*sem);
156 }
157
158
159 /*
160  * Called by the eCos application at startup
161  * wraps various init calls
162  */
163 int
164 lwip_init(void)
165 {
166 #if LWIP_HAVE_LOOPIF
167         struct ip_addr ipaddr, netmask, gw;
168 #endif
169         static int inited = 0;
170         sys_sem_t sem;
171         if (inited)
172                 return 1;
173         inited++;
174         
175         sys_init();     /* eCos specific initialization */
176         mem_init();     /* heap based memory allocator */
177         memp_init();    /* pool based memory allocator */
178         pbuf_init();    /* packet buffer allocator */
179         netif_init();   /* netif layer */
180         
181         /* Start the stack.It will spawn a new dedicated thread */
182         sem = sys_sem_new(0);
183         tcpip_init(tcpip_init_done,&sem);
184         sys_sem_wait(sem);
185         sys_sem_free(sem);
186
187 #if LWIP_HAVE_LOOPIF
188         IP4_ADDR(&gw, 127,0,0,1);
189         IP4_ADDR(&ipaddr, 127,0,0,1);
190         IP4_ADDR(&netmask, 255,0,0,0);
191   
192         netif_add(&ecos_loopif, &ipaddr, &netmask, &gw, NULL, loopif_init,
193             tcpip_input);
194 #endif
195         
196 #if LWIP_SLIP   
197         lwip_set_addr(&mynetif);
198         slipif_init(&mynetif);
199         netif_set_default(&mynetif);
200 #elif PPP_SUPPORT
201         pppInit();
202 #if PAP_SUPPORT || CHAP_SUPPORT
203         pppSetAuth(PPPAUTHTYPE_PAP, PPP_USER, PPP_PASS);
204 #endif
205         pppOpen(sio_open(2), pppMyCallback, NULL);
206 #else   
207         ecosglue_init();                
208 #endif  
209         return 0;
210 }
211
212
213 err_t lwip_dummy_netif_init(struct netif *netif)
214 {
215     return ERR_OK; 
216 }
217
218
219 void
220 lwip_set_addr(struct netif *netif)
221 {
222         struct ip_addr ipaddr, netmask, gw;
223   
224 #if LWIP_DHCP
225     IP4_ADDR(&gw, 0,0,0,0);
226     IP4_ADDR(&ipaddr, 0,0,0,0);
227     IP4_ADDR(&netmask, 0,0,0,0);
228
229     netif_add(netif, &ipaddr, &netmask, &gw, netif->state, lwip_dummy_netif_init, tcpip_input);
230     netif_set_default(netif);
231     netif_set_up(netif);        // new step from lwip 1.0.0
232 #else
233         IP_ADDR(&gw, CYGDAT_LWIP_SERV_ADDR);
234         IP_ADDR(&ipaddr, CYGDAT_LWIP_MY_ADDR);
235         IP_ADDR(&netmask, CYGDAT_LWIP_NETMASK);
236
237         netif_add(netif, &ipaddr, &netmask, &gw, netif->state, lwip_dummy_netif_init, tcpip_input);
238     netif_set_default(netif); 
239     netif_set_up(netif);        // new step from lwip 1.0.0
240 #endif 
241 }
242
243 void lwip_dhcp_init(struct netif *netif)
244 {
245 #ifdef CYGOPT_LWIP_DHCP_MANAGEMENT
246     dhcp_start(netif);
247 #endif
248 }
249
250
251 #ifdef CYGPKG_LWIP_ETH
252 //io eth stuff
253
254 cyg_sem_t delivery;
255
256 void
257 lwip_dsr_stuff(void)
258 {
259   cyg_semaphore_post(&delivery);
260 }
261
262 //Input thread signalled by DSR calls deliver() on low level drivers
263 static void
264 input_thread(void *arg)
265 {
266   cyg_netdevtab_entry_t *t;
267
268   for (;;) {
269     cyg_semaphore_wait(&delivery);
270
271     for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
272       struct eth_drv_sc *sc = (struct eth_drv_sc *)t->device_instance;
273       if (sc->state & ETH_DRV_NEEDS_DELIVERY) {
274 #if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
275         cyg_bool was_ctrlc_int;
276 #endif
277         sc->state &= ~ETH_DRV_NEEDS_DELIVERY;
278 #if defined(CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT)
279         was_ctrlc_int = HAL_CTRLC_CHECK((*sc->funs->int_vector)(sc), (int)sc);
280           if (!was_ctrlc_int) // Fall through and run normal code
281                   
282 #endif
283         (sc->funs->deliver) (sc);
284       }
285     }
286   }
287
288 }
289
290
291 // Initialize all network devices
292 static void
293 init_hw_drivers(void)
294 {
295   cyg_netdevtab_entry_t *t;
296
297   for (t = &__NETDEVTAB__[0]; t != &__NETDEVTAB_END__; t++) {
298     if (t->init(t)) {
299       t->status = CYG_NETDEVTAB_STATUS_AVAIL;
300     } else {
301       // What to do if device init fails?
302       t->status = 0;            // Device not [currently] available
303     }
304   }
305 }
306
307 extern struct netif *netif_default;
308
309 static void
310 ecosglue_init(void)
311 {
312     etharp_init();
313     cyg_semaphore_init(&delivery, 0);
314     //
315     // start input thread before hardware drivers are initialized because
316     // init_hw_drivers() calls dhcp_init() if DHCP support is configured 
317     // and dhcp_init() requires a running input thread
318     //
319     sys_thread_new(input_thread, (void*)0, CYGNUM_LWIP_ETH_THREAD_PRIORITY);
320     init_hw_drivers();
321 }
322
323 #endif //CYGPKG_LWIP_ETH