]> Joshua Wise's Git repositories - netwatch.git/blob - net/net.c
tnt2 skel
[netwatch.git] / net / net.c
1 #include <pci.h>
2 #include <smram.h>
3 #include <pci-bother.h>
4 #include <output.h>
5 #include <minilib.h>
6 #include <lwip/init.h>
7 #include "net.h"
8
9 #include "lwip/opt.h"
10 #include "lwip/def.h"
11 #include "lwip/mem.h"
12 #include "lwip/pbuf.h"
13 #include "lwip/sys.h"
14 #include <lwip/stats.h>
15 #include <lwip/snmp.h>
16 #include <lwip/dhcp.h>
17 #include <lwip/tcp.h>
18 #include "netif/etharp.h"
19 #include "netif/ppp_oe.h"
20
21 static struct nic *_nic = 0x0;
22 static struct netif _netif;
23
24 extern struct pci_driver a3c90x_driver;
25
26 void eth_poll()
27 {
28         unsigned char pkt[1600];
29         int len;
30         struct pbuf *p, *q;
31         struct eth_hdr *ethhdr;
32         int pos = 0;
33         static int ticks = 0;
34         
35         if (!_nic)
36                 return;
37         
38         smram_tseg_set_state(SMRAM_TSEG_OPEN);
39         
40         if ((ticks % 1000) == 0)        /* About a minute */
41                 dhcp_coarse_tmr();
42         if ((ticks % 8) == 0)   /* About 500msec*/
43                 dhcp_fine_tmr();
44         if ((ticks % 4) == 0)   /* About 250msec*/
45                 tcp_tmr();
46         ticks++;
47
48         if (_nic->poll(_nic, 0))
49         {
50                 _nic->packet = pkt;
51                 _nic->poll(_nic, 1);
52                 
53                 len = _nic->packetlen;
54                 
55                 outputf("NIC: Packet: %d bytes", len);
56                         
57                 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
58                 if (p == NULL)
59                 {
60                         outputf("NIC: out of memory for packet?");
61                         LINK_STATS_INC(link.memerr);
62                         LINK_STATS_INC(link.drop);
63                         return;
64                 }
65         
66                 for(q = p; q != NULL; q = q->next)
67                 {
68                         memcpy(q->payload, pkt+pos, q->len);
69                         pos += q->len;
70                 }
71                 
72                 LINK_STATS_INC(link.recv);
73                 
74                 ethhdr = p->payload;
75                 
76                 switch (htons(ethhdr->type)) {
77                 case ETHTYPE_IP:
78                 case ETHTYPE_ARP:
79                         if (_netif.input(p, &_netif) != ERR_OK)
80                         {
81                                 LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n"));
82                                 pbuf_free(p);
83                         }
84                         break;
85                         
86                 default:
87                         outputf("Unhandled packet type %04x input", ethhdr->type);
88                         pbuf_free(p);
89                         break;
90                 }
91         }
92 }
93
94 static err_t _transmit(struct netif *netif, struct pbuf *p)
95 {
96         struct nic *nic = netif->state;
97         struct pbuf *q;
98         unsigned char pkt[1600];
99         unsigned int len = 0;
100
101         for(q = p; q != NULL; q = q->next)
102         {
103                 memcpy(pkt + len, q->payload, q->len);
104                 len += q->len;
105         }
106
107         outputf("NIC: Transmit packet: %d bytes", len);
108
109         nic->transmit(len, pkt);
110
111         LINK_STATS_INC(link.xmit);
112
113         return ERR_OK;
114 }
115
116 static err_t _init(struct netif *netif)
117 {
118         struct nic *nic = netif->state;
119         
120         LWIP_ASSERT("netif != NULL", (netif != NULL));
121                 
122 #if LWIP_NETIF_HOSTNAME
123         netif->hostname = "netwatch";
124 #endif
125         
126         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
127
128         netif->name[0] = 'e';
129         netif->name[1] = 'n';
130         netif->output = etharp_output;
131         netif->linkoutput = _transmit;
132         
133         memcpy(netif->hwaddr, nic->hwaddr, 6);
134         netif->mtu = 1500;
135         netif->hwaddr_len = ETHARP_HWADDR_LEN;
136         netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
137         
138         return ERR_OK;
139 }
140
141 int eth_register(struct nic *nic)
142 {
143         static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 };
144         
145         if (_nic)
146                 return -1;
147         netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input);
148         netif_set_default(&_netif);
149         netif_set_up(&_netif);
150         dhcp_start(&_netif);
151         _nic = nic;
152         return 0;
153 }
154
155 void eth_init()
156 {
157         extern void httpd_init();
158         
159         /* Required for DMA to work. :( */
160         smram_tseg_set_state(SMRAM_TSEG_OPEN);
161         lwip_init();
162         pci_probe_driver(a3c90x_driver);
163         httpd_init();
164 }
This page took 0.039982 seconds and 4 git commands to generate.