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,
26 #include <stdio_dev.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 static char input_buffer[512];
32 static int input_size; /* char count in input buffer */
33 static int input_offset; /* offset to valid chars in input buffer */
34 static int input_recursion;
35 static int output_recursion;
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_out_port; /* target output port */
40 static short nc_in_port; /* source input port */
41 static const char *output_packet; /* used by first send udp */
42 static int output_packet_len;
44 static void nc_wait_arp_handler(uchar *pkt, unsigned dest,
45 IPaddr_t sip, unsigned src,
48 net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */
51 static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
55 net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */
58 static void nc_timeout(void)
60 net_set_state(NETLOOP_SUCCESS);
65 if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) {
66 /* going to check for input packet */
67 net_set_udp_handler(nc_handler);
68 NetSetTimeout(net_timeout, nc_timeout);
70 /* send arp request */
72 net_set_arp_handler(nc_wait_arp_handler);
73 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
74 memcpy(pkt, output_packet, output_packet_len);
75 NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port,
80 int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len)
84 if (dest != nc_in_port || !len)
85 return 0; /* not for us */
87 debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt);
89 if (input_size == sizeof(input_buffer))
90 return 1; /* no space */
91 if (len > sizeof(input_buffer) - input_size)
92 len = sizeof(input_buffer) - input_size;
94 end = input_offset + input_size;
95 if (end > sizeof(input_buffer))
96 end -= sizeof(input_buffer);
99 if (end + len > sizeof(input_buffer)) {
100 chunk = sizeof(input_buffer) - end;
101 memcpy(input_buffer, pkt + chunk, len - chunk);
103 memcpy(input_buffer + end, pkt, chunk);
110 static void nc_send_packet(const char *buf, int len)
112 struct eth_device *eth;
118 debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf);
124 if (!memcmp(nc_ether, NetEtherNullAddr, 6)) {
125 if (eth->state == ETH_STATE_ACTIVE)
126 return; /* inside net loop */
128 output_packet_len = len;
129 NetLoop(NETCONS); /* wait for arp reply and send packet */
130 output_packet_len = 0;
134 if (eth->state != ETH_STATE_ACTIVE) {
135 if (eth_init(gd->bd) < 0)
139 pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
140 memcpy(pkt, buf, len);
143 NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len);
149 static int nc_start(void)
154 nc_out_port = 6666; /* default port */
155 nc_in_port = nc_out_port;
157 if (getenv("ncip")) {
159 nc_ip = getenv_IPaddr("ncip");
161 return -1; /* ncip is 0.0.0.0 */
162 p = strchr(getenv("ncip"), ':');
164 nc_out_port = simple_strtoul(p + 1, NULL, 10);
165 nc_in_port = nc_out_port;
168 nc_ip = ~0; /* ncip is not set, so broadcast */
170 p = getenv("ncoutport");
172 nc_out_port = simple_strtoul(p, NULL, 10);
173 p = getenv("ncinport");
175 nc_in_port = simple_strtoul(p, NULL, 10);
177 our_ip = getenv_IPaddr("ipaddr");
178 netmask = getenv_IPaddr("netmask");
180 if (nc_ip == ~0 || /* 255.255.255.255 */
181 ((netmask & our_ip) == (netmask & nc_ip) && /* on the same net */
182 (netmask | nc_ip) == ~0)) /* broadcast to our net */
183 memset(nc_ether, 0xff, sizeof(nc_ether));
185 memset(nc_ether, 0, sizeof(nc_ether)); /* force arp request */
188 * Initialize the static IP settings and buffer pointers
189 * incase we call NetSendUDPPacket before NetLoop
196 static void nc_putc(char c)
198 if (output_recursion)
200 output_recursion = 1;
202 nc_send_packet(&c, 1);
204 output_recursion = 0;
207 static void nc_puts(const char *s)
211 if (output_recursion)
213 output_recursion = 1;
217 int send_len = min(len, 512);
218 nc_send_packet(s, send_len);
223 output_recursion = 0;
226 static int nc_getc(void)
232 net_timeout = 0; /* no timeout */
238 c = input_buffer[input_offset++];
240 if (input_offset >= sizeof(input_buffer))
241 input_offset -= sizeof(input_buffer);
247 static int nc_tstc(void)
249 struct eth_device *eth;
258 if (eth && eth->state == ETH_STATE_ACTIVE)
259 return 0; /* inside net loop */
264 NetLoop(NETCONS); /* kind of poll */
268 return input_size != 0;
271 int drv_nc_init(void)
273 struct stdio_dev dev;
276 memset(&dev, 0, sizeof(dev));
278 strcpy(dev.name, "nc");
279 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
280 dev.start = nc_start;
286 rc = stdio_register(&dev);
288 return (rc == 0) ? 1 : rc;