1 |
27 |
unneback |
|
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 |
|
|
|
40 |
|
|
|
41 |
|
|
eCos USB Slave Support
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
|
Introduction
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
Introduction
|
51 |
|
|
eCos support for USB slave devices
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
Introduction
|
55 |
|
|
|
56 |
|
|
The eCos USB slave support allows developers to produce USB
|
57 |
|
|
peripherals. It consists of a number of different eCos packages:
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
Device drivers for specific implementations of USB slave hardware, for
|
64 |
|
|
example the on-chip USB Device Controller provided by the Intel SA1110
|
65 |
|
|
processor. A typical USB peripheral will only provide one USB slave
|
66 |
|
|
port and therefore only one such device driver package will be needed.
|
67 |
|
|
Usually the device driver package will be loaded automatically when
|
68 |
|
|
you create an eCos configuration for target hardware that has a USB
|
69 |
|
|
slave device. If you select a target which does have a USB slave
|
70 |
|
|
device but no USB device driver is loaded, this implies that no such
|
71 |
|
|
device driver is currently available.
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
The common USB slave package. This serves two purposes. It defines the
|
78 |
|
|
API that specific device drivers should implement. It also provides
|
79 |
|
|
various utilities that will be needed by most USB device drivers and
|
80 |
|
|
applications, such as handlers for standard control messages.
|
81 |
|
|
Usually this package will be loaded automatically at the same time as
|
82 |
|
|
the USB device driver.
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
The common USB package. This merely provides some information common
|
89 |
|
|
to both the host and slave sides of USB, such as details of the
|
90 |
|
|
control protocol. It is also used to place the other USB-related
|
91 |
|
|
packages appropriately in the overall configuration hierarchy. Usually
|
92 |
|
|
this package will be loaded at the same time as the USB device driver.
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
Class-specific USB support packages. These make it easier to develop
|
99 |
|
|
specific classes of USB peripheral, such as a USB-ethernet device. If
|
100 |
|
|
no suitable package is available for a given class of peripheral then
|
101 |
|
|
the USB device driver can instead be accessed directly from
|
102 |
|
|
application code. Such packages will never be loaded automatically
|
103 |
|
|
since the configuration system has no way of knowing what class of USB
|
104 |
|
|
peripheral is being developed. Instead developers have to add the
|
105 |
|
|
appropriate package or packages explicitly.
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
These packages only provide support for developing USB peripherals,
|
113 |
|
|
not USB hosts.
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
USB Concepts
|
118 |
|
|
|
119 |
|
|
Information about USB can be obtained from a number of sources
|
120 |
|
|
including the USB Implementers Forum
|
121 |
|
|
web site. Only a brief summary is provided here.
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
A USB network is asymmetrical: it consists of a single host, one or
|
125 |
|
|
more slave devices, and possibly some number of intermediate hubs. The
|
126 |
|
|
host side is significantly more complicated than the slave side.
|
127 |
|
|
Essentially, all operations are initiated by the host. For example, if
|
128 |
|
|
the host needs to receive some data from a particular USB peripheral
|
129 |
|
|
then it will send an IN token to that peripheral; the latter should
|
130 |
|
|
respond with either a NAK or with appropriate data. Similarly, when
|
131 |
|
|
the host wants to transmit data to a peripheral it will send an OUT
|
132 |
|
|
token followed by the data; the peripheral will return a NAK if it is
|
133 |
|
|
currently unable to receive more data or if there was corruption,
|
134 |
|
|
otherwise it will return an ACK. All transfers are check-summed and
|
135 |
|
|
there is a clearly-defined error recovery process. USB peripherals can
|
136 |
|
|
only interact with the host, not with each other.
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
USB supports four different types of communication: control messages,
|
140 |
|
|
interrupt transfers, isochronous transfers, and bulk transfers.
|
141 |
|
|
Control messages are further subdivided into four categories:
|
142 |
|
|
standard, class, vendor and a reserved category. All USB peripherals
|
143 |
|
|
must respond to certain standard control messages, and usually this
|
144 |
|
|
will be handled by the common USB slave package (for complicated
|
145 |
|
|
peripherals, application support will be needed). Class and vendor
|
146 |
|
|
control messages may be handled by an class-specific USB support
|
147 |
|
|
package, for example the USB-ethernet package will handle control
|
148 |
|
|
messages such as getting the MAC address or enabling/disabling
|
149 |
|
|
promiscuous mode. Alternatively, some or all of these messages will
|
150 |
|
|
have to be handled by application code.
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
Interrupt transfers are used for devices which need to be polled
|
154 |
|
|
regularly. For example, a USB keyboard might be polled once every
|
155 |
|
|
millisecond. The host will not poll the device more frequently than
|
156 |
|
|
this, so interrupt transfers are best suited to peripherals that
|
157 |
|
|
involve a relatively small amount of data. Isochronous transfers are
|
158 |
|
|
intended for multimedia-related peripherals where typically a large
|
159 |
|
|
amount of video or audio data needs to be exchanged continuously.
|
160 |
|
|
Given appropriate host support a USB peripheral can reserve some of
|
161 |
|
|
the available bandwidth. Isochronous transfers are not reliable; if a
|
162 |
|
|
particular packet is corrupted then it will just be discarded and
|
163 |
|
|
software is expected to recover from this. Bulk transfers are used for
|
164 |
|
|
everything else: after taking care of any pending control, isochronous
|
165 |
|
|
and interrupt transfers the host will use whatever bandwidth remains
|
166 |
|
|
for bulk transfers. Bulk transfers are reliable.
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
Transfers are organized into USB packets, with the details depending
|
170 |
|
|
on the transfer type. Control messages always involve an initial
|
171 |
|
|
8-byte packet from host to peripheral, optionally followed by some
|
172 |
|
|
additional packets; in theory these additional packets can be up to 64
|
173 |
|
|
bytes, but hardware may limit it to 8 bytes. Interrupt transfers
|
174 |
|
|
involve a single packet of up to 64 bytes. Isochronous transfers
|
175 |
|
|
involve a single packet of up to 1024 bytes. Bulk transfers involve
|
176 |
|
|
multiple packets. There will be some number, possibly zero, of 64-byte
|
177 |
|
|
packets. The transfer is terminated by a single packet of less than 64
|
178 |
|
|
bytes. If the transfer involves an exact multiple of 64 bytes than the
|
179 |
|
|
final packet will be 0 bytes, consisting of just a header and checksum
|
180 |
|
|
which typically will be generated by the hardware. There is no
|
181 |
|
|
pre-defined limit on the size of a bulk transfer. Instead higher-level
|
182 |
|
|
protocols are expected to handle this, so for a USB-ethernet
|
183 |
|
|
peripheral the protocol could impose a limit of 1514 bytes of data
|
184 |
|
|
plus maybe some additional protocol overhead.
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
Transfers from the host to a peripheral are addressed not just to that
|
188 |
|
|
peripheral but to a specific endpoint within that peripheral.
|
189 |
|
|
Similarly, the host requests incoming data from a specific endpoint
|
190 |
|
|
rather than from the peripheral as a whole. For example, a combined
|
191 |
|
|
keyboard/touchpad device could provide the keyboard events on endpoint
|
192 |
|
|
1 and the mouse events on endpoint 2. A given USB peripheral can have
|
193 |
|
|
up to 16 endpoints for incoming data and another 16 for outgoing data.
|
194 |
|
|
However, given the comparatively high speed of USB I/O this endpoint
|
195 |
|
|
addressing is typically implemented in hardware rather than software,
|
196 |
|
|
and the hardware will only implement a small number of endpoints.
|
197 |
|
|
Endpoint 0 is generally used only for control messages.
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
In practice, many of these details are irrelevant to application code
|
201 |
|
|
or to class packages. Instead, such higher-level code usually just
|
202 |
|
|
performs blocking read and
|
203 |
|
|
write, or non-blocking USB-specific calls, to
|
204 |
|
|
transfer data between host and target via a specific endpoint. Control
|
205 |
|
|
messages are more complicated but are usually handled by existing
|
206 |
|
|
code.
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
When a USB peripheral is plugged into the host there is an initial
|
210 |
|
|
enumeration and configuration process. The peripheral provides
|
211 |
|
|
information such as its class of device (audio, video, etc.), a
|
212 |
|
|
vendor id, which endpoints should be used for what kind of data, and
|
213 |
|
|
so on. The host OS uses this information to identify a suitable host
|
214 |
|
|
device driver. This could be a generic driver for a class of
|
215 |
|
|
peripherals, or it could be a vendor-specific driver. Assuming a
|
216 |
|
|
suitable driver is installed the host will then activate the USB
|
217 |
|
|
peripheral and perform additional application-specific initialisation.
|
218 |
|
|
For example for a USB-ethernet device this would involve obtaining an
|
219 |
|
|
ethernet MAC address. Most USB peripherals will be fairly simple, but
|
220 |
|
|
it is possible to build multifunction peripherals with multiple
|
221 |
|
|
configurations, interfaces, and alternate interface settings.
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
It is not possible for any of the eCos packages to generate all the
|
225 |
|
|
enumeration data automatically. Some of the required information such
|
226 |
|
|
as the vendor id cannot be supplied by generic packages; only by the
|
227 |
|
|
application developer. Class support code such as the USB-ethernet
|
228 |
|
|
package could in theory supply some of the information automatically,
|
229 |
|
|
but there are also hardware dependencies such as which endpoints get
|
230 |
|
|
used for incoming and outgoing ethernet frames. Instead it is the
|
231 |
|
|
responsibility of the application developer to provide all the
|
232 |
|
|
enumeration data and perform some additional initialisation. In
|
233 |
|
|
addition, the common USB slave package can handle all the standard
|
234 |
|
|
control messages for a simple USB peripheral, but for something like a
|
235 |
|
|
multifunction peripheral additional application support is needed.
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
The initial implementation of the eCos USB slave packages involved
|
240 |
|
|
hardware that only supported control and bulk transfers, not
|
241 |
|
|
isochronous or interrupt. There may be future changes to the USB
|
242 |
|
|
code and API to allow for isochronous and interrupt transfers,
|
243 |
|
|
especially the former. Other changes may be required to support
|
244 |
|
|
different USB devices. At present there is no support for USB remote
|
245 |
|
|
wakeups, since again it is not supported by the hardware.
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
|
250 |
|
|
eCos USB I/O Facilities
|
251 |
|
|
|
252 |
|
|
For protocols other than control messages, eCos provides two ways of
|
253 |
|
|
performing USB I/O. The first involves device table or devtab entries such
|
254 |
|
|
as /dev/usb1r,
|
255 |
|
|
with one entry per endpoint per USB device. It is possible to
|
256 |
|
|
open these devices and use conventional blocking
|
257 |
|
|
I/O functions such as read and
|
258 |
|
|
write to exchange data between host and
|
259 |
|
|
peripheral.
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
There is also a lower-level USB-specific API, consisting of functions
|
263 |
|
|
such as
|
264 |
|
|
linkend="usbs-start-rx">usbs_start_rx_buffer.
|
265 |
|
|
A USB device driver will supply a data structure for each endpoint,
|
266 |
|
|
for example a
|
267 |
|
|
linkend="usbs-data">usbs_rx_endpoint
|
268 |
|
|
structure for every receive endpoint. The first argument to
|
269 |
|
|
usbs_start_rx_buffer should be a pointer to such
|
270 |
|
|
a data structure. The USB-specific API is non-blocking: the initial
|
271 |
|
|
call merely starts the transfer; some time later, once the transfer
|
272 |
|
|
has completed or has been aborted, the device driver will invoke a
|
273 |
|
|
completion function.
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
Control messages are different. With four different categories of
|
277 |
|
|
control messages including application and vendor specific ones, the
|
278 |
|
|
conventional
|
279 |
|
|
open/read/write
|
280 |
|
|
model of I/O cannot easily be applied. Instead, a USB device driver
|
281 |
|
|
will supply a
|
282 |
|
|
linkend="usbs-control">usbs_control_endpoint
|
283 |
|
|
data structure which can be manipulated appropriately. In practice the
|
284 |
|
|
standard control messages will usually be handled by the common USB
|
285 |
|
|
slave package, and other control messages will be handled by
|
286 |
|
|
class-specific code such as the USB-ethernet package. Typically,
|
287 |
|
|
application code remains responsible for supplying the
|
288 |
|
|
linkend="usbs-enum">enumeration data and for actually
|
289 |
|
|
linkend="usbs-start">starting up the USB device.
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
Enabling the USB code
|
294 |
|
|
|
295 |
|
|
If the target hardware contains a USB slave device then the
|
296 |
|
|
appropriate USB device driver and the common packages will typically
|
297 |
|
|
be loaded into the configuration automatically when that target is
|
298 |
|
|
selected (assuming a suitable device driver exists). However, the
|
299 |
|
|
driver will not necessarily be active. For example a processor might
|
300 |
|
|
have an on-chip USB device, but not all applications using that
|
301 |
|
|
processor will want to use USB functionality. Hence by default the USB
|
302 |
|
|
device is disabled, ensuring that applications do not suffer any
|
303 |
|
|
memory or other penalties for functionality that is not required.
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
If the application developer explicitly adds a class support package
|
307 |
|
|
such as the USB-ethernet one then this implies that the USB device is
|
308 |
|
|
actually needed, and the device will be enabled automatically.
|
309 |
|
|
However, if no suitable class package is available and the USB device
|
310 |
|
|
will instead be accessed by application code, it is necessary to
|
311 |
|
|
enable the USB device manually. Usually the easiest way to do this is
|
312 |
|
|
to enable the configuration option
|
313 |
|
|
CYGGLO_IO_USB_SLAVE_APPLICATION, and the USB device
|
314 |
|
|
driver and related packages will adjust accordingly. Alternatively,
|
315 |
|
|
the device driver may provide some configuration options to provide
|
316 |
|
|
more fine-grained control.
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
USB Enumeration Data
|
328 |
|
|
|
329 |
|
|
|
330 |
|
|
Enumeration Data
|
331 |
|
|
The USB enumeration data structures
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
#include <cyg/io/usb/usb.h>
|
337 |
|
|
#include <cyg/io/usb/usbs.h>
|
338 |
|
|
|
339 |
|
|
typedef struct usb_device_descriptor {
|
340 |
|
|
…
|
341 |
|
|
} usb_device_descriptor __attribute__((packed));
|
342 |
|
|
|
343 |
|
|
typedef struct usb_configuration_descriptor {
|
344 |
|
|
…
|
345 |
|
|
} usb_configuration_descriptor __attribute__((packed));
|
346 |
|
|
|
347 |
|
|
typedef struct usb_interface_descriptor {
|
348 |
|
|
…
|
349 |
|
|
} usb_interface_descriptor __attribute__((packed));
|
350 |
|
|
|
351 |
|
|
typedef struct usb_endpoint_descriptor {
|
352 |
|
|
…
|
353 |
|
|
} usb_endpoint_descriptor;
|
354 |
|
|
|
355 |
|
|
typedef struct usbs_enumeration_data {
|
356 |
|
|
usb_device_descriptor device;
|
357 |
|
|
int total_number_interfaces;
|
358 |
|
|
int total_number_endpoints;
|
359 |
|
|
int total_number_strings;
|
360 |
|
|
const usb_configuration_descriptor* configurations;
|
361 |
|
|
const usb_interface_descriptor* interfaces;
|
362 |
|
|
const usb_endpoint_descriptor* endpoints;
|
363 |
|
|
const unsigned char** strings;
|
364 |
|
|
} usbs_enumeration_data;
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
USB Enumeration Data
|
369 |
|
|
|
370 |
|
|
When a USB host detects that a peripheral has been plugged in or
|
371 |
|
|
powered up, one of the first steps is to ask the peripheral to
|
372 |
|
|
describe itself by supplying enumeration data. Some of this data
|
373 |
|
|
depends on the class of peripheral. Other fields are vendor-specific.
|
374 |
|
|
There is also a dependency on the hardware, specifically which
|
375 |
|
|
endpoints are available should be used. In general it is not possible
|
376 |
|
|
for generic code to provide this information, so it is the
|
377 |
|
|
responsibility of application code to provide a suitable
|
378 |
|
|
usbs_enumeration_data data structure and
|
379 |
|
|
install it in the endpoint 0 data structure during initialization.
|
380 |
|
|
This must happen before the USB device is enabled by a call to
|
381 |
|
|
usbs_start, for example:
|
382 |
|
|
|
383 |
|
|
|
384 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
385 |
|
|
…
|
386 |
|
|
};
|
387 |
|
|
|
388 |
|
|
int
|
389 |
|
|
main(int argc, char** argv)
|
390 |
|
|
{
|
391 |
|
|
usbs_sa11x0_ep0.enumeration_data = &usb_enum_data;
|
392 |
|
|
…
|
393 |
|
|
usbs_start(&usbs_sa11x0_ep0);
|
394 |
|
|
…
|
395 |
|
|
}
|
396 |
|
|
|
397 |
|
|
|
398 |
|
|
For most applications the enumeration data will be static, although
|
399 |
|
|
the usbs_enumeration_data structure can be
|
400 |
|
|
filled in at run-time if necessary. Full details of the enumeration
|
401 |
|
|
data can be found in the Universal Serial Bus specification obtainable
|
402 |
|
|
from the USB Implementers Forum web
|
403 |
|
|
site, although the meaning of most fields is fairly obvious.
|
404 |
|
|
The various data structures and utility macros are defined in the
|
405 |
|
|
header files
|
406 |
|
|
and . Note
|
407 |
|
|
that the example code below makes use of the gcc labelled element
|
408 |
|
|
extension.
|
409 |
|
|
|
410 |
|
|
|
411 |
|
|
usb_device_descriptor
|
412 |
|
|
|
413 |
|
|
The main information about a USB peripheral comes from a single
|
414 |
|
|
usb_device_descriptor structure, which is
|
415 |
|
|
embedded in the usbs_enumeration_data
|
416 |
|
|
structure. A typical example might look like this:
|
417 |
|
|
|
418 |
|
|
|
419 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
420 |
|
|
{
|
421 |
|
|
length: USB_DEVICE_DESCRIPTOR_LENGTH,
|
422 |
|
|
type: USB_DEVICE_DESCRIPTOR_TYPE,
|
423 |
|
|
usb_spec_lo: USB_DEVICE_DESCRIPTOR_USB11_LO,
|
424 |
|
|
usb_spec_hi: USB_DEVICE_DESCRIPTOR_USB11_HI,
|
425 |
|
|
device_class: USB_DEVICE_DESCRIPTOR_CLASS_VENDOR,
|
426 |
|
|
device_subclass: USB_DEVICE_DESCRIPTOR_SUBCLASS_VENDOR,
|
427 |
|
|
device_protocol: USB_DEVICE_DESCRIPTOR_PROTOCOL_VENDOR,
|
428 |
|
|
max_packet_size: 8,
|
429 |
|
|
vendor_lo: 0x42,
|
430 |
|
|
vendor_hi: 0x42,
|
431 |
|
|
product_lo: 0x42,
|
432 |
|
|
product_hi: 0x42,
|
433 |
|
|
device_lo: 0x00,
|
434 |
|
|
device_hi: 0x01,
|
435 |
|
|
manufacturer_str: 1,
|
436 |
|
|
product_str: 2,
|
437 |
|
|
serial_number_str: 0,
|
438 |
|
|
number_configurations: 1
|
439 |
|
|
},
|
440 |
|
|
…
|
441 |
|
|
};
|
442 |
|
|
|
443 |
|
|
|
444 |
|
|
The length and type fields are specified by the USB standard. The
|
445 |
|
|
usb_spec_lo and
|
446 |
|
|
usb_spec_hi fields identify the particular
|
447 |
|
|
revision of the standard that the peripheral implements, for example
|
448 |
|
|
revision 1.1.
|
449 |
|
|
|
450 |
|
|
|
451 |
|
|
The device class, subclass, and protocol fields are used by generic
|
452 |
|
|
host-side USB software to determine which host-side device driver
|
453 |
|
|
should be loaded to interact with the peripheral. A number of standard
|
454 |
|
|
classes are defined, for example mass-storage devices and
|
455 |
|
|
human-interface devices. If a peripheral implements one of the
|
456 |
|
|
standard classes then a standard existing host-side device driver may
|
457 |
|
|
exist, eliminating the need to write a custom driver. The value
|
458 |
|
|
0xFF (VENDOR) is reserved for
|
459 |
|
|
peripherals that implement a vendor-specific protocol rather than a
|
460 |
|
|
standard one. Such peripherals will require a custom host-side device
|
461 |
|
|
driver. The value 0x00
|
462 |
|
|
(INTERFACE) is reserved and indicates that the
|
463 |
|
|
protocol used by the peripheral is defined at the interface level
|
464 |
|
|
rather than for the peripheral as a whole.
|
465 |
|
|
|
466 |
|
|
|
467 |
|
|
The max_package_size field specifies the
|
468 |
|
|
maximum length of a control message. There is a lower bound of eight
|
469 |
|
|
bytes, and typical hardware will not support anything larger because
|
470 |
|
|
control messages are usually small and not performance-critical.
|
471 |
|
|
|
472 |
|
|
|
473 |
|
|
The vendor_lo and
|
474 |
|
|
vendor_hi fields specify a vendor id, which
|
475 |
|
|
must be obtained from the USB Implementor's Forum. The numbers used in
|
476 |
|
|
the code fragment above are examples only and must not be used in real
|
477 |
|
|
USB peripherals. The product identifier is determined by the vendor,
|
478 |
|
|
and different USB peripherals should use different identifiers. The
|
479 |
|
|
device identifier field should indicate a release number in
|
480 |
|
|
binary-coded decimal.
|
481 |
|
|
|
482 |
|
|
|
483 |
|
|
The above fields are all numerical in nature. A USB peripheral can
|
484 |
|
|
also provide a number of strings as described
|
485 |
|
|
linkend="usbs-enum-strings">below, for example the name of the
|
486 |
|
|
vendor can be provided. The various _str
|
487 |
|
|
fields act as indices into an array of strings, with index 0
|
488 |
|
|
indicating that no string is available.
|
489 |
|
|
|
490 |
|
|
|
491 |
|
|
A typical USB peripheral involves just a single configuration. However
|
492 |
|
|
more complicated peripherals can support multiple configurations. Only
|
493 |
|
|
one configuration will be active at any one time, and the host will
|
494 |
|
|
switch between them as appropriate. If a peripheral does involve
|
495 |
|
|
multiple configurations then typically it will be the responsibility
|
496 |
|
|
of application code to
|
497 |
|
|
linkend="usbs-control-standard">handle the standard
|
498 |
|
|
set-configuration control message.
|
499 |
|
|
|
500 |
|
|
|
501 |
|
|
|
502 |
|
|
usb_configuration_descriptor
|
503 |
|
|
|
504 |
|
|
A USB peripheral involves at least one and possible several different
|
505 |
|
|
configurations. The usbs_enumeration_data
|
506 |
|
|
structure requires a pointer to an array, possibly of length 1, of
|
507 |
|
|
usb_configuration_descriptor structures.
|
508 |
|
|
Usually a single structure suffices:
|
509 |
|
|
|
510 |
|
|
|
511 |
|
|
const usb_configuration_descriptor usb_configuration = {
|
512 |
|
|
length: USB_CONFIGURATION_DESCRIPTOR_LENGTH,
|
513 |
|
|
type: USB_CONFIGURATION_DESCRIPTOR_TYPE,
|
514 |
|
|
total_length_lo: USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_LO(1, 2),
|
515 |
|
|
total_length_hi: USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_HI(1, 2),
|
516 |
|
|
number_interfaces: 1,
|
517 |
|
|
configuration_id: 1,
|
518 |
|
|
configuration_str: 0,
|
519 |
|
|
attributes: USB_CONFIGURATION_DESCRIPTOR_ATTR_REQUIRED |
|
520 |
|
|
USB_CONFIGURATION_DESCRIPTOR_ATTR_SELF_POWERED,
|
521 |
|
|
max_power: 50
|
522 |
|
|
};
|
523 |
|
|
|
524 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
525 |
|
|
…
|
526 |
|
|
configurations: &usb_configuration,
|
527 |
|
|
…
|
528 |
|
|
};
|
529 |
|
|
|
530 |
|
|
|
531 |
|
|
The values for the length and
|
532 |
|
|
type fields are determined by the standard.
|
533 |
|
|
The total_length field depends on the
|
534 |
|
|
number of interfaces and endpoints used by this configuration, and
|
535 |
|
|
convenience macros are provided to calculate this: the first argument
|
536 |
|
|
to the macros specify the number of interfaces, the second the number
|
537 |
|
|
of endpoints. The number_interfaces field
|
538 |
|
|
is self-explanatory. If the peripheral involves multiple
|
539 |
|
|
configurations then each one must have a unique id, and this will be
|
540 |
|
|
used in the set-configuration control message. The id
|
541 |
|
|
0 is reserved, and a set-configuration control
|
542 |
|
|
message that uses this id indicates that the peripheral should be
|
543 |
|
|
inactive. Configurations can have a string description if required.
|
544 |
|
|
The attributes field must have the
|
545 |
|
|
REQUIRED bit set; the
|
546 |
|
|
SELF_POWERED bit informs the host that the
|
547 |
|
|
peripheral has its own power supply and will not draw any power over
|
548 |
|
|
the bus, leaving more bus power available to other peripherals; the
|
549 |
|
|
REMOTE_WAKEUP bit is used if the peripheral can
|
550 |
|
|
interrupt the host when the latter is in power-saving mode. For
|
551 |
|
|
peripherals that are not self-powered, the
|
552 |
|
|
max_power field specifies the power
|
553 |
|
|
requirements in units of 2mA.
|
554 |
|
|
|
555 |
|
|
|
556 |
|
|
|
557 |
|
|
usb_interface_descriptor
|
558 |
|
|
|
559 |
|
|
A USB configuration involves one or more interfaces, typically
|
560 |
|
|
corresponding to different streams of data. For example, one interface
|
561 |
|
|
might involve video data while another interface is for audio.
|
562 |
|
|
Multiple interfaces in a single configuration will be active at the
|
563 |
|
|
same time.
|
564 |
|
|
|
565 |
|
|
|
566 |
|
|
const usb_interface_descriptor usb_interface = {
|
567 |
|
|
length: USB_INTERFACE_DESCRIPTOR_LENGTH,
|
568 |
|
|
type: USB_INTERFACE_DESCRIPTOR_TYPE,
|
569 |
|
|
interface_id: 0,
|
570 |
|
|
alternate_setting: 0,
|
571 |
|
|
number_endpoints: 2,
|
572 |
|
|
interface_class: USB_INTERFACE_DESCRIPTOR_CLASS_VENDOR,
|
573 |
|
|
interface_subclass: USB_INTERFACE_DESCRIPTOR_SUBCLASS_VENDOR,
|
574 |
|
|
interface_protocol: USB_INTERFACE_DESCRIPTOR_PROTOCOL_VENDOR,
|
575 |
|
|
interface_str: 0
|
576 |
|
|
};
|
577 |
|
|
|
578 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
579 |
|
|
…
|
580 |
|
|
total_number_interfaces: 1,
|
581 |
|
|
interfaces: &usb_interface,
|
582 |
|
|
…
|
583 |
|
|
};
|
584 |
|
|
|
585 |
|
|
|
586 |
|
|
Again, the length and
|
587 |
|
|
type fields are specified by the standard.
|
588 |
|
|
Each interface within a configuration requires its own id. However, a
|
589 |
|
|
given interface may have several alternate settings, in other words
|
590 |
|
|
entries in the interfaces array with the same id but different
|
591 |
|
|
alternate_setting fields. For example,
|
592 |
|
|
there might be one setting which requires a bandwidth of 100K/s and
|
593 |
|
|
another setting that only needs 50K/s. The host can use the standard
|
594 |
|
|
set-interface control message to choose the most appropriate setting.
|
595 |
|
|
The handling of this request is the responsibility of higher-level
|
596 |
|
|
code, so the application may have to
|
597 |
|
|
linkend="usbs-control-standard">install its own handler.
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
The number of endpoints used by an interface is specified in the
|
601 |
|
|
number_endpoints field. Exact details of
|
602 |
|
|
which endpoints are used is held in a separate array of endpoint
|
603 |
|
|
descriptors. The class, subclass and protocol fields are used by
|
604 |
|
|
host-side code to determine which host-side device driver should
|
605 |
|
|
handle this specific interface. Usually this is determined on a
|
606 |
|
|
per-peripheral basis in the
|
607 |
|
|
usb_device_descriptor structure, but that can
|
608 |
|
|
defer the details to individual interfaces. A per-interface string
|
609 |
|
|
is allowed as well.
|
610 |
|
|
|
611 |
|
|
|
612 |
|
|
For USB peripherals involving multiple configurations, the array of
|
613 |
|
|
usb_interface_descriptor structures should
|
614 |
|
|
first contain all the interfaces for the first configuration, then all
|
615 |
|
|
the interfaces for the second configuration, and so on.
|
616 |
|
|
|
617 |
|
|
|
618 |
|
|
|
619 |
|
|
usb_endpoint_descriptor
|
620 |
|
|
|
621 |
|
|
The host also needs information about which endpoint should be used
|
622 |
|
|
for what. This involves an array of endpoint descriptors:
|
623 |
|
|
|
624 |
|
|
|
625 |
|
|
const usb_endpoint_descriptor usb_endpoints[] = {
|
626 |
|
|
{
|
627 |
|
|
length: USB_ENDPOINT_DESCRIPTOR_LENGTH,
|
628 |
|
|
type: USB_ENDPOINT_DESCRIPTOR_TYPE,
|
629 |
|
|
endpoint: USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT | 1,
|
630 |
|
|
attributes: USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
|
631 |
|
|
max_packet_lo: 64,
|
632 |
|
|
max_packet_hi: 0,
|
633 |
|
|
interval: 0
|
634 |
|
|
},
|
635 |
|
|
{
|
636 |
|
|
length: USB_ENDPOINT_DESCRIPTOR_LENGTH,
|
637 |
|
|
type: USB_ENDPOINT_DESCRIPTOR_TYPE,
|
638 |
|
|
endpoint: USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN | 2,
|
639 |
|
|
attributes: USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
|
640 |
|
|
max_packet_lo: 64,
|
641 |
|
|
max_packet_hi: 0,
|
642 |
|
|
interval: 0
|
643 |
|
|
}
|
644 |
|
|
};
|
645 |
|
|
|
646 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
647 |
|
|
…
|
648 |
|
|
total_number_endpoints: 2,
|
649 |
|
|
endpoints: usb_endpoints,
|
650 |
|
|
…
|
651 |
|
|
};
|
652 |
|
|
|
653 |
|
|
|
654 |
|
|
As usual the values for the length and
|
655 |
|
|
type fields are specified by the standard.
|
656 |
|
|
The endpoint field gives both the endpoint
|
657 |
|
|
number and the direction, so in the above example endpoint 1 is used
|
658 |
|
|
for OUT (host to peripheral) transfers and endpoint 2 is used for IN
|
659 |
|
|
(peripheral to host) transfers. The
|
660 |
|
|
attributes field indicates the USB protocol
|
661 |
|
|
that should be used on this endpoint: CONTROL,
|
662 |
|
|
ISOCHRONOUS, BULK or
|
663 |
|
|
INTERRUPT. The
|
664 |
|
|
max_packet field specifies the maximum size
|
665 |
|
|
of a single USB packet. For bulk transfers this will typically be 64
|
666 |
|
|
bytes. For isochronous transfers this can be up to 1023 bytes. For
|
667 |
|
|
interrupt transfers it can be up to 64 bytes, although usually a
|
668 |
|
|
smaller value will be used. The interval
|
669 |
|
|
field is ignored for control and bulk transfers. For isochronous
|
670 |
|
|
transfers it should be set to 1. For interrupt transfers it can be a
|
671 |
|
|
value between 1 and 255, and indicates the number of milliseconds
|
672 |
|
|
between successive polling operations.
|
673 |
|
|
|
674 |
|
|
|
675 |
|
|
For USB peripherals involving multiple configurations or interfaces
|
676 |
|
|
the array of endpoint descriptors should be organized sequentially:
|
677 |
|
|
first the endpoints corresponding to the first interface of the first
|
678 |
|
|
configuration, then the second interface in that configuration, and so
|
679 |
|
|
on; then all the endpoints for all the interfaces in the second
|
680 |
|
|
configuration; etc.
|
681 |
|
|
|
682 |
|
|
|
683 |
|
|
|
684 |
|
|
Strings
|
685 |
|
|
|
686 |
|
|
The enumeration data can contain a number of strings with additional
|
687 |
|
|
information. Unicode encoding is used for the strings, and it is
|
688 |
|
|
possible for a peripheral to supply a given string in multiple
|
689 |
|
|
languages using the appropriate characters. The first two bytes of
|
690 |
|
|
each string give a length and type field. The first string is special;
|
691 |
|
|
after the two bytes header it consists of an array of 2-byte language
|
692 |
|
|
id codes, indicating the supported languages. The language code
|
693 |
|
|
0x0409 corresponds to English (United States).
|
694 |
|
|
|
695 |
|
|
|
696 |
|
|
const unsigned char* usb_strings[] = {
|
697 |
|
|
"\004\003\011\004",
|
698 |
|
|
"\020\003R\000e\000d\000 \000H\000a\000t\000"
|
699 |
|
|
};
|
700 |
|
|
|
701 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
702 |
|
|
…
|
703 |
|
|
total_number_strings: 2,
|
704 |
|
|
strings: usb_strings,
|
705 |
|
|
…
|
706 |
|
|
};
|
707 |
|
|
|
708 |
|
|
|
709 |
|
|
The default handler for standard control messages assumes that the
|
710 |
|
|
peripheral only uses a single language. If this is not the case then
|
711 |
|
|
higher-level code will have to handle the standard get-descriptor
|
712 |
|
|
control messages when a string descriptor is requested.
|
713 |
|
|
|
714 |
|
|
|
715 |
|
|
|
716 |
|
|
usbs_enumeration_data
|
717 |
|
|
|
718 |
|
|
The usbs_enumeration_data data structure
|
719 |
|
|
collects together all the various descriptors that make up the
|
720 |
|
|
enumeration data. It is the responsibility of application code to
|
721 |
|
|
supply a suitable data structure and install it in the control
|
722 |
|
|
endpoints's enumeration_data field before
|
723 |
|
|
the USB device is started.
|
724 |
|
|
|
725 |
|
|
|
726 |
|
|
|
727 |
|
|
|
728 |
|
|
|
729 |
|
|
|
730 |
|
|
|
731 |
|
|
|
732 |
|
|
|
733 |
|
|
|
734 |
|
|
|
735 |
|
|
Starting up a USB Device
|
736 |
|
|
|
737 |
|
|
|
738 |
|
|
usbs_start
|
739 |
|
|
Starting up a USB Device
|
740 |
|
|
|
741 |
|
|
|
742 |
|
|
|
743 |
|
|
|
744 |
|
|
|
745 |
|
|
#include <cyg/io/usb/usbs.h>
|
746 |
|
|
|
747 |
|
|
|
748 |
|
|
void usbs_start
|
749 |
|
|
usbs_control_endpoint* ep0
|
750 |
|
|
|
751 |
|
|
|
752 |
|
|
|
753 |
|
|
|
754 |
|
|
Description
|
755 |
|
|
|
756 |
|
|
Initializing a USB device requires some support from higher-level
|
757 |
|
|
code, typically the application, in the form of enumeration data.
|
758 |
|
|
Hence it is not possible for the low-level USB driver to activate a
|
759 |
|
|
USB device itself. Instead the higher-level code has to take care of
|
760 |
|
|
this by invoking usbs_start. This function takes
|
761 |
|
|
a pointer to a USB control endpoint data structure. USB device drivers
|
762 |
|
|
should provide exactly one such data structure for every USB device,
|
763 |
|
|
so the pointer uniquely identifies the device.
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
const usbs_enumeration_data usb_enum_data = {
|
767 |
|
|
…
|
768 |
|
|
};
|
769 |
|
|
|
770 |
|
|
int
|
771 |
|
|
main(int argc, char** argv)
|
772 |
|
|
{
|
773 |
|
|
usbs_sa11x0_ep0.enumeration_data = &usb_enum_data;
|
774 |
|
|
…
|
775 |
|
|
usbs_start(&usbs_sa11x0_ep0);
|
776 |
|
|
…
|
777 |
|
|
}
|
778 |
|
|
|
779 |
|
|
|
780 |
|
|
The exact behaviour of usbs_start depends on the
|
781 |
|
|
USB hardware and the device driver. A typical implementation would
|
782 |
|
|
change the USB data pins from tristated to active. If the peripheral
|
783 |
|
|
is already plugged into a host then the latter should detect this
|
784 |
|
|
change and start interacting with the peripheral, including requesting
|
785 |
|
|
the enumeration data. Some of this may happen before
|
786 |
|
|
usbs_start returns, but given that multiple
|
787 |
|
|
interactions between USB host and peripheral are required it is likely
|
788 |
|
|
that the function will return before the peripheral is fully
|
789 |
|
|
configured. Control endpoints provide a
|
790 |
|
|
linkend="usbs-control-state">mechanism for informing
|
791 |
|
|
higher-level code of USB state changes.
|
792 |
|
|
usbs_start will return even if the peripheral is
|
793 |
|
|
not currently connected to a host: it will not block until the
|
794 |
|
|
connection is established.
|
795 |
|
|
|
796 |
|
|
|
797 |
|
|
usbs_start should only be called once for a given
|
798 |
|
|
USB device. There are no defined error conditions. Note that the
|
799 |
|
|
function affects the entire USB device and not just the control
|
800 |
|
|
endpoint: there is no need to start any data endpoints as well.
|
801 |
|
|
|
802 |
|
|
|
803 |
|
|
|
804 |
|
|
|
805 |
|
|
|
806 |
|
|
|
807 |
|
|
|
808 |
|
|
|
809 |
|
|
|
810 |
|
|
Devtab Entries
|
811 |
|
|
|
812 |
|
|
|
813 |
|
|
Devtab Entries
|
814 |
|
|
Data endpoint data structure
|
815 |
|
|
|
816 |
|
|
|
817 |
|
|
|
818 |
|
|
|
819 |
|
|
/dev/usb0c
|
820 |
|
|
/dev/usb1r
|
821 |
|
|
/dev/usb2w
|
822 |
|
|
|
823 |
|
|
|
824 |
|
|
|
825 |
|
|
Devtab Entries
|
826 |
|
|
|
827 |
|
|
USB device drivers provide two ways of transferring data between host
|
828 |
|
|
and peripheral. The first involves USB-specific functionality such as
|
829 |
|
|
|
830 |
|
|
linkend="usbs-start-rx">usbs_start_rx_buffer.
|
831 |
|
|
This provides non-blocking I/O: a transfer is started, and some time
|
832 |
|
|
later the device driver will call a supplied completion function. The
|
833 |
|
|
second uses the conventional I/O model: there are entries in the
|
834 |
|
|
device table corresponding to the various endpoints. Standard calls
|
835 |
|
|
such as open can then be used to get a suitable
|
836 |
|
|
handle. Actual I/O happens via blocking read and
|
837 |
|
|
write calls. In practice the blocking operations
|
838 |
|
|
are simply implemented using the underlying non-blocking
|
839 |
|
|
functionality.
|
840 |
|
|
|
841 |
|
|
|
842 |
|
|
Each endpoint will have its own devtab entry. The exact names are
|
843 |
|
|
controlled by the device driver package, but typically the root will
|
844 |
|
|
be /dev/usb. This is followed by one or more
|
845 |
|
|
decimal digits giving the endpoint number, followed by
|
846 |
|
|
c for a control endpoint, r for
|
847 |
|
|
a receive endpoint (host to peripheral), and w for
|
848 |
|
|
a transmit endpoint (peripheral to host). If the target hardware
|
849 |
|
|
involves more than one USB device then different roots should be used,
|
850 |
|
|
for example /dev/usb0c and
|
851 |
|
|
/dev/usb1_0c. This may require explicit
|
852 |
|
|
manipulation of device driver configuration options by the application
|
853 |
|
|
developer.
|
854 |
|
|
|
855 |
|
|
|
856 |
|
|
At present the devtab entry for a control endpoint does not support
|
857 |
|
|
any I/O operations.
|
858 |
|
|
|
859 |
|
|
|
860 |
|
|
write operations
|
861 |
|
|
|
862 |
|
|
cyg_io_write and similar functions in
|
863 |
|
|
higher-level packages can be used to perform a transfer from
|
864 |
|
|
peripheral to host. Successive write operations will not be coalesced.
|
865 |
|
|
For example, when doing a 1000 byte write to an endpoint that uses the
|
866 |
|
|
bulk transfer protocol this will involve 15 full-size 64-byte packets
|
867 |
|
|
and a terminating 40-byte packet. USB device drivers are not expected
|
868 |
|
|
to do any locking, and if higher-level code performs multiple
|
869 |
|
|
concurrent write operations on a single endpoint then the resulting
|
870 |
|
|
behaviour is undefined.
|
871 |
|
|
|
872 |
|
|
|
873 |
|
|
A USB write operation will never transfer less
|
874 |
|
|
data than specified. It is the responsibility of higher-level code to
|
875 |
|
|
ensure that the amount of data being transferred is acceptable to the
|
876 |
|
|
host-side code. Usually this will be defined by a higher-level
|
877 |
|
|
protocol. If an attempt is made to transfer more data than the host
|
878 |
|
|
expects then the resulting behaviour is undefined.
|
879 |
|
|
|
880 |
|
|
|
881 |
|
|
There are two likely error conditions. EPIPE
|
882 |
|
|
indicates that the connection between host and target has been broken.
|
883 |
|
|
EAGAIN indicates that the endpoint has been
|
884 |
|
|
stalled, either at the request of the host or by other activity
|
885 |
|
|
inside the peripheral.
|
886 |
|
|
|
887 |
|
|
|
888 |
|
|
|
889 |
|
|
read operations
|
890 |
|
|
|
891 |
|
|
cyg_io_read and similar functions in higher-level
|
892 |
|
|
packages can be used to perform a transfer from host to peripheral.
|
893 |
|
|
This should be a complete transfer: higher-level protocols should
|
894 |
|
|
define an upper bound on the amount of data being transferred, and the
|
895 |
|
|
read operation should involve at least this
|
896 |
|
|
amount of data. The return value will indicate the actual transfer
|
897 |
|
|
size, which may be less than requested.
|
898 |
|
|
|
899 |
|
|
|
900 |
|
|
Some device drivers may support partial reads, but USB device drivers
|
901 |
|
|
are not expected to perform any buffering because that involves both
|
902 |
|
|
memory and code overheads. One technique that may work for bulk
|
903 |
|
|
transfers is to exploit the fact that such transfers happen in 64-byte
|
904 |
|
|
packets. It is possible to read an initial 64
|
905 |
|
|
bytes, corresponding to the first packet in the transfer. These 64
|
906 |
|
|
bytes can then be examined to determine the total transfer size, and
|
907 |
|
|
the remaining data can be transferred in another
|
908 |
|
|
read operation. This technique is not guaranteed
|
909 |
|
|
to work with all USB hardware. Also, if the delay between accepting
|
910 |
|
|
the first packet and the remainder of the transfer is excessive then
|
911 |
|
|
this could cause timeout problems for the host-side software. For
|
912 |
|
|
these reasons the use of partial reads should be avoided.
|
913 |
|
|
|
914 |
|
|
|
915 |
|
|
There are two likely error conditions. EPIPE
|
916 |
|
|
indicates that the connection between host and target has been broken.
|
917 |
|
|
EAGAIN indicates that the endpoint has been
|
918 |
|
|
stalled, either at the request of the host or by other activity
|
919 |
|
|
inside the peripheral.
|
920 |
|
|
|
921 |
|
|
|
922 |
|
|
USB device drivers are not expected to do any locking. If higher-level
|
923 |
|
|
code performs multiple concurrent read operations on a single endpoint
|
924 |
|
|
then the resulting behaviour is undefined.
|
925 |
|
|
|
926 |
|
|
|
927 |
|
|
|
928 |
|
|
select operations
|
929 |
|
|
|
930 |
|
|
Typical USB device drivers will not provide any support for
|
931 |
|
|
select. Consider bulk transfers from the host to
|
932 |
|
|
the peripheral. At the USB device driver level there is no way of
|
933 |
|
|
knowing in advance how large a transfer will be, so it is not feasible
|
934 |
|
|
for the device driver to buffer the entire transfer. It may be
|
935 |
|
|
possible to buffer part of the transfer, for example the first 64-byte
|
936 |
|
|
packet, and copy this into application space at the start of a
|
937 |
|
|
read, but this adds code and memory overheads.
|
938 |
|
|
Worse, it means that there is an unknown but potentially long delay
|
939 |
|
|
between a peripheral accepting the first packet of a transfer and the
|
940 |
|
|
remaining packets, which could confuse or upset the host-side
|
941 |
|
|
software.
|
942 |
|
|
|
943 |
|
|
|
944 |
|
|
With some USB hardware it may be possible for the device driver to
|
945 |
|
|
detect OUT tokens from the host without actually accepting the data,
|
946 |
|
|
and this would indicate that a read is likely to
|
947 |
|
|
succeed. However, it would not be reliable since the host-side I/O
|
948 |
|
|
operation could time out. A similar mechanism could be used to
|
949 |
|
|
implement select for outgoing data, but again
|
950 |
|
|
this would not be reliable.
|
951 |
|
|
|
952 |
|
|
|
953 |
|
|
Some device drivers may provide partial support for
|
954 |
|
|
select anyway, possibly under the control of a
|
955 |
|
|
configuration option. The device driver's documentation should be
|
956 |
|
|
consulted for further information. It is also worth noting that the
|
957 |
|
|
USB-specific non-blocking API can often be used as an alternative to
|
958 |
|
|
select.
|
959 |
|
|
|
960 |
|
|
|
961 |
|
|
|
962 |
|
|
get_config and
|
963 |
|
|
set_config operations
|
964 |
|
|
|
965 |
|
|
There are no set_config or
|
966 |
|
|
get_config (also known as
|
967 |
|
|
ioctl) operations defined for USB devices.
|
968 |
|
|
Some device drivers may provide hardware-specific facilities this way.
|
969 |
|
|
|
970 |
|
|
|
971 |
|
|
|
972 |
|
|
Currently the USB-specific functions related to
|
973 |
|
|
linkend="usbs-halt">halted endpoints cannot be accessed readily
|
974 |
|
|
via devtab entries. This functionality should probably be made
|
975 |
|
|
available via set_config and
|
976 |
|
|
get_config. It may also prove useful to provide
|
977 |
|
|
a get_config operation that maps from the
|
978 |
|
|
devtab entries to the underlying endpoint data structures.
|
979 |
|
|
|
980 |
|
|
|
981 |
|
|
|
982 |
|
|
|
983 |
|
|
Presence
|
984 |
|
|
|
985 |
|
|
The devtab entries are optional. If the USB device is accessed
|
986 |
|
|
primarily by class-specific code such as the USB-ethernet package and
|
987 |
|
|
that package uses the USB-specific API directly, the devtab entries
|
988 |
|
|
are redundant. Even if application code does need to access the USB
|
989 |
|
|
device, the non-blocking API may be more convenient than the blocking
|
990 |
|
|
I/O provided via the devtab entries. In these cases the devtab entries
|
991 |
|
|
serve no useful purpose, but they still impose a memory overhead. It
|
992 |
|
|
is possible to suppress the presence of these entries by disabling the
|
993 |
|
|
configuration option
|
994 |
|
|
CYGGLO_IO_USB_SLAVE_PROVIDE_DEVTAB_ENTRIES.
|
995 |
|
|
|
996 |
|
|
|
997 |
|
|
|
998 |
|
|
|
999 |
|
|
|
1000 |
|
|
|
1001 |
|
|
|
1002 |
|
|
|
1003 |
|
|
|
1004 |
|
|
|
1005 |
|
|
Receiving Data from the Host
|
1006 |
|
|
|
1007 |
|
|
|
1008 |
|
|
usbs_start_rx_buffer
|
1009 |
|
|
Receiving Data from the Host
|
1010 |
|
|
|
1011 |
|
|
|
1012 |
|
|
|
1013 |
|
|
|
1014 |
|
|
|
1015 |
|
|
#include <cyg/io/usb/usbs.h>
|
1016 |
|
|
|
1017 |
|
|
|
1018 |
|
|
void usbs_start_rx_buffer
|
1019 |
|
|
usbs_rx_endpoint* ep
|
1020 |
|
|
unsigned char* buffer
|
1021 |
|
|
int length
|
1022 |
|
|
void (*)(void*,int) complete_fn
|
1023 |
|
|
void * complete_data
|
1024 |
|
|
|
1025 |
|
|
|
1026 |
|
|
|
1027 |
|
|
void usbs_start_rx
|
1028 |
|
|
usbs_rx_endpoint* ep
|
1029 |
|
|
|
1030 |
|
|
|
1031 |
|
|
|
1032 |
|
|
|
1033 |
|
|
|
1034 |
|
|
Description
|
1035 |
|
|
|
1036 |
|
|
usbs_start_rx_buffer is a USB-specific function
|
1037 |
|
|
to accept a transfer from host to peripheral. It can be used for bulk,
|
1038 |
|
|
interrupt or isochronous transfers, but not for control messages.
|
1039 |
|
|
Instead those involve manipulating the
|
1040 |
|
|
linkend="usbs-control">usbs_control_endpoint
|
1041 |
|
|
data structure directly. The function takes five arguments:
|
1042 |
|
|
|
1043 |
|
|
|
1044 |
|
|
|
1045 |
|
|
|
1046 |
|
|
The first argument identifies the specific endpoint that should be
|
1047 |
|
|
used. Different USB devices will support different sets of endpoints
|
1048 |
|
|
and the device driver will provide appropriate data structures. The
|
1049 |
|
|
device driver's documentation should be consulted for details of which
|
1050 |
|
|
endpoints are available.
|
1051 |
|
|
|
1052 |
|
|
|
1053 |
|
|
|
1054 |
|
|
|
1055 |
|
|
The buffer and length
|
1056 |
|
|
arguments control the actual transfer. USB device drivers are not
|
1057 |
|
|
expected to perform any buffering or to support partial transfers, so
|
1058 |
|
|
the length specified should correspond to the maximum transfer that is
|
1059 |
|
|
currently possible and the buffer should be at least this large. For
|
1060 |
|
|
isochronous transfers the USB specification imposes an upper bound of
|
1061 |
|
|
1023 bytes, and a smaller limit may be set in the
|
1062 |
|
|
linkend="usbs-enum-endpoint">enumeration data. Interrupt
|
1063 |
|
|
transfers are similarly straightforward with an upper bound of 64
|
1064 |
|
|
bytes, or less as per the enumeration data. Bulk transfers are more
|
1065 |
|
|
complicated because they can involve multiple 64-byte packets plus a
|
1066 |
|
|
terminating packet of less than 64 bytes, so there is no predefined
|
1067 |
|
|
limit on the transfer size. Instead it is left to higher-level
|
1068 |
|
|
protocols to specify an appropriate upper bound.
|
1069 |
|
|
|
1070 |
|
|
|
1071 |
|
|
One technique that may work for bulk transfers is to exploit the fact
|
1072 |
|
|
that such transfers happen in 64-byte packets: it may be possible to
|
1073 |
|
|
receive an initial 64 bytes, corresponding to the first packet in the
|
1074 |
|
|
transfer; these 64 bytes can then be examined to determine the total
|
1075 |
|
|
transfer size, and the remaining data can be transferred in another
|
1076 |
|
|
receive operation. This technique is not guaranteed to work with all
|
1077 |
|
|
USB hardware. Also, if the delay between accepting the first packet and
|
1078 |
|
|
the remainder of the transfer is excessive then this could cause
|
1079 |
|
|
timeout problems for the host-side software. For these reasons this
|
1080 |
|
|
technique should be avoided.
|
1081 |
|
|
|
1082 |
|
|
|
1083 |
|
|
|
1084 |
|
|
|
1085 |
|
|
usbs_start_rx_buffer is non-blocking. It merely
|
1086 |
|
|
starts the receive operation, and does not wait for completion. At
|
1087 |
|
|
some later point the USB device driver will invoke the completion
|
1088 |
|
|
function parameter with two arguments: the completion data defined by
|
1089 |
|
|
the last parameter and a result field. A result >=
|
1090 |
|
|
0 indicates a successful transfer of that many
|
1091 |
|
|
bytes, which may be less than the upper bound imposed by the
|
1092 |
|
|
length argument. A result <
|
1093 |
|
|
0 indicates an error. The most likely errors are
|
1094 |
|
|
-EPIPE to indicate that the connection between the
|
1095 |
|
|
host and the target has been broken, and -EAGAIN
|
1096 |
|
|
for when the endpoint has been
|
1097 |
|
|
linkend="usbs-halt">halted. Specific USB device drivers may
|
1098 |
|
|
specify additional error conditions.
|
1099 |
|
|
|
1100 |
|
|
|
1101 |
|
|
|
1102 |
|
|
|
1103 |
|
|
The normal sequence of events is that the USB device driver will
|
1104 |
|
|
update the appropriate hardware registers. At some point after that
|
1105 |
|
|
the host will attempt to send data by transmitting an OUT token
|
1106 |
|
|
followed by a data packet, and since a receive operation is now in
|
1107 |
|
|
progress the data will be accepted and ACK'd. If there were no receive
|
1108 |
|
|
operation then the peripheral would instead generate a NAK. The USB
|
1109 |
|
|
hardware will generate an interrupt once the whole packet has been
|
1110 |
|
|
received, and the USB device driver will service this interrupt and
|
1111 |
|
|
arrange for a DSR to be called. Isochronous and interrupt transfers
|
1112 |
|
|
involve just a single packet. However, bulk transfers may involve
|
1113 |
|
|
multiple packets so the device driver has to check whether the packet
|
1114 |
|
|
was a full 64 bytes or whether it was a terminating packet of less
|
1115 |
|
|
than this. When the device driver DSR detects a complete transfer it
|
1116 |
|
|
will inform higher-level code by invoking the supplied completion
|
1117 |
|
|
function.
|
1118 |
|
|
|
1119 |
|
|
|
1120 |
|
|
This means that the completion function will normally be invoked by a
|
1121 |
|
|
DSR and not in thread context - although some USB device drivers may
|
1122 |
|
|
have a different implementation. Therefore the completion function is
|
1123 |
|
|
restricted in what it can do. In particular it must not make any
|
1124 |
|
|
calls that will or may block such as locking a mutex or allocating
|
1125 |
|
|
memory. The kernel documentation should be consulted for more details
|
1126 |
|
|
of DSR's and interrupt handling generally.
|
1127 |
|
|
|
1128 |
|
|
|
1129 |
|
|
It is possible that the completion function will be invoked before
|
1130 |
|
|
usbs_start_rx_buffer returns. Such an event would
|
1131 |
|
|
be unusual because the transfer cannot happen until the next time the
|
1132 |
|
|
host tries to send data to this peripheral, but it may happen if for
|
1133 |
|
|
example another interrupt happens and a higher priority thread is
|
1134 |
|
|
scheduled to run. Also, if the endpoint is currently halted then the
|
1135 |
|
|
completion function will be invoked immediately with
|
1136 |
|
|
-EAGAIN: typically this will happen in the current
|
1137 |
|
|
thread rather than in a separate DSR. The completion function is
|
1138 |
|
|
allowed to start another transfer immediately by calling
|
1139 |
|
|
usbs_start_rx_buffer again.
|
1140 |
|
|
|
1141 |
|
|
|
1142 |
|
|
USB device drivers are not expected to perform any locking. It is the
|
1143 |
|
|
responsibility of higher-level code to ensure that there is only one
|
1144 |
|
|
receive operation for a given endpoint in progress at any one time. If
|
1145 |
|
|
there are concurrent calls to
|
1146 |
|
|
usbs_start_rx_buffer then the resulting behaviour
|
1147 |
|
|
is undefined. For typical USB applications this does not present any
|
1148 |
|
|
problems, because only one piece of code will access a given endpoint
|
1149 |
|
|
at any particular time.
|
1150 |
|
|
|
1151 |
|
|
|
1152 |
|
|
The following code fragment illustrates a very simple use of
|
1153 |
|
|
usbs_start_rx_buffer to implement a blocking
|
1154 |
|
|
receive, using a semaphore to synchronise between the foreground
|
1155 |
|
|
thread and the DSR. For a simple example like this no completion data
|
1156 |
|
|
is needed.
|
1157 |
|
|
|
1158 |
|
|
|
1159 |
|
|
static int error_code = 0;
|
1160 |
|
|
static cyg_sem_t completion_wait;
|
1161 |
|
|
|
1162 |
|
|
static void
|
1163 |
|
|
completion_fn(void* data, int result)
|
1164 |
|
|
{
|
1165 |
|
|
error_code = result;
|
1166 |
|
|
cyg_semaphore_post(&completion_wait);
|
1167 |
|
|
}
|
1168 |
|
|
|
1169 |
|
|
int
|
1170 |
|
|
blocking_receive(usbs_rx_endpoint* ep, unsigned char* buf, int len)
|
1171 |
|
|
{
|
1172 |
|
|
error_code = 0;
|
1173 |
|
|
usbs_start_rx_buffer(ep, buf, len, &completion_fn, NULL);
|
1174 |
|
|
cyg_semaphore_wait(&completion_wait);
|
1175 |
|
|
return error_code;
|
1176 |
|
|
}
|
1177 |
|
|
|
1178 |
|
|
|
1179 |
|
|
There is also a utility function usbs_start_rx. This
|
1180 |
|
|
can be used by code that wants to manipulate
|
1181 |
|
|
linkend="usbs-data">data endpoints directly, specifically the
|
1182 |
|
|
complete_fn,
|
1183 |
|
|
complete_data,
|
1184 |
|
|
buffer and
|
1185 |
|
|
buffer_size fields.
|
1186 |
|
|
usbs_start_tx just invokes a function
|
1187 |
|
|
supplied by the device driver.
|
1188 |
|
|
|
1189 |
|
|
|
1190 |
|
|
|
1191 |
|
|
|
1192 |
|
|
|
1193 |
|
|
|
1194 |
|
|
|
1195 |
|
|
|
1196 |
|
|
|
1197 |
|
|
Sending Data to the Host
|
1198 |
|
|
|
1199 |
|
|
|
1200 |
|
|
usbs_start_tx_buffer
|
1201 |
|
|
Sending Data to the Host
|
1202 |
|
|
|
1203 |
|
|
|
1204 |
|
|
|
1205 |
|
|
|
1206 |
|
|
|
1207 |
|
|
#include <cyg/io/usb/usbs.h>
|
1208 |
|
|
|
1209 |
|
|
|
1210 |
|
|
void usbs_start_tx_buffer
|
1211 |
|
|
usbs_tx_endpoint* ep
|
1212 |
|
|
const unsigned char* buffer
|
1213 |
|
|
int length
|
1214 |
|
|
void (*)(void*,int) complete_fn
|
1215 |
|
|
void * complete_data
|
1216 |
|
|
|
1217 |
|
|
|
1218 |
|
|
|
1219 |
|
|
void usbs_start_tx
|
1220 |
|
|
usbs_tx_endpoint* ep
|
1221 |
|
|
|
1222 |
|
|
|
1223 |
|
|
|
1224 |
|
|
|
1225 |
|
|
|
1226 |
|
|
Description
|
1227 |
|
|
|
1228 |
|
|
usbs_start_tx_buffer is a USB-specific function
|
1229 |
|
|
to transfer data from peripheral to host. It can be used for bulk,
|
1230 |
|
|
interrupt or isochronous transfers, but not for control messages;
|
1231 |
|
|
instead those involve manipulating the
|
1232 |
|
|
linkend="usbs-control">usbs_control_endpoint
|
1233 |
|
|
data structure directly. The function takes five arguments:
|
1234 |
|
|
|
1235 |
|
|
|
1236 |
|
|
|
1237 |
|
|
|
1238 |
|
|
The first argument identifies the specific endpoint that should be
|
1239 |
|
|
used. Different USB devices will support different sets of endpoints
|
1240 |
|
|
and the device driver will provide appropriate data structures. The
|
1241 |
|
|
device driver's documentation should be consulted for details of which
|
1242 |
|
|
endpoints are available.
|
1243 |
|
|
|
1244 |
|
|
|
1245 |
|
|
|
1246 |
|
|
|
1247 |
|
|
The buffer and length
|
1248 |
|
|
arguments control the actual transfer. USB device drivers are not
|
1249 |
|
|
allowed to modify the buffer during the transfer, so the data can
|
1250 |
|
|
reside in read-only memory. The transfer will be for all the data
|
1251 |
|
|
specified, and it is the responsibility of higher-level code to make
|
1252 |
|
|
sure that the host is expecting this amount of data. For isochronous
|
1253 |
|
|
transfers the USB specification imposes an upper bound of 1023 bytes,
|
1254 |
|
|
but a smaller limit may be set in the
|
1255 |
|
|
linkend="usbs-enum-endpoint">enumeration data. Interrupt
|
1256 |
|
|
transfers have an upper bound of 64 bytes or less, as per the
|
1257 |
|
|
enumeration data. Bulk transfers are more complicated because they can
|
1258 |
|
|
involve multiple 64-byte packets plus a terminating packet of less
|
1259 |
|
|
than 64 bytes, so the basic USB specification does not impose an upper
|
1260 |
|
|
limit on the total transfer size. Instead it is left to higher-level
|
1261 |
|
|
protocols to specify an appropriate upper bound. If the peripheral
|
1262 |
|
|
attempts to send more data than the host is willing to accept then the
|
1263 |
|
|
resulting behaviour is undefined and may well depend on the specific
|
1264 |
|
|
host operating system being used.
|
1265 |
|
|
|
1266 |
|
|
|
1267 |
|
|
For bulk transfers, the USB device driver or the underlying hardware
|
1268 |
|
|
will automatically split the transfer up into the appropriate number
|
1269 |
|
|
of full-size 64-byte packets plus a single terminating packet, which
|
1270 |
|
|
may be 0 bytes.
|
1271 |
|
|
|
1272 |
|
|
|
1273 |
|
|
|
1274 |
|
|
|
1275 |
|
|
usbs_start_tx_buffer is non-blocking. It merely
|
1276 |
|
|
starts the transmit operation, and does not wait for completion. At
|
1277 |
|
|
some later point the USB device driver will invoke the completion
|
1278 |
|
|
function parameter with two arguments: the completion data defined by
|
1279 |
|
|
the last parameter, and a result field. This result will be either an
|
1280 |
|
|
error code < 0, or the amount of data
|
1281 |
|
|
transferred which should correspond to the
|
1282 |
|
|
length argument. The most likely errors are
|
1283 |
|
|
-EPIPE to indicate that the connection between the
|
1284 |
|
|
host and the target has been broken, and -EAGAIN
|
1285 |
|
|
for when the endpoint has been
|
1286 |
|
|
linkend="usbs-halt">halted. Specific USB device drivers may
|
1287 |
|
|
define additional error conditions.
|
1288 |
|
|
|
1289 |
|
|
|
1290 |
|
|
|
1291 |
|
|
|
1292 |
|
|
The normal sequence of events is that the USB device driver will
|
1293 |
|
|
update the appropriate hardware registers. At some point after that
|
1294 |
|
|
the host will attempt to fetch data by transmitting an IN token. Since
|
1295 |
|
|
a transmit operation is now in progress the peripheral can send a
|
1296 |
|
|
packet of data, and the host will generate an ACK. At this point the
|
1297 |
|
|
USB hardware will generate an interrupt, and the device driver will
|
1298 |
|
|
service this interrupt and arrange for a DSR to be called. Isochronous
|
1299 |
|
|
and interrupt transfers involve just a single packet. However, bulk
|
1300 |
|
|
transfers may involve multiple packets so the device driver has to
|
1301 |
|
|
check whether there is more data to send and set things up for the
|
1302 |
|
|
next packet. When the device driver DSR detects a complete transfer it
|
1303 |
|
|
will inform higher-level code by invoking the supplied completion
|
1304 |
|
|
function.
|
1305 |
|
|
|
1306 |
|
|
|
1307 |
|
|
This means that the completion function will normally be invoked by a
|
1308 |
|
|
DSR and not in thread context - although some USB device drivers may
|
1309 |
|
|
have a different implementation. Therefore the completion function is
|
1310 |
|
|
restricted in what it can do, in particular it must not make any
|
1311 |
|
|
calls that will or may block such as locking a mutex or allocating
|
1312 |
|
|
memory. The kernel documentation should be consulted for more details
|
1313 |
|
|
of DSR's and interrupt handling generally.
|
1314 |
|
|
|
1315 |
|
|
|
1316 |
|
|
It is possible that the completion function will be invoked before
|
1317 |
|
|
usbs_start_tx_buffer returns. Such an event would
|
1318 |
|
|
be unusual because the transfer cannot happen until the next time the
|
1319 |
|
|
host tries to fetch data from this peripheral, but it may happen if,
|
1320 |
|
|
for example, another interrupt happens and a higher priority thread is
|
1321 |
|
|
scheduled to run. Also, if the endpoint is currently halted then the
|
1322 |
|
|
completion function will be invoked immediately with
|
1323 |
|
|
-EAGAIN: typically this will happen in the current
|
1324 |
|
|
thread rather than in a separate DSR. The completion function is
|
1325 |
|
|
allowed to start another transfer immediately by calling
|
1326 |
|
|
usbs_start_tx_buffer again.
|
1327 |
|
|
|
1328 |
|
|
|
1329 |
|
|
USB device drivers are not expected to perform any locking. It is the
|
1330 |
|
|
responsibility of higher-level code to ensure that there is only one
|
1331 |
|
|
transmit operation for a given endpoint in progress at any one time.
|
1332 |
|
|
If there are concurrent calls to
|
1333 |
|
|
usbs_start_tx_buffer then the resulting behaviour
|
1334 |
|
|
is undefined. For typical USB applications this does not present any
|
1335 |
|
|
problems because only piece of code will access a given endpoint at
|
1336 |
|
|
any particular time.
|
1337 |
|
|
|
1338 |
|
|
|
1339 |
|
|
The following code fragment illustrates a very simple use of
|
1340 |
|
|
usbs_start_tx_buffer to implement a blocking
|
1341 |
|
|
transmit, using a semaphore to synchronise between the foreground
|
1342 |
|
|
thread and the DSR. For a simple example like this no completion data
|
1343 |
|
|
is needed.
|
1344 |
|
|
|
1345 |
|
|
|
1346 |
|
|
static int error_code = 0;
|
1347 |
|
|
static cyg_sem_t completion_wait;
|
1348 |
|
|
|
1349 |
|
|
static void
|
1350 |
|
|
completion_fn(void* data, int result)
|
1351 |
|
|
{
|
1352 |
|
|
error_code = result;
|
1353 |
|
|
cyg_semaphore_post(&completion_wait);
|
1354 |
|
|
}
|
1355 |
|
|
|
1356 |
|
|
int
|
1357 |
|
|
blocking_transmit(usbs_tx_endpoint* ep, const unsigned char* buf, int len)
|
1358 |
|
|
{
|
1359 |
|
|
error_code = 0;
|
1360 |
|
|
usbs_start_tx_buffer(ep, buf, len, &completion_fn, NULL);
|
1361 |
|
|
cyg_semaphore_wait(&completion_wait);
|
1362 |
|
|
return error_code;
|
1363 |
|
|
}
|
1364 |
|
|
|
1365 |
|
|
|
1366 |
|
|
There is also a utility function usbs_start. This
|
1367 |
|
|
can be used by code that wants to manipulate
|
1368 |
|
|
linkend="usbs-data">data endpoints directly, specifically the
|
1369 |
|
|
complete_fn,
|
1370 |
|
|
complete_data,
|
1371 |
|
|
buffer and
|
1372 |
|
|
buffer_size fields.
|
1373 |
|
|
usbs_start_tx just calls a function supplied by
|
1374 |
|
|
the device driver.
|
1375 |
|
|
|
1376 |
|
|
|
1377 |
|
|
|
1378 |
|
|
|
1379 |
|
|
|
1380 |
|
|
|
1381 |
|
|
|
1382 |
|
|
|
1383 |
|
|
|
1384 |
|
|
Halted Endpoints
|
1385 |
|
|
|
1386 |
|
|
|
1387 |
|
|
Halted Endpoints
|
1388 |
|
|
Support for Halting and Halted Endpoints
|
1389 |
|
|
|
1390 |
|
|
|
1391 |
|
|
|
1392 |
|
|
|
1393 |
|
|
|
1394 |
|
|
|
1395 |
|
|
#include <cyg/io/usb/usbs.h>
|
1396 |
|
|
|
1397 |
|
|
|
1398 |
|
|
|
1399 |
|
|
cyg_bool usbs_rx_endpoint_halted
|
1400 |
|
|
usbs_rx_endpoint* ep
|
1401 |
|
|
|
1402 |
|
|
|
1403 |
|
|
void usbs_set_rx_endpoint_halted
|
1404 |
|
|
usbs_rx_endpoint* ep
|
1405 |
|
|
cyg_bool new_state
|
1406 |
|
|
|
1407 |
|
|
|
1408 |
|
|
void usbs_start_rx_endpoint_wait
|
1409 |
|
|
usbs_rx_endpoint* ep
|
1410 |
|
|
void (*)(void*, int) complete_fn
|
1411 |
|
|
void * complete_data
|
1412 |
|
|
|
1413 |
|
|
|
1414 |
|
|
|
1415 |
|
|
cyg_bool
|
1416 |
|
|
usbs_tx_endpoint_halted
|
1417 |
|
|
usbs_tx_endpoint* ep
|
1418 |
|
|
|
1419 |
|
|
|
1420 |
|
|
void usbs_set_tx_endpoint_halted
|
1421 |
|
|
usbs_tx_endpoint* ep
|
1422 |
|
|
cyg_bool new_state
|
1423 |
|
|
|
1424 |
|
|
|
1425 |
|
|
void usbs_start_tx_endpoint_wait
|
1426 |
|
|
usbs_tx_endpoint* ep
|
1427 |
|
|
void (*)(void*, int) complete_fn
|
1428 |
|
|
void * complete_data
|
1429 |
|
|
|
1430 |
|
|
|
1431 |
|
|
|
1432 |
|
|
|
1433 |
|
|
|
1434 |
|
|
Description
|
1435 |
|
|
|
1436 |
|
|
Normal USB traffic involves straightforward handshakes, with either an
|
1437 |
|
|
ACK to indicate that a packet was transferred
|
1438 |
|
|
without errors, or a NAK if an error occurred, or
|
1439 |
|
|
if a peripheral is currently unable to process another packet from the
|
1440 |
|
|
host, or has no packet to send to the host. There is a third form of
|
1441 |
|
|
handshake, a STALL, which indicates that the
|
1442 |
|
|
endpoint is currently halted.
|
1443 |
|
|
|
1444 |
|
|
|
1445 |
|
|
When an endpoint is halted it means that the host-side code needs to
|
1446 |
|
|
take some sort of recovery action before communication over that
|
1447 |
|
|
endpoint can resume. The exact circumstances under which this can
|
1448 |
|
|
happen are not defined by the USB specification, but one example would
|
1449 |
|
|
be a protocol violation if say the peripheral attempted to transmit
|
1450 |
|
|
more data to the host than was permitted by the protocol in use. The
|
1451 |
|
|
host can use the standard control messages get-status, set-feature and
|
1452 |
|
|
clear-feature to examine and manipulate the halted status of a given
|
1453 |
|
|
endpoint. There are USB-specific functions which can be used inside
|
1454 |
|
|
the peripheral to achieve the same effect. Once an endpoint has been
|
1455 |
|
|
halted the host can then interact with the peripheral using class or
|
1456 |
|
|
vendor control messages to perform appropriate recovery, and then the
|
1457 |
|
|
halted condition can be cleared.
|
1458 |
|
|
|
1459 |
|
|
|
1460 |
|
|
Halting an endpoint does not constitute a device state change, and
|
1461 |
|
|
there is no mechanism by which higher-level code can be informed
|
1462 |
|
|
immediately. However, any ongoing receive or transmit operations will
|
1463 |
|
|
be aborted with an -EAGAIN error, and any new
|
1464 |
|
|
receives or transmits will fail immediately with the same error.
|
1465 |
|
|
|
1466 |
|
|
|
1467 |
|
|
There are six functions to support halted endpoints, one set for
|
1468 |
|
|
receive endpoints and another for transmit endpoints, with both sets
|
1469 |
|
|
behaving in essentially the same way. The first,
|
1470 |
|
|
usbs_rx_endpoint_halted, can be used to determine
|
1471 |
|
|
whether or not an endpoint is currently halted: it takes a single
|
1472 |
|
|
argument that identifies the endpoint of interest. The second
|
1473 |
|
|
function, usbs_set_rx_endpoint_halted, can be
|
1474 |
|
|
used to change the halted condition of an endpoint: it takes two
|
1475 |
|
|
arguments; one to identify the endpoint and another to specify the new
|
1476 |
|
|
state. The last function
|
1477 |
|
|
usbs_start_rx_endpoint_wait operates in much the
|
1478 |
|
|
same way as usbs_start_rx_buffer: when the
|
1479 |
|
|
endpoint is no longer halted the device driver will invoke the
|
1480 |
|
|
supplied completion function with a status of 0. The completion
|
1481 |
|
|
function has the same signature as that for a transfer operation.
|
1482 |
|
|
Often it will be possible to use a single completion function and have
|
1483 |
|
|
the foreground code invoke either
|
1484 |
|
|
usbs_start_rx_buffer or
|
1485 |
|
|
usbs_start_rx_endpoint_wait depending on the
|
1486 |
|
|
current state of the endpoint.
|
1487 |
|
|
|
1488 |
|
|
|
1489 |
|
|
|
1490 |
|
|
|
1491 |
|
|
|
1492 |
|
|
|
1493 |
|
|
|
1494 |
|
|
|
1495 |
|
|
|
1496 |
|
|
Control Endpoints
|
1497 |
|
|
|
1498 |
|
|
|
1499 |
|
|
Control Endpoints
|
1500 |
|
|
Control endpoint data structure
|
1501 |
|
|
|
1502 |
|
|
|
1503 |
|
|
|
1504 |
|
|
|
1505 |
|
|
#include <cyg/io/usb/usbs.h>
|
1506 |
|
|
|
1507 |
|
|
typedef struct usbs_control_endpoint {
|
1508 |
|
|
*hellip;
|
1509 |
|
|
} usbs_control_endpoint;
|
1510 |
|
|
|
1511 |
|
|
|
1512 |
|
|
|
1513 |
|
|
usbs_control_endpoint Data Structure
|
1514 |
|
|
|
1515 |
|
|
The device driver for a USB slave device should supply one
|
1516 |
|
|
usbs_control_endpoint data structure per USB
|
1517 |
|
|
device. This corresponds to endpoint 0 which will be used for all
|
1518 |
|
|
control message interaction between the host and that device. The data
|
1519 |
|
|
structure is also used for internal management purposes, for example
|
1520 |
|
|
to keep track of the current state. In a typical USB peripheral there
|
1521 |
|
|
will only be one such data structure in the entire system, but if
|
1522 |
|
|
there are multiple USB slave ports, allowing the peripheral to be
|
1523 |
|
|
connected to multiple hosts, then there will be a separate data
|
1524 |
|
|
structure for each one. The name or names of the data structures are
|
1525 |
|
|
determined by the device drivers. For example, the SA11x0 USB device
|
1526 |
|
|
driver package provides usbs_sa11x0_ep0.
|
1527 |
|
|
|
1528 |
|
|
|
1529 |
|
|
The operations on a control endpoint do not fit cleanly into a
|
1530 |
|
|
conventional open/read/write I/O model. For example, when the host
|
1531 |
|
|
sends a control message to the USB peripheral this may be one of four
|
1532 |
|
|
types: standard, class, vendor and reserved. Some or all of the
|
1533 |
|
|
standard control messages will be handled automatically by the common
|
1534 |
|
|
USB slave package or by the device driver itself. Other standard
|
1535 |
|
|
control messages and the other types of control messages may be
|
1536 |
|
|
handled by a class-specific package or by application code. Although
|
1537 |
|
|
it would be possible to have devtab entries such as
|
1538 |
|
|
/dev/usbs_ep0/standard and
|
1539 |
|
|
/dev/usbs_ep0/class, and then support read and
|
1540 |
|
|
write operations on these devtab entries, this would add significant
|
1541 |
|
|
overhead and code complexity. Instead, all of the fields in the
|
1542 |
|
|
control endpoint data structure are public and can be manipulated
|
1543 |
|
|
directly by higher level code if and when required.
|
1544 |
|
|
|
1545 |
|
|
|
1546 |
|
|
Control endpoints involve a number of callback functions, with
|
1547 |
|
|
higher-level code installing suitable function pointers in the control
|
1548 |
|
|
endpoint data structure. For example, if the peripheral involves
|
1549 |
|
|
vendor-specific control messages then a suitable handler for these
|
1550 |
|
|
messages should be installed. Although the exact details depend on the
|
1551 |
|
|
device driver, typically these callback functions will be invoked at
|
1552 |
|
|
DSR level rather than thread level. Therefore, only certain eCos
|
1553 |
|
|
functions can be invoked; specifically, those functions that are
|
1554 |
|
|
guaranteed not to block. If a potentially blocking function such as a
|
1555 |
|
|
semaphore wait or a mutex lock operation is invoked from inside the
|
1556 |
|
|
callback then the resulting behaviour is undefined, and the system as
|
1557 |
|
|
a whole may fail. In addition, if one of the callback functions
|
1558 |
|
|
involves significant processing effort then this may adversely affect
|
1559 |
|
|
the system's real time characteristics. The eCos kernel documentation
|
1560 |
|
|
should be consulted for more details of DSR handling.
|
1561 |
|
|
|
1562 |
|
|
|
1563 |
|
|
Initialization
|
1564 |
|
|
|
1565 |
|
|
The usbs_control_endpoint data structure
|
1566 |
|
|
contains the following fields related to initialization.
|
1567 |
|
|
|
1568 |
|
|
|
1569 |
|
|
typedef struct usbs_control_endpoint {
|
1570 |
|
|
…
|
1571 |
|
|
const usbs_enumeration_data* enumeration_data;
|
1572 |
|
|
void (*start_fn)(usbs_control_endpoint*);
|
1573 |
|
|
…
|
1574 |
|
|
};
|
1575 |
|
|
|
1576 |
|
|
|
1577 |
|
|
It is the responsibility of higher-level code, usually the
|
1578 |
|
|
application, to define the USB enumeration data. This needs to be
|
1579 |
|
|
installed in the control endpoint data structure early on during
|
1580 |
|
|
system startup, before the USB device is actually started and any
|
1581 |
|
|
interaction with the host is possible. Details of the enumeration data
|
1582 |
|
|
are supplied in the section USB Enumeration
|
1583 |
|
|
Data. Typically, the enumeration data is constant for a given
|
1584 |
|
|
peripheral, although it can be constructed dynamically if necessary.
|
1585 |
|
|
However, the enumeration data cannot change while the peripheral is
|
1586 |
|
|
connected to a host: the peripheral cannot easily claim to be a
|
1587 |
|
|
keyboard one second and a printer the next.
|
1588 |
|
|
|
1589 |
|
|
|
1590 |
|
|
The start_fn member is normally accessed
|
1591 |
|
|
via the utility
|
1592 |
|
|
linkend="usbs-start">usbs_start rather
|
1593 |
|
|
than directly. It is provided by the device driver and should be
|
1594 |
|
|
invoked once the system is fully initialized and interaction with the
|
1595 |
|
|
host is possible. A typical implementation would change the USB data
|
1596 |
|
|
pins from tristated to active. If the peripheral is already plugged
|
1597 |
|
|
into a host then the latter should detect this change and start
|
1598 |
|
|
interacting with the peripheral, including requesting the enumeration
|
1599 |
|
|
data.
|
1600 |
|
|
|
1601 |
|
|
|
1602 |
|
|
|
1603 |
|
|
State
|
1604 |
|
|
|
1605 |
|
|
There are three usbs_control_endpoint fields
|
1606 |
|
|
related to the current state of a USB slave device, plus some state
|
1607 |
|
|
constants and an enumeration of the possible state changes:
|
1608 |
|
|
|
1609 |
|
|
|
1610 |
|
|
typedef struct usbs_control_endpoint {
|
1611 |
|
|
…
|
1612 |
|
|
int state;
|
1613 |
|
|
void (*state_change_fn)(struct usbs_control_endpoint*, void*,
|
1614 |
|
|
usbs_state_change, int);
|
1615 |
|
|
void* state_change_data;
|
1616 |
|
|
…
|
1617 |
|
|
};
|
1618 |
|
|
|
1619 |
|
|
#define USBS_STATE_DETACHED 0x01
|
1620 |
|
|
#define USBS_STATE_ATTACHED 0x02
|
1621 |
|
|
#define USBS_STATE_POWERED 0x03
|
1622 |
|
|
#define USBS_STATE_DEFAULT 0x04
|
1623 |
|
|
#define USBS_STATE_ADDRESSED 0x05
|
1624 |
|
|
#define USBS_STATE_CONFIGURED 0x06
|
1625 |
|
|
#define USBS_STATE_MASK 0x7F
|
1626 |
|
|
#define USBS_STATE_SUSPENDED (1 << 7)
|
1627 |
|
|
|
1628 |
|
|
typedef enum {
|
1629 |
|
|
USBS_STATE_CHANGE_DETACHED = 1,
|
1630 |
|
|
USBS_STATE_CHANGE_ATTACHED = 2,
|
1631 |
|
|
USBS_STATE_CHANGE_POWERED = 3,
|
1632 |
|
|
USBS_STATE_CHANGE_RESET = 4,
|
1633 |
|
|
USBS_STATE_CHANGE_ADDRESSED = 5,
|
1634 |
|
|
USBS_STATE_CHANGE_CONFIGURED = 6,
|
1635 |
|
|
USBS_STATE_CHANGE_DECONFIGURED = 7,
|
1636 |
|
|
USBS_STATE_CHANGE_SUSPENDED = 8,
|
1637 |
|
|
USBS_STATE_CHANGE_RESUMED = 9
|
1638 |
|
|
} usbs_state_change;
|
1639 |
|
|
|
1640 |
|
|
|
1641 |
|
|
The USB standard defines a number of states for a given USB
|
1642 |
|
|
peripheral. The initial state is detached, where
|
1643 |
|
|
the peripheral is either not connected to a host at all or, from the
|
1644 |
|
|
host's perspective, the peripheral has not started up yet because the
|
1645 |
|
|
relevant pins are tristated. The peripheral then moves via
|
1646 |
|
|
intermediate attached and
|
1647 |
|
|
powered states to its default or
|
1648 |
|
|
reset state, at which point the host and
|
1649 |
|
|
peripheral can actually start exchanging data. The first message is
|
1650 |
|
|
from host to peripheral and provides a unique 7-bit address within the
|
1651 |
|
|
local USB network, resulting in a state change to
|
1652 |
|
|
addressed. The host then requests enumeration
|
1653 |
|
|
data and performs other initialization. If everything succeeds the
|
1654 |
|
|
host sends a standard set-configuration control message, after which
|
1655 |
|
|
the peripheral is configured and expected to be
|
1656 |
|
|
up and running. Note that some USB device drivers may be unable to
|
1657 |
|
|
distinguish between the detached,
|
1658 |
|
|
attached and powered states
|
1659 |
|
|
but generally this is not important to higher-level code.
|
1660 |
|
|
|
1661 |
|
|
|
1662 |
|
|
A USB host should generate at least one token every millisecond. If a
|
1663 |
|
|
peripheral fails to detect any USB traffic for a period of time then
|
1664 |
|
|
typically this indicates that the host has entered a power-saving
|
1665 |
|
|
mode, and the peripheral should do the same if possible. This
|
1666 |
|
|
corresponds to the suspended bit. The actual
|
1667 |
|
|
state is a combination of suspended and the
|
1668 |
|
|
previous state, for example configured and
|
1669 |
|
|
suspended rather than just
|
1670 |
|
|
suspended. When the peripheral subsequently
|
1671 |
|
|
detects USB traffic it would switch back to the
|
1672 |
|
|
configured state.
|
1673 |
|
|
|
1674 |
|
|
|
1675 |
|
|
The USB device driver and the common USB slave package will maintain
|
1676 |
|
|
the current state in the control endpoint's
|
1677 |
|
|
state field. There should be no need for
|
1678 |
|
|
any other code to change this field, but it can be examined whenever
|
1679 |
|
|
appropriate. In addition whenever a state change occurs the generic
|
1680 |
|
|
code can invoke a state change callback function. By default, no such
|
1681 |
|
|
callback function will be installed. Some class-specific packages such
|
1682 |
|
|
as the USB-ethernet package will install a suitable function to keep
|
1683 |
|
|
track of whether or not the host-peripheral connection is up, that is
|
1684 |
|
|
whether or not ethernet packets can be exchanged. Application code can
|
1685 |
|
|
also update this field. If multiple parties want to be informed of
|
1686 |
|
|
state changes, for example both a class-specific package and
|
1687 |
|
|
application code, then typically the application code will install its
|
1688 |
|
|
state change handler after the class-specific package and is
|
1689 |
|
|
responsible for chaining into the package's handler.
|
1690 |
|
|
|
1691 |
|
|
|
1692 |
|
|
The state change callback function is invoked with four arguments. The
|
1693 |
|
|
first identifies the control endpoint. The second is an arbitrary
|
1694 |
|
|
pointer: higher-level code can fill in the
|
1695 |
|
|
state_change_data field to set this. The
|
1696 |
|
|
third argument specifies the state change that has occurred, and the
|
1697 |
|
|
last argument supplies the previous state (the new state is readily
|
1698 |
|
|
available from the control endpoint structure).
|
1699 |
|
|
|
1700 |
|
|
|
1701 |
|
|
eCos does not provide any utility functions for updating or examining
|
1702 |
|
|
the state_change_fn or
|
1703 |
|
|
state_change_data fields. Instead, it is
|
1704 |
|
|
expected that the fields in the
|
1705 |
|
|
usbs_control_endpoint data structure will be
|
1706 |
|
|
manipulated directly. Any utility functions would do just this, but
|
1707 |
|
|
at the cost of increased code and cpu overheads.
|
1708 |
|
|
|
1709 |
|
|
|
1710 |
|
|
|
1711 |
|
|
Standard Control Messages
|
1712 |
|
|
|
1713 |
|
|
typedef struct usbs_control_endpoint {
|
1714 |
|
|
…
|
1715 |
|
|
unsigned char control_buffer[8];
|
1716 |
|
|
usbs_control_return (*standard_control_fn)(struct usbs_control_endpoint*, void*);
|
1717 |
|
|
void* standard_control_data;
|
1718 |
|
|
…
|
1719 |
|
|
} usbs_control_endpoint;
|
1720 |
|
|
|
1721 |
|
|
typedef enum {
|
1722 |
|
|
USBS_CONTROL_RETURN_HANDLED = 0,
|
1723 |
|
|
USBS_CONTROL_RETURN_UNKNOWN = 1,
|
1724 |
|
|
USBS_CONTROL_RETURN_STALL = 2
|
1725 |
|
|
} usbs_control_return;
|
1726 |
|
|
|
1727 |
|
|
extern usbs_control_return usbs_handle_standard_control(struct usbs_control_endpoint*);
|
1728 |
|
|
|
1729 |
|
|
|
1730 |
|
|
When a USB peripheral is connected to the host it must always respond
|
1731 |
|
|
to control messages sent to endpoint 0. Control messages always
|
1732 |
|
|
consist of an initial eight-byte header, containing fields such as a
|
1733 |
|
|
request type. This may be followed by a further data transfer, either
|
1734 |
|
|
from host to peripheral or from peripheral to host. The way this is
|
1735 |
|
|
handled is described in the
|
1736 |
|
|
linkend="usbs-control-buffer">Buffer Management section below.
|
1737 |
|
|
|
1738 |
|
|
|
1739 |
|
|
The USB device driver will always accept the initial eight-byte
|
1740 |
|
|
header, storing it in the control_buffer
|
1741 |
|
|
field. Then it determines the request type: standard, class, vendor,
|
1742 |
|
|
or reserved. The way in which the last three of these are processed is
|
1743 |
|
|
described in the section Other
|
1744 |
|
|
Control Messages. Some
|
1745 |
|
|
standard control messages will be handled by the device driver itself;
|
1746 |
|
|
typically the set-address request and the
|
1747 |
|
|
get-status, set-feature and
|
1748 |
|
|
clear-feature requests when applied to endpoints.
|
1749 |
|
|
|
1750 |
|
|
|
1751 |
|
|
If a standard control message cannot be handled by the device driver
|
1752 |
|
|
itself, the driver checks the
|
1753 |
|
|
standard_control_fn field in the control
|
1754 |
|
|
endpoint data structure. If higher-level code has installed a suitable
|
1755 |
|
|
callback function then this will be invoked with two argument, the
|
1756 |
|
|
control endpoint data structure itself and the
|
1757 |
|
|
standard_control_data field. The latter
|
1758 |
|
|
allows the higher level code to associate arbitrary data with the
|
1759 |
|
|
control endpoint. The callback function can return one of three
|
1760 |
|
|
values: HANDLED to indicate that the request has
|
1761 |
|
|
been processed; UNKNOWN if the message should be
|
1762 |
|
|
handled by the default code; or STALL to indicate
|
1763 |
|
|
an error condition. If higher level code has not installed a callback
|
1764 |
|
|
function or if the callback function has returned
|
1765 |
|
|
UNKNOWN then the device driver will invoke a
|
1766 |
|
|
default handler, usbs_handle_standard_control
|
1767 |
|
|
provided by the common USB slave package.
|
1768 |
|
|
|
1769 |
|
|
|
1770 |
|
|
The default handler can cope with all of the standard control messages
|
1771 |
|
|
for a simple USB peripheral. However, if the peripheral involves
|
1772 |
|
|
multiple configurations, multiple interfaces in a configuration, or
|
1773 |
|
|
alternate settings for an interface, then this cannot be handled by
|
1774 |
|
|
generic code. For example, a multimedia peripheral may support various
|
1775 |
|
|
alternate settings for a given data source with different bandwidth
|
1776 |
|
|
requirements, and the host can select a setting that takes into
|
1777 |
|
|
account the current load. Clearly higher-level code needs to be aware
|
1778 |
|
|
when the host changes the current setting, so that it can adjust the
|
1779 |
|
|
rate at which data is fed to or retrieved from the host. Therefore the
|
1780 |
|
|
higher-level code needs to install its own standard control callback
|
1781 |
|
|
and process appropriate messages, rather than leaving these to the
|
1782 |
|
|
default handler.
|
1783 |
|
|
|
1784 |
|
|
|
1785 |
|
|
The default handler will take care of the
|
1786 |
|
|
get-descriptor request used to obtain the
|
1787 |
|
|
enumeration data. It has support for string descriptors but ignores
|
1788 |
|
|
language encoding issues. If language encoding is important for the
|
1789 |
|
|
peripheral then this will have to be handled by an
|
1790 |
|
|
application-specific standard control handler.
|
1791 |
|
|
|
1792 |
|
|
|
1793 |
|
|
The header file
|
1794 |
|
|
class="headerfile"><cyg/io/usb/usb.h> defines various
|
1795 |
|
|
constants related to control messages, for example the function codes
|
1796 |
|
|
corresponding to the standard request types. This header file is
|
1797 |
|
|
provided by the common USB package, not by the USB slave package,
|
1798 |
|
|
since the information is also relevant to USB hosts.
|
1799 |
|
|
|
1800 |
|
|
|
1801 |
|
|
|
1802 |
|
|
Other Control Messages
|
1803 |
|
|
|
1804 |
|
|
typedef struct usbs_control_endpoint {
|
1805 |
|
|
…
|
1806 |
|
|
usbs_control_return (*class_control_fn)(struct usbs_control_endpoint*, void*);
|
1807 |
|
|
void* class_control_data;
|
1808 |
|
|
usbs_control_return (*vendor_control_fn)(struct usbs_control_endpoint*, void*);
|
1809 |
|
|
void* vendor_control_data;
|
1810 |
|
|
usbs_control_return (*reserved_control_fn)(struct usbs_control_endpoint*, void*);
|
1811 |
|
|
void* reserved_control_data;
|
1812 |
|
|
…
|
1813 |
|
|
} usbs_control_endpoint;
|
1814 |
|
|
|
1815 |
|
|
|
1816 |
|
|
Non-standard control messages always have to be processed by
|
1817 |
|
|
higher-level code. This could be class-specific packages. For example,
|
1818 |
|
|
the USB-ethernet package will handle requests for getting the MAC
|
1819 |
|
|
address and for enabling or disabling promiscuous mode. In all cases
|
1820 |
|
|
the device driver will store the initial request in the
|
1821 |
|
|
control_buffer field, check for an
|
1822 |
|
|
appropriate handler, and invoke it with details of the control
|
1823 |
|
|
endpoint and any handler-specific data that has been installed
|
1824 |
|
|
alongside the handler itself. The handler should return either
|
1825 |
|
|
USBS_CONTROL_RETURN_HANDLED to report success or
|
1826 |
|
|
USBS_CONTROL_RETURN_STALL to report failure. The
|
1827 |
|
|
device driver will report this to the host.
|
1828 |
|
|
|
1829 |
|
|
|
1830 |
|
|
If there are multiple parties interested in a particular type of
|
1831 |
|
|
control messages, it is the responsibility of application code to
|
1832 |
|
|
install an appropriate handler and process the requests appropriately.
|
1833 |
|
|
|
1834 |
|
|
|
1835 |
|
|
|
1836 |
|
|
Buffer Management
|
1837 |
|
|
|
1838 |
|
|
typedef struct usbs_control_endpoint {
|
1839 |
|
|
…
|
1840 |
|
|
unsigned char* buffer;
|
1841 |
|
|
int buffer_size;
|
1842 |
|
|
void (*fill_buffer_fn)(struct usbs_control_endpoint*);
|
1843 |
|
|
void* fill_data;
|
1844 |
|
|
int fill_index;
|
1845 |
|
|
usbs_control_return (*complete_fn)(struct usbs_control_endpoint*, int);
|
1846 |
|
|
…
|
1847 |
|
|
} usbs_control_endpoint;
|
1848 |
|
|
|
1849 |
|
|
|
1850 |
|
|
Many USB control messages involve transferring more data than just the
|
1851 |
|
|
initial eight-byte header. The header indicates the direction of the
|
1852 |
|
|
transfer, OUT for host to peripheral or IN for peripheral to host.
|
1853 |
|
|
It also specifies a length field, which is exact for an OUT transfer
|
1854 |
|
|
or an upper bound for an IN transfer. Control message handlers can
|
1855 |
|
|
manipulate six fields within the control endpoint data structure to
|
1856 |
|
|
ensure that the transfer happens correctly.
|
1857 |
|
|
|
1858 |
|
|
|
1859 |
|
|
For an OUT transfer, the handler should examine the length field in
|
1860 |
|
|
the header and provide a single buffer for all the data. A
|
1861 |
|
|
class-specific protocol would typically impose an upper bound on the
|
1862 |
|
|
amount of data, allowing the buffer to be allocated statically.
|
1863 |
|
|
The handler should update the buffer and
|
1864 |
|
|
complete_fn fields. When all the data has
|
1865 |
|
|
been transferred the completion callback will be invoked, and its
|
1866 |
|
|
return value determines the response sent back to the host. The USB
|
1867 |
|
|
standard allows for a new control message to be sent before the
|
1868 |
|
|
current transfer has completed, effectively cancelling the current
|
1869 |
|
|
operation. When this happens the completion function will also be
|
1870 |
|
|
invoked. The second argument to the completion function specifies what
|
1871 |
|
|
has happened, with a value of 0 indicating success and an error code
|
1872 |
|
|
such as -EPIPE or -EIO
|
1873 |
|
|
indicating that the current transfer has been cancelled.
|
1874 |
|
|
|
1875 |
|
|
|
1876 |
|
|
IN transfers are a little bit more complicated. The required
|
1877 |
|
|
information, for example the enumeration data, may not be in a single
|
1878 |
|
|
contiguous buffer. Instead a mechanism is provided by which the buffer
|
1879 |
|
|
can be refilled, thus allowing the transfer to move from one record to
|
1880 |
|
|
the next. Essentially, the transfer operates as follows:
|
1881 |
|
|
|
1882 |
|
|
|
1883 |
|
|
|
1884 |
|
|
|
1885 |
|
|
When the host requests another chunk of data (typically eight bytes),
|
1886 |
|
|
the USB device driver will examine the
|
1887 |
|
|
buffer_size field. If non-zero then
|
1888 |
|
|
buffer contains at least one more byte of
|
1889 |
|
|
data, and then buffer_size is decremented.
|
1890 |
|
|
|
1891 |
|
|
|
1892 |
|
|
|
1893 |
|
|
|
1894 |
|
|
When buffer_size has dropped to 0, the
|
1895 |
|
|
fill_buffer_fn field will be examined. If
|
1896 |
|
|
non-null it will be invoked to refill the buffer.
|
1897 |
|
|
|
1898 |
|
|
|
1899 |
|
|
|
1900 |
|
|
|
1901 |
|
|
The fill_data and
|
1902 |
|
|
fill_index fields are not used by the
|
1903 |
|
|
device driver. Instead these fields are available to the refill
|
1904 |
|
|
function to keep track of the current state of the transfer.
|
1905 |
|
|
|
1906 |
|
|
|
1907 |
|
|
|
1908 |
|
|
|
1909 |
|
|
When buffer_size is 0 and
|
1910 |
|
|
fill_buffer_fn is NULL, no more data is
|
1911 |
|
|
available and the transfer has completed.
|
1912 |
|
|
|
1913 |
|
|
|
1914 |
|
|
|
1915 |
|
|
|
1916 |
|
|
Optionally a completion function can be installed. This will be
|
1917 |
|
|
invoked with 0 if the transfer completes successfully, or with an
|
1918 |
|
|
error code if the transfer is cancelled because of another control
|
1919 |
|
|
messsage.
|
1920 |
|
|
|
1921 |
|
|
|
1922 |
|
|
|
1923 |
|
|
|
1924 |
|
|
If the requested data is contiguous then the only fields that need
|
1925 |
|
|
to be manipulated are buffer and
|
1926 |
|
|
buffer_size, and optionally
|
1927 |
|
|
complete_fn. If the requested data is not
|
1928 |
|
|
contiguous then the initial control message handler should update
|
1929 |
|
|
fill_buffer_fn and some or all of the other
|
1930 |
|
|
fields, as required. An example of this is the handling of the
|
1931 |
|
|
standard get-descriptor control message by
|
1932 |
|
|
usbs_handle_standard_control.
|
1933 |
|
|
|
1934 |
|
|
|
1935 |
|
|
|
1936 |
|
|
Polling Support
|
1937 |
|
|
|
1938 |
|
|
typedef struct usbs_control_endpoint {
|
1939 |
|
|
void (*poll_fn)(struct usbs_control_endpoint*);
|
1940 |
|
|
int interrupt_vector;
|
1941 |
|
|
…
|
1942 |
|
|
} usbs_control_endpoint;
|
1943 |
|
|
|
1944 |
|
|
|
1945 |
|
|
In nearly all circumstances USB I/O should be interrupt-driven.
|
1946 |
|
|
However, there are special environments such as RedBoot where polled
|
1947 |
|
|
operation may be appropriate. If the device driver can operate in
|
1948 |
|
|
polled mode then it will provide a suitable function via the
|
1949 |
|
|
poll_fn field, and higher-level code can
|
1950 |
|
|
invoke this regularly. This polling function will take care of all
|
1951 |
|
|
endpoints associated with the device, not just the control endpoint.
|
1952 |
|
|
If the USB hardware involves a single interrupt vector then this will
|
1953 |
|
|
be identified in the data structure as well.
|
1954 |
|
|
|
1955 |
|
|
|
1956 |
|
|
|
1957 |
|
|
|
1958 |
|
|
|
1959 |
|
|
|
1960 |
|
|
|
1961 |
|
|
|
1962 |
|
|
|
1963 |
|
|
|
1964 |
|
|
|
1965 |
|
|
|
1966 |
|
|
Data Endpoints
|
1967 |
|
|
|
1968 |
|
|
|
1969 |
|
|
Data Endpoints
|
1970 |
|
|
Data endpoint data structures
|
1971 |
|
|
|
1972 |
|
|
|
1973 |
|
|
|
1974 |
|
|
|
1975 |
|
|
#include <cyg/io/usb/usbs.h>
|
1976 |
|
|
|
1977 |
|
|
typedef struct usbs_rx_endpoint {
|
1978 |
|
|
void (*start_rx_fn)(struct usbs_rx_endpoint*);
|
1979 |
|
|
void (*set_halted_fn)(struct usbs_rx_endpoint*, cyg_bool);
|
1980 |
|
|
void (*complete_fn)(void*, int);
|
1981 |
|
|
void* complete_data;
|
1982 |
|
|
unsigned char* buffer;
|
1983 |
|
|
int buffer_size;
|
1984 |
|
|
cyg_bool halted;
|
1985 |
|
|
} usbs_rx_endpoint;
|
1986 |
|
|
|
1987 |
|
|
typedef struct usbs_tx_endpoint {
|
1988 |
|
|
void (*start_tx_fn)(struct usbs_tx_endpoint*);
|
1989 |
|
|
void (*set_halted_fn)(struct usbs_tx_endpoint*, cyg_bool);
|
1990 |
|
|
void (*complete_fn)(void*, int);
|
1991 |
|
|
void* complete_data;
|
1992 |
|
|
const unsigned char* buffer;
|
1993 |
|
|
int buffer_size;
|
1994 |
|
|
cyg_bool halted;
|
1995 |
|
|
} usbs_tx_endpoint;
|
1996 |
|
|
|
1997 |
|
|
|
1998 |
|
|
|
1999 |
|
|
Receive and Transmit Data Structures
|
2000 |
|
|
|
2001 |
|
|
In addition to a single usbs_control_endpoint
|
2002 |
|
|
data structure per USB slave device, the USB device driver should also
|
2003 |
|
|
provide receive and transmit data structures corresponding to the
|
2004 |
|
|
other endpoints. The names of these are determined by the device
|
2005 |
|
|
driver. For example, the SA1110 USB device driver package provides
|
2006 |
|
|
usbs_sa11x0_ep1 for receives and
|
2007 |
|
|
usbs_sa11x0_ep2 for transmits.
|
2008 |
|
|
|
2009 |
|
|
|
2010 |
|
|
Unlike control endpoints, the common USB slave package does provide a
|
2011 |
|
|
number of utility routines to manipulate data endpoints. For example
|
2012 |
|
|
|
2013 |
|
|
linkend="usbs-start-rx">usbs_start_rx_buffer
|
2014 |
|
|
can be used to receive data from the host into a buffer. In addition
|
2015 |
|
|
the USB device driver can provide devtab entries such as
|
2016 |
|
|
/dev/usbs1r and /dev/usbs2w, so
|
2017 |
|
|
higher-level code can open these devices and then
|
2018 |
|
|
perform blocking read and
|
2019 |
|
|
write operations.
|
2020 |
|
|
|
2021 |
|
|
|
2022 |
|
|
However, the operation of data endpoints and the various
|
2023 |
|
|
endpoint-related functions is relatively straightforward. First
|
2024 |
|
|
consider a usbs_rx_endpoint structure. The
|
2025 |
|
|
device driver will provide the members
|
2026 |
|
|
start_rx_fn and
|
2027 |
|
|
set_halted_fn, and it will maintain the
|
2028 |
|
|
halted field. To receive data, higher-level
|
2029 |
|
|
code sets the buffer,
|
2030 |
|
|
buffer_size,
|
2031 |
|
|
complete_fn and optionally the
|
2032 |
|
|
complete_data fields. Next the
|
2033 |
|
|
start_rx_fn member should be called. When
|
2034 |
|
|
the transfer has finished the device driver will invoke the completion
|
2035 |
|
|
function, using complete_data as the first
|
2036 |
|
|
argument and a size field for the second argument. A negative size
|
2037 |
|
|
indicates an error of some sort: -EGAIN indicates
|
2038 |
|
|
that the endpoint has been halted, usually at the request of the host;
|
2039 |
|
|
-EPIPE indicates that the connection between the
|
2040 |
|
|
host and the peripheral has been broken. Certain device drivers may
|
2041 |
|
|
generate other error codes.
|
2042 |
|
|
|
2043 |
|
|
|
2044 |
|
|
If higher-level code needs to halt or unhalt an endpoint then it can
|
2045 |
|
|
invoke the set_halted_fn member. When an
|
2046 |
|
|
endpoint is halted, invoking start_rx_fn
|
2047 |
|
|
wit buffer_size set to 0 indicates that
|
2048 |
|
|
higher-level code wants to block until the endpoint is no longer
|
2049 |
|
|
halted; at that point the completion function will be invoked.
|
2050 |
|
|
|
2051 |
|
|
|
2052 |
|
|
USB device drivers are allowed to assume that higher-level protocols
|
2053 |
|
|
ensure that host and peripheral agree on the amount of data that will
|
2054 |
|
|
be transferred, or at least on an upper bound. Therefore there is no
|
2055 |
|
|
need for the device driver to maintain its own buffers, and copy
|
2056 |
|
|
operations are avoided. If the host sends more data than expected then
|
2057 |
|
|
the resulting behaviour is undefined.
|
2058 |
|
|
|
2059 |
|
|
|
2060 |
|
|
Transmit endpoints work in essentially the same way as receive
|
2061 |
|
|
endpoints. Higher-level code should set the
|
2062 |
|
|
buffer and
|
2063 |
|
|
buffer_size fields to point at the data to
|
2064 |
|
|
be transferred, then call start_tx_fn, and
|
2065 |
|
|
the device driver will invoked the completion function when the
|
2066 |
|
|
transfer has completed.
|
2067 |
|
|
|
2068 |
|
|
|
2069 |
|
|
USB device drivers are not expected to perform any locking. If at any
|
2070 |
|
|
time there are two concurrent receive operations for a given endpoint,
|
2071 |
|
|
or two concurrent transmit operations, then the resulting behaviour is
|
2072 |
|
|
undefined. It is the responsibility of higher-level code to perform
|
2073 |
|
|
any synchronisation that may be necessary. In practice, conflicts are
|
2074 |
|
|
unlikely because typically a given endpoint will only be accessed
|
2075 |
|
|
sequentially by just one part of the overall system.
|
2076 |
|
|
|
2077 |
|
|
|
2078 |
|
|
|
2079 |
|
|
|
2080 |
|
|
|
2081 |
|
|
|
2082 |
|
|
|
2083 |
|
|
|
2084 |
|
|
|
2085 |
|
|
|
2086 |
|
|
|
2087 |
|
|
Writing a USB Device Driver
|
2088 |
|
|
|
2089 |
|
|
|
2090 |
|
|
Writing a USB Device Driver
|
2091 |
|
|
USB Device Driver Porting Guide
|
2092 |
|
|
|
2093 |
|
|
|
2094 |
|
|
Introduction
|
2095 |
|
|
|
2096 |
|
|
Often the best way to write a USB device driver will be to start with
|
2097 |
|
|
an existing one and modify it as necessary. The information given here
|
2098 |
|
|
is intended primarily as an outline rather than as a complete guide.
|
2099 |
|
|
|
2100 |
|
|
|
2101 |
|
|
|
2102 |
|
|
At the time of writing only one USB device driver has been
|
2103 |
|
|
implemented. Hence it is possible, perhaps probable, that some
|
2104 |
|
|
portability issues have not yet been addressed. One issue
|
2105 |
|
|
involves the different types of transfer, for example the initial
|
2106 |
|
|
target hardware had no support for isochronous or interrupt transfers,
|
2107 |
|
|
so additional functionality may be needed to switch between transfer
|
2108 |
|
|
types. Another issue would be hardware where a given endpoint number,
|
2109 |
|
|
say endpoint 1, could be used for either receiving or transmitting
|
2110 |
|
|
data, but not both because a single fifo is used. Issues like these
|
2111 |
|
|
will have to be resolved as and when additional USB device drivers are
|
2112 |
|
|
written.
|
2113 |
|
|
|
2114 |
|
|
|
2115 |
|
|
|
2116 |
|
|
|
2117 |
|
|
The Control Endpoint
|
2118 |
|
|
|
2119 |
|
|
A USB device driver should provide a single
|
2120 |
|
|
linkend="usbs-control">usbs_control_endpoint
|
2121 |
|
|
data structure for every USB device. Typical peripherals will have
|
2122 |
|
|
only one USB port so there will be just one such data structure in the
|
2123 |
|
|
entire system, but theoretically it is possible to have multiple USB
|
2124 |
|
|
devices. These may all involve the same chip, in which case a single
|
2125 |
|
|
device driver should support multiple device instances, or they may
|
2126 |
|
|
involve different chips. The name or names of these data structures
|
2127 |
|
|
are determined by the device driver, but appropriate care should be
|
2128 |
|
|
taken to avoid name clashes.
|
2129 |
|
|
|
2130 |
|
|
|
2131 |
|
|
A USB device cannot be used unless the control endpoint data structure
|
2132 |
|
|
exists. However, the presence of USB hardware in the target processor
|
2133 |
|
|
or board does not guarantee that the application will necessarily want
|
2134 |
|
|
to use that hardware. To avoid unwanted code or data overheads, the
|
2135 |
|
|
device driver can provide a configuration option to determine whether
|
2136 |
|
|
or not the endpoint 0 data structure is actually provided. A default
|
2137 |
|
|
value of CYGINT_IO_USB_SLAVE_CLIENTS ensures that
|
2138 |
|
|
the USB driver will be enabled automatically if higher-level code does
|
2139 |
|
|
require USB support, while leaving ultimate control to the user.
|
2140 |
|
|
|
2141 |
|
|
|
2142 |
|
|
The USB device driver is responsible for filling in the
|
2143 |
|
|
start_fn,
|
2144 |
|
|
poll_fn and
|
2145 |
|
|
interrupt_vector fields. Usually this can
|
2146 |
|
|
be achieved by static initialization. The driver is also largely
|
2147 |
|
|
responsible for maintaining the state
|
2148 |
|
|
field. The control_buffer array should be
|
2149 |
|
|
used to hold the first packet of a control message. The
|
2150 |
|
|
buffer and other fields related to data
|
2151 |
|
|
transfers will be managed
|
2152 |
|
|
linkend="usbs-control-buffer">jointly by higher-level code and
|
2153 |
|
|
the device driver. The remaining fields are generally filled in by
|
2154 |
|
|
higher-level code, although the driver should initialize them to NULL
|
2155 |
|
|
values.
|
2156 |
|
|
|
2157 |
|
|
|
2158 |
|
|
Hardware permitting, the USB device should be inactive until the
|
2159 |
|
|
start_fn is invoked, for example by
|
2160 |
|
|
tristating the appropriate pins. This prevents the host from
|
2161 |
|
|
interacting with the peripheral before all other parts of the system
|
2162 |
|
|
have initialized. It is expected that the
|
2163 |
|
|
start_fn will only be invoked once, shortly
|
2164 |
|
|
after power-up.
|
2165 |
|
|
|
2166 |
|
|
|
2167 |
|
|
Where possible the device driver should detect state changes, such as
|
2168 |
|
|
when the connection between host and peripheral is established, and
|
2169 |
|
|
report these to higher-level
|
2170 |
|
|
code via the state_change_fn callback, if
|
2171 |
|
|
any. The state change to and from configured state cannot easily be
|
2172 |
|
|
handled by the device driver itself, instead higher-level code such as
|
2173 |
|
|
the common USB slave package will take care of this.
|
2174 |
|
|
|
2175 |
|
|
|
2176 |
|
|
Once the connection between host and peripheral has been established,
|
2177 |
|
|
the peripheral must be ready to accept control messages at all times,
|
2178 |
|
|
and must respond to these within certain time constraints. For
|
2179 |
|
|
example, the standard set-address control message must be handled
|
2180 |
|
|
within 50ms. The USB specification provides more information on these
|
2181 |
|
|
constraints. The device driver is responsible for receiving the
|
2182 |
|
|
initial packet of a control message. This packet will always be eight
|
2183 |
|
|
bytes and should be stored in the
|
2184 |
|
|
control_buffer field. Certain standard
|
2185 |
|
|
control messages should be detected and handled by the device driver
|
2186 |
|
|
itself. The most important is set-address, but usually the get-status,
|
2187 |
|
|
set-feature and clear-feature requests when applied to halted
|
2188 |
|
|
endpoints should also be handled by the driver. Other standard control
|
2189 |
|
|
messages should first be passed on to the
|
2190 |
|
|
standard_control_fn callback (if any), and
|
2191 |
|
|
finally to the default handler
|
2192 |
|
|
usbs_handle_standard_control provided by the
|
2193 |
|
|
common USB slave package. Class, vendor and reserved control messages
|
2194 |
|
|
should always be dispatched to the appropriate callback and there is
|
2195 |
|
|
no default handler for these.
|
2196 |
|
|
|
2197 |
|
|
|
2198 |
|
|
Some control messages will involve further data transfer, not just the
|
2199 |
|
|
initial packet. The device driver must handle this in accordance with
|
2200 |
|
|
the USB specification and the
|
2201 |
|
|
linkend="usbs-control-buffer">buffer management strategy. The
|
2202 |
|
|
driver is also responsible for keeping track of whether or not the
|
2203 |
|
|
control operation has succeeded and generating an ACK or STALL
|
2204 |
|
|
handshake.
|
2205 |
|
|
|
2206 |
|
|
|
2207 |
|
|
The polling support is optional and may not be feasible on all
|
2208 |
|
|
hardware. It is only used in certain specialised environments such as
|
2209 |
|
|
RedBoot. A typical implementation of the polling function would just
|
2210 |
|
|
check whether or not an interrupt would have occurred and, if so, call
|
2211 |
|
|
the same code that the interrupt handler would.
|
2212 |
|
|
|
2213 |
|
|
|
2214 |
|
|
|
2215 |
|
|
Data Endpoints
|
2216 |
|
|
|
2217 |
|
|
In addition to the control endpoint data structure, a USB device
|
2218 |
|
|
driver should also provide appropriate data
|
2219 |
|
|
endpoint data structures. Obviously this is only relevant if
|
2220 |
|
|
the USB support generally is desired, that is if the control endpoint is
|
2221 |
|
|
provided. In addition, higher-level code may not require all the
|
2222 |
|
|
endpoints, so it may be useful to provide configuration options that
|
2223 |
|
|
control the presence of each endpoint. For example, the intended
|
2224 |
|
|
application might only involve a single transmit endpoint and of
|
2225 |
|
|
course control messages, so supporting receive endpoints might waste
|
2226 |
|
|
memory.
|
2227 |
|
|
|
2228 |
|
|
|
2229 |
|
|
Conceptually, data endpoints are much simpler than the control
|
2230 |
|
|
endpoint. The device driver has to supply two functions, one for
|
2231 |
|
|
data transfers and another to control the halted condition. These
|
2232 |
|
|
implement the functionality for
|
2233 |
|
|
usbs_start_rx_buffer,
|
2234 |
|
|
usbs_start_tx_buffer,
|
2235 |
|
|
usbs_set_rx_endpoint_halted and
|
2236 |
|
|
usbs_set_tx_endpoint_halted.
|
2237 |
|
|
The device driver is also responsible for maintaining the
|
2238 |
|
|
halted status.
|
2239 |
|
|
|
2240 |
|
|
|
2241 |
|
|
For data transfers, higher-level code will have filled in the
|
2242 |
|
|
buffer,
|
2243 |
|
|
buffer_size,
|
2244 |
|
|
complete_fn and
|
2245 |
|
|
complete_data fields. The transfer function
|
2246 |
|
|
should arrange for the transfer to start, allowing the host to send or
|
2247 |
|
|
receive packets. Typically this will result in an interrupt at the end
|
2248 |
|
|
of the transfer or after each packet. Once the entire transfer has
|
2249 |
|
|
been completed, the driver's interrupt handling code should invoke the
|
2250 |
|
|
completion function. This can happen either in DSR context or thread
|
2251 |
|
|
context, depending on the driver's implementation. There are a number
|
2252 |
|
|
of special cases to consider. If the endpoint is halted when the
|
2253 |
|
|
transfer is started then the completion function can be invoked
|
2254 |
|
|
immediately with -EAGAIN. If the transfer cannot be
|
2255 |
|
|
completed because the connection is broken then the completion
|
2256 |
|
|
function should be invoked with -EPIPE. If the
|
2257 |
|
|
endpoint is stalled during the transfer, either because of a standard
|
2258 |
|
|
control message or because higher-level code calls the appropriate
|
2259 |
|
|
set_halted_fn, then again the completion
|
2260 |
|
|
function should be invoked with -EAGAIN. Finally,
|
2261 |
|
|
the <usbs_start_rx_endpoint_wait and
|
2262 |
|
|
usbs_start_tx_endpoint_wait functions involve
|
2263 |
|
|
calling the device driver's data transfer function with a buffer size
|
2264 |
|
|
of 0 bytes.
|
2265 |
|
|
|
2266 |
|
|
|
2267 |
|
|
Giving a buffer size of 0 bytes a special meaning is problematical
|
2268 |
|
|
because it prevents transfers of that size. Such transfers are allowed
|
2269 |
|
|
by the USB protocol, consisting of just headers and acknowledgements
|
2270 |
|
|
and an empty data phase, although rarely useful. A future modification
|
2271 |
|
|
of the device driver specification will address this issue, although
|
2272 |
|
|
care has to be taken that the functionality remains accessible through
|
2273 |
|
|
devtab entries as well as via low-level accesses.
|
2274 |
|
|
|
2275 |
|
|
|
2276 |
|
|
|
2277 |
|
|
Devtab Entries
|
2278 |
|
|
|
2279 |
|
|
For some applications or higher-level packages it may be more
|
2280 |
|
|
convenient to use traditional open/read/write I/O calls rather than
|
2281 |
|
|
the non-blocking USB I/O calls. To support this the device driver can
|
2282 |
|
|
provide a devtab entry for each endpoint, for example:
|
2283 |
|
|
|
2284 |
|
|
|
2285 |
|
|
#ifdef CYGVAR_DEVS_USB_SA11X0_EP1_DEVTAB_ENTRY
|
2286 |
|
|
|
2287 |
|
|
static CHAR_DEVIO_TABLE(usbs_sa11x0_ep1_devtab_functions,
|
2288 |
|
|
&cyg_devio_cwrite,
|
2289 |
|
|
&usbs_devtab_cread,
|
2290 |
|
|
&cyg_devio_bwrite,
|
2291 |
|
|
&cyg_devio_bread,
|
2292 |
|
|
&cyg_devio_select,
|
2293 |
|
|
&cyg_devio_get_config,
|
2294 |
|
|
&cyg_devio_set_config);
|
2295 |
|
|
|
2296 |
|
|
static CHAR_DEVTAB_ENTRY(usbs_sa11x0_ep1_devtab_entry,
|
2297 |
|
|
CYGDAT_DEVS_USB_SA11X0_DEVTAB_BASENAME "1r",
|
2298 |
|
|
0,
|
2299 |
|
|
&usbs_sa11x0_ep1_devtab_functions,
|
2300 |
|
|
&usbs_sa11x0_devtab_dummy_init,
|
2301 |
|
|
0,
|
2302 |
|
|
(void*) &usbs_sa11x0_ep1);
|
2303 |
|
|
#endif
|
2304 |
|
|
|
2305 |
|
|
|
2306 |
|
|
Again care must be taken to avoid name clashes. This can be achieved
|
2307 |
|
|
by having a configuration option to control the base name, with a
|
2308 |
|
|
default value of e.g. /dev/usbs, and appending an
|
2309 |
|
|
endpoint-specific string. This gives the application developer
|
2310 |
|
|
sufficient control to eliminate any name clashes. The common USB slave
|
2311 |
|
|
package provides functions usbs_devtab_cwrite and
|
2312 |
|
|
usbs_devtab_cread, which can be used in the
|
2313 |
|
|
function tables for transmit and receive endpoints respectively. The
|
2314 |
|
|
private field priv of the devtab entry
|
2315 |
|
|
should be a pointer to the underlying endpoint data structure.
|
2316 |
|
|
|
2317 |
|
|
|
2318 |
|
|
Because devtab entries are never accessed directly, only indirectly,
|
2319 |
|
|
they would usually be eliminated by the linker. To avoid this the
|
2320 |
|
|
devtab entries should normally be defined in a separate source file
|
2321 |
|
|
which ends up the special library libextras.a
|
2322 |
|
|
rather than in the default library libtarget.a.
|
2323 |
|
|
|
2324 |
|
|
|
2325 |
|
|
Not all applications or higher-level packages will want to use the
|
2326 |
|
|
devtab entries and the blocking I/O facilities. It may be appropriate
|
2327 |
|
|
for the device driver to provide additional configuration options that
|
2328 |
|
|
control whether or not any or all of the devtab entries should be
|
2329 |
|
|
provided, to avoid unnecessary memory overheads.
|
2330 |
|
|
|
2331 |
|
|
|
2332 |
|
|
|
2333 |
|
|
Interrupt Handling
|
2334 |
|
|
|
2335 |
|
|
A typical USB device driver will need to service interrupts for all of
|
2336 |
|
|
the endpoints and possibly for additional USB events such as entering
|
2337 |
|
|
or leaving suspended mode. Usually these interrupts need not be
|
2338 |
|
|
serviced directly by the ISR. Instead, they can be left to a DSR. If
|
2339 |
|
|
the peripheral is not able to accept or send another packet just yet,
|
2340 |
|
|
the hardware will generate a NAK and the host will just retry a little
|
2341 |
|
|
bit later. If high throughput is required then it may be desirable to
|
2342 |
|
|
handle the bulk transfer protocol largely at ISR level, that is take
|
2343 |
|
|
care of each packet in the ISR and only activate the DSR once the
|
2344 |
|
|
whole transfer has completed.
|
2345 |
|
|
|
2346 |
|
|
|
2347 |
|
|
Control messages may involve invoking arbitrary callback functions in
|
2348 |
|
|
higher-level code. This should normally happen at DSR level. Doing it
|
2349 |
|
|
at ISR level could seriously affect the system's interrupt latency and
|
2350 |
|
|
impose unacceptable constraints on what operations can be performed by
|
2351 |
|
|
those callbacks. If the device driver requires a thread anyway then it
|
2352 |
|
|
may be appropriate to use this thread for invoking the callbacks, but
|
2353 |
|
|
usually it is not worthwhile to add a new thread to the system just
|
2354 |
|
|
for this; higher-level code is expected to write callbacks that
|
2355 |
|
|
function sensibly at DSR level. Much the same applies to the
|
2356 |
|
|
completion functions associated with data transfers. These should also
|
2357 |
|
|
be invoked at DSR or thread level.
|
2358 |
|
|
|
2359 |
|
|
|
2360 |
|
|
|
2361 |
|
|
Support for USB Testing
|
2362 |
|
|
|
2363 |
|
|
Optionally a USB device driver can provide support for the
|
2364 |
|
|
USB test software. This requires
|
2365 |
|
|
defining a number of additional data structures, allowing the
|
2366 |
|
|
generic test code to work out just what the hardware is capable of and
|
2367 |
|
|
hence what testing can be performed.
|
2368 |
|
|
|
2369 |
|
|
|
2370 |
|
|
The key data structure is
|
2371 |
|
|
usbs_testing_endpoint, defined in
|
2372 |
|
|
class="headerfile">cyg/io/usb/usbs.h. In addition some
|
2373 |
|
|
commonly required constants are provided by the common USB package in
|
2374 |
|
|
. One
|
2375 |
|
|
usbs_testing_endpoint structure should be
|
2376 |
|
|
defined for each supported endpoint. The following fields need to be
|
2377 |
|
|
filled in:
|
2378 |
|
|
|
2379 |
|
|
|
2380 |
|
|
|
2381 |
|
|
endpoint_type
|
2382 |
|
|
|
2383 |
|
|
This specifies the type of endpoint and should be one of
|
2384 |
|
|
USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL,
|
2385 |
|
|
BULK, ISOCHRONOUS or
|
2386 |
|
|
INTERRUPT.
|
2387 |
|
|
|
2388 |
|
|
|
2389 |
|
|
|
2390 |
|
|
endpoint_number
|
2391 |
|
|
|
2392 |
|
|
This identifies the number that should be used by the host
|
2393 |
|
|
to address this endpoint. For a control endpoint it should
|
2394 |
|
|
be 0. For other types of endpoints it should be between
|
2395 |
|
|
1 and 15.
|
2396 |
|
|
|
2397 |
|
|
|
2398 |
|
|
|
2399 |
|
|
endpoint_direction
|
2400 |
|
|
|
2401 |
|
|
For control endpoints this field is irrelevant. For other
|
2402 |
|
|
types of endpoint it should be either
|
2403 |
|
|
USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN or
|
2404 |
|
|
USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT. If a given
|
2405 |
|
|
endpoint number can be used for traffic in both directions then
|
2406 |
|
|
there should be two entries in the array, one for each direction.
|
2407 |
|
|
|
2408 |
|
|
|
2409 |
|
|
|
2410 |
|
|
endpoint
|
2411 |
|
|
|
2412 |
|
|
This should be a pointer to the appropriate
|
2413 |
|
|
usbs_control_endpoint,
|
2414 |
|
|
usbs_rx_endpoint or
|
2415 |
|
|
usbs_tx_endpoint structure, allowing the
|
2416 |
|
|
generic testing code to perform low-level I/O.
|
2417 |
|
|
|
2418 |
|
|
|
2419 |
|
|
|
2420 |
|
|
devtab_entry
|
2421 |
|
|
|
2422 |
|
|
If the endpoint also has an entry in the system's device table then
|
2423 |
|
|
this field should give the corresponding string, for example
|
2424 |
|
|
"/dev/usbs1r". This allows the
|
2425 |
|
|
generic testing code to access the device via higher-level
|
2426 |
|
|
calls like open and read.
|
2427 |
|
|
|
2428 |
|
|
|
2429 |
|
|
|
2430 |
|
|
min_size
|
2431 |
|
|
|
2432 |
|
|
This indicates the smallest transfer size that the hardware can
|
2433 |
|
|
support on this endpoint. Typically this will be one.
|
2434 |
|
|
|
2435 |
|
|
|
2436 |
|
|
Strictly speaking a minimum size of one is not quite right since it
|
2437 |
|
|
is valid for a USB transfer to involve zero bytes, in other words a
|
2438 |
|
|
transfer that involves just headers and acknowledgements and an
|
2439 |
|
|
empty data phase, and that should be tested as well. However current
|
2440 |
|
|
device drivers interpret a transfer size of 0 as special, so that
|
2441 |
|
|
would have to be resolved first.
|
2442 |
|
|
|
2443 |
|
|
|
2444 |
|
|
|
2445 |
|
|
|
2446 |
|
|
max_size
|
2447 |
|
|
|
2448 |
|
|
Similarly, this specifies the largest transfer size. For control
|
2449 |
|
|
endpoints the USB protocol uses only two bytes to hold the transfer
|
2450 |
|
|
length, so there is an upper bound of 65535 bytes. In practice
|
2451 |
|
|
it is very unlikely that any control transfers would ever need to
|
2452 |
|
|
be this large, and in fact such transfers would take a long time
|
2453 |
|
|
and probably violate timing constraints. For other types of endpoint
|
2454 |
|
|
any of the protocol, the hardware, or the device driver may impose
|
2455 |
|
|
size limits. For example a given device driver might be unable to
|
2456 |
|
|
cope with transfers larger than 65535 bytes. If it should be
|
2457 |
|
|
possible to transfer arbitrary amounts of data then a value of
|
2458 |
|
|
-1 indicates no upper limit, and transfer
|
2459 |
|
|
sizes will be limited by available memory and by the capabilities
|
2460 |
|
|
of the host machine.
|
2461 |
|
|
|
2462 |
|
|
|
2463 |
|
|
|
2464 |
|
|
max_in_padding
|
2465 |
|
|
|
2466 |
|
|
This field is needed on some hardware where it is impossible to
|
2467 |
|
|
send packets of a certain size. For example the hardware may be
|
2468 |
|
|
incapable of sending an empty bulk packet to terminate a transfer
|
2469 |
|
|
that is an exact multiple of the 64-byte bulk packet size.
|
2470 |
|
|
Instead the driver has to do some padding and send an extra byte,
|
2471 |
|
|
and the host has to be prepared to receive this extra byte. Such a
|
2472 |
|
|
driver should specify a value of 1 for the
|
2473 |
|
|
padding field. For most drivers this field should be set to
|
2474 |
|
|
0.
|
2475 |
|
|
|
2476 |
|
|
|
2477 |
|
|
A better solution would be for the device driver to supply a
|
2478 |
|
|
fragment of Tcl code that would adjust the receive buffer size
|
2479 |
|
|
only when necessary, rather than for every transfer. Forcing
|
2480 |
|
|
receive padding on all transfers when only certain transfers
|
2481 |
|
|
will actually be padded reduces the accuracy of certain tests.
|
2482 |
|
|
|
2483 |
|
|
|
2484 |
|
|
|
2485 |
|
|
alignment
|
2486 |
|
|
|
2487 |
|
|
On some hardware data transfers may need to be aligned to certain
|
2488 |
|
|
boundaries, for example a word boundary or a cacheline boundary.
|
2489 |
|
|
Although in theory device drivers could hide such alignment
|
2490 |
|
|
restrictions from higher-level code by having their own buffers and
|
2491 |
|
|
performing appropriate copying, that would be expensive in terms of
|
2492 |
|
|
both memory and cpu cycles. Instead the generic testing code will
|
2493 |
|
|
align any buffers passed to the device driver to the specified
|
2494 |
|
|
boundary. For example, if the driver requires that buffers be
|
2495 |
|
|
aligned to a word boundary then it should specify an alignment
|
2496 |
|
|
value of 4.
|
2497 |
|
|
|
2498 |
|
|
|
2499 |
|
|
|
2500 |
|
|
|
2501 |
|
|
|
2502 |
|
|
The device driver should provide an array of these structures
|
2503 |
|
|
usbs_testing_endpoints[]. The USB testing code
|
2504 |
|
|
examines this array and uses the information to perform appropriate
|
2505 |
|
|
tests. Because different USB devices support different numbers of
|
2506 |
|
|
endpoints the number of entries in the array is not known in advance,
|
2507 |
|
|
so instead the testing code looks for a special terminator
|
2508 |
|
|
USBS_TESTING_ENDPOINTS_TERMINATOR. An example
|
2509 |
|
|
array, showing just the control endpoint and the terminator, might
|
2510 |
|
|
look like this:
|
2511 |
|
|
|
2512 |
|
|
|
2513 |
|
|
usbs_testing_endpoint usbs_testing_endpoints[] = {
|
2514 |
|
|
{
|
2515 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL,
|
2516 |
|
|
endpoint_number : 0,
|
2517 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
|
2518 |
|
|
endpoint : (void*) &ep0.common,
|
2519 |
|
|
devtab_entry : (const char*) 0,
|
2520 |
|
|
min_size : 1,
|
2521 |
|
|
max_size : 0x0FFFF,
|
2522 |
|
|
max_in_padding : 0,
|
2523 |
|
|
alignment : 0
|
2524 |
|
|
},
|
2525 |
|
|
…,
|
2526 |
|
|
USBS_TESTING_ENDPOINTS_TERMINATOR
|
2527 |
|
|
};
|
2528 |
|
|
|
2529 |
|
|
|
2530 |
|
|
|
2531 |
|
|
|
2532 |
|
|
The use of a single array usbs_testing_endpoints
|
2533 |
|
|
limits USB testing to platforms with a single USB device: if there
|
2534 |
|
|
were multiple devices, each defining their own instance of this array,
|
2535 |
|
|
then there would a collision at link time. In practice this should not
|
2536 |
|
|
be a major problem since typical USB peripherals only interact with a
|
2537 |
|
|
single host machine via a single slave port. In addition, even if a
|
2538 |
|
|
peripheral did have multiple slave ports the current USB testing code
|
2539 |
|
|
would not support this since it would not know which port to use.
|
2540 |
|
|
|
2541 |
|
|
|
2542 |
|
|
|
2543 |
|
|
|
2544 |
|
|
|
2545 |
|
|
|
2546 |
|
|
|
2547 |
|
|
|
2548 |
|
|
|
2549 |
|
|
|
2550 |
|
|
|
2551 |
|
|
|
2552 |
|
|
Testing
|
2553 |
|
|
|
2554 |
|
|
|
2555 |
|
|
Testing
|
2556 |
|
|
Testing of USB Device Drivers
|
2557 |
|
|
|
2558 |
|
|
|
2559 |
|
|
Introduction
|
2560 |
|
|
|
2561 |
|
|
The support for USB testing provided by the eCos USB common slave
|
2562 |
|
|
package is somewhat different in nature from the kind of testing used
|
2563 |
|
|
in many other packages. One obvious problem is that USB tests cannot
|
2564 |
|
|
be run on just a bare target platform: instead the target platform
|
2565 |
|
|
must be connected to a suitable USB host machine, and that host
|
2566 |
|
|
machine must be running appropriate software for the test code to
|
2567 |
|
|
interact with. This is very different from say a kernel test which
|
2568 |
|
|
typically will have no external dependencies. Another important
|
2569 |
|
|
difference between USB testing and say a C library
|
2570 |
|
|
strcmp test is sensitivity to timing and to
|
2571 |
|
|
hardware boundary conditions: although a simple test case that just
|
2572 |
|
|
performs a small number of USB transfers is better than no testing at
|
2573 |
|
|
all, it should also be possible to run tests for hours or days on end,
|
2574 |
|
|
under a variety of loads. In order to provide the required
|
2575 |
|
|
functionality the basic architecture of the USB testing support is as
|
2576 |
|
|
follows:
|
2577 |
|
|
|
2578 |
|
|
|
2579 |
|
|
|
2580 |
|
|
There is a single target-side program
|
2581 |
|
|
usbtarget. By default when this is run
|
2582 |
|
|
on a target platform it will appear to do nothing. In fact it is
|
2583 |
|
|
waiting to be contacted by another program
|
2584 |
|
|
usbhost which will tell it what test or
|
2585 |
|
|
tests to run. usbtarget provides
|
2586 |
|
|
mechanisms for running a wide range of tests.
|
2587 |
|
|
|
2588 |
|
|
|
2589 |
|
|
usbtarget is a generic program, but USB
|
2590 |
|
|
testing depends to some extent on the functionality provided by the
|
2591 |
|
|
hardware. For example there is no point in testing bulk transmits
|
2592 |
|
|
to endpoint 12 if the target hardware does not support an endpoint
|
2593 |
|
|
12. Therefore each USB device driver should supply information about
|
2594 |
|
|
what the hardware is actually capable of, in the form of an array of
|
2595 |
|
|
usbs_testing_endpoint data structures.
|
2596 |
|
|
|
2597 |
|
|
|
2598 |
|
|
There is a single host-side program
|
2599 |
|
|
usbhost, which acts as a counterpart to
|
2600 |
|
|
usbtarget. Again
|
2601 |
|
|
usbhost has no built-in knowledge of
|
2602 |
|
|
the test or tests that are supposed to run, it only provides
|
2603 |
|
|
mechanisms for running a wide range of tests. On start-up
|
2604 |
|
|
usbhost will search the USB bus for
|
2605 |
|
|
hardware running the target-side program, specifically a USB device
|
2606 |
|
|
that identifies itself as the product "Red Hat eCos
|
2607 |
|
|
USB test".
|
2608 |
|
|
|
2609 |
|
|
|
2610 |
|
|
usbhost contains a Tcl interpreter, and
|
2611 |
|
|
will execute any Tcl scripts specified on the command line
|
2612 |
|
|
together with appropriate arguments. The Tcl interpreter has been
|
2613 |
|
|
extended with various commands such as
|
2614 |
|
|
usbtest::bulktest, so the script can perform
|
2615 |
|
|
the desired test or tests.
|
2616 |
|
|
|
2617 |
|
|
|
2618 |
|
|
Adding a new test simply involves writing a short Tcl script that
|
2619 |
|
|
invokes the appropriate USB-specific commands. Running multiple
|
2620 |
|
|
tests involves passing appropriate arguments to
|
2621 |
|
|
usbhost, or alternatively writing a
|
2622 |
|
|
single script that just invokes other scripts.
|
2623 |
|
|
|
2624 |
|
|
|
2625 |
|
|
|
2626 |
|
|
The current implementation of usbhost
|
2627 |
|
|
depends heavily on functionality provided by the Linux kernel and in
|
2628 |
|
|
particular the usbdevfs support. It uses
|
2629 |
|
|
/proc/bus/usb/devices to find out what devices
|
2630 |
|
|
are attached to the bus, and will then access the device by opening
|
2631 |
|
|
/proc/bus/usb/xxx/yyy and performing
|
2632 |
|
|
ioctl operations. This allows USB testing to take
|
2633 |
|
|
place without having to write a new host-side device driver, but
|
2634 |
|
|
getting the code working on host machines not running Linux would
|
2635 |
|
|
obviously be problematical.
|
2636 |
|
|
|
2637 |
|
|
|
2638 |
|
|
|
2639 |
|
|
Building and Running the Target-side Code
|
2640 |
|
|
|
2641 |
|
|
The target-side component of the USB testing software consists of a
|
2642 |
|
|
single program usbtarget which contains
|
2643 |
|
|
support for a range of different tests, under the control of host-side
|
2644 |
|
|
software. This program is not built by default alongside other eCos
|
2645 |
|
|
test cases since it will only operate in certain environments,
|
2646 |
|
|
specifically when the target board's connector is plugged into a Linux
|
2647 |
|
|
host, and when the appropriate host-side software has been installed
|
2648 |
|
|
on that host. Instead the user must enable a configuration option
|
2649 |
|
|
CYGBLD_IO_USB_SLAVE_USBTEST to add the program to
|
2650 |
|
|
the list of tests for the current configuration.
|
2651 |
|
|
|
2652 |
|
|
|
2653 |
|
|
Starting the usbtarget program does not
|
2654 |
|
|
require anything unusual, so it can be run in a normal
|
2655 |
|
|
gdb session just like any eCos application.
|
2656 |
|
|
After initialization the program will wait for activity from the host.
|
2657 |
|
|
Depending on the hardware, the Linux host will detect that a new USB
|
2658 |
|
|
peripheral is present on the bus either when the
|
2659 |
|
|
usbtarget initialization is complete or
|
2660 |
|
|
when the cable between target and host is connected. The host will
|
2661 |
|
|
perform the normal USB enumeration sequence and discover that the
|
2662 |
|
|
peripheral does not match any known vendor or product id and that
|
2663 |
|
|
there is no device driver for "Red Hat eCos USB
|
2664 |
|
|
test", so it will ignore the peripheral. When the
|
2665 |
|
|
usbhost program is run on the host it will
|
2666 |
|
|
connect to the target-side software, and testing can now commence.
|
2667 |
|
|
|
2668 |
|
|
|
2669 |
|
|
|
2670 |
|
|
Building and Running the Host-side Code
|
2671 |
|
|
|
2672 |
|
|
In theory the host-side software should be built when the package is
|
2673 |
|
|
installed in the component repository, and removed when a package
|
2674 |
|
|
is uninstalled. The current eCos administration tool does not provide
|
2675 |
|
|
this functionality.
|
2676 |
|
|
|
2677 |
|
|
|
2678 |
|
|
The host-side software should be built via the usual sequence of
|
2679 |
|
|
"configure/make/make install". It can only be built on a
|
2680 |
|
|
Linux host and the configure script contains an
|
2681 |
|
|
explicit test for this. Because the eCos component repository should
|
2682 |
|
|
generally be treated as a read-only resource the configure script will
|
2683 |
|
|
also prevent you from trying to build inside the source tree. Instead
|
2684 |
|
|
a separate build tree is required. Hence a typical sequence for
|
2685 |
|
|
building the host-side software would be as follows:
|
2686 |
|
|
|
2687 |
|
|
|
2688 |
|
|
$ mkdir usbhost_build
|
2689 |
|
|
$ cd usbhost_build
|
2690 |
|
|
$ <repo>packages/io/usb/slave/current/host/configure <args>
|
2691 |
|
|
$ make
|
2692 |
|
|
<output from make>
|
2693 |
|
|
$ su
|
2694 |
|
|
$ make install
|
2695 |
|
|
<output from make install>
|
2696 |
|
|
$
|
2697 |
|
|
|
2698 |
|
|
|
2699 |
|
|
|
2700 |
|
|
|
2701 |
|
|
The location of the eCos component repository should be substituted
|
2702 |
|
|
for <repo>.
|
2703 |
|
|
|
2704 |
|
|
|
2705 |
|
|
|
2706 |
|
|
|
2707 |
|
|
If the package has been obtained via CVS or anonymous CVS then the
|
2708 |
|
|
package version will be current, as per the
|
2709 |
|
|
example. If instead the package has been obtained as part of a full
|
2710 |
|
|
eCos release or as a separate .epk file then the
|
2711 |
|
|
appropriate package version should be used instead of
|
2712 |
|
|
current.
|
2713 |
|
|
|
2714 |
|
|
|
2715 |
|
|
|
2716 |
|
|
|
2717 |
|
|
The configure script takes the usual arguments such
|
2718 |
|
|
as --prefix= to specify where the executables
|
2719 |
|
|
and support files should be installed. The only other parameter that
|
2720 |
|
|
some users may wish to specify is the location of a suitable Tcl
|
2721 |
|
|
installation. By default usbhost will use
|
2722 |
|
|
the existing Tcl installation in /usr,
|
2723 |
|
|
as provided by your Linux distribution. An alternative Tcl
|
2724 |
|
|
installation can be specified using the parameter
|
2725 |
|
|
--with-tcl=, or alternatively using some
|
2726 |
|
|
combination of --with-tcl-include,
|
2727 |
|
|
--with-tcl-lib and
|
2728 |
|
|
--with-tcl-version.
|
2729 |
|
|
|
2730 |
|
|
|
2731 |
|
|
|
2732 |
|
|
|
2733 |
|
|
One of the host-side executables that gets built,
|
2734 |
|
|
usbchmod, needs to be installed with suid
|
2735 |
|
|
root privileges. Although the Linux kernel makes it possible for
|
2736 |
|
|
applications to perform low-level USB operations such as transmitting
|
2737 |
|
|
bulk packets, by default access to this functionality is restricted to
|
2738 |
|
|
programs with superuser privileges. It is undesirable to run a complex
|
2739 |
|
|
program such as usbhost with such
|
2740 |
|
|
privileges, especially since the program contains a general-purpose
|
2741 |
|
|
Tcl interpreter. Therefore when usbhost
|
2742 |
|
|
starts up and discovers that it does not have sufficient access to the
|
2743 |
|
|
appropriate entries in /proc/bus/usb,
|
2744 |
|
|
it spawns an instance of usbchmod to modify
|
2745 |
|
|
the permissions on these entries. usbchmod
|
2746 |
|
|
will only do this for a USB device "Red Hat eCos USB
|
2747 |
|
|
test", so installing this program suid root should not
|
2748 |
|
|
introduce any security problems.
|
2749 |
|
|
|
2750 |
|
|
|
2751 |
|
|
|
2752 |
|
|
|
2753 |
|
|
|
2754 |
|
|
During make install the following actions will take
|
2755 |
|
|
place:
|
2756 |
|
|
|
2757 |
|
|
|
2758 |
|
|
|
2759 |
|
|
|
2760 |
|
|
usbhost will be installed in /usr/local/bin,
|
2761 |
|
|
or some other bin directory if
|
2762 |
|
|
the default location is changed at configure-time using a
|
2763 |
|
|
--prefix= or similar option. It will be
|
2764 |
|
|
installed as the executable
|
2765 |
|
|
usbhost_<version>, for example
|
2766 |
|
|
usbhost_current, thus allowing several
|
2767 |
|
|
releases of the USB slave package to co-exist. For convenience a
|
2768 |
|
|
symbolic link from usbhost to this executable
|
2769 |
|
|
will be created, so users can just run usbhost to
|
2770 |
|
|
access the most recently-installed version.
|
2771 |
|
|
|
2772 |
|
|
|
2773 |
|
|
|
2774 |
|
|
|
2775 |
|
|
usbchmod will be installed in
|
2776 |
|
|
/usr/local/libexec/ecos/io_usb_slave_<version>.
|
2777 |
|
|
This program should only be run by usbhost,
|
2778 |
|
|
not invoked directly, so it is not placed in the bin
|
2779 |
|
|
directory. Again the presence of the package version in the directory
|
2780 |
|
|
name allows multiple releases of the package to co-exist.
|
2781 |
|
|
|
2782 |
|
|
|
2783 |
|
|
|
2784 |
|
|
|
2785 |
|
|
A Tcl script usbhost.tcl will get installed in
|
2786 |
|
|
the same directory as usbchmod. This Tcl
|
2787 |
|
|
script is loaded automatically by the
|
2788 |
|
|
usbhost executable.
|
2789 |
|
|
|
2790 |
|
|
|
2791 |
|
|
|
2792 |
|
|
|
2793 |
|
|
A number of additional Tcl scripts, for example
|
2794 |
|
|
list.tcl will get installed alongside
|
2795 |
|
|
usbhost.tcl. These correspond to various test
|
2796 |
|
|
cases provided as standard. If a given test case is specified on the
|
2797 |
|
|
command line and cannot be found relative to the current directory
|
2798 |
|
|
then usbhost will search the install
|
2799 |
|
|
directory for these test cases.
|
2800 |
|
|
|
2801 |
|
|
|
2802 |
|
|
Strictly speaking installing the usbhost.tcl and
|
2803 |
|
|
other Tcl scripts below the libexec
|
2804 |
|
|
directory deviates from standard practice: they are
|
2805 |
|
|
architecture-independent data files so should be installed below
|
2806 |
|
|
the share subdirectory. In
|
2807 |
|
|
practice the files are sufficiently small that there is no point in
|
2808 |
|
|
sharing them, and keeping them below libexec
|
2809 |
|
|
simplifies the host-side software somewhat.
|
2810 |
|
|
|
2811 |
|
|
|
2812 |
|
|
|
2813 |
|
|
|
2814 |
|
|
|
2815 |
|
|
The usbhost should be run only when there is a
|
2816 |
|
|
suitable target attached to the USB bus and running the
|
2817 |
|
|
usbtarget program. It will search
|
2818 |
|
|
/proc/bus/usb/devices for an entry corresponding
|
2819 |
|
|
to this program, invoke usbchmod if
|
2820 |
|
|
necessary to change the access rights, and then interact with
|
2821 |
|
|
usbtarget over the USB bus.
|
2822 |
|
|
usbhost should be invoked as follows:
|
2823 |
|
|
|
2824 |
|
|
|
2825 |
|
|
$ usbhost [-v|--version] [-h|--help] [-V|--verbose] <test> [<test parameters>]
|
2826 |
|
|
|
2827 |
|
|
|
2828 |
|
|
|
2829 |
|
|
|
2830 |
|
|
The -v or --version
|
2831 |
|
|
option will display version information for
|
2832 |
|
|
usbhost including the version of the USB
|
2833 |
|
|
slave package that was used to build the executable.
|
2834 |
|
|
|
2835 |
|
|
|
2836 |
|
|
|
2837 |
|
|
|
2838 |
|
|
The -h or --help option
|
2839 |
|
|
will display usage information.
|
2840 |
|
|
|
2841 |
|
|
|
2842 |
|
|
|
2843 |
|
|
|
2844 |
|
|
The -V or --verbose
|
2845 |
|
|
option can be used to obtain more information at run-time, for example
|
2846 |
|
|
some output for every USB transfer. This option can be repeated
|
2847 |
|
|
multiple times to increase the amount of output.
|
2848 |
|
|
|
2849 |
|
|
|
2850 |
|
|
|
2851 |
|
|
|
2852 |
|
|
The first argument that does not begin with a hyphen specifies a test
|
2853 |
|
|
that should be run, in the form of a Tcl script. For example an
|
2854 |
|
|
argument of list.tcl will cause
|
2855 |
|
|
usbhost to look for a script with that
|
2856 |
|
|
name, adding a .tcl suffix if necessarary, and
|
2857 |
|
|
run that script. usbhost will look in the
|
2858 |
|
|
current directory first, then in the install tree for standard test
|
2859 |
|
|
scripts provided by the USB slave package.
|
2860 |
|
|
|
2861 |
|
|
|
2862 |
|
|
|
2863 |
|
|
|
2864 |
|
|
Some test scripts may want their own parameters, for example a
|
2865 |
|
|
duration in seconds. These can be passed on the command line after
|
2866 |
|
|
the name of the test, for example
|
2867 |
|
|
usbhost mytest 60.
|
2868 |
|
|
|
2869 |
|
|
|
2870 |
|
|
|
2871 |
|
|
|
2872 |
|
|
|
2873 |
|
|
Writing a Test
|
2874 |
|
|
|
2875 |
|
|
Each test is defined by a Tcl script, running inside an interpreter
|
2876 |
|
|
provided by usbhost. In addition to the
|
2877 |
|
|
normal Tcl functionality this interpreter provides a number of
|
2878 |
|
|
variables and functions related to USB testing. For example there is a
|
2879 |
|
|
variable bulk_in_endpoints that lists all the
|
2880 |
|
|
endpoints on the target that can perform bulk IN operations, and a
|
2881 |
|
|
related array bulk_in which contains information
|
2882 |
|
|
such as the minimum and maximum packets sizes. There is a function
|
2883 |
|
|
bulktest which can be used to perform bulk tests
|
2884 |
|
|
on a particular endpoint. A simple test script aimed at specific
|
2885 |
|
|
hardware could ignore the information variables since it would know
|
2886 |
|
|
exactly what USB hardware is available on the target, whereas a
|
2887 |
|
|
general-purpose script would use the information to adapt to the
|
2888 |
|
|
hardware capabilities.
|
2889 |
|
|
|
2890 |
|
|
|
2891 |
|
|
To avoid namespace pollution all USB-related Tcl variables and
|
2892 |
|
|
functions live in the usbtest:: namespace.
|
2893 |
|
|
Therefore accessing requires either explicitly including the
|
2894 |
|
|
namespace any references, for example
|
2895 |
|
|
$usbtest::bulk_in_endpoints, or by using Tcl's
|
2896 |
|
|
namespace import facility.
|
2897 |
|
|
|
2898 |
|
|
|
2899 |
|
|
A very simple test script might look like this:
|
2900 |
|
|
|
2901 |
|
|
|
2902 |
|
|
usbtest::bulktest 1 out 4000
|
2903 |
|
|
usbtest::bulktest 2 in 4000
|
2904 |
|
|
if { [usbtest::start 60] } {
|
2905 |
|
|
puts "Test successful"
|
2906 |
|
|
} else
|
2907 |
|
|
puts "Test failed"
|
2908 |
|
|
foreach result $usbtest::results {
|
2909 |
|
|
puts $result
|
2910 |
|
|
}
|
2911 |
|
|
}
|
2912 |
|
|
|
2913 |
|
|
|
2914 |
|
|
This would perform a test run involving 4000 bulk transfers from the
|
2915 |
|
|
host to the target's endpoint 1, and concurrently 4000 bulk transfers
|
2916 |
|
|
from endpoint 2. Default settings for packet sizes, contents, and
|
2917 |
|
|
delays would be used. The actual test would not start running until
|
2918 |
|
|
usbtest is invoked, and it is expected that the
|
2919 |
|
|
test would complete within 60 seconds. If any failures occur then they
|
2920 |
|
|
are reported.
|
2921 |
|
|
|
2922 |
|
|
|
2923 |
|
|
|
2924 |
|
|
Available Hardware
|
2925 |
|
|
|
2926 |
|
|
Each target-side USB device driver provides information about the
|
2927 |
|
|
actual capabilities of the hardware, for example which endpoints are
|
2928 |
|
|
available. Strictly speaking it provides information about what is
|
2929 |
|
|
actually supported by the device driver, which may be a subset of what
|
2930 |
|
|
the hardware is capable of. For example, the hardware may support
|
2931 |
|
|
isochronous transfers on a particular endpoint but if there is no
|
2932 |
|
|
software support for this in the driver then this endpoint will not be
|
2933 |
|
|
listed. When usbhost first contacts the
|
2934 |
|
|
usbtarget program running on the target
|
2935 |
|
|
platform, it obtains this information and makes it available to test
|
2936 |
|
|
scripts via Tcl variables:
|
2937 |
|
|
|
2938 |
|
|
|
2939 |
|
|
|
2940 |
|
|
bulk_in_endpoints
|
2941 |
|
|
|
2942 |
|
|
This is a simple list of the endpoints which can support bulk IN
|
2943 |
|
|
transfers. For example if the target-side hardware supports
|
2944 |
|
|
these transfers on endpoints 3 and 5 then the value would be
|
2945 |
|
|
"3 5" Typical test scripts would
|
2946 |
|
|
iterate over the list using something like:
|
2947 |
|
|
|
2948 |
|
|
|
2949 |
|
|
if { 0 != [llength $usbtest::bulk_in_endpoints] } {
|
2950 |
|
|
puts"Bulk IN endpoints: $usbtest::bulk_in_endpoints"
|
2951 |
|
|
foreach endpoint $usbtest:bulk_in_endpoints {
|
2952 |
|
|
…
|
2953 |
|
|
}
|
2954 |
|
|
}
|
2955 |
|
|
|
2956 |
|
|
|
2957 |
|
|
|
2958 |
|
|
|
2959 |
|
|
bulk_in()
|
2960 |
|
|
|
2961 |
|
|
This array holds additional information about each bulk IN endpoint.
|
2962 |
|
|
The array is indexed by two fields, the endpoint number and one of
|
2963 |
|
|
min_size, max_size,
|
2964 |
|
|
max_in_padding and devtab:
|
2965 |
|
|
|
2966 |
|
|
|
2967 |
|
|
|
2968 |
|
|
min_size
|
2969 |
|
|
|
2970 |
|
|
This field specifies a lower bound on the size of bulk transfers,
|
2971 |
|
|
and will typically will have a value of 1.
|
2972 |
|
|
|
2973 |
|
|
|
2974 |
|
|
The typical minimum transfer size of a single byte is not strictly
|
2975 |
|
|
speaking correct, since under some circumstances it can make sense
|
2976 |
|
|
to have a transfer size of zero bytes. However current target-side
|
2977 |
|
|
device drivers interpret a request to transfer zero bytes as a way
|
2978 |
|
|
for higher-level code to determine whether or not an endpoint is
|
2979 |
|
|
stalled, so it is not actually possible to perform zero-byte
|
2980 |
|
|
transfers. This issue will be addressed at some future point.
|
2981 |
|
|
|
2982 |
|
|
|
2983 |
|
|
|
2984 |
|
|
|
2985 |
|
|
max_size
|
2986 |
|
|
|
2987 |
|
|
This field specifies an upper bound on the size of bulk transfers.
|
2988 |
|
|
Some target-side drivers may be limited to transfers of say
|
2989 |
|
|
0x0FFFF bytes because of hardware limitations. In practice the
|
2990 |
|
|
transfer size is likely to be limited primarily to limit memory
|
2991 |
|
|
consumption of the test code on the target hardware, and to ensure
|
2992 |
|
|
that tests complete reasonably quickly. At the time of writing
|
2993 |
|
|
transfers are limited to 4K.
|
2994 |
|
|
|
2995 |
|
|
|
2996 |
|
|
|
2997 |
|
|
max_in_padding
|
2998 |
|
|
|
2999 |
|
|
On some hardware it may be necessary for the target-side device
|
3000 |
|
|
driver to send more data than is actually intended. For example
|
3001 |
|
|
the SA11x0 USB hardware cannot perform bulk transfers that are
|
3002 |
|
|
an exact multiple of 64 bytes, instead it must pad such
|
3003 |
|
|
transfers with an extra byte and the host must be ready to
|
3004 |
|
|
accept and discard this byte. The
|
3005 |
|
|
max_in_padding field indicates the amount of
|
3006 |
|
|
padding that is required. The low-level code inside
|
3007 |
|
|
usbhost will use this field
|
3008 |
|
|
automatically, and there is no need for test scripts to adjust
|
3009 |
|
|
packet sizes for padding. The field is provided for
|
3010 |
|
|
informational purposes only.
|
3011 |
|
|
|
3012 |
|
|
|
3013 |
|
|
|
3014 |
|
|
devtab
|
3015 |
|
|
|
3016 |
|
|
This is a string indicating whether or not the
|
3017 |
|
|
target-side USB device driver supports access to this endpoint
|
3018 |
|
|
via entries in the device table, in other words through
|
3019 |
|
|
conventional calls like open and
|
3020 |
|
|
write. Some device drivers may only
|
3021 |
|
|
support low-level USB access because typically that is what gets
|
3022 |
|
|
used by USB class-specific packages such as USB-ethernet.
|
3023 |
|
|
An empty string indicates that no devtab entry is available,
|
3024 |
|
|
otherwise it will be something like
|
3025 |
|
|
"/dev/usbs2w".
|
3026 |
|
|
|
3027 |
|
|
|
3028 |
|
|
|
3029 |
|
|
|
3030 |
|
|
Typical test scripts would access this data using something like:
|
3031 |
|
|
|
3032 |
|
|
|
3033 |
|
|
foreach endpoint $usbtest:bulk_in_endpoints {
|
3034 |
|
|
puts "Endpoint $endpoint: "
|
3035 |
|
|
puts " minimum transfer size $usbtest::bulk_in($endpoint,min_size)"
|
3036 |
|
|
puts " maximum transfer size $usbtest::bulk_in($endpoint,max_size)"
|
3037 |
|
|
if { 0 == $usbtest::bulk_in($endpoint,max_in_padding) } {
|
3038 |
|
|
puts " no IN padding required"
|
3039 |
|
|
} else {
|
3040 |
|
|
puts " $usbtest::bulk_in($endpoint,max_in_padding) bytes of IN padding required"
|
3041 |
|
|
}
|
3042 |
|
|
if { "" == $usbtest::bulk_in($endpoint,devtab) } {
|
3043 |
|
|
puts " no devtab entry provided"
|
3044 |
|
|
} else {
|
3045 |
|
|
puts " corresponding devtab entry is $usbtest::bulk_in($endpoint,devtab)"
|
3046 |
|
|
}
|
3047 |
|
|
}
|
3048 |
|
|
|
3049 |
|
|
|
3050 |
|
|
|
3051 |
|
|
|
3052 |
|
|
bulk_out_endpoint
|
3053 |
|
|
|
3054 |
|
|
This is a simple list of the endpoints which can support bulk OUT
|
3055 |
|
|
transfers. It is analogous to
|
3056 |
|
|
bulk_in_endpoints.
|
3057 |
|
|
|
3058 |
|
|
|
3059 |
|
|
|
3060 |
|
|
bulk_out()
|
3061 |
|
|
|
3062 |
|
|
This array holds additional information about each bulk OUT
|
3063 |
|
|
endpoint. It can be accessed in the same way as
|
3064 |
|
|
bulk_in(), except that there is no
|
3065 |
|
|
max_in_padding field because that field only
|
3066 |
|
|
makes sense for IN transfers.
|
3067 |
|
|
|
3068 |
|
|
|
3069 |
|
|
|
3070 |
|
|
control()
|
3071 |
|
|
|
3072 |
|
|
This array holds information about the control endpoint. It contains
|
3073 |
|
|
two fields, min_size and
|
3074 |
|
|
max_size. Note that there is no variable
|
3075 |
|
|
control_endpoints because a USB target always
|
3076 |
|
|
supports a single control endpoint 0. Similarly
|
3077 |
|
|
the control array does not use an endpoint number
|
3078 |
|
|
as the first index because that would be redundant.
|
3079 |
|
|
|
3080 |
|
|
|
3081 |
|
|
|
3082 |
|
|
isochronous_in_endpoints and
|
3083 |
|
|
isochronous_in()
|
3084 |
|
|
|
3085 |
|
|
These variables provide the same information as
|
3086 |
|
|
bulk_in_endpoints and bulk_in,
|
3087 |
|
|
but for endpoints that support isochronous IN transfers.
|
3088 |
|
|
|
3089 |
|
|
|
3090 |
|
|
|
3091 |
|
|
isochronous_out_endpoints and
|
3092 |
|
|
isochronous_out()
|
3093 |
|
|
|
3094 |
|
|
These variables provide the same information as
|
3095 |
|
|
bulk_out_endpoints and bulk_out,
|
3096 |
|
|
but for endpoints that support isochronous OUT transfers.
|
3097 |
|
|
|
3098 |
|
|
|
3099 |
|
|
|
3100 |
|
|
interrupt_in_endpoints and
|
3101 |
|
|
interrupt_in()
|
3102 |
|
|
|
3103 |
|
|
These variables provide the same information as
|
3104 |
|
|
bulk_in_endpoints and bulk_in,
|
3105 |
|
|
but for endpoints that support interrupt IN transfers.
|
3106 |
|
|
|
3107 |
|
|
|
3108 |
|
|
|
3109 |
|
|
interrupt_out_endpoints and
|
3110 |
|
|
interrupt_out()
|
3111 |
|
|
|
3112 |
|
|
These variables provide the same information as
|
3113 |
|
|
bulk_out_endpoints and bulk_out,
|
3114 |
|
|
but for endpoints that support interrupt OUT transfers.
|
3115 |
|
|
|
3116 |
|
|
|
3117 |
|
|
|
3118 |
|
|
|
3119 |
|
|
|
3120 |
|
|
|
3121 |
|
|
Testing Bulk Transfers
|
3122 |
|
|
|
3123 |
|
|
The main function for initiating a bulk test is
|
3124 |
|
|
usbtest::bulktest. This takes three compulsory
|
3125 |
|
|
arguments, and can be given a number of additional arguments to
|
3126 |
|
|
control the exact behaviour. The compulsory arguments are:
|
3127 |
|
|
|
3128 |
|
|
|
3129 |
|
|
|
3130 |
|
|
endpoint
|
3131 |
|
|
|
3132 |
|
|
This specifies the endpoint to use. It should correspond to
|
3133 |
|
|
one of the entries in
|
3134 |
|
|
usbtest::bulk_in_endpoints or
|
3135 |
|
|
usbtest::bulk_out_endpoints, depending on the
|
3136 |
|
|
transfer direction.
|
3137 |
|
|
|
3138 |
|
|
|
3139 |
|
|
|
3140 |
|
|
direction
|
3141 |
|
|
|
3142 |
|
|
This should be either in or out.
|
3143 |
|
|
|
3144 |
|
|
|
3145 |
|
|
|
3146 |
|
|
number of transfers
|
3147 |
|
|
|
3148 |
|
|
This specifies the number of transfers that should take place. The
|
3149 |
|
|
testing software does not currently support the concept of performing
|
3150 |
|
|
transfers for a given period of time because synchronising this on
|
3151 |
|
|
both the host and a wide range of targets is difficult. However it
|
3152 |
|
|
is relatively easy to work out the approximate time a number of bulk
|
3153 |
|
|
transfers should take place, based on a typical bandwidth of
|
3154 |
|
|
1MB/second and assuming say a 1ms overhead per transfer.
|
3155 |
|
|
Alternatively a test script could perform a small initial run to
|
3156 |
|
|
determine what performance can actually be expected from a given
|
3157 |
|
|
target, and then use this information to run a much longer test.
|
3158 |
|
|
|
3159 |
|
|
|
3160 |
|
|
|
3161 |
|
|
|
3162 |
|
|
Additional arguments can be used to control the exact transfer. For
|
3163 |
|
|
example a txdelay+ argument can be used to
|
3164 |
|
|
slowly increase the delay between transfers. All such arguments involve
|
3165 |
|
|
a value which can be passed either as part of the argument itself,
|
3166 |
|
|
for example txdelay+=5, or as a subsequent
|
3167 |
|
|
argument, txdelay+ 5. The possible arguments fall
|
3168 |
|
|
into a number of categories: data, I/O mechanism, transmit size,
|
3169 |
|
|
receive size, transmit delay, and receive delay.
|
3170 |
|
|
|
3171 |
|
|
|
3172 |
|
|
Data
|
3173 |
|
|
|
3174 |
|
|
An obvious parameter to control is the actual data that gets sent.
|
3175 |
|
|
This can be controlled by the argument data
|
3176 |
|
|
which can take one of five values: none,
|
3177 |
|
|
bytefill, intfill,
|
3178 |
|
|
byteseq and wordseq. The default
|
3179 |
|
|
value is none.
|
3180 |
|
|
|
3181 |
|
|
|
3182 |
|
|
|
3183 |
|
|
none
|
3184 |
|
|
|
3185 |
|
|
The transmit code will not attempt to fill the buffer in any way,
|
3186 |
|
|
and the receive code will not check it. The actual data that gets
|
3187 |
|
|
transferred will be whatever happened to be in the buffer before
|
3188 |
|
|
the transfer started.
|
3189 |
|
|
|
3190 |
|
|
|
3191 |
|
|
|
3192 |
|
|
bytefill
|
3193 |
|
|
|
3194 |
|
|
The entire buffer will be filled with a single byte, as per
|
3195 |
|
|
memset.
|
3196 |
|
|
|
3197 |
|
|
|
3198 |
|
|
|
3199 |
|
|
intfill
|
3200 |
|
|
|
3201 |
|
|
The buffer will be treated as an array of 32-bit integers, and will
|
3202 |
|
|
be filled with the same integer repeated the appropriate number of
|
3203 |
|
|
times. If the buffer size is not a multiple of four bytes then
|
3204 |
|
|
the last few bytes will be set to 0.
|
3205 |
|
|
|
3206 |
|
|
|
3207 |
|
|
|
3208 |
|
|
byteseq
|
3209 |
|
|
|
3210 |
|
|
The buffer will be filled with a sequence of bytes, generated by
|
3211 |
|
|
a linear congruential generator. If the first byte in the buffer is
|
3212 |
|
|
filled with the value x, the next byte will be
|
3213 |
|
|
(m*x)+i. For example a sequence of slowly
|
3214 |
|
|
incrementing bytes can be achieved by setting both the multiplier
|
3215 |
|
|
and the increment to 1. Alternatively a pseudo-random number
|
3216 |
|
|
sequence can be achieved using values 1103515245 and 12345, as
|
3217 |
|
|
per the standard C library rand function.
|
3218 |
|
|
For convenience these two constants are available as Tcl
|
3219 |
|
|
variables usbtest::MULTIPLIER and
|
3220 |
|
|
usbtest::INCREMENT.
|
3221 |
|
|
|
3222 |
|
|
|
3223 |
|
|
|
3224 |
|
|
wordseq
|
3225 |
|
|
|
3226 |
|
|
This acts like byteseq, except that the buffer is
|
3227 |
|
|
treated as an array of 32-bit integers rather than as an array of
|
3228 |
|
|
bytes. If the buffer is not a multiple of four bytes then the last
|
3229 |
|
|
few bytes will be filled with zeroes.
|
3230 |
|
|
|
3231 |
|
|
|
3232 |
|
|
|
3233 |
|
|
|
3234 |
|
|
The above requires three additional parameters
|
3235 |
|
|
data1, data* and
|
3236 |
|
|
data+. data1 specifies
|
3237 |
|
|
the value to be used for byte or word fills, or the first number when
|
3238 |
|
|
calculating a sequence. The default value is 0.
|
3239 |
|
|
data* and data+ specify
|
3240 |
|
|
the multiplier and increment for a sequence, and have default values
|
3241 |
|
|
of 1 and 0 respectively. For
|
3242 |
|
|
example, to perform a bulk transfer of a pseudo-random sequence of
|
3243 |
|
|
integers starting with 42 the following code could be used:
|
3244 |
|
|
|
3245 |
|
|
|
3246 |
|
|
bulktest 2 IN 1000 data=wordseq data1=42 \
|
3247 |
|
|
data* $usbtest::MULTIPLIER data+ $usbtest::INCREMENT
|
3248 |
|
|
|
3249 |
|
|
|
3250 |
|
|
The above parameters define what data gets transferred for the first
|
3251 |
|
|
transfer, but a test can involve multiple transfers. The data format
|
3252 |
|
|
will be the same for all transfers, but it is possible to adjust the
|
3253 |
|
|
current value, the multiplier, and the increment between each
|
3254 |
|
|
transfer. This is achieved with parameters data1*,
|
3255 |
|
|
data1+, data**,
|
3256 |
|
|
data*+, data+*, and
|
3257 |
|
|
data++, with default values of 1 for each
|
3258 |
|
|
multiplier and 0 for each increment. For example, if the multiplier
|
3259 |
|
|
for the first transfer is set to 2 using
|
3260 |
|
|
data*, and arguments
|
3261 |
|
|
data** 2 and data*+ -1 are also
|
3262 |
|
|
supplied, then the multiplier for subsequent transfers will be
|
3263 |
|
|
3, 5, 9,
|
3264 |
|
|
….
|
3265 |
|
|
|
3266 |
|
|
|
3267 |
|
|
|
3268 |
|
|
Currently it is not possible for a test script to send specific data,
|
3269 |
|
|
for example a specific sequence of bytes captured by a protocol analyser
|
3270 |
|
|
that caused a problem. If the transfer was from host to target then
|
3271 |
|
|
the target would have to know the exact sequence of bytes to expect,
|
3272 |
|
|
which means transferring data over the USB bus when that data is known
|
3273 |
|
|
to have caused problems in the past. Similarly for target to host
|
3274 |
|
|
transfers the target would have to know what bytes to send. A possible
|
3275 |
|
|
future extension of the USB testing support would allow for bounce
|
3276 |
|
|
operations, where a given message is first sent to the target and then
|
3277 |
|
|
sent back to the host, with only the host checking that the data was
|
3278 |
|
|
returned correctly.
|
3279 |
|
|
|
3280 |
|
|
|
3281 |
|
|
|
3282 |
|
|
I/O Mechanism
|
3283 |
|
|
|
3284 |
|
|
On the target side USB transfers can happen using either low-level
|
3285 |
|
|
USB calls such as usbs_start_rx_buffer, or by
|
3286 |
|
|
higher-level calls which go through the device table. By default the
|
3287 |
|
|
target-side code will use the low-level calls. If it is desired to
|
3288 |
|
|
test the higher-level calls instead, for example because those are
|
3289 |
|
|
what the application uses, then that can be achieved with an
|
3290 |
|
|
argument mechanism=devtab.
|
3291 |
|
|
|
3292 |
|
|
|
3293 |
|
|
|
3294 |
|
|
Transmit Size
|
3295 |
|
|
|
3296 |
|
|
The next set of arguments can be used to control the size of the
|
3297 |
|
|
transmitted buffer: txsize1,
|
3298 |
|
|
txsize>=, txsize<=
|
3299 |
|
|
txsize*, txsize/,
|
3300 |
|
|
and txsize+.
|
3301 |
|
|
|
3302 |
|
|
|
3303 |
|
|
txsize1 determines the size of the first
|
3304 |
|
|
transfer, and has a default value of 32 bytes. The size of the next
|
3305 |
|
|
transfer is calculated by first multiplying by the
|
3306 |
|
|
txsize* value, then dividing by the
|
3307 |
|
|
txsize/ value, and finally adding the
|
3308 |
|
|
txsize+ value. The defaults for these are
|
3309 |
|
|
1, 1, and 0
|
3310 |
|
|
respectively, which means that the transfer size will remain
|
3311 |
|
|
unchanged. If for example the transfer size should increase by
|
3312 |
|
|
approximately 50 per cent each time then suitable values might be
|
3313 |
|
|
txsize* 3, txsize/ 2,
|
3314 |
|
|
and txsize+ 1.
|
3315 |
|
|
|
3316 |
|
|
|
3317 |
|
|
The txsize>= and
|
3318 |
|
|
txsize<= arguments can be used to impose
|
3319 |
|
|
lower and upper bounds on the transfer. By default the
|
3320 |
|
|
min_size and max_size values
|
3321 |
|
|
appropriate for the endpoint will be used. If at any time the
|
3322 |
|
|
current size falls outside the bounds then it will be normalized.
|
3323 |
|
|
|
3324 |
|
|
|
3325 |
|
|
|
3326 |
|
|
Receive Size
|
3327 |
|
|
|
3328 |
|
|
The receive size, in other words the number of bytes that either host
|
3329 |
|
|
or target will expect to receive as opposed to the number of bytes
|
3330 |
|
|
that actually get sent, can be adjusted using a similar set of
|
3331 |
|
|
arguments: rxsize1,
|
3332 |
|
|
rxsize>=,
|
3333 |
|
|
rxsize<=,
|
3334 |
|
|
rxsize*, rxsize/ and
|
3335 |
|
|
rxsize+. The current receive size will be
|
3336 |
|
|
adjusted between transfers just like the transmit size. However when
|
3337 |
|
|
communicating over USB it is not a good idea to attempt to receive
|
3338 |
|
|
less data than will actually be sent: typically neither the hardware
|
3339 |
|
|
nor the software will be able to do anything useful with the excess,
|
3340 |
|
|
so there will be problems. Therefore if at any time the calculated
|
3341 |
|
|
receive size is less than the transmit size, the actual receive will
|
3342 |
|
|
be for the exact number of bytes that will get transmitted. However
|
3343 |
|
|
this will not affect the calculations for the next receive size.
|
3344 |
|
|
|
3345 |
|
|
|
3346 |
|
|
The default values for rxsize1,
|
3347 |
|
|
rxsize*, rxsize/ and
|
3348 |
|
|
rxsize+ are 0,
|
3349 |
|
|
1, 1 and 0
|
3350 |
|
|
respectively. This means that the calculated receive size will always
|
3351 |
|
|
be less than the transmit size, so the receive operation will be for
|
3352 |
|
|
the exact number of bytes transmitted. For some USB protocols this
|
3353 |
|
|
would not accurately reflect the traffic that will happen. For example
|
3354 |
|
|
with USB-ethernet transfer sizes will vary between 16 and 1516 bytes,
|
3355 |
|
|
so the receiver will always expect up to 1516 bytes. This can be
|
3356 |
|
|
achieved using rxsize1 1516, leaving the
|
3357 |
|
|
other parameters at their default values.
|
3358 |
|
|
|
3359 |
|
|
|
3360 |
|
|
For target hardware which involves non-zero
|
3361 |
|
|
max_in_padding, on the host side the padding will
|
3362 |
|
|
be added automatically to the receive size if necessary.
|
3363 |
|
|
|
3364 |
|
|
|
3365 |
|
|
|
3366 |
|
|
Transmit and Receive Delays
|
3367 |
|
|
|
3368 |
|
|
Typically during the testing there will be some minor delays between
|
3369 |
|
|
transfers on both host and target. Some of these delays will be caused
|
3370 |
|
|
by timeslicing, for example another process running on the host, or a
|
3371 |
|
|
concurrent test thread running inside the target. Other delays will be
|
3372 |
|
|
caused by the USB bus itself, for example activity from another device
|
3373 |
|
|
on the bus. However it is desirable that test cases be allowed to
|
3374 |
|
|
inject additional and somewhat more controlled delays into the system,
|
3375 |
|
|
for example to make sure that the target behaves correctly even if the
|
3376 |
|
|
target is not yet ready to receive data from the host.
|
3377 |
|
|
|
3378 |
|
|
|
3379 |
|
|
The transmit delay is controlled by six parameters:
|
3380 |
|
|
txdelay1, txdelay*,
|
3381 |
|
|
txdelay/, txdelay+,
|
3382 |
|
|
txdelay>= and
|
3383 |
|
|
txdelay<=. The default values for these are
|
3384 |
|
|
0, 1, 1,
|
3385 |
|
|
0, 0 and
|
3386 |
|
|
1000000000 respectively, so that by default
|
3387 |
|
|
transmits will happen as quickly as possible. Delays are measured in
|
3388 |
|
|
nanoseconds, so a value of 1000000 would correspond
|
3389 |
|
|
to a delay of 0.001 seconds or one millisecond. By default delays have
|
3390 |
|
|
an upper bound of one second. Between transfers the transmit delay is
|
3391 |
|
|
updated in much the same was as the transfer sizes.
|
3392 |
|
|
|
3393 |
|
|
|
3394 |
|
|
The receive delay is controlled by a similar set of six parameters:
|
3395 |
|
|
rxdelay1, rxdelay*,
|
3396 |
|
|
rxdelay/, rxdelay+,
|
3397 |
|
|
rxdelay>= and
|
3398 |
|
|
rxdelay<=. The default values for these are
|
3399 |
|
|
the same as for transmit delays.
|
3400 |
|
|
|
3401 |
|
|
|
3402 |
|
|
The transmit delay is used on the side which sends data over the USB
|
3403 |
|
|
bus, so for a bulk IN transfer it is the target that sends data and
|
3404 |
|
|
hence sleeps for the specified transmit delay, while the host receives
|
3405 |
|
|
data sleeps for the receive delay. For an OUT transfer the positions
|
3406 |
|
|
are reversed.
|
3407 |
|
|
|
3408 |
|
|
|
3409 |
|
|
It should be noted that although the delays are measured in
|
3410 |
|
|
nanoseconds, the actual delays will be much less precise and are
|
3411 |
|
|
likely to be of the order of milliseconds. The exact details will
|
3412 |
|
|
depend on the kernel clock speed.
|
3413 |
|
|
|
3414 |
|
|
|
3415 |
|
|
|
3416 |
|
|
|
3417 |
|
|
|
3418 |
|
|
Other Types of Transfer
|
3419 |
|
|
|
3420 |
|
|
Support for testing other types of USB traffic such as isochronous
|
3421 |
|
|
transfers is not yet implemented.
|
3422 |
|
|
|
3423 |
|
|
|
3424 |
|
|
|
3425 |
|
|
Starting a Test and Collecting Results
|
3426 |
|
|
|
3427 |
|
|
A USB test script should prepare one or more transfers using
|
3428 |
|
|
appropriate functions such as usbtest::bulktest.
|
3429 |
|
|
Once all the individual tests have been prepared they can be started
|
3430 |
|
|
by a call to usbtest::start. This takes a single
|
3431 |
|
|
argument, a maximum duration measured in seconds. If all transfers
|
3432 |
|
|
have not been completed in the specified time then any remaining
|
3433 |
|
|
transfers will be aborted.
|
3434 |
|
|
|
3435 |
|
|
|
3436 |
|
|
usbtest::start will return 1
|
3437 |
|
|
if all the tests have succeeded, or 0 if any of
|
3438 |
|
|
them have failed. More detailed reports will be stored in the
|
3439 |
|
|
Tcl variable usbtests::results, which will be a
|
3440 |
|
|
list of string messages.
|
3441 |
|
|
|
3442 |
|
|
|
3443 |
|
|
|
3444 |
|
|
Existing Test Scripts
|
3445 |
|
|
|
3446 |
|
|
A number of test scripts are provided as standard. These are located
|
3447 |
|
|
in the host subdirectory of the
|
3448 |
|
|
common USB slave package, and will be installed as part of the process
|
3449 |
|
|
of building the host-side software. When a script is specified on the
|
3450 |
|
|
command line usbhost will first search for
|
3451 |
|
|
it in the current directory, then in the install tree. Standard
|
3452 |
|
|
test scripts include the following:
|
3453 |
|
|
|
3454 |
|
|
|
3455 |
|
|
list.tcl
|
3456 |
|
|
|
3457 |
|
|
This script simply displays information about the capabilities
|
3458 |
|
|
of the target platform, as provided by the target-side USB
|
3459 |
|
|
device driver. It can help with tracking down problems, but its
|
3460 |
|
|
primary purpose is to let users check that everything is working
|
3461 |
|
|
correctly: if running usbhost list.tcl
|
3462 |
|
|
outputs sensible information then the user knows that the
|
3463 |
|
|
target side is running correctly and that communication between
|
3464 |
|
|
host and target is possible.
|
3465 |
|
|
|
3466 |
|
|
|
3467 |
|
|
verbose.tcl
|
3468 |
|
|
|
3469 |
|
|
The target-side code can provide information about what
|
3470 |
|
|
is happening while tests are prepared and run. This facility
|
3471 |
|
|
should not normally be used since the extra I/O involved will
|
3472 |
|
|
significantly affect the behaviour of the system, but in some
|
3473 |
|
|
circumstances it may prove useful. Since an eCos application
|
3474 |
|
|
cannot easily be given command-line arguments the target-side
|
3475 |
|
|
verbosity level cannot be controlled using
|
3476 |
|
|
-V or --verbose
|
3477 |
|
|
options. Instead it can be controlled from inside
|
3478 |
|
|
gdb by changing the integer
|
3479 |
|
|
variable verbose. Alternatively it can
|
3480 |
|
|
be manipulated by running the test script
|
3481 |
|
|
verbose.tcl. This script takes a single
|
3482 |
|
|
argument, the desired verbosity level, which should be a small
|
3483 |
|
|
integer. For example, to disable target-side run-time logging
|
3484 |
|
|
the command usbhost verbose 0 can
|
3485 |
|
|
be used.
|
3486 |
|
|
|
3487 |
|
|
|
3488 |
|
|
|
3489 |
|
|
|
3490 |
|
|
|
3491 |
|
|
Possible Problems
|
3492 |
|
|
|
3493 |
|
|
If all transfers succeed within the specified time then both host and
|
3494 |
|
|
target remain in synch and further tests can be run without problem.
|
3495 |
|
|
However, if at any time a failure occurs then things get more
|
3496 |
|
|
complicated. For example, if the current test involves a series of
|
3497 |
|
|
bulk OUT transfers and the target detects that for one of these
|
3498 |
|
|
transfers it received less data than was expected then the test has
|
3499 |
|
|
failed, and the target will stop accepting data on this endpoint.
|
3500 |
|
|
However the host-side software may not have detected anything wrong
|
3501 |
|
|
and is now blocked trying to send the next lot of data.
|
3502 |
|
|
|
3503 |
|
|
|
3504 |
|
|
The test code goes to considerable effort to recover from problems
|
3505 |
|
|
such as these. On the host-side separate threads are used for
|
3506 |
|
|
concurrent transfers, and on the target-side appropriate asynchronous
|
3507 |
|
|
I/O mechanisms are used. In addition there is a control thread on the
|
3508 |
|
|
host that checks the state of all the main host-side threads, and the
|
3509 |
|
|
state of the target using private control messages. If it discovers
|
3510 |
|
|
that one side has stopped sending or receiving data because of an
|
3511 |
|
|
error and the other side is blocked as a result, it will set certain
|
3512 |
|
|
flags and then cause one additional transfer to take place. That
|
3513 |
|
|
additional transfer will have the effect of unblocking the other side,
|
3514 |
|
|
which then discovers that an error has occurred by checking the
|
3515 |
|
|
appropriate flags. In this way both host and target should end up back
|
3516 |
|
|
in synch, and it is possible to move on to the next set of tests.
|
3517 |
|
|
|
3518 |
|
|
|
3519 |
|
|
However, the above assumes that the testing has not triggered any
|
3520 |
|
|
serious hardware conditions. If instead the target-side hardware has
|
3521 |
|
|
been left in some strange state so that, for example, it will no
|
3522 |
|
|
longer raise an interrupt for traffic on a particular endpoint then
|
3523 |
|
|
recovery is not currently possible, and the testing software will just
|
3524 |
|
|
hang.
|
3525 |
|
|
|
3526 |
|
|
|
3527 |
|
|
A possible future enhancement to the testing software would allow the
|
3528 |
|
|
host-side to raise a USB reset signal whenever a failure occurs, in
|
3529 |
|
|
the hope that this would clear any remaining problems within the
|
3530 |
|
|
target-side USB hardware.
|
3531 |
|
|
|
3532 |
|
|
|
3533 |
|
|
|
3534 |
|
|
|
3535 |
|
|
|
3536 |
|
|
|
3537 |
|
|
|
3538 |
|
|
|
3539 |
|
|
|
3540 |
|
|
|