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 */ |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 26 | static struct in_addr 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, |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 38 | struct in_addr 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 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 44 | static void nc_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 45 | unsigned src, unsigned len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 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 | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 51 | static void nc_timeout_handler(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 | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 56 | static int is_broadcast(struct in_addr ip) |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 57 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 58 | static struct in_addr netmask; |
| 59 | static struct in_addr our_ip; |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 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) { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 65 | netmask = getenv_ip("netmask"); |
| 66 | our_ip = getenv_ip("ipaddr"); |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 67 | |
| 68 | env_changed_id = env_id; |
| 69 | } |
| 70 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 71 | return (ip.s_addr == ~0 || /* 255.255.255.255 (global bcast) */ |
| 72 | ((netmask.s_addr & our_ip.s_addr) == |
| 73 | (netmask.s_addr & ip.s_addr) && /* on the same net and */ |
| 74 | (netmask.s_addr | ip.s_addr) == ~0)); /* bcast to our net */ |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int refresh_settings_from_env(void) |
| 78 | { |
| 79 | const char *p; |
| 80 | static int env_changed_id; |
| 81 | int env_id = get_env_id(); |
| 82 | |
| 83 | /* update only when the environment has changed */ |
| 84 | if (env_changed_id != env_id) { |
| 85 | if (getenv("ncip")) { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 86 | nc_ip = getenv_ip("ncip"); |
| 87 | if (!nc_ip.s_addr) |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 88 | return -1; /* ncip is 0.0.0.0 */ |
| 89 | p = strchr(getenv("ncip"), ':'); |
| 90 | if (p != NULL) { |
| 91 | nc_out_port = simple_strtoul(p + 1, NULL, 10); |
| 92 | nc_in_port = nc_out_port; |
| 93 | } |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 94 | } else { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 95 | nc_ip.s_addr = ~0; /* ncip is not set, so broadcast */ |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 96 | } |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 97 | |
| 98 | p = getenv("ncoutport"); |
| 99 | if (p != NULL) |
| 100 | nc_out_port = simple_strtoul(p, NULL, 10); |
| 101 | p = getenv("ncinport"); |
| 102 | if (p != NULL) |
| 103 | nc_in_port = simple_strtoul(p, NULL, 10); |
| 104 | |
| 105 | if (is_broadcast(nc_ip)) |
| 106 | /* broadcast MAC address */ |
| 107 | memset(nc_ether, 0xff, sizeof(nc_ether)); |
| 108 | else |
| 109 | /* force arp request */ |
| 110 | memset(nc_ether, 0, sizeof(nc_ether)); |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /** |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 116 | * Called from net_loop in net/net.c before each packet |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 117 | */ |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 118 | void nc_start(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 119 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 120 | refresh_settings_from_env(); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 121 | if (!output_packet_len || memcmp(nc_ether, net_null_ethaddr, 6)) { |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 122 | /* going to check for input packet */ |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 123 | net_set_udp_handler(nc_handler); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 124 | net_set_timeout_handler(net_timeout, nc_timeout_handler); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 125 | } else { |
| 126 | /* send arp request */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 127 | uchar *pkt; |
Joe Hershberger | ece223b | 2012-05-23 07:59:15 +0000 | [diff] [blame] | 128 | net_set_arp_handler(nc_wait_arp_handler); |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 129 | pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + |
| 130 | IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 131 | memcpy(pkt, output_packet, output_packet_len); |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 132 | net_send_udp_packet(nc_ether, nc_ip, nc_out_port, nc_in_port, |
| 133 | output_packet_len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 137 | int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned dest_port, |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 138 | unsigned src_port, unsigned len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 139 | { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 140 | int end, chunk; |
| 141 | |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 142 | if (dest_port != nc_in_port || !len) |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 143 | return 0; /* not for us */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 144 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 145 | if (src_ip.s_addr != nc_ip.s_addr && !is_broadcast(nc_ip)) |
Joe Hershberger | 8a0eccb | 2012-09-18 10:01:32 +0000 | [diff] [blame] | 146 | return 0; /* not from our client */ |
| 147 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 148 | debug_cond(DEBUG_DEV_PKT, "input: \"%*.*s\"\n", len, len, pkt); |
| 149 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 150 | if (input_size == sizeof(input_buffer)) |
| 151 | return 1; /* no space */ |
| 152 | if (len > sizeof(input_buffer) - input_size) |
| 153 | len = sizeof(input_buffer) - input_size; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 154 | |
| 155 | end = input_offset + input_size; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 156 | if (end > sizeof(input_buffer)) |
| 157 | end -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 158 | |
| 159 | chunk = len; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 160 | if (end + len > sizeof(input_buffer)) { |
| 161 | chunk = sizeof(input_buffer) - end; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 162 | memcpy(input_buffer, pkt + chunk, len - chunk); |
| 163 | } |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 164 | memcpy(input_buffer + end, pkt, chunk); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 165 | |
| 166 | input_size += len; |
| 167 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 168 | return 1; |
| 169 | } |
| 170 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 171 | static void nc_send_packet(const char *buf, int len) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 172 | { |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 173 | struct eth_device *eth; |
| 174 | int inited = 0; |
| 175 | uchar *pkt; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 176 | uchar *ether; |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 177 | struct in_addr ip; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 178 | |
Joe Hershberger | 4ef8d53 | 2012-05-23 08:01:04 +0000 | [diff] [blame] | 179 | debug_cond(DEBUG_DEV_PKT, "output: \"%*.*s\"\n", len, len, buf); |
| 180 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 181 | eth = eth_get_dev(); |
| 182 | if (eth == NULL) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 183 | return; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 184 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 185 | if (!memcmp(nc_ether, net_null_ethaddr, 6)) { |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 186 | if (eth->state == ETH_STATE_ACTIVE) |
| 187 | return; /* inside net loop */ |
| 188 | output_packet = buf; |
| 189 | output_packet_len = len; |
Suriyan Ramasami | efd9bb9 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 190 | input_recursion = 1; |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 191 | net_loop(NETCONS); /* wait for arp reply and send packet */ |
Suriyan Ramasami | efd9bb9 | 2013-10-16 09:54:24 -0700 | [diff] [blame] | 192 | input_recursion = 0; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 193 | output_packet_len = 0; |
| 194 | return; |
| 195 | } |
| 196 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 197 | if (eth->state != ETH_STATE_ACTIVE) { |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 198 | if (eth_is_on_demand_init()) { |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 199 | if (eth_init() < 0) |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 200 | return; |
| 201 | eth_set_last_protocol(NETCONS); |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 202 | } else { |
Joe Hershberger | d2eaec6 | 2015-03-22 17:09:06 -0500 | [diff] [blame] | 203 | eth_init_state_only(); |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 204 | } |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 205 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 206 | inited = 1; |
| 207 | } |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 208 | pkt = (uchar *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 209 | memcpy(pkt, buf, len); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 210 | ether = nc_ether; |
| 211 | ip = nc_ip; |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 212 | net_send_udp_packet(ether, ip, nc_out_port, nc_in_port, len); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 213 | |
Joe Hershberger | f8be7d6 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 214 | if (inited) { |
| 215 | if (eth_is_on_demand_init()) |
| 216 | eth_halt(); |
| 217 | else |
| 218 | eth_halt_state_only(); |
| 219 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 222 | static int nc_stdio_start(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 223 | { |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 224 | int retval; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 225 | |
Joe Hershberger | 7f51898 | 2012-07-31 06:06:41 +0000 | [diff] [blame] | 226 | nc_out_port = 6666; /* default port */ |
| 227 | nc_in_port = nc_out_port; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 228 | |
Joe Hershberger | e827fec | 2012-09-18 10:01:31 +0000 | [diff] [blame] | 229 | retval = refresh_settings_from_env(); |
| 230 | if (retval != 0) |
| 231 | return retval; |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 232 | |
Joe Hershberger | d7310c7 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 233 | /* |
| 234 | * Initialize the static IP settings and buffer pointers |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 235 | * incase we call net_send_udp_packet before net_loop |
Joe Hershberger | d7310c7 | 2012-05-23 07:59:23 +0000 | [diff] [blame] | 236 | */ |
| 237 | net_init(); |
| 238 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 239 | return 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 242 | static void nc_stdio_putc(struct stdio_dev *dev, char c) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 243 | { |
| 244 | if (output_recursion) |
| 245 | return; |
| 246 | output_recursion = 1; |
| 247 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 248 | nc_send_packet(&c, 1); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 249 | |
| 250 | output_recursion = 0; |
| 251 | } |
| 252 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 253 | static void nc_stdio_puts(struct stdio_dev *dev, const char *s) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 254 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 255 | int len; |
| 256 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 257 | if (output_recursion) |
| 258 | return; |
| 259 | output_recursion = 1; |
| 260 | |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 261 | len = strlen(s); |
| 262 | while (len) { |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 263 | int send_len = min(len, (int)sizeof(input_buffer)); |
Michael Walle | 1a9845b | 2011-10-07 12:27:50 +0000 | [diff] [blame] | 264 | nc_send_packet(s, send_len); |
| 265 | len -= send_len; |
| 266 | s += send_len; |
| 267 | } |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 268 | |
| 269 | output_recursion = 0; |
| 270 | } |
| 271 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 272 | static int nc_stdio_getc(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 273 | { |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 274 | uchar c; |
| 275 | |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 276 | input_recursion = 1; |
| 277 | |
| 278 | net_timeout = 0; /* no timeout */ |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 279 | while (!input_size) |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 280 | net_loop(NETCONS); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 281 | |
| 282 | input_recursion = 0; |
| 283 | |
wdenk | b2323ea | 2005-04-20 09:28:54 +0000 | [diff] [blame] | 284 | c = input_buffer[input_offset++]; |
| 285 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 286 | if (input_offset >= sizeof(input_buffer)) |
| 287 | input_offset -= sizeof(input_buffer); |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 288 | input_size--; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 289 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 290 | return c; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 293 | static int nc_stdio_tstc(struct stdio_dev *dev) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 294 | { |
| 295 | struct eth_device *eth; |
| 296 | |
| 297 | if (input_recursion) |
| 298 | return 0; |
| 299 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 300 | if (input_size) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 301 | return 1; |
| 302 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 303 | eth = eth_get_dev(); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 304 | if (eth && eth->state == ETH_STATE_ACTIVE) |
| 305 | return 0; /* inside net loop */ |
| 306 | |
| 307 | input_recursion = 1; |
| 308 | |
| 309 | net_timeout = 1; |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 310 | net_loop(NETCONS); /* kind of poll */ |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 311 | |
| 312 | input_recursion = 0; |
| 313 | |
wdenk | eedcd07 | 2004-09-08 22:03:11 +0000 | [diff] [blame] | 314 | return input_size != 0; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 317 | int drv_nc_init(void) |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 318 | { |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 319 | struct stdio_dev dev; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 320 | int rc; |
| 321 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 322 | memset(&dev, 0, sizeof(dev)); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 323 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 324 | strcpy(dev.name, "nc"); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 325 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
Joe Hershberger | 6a38a5f | 2015-04-08 01:41:16 -0500 | [diff] [blame] | 326 | dev.start = nc_stdio_start; |
| 327 | dev.putc = nc_stdio_putc; |
| 328 | dev.puts = nc_stdio_puts; |
| 329 | dev.getc = nc_stdio_getc; |
| 330 | dev.tstc = nc_stdio_tstc; |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 331 | |
Joe Hershberger | e1902ac | 2012-05-15 08:59:13 +0000 | [diff] [blame] | 332 | rc = stdio_register(&dev); |
wdenk | 68ceb29 | 2004-08-02 21:11:11 +0000 | [diff] [blame] | 333 | |
| 334 | return (rc == 0) ? 1 : rc; |
| 335 | } |