1 |
786 |
skrzyp |
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
eCos Support for Developing USB-ethernet Peripherals
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
Introduction
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
Introduction
|
49 |
|
|
eCos support for developing USB ethernet peripherals
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
Introduction
|
53 |
|
|
|
54 |
|
|
The eCos USB-ethernet package provides additional support for USB
|
55 |
|
|
peripherals that involve some sort of ethernet-style network. This can
|
56 |
|
|
be a traditional ethernet, or it can involve some other networking
|
57 |
|
|
technology that uses ethernet frames as a unit of transfer. It
|
58 |
|
|
provides functions to transfer ethernet frames over the USB bus,
|
59 |
|
|
handles certain control messages from the host, and optionally it can
|
60 |
|
|
provide a network device driver for use by the eCos TCP/IP stack.
|
61 |
|
|
The package comes with an example host-side device driver.
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
The USB-ethernet package is not tied to any specific hardware. It
|
65 |
|
|
requires the presence of USB hardware and a suitable device driver,
|
66 |
|
|
but not all USB peripherals involve ethernet communications. Hence the
|
67 |
|
|
configuration system cannot load the package automatically for
|
68 |
|
|
specific targets, in the way that a USB device driver or an ethernet
|
69 |
|
|
driver can be loaded automatically. Instead, the package has to be
|
70 |
|
|
added explicitly. When using the command line tools this will involve
|
71 |
|
|
an operation like the following:
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
$ ecosconfig add usbs_eth
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
Typically, this will automatically cause the USB device driver to
|
78 |
|
|
become active. Loading the USB-ethernet package automatically provides
|
79 |
|
|
functionality for initialization,
|
80 |
|
|
data transfer, and the handling of
|
81 |
|
|
control messages and state
|
82 |
|
|
changes. If the current configuration includes the eCos TCP/IP stack
|
83 |
|
|
then the network device driver
|
84 |
|
|
support will be enabled as well by default, allowing the stack to
|
85 |
|
|
exchange ethernet frames over the USB bus.
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
There is a USB standard for a class of communication devices including
|
89 |
|
|
ethernet. The package does not implement this standard, due to
|
90 |
|
|
limitations in the hardware for which the package was first developed.
|
91 |
|
|
Instead, the package uses its own
|
92 |
|
|
linkend="usbseth-protocol">protocol between USB
|
93 |
|
|
host device driver and the
|
94 |
|
|
peripheral.
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
Usage Scenarios
|
99 |
|
|
|
100 |
|
|
The USB-ethernet package can be used several different scenarios. In
|
101 |
|
|
a simple scenario, the peripheral serves only to connect the USB host
|
102 |
|
|
to a suitable network:
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
After initialization, and once the USB connection between host and
|
113 |
|
|
peripheral has been established, higher-level code needs to detect
|
114 |
|
|
packets that are intended for the host, and to forward these. This can
|
115 |
|
|
be achieved by the low-level usbs_eth_start_tx
|
116 |
|
|
function. Similarly, higher-level code needs to detect packets coming
|
117 |
|
|
from the host, using usbs_eth_start_rx, and to
|
118 |
|
|
forward these using the real network. As far as the host is concerned
|
119 |
|
|
it is connected directly to the network. In this scenario there is no
|
120 |
|
|
confusion about addresses: there is a single MAC address for the
|
121 |
|
|
host/peripheral combination, corresponding to the connection to the
|
122 |
|
|
real network, and it is this address which should be supplied during
|
123 |
|
|
initialization.
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
In a more complicated scenario, there is a TCP/IP stack running inside
|
127 |
|
|
the peripheral.
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
This involves the USB-ethernet package providing a service both to the
|
138 |
|
|
host and to the eCos TCP/IP stack. It achieves the latter by acting as
|
139 |
|
|
an eCos network device. Typically, the TCP/IP stack will be configured
|
140 |
|
|
to act as a network bridge. The USB peripheral needs to examine the
|
141 |
|
|
packets arriving over the real network. Some of these packets will be
|
142 |
|
|
intended for the host, while others will be intended for the
|
143 |
|
|
peripheral itself. To distinguish between these two scenarios, two
|
144 |
|
|
distinct MAC addresses are needed: one for the host, and one for the
|
145 |
|
|
peripheral. Similarly, packets sent by the host may have to be
|
146 |
|
|
forwarded via the real network, or they may be intended for the TCP/IP
|
147 |
|
|
stack inside the peripheral. Packets generated inside the peripheral's
|
148 |
|
|
TCP/IP stack may need to be sent via the real network or over the USB
|
149 |
|
|
bus. The network bridge software will have to take care of all these
|
150 |
|
|
possibilities. Unusually for a network bridge, one of the network
|
151 |
|
|
segments being bridged will only ever have one machine attached.
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
There are other possible usage scenarios. For example, the peripheral
|
155 |
|
|
might not be attached to a real network at all. Instead it could be
|
156 |
|
|
the USB host that acts as a network bridge, allowing a TCP/IP stack
|
157 |
|
|
inside the peripheral to communicate with the outside world. The
|
158 |
|
|
various details will depend on the exact type of peripheral being
|
159 |
|
|
developed.
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
Initializing the USB-ethernet Package
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
usbs_eth_init
|
173 |
|
|
Initializing the USB-ethernet Package
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
#include <cyg/io/usb/usbs_eth.h>
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
void usbs_eth_init
|
183 |
|
|
usbs_eth* usbeth
|
184 |
|
|
usbs_control_endpoint* ep0
|
185 |
|
|
usbs_rx_endpoint* ep1
|
186 |
|
|
usbs_tx_endpoint* ep2
|
187 |
|
|
unsigned char* mac_address
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
Description
|
193 |
|
|
|
194 |
|
|
The USB-ethernet package is not tied to any specific hardware. It
|
195 |
|
|
requires certain functionality: there must be USB-slave hardware
|
196 |
|
|
supported by a device driver; there must also be two endpoints for
|
197 |
|
|
bulk transfers between host and peripheral, one for each direction;
|
198 |
|
|
there must also be a control endpoint, although of course that is
|
199 |
|
|
implicit with any USB hardware.
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
However, USB-slave hardware may well provide more endpoints than the
|
203 |
|
|
minimum required for ethernet support. Some of those endpoints might
|
204 |
|
|
be used by other packages, while other endpoints might be used
|
205 |
|
|
directly by the application, or might not be needed for the peripheral
|
206 |
|
|
being built. There is also the possibility of a USB peripheral that
|
207 |
|
|
supports multiple configurations, with the ethernet support active in
|
208 |
|
|
only some of those configurations. The USB-ethernet package has no
|
209 |
|
|
knowledge about any of this, so it relies on higher-level code to tell
|
210 |
|
|
it which endpoints should be used and other information. This is the
|
211 |
|
|
purpose of the usbs_eth_init function.
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
The first argument identifies the specific
|
215 |
|
|
usbs_eth data structure that is affected. It
|
216 |
|
|
is expected that the vast majority of affected applications will only
|
217 |
|
|
provide a single USB-ethernet device to a single host, and the package
|
218 |
|
|
automatically provides a suitable data structure
|
219 |
|
|
usbs_eth0 to support this. If multiple
|
220 |
|
|
usbs_eth structures are needed for some
|
221 |
|
|
reason then these need to be instantiated by other code, and each one
|
222 |
|
|
needs to be initialised by a call to
|
223 |
|
|
usbs_eth_init().
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
The next three arguments identify the endpoints that should be used
|
227 |
|
|
for USB communications: a control endpoint, a receive endpoint for
|
228 |
|
|
ethernet packets coming from the host to the peripheral, and a
|
229 |
|
|
transmit endpoint for ethernet packets going in the other direction.
|
230 |
|
|
Obviously all three endpoints should be provided by the same USB
|
231 |
|
|
hardware. The USB-ethernet package assumes that it has sole access to
|
232 |
|
|
the receive and transmit endpoints, subject to the use of
|
233 |
|
|
usbs_eth_disable and
|
234 |
|
|
usbs_eth_enable control functions. The package
|
235 |
|
|
also assumes that no other code is interested in USB state changes or
|
236 |
|
|
class control messages: it installs handlers
|
237 |
|
|
usbs_eth_state_change_handler
|
238 |
|
|
and
|
239 |
|
|
usbs_eth_class_control_handler
|
240 |
|
|
in the control endpoint. If any other code does need to handle USB
|
241 |
|
|
state changes or class control messages then replacement handlers
|
242 |
|
|
should be installed after the call to
|
243 |
|
|
usbs_eth_init, and those replacements should
|
244 |
|
|
invoke the USB-ethernet ones when appropriate.
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
The final argument to usbs_eth_init specifies
|
248 |
|
|
the MAC address (or Ethernet Station Address) that should be provided
|
249 |
|
|
to the host-side device driver. Since the USB-ethernet package does not
|
250 |
|
|
interact directly with a real ethernet device it cannot obtain the MAC
|
251 |
|
|
address from any hardware. Instead, it must be supplied by higher-level
|
252 |
|
|
code. The details depend on the
|
253 |
|
|
linkend="usbseth-intro-scenarios">scenario in which the
|
254 |
|
|
USB-ethernet package is being used.
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
The call to usbs_eth_init should normally happen
|
258 |
|
|
after the enumeration data has been provided but before the underlying
|
259 |
|
|
USB device driver has been started. If the USB device were to be
|
260 |
|
|
started first then a connection between host and peripheral could be
|
261 |
|
|
established immediately, and the host-side device driver would attempt
|
262 |
|
|
to contact the USB-ethernet package for information such as the MAC
|
263 |
|
|
address.
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
int
|
267 |
|
|
main(int argc, char** argv)
|
268 |
|
|
{
|
269 |
|
|
unsigned char host_MAC[6] = { 0x40, 0x5d, 0x90, 0xa9, 0xbc, 0x02 };
|
270 |
|
|
|
271 |
|
|
usbs_sa11x0_ep0.enumeration_data = &usb_enum_data;
|
272 |
|
|
…
|
273 |
|
|
usbs_eth_init(&usbs_eth0, &usbs_sa11x0_ep0, &usbs_sa11x0_ep1, &usbs_sa11x0_ep2, host_MAC);
|
274 |
|
|
…
|
275 |
|
|
usbs_start(&usbs_sa11x0_ep0);
|
276 |
|
|
…
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
|
|
USB-ethernet Data Transfers
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
USB-ethernet Data Transfers
|
292 |
|
|
Exchanging ethernet packets with the USB host
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
|
|
#include <cyg/io/usb/usbs_eth.h>
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
void usbs_eth_start_rx
|
303 |
|
|
usbs_eth* usbseth
|
304 |
|
|
unsigned char* buffer
|
305 |
|
|
void (*)(usbs_eth*, void*, int) complete_fn
|
306 |
|
|
void* complete_data
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
void usbs_eth_start_tx
|
311 |
|
|
usbs_eth* usbseth
|
312 |
|
|
unsigned char* buffer
|
313 |
|
|
void (*)(usbs_eth*, void*, int) complete_fn
|
314 |
|
|
void* complete_data
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
Description
|
321 |
|
|
|
322 |
|
|
The USB-ethernet package provides two main modes of operation. In the
|
323 |
|
|
first mode it provides a network device
|
324 |
|
|
driver for use by a TCP/IP stack running inside the USB
|
325 |
|
|
peripheral. All incoming ethernet packets should be passed up the
|
326 |
|
|
TCP/IP stack, and only the stack will generate outgoing packets. Apart
|
327 |
|
|
from initialization and possibly
|
328 |
|
|
certain control operations,
|
329 |
|
|
higher-level code will not interact with the USB-ethernet package
|
330 |
|
|
directly.
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
In the second mode there is no TCP/IP stack running inside the USB
|
334 |
|
|
peripheral. For example, a simple USB-ethernet converter has an
|
335 |
|
|
ethernet chip and a USB port: ethernet packets received by the
|
336 |
|
|
ethernet chip need to be forwarded to the USB host, and ethernet
|
337 |
|
|
packets sent by the USB host need to be sent out of the ethernet chip.
|
338 |
|
|
usbs_eth_start_rx and
|
339 |
|
|
usbs_eth_start_tx allow for this lower-level
|
340 |
|
|
access to the USB-ethernet package.
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
The two modes of operation are mutually exclusive. If the network
|
344 |
|
|
device driver mode is enabled then application code should communicate
|
345 |
|
|
at the TCP/IP level, and not by using the lower-level functions.
|
346 |
|
|
Instead, it is the network device driver that will make use of these
|
347 |
|
|
functions, and it assumes that it has exclusive access. The package
|
348 |
|
|
does not perform any locking.
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
The transmit and receive functions work in much the same way. The
|
352 |
|
|
first argument identifies the usbs_eth
|
353 |
|
|
structure that should be used. For the majority of applications this
|
354 |
|
|
will be usbs_eth0. The second argument specifies
|
355 |
|
|
the location of the ethernet packet; outgoing for
|
356 |
|
|
usbs_eth_start_tx and incoming for
|
357 |
|
|
usbs_eth_start_rx. This buffer should correspond
|
358 |
|
|
to the protocol:
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
Outgoing packets can consist of up to 1516 bytes, consisting of a
|
364 |
|
|
two-byte header specific to USB-ethernet followed by a standard
|
365 |
|
|
ethernet frame (a header with 6-byte destination address, 6-byte
|
366 |
|
|
source address and a further two bytes, followed by a payload of
|
367 |
|
|
up to 1500 bytes). The two-byte USB-ethernet header consists simply of
|
368 |
|
|
the size of the ethernet frame, i.e. the size of the rest of the
|
369 |
|
|
packet not including the USB-ethernet header, with the least
|
370 |
|
|
significant byte first.
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
For incoming packets the supplied buffer should usually be at least
|
376 |
|
|
1516 bytes. There may be special circumstances in which a smaller
|
377 |
|
|
buffer might be safe; for example, if the host-side device driver is
|
378 |
|
|
modified to support only smaller packets. Once the packet has been
|
379 |
|
|
received the buffer will contain a two-byte header specific to
|
380 |
|
|
USB-ethernet, followed by a normal ethernet frame. The header
|
381 |
|
|
gives the size of the ethernet frame, excluding the header, with the
|
382 |
|
|
least significant byte first.
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
Both usbs_eth_start_tx and
|
388 |
|
|
usbs_eth_start_rx are asynchronous: the transfer
|
389 |
|
|
is started and, some time later, a completion function will be invoked.
|
390 |
|
|
The third and fourth arguments to both
|
391 |
|
|
usbs_eth_start_tx and
|
392 |
|
|
usbs_eth_start_rx supply the completion function
|
393 |
|
|
and an argument to that function respectively. The completion function
|
394 |
|
|
will be invoked with three arguments: a pointer to the
|
395 |
|
|
usbs_eth data structure, usually
|
396 |
|
|
usbs_eth0; the supplied completion data ; and a
|
397 |
|
|
return code field. A negative value indicates that an error occurred,
|
398 |
|
|
for example -EPIPE if the connection between USB
|
399 |
|
|
host and peripheral has been broken, or -EAGAIN if
|
400 |
|
|
an endpoint has been halted. A positive value indicates the total size
|
401 |
|
|
of the transfer, which should correspond to the size in the
|
402 |
|
|
USB-ethernet header plus an additional two bytes for the header
|
403 |
|
|
itself.
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
If the data transfer is succesful then the completion function will
|
407 |
|
|
typically be invoked in DSR context rather than in thread context,
|
408 |
|
|
although this depends on the implementation of the underlying USB
|
409 |
|
|
device driver. Therefore the completion function is restricted in what
|
410 |
|
|
it can do; in particular, it must not make any calls that will or may
|
411 |
|
|
block such as locking a mutex or allocating memory. The kernel
|
412 |
|
|
documentation should be consulted for more details of DSR's and
|
413 |
|
|
interrupt handling generally. Note that if the transfer finishes
|
414 |
|
|
quickly then the completion function may be invoked before
|
415 |
|
|
usbs_eth_start_rx or
|
416 |
|
|
usbs_eth_start_tx returns. This is especially
|
417 |
|
|
likely to happen if the current thread is descheduled after starting
|
418 |
|
|
the data transfer but before returning from these functions.
|
419 |
|
|
|
420 |
|
|
|
421 |
|
|
For transmit operations, it is possible for
|
422 |
|
|
usbs_eth_start_tx to invoke the completion
|
423 |
|
|
function immediately. If there is no current connection between host
|
424 |
|
|
and target then the transmit will fail immediately with
|
425 |
|
|
-EPIPE. In addition the USB-ethernet package will
|
426 |
|
|
check the destination MAC address and make sure that the ethernet
|
427 |
|
|
frame really is intended for the host: either it must be for the
|
428 |
|
|
address specified in the initialization call
|
429 |
|
|
linkend="usbseth-init">usbs_eth_init, or
|
430 |
|
|
it must be a broadcast packet, or the host must have enabled
|
431 |
|
|
promiscuous mode.
|
432 |
|
|
|
433 |
|
|
|
434 |
|
|
|
435 |
|
|
|
436 |
|
|
|
437 |
|
|
|
438 |
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|
|
|
442 |
|
|
USB-ethernet State Handling
|
443 |
|
|
|
444 |
|
|
|
445 |
|
|
USB-ethernet State Handling
|
446 |
|
|
Maintaining the USB-ethernet connection with the host
|
447 |
|
|
|
448 |
|
|
|
449 |
|
|
|
450 |
|
|
|
451 |
|
|
|
452 |
|
|
#include <cyg/io/usb/usbs_eth.h>
|
453 |
|
|
|
454 |
|
|
|
455 |
|
|
|
456 |
|
|
usbs_control_return usbs_eth_class_control_handler
|
457 |
|
|
usbs_control_endpoint* ep0
|
458 |
|
|
void* callback_data
|
459 |
|
|
|
460 |
|
|
|
461 |
|
|
|
462 |
|
|
void usbs_eth_state_change_handler
|
463 |
|
|
usbs_control_endpoint* ep0
|
464 |
|
|
void* callback_data
|
465 |
|
|
usbs_state_change change
|
466 |
|
|
int old_state
|
467 |
|
|
|
468 |
|
|
|
469 |
|
|
|
470 |
|
|
void usbs_eth_disable
|
471 |
|
|
usbs_eth* usbseth>
|
472 |
|
|
|
473 |
|
|
|
474 |
|
|
void usbs_eth_enable
|
475 |
|
|
usbs_eth* usbseth>
|
476 |
|
|
|
477 |
|
|
|
478 |
|
|
|
479 |
|
|
|
480 |
|
|
|
481 |
|
|
Description
|
482 |
|
|
|
483 |
|
|
When the USB-ethernet package is initialized by a call to
|
484 |
|
|
linkend="usbseth-init">usbs_eth_init it
|
485 |
|
|
installs usbs_eth_state_change_handler to handle
|
486 |
|
|
USB state changes. This allows the package to detect when the
|
487 |
|
|
connection between the host and the peripheral is established or
|
488 |
|
|
broken, resulting in internal calls to
|
489 |
|
|
usbs_eth_enable and
|
490 |
|
|
usbs_eth_disable respectively. This is
|
491 |
|
|
appropriate if no other code needs to access the USB device. However,
|
492 |
|
|
if there is other code, either other USB-related packages or the
|
493 |
|
|
application itself, that needs to perform I/O over the USB bus, then
|
494 |
|
|
typically the USB-ethernet package should not have exclusive access to
|
495 |
|
|
state change events. Instead, the assumption is that higher-level
|
496 |
|
|
code, typically provided by the application, will install an
|
497 |
|
|
alternative state change handler in the control endpoint data
|
498 |
|
|
structure after the call to usbs_eth_init. This
|
499 |
|
|
alternative handler will either chain into
|
500 |
|
|
usbs_eth_state_change_handler when appropriate,
|
501 |
|
|
or else it will invoke usbs_eth_enable and
|
502 |
|
|
usbs_eth_disable directly. For further details of
|
503 |
|
|
state change handlers and control endpoints generally, see the
|
504 |
|
|
documentation for the common USB-slave package.
|
505 |
|
|
|
506 |
|
|
|
507 |
|
|
Similarly, usbs_eth_init will install
|
508 |
|
|
usbs_eth_class_control_handler in the control
|
509 |
|
|
endpoint data structure as the appropriate handler for class-specific
|
510 |
|
|
USB control messages. This code will handle the ethernet-specific
|
511 |
|
|
control messages , for example
|
512 |
|
|
requests by the host to enable or disable promiscuous mode or to
|
513 |
|
|
obtain the MAC address. If the USB device is not shared with any other
|
514 |
|
|
code then this is both necessary and sufficient. However, if other code
|
515 |
|
|
is involved and if that code also needs to process certain control
|
516 |
|
|
messages, higher-level code should install its own handler and chain
|
517 |
|
|
to the USB-ethernet one when appropriate. It should be noted that the
|
518 |
|
|
request code is encoded in just a single byte, so there is a real
|
519 |
|
|
possibility that exactly the same number will be used by different
|
520 |
|
|
protocols for different requests. Any such problems will have to be
|
521 |
|
|
identified and resolved by application developers, and may involve
|
522 |
|
|
modifying the source code for the USB-ethernet package.
|
523 |
|
|
|
524 |
|
|
|
525 |
|
|
As an alternative to chaining the state change handler, higher-level
|
526 |
|
|
code can instead call usbs_eth_disable and
|
527 |
|
|
usbs_eth_enable directly. These functions may
|
528 |
|
|
also be called if the USB-ethernet package should become inactive for
|
529 |
|
|
reasons not related directly to events on the USB bus. The main effect
|
530 |
|
|
of usbs_eth_enable is to restart receive
|
531 |
|
|
operations and to allow transmits. The main effect of
|
532 |
|
|
usbs_eth_disable is to block further transmits:
|
533 |
|
|
any current receive operations need to be aborted at the USB level,
|
534 |
|
|
for example by halting the appropriate endpoint.
|
535 |
|
|
|
536 |
|
|
|
537 |
|
|
|
538 |
|
|
|
539 |
|
|
|
540 |
|
|
|
541 |
|
|
|
542 |
|
|
|
543 |
|
|
|
544 |
|
|
|
545 |
|
|
Network Device for the eCos TCP/IP Stack
|
546 |
|
|
|
547 |
|
|
|
548 |
|
|
Network Device
|
549 |
|
|
USB-ethernet support for the eCos TCP/IP Stack
|
550 |
|
|
|
551 |
|
|
|
552 |
|
|
Description
|
553 |
|
|
|
554 |
|
|
If the USB peripheral involves running the eCos TCP/IP stack and that
|
555 |
|
|
stack needs to use USB-ethernet as a transport layer (or as one of the
|
556 |
|
|
transports), then the USB-ethernet package can provide a suitable
|
557 |
|
|
network device driver. It is still necessary for higher-level code to
|
558 |
|
|
perform appropriate initialization by calling
|
559 |
|
|
linkend="usbseth-init">usbs_eth_init, but
|
560 |
|
|
after that it will be the TCP/IP stack rather than application code
|
561 |
|
|
that transmits or receives ethernet frames.
|
562 |
|
|
|
563 |
|
|
|
564 |
|
|
Not all peripherals involving the USB-ethernet package will require a
|
565 |
|
|
TCP/IP stack. Hence the provision of the network device is controlled
|
566 |
|
|
by a configuration option CYGPKG_USBS_ETHDRV. By
|
567 |
|
|
default this will be enabled if the TCP/IP package
|
568 |
|
|
CYGPKG_NET is loaded, and disabled otherwise.
|
569 |
|
|
|
570 |
|
|
|
571 |
|
|
There are a number of other configuration options related to the
|
572 |
|
|
network device. CYGFUN_USBS_ETHDRV_STATISTICS
|
573 |
|
|
determines whether or not the package will maintain statistics, mainly
|
574 |
|
|
intended for SNMP: by default this will be enabled if the SNMP support
|
575 |
|
|
package CYGPKG_SNMPAGENT is loaded, and disabled
|
576 |
|
|
otherwise. The name of the ethernet device is controlled by
|
577 |
|
|
CYGDATA_USBS_ETHDRV_NAME, and has a default value
|
578 |
|
|
of either eth0 or eth1
|
579 |
|
|
depending on whether or not there is another network device driver
|
580 |
|
|
present in the configuration.
|
581 |
|
|
|
582 |
|
|
|
583 |
|
|
Usually eCos network device drivers default to using DHCP for
|
584 |
|
|
obtaining necessary information such as IP addresses. This is not
|
585 |
|
|
appropriate for USB-ethernet devices. On the host-side the
|
586 |
|
|
USB-ethernet network device will not exist until the USB peripheral
|
587 |
|
|
has been plugged in and communication has been established. Therefore
|
588 |
|
|
any DHCP daemon on the host would not be listening on that network
|
589 |
|
|
device at the point that eCos requests its IP and other information. A
|
590 |
|
|
related issue is that the use of DHCP would imply the presence of a
|
591 |
|
|
DHCP daemon on every affected host machine, as opposed to a single
|
592 |
|
|
daemon (plus backups) for the network as a whole. For these reasons
|
593 |
|
|
the USB-ethernet package precludes the use of DHCP as a way of setting
|
594 |
|
|
the IP address, instead requiring alternatives such as manual
|
595 |
|
|
configuration.
|
596 |
|
|
|
597 |
|
|
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
|
601 |
|
|
|
602 |
|
|
|
603 |
|
|
|
604 |
|
|
|
605 |
|
|
|
606 |
|
|
Example Host-side Device Driver
|
607 |
|
|
|
608 |
|
|
|
609 |
|
|
Example Host-side Device Driver
|
610 |
|
|
Provide host-side support for the eCos USB-ethernet package
|
611 |
|
|
|
612 |
|
|
|
613 |
|
|
Description
|
614 |
|
|
|
615 |
|
|
The USB-ethernet package is supplied with a single host-side device
|
616 |
|
|
driver. This driver has been developed against the Linux kernel
|
617 |
|
|
2.2.16-22, as shipped with Red Hat 7. The driver is provided as is and
|
618 |
|
|
should not be considered production quality: for example it only
|
619 |
|
|
checks for a bogus vendor id 0x4242 rather than an
|
620 |
|
|
official vendor id supplied by the
|
621 |
|
|
url="http://www.usb.org/">USB Implementers Forum. Also, if the
|
622 |
|
|
peripheral involves multiple configurations or multiple interfaces, it
|
623 |
|
|
will fail to detect this. However, the driver can be used for simple
|
624 |
|
|
testing and as the basis of a full device driver. Details of the
|
625 |
|
|
protocol used between host and peripheral can be found in the
|
626 |
|
|
linkend="usbseth-protocol">Communication Protocol section.
|
627 |
|
|
|
628 |
|
|
|
629 |
|
|
The host-side device driver can be found in the
|
630 |
|
|
class="directory">host subdirectory of the USB-ethernet
|
631 |
|
|
package, specifically the file ecos_usbeth.c, and
|
632 |
|
|
comes with a Makefile. Both files may need
|
633 |
|
|
to be modified for specific applications. For example, the vendor id
|
634 |
|
|
table ecos_usbeth_implementations may need to be
|
635 |
|
|
updated for the specific USB peripheral being built. The
|
636 |
|
|
Makefile assumes that the Linux kernel sources
|
637 |
|
|
reside in /usr/src/linux, and
|
638 |
|
|
that the kernel has already been configured and built. Assuming this
|
639 |
|
|
is the case, the device driver can be built simply by invoking
|
640 |
|
|
make with no additional arguments. This will result
|
641 |
|
|
in a dynamically loadable kernel module,
|
642 |
|
|
ecos_usbeth.o, in the current directory.
|
643 |
|
|
|
644 |
|
|
|
645 |
|
|
|
646 |
|
|
As normal for Linux kernel builds, the generated files such as
|
647 |
|
|
ecos_usbeth.o live in the same directory as the
|
648 |
|
|
source tree. This is very different from eCos where the source tree
|
649 |
|
|
(or component repository) is kept separate from any builds. There may
|
650 |
|
|
be problems if the component repository is kept read-only or if it is
|
651 |
|
|
put under source code control. Any such problems can be avoided by
|
652 |
|
|
making a copy of the host
|
653 |
|
|
subdirectory and building that copy.
|
654 |
|
|
|
655 |
|
|
|
656 |
|
|
|
657 |
|
|
Loading the kernel module into the current system requires root
|
658 |
|
|
privileges. If the generic USB support is also a loadable module and
|
659 |
|
|
has not been loaded already, this must happen first:
|
660 |
|
|
|
661 |
|
|
|
662 |
|
|
# insmod usb-uhci
|
663 |
|
|
Using /lib/modules/2.2.16-22/usb/usb-uhci.o
|
664 |
|
|
|
665 |
|
|
|
666 |
|
|
Depending on the host hardware, the uhci or
|
667 |
|
|
usb-ohci modules may be more appropriate. Loading
|
668 |
|
|
the generic USB module will typically result in a number of messages
|
669 |
|
|
to the logfile /var/log/messages, giving details
|
670 |
|
|
of the specific host-side hardware that has been detected plus any
|
671 |
|
|
hubs. The next step is to load the USB-ethernet module:
|
672 |
|
|
|
673 |
|
|
|
674 |
|
|
# insmod ecos_usbeth.o
|
675 |
|
|
|
676 |
|
|
|
677 |
|
|
This should result in a number of additional diagnostics in the
|
678 |
|
|
logfile:
|
679 |
|
|
|
680 |
|
|
|
681 |
|
|
Apr 1 18:01:08 grumpy kernel: eCos USB-ethernet device driver
|
682 |
|
|
Apr 1 18:01:08 grumpy kernel: usb.c: registered new driver ecos_usbeth
|
683 |
|
|
|
684 |
|
|
|
685 |
|
|
If a suitable USB peripheral is now connected the host will detect
|
686 |
|
|
this, assign an address in the local USB network, obtain enumeration
|
687 |
|
|
data, and find a suitable device driver. Assuming the peripheral and
|
688 |
|
|
device driver agree on the supported vendor ids, the
|
689 |
|
|
ecos_usbeth.o module will be selected and this
|
690 |
|
|
will be reported in the system log:
|
691 |
|
|
|
692 |
|
|
|
693 |
|
|
Apr 1 18:04:12 grumpy kernel: usb.c: USB new device connect, assigned device number 3
|
694 |
|
|
Apr 1 18:04:12 grumpy kernel: eCos-based USB ethernet peripheral active at eth1
|
695 |
|
|
|
696 |
|
|
|
697 |
|
|
What can happen next depends very much on the software that is running
|
698 |
|
|
on top of the USB-ethernet package inside the peripheral. For example,
|
699 |
|
|
if there is a TCP/IP stack then it should be possible to bring up a
|
700 |
|
|
network connection between host and peripheral using
|
701 |
|
|
ifconfig.
|
702 |
|
|
|
703 |
|
|
|
704 |
|
|
|
705 |
|
|
|
706 |
|
|
|
707 |
|
|
|
708 |
|
|
|
709 |
|
|
|
710 |
|
|
|
711 |
|
|
|
712 |
|
|
Communication Protocol
|
713 |
|
|
|
714 |
|
|
|
715 |
|
|
Communication Protocol
|
716 |
|
|
Protocol used between the host-side device driver and the eCos
|
717 |
|
|
USB-ethernet package
|
718 |
|
|
|
719 |
|
|
|
720 |
|
|
Description
|
721 |
|
|
|
722 |
|
|
There is a USB standard for the protocol to be used between the host
|
723 |
|
|
and a class of communication devices, including ethernet. However, the
|
724 |
|
|
eCos USB-ethernet package does not implement this protocol: the target
|
725 |
|
|
hardware for which the package was first developed had certain
|
726 |
|
|
limitations, and could not implement the standard. Instead, the package
|
727 |
|
|
implements a simple new protocol.
|
728 |
|
|
|
729 |
|
|
|
730 |
|
|
A USB-ethernet peripheral involves bulk transfers on two endpoints:
|
731 |
|
|
one endpoint will be used for packets from host to peripheral and the
|
732 |
|
|
other will be used for the opposite direction. Transfers in both
|
733 |
|
|
directions are variable length, with a lower limit of 16 bytes and an
|
734 |
|
|
upper limit of 1516 bytes. The first two bytes of each transfer
|
735 |
|
|
constitute a header specific to USB-ethernet. The next 14 bytes form
|
736 |
|
|
the normal header for an ethernet frame: destination MAC address,
|
737 |
|
|
source MAC address, and a protocol field. The remaining data, up to
|
738 |
|
|
1500 bytes, are the payload. The first two bytes give the size of the
|
739 |
|
|
ethernet frame, least significant byte first, with a value between 14
|
740 |
|
|
and 1514.
|
741 |
|
|
|
742 |
|
|
|
743 |
|
|
For example an ARP request from host to peripheral involves an
|
744 |
|
|
ethernet frame of 42 bytes (0x002A), with the usual 14-byte header and
|
745 |
|
|
a 28-byte payload. The destination is the broadcast address
|
746 |
|
|
0xFFFFFFFFFFFF. The source depends on the MAC address specified for
|
747 |
|
|
the host in the call to
|
748 |
|
|
linkend="usbseth-init">usbs_eth_init, e.g.
|
749 |
|
|
0x405D90A9BC02. The remaining data is as specified by the appropriate
|
750 |
|
|
IETF RFC's. The actual bulk
|
751 |
|
|
USB transfer involves the following sequence of 44 bytes:
|
752 |
|
|
|
753 |
|
|
|
754 |
|
|
2a 00 ff ff ff ff ff ff 40 5d 90 a9 bc 02 08 06
|
755 |
|
|
00 01 08 00 06 04 00 01 40 5d 90 a9 bc 02 0a 00
|
756 |
|
|
00 01 00 00 00 00 00 00 0a 00 00 02
|
757 |
|
|
|
758 |
|
|
|
759 |
|
|
In addition there are two control messages. These will be sent by the
|
760 |
|
|
host to endpoint 0, the control endpoint, and by default they will
|
761 |
|
|
be handled by
|
762 |
|
|
usbs_eth_class_control_handler. If class-specific
|
763 |
|
|
control messages are intercepted by other code then it is the
|
764 |
|
|
responsibility of that code to invoke the USB-ethernet handler when
|
765 |
|
|
appropriate.
|
766 |
|
|
|
767 |
|
|
|
768 |
|
|
The first control message can be used by the host to obtain a MAC
|
769 |
|
|
address:
|
770 |
|
|
|
771 |
|
|
|
772 |
|
|
#define ECOS_USBETH_CONTROL_GET_MAC_ADDRESS 0x01
|
773 |
|
|
|
774 |
|
|
|
775 |
|
|
The control message's type field should specify IN as the direction.
|
776 |
|
|
The request field should be 0x01. The length fields
|
777 |
|
|
should specify a size of 6 bytes. The remaining fields of the control
|
778 |
|
|
message will be ignored by the USB-ethernet package. The response
|
779 |
|
|
consists of the 6-byte MAC address supplied by the initialization call
|
780 |
|
|
|
781 |
|
|
linkend="usbseth-init">usbs_eth_init.
|
782 |
|
|
|
783 |
|
|
|
784 |
|
|
The second control message can be used by the host to enable or
|
785 |
|
|
disable promiscuous mode.
|
786 |
|
|
|
787 |
|
|
|
788 |
|
|
#define ECOS_USBETH_CONTROL_SET_PROMISCUOUS_MODE 0x02
|
789 |
|
|
|
790 |
|
|
|
791 |
|
|
This control message involves no further data so the length field
|
792 |
|
|
should be set to 0. The value field should be non-zero to enable
|
793 |
|
|
promiscuous mode, zero to disable it. The request field should be
|
794 |
|
|
0x02. The remaining fields in the control message
|
795 |
|
|
will be ignored. It is the responsibility of the host-side device
|
796 |
|
|
driver to keep track of whether or not promiscuous mode is currently
|
797 |
|
|
enabled. It will be disabled when the peripheral changes to
|
798 |
|
|
Configured state, typically at the point where the host-side device
|
799 |
|
|
driver has been activated.
|
800 |
|
|
|
801 |
|
|
|
802 |
|
|
|
803 |
|
|
|
804 |
|
|
|
805 |
|
|
|
806 |
|
|
|
807 |
|
|
|
808 |
|
|
|