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