]> Joshua Wise's Git repositories - netwatch.git/blob - net/net.c
Only memcpy as much as we need to when we need to.
[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 #include "rfb.h"
22
23 static struct nic *_nic = 0x0;
24 static struct netif _netif;
25
26 extern struct pci_driver a3c90x_driver;
27
28 void eth_poll()
29 {
30         struct pbuf *p;
31         struct eth_hdr *ethhdr;
32         static int ticks = 0;
33         int i = 15;     /* Don't process more than 15 packets at a time; we don't want the host to get TOO badly slowed down... */
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         while ((i--) && ((p = _nic->recv(_nic)) != NULL))
49         {
50 //              outputf("NIC: Packet: %d bytes", p->tot_len);
51                         
52                 LINK_STATS_INC(link.recv);
53                 
54                 ethhdr = p->payload;
55                 
56                 switch (htons(ethhdr->type)) {
57                 case ETHTYPE_IP:
58                 case ETHTYPE_ARP:
59                         if (_netif.input(p, &_netif) != ERR_OK)
60                         {
61                                 LWIP_DEBUGF(NETIF_DEBUG, ("netdev_input: IP input error\n"));
62                                 pbuf_free(p);
63                         }
64                         break;
65                         
66                 default:
67                         outputf("Unhandled packet type %04x input", ethhdr->type);
68                         pbuf_free(p);
69                         break;
70                 }
71         }
72 }
73
74 static err_t _transmit(struct netif *netif, struct pbuf *p)
75 {
76         struct nic *nic = netif->state;
77
78 //      outputf("NIC: Transmit packet");
79
80         nic->transmit(p);
81
82         LINK_STATS_INC(link.xmit);
83
84         return ERR_OK;
85 }
86
87 static err_t _init(struct netif *netif)
88 {
89         struct nic *nic = netif->state;
90         
91         LWIP_ASSERT("netif != NULL", (netif != NULL));
92                 
93 #if LWIP_NETIF_HOSTNAME
94         netif->hostname = "netwatch";
95 #endif
96         
97         NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
98
99         netif->name[0] = 'e';
100         netif->name[1] = 'n';
101         netif->output = etharp_output;
102         netif->linkoutput = _transmit;
103         
104         memcpy(netif->hwaddr, nic->hwaddr, 6);
105         netif->mtu = 1500;
106         netif->hwaddr_len = ETHARP_HWADDR_LEN;
107         netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
108         
109         return ERR_OK;
110 }
111
112 int eth_register(struct nic *nic)
113 {
114         static struct ip_addr ipa = { 0 } , netmask = { 0 } , gw = { 0 };
115         
116         if (_nic)
117                 return -1;
118         netif_add(&_netif, &ipa, &netmask, &gw, (void*)nic, _init, ethernet_input);
119         netif_set_default(&_netif);
120         netif_set_up(&_netif);
121         dhcp_start(&_netif);
122         _nic = nic;
123         return 0;
124 }
125
126 void eth_init()
127 {
128         extern void httpd_init();
129         
130         /* Required for DMA to work. :( */
131         smram_tseg_set_state(SMRAM_TSEG_OPEN);
132         lwip_init();
133         httpd_init();
134
135         rfb_init();
136
137 }
This page took 0.034346 seconds and 4 git commands to generate.