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