wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 25 | #include <command.h> |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 26 | #include <stdio_dev.h> |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 27 | #include <net.h> |
| 28 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Joe Hershberger | 2c8fe51 | 2012-07-31 06:09:17 +0000 | [diff] [blame] | 31 | #ifndef CONFIG_NETCONSOLE_BUFFER_SIZE |
| 32 | #define CONFIG_NETCONSOLE_BUFFER_SIZE 512 |
| 33 | #endif |
| 34 | |
| 35 | static char input_buffer[CONFIG_NETCONSOLE_BUFFER_SIZE]; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 36 | static int input_size; /* char count in input buffer */ |
| 37 | static int input_offset; /* offset to valid chars in input buffer */ |
| 38 | static int input_recursion; |
| 39 | static int output_recursion; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 40 | static int net_timeout; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 41 | static uchar nc_ether[6]; /* server enet address */ |
| 42 | static IPaddr_t nc_ip; /* server ip */ |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 43 | static short nc_out_port; /* target output port */ |
| 44 | static short nc_in_port; /* source input port */ |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 45 | static const char *output_packet; /* used by first send udp */ |
| 46 | static int output_packet_len; |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Start with a default last protocol. |
| 49 | * We are only interested in NETCONS or not. |
| 50 | */ |
| 51 | enum proto_t net_loop_last_protocol = BOOTP; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 52 | |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 53 | static void nc_wait_arp_handler(uchar *pkt, unsigned dest, |
| 54 | IPaddr_t sip, unsigned src, |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 55 | unsigned len) |
| 56 | { |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 57 | net_set_state(NETLOOP_SUCCESS); /* got arp reply - quit net loop */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Luca Ceresoli | 03eb129 | 2011-04-18 06:19:50 +0000 | [diff] [blame] | 60 | static void nc_handler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 61 | unsigned len) |
| 62 | { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 63 | if (input_size) |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 64 | net_set_state(NETLOOP_SUCCESS); /* got input - quit net loop */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 67 | static void nc_timeout(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 68 | { |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 69 | net_set_state(NETLOOP_SUCCESS); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 72 | static int is_broadcast(IPaddr_t ip) |
| 73 | { |
| 74 | static IPaddr_t netmask; |
| 75 | static IPaddr_t our_ip; |
| 76 | static int env_changed_id; |
| 77 | int env_id = get_env_id(); |
| 78 | |
| 79 | /* update only when the environment has changed */ |
| 80 | if (env_changed_id != env_id) { |
| 81 | netmask = getenv_IPaddr("netmask"); |
| 82 | our_ip = getenv_IPaddr("ipaddr"); |
| 83 | |
| 84 | env_changed_id = env_id; |
| 85 | } |
| 86 | |
| 87 | return (ip == ~0 || /* 255.255.255.255 */ |
| 88 | ((netmask & our_ip) == (netmask & ip) && /* on the same net */ |
| 89 | (netmask | ip) == ~0)); /* broadcast to our net */ |
| 90 | } |
| 91 | |
| 92 | static int refresh_settings_from_env(void) |
| 93 | { |
| 94 | const char *p; |
| 95 | static int env_changed_id; |
| 96 | int env_id = get_env_id(); |
| 97 | |
| 98 | /* update only when the environment has changed */ |
| 99 | if (env_changed_id != env_id) { |
| 100 | if (getenv("ncip")) { |
| 101 | nc_ip = getenv_IPaddr("ncip"); |
| 102 | if (!nc_ip) |
| 103 | return -1; /* ncip is 0.0.0.0 */ |
| 104 | p = strchr(getenv("ncip"), ':'); |
| 105 | if (p != NULL) { |
| 106 | nc_out_port = simple_strtoul(p + 1, NULL, 10); |
| 107 | nc_in_port = nc_out_port; |
| 108 | } |
| 109 | } else |
| 110 | nc_ip = ~0; /* ncip is not set, so broadcast */ |
| 111 | |
| 112 | p = getenv("ncoutport"); |
| 113 | if (p != NULL) |
| 114 | nc_out_port = simple_strtoul(p, NULL, 10); |
| 115 | p = getenv("ncinport"); |
| 116 | if (p != NULL) |
| 117 | nc_in_port = simple_strtoul(p, NULL, 10); |
| 118 | |
| 119 | if (is_broadcast(nc_ip)) |
| 120 | /* broadcast MAC address */ |
| 121 | memset(nc_ether, 0xff, sizeof(nc_ether)); |
| 122 | else |
| 123 | /* force arp request */ |
| 124 | memset(nc_ether, 0, sizeof(nc_ether)); |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Called from NetLoop in net/net.c before each packet |
| 131 | */ |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 132 | void NcStart(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 133 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 134 | refresh_settings_from_env(); |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 135 | if (!output_packet_len || memcmp(nc_ether, NetEtherNullAddr, 6)) { |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 136 | /* going to check for input packet */ |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 137 | net_set_udp_handler(nc_handler); |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 138 | NetSetTimeout(net_timeout, nc_timeout); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 139 | } else { |
| 140 | /* send arp request */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 141 | uchar *pkt; |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 142 | net_set_arp_handler(nc_wait_arp_handler); |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 143 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 144 | memcpy(pkt, output_packet, output_packet_len); |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 145 | NetSendUDPPacket(nc_ether, nc_ip, nc_out_port, nc_in_port, |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 146 | output_packet_len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 150 | int nc_input_packet(uchar *pkt, IPaddr_t src_ip, unsigned dest_port, |
| 151 | unsigned src_port, unsigned len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 152 | { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 153 | int end, chunk; |
| 154 | |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 155 | if (dest_port != nc_in_port || !len) |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 156 | return 0; /* not for us */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 157 | |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 158 | if (src_ip != nc_ip && !is_broadcast(nc_ip)) |
| 159 | return 0; /* not from our client */ |
| 160 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 161 | debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt); |
| 162 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 163 | if (input_size == sizeof(input_buffer)) |
| 164 | return 1; /* no space */ |
| 165 | if (len > sizeof(input_buffer) - input_size) |
| 166 | len = sizeof(input_buffer) - input_size; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 167 | |
| 168 | end = input_offset + input_size; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 169 | if (end > sizeof(input_buffer)) |
| 170 | end -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 171 | |
| 172 | chunk = len; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 173 | if (end + len > sizeof(input_buffer)) { |
| 174 | chunk = sizeof(input_buffer) - end; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 175 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 176 | } |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 177 | memcpy(input_buffer + end, pkt, chunk); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 178 | |
| 179 | input_size += len; |
| 180 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 184 | static void nc_send_packet(const char *buf, int len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 185 | { |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 186 | struct eth_device *eth; |
| 187 | int inited = 0; |
| 188 | uchar *pkt; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 189 | uchar *ether; |
| 190 | IPaddr_t ip; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 191 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 192 | debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf); |
| 193 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 194 | eth = eth_get_dev(); |
| 195 | if (eth == NULL) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 196 | return; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 197 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 198 | if (!memcmp(nc_ether, NetEtherNullAddr, 6)) { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 199 | if (eth->state == ETH_STATE_ACTIVE) |
| 200 | return; /* inside net loop */ |
| 201 | output_packet = buf; |
| 202 | output_packet_len = len; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 203 | NetLoop(NETCONS); /* wait for arp reply and send packet */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 204 | output_packet_len = 0; |
| 205 | return; |
| 206 | } |
| 207 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 208 | if (eth->state != ETH_STATE_ACTIVE) { |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 209 | if (eth_is_on_demand_init()) { |
| 210 | if (eth_init(gd->bd) < 0) |
| 211 | return; |
| 212 | eth_set_last_protocol(NETCONS); |
| 213 | } else |
| 214 | eth_init_state_only(gd->bd); |
| 215 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 216 | inited = 1; |
| 217 | } |
Joe Hershberger | 594c26f | 2012-05-23 07:58:04 +0000 | [diff] [blame] | 218 | pkt = (uchar *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 219 | memcpy(pkt, buf, len); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 220 | ether = nc_ether; |
| 221 | ip = nc_ip; |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 222 | NetSendUDPPacket(ether, ip, nc_out_port, nc_in_port, len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 223 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 224 | if (inited) { |
| 225 | if (eth_is_on_demand_init()) |
| 226 | eth_halt(); |
| 227 | else |
| 228 | eth_halt_state_only(); |
| 229 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Mike Frysinger | fa2744d | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 232 | static int nc_start(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 233 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 234 | int retval; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 235 | |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 236 | nc_out_port = 6666; /* default port */ |
| 237 | nc_in_port = nc_out_port; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 238 | |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 239 | retval = refresh_settings_from_env(); |
| 240 | if (retval != 0) |
| 241 | return retval; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 242 | |
Joe Hershberger | d7310c7 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 243 | /* |
| 244 | * Initialize the static IP settings and buffer pointers |
| 245 | * incase we call NetSendUDPPacket before NetLoop |
| 246 | */ |
| 247 | net_init(); |
| 248 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 249 | return 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Mike Frysinger | fa2744d | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 252 | static void nc_putc(char c) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 253 | { |
| 254 | if (output_recursion) |
| 255 | return; |
| 256 | output_recursion = 1; |
| 257 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 258 | nc_send_packet(&c, 1); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 259 | |
| 260 | output_recursion = 0; |
| 261 | } |
| 262 | |
Mike Frysinger | fa2744d | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 263 | static void nc_puts(const char *s) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 264 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 265 | int len; |
| 266 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 267 | if (output_recursion) |
| 268 | return; |
| 269 | output_recursion = 1; |
| 270 | |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 271 | len = strlen(s); |
| 272 | while (len) { |
Joe Hershberger | 2c8fe51 | 2012-07-31 06:09:17 +0000 | [diff] [blame] | 273 | int send_len = min(len, sizeof(input_buffer)); |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 274 | nc_send_packet(s, send_len); |
| 275 | len -= send_len; |
| 276 | s += send_len; |
| 277 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 278 | |
| 279 | output_recursion = 0; |
| 280 | } |
| 281 | |
Mike Frysinger | fa2744d | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 282 | static int nc_getc(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 283 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 284 | uchar c; |
| 285 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 286 | input_recursion = 1; |
| 287 | |
| 288 | net_timeout = 0; /* no timeout */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 289 | while (!input_size) |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 290 | NetLoop(NETCONS); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 291 | |
| 292 | input_recursion = 0; |
| 293 | |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 294 | c = input_buffer[input_offset++]; |
| 295 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 296 | if (input_offset >= sizeof(input_buffer)) |
| 297 | input_offset -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 298 | input_size--; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 299 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 300 | return c; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Mike Frysinger | fa2744d | 2009-12-21 14:19:12 -0500 | [diff] [blame] | 303 | static int nc_tstc(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 304 | { |
| 305 | struct eth_device *eth; |
| 306 | |
| 307 | if (input_recursion) |
| 308 | return 0; |
| 309 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 310 | if (input_size) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 311 | return 1; |
| 312 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 313 | eth = eth_get_dev(); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 314 | if (eth && eth->state == ETH_STATE_ACTIVE) |
| 315 | return 0; /* inside net loop */ |
| 316 | |
| 317 | input_recursion = 1; |
| 318 | |
| 319 | net_timeout = 1; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 320 | NetLoop(NETCONS); /* kind of poll */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 321 | |
| 322 | input_recursion = 0; |
| 323 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 324 | return input_size != 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 327 | int drv_nc_init(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 328 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 329 | struct stdio_dev dev; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 330 | int rc; |
| 331 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 332 | memset(&dev, 0, sizeof(dev)); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 333 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 334 | strcpy(dev.name, "nc"); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 335 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 336 | dev.start = nc_start; |
| 337 | dev.putc = nc_putc; |
| 338 | dev.puts = nc_puts; |
| 339 | dev.getc = nc_getc; |
| 340 | dev.tstc = nc_tstc; |
| 341 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 342 | rc = stdio_register(&dev); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 343 | |
| 344 | return (rc == 0) ? 1 : rc; |
| 345 | } |