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 #ifdef CONFIG_NETCONSOLE
32 DECLARE_GLOBAL_DATA_PTR
;
34 static char input_buffer
[512];
35 static int input_size
= 0; /* char count in input buffer */
36 static int input_offset
= 0; /* offset to valid chars in input buffer */
37 static int input_recursion
= 0;
38 static int output_recursion
= 0;
39 static int net_timeout
;
40 static uchar nc_ether
[6]; /* server enet address */
41 static IPaddr_t nc_ip
; /* server ip */
42 static short nc_port
; /* source/target port */
43 static const char *output_packet
; /* used by first send udp */
44 static int output_packet_len
= 0;
46 static void nc_wait_arp_handler (uchar
* pkt
, unsigned dest
, unsigned src
,
49 NetState
= NETLOOP_SUCCESS
; /* got arp reply - quit net loop */
52 static void nc_handler (uchar
* pkt
, unsigned dest
, unsigned src
,
56 NetState
= NETLOOP_SUCCESS
; /* got input - quit net loop */
59 static void nc_timeout (void)
61 NetState
= NETLOOP_SUCCESS
;
66 if (!output_packet_len
|| memcmp (nc_ether
, NetEtherNullAddr
, 6)) {
67 /* going to check for input packet */
68 NetSetHandler (nc_handler
);
69 NetSetTimeout (net_timeout
, nc_timeout
);
71 /* send arp request */
73 NetSetHandler (nc_wait_arp_handler
);
74 pkt
= (uchar
*) NetTxPacket
+ NetEthHdrSize () + IP_HDR_SIZE
;
75 memcpy (pkt
, output_packet
, output_packet_len
);
76 NetSendUDPPacket (nc_ether
, nc_ip
, nc_port
, nc_port
, output_packet_len
);
80 int nc_input_packet (uchar
* pkt
, unsigned dest
, unsigned src
, unsigned len
)
84 if (dest
!= nc_port
|| !len
)
85 return 0; /* not for us */
87 if (input_size
== sizeof input_buffer
)
88 return 1; /* no space */
89 if (len
> sizeof input_buffer
- input_size
)
90 len
= sizeof input_buffer
- input_size
;
92 end
= input_offset
+ input_size
;
93 if (end
> sizeof input_buffer
)
94 end
-= sizeof input_buffer
;
97 if (end
+ len
> sizeof input_buffer
) {
98 chunk
= sizeof input_buffer
- end
;
99 memcpy(input_buffer
, pkt
+ chunk
, len
- chunk
);
101 memcpy (input_buffer
+ end
, pkt
, chunk
);
108 static void nc_send_packet (const char *buf
, int len
)
110 struct eth_device
*eth
;
116 if ((eth
= eth_get_dev ()) == NULL
) {
120 if (!memcmp (nc_ether
, NetEtherNullAddr
, 6)) {
121 if (eth
->state
== ETH_STATE_ACTIVE
)
122 return; /* inside net loop */
124 output_packet_len
= len
;
125 NetLoop (NETCONS
); /* wait for arp reply and send packet */
126 output_packet_len
= 0;
130 if (eth
->state
!= ETH_STATE_ACTIVE
) {
131 if (eth_init (gd
->bd
) < 0)
135 pkt
= (uchar
*) NetTxPacket
+ NetEthHdrSize () + IP_HDR_SIZE
;
136 memcpy (pkt
, buf
, len
);
139 NetSendUDPPacket (ether
, ip
, nc_port
, nc_port
, len
);
149 nc_port
= 6666; /* default port */
151 if (getenv ("ncip")) {
154 nc_ip
= getenv_IPaddr ("ncip");
156 return -1; /* ncip is 0.0.0.0 */
157 if ((p
= strchr (getenv ("ncip"), ':')) != NULL
)
158 nc_port
= simple_strtoul (p
+ 1, NULL
, 10);
160 nc_ip
= ~0; /* ncip is not set */
162 our_ip
= getenv_IPaddr ("ipaddr");
163 netmask
= getenv_IPaddr ("netmask");
165 if (nc_ip
== ~0 || /* 255.255.255.255 */
166 ((netmask
& our_ip
) == (netmask
& nc_ip
) && /* on the same net */
167 (netmask
| nc_ip
) == ~0)) /* broadcast to our net */
168 memset (nc_ether
, 0xff, sizeof nc_ether
);
170 memset (nc_ether
, 0, sizeof nc_ether
); /* force arp request */
175 void nc_putc (char c
)
177 if (output_recursion
)
179 output_recursion
= 1;
181 nc_send_packet (&c
, 1);
183 output_recursion
= 0;
186 void nc_puts (const char *s
)
190 if (output_recursion
)
192 output_recursion
= 1;
194 if ((len
= strlen (s
)) > 512)
197 nc_send_packet (s
, len
);
199 output_recursion
= 0;
208 net_timeout
= 0; /* no timeout */
214 c
= input_buffer
[input_offset
++];
216 if (input_offset
>= sizeof input_buffer
)
217 input_offset
-= sizeof input_buffer
;
225 struct eth_device
*eth
;
233 eth
= eth_get_dev ();
234 if (eth
&& eth
->state
== ETH_STATE_ACTIVE
)
235 return 0; /* inside net loop */
240 NetLoop (NETCONS
); /* kind of poll */
244 return input_size
!= 0;
247 int drv_nc_init (void)
252 memset (&dev
, 0, sizeof (dev
));
254 strcpy (dev
.name
, "nc");
255 dev
.flags
= DEV_FLAGS_OUTPUT
| DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
256 dev
.start
= nc_start
;
262 rc
= device_register (&dev
);
264 return (rc
== 0) ? 1 : rc
;
267 #endif /* CONFIG_NETCONSOLE */