3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 DECLARE_GLOBAL_DATA_PTR;
31 static char input_buffer[512];
32 static int input_size = 0; /* char count in input buffer */
33 static int input_offset = 0; /* offset to valid chars in input buffer */
34 static int input_recursion = 0;
35 static int output_recursion = 0;
36 static int net_timeout;
37 static uchar nc_ether[6]; /* server enet address */
38 static IPaddr_t nc_ip; /* server ip */
39 static short nc_port; /* source/target port */
40 static const char *output_packet; /* used by first send udp */
41 static int output_packet_len = 0;
43 static void nc_wait_arp_handler (uchar * pkt, unsigned dest, unsigned src,
46 NetState = NETLOOP_SUCCESS; /* got arp reply - quit net loop */
49 static void nc_handler (uchar * pkt, unsigned dest, unsigned src,
53 NetState = NETLOOP_SUCCESS; /* got input - quit net loop */
56 static void nc_timeout (void)
58 NetState = NETLOOP_SUCCESS;
63 if (!output_packet_len || memcmp (nc_ether, NetEtherNullAddr, 6)) {
64 /* going to check for input packet */
65 NetSetHandler (nc_handler);
66 NetSetTimeout (net_timeout, nc_timeout);
68 /* send arp request */
70 NetSetHandler (nc_wait_arp_handler);
71 pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
72 memcpy (pkt, output_packet, output_packet_len);
73 NetSendUDPPacket (nc_ether, nc_ip, nc_port, nc_port, output_packet_len);
77 int nc_input_packet (uchar * pkt, unsigned dest, unsigned src, unsigned len)
81 if (dest != nc_port || !len)
82 return 0; /* not for us */
84 if (input_size == sizeof input_buffer)
85 return 1; /* no space */
86 if (len > sizeof input_buffer - input_size)
87 len = sizeof input_buffer - input_size;
89 end = input_offset + input_size;
90 if (end > sizeof input_buffer)
91 end -= sizeof input_buffer;
94 if (end + len > sizeof input_buffer) {
95 chunk = sizeof input_buffer - end;
96 memcpy(input_buffer, pkt + chunk, len - chunk);
98 memcpy (input_buffer + end, pkt, chunk);
105 static void nc_send_packet (const char *buf, int len)
107 struct eth_device *eth;
113 if ((eth = eth_get_dev ()) == NULL) {
117 if (!memcmp (nc_ether, NetEtherNullAddr, 6)) {
118 if (eth->state == ETH_STATE_ACTIVE)
119 return; /* inside net loop */
121 output_packet_len = len;
122 NetLoop (NETCONS); /* wait for arp reply and send packet */
123 output_packet_len = 0;
127 if (eth->state != ETH_STATE_ACTIVE) {
128 if (eth_init (gd->bd) < 0)
132 pkt = (uchar *) NetTxPacket + NetEthHdrSize () + IP_HDR_SIZE;
133 memcpy (pkt, buf, len);
136 NetSendUDPPacket (ether, ip, nc_port, nc_port, len);
146 nc_port = 6666; /* default port */
148 if (getenv ("ncip")) {
151 nc_ip = getenv_IPaddr ("ncip");
153 return -1; /* ncip is 0.0.0.0 */
154 if ((p = strchr (getenv ("ncip"), ':')) != NULL)
155 nc_port = simple_strtoul (p + 1, NULL, 10);
157 nc_ip = ~0; /* ncip is not set */
159 our_ip = getenv_IPaddr ("ipaddr");
160 netmask = getenv_IPaddr ("netmask");
162 if (nc_ip == ~0 || /* 255.255.255.255 */
163 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
164 (netmask | nc_ip) == ~0)) /* broadcast to our net */
165 memset (nc_ether, 0xff, sizeof nc_ether);
167 memset (nc_ether, 0, sizeof nc_ether); /* force arp request */
172 void nc_putc (char c)
174 if (output_recursion)
176 output_recursion = 1;
178 nc_send_packet (&c, 1);
180 output_recursion = 0;
183 void nc_puts (const char *s)
187 if (output_recursion)
189 output_recursion = 1;
191 if ((len = strlen (s)) > 512)
194 nc_send_packet (s, len);
196 output_recursion = 0;
205 net_timeout = 0; /* no timeout */
211 c = input_buffer[input_offset++];
213 if (input_offset >= sizeof input_buffer)
214 input_offset -= sizeof input_buffer;
222 struct eth_device *eth;
230 eth = eth_get_dev ();
231 if (eth && eth->state == ETH_STATE_ACTIVE)
232 return 0; /* inside net loop */
237 NetLoop (NETCONS); /* kind of poll */
241 return input_size != 0;
244 int drv_nc_init (void)
249 memset (&dev, 0, sizeof (dev));
251 strcpy (dev.name, "nc");
252 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
253 dev.start = nc_start;
259 rc = device_register (&dev);
261 return (rc == 0) ? 1 : rc;