]> Joshua Wise's Git repositories - netwatch.git/blame - net/net.c
Add httpd.
[netwatch.git] / net / net.c
CommitLineData
3b3161a1 1#include <pci.h>
6c744a5a 2#include <smram.h>
4d898e0b 3#include <pci-bother.h>
3b3161a1 4#include <output.h>
14e7cde4 5#include <minilib.h>
9552cf46 6#include <lwip/init.h>
4d898e0b 7#include "net.h"
47c41031
JW
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>
98d29191
JW
16#include <lwip/dhcp.h>
17#include <lwip/tcp.h>
47c41031
JW
18#include "netif/etharp.h"
19#include "netif/ppp_oe.h"
3b3161a1 20
7a914840 21static struct nic *_nic = 0x0;
47c41031 22static struct netif _netif;
3e6d6106 23
7a914840
JW
24extern struct pci_driver a3c90x_driver;
25
47c41031 26void eth_poll()
6c744a5a 27{
47c41031
JW
28 unsigned char pkt[1600];
29 int len;
30 struct pbuf *p, *q;
31 struct eth_hdr *ethhdr;
32 int pos = 0;
98d29191 33 static int ticks = 0;
47c41031
JW
34
35 if (!_nic)
36 return;
37
38 smram_tseg_set_state(SMRAM_TSEG_OPEN);
98d29191
JW
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++;
6c744a5a 47
47c41031
JW
48 if (!_nic->poll(_nic, 0))
49 return;
50
51 _nic->packet = pkt;
52 _nic->poll(_nic, 1);
53
54 len = _nic->packetlen;
14e7cde4 55
47c41031
JW
56 outputf("NIC: Packet: %d bytes", len);
57
58 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
59 if (p == NULL)
60 {
61 outputf("NIC: out of memory for packet?");
62 LINK_STATS_INC(link.memerr);
63 LINK_STATS_INC(link.drop);
64 return;
65 }
66
67 for(q = p; q != NULL; q = q->next)
68 {
69 memcpy(q->payload, pkt+pos, q->len);
70 pos += q->len;
71 }
14e7cde4 72
47c41031
JW
73 LINK_STATS_INC(link.recv);
74
75 ethhdr = p->payload;
76
77 switch (htons(ethhdr->type)) {
78 case ETHTYPE_IP:
79 case ETHTYPE_ARP:
80 if (_netif.input(p, &_netif) != ERR_OK)
81 {
82 LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n"));
83 pbuf_free(p);
84 }
31e64b08 85 break;
47c41031
JW
86
87 default:
88 outputf("Unhandled packet type %04x input", ethhdr->type);
89 pbuf_free(p);
31e64b08 90 break;
14e7cde4
JP
91 }
92}
93
47c41031 94static err_t _transmit(struct netif *netif, struct pbuf *p)
3b3161a1 95{
47c41031
JW
96 struct nic *nic = netif->state;
97 struct pbuf *q;
98 unsigned char pkt[1600];
99 unsigned int len = 0;
99182958 100
47c41031
JW
101 for(q = p; q != NULL; q = q->next)
102 {
103 memcpy(pkt + len, q->payload, q->len);
104 len += q->len;
105 }
50d89a31 106
47c41031 107 outputf("NIC: Transmit packet: %d bytes", len);
50d89a31 108
47c41031 109 nic->transmit(len, pkt);
d3411e0d 110
47c41031
JW
111 LINK_STATS_INC(link.xmit);
112
113 return ERR_OK;
114}
115
116static 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
6c744a5a 125
47c41031 126 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
6c744a5a 127
47c41031
JW
128 netif->name[0] = 'e';
129 netif->name[1] = 'n';
130 netif->output = etharp_output;
131 netif->linkoutput = _transmit;
99182958 132
47c41031
JW
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;
3b3161a1
JW
139}
140
7a914840 141int eth_register(struct nic *nic)
3b3161a1 142{
98d29191 143 static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 };
47c41031 144
7a914840
JW
145 if (_nic)
146 return -1;
47c41031
JW
147 netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input);
148 netif_set_default(&_netif);
149 netif_set_up(&_netif);
98d29191 150 dhcp_start(&_netif);
7a914840
JW
151 _nic = nic;
152 return 0;
3b3161a1
JW
153}
154
155void eth_init()
156{
6f9272bd
JW
157 extern void httpd_init();
158
c25f3f39
JW
159 /* Required for DMA to work. :( */
160 smram_tseg_set_state(SMRAM_TSEG_OPEN);
9552cf46 161 lwip_init();
47c41031 162 pci_probe_driver(a3c90x_driver);
6f9272bd 163 httpd_init();
3b3161a1 164}
This page took 0.039958 seconds and 4 git commands to generate.