3 //=================================================================
7 // USB testing - host-side
9 //==========================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 2005 eCosCentric Ltd.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
48 //####DESCRIPTIONEND####
49 //==========================================================================
51 // The overall architecture is as follows.
53 // The target hardware runs a special application which provides a
54 // particular type of USB application, "Red Hat eCos USB testing".
55 // This will not be recognised by any device driver, so the Linux
56 // kernel will pretty much ignore the device (other host OS's are not
57 // considered at this time).
59 // This program is the only supported way to interact with that service.
60 // It acts as an extended Tcl interpreter, providing a number of new
61 // Tcl commands for interacting with the target. All test cases can
62 // then be written as Tcl scripts which invoke a series of these commands.
63 // These Tcl commands operate essentially though the LINUX usb devfs
64 // service which allows ordinary application code to perform USB operations
76 #include <sys/types.h>
80 #include <sys/ioctl.h>
83 #include <semaphore.h>
84 // Avoid compatibility problems with Tcl 8.4 vs. earlier
87 #include <linux/usb.h>
88 #include <linux/usbdevice_fs.h>
89 #include "../tests/protocol.h"
94 /*{{{ Backwards compatibility */
96 // The header file <linux/usbdevice_fs.h> has changed in an incompatible
97 // way. This is detected by autoconf
98 #ifndef CYGBLD_USE_NEW_FIELD_NAMES
99 # define bRequestType requesttype
100 # define bRequest request
101 # define wValue value
102 # define wIndex index
103 # define wLength length
109 // ----------------------------------------------------------------------------
112 // Has the current batch of tests actually terminated? This flag is
113 // checked by the various test handlers at appropriate intervals, and
114 // helps to handle the case where one of the side has terminated early
115 // because an error has been detected.
116 static int current_tests_terminated = 0;
118 // The next local thread to be allocated for testing. This variable can also
119 // be used to find out how many threads are involved in the current test.
120 // This counter should always be reset to 0 at the end of every test run.
121 static int local_thread_count = 0;
123 // A similar counter for remote threads.
124 static int remote_thread_count = 0;
126 // A file handle for manipulating the USB device at a low level
127 static int usb_master_fd = -1;
132 // ----------------------------------------------------------------------------
133 // The user can provide one or more -V/--verbose arguments to increase
134 // the amount of output generated.
136 static int verbose = 0;
138 #define VERBOSE(_level_, _format_, _args_...) \
140 if (verbose >= _level_) { \
141 printf(_format_, ## _args_); \
146 /*{{{ Low-level USB access */
148 // ----------------------------------------------------------------------------
149 // Low-level access to a USB device.
151 // The various ioctl() calls require a file handle which corresponds to one
152 // of the /proc/bus/usb/<abc>/<def> entries. <abc> is a bus number,
153 // typically 001 or 001, and <def> is a device number on that bus,
154 // e.g. 003. Figuring out <abc> and <def> requires scanning
155 // /proc/bus/usb/devices, which is a somewhat complicated text file.
157 // This is all somewhat vulnerable to incompatible changes in the
158 // Linux kernel, specifically the implementation of the /proc/bus/usb.
159 // An alternative approach would be to write a new Linux device driver
160 // and interact with that, but that approach is vulnerable to any
161 // internal kernel API changes affecting USB device drivers.
163 // How to access USB devices from userland
164 #define USB_ROOT "/proc/bus/usb/"
166 // How to identify the eCos test case
167 #define PRODUCT_STRING "Red Hat eCos USB test"
169 // Scan through /proc/bus/usb/devices looking for an entry that
170 // matches what we are after, specifically a line
171 // S: Product=Red Hat eCos USB testcase
172 // The required information can then be obtained from the previous
174 // T: Bus=<abc> ... Dev#= <def> ...
176 // Of course the T: line is going to come first, so it is necessary
177 // to keep track of the current bus and device numbers.
179 // Note: this code is duplicated in usbchmod.c. Any changes here
180 // should be propagated. For now the routine is too small to warrant
181 // a separate source file.
184 usb_scan_devices(int* bus, int* dev)
187 int current_bus = -1;
188 int current_dev = -1;
194 VERBOSE(1, "Searching " USB_ROOT "devices for the eCos USB test code\n");
196 devs_file = fopen(USB_ROOT "devices", "r");
197 if (NULL == devs_file) {
198 fprintf(stderr, "usbhost: error, unable to access " USB_ROOT "devices\n");
201 ch = getc(devs_file);
204 if (2 !=fscanf(devs_file, ": Bus=%d %*[^D\n]Dev#=%d", ¤t_bus, ¤t_dev)) {
208 } else if ('S' == ch) {
209 int start = 0, end = 0;
210 if (EOF != fscanf(devs_file, ": Product=%n" PRODUCT_STRING "%n", &start, &end)) {
218 // Move to the end of the current line.
219 while ((EOF != ch) && ('\n' != ch)) {
220 ch = getc(devs_file);
223 ch = getc(devs_file);
228 if ((-1 != *bus) && (-1 != *dev)) {
229 VERBOSE(1, "Found eCos USB test code on bus %d, device %d\n", *bus, *dev);
232 fprintf(stderr, "usbhost: error, failed to find a USB device \"" PRODUCT_STRING "\"\n");
236 // Actually open the USB device, allowing subsequent ioctl() operations.
238 // Typically /proc/bus/usb/... will not allow ordinary applications
239 // to perform ioctl()'s. Instead root privileges are required. To work
240 // around this there is a little utility usbchmod, installed suid,
241 // which can be used to get access to the raw device.
243 usb_open_device(void)
245 char devname[_POSIX_PATH_MAX];
250 if ((-1 == bus) || (-1 == dev)) {
251 if (!usb_scan_devices(&bus, &dev)) {
256 if (_POSIX_PATH_MAX == snprintf(devname, _POSIX_PATH_MAX, USB_ROOT "%03d/%03d", bus, dev)) {
257 fprintf(stderr, "usbhost: internal error, buffer overflow\n");
261 VERBOSE(1, "Attempting to access USB target via %s\n", devname);
263 result = open(devname, O_RDWR);
265 // Check for access right problems. If so, try to work around them
266 // by invoking usbchmod. Always look for this in the install tree,
267 // since it is only that version which is likely to have been
268 // chown'ed and chmod'ed to be suid root.
269 if (EACCES == errno) {
270 char command_name[_POSIX_PATH_MAX];
272 VERBOSE(1, "Insufficient access to USB target, running usbchmod\n");
273 if (_POSIX_PATH_MAX == snprintf(command_name, _POSIX_PATH_MAX, "%s/usbchmod %d %d", USBAUXDIR, bus, dev)) {
274 fprintf(stderr, "usbhost: internal error, buffer overflow\n");
277 (void) system(command_name);
278 result = open(devname, O_RDWR);
282 fprintf(stderr, "usbhost: error, failed to open \"%s\", errno %d\n", devname, errno);
286 VERBOSE(1, "USB device now accessible via file descriptor %d\n", result);
288 // Also perform a set-configuration call, to avoid warnings from
289 // the Linux kernel. Target-side testing is always configuration 1
290 // because only a single configuration is supported.
291 (void) ioctl(result, USBDEVFS_SETCONFIGURATION, 1);
295 // Exchange a control message with the host. The return value should
296 // be 0, or a small positive number indicating the actual number of
297 // bytes received which may be less than requested.
299 // There appear to be problems with some hosts, manifesting itself as
300 // an inability to send control messages that involve additional data
301 // from host->target. These problems are not yet well-understood. For
302 // now the workaround is to send multiple packets, each with up to
303 // four bytes encoded in the index and length fields.
305 usb_control_message(int fd, int request_type, int request, int value, int index, int length, void* data)
307 struct usbdevfs_ctrltransfer transfer;
310 VERBOSE(3, "usb_control_message, request %02x, len %d\n", request, length);
312 if (length > USBTEST_MAX_CONTROL_DATA) {
313 fprintf(stderr, "usbhost: internal error, control message involves too much data.\n");
318 // Workaround - send additional data in the index and length fields.
319 if ((length > 0) && (USB_DIR_OUT == (USB_ENDPOINT_DIR_MASK & request_type))) {
321 unsigned char* buf = (unsigned char*) data;
323 for (i = 0; i < length; i+= 4) {
324 int this_len = length - 1;
327 transfer.bRequestType = USB_TYPE_CLASS | USB_RECIP_DEVICE;
332 case 1: transfer.bRequest = USBTEST_CONTROL_DATA1; break;
333 case 2: transfer.bRequest = USBTEST_CONTROL_DATA2; break;
334 case 3: transfer.bRequest = USBTEST_CONTROL_DATA3; break;
335 case 4: transfer.bRequest = USBTEST_CONTROL_DATA4; break;
337 fprintf(stderr, "usbhost: internal error, confusion about transfer length.\n");
340 transfer.wValue = (buf[i] << 8) | buf[i+1]; // Possible read beyond end of buffer,
341 transfer.wIndex = (buf[i+2] << 8) | buf[i+3]; // but not worth worrying about.
342 transfer.wLength = 0;
343 transfer.timeout = 10 * 1000; // ten seconds, the target should always accept data faster than this.
344 transfer.data = NULL;
346 // This is too strict, deciding what to do about errors should be
347 // handled by higher-level code. However it will do for now.
348 ioctl_result = ioctl(fd, USBDEVFS_CONTROL, &transfer);
349 if (0 != ioctl_result) {
350 fprintf(stderr, "usbhost: error, failed to send control message (data) to target.\n");
354 // There is no more data to be transferred.
358 transfer.bRequestType = request_type;
359 transfer.bRequest = request;
360 transfer.wValue = value;
361 transfer.wIndex = index;
362 transfer.wLength = length;
363 transfer.timeout = 10000;
364 transfer.data = data;
366 result = ioctl(fd, USBDEVFS_CONTROL, &transfer);
370 // A variant of the above which can be called when the target should always respond
371 // correctly. This can be used for class control messages.
373 usb_reliable_control_message(int fd, int request_type, int request, int value, int index, int length, void* data)
375 int result = usb_control_message(fd, request_type, request, value, index, length, data);
377 fprintf(stderr, "usbhost: error, failed to send control message %02x to target.\n", request);
378 fprintf(stderr, " : errno %d (%s)\n", errno, strerror(errno));
385 // Either send or receive a single bulk message. The top bit of the endpoint
386 // number indicates the direction.
388 usb_bulk_message(int fd, int endpoint, unsigned char* buffer, int length)
390 struct usbdevfs_bulktransfer transfer;
393 transfer.ep = endpoint;
394 transfer.len = length;
395 transfer.timeout = 60 * 60 * 1000;
396 // An hour. These operations should not time out because that
397 // leaves the system in a confused state. Instead there is
398 // higher-level recovery code that should ensure the operation
399 // really does complete, and the return value here is used
400 // by the calling code to determine whether the operation
401 // was successful or whether there was an error and the recovery
403 transfer.data = buffer;
405 result = ioctl(fd, USBDEVFS_BULK, &transfer);
410 // Synchronise with the target. This can be used after the host has sent a request that
411 // may take a bit of time, e.g. it may involve waking up a thread. The host will send
412 // synch requests at regular intervals, until the target is ready.
414 // The limit argument can be used to avoid locking up. -1 means loop forever, otherwise
415 // it means that many iterations of 100ms apiece.
417 usb_sync(int fd, int limit)
419 unsigned char buf[1];
420 struct timespec delay;
424 VERBOSE(2, "Synchronizing with target\n");
428 usb_reliable_control_message(fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_SYNCH, 0, 0, 1, buf);
433 if ((-1 != limit) && (++loops > limit)) {
436 VERBOSE(3, "Not yet synchronized, sleeping\n");
438 delay.tv_nsec = 100000000; // 100 ms
439 nanosleep(&delay, NULL);
443 VERBOSE(2, "%s\n", result ? "Synchronized" : "Not synchronized");
447 // Abort the target. Things seem to be completely messed up and there is no easy
448 // way to restore sanity to both target and host.
452 VERBOSE(2, "Target-side abort operation invoked\n");
453 usb_reliable_control_message(fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_ABORT, 0, 0, 0, (void*)0);
457 /*{{{ Initialise endpoints */
459 // ----------------------------------------------------------------------------
460 // On power-up some endpoints may not be in a sensible state. For example,
461 // with the SA11x0 the hardware may start accepting bulk OUT transfers
462 // before the target-side software has started a receive operation,
463 // so if the host sends a bulk packet before the target is ready then
464 // things get messy. This is especially troublesome if the target-side
465 // attempts any diagnostic output because of verbosity.
467 // This code loops through the various endpoints and makes sure that
468 // they are all in a reasonable state, before any real tests get run
469 // That means known hardware flaws do not show up as test failures,
470 // but of course they are still documented and application software
471 // will have to do the right thing.
474 usb_initialise_control_endpoint(int min_size, int max_size)
476 // At this time there are no known problems on any hardware
477 // that would need to be addressed
481 usb_initialise_isochronous_in_endpoint(int number, int min_size, int max_size)
483 // At this time there are no known problems on any hardware
484 // that would need to be addressed
488 usb_initialise_isochronous_out_endpoint(int number, int min_size, int max_size)
490 // At this time there are no known problems on any hardware
491 // that would need to be addressed
495 usb_initialise_bulk_in_endpoint(int number, int min_size, int max_size, int padding)
497 // At this time there are no known problems on any hardware
498 // that would need to be addressed
502 usb_initialise_bulk_out_endpoint(int number, int min_size, int max_size)
504 unsigned char buf[1];
506 // On the SA1110 the hardware comes up with a bogus default value,
507 // causing the hardware to accept packets before the software has
508 // set up DMA or in any way prepared for incoming data. This is
509 // a problem. It is worked around by making the target receive
510 // a single packet, sending that packet, and then performing a
512 VERBOSE(2, "Performing bulk OUT initialization on endpoint %d\n", number);
514 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN,
515 USBTEST_INIT_BULK_OUT, number, 0, 0, (void*) 0);
516 usb_bulk_message(usb_master_fd, number, buf, 1);
517 usb_sync(usb_master_fd, 10);
521 usb_initialise_interrupt_in_endpoint(int number, int min_size, int max_size)
523 // At this time there are no known problems on any hardware
524 // that would need to be addressed
528 usb_initialise_interrupt_out_endpoint(int number, int min_size, int max_size)
530 // At this time there are no known problems on any hardware
531 // that would need to be addressed
535 /*{{{ Host/target common code */
538 #include "../tests/common.c"
541 /*{{{ The test cases themselves */
543 /*{{{ UsbTest definition */
545 // ----------------------------------------------------------------------------
546 // All the data associated with a single test.
548 typedef struct UsbTest {
550 // A "unique" identifier to make verbose output easier to understand.
552 // Which file descriptor should be used to access USB.
555 // Which test should be run.
558 // Test-specific details.
561 UsbTest_ControlIn control_in;
564 // How to recover from any problems. Specifically, what kind of message
565 // could the target send or receive that would unlock the thread on this
567 UsbTest_Recovery recovery;
570 char result_message[USBTEST_MAX_MESSAGE];
571 unsigned char buffer[USBTEST_MAX_BULK_DATA + USBTEST_MAX_BULK_DATA_EXTRA];
574 // Reset the information in a given test. This is used by the pool allocation
575 // code. The data union is left alone, filling in the appropriate union
576 // member is left to other code.
578 reset_usbtest(UsbTest* test)
580 static int next_id = 1;
581 test->id = next_id++;
582 test->which_test = usbtest_invalid;
583 usbtest_recovery_reset(&(test->recovery));
584 test->result_pass = 0;
585 test->result_message[0] = '\0';
592 run_test_bulk_out(UsbTest* test)
594 unsigned char* buf = test->buffer;
597 VERBOSE(1, "Starting test %d, bulk OUT on endpoint %d\n", test->id, test->test_params.bulk.endpoint);
599 for (i = 0; i < test->test_params.bulk.number_packets; i++) {
601 int packet_size = test->test_params.bulk.tx_size;
603 test->recovery.endpoint = test->test_params.bulk.endpoint;
604 test->recovery.protocol = USB_ENDPOINT_XFER_BULK;
605 test->recovery.size = packet_size;
607 usbtest_fill_buffer(&(test->test_params.bulk.data), buf, packet_size);
609 VERBOSE(2, "Bulk OUT test %d: iteration %d, packet size %d\n", test->id, i, packet_size);
611 // Output the first 32 bytes of data as well.
615 index = snprintf(msg, 255, "Bulk OUT test %d: iteration %d, packet size %d\n Data %s:",
616 test->id, i, packet_size,
617 (usbtestdata_none == test->test_params.bulk.data.format) ? "(uninitialized)" : "");
619 for (j = 0; ((j + 3) < packet_size) && (j < 32); j+= 4) {
620 index += snprintf(msg+index, 255-index, " %02x%02x%02x%02x",
621 buf[j], buf[j+1], buf[j+2], buf[j+3]);
624 index += snprintf(msg+index, 255-index, " ");
625 for ( ; j < packet_size; j++) {
626 index += snprintf(msg+index, 255-index, "%02x", buf[j]);
630 VERBOSE(3, "%s\n", msg);
633 transferred = usb_bulk_message(test->fd, test->test_params.bulk.endpoint, buf, packet_size);
635 // Has this test run been aborted for some reason?
636 if (current_tests_terminated) {
637 VERBOSE(2, "Bulk OUT test %d: iteration %d, termination detected\n", test->id, i);
638 test->result_pass = 0;
639 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
640 "Host, bulk OUT transfer on endpoint %d: aborted after %d iterations\n",
641 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK, i);
645 // If an error occurred, abort this run.
646 if (-1 == transferred) {
647 char errno_buf[USBTEST_MAX_MESSAGE];
648 test->result_pass = 0;
649 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
650 "Host, bulk OUT transfer on endpoint %d : host ioctl() system call failed\n errno %d (%s)",
651 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK, errno,
652 strerror_r(errno, errno_buf, USBTEST_MAX_MESSAGE));
653 VERBOSE(2, "Bulk OUT test %d: iteration %d, error:\n %s\n", test->id, i, test->result_message);
657 if (0 != test->test_params.bulk.tx_delay) {
658 struct timespec delay;
660 VERBOSE(2, "Bulk OUT test %d: iteration %d, sleeping for %d nanoseconds\n", test->id, \
661 i, test->test_params.bulk.tx_delay);
662 // Note that nanosleep() can return early due to incoming signals,
663 // with the unelapsed time returned in a second argument. This
664 // allows for a retry loop. In practice this does not seem
665 // worthwhile, the delays are approximate anyway.
666 delay.tv_sec = test->test_params.bulk.tx_delay / 1000000000;
667 delay.tv_nsec = test->test_params.bulk.tx_delay % 1000000000;
668 nanosleep(&delay, NULL);
671 // Now move on to the next transfer
672 USBTEST_BULK_NEXT(test->test_params.bulk);
675 // If all the packets have been transferred this test has passed.
676 if (i >= test->test_params.bulk.number_packets) {
677 test->result_pass = 1;
680 VERBOSE(1, "Test %d bulk OUT on endpoint %d, result %d\n", test->id, test->test_params.bulk.endpoint, test->result_pass);
687 run_test_bulk_in(UsbTest* test)
689 unsigned char* buf = test->buffer;
692 VERBOSE(1, "Starting test %d bulk IN on endpoint %d\n", test->id, test->test_params.bulk.endpoint);
694 for (i = 0; i < test->test_params.bulk.number_packets; i++) {
696 int tx_size = test->test_params.bulk.tx_size;
697 int rx_size = test->test_params.bulk.rx_size;
698 int size_plus_padding;
700 VERBOSE(2, "Bulk IN test %d: iteration %d, rx size %d, tx size %d\n", test->id, i, rx_size, tx_size);
702 if (rx_size < tx_size) {
704 VERBOSE(2, "Bulk IN test %d: iteration %d, packet size reset to %d to match tx size\n",
705 test->id, i, rx_size);
707 test->recovery.endpoint = test->test_params.bulk.endpoint;
708 test->recovery.protocol = USB_ENDPOINT_XFER_BULK;
709 test->recovery.size = rx_size;
711 // Make sure there is no old data lying around
712 if (usbtestdata_none != test->test_params.bulk.data.format) {
713 memset(buf, 0, rx_size);
716 // And do the actual transfer.
717 size_plus_padding = rx_size;
718 if (size_plus_padding < (tx_size + test->test_params.bulk.rx_padding)) {
719 size_plus_padding += test->test_params.bulk.rx_padding;
722 transferred = usb_bulk_message(test->fd, test->test_params.bulk.endpoint, buf, size_plus_padding);
723 } while (0 == transferred);
725 // Has this test run been aborted for some reason?
726 if (current_tests_terminated) {
727 VERBOSE(2, "Bulk IN test %d: iteration %d, termination detected\n", test->id, i);
728 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
729 "Host, bulk IN transfer on endpoint %d: aborted after %d iterations\n",
730 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK, i);
734 // If an error occurred, abort this run.
735 if (-1 == transferred) {
736 char errno_buf[USBTEST_MAX_MESSAGE];
737 test->result_pass = 0;
738 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
739 "Host, bulk IN transfer on endpoint %d : host ioctl() system call failed\n errno %d (%s)",
740 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK, errno,
741 strerror_r(errno, errno_buf, USBTEST_MAX_MESSAGE));
742 VERBOSE(2, "Bulk IN test %d: iteration %d, error:\n %s\n", test->id, i, test->result_message);
746 // Did the target send the expected amount of data?
747 if (transferred < tx_size) {
748 test->result_pass = 0;
749 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
750 "Host, bulk IN transfer on endpoint %d : the target only sent %d bytes when %d were expected",
751 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK, transferred, tx_size);
752 VERBOSE(2, "Bulk IN test %d: iteration %d, error:\n %s\n", test->id, i, test->result_message);
757 // Output the first 32 bytes of data
761 index = snprintf(msg, 255, "Bulk IN test %d: iteration %d, transferred %d\n Data %s:",
762 test->id, i, transferred,
763 (usbtestdata_none == test->test_params.bulk.data.format) ? "(uninitialized)" : "");
765 for (j = 0; ((j + 3) < transferred) && (j < 32); j+= 4) {
766 index += snprintf(msg+index, 255-index, " %02x%02x%02x%02x",
767 buf[j], buf[j+1], buf[j+2], buf[j+3]);
770 index += snprintf(msg+index, 255-index, " ");
771 for ( ; j < transferred; j++) {
772 index += snprintf(msg+index, 255-index, "%02x", buf[j]);
776 VERBOSE(3, "%s\n", msg);
779 // Is the data correct?
780 if (!usbtest_check_buffer(&(test->test_params.bulk.data), buf, tx_size)) {
781 test->result_pass = 0;
782 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
783 "Host, bulk IN transfer on endpoint %d : mismatch between received and expected data",
784 test->test_params.bulk.endpoint & USB_ENDPOINT_NUMBER_MASK);
785 VERBOSE(2, "Bulk IN test %d: iteration %d, error:\n %s\n", test->id, i, test->result_message);
789 if (0 != test->test_params.bulk.rx_delay) {
790 struct timespec delay;
792 VERBOSE(2, "Bulk IN test %d: iteration %d, sleeping for %d nanoseconds\n", test->id, \
793 i, test->test_params.bulk.tx_delay);
794 // Note that nanosleep() can return early due to incoming signals,
795 // with the unelapsed time returned in a second argument. This
796 // allows for a retry loop. In practice this does not seem
797 // worthwhile, the delays are approximate anyway.
798 delay.tv_sec = test->test_params.bulk.rx_delay / 1000000000;
799 delay.tv_nsec = test->test_params.bulk.rx_delay % 1000000000;
800 nanosleep(&delay, NULL);
803 USBTEST_BULK_NEXT(test->test_params.bulk);
807 // If all the packets have been transferred this test has passed.
808 if (i >= test->test_params.bulk.number_packets) {
809 test->result_pass = 1;
812 VERBOSE(1, "Test %d bulk IN on endpoint %d, result %d\n", test->id, test->test_params.bulk.endpoint, test->result_pass);
818 // Receive appropriate packets via the control endpoint. This is somewhat
819 // different from bulk transfers. It is implemented using reserved control
822 // Note: it is not entirely clear that this test is safe. There will be
823 // concurrent control traffic to detect test termination and the like,
824 // and these control messages may interfere with each other. It is not
825 // entirely clear how the Linux kernel handles concurrent control
829 run_test_control_in(UsbTest* test)
831 unsigned char* buf = test->buffer;
835 packet_size = test->test_params.control_in.packet_size_initial;
836 for (i = 0; i < test->test_params.control_in.number_packets; i++) {
839 test->recovery.endpoint = 0;
840 test->recovery.protocol = USB_ENDPOINT_XFER_CONTROL;
841 test->recovery.size = packet_size;
843 // Make sure there is no old data lying around
844 if (usbtestdata_none != test->test_params.control_in.data.format) {
845 memset(buf, 0, packet_size);
848 // And do the actual transfer.
849 transferred = usb_control_message(test->fd, USB_TYPE_RESERVED | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_RESERVED_CONTROL_IN,
850 0, 0, packet_size, buf);
852 // Has this test run been aborted for some reason?
853 if (current_tests_terminated) {
857 // If an error occurred, abort this run.
858 if (-1 == transferred) {
859 char errno_buf[USBTEST_MAX_MESSAGE];
860 test->result_pass = 0;
861 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
862 "Host, control IN transfer: host ioctl() system call failed\n errno %d (%s)",
863 errno, strerror_r(errno, errno_buf, USBTEST_MAX_MESSAGE));
867 // Did the target send the expected amount of data?
868 if (transferred < packet_size) {
869 test->result_pass = 0;
870 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
871 "Host, control IN transfer: the target only sent %d bytes when %d were expected",
872 transferred, packet_size);
876 // Is the data correct?
877 if (!usbtest_check_buffer(&(test->test_params.control_in.data), buf, packet_size)) {
878 test->result_pass = 0;
879 snprintf(test->result_message, USBTEST_MAX_MESSAGE,
880 "Host, control IN transfer: mismatch between received and expected data");
884 USBTEST_CONTROL_NEXT_PACKET_SIZE(packet_size, test->test_params.control_in);
887 // If all the packets have been transferred this test has passed.
888 if (i >= test->test_params.control_in.number_packets) {
889 test->result_pass = 1;
895 // FIXME: add more tests
899 // This utility is invoked from a thread in the thread pool whenever there is
900 // work to be done. It simply dispatches to the appropriate handler.
902 run_test(UsbTest* test)
904 switch (test->which_test) {
905 case usbtest_bulk_out: run_test_bulk_out(test); break;
906 case usbtest_bulk_in: run_test_bulk_in(test); break;
907 case usbtest_control_in: run_test_control_in(test); break;
909 fprintf(stderr, "usbhost: internal error, attempt to execute an unknown test.\n");
917 /*{{{ The thread pool */
919 // ----------------------------------------------------------------------------
920 // A pool of threads and buffers which do the real work. The number of possible
921 // concurrent tests is defined in protocol.h. Each one requires a separate
922 // thread, transfer buffer, semaphore, and some state information.
924 // Although the application is multi-threaded, in practice there is little
925 // need for synchronization. Tests will only be started while the pool threads
926 // are idle. When the pool threads are running the main thread will be waiting
927 // for them all to finish, with a bit of polling to detect error conditions.
928 // The pool threads do not share any data, apart from the file descriptor for
931 typedef struct PoolEntry {
938 static PoolEntry pool[USBTEST_MAX_CONCURRENT_TESTS];
940 // This is the entry point for every thread in the pool. It just loops forever,
941 // waiting until it is supposed to run a test. These threads never actually
942 // exit, instead there should be a call to exit() somewhere.
944 pool_function(void* arg)
946 PoolEntry* pool_entry = (PoolEntry*) arg;
951 ret = sem_wait(&(pool_entry->wakeup));
952 if (ret != 0 && errno != EINTR) {
957 run_test(&(pool_entry->test));
958 pool_entry->running = 0;
964 // Initialize all threads in the pool.
966 pool_initialize(void)
969 for (i = 0; i < USBTEST_MAX_CONCURRENT_TESTS; i++) {
971 pool[i].test.fd = dup(usb_master_fd);
972 if (0 != sem_init(&(pool[i].wakeup), 0, 0)) {
973 fprintf(stderr, "usbhost: internal error, failed to initialize all semaphores.\n");
976 if (0 != pthread_create(&(pool[i].thread), NULL, &pool_function, (void*) &(pool[i]))) {
977 fprintf(stderr, "usbhost: internal error, failed to start all threads.\n");
983 // Allocate a single entry in the thread pool.
987 UsbTest* result = (UsbTest*) 0;
989 if (local_thread_count == USBTEST_MAX_CONCURRENT_TESTS) {
990 fprintf(stderr, "usbhost: internal error, thread resource exhausted.\n");
994 result = &(pool[local_thread_count].test);
995 local_thread_count++;
996 reset_usbtest(result);
1000 // Start all the threads that are supposed to be running tests.
1005 for (i = 0; i < local_thread_count; i++) {
1006 pool[i].running = 1;
1007 sem_post(&(pool[i].wakeup));
1012 /*{{{ Tcl routines */
1014 // ----------------------------------------------------------------------------
1015 // Tcl routines to provide access to the USB device from inside Tcl
1016 // scripts, plus some general utilities. These routines deal mostly
1017 // with preparing a test run. The actual work is done in C: the
1018 // ioctl() operations are not readily accessible from Tcl, and
1019 // operations like filling in buffers and calculating checksums are
1022 /*{{{ pass/fail/abort */
1024 // ----------------------------------------------------------------------------
1025 // Some simple routines accessible from Tcl to get the target to report pass/fail or
1026 // to make the target abort.
1029 tcl_target_pass(ClientData clientData __attribute__ ((unused)),
1035 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_pass <message>\"", TCL_STATIC);
1038 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_PASS, 0, 0, strlen(argv[1]) + 1, argv[1]);
1039 usb_sync(usb_master_fd, -1);
1044 tcl_target_fail(ClientData clientData __attribute__ ((unused)),
1050 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_fail <message>\"", TCL_STATIC);
1053 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_FAIL, 0, 0, strlen(argv[1]) + 1, argv[1]);
1054 usb_sync(usb_master_fd, -1);
1058 // The next three routines cause the target to exit, so a usb_sync() is inappropriate.
1060 tcl_target_pass_exit(ClientData clientData __attribute__ ((unused)),
1066 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_pass_exit <message>\"", TCL_STATIC);
1069 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_PASS_EXIT, 0, 0,
1070 strlen(argv[1]) + 1, argv[1]);
1076 tcl_target_fail_exit(ClientData clientData __attribute__ ((unused)),
1082 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_fail_exit <message>\"", TCL_STATIC);
1085 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_FAIL_EXIT, 0, 0,
1086 strlen(argv[1]) + 1, argv[1]);
1091 tcl_target_abort(ClientData clientData __attribute__ ((unused)),
1094 char** argv __attribute__ ((unused)) )
1097 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_abort\"", TCL_STATIC);
1100 usb_abort(usb_master_fd);
1105 /*{{{ start bulk test */
1107 // ----------------------------------------------------------------------------
1108 // Start a bulk test. The real Tcl interface to this functionality is
1109 // implemented in Tcl: it takes care of figuring out sensible default
1110 // arguments, validating the data, etc. All that this code does is
1111 // allocate a thread and fill in the appropriate data, plus request
1112 // the target-side to do the same thing.
1115 tcl_test_bulk(ClientData clientData __attribute__ ((unused)),
1123 unsigned char request[USBTEST_MAX_CONTROL_DATA];
1126 // The data consists of 28 numbers for UsbTest_Bulk itself, and
1127 // another 10 numbers for the test data definition.
1129 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_test_bulk <message>\"", TCL_STATIC);
1132 for (i = 1; i < 39; i++) {
1134 if (TCL_OK != Tcl_GetInt(interp, argv[i], &discard)) {
1135 Tcl_SetResult(interp, "invalid argument: all arguments should be numbers", TCL_STATIC);
1140 test = pool_allocate();
1141 Tcl_GetInt(interp, argv[1], &(test->test_params.bulk.number_packets));
1142 Tcl_GetInt(interp, argv[2], &(test->test_params.bulk.endpoint));
1143 test->which_test = (USB_DIR_IN == (test->test_params.bulk.endpoint & USB_ENDPOINT_DIR_MASK))
1144 ? usbtest_bulk_in : usbtest_bulk_out;
1145 Tcl_GetInt(interp, argv[ 3], &(test->test_params.bulk.tx_size));
1146 Tcl_GetInt(interp, argv[ 4], &(test->test_params.bulk.tx_size_min));
1147 Tcl_GetInt(interp, argv[ 5], &(test->test_params.bulk.tx_size_max));
1148 Tcl_GetInt(interp, argv[ 6], &(test->test_params.bulk.tx_size_multiplier));
1149 Tcl_GetInt(interp, argv[ 7], &(test->test_params.bulk.tx_size_divisor));
1150 Tcl_GetInt(interp, argv[ 8], &(test->test_params.bulk.tx_size_increment));
1151 Tcl_GetInt(interp, argv[ 9], &(test->test_params.bulk.rx_size));
1152 Tcl_GetInt(interp, argv[10], &(test->test_params.bulk.rx_size_min));
1153 Tcl_GetInt(interp, argv[11], &(test->test_params.bulk.rx_size_max));
1154 Tcl_GetInt(interp, argv[12], &(test->test_params.bulk.rx_size_multiplier));
1155 Tcl_GetInt(interp, argv[13], &(test->test_params.bulk.rx_size_divisor));
1156 Tcl_GetInt(interp, argv[14], &(test->test_params.bulk.rx_size_increment));
1157 Tcl_GetInt(interp, argv[15], &(test->test_params.bulk.rx_padding));
1158 Tcl_GetInt(interp, argv[16], &(test->test_params.bulk.tx_delay));
1159 Tcl_GetInt(interp, argv[17], &(test->test_params.bulk.tx_delay_min));
1160 Tcl_GetInt(interp, argv[18], &(test->test_params.bulk.tx_delay_max));
1161 Tcl_GetInt(interp, argv[19], &(test->test_params.bulk.tx_delay_multiplier));
1162 Tcl_GetInt(interp, argv[20], &(test->test_params.bulk.tx_delay_divisor));
1163 Tcl_GetInt(interp, argv[21], &(test->test_params.bulk.tx_delay_increment));
1164 Tcl_GetInt(interp, argv[22], &(test->test_params.bulk.rx_delay));
1165 Tcl_GetInt(interp, argv[23], &(test->test_params.bulk.rx_delay_min));
1166 Tcl_GetInt(interp, argv[24], &(test->test_params.bulk.rx_delay_max));
1167 Tcl_GetInt(interp, argv[25], &(test->test_params.bulk.rx_delay_multiplier));
1168 Tcl_GetInt(interp, argv[26], &(test->test_params.bulk.rx_delay_divisor));
1169 Tcl_GetInt(interp, argv[27], &(test->test_params.bulk.rx_delay_increment));
1170 Tcl_GetInt(interp, argv[28], &tmp);
1171 test->test_params.bulk.io_mechanism = (usb_io_mechanism) tmp;
1172 Tcl_GetInt(interp, argv[29], &tmp);
1173 test->test_params.bulk.data.format = (usbtestdata) tmp;
1174 Tcl_GetInt(interp, argv[30], &(test->test_params.bulk.data.seed));
1175 Tcl_GetInt(interp, argv[31], &(test->test_params.bulk.data.multiplier));
1176 Tcl_GetInt(interp, argv[32], &(test->test_params.bulk.data.increment));
1177 Tcl_GetInt(interp, argv[33], &(test->test_params.bulk.data.transfer_seed_multiplier));
1178 Tcl_GetInt(interp, argv[34], &(test->test_params.bulk.data.transfer_seed_increment));
1179 Tcl_GetInt(interp, argv[35], &(test->test_params.bulk.data.transfer_multiplier_multiplier));
1180 Tcl_GetInt(interp, argv[36], &(test->test_params.bulk.data.transfer_multiplier_increment));
1181 Tcl_GetInt(interp, argv[37], &(test->test_params.bulk.data.transfer_increment_multiplier));
1182 Tcl_GetInt(interp, argv[38], &(test->test_params.bulk.data.transfer_increment_increment));
1184 VERBOSE(3, "Preparing USB bulk test on endpoint %d, direction %s, for %d packets\n", \
1185 test->test_params.bulk.endpoint, \
1186 (usbtest_bulk_in == test->which_test) ? "IN" : "OUT", \
1187 test->test_params.bulk.number_packets);
1188 VERBOSE(3, " I/O mechanism is %s\n", \
1189 (usb_io_mechanism_usb == test->test_params.bulk.io_mechanism) ? "low-level USB" : \
1190 (usb_io_mechanism_dev == test->test_params.bulk.io_mechanism) ? "devtab" : "<invalid>");
1191 VERBOSE(3, " Data format %s, data1 %d, data* %d, data+ %d, data1* %d, data1+ %d, data** %d, data*+ %d, data+* %d, data++ %d\n",\
1192 (usbtestdata_none == test->test_params.bulk.data.format) ? "none" : \
1193 (usbtestdata_bytefill == test->test_params.bulk.data.format) ? "bytefill" : \
1194 (usbtestdata_wordfill == test->test_params.bulk.data.format) ? "wordfill" : \
1195 (usbtestdata_byteseq == test->test_params.bulk.data.format) ? "byteseq" : \
1196 (usbtestdata_wordseq == test->test_params.bulk.data.format) ? "wordseq" : "<invalid>", \
1197 test->test_params.bulk.data.seed, \
1198 test->test_params.bulk.data.multiplier, \
1199 test->test_params.bulk.data.increment, \
1200 test->test_params.bulk.data.transfer_seed_multiplier, \
1201 test->test_params.bulk.data.transfer_seed_increment, \
1202 test->test_params.bulk.data.transfer_multiplier_multiplier, \
1203 test->test_params.bulk.data.transfer_multiplier_increment, \
1204 test->test_params.bulk.data.transfer_increment_multiplier, \
1205 test->test_params.bulk.data.transfer_increment_increment);
1206 VERBOSE(3, " txsize1 %d, txsize>= %d, txsize<= %d, txsize* %d, txsize/ %d, txsize+ %d\n", \
1207 test->test_params.bulk.tx_size, test->test_params.bulk.tx_size_min, \
1208 test->test_params.bulk.tx_size_max, test->test_params.bulk.tx_size_multiplier, \
1209 test->test_params.bulk.tx_size_divisor, test->test_params.bulk.tx_size_increment);
1210 VERBOSE(3, " rxsize1 %d, rxsize>= %d, rxsize<= %d, rxsize* %d, rxsize/ %d, rxsize+ %d\n", \
1211 test->test_params.bulk.rx_size, test->test_params.bulk.rx_size_min, \
1212 test->test_params.bulk.rx_size_max, test->test_params.bulk.rx_size_multiplier, \
1213 test->test_params.bulk.rx_size_divisor, test->test_params.bulk.rx_size_increment);
1214 VERBOSE(3, " txdelay1 %d, txdelay>= %d, txdelay<= %d, txdelay* %d, txdelay/ %d, txdelay+ %d\n", \
1215 test->test_params.bulk.tx_delay, test->test_params.bulk.tx_delay_min, \
1216 test->test_params.bulk.tx_delay_max, test->test_params.bulk.tx_delay_multiplier, \
1217 test->test_params.bulk.tx_delay_divisor, test->test_params.bulk.tx_delay_increment);
1218 VERBOSE(3, " rxdelay1 %d, rxdelay>= %d, rxdelay<= %d, rxdelay* %d, rxdelay/ %d, rxdelay+ %d\n", \
1219 test->test_params.bulk.rx_delay, test->test_params.bulk.rx_delay_min, \
1220 test->test_params.bulk.rx_delay_max, test->test_params.bulk.rx_delay_multiplier, \
1221 test->test_params.bulk.rx_delay_divisor, test->test_params.bulk.rx_delay_increment);
1224 // That is all the data converted from Tcl to C, and a local thread is set up to handle this
1225 // request. Also set up a thread on the target.
1227 pack_usbtest_bulk(&(test->test_params.bulk), request, &request_index);
1228 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_TEST_BULK, 0, 0, request_index, request);
1229 remote_thread_count++;
1235 /*{{{ start control-in test */
1237 // ----------------------------------------------------------------------------
1238 // Start a control-in test. The real Tcl interface to this
1239 // functionality is implemented in Tcl: it takes care of figuring out
1240 // sensible default arguments, validating the data, etc. All that this
1241 // code does is allocate a thread and fill in the appropriate data,
1242 // plus request the target-side to do the same thing.
1245 tcl_test_control_in(ClientData clientData __attribute__ ((unused)),
1253 unsigned char request[USBTEST_MAX_CONTROL_DATA];
1256 // The data consists of 6 numbers for UsbTest_ControlIn itself, and
1257 // another 10 numbers for the test data definition.
1259 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_test_control_in <message>\"", TCL_STATIC);
1262 for (i = 1; i < 17; i++) {
1264 if (TCL_OK != Tcl_GetInt(interp, argv[i], &discard)) {
1265 Tcl_SetResult(interp, "invalid argument: all arguments should be numbers", TCL_STATIC);
1270 test = pool_allocate();
1271 test->which_test = usbtest_control_in;
1272 Tcl_GetInt(interp, argv[1], &(test->test_params.control_in.number_packets));
1273 Tcl_GetInt(interp, argv[2], &(test->test_params.control_in.packet_size_initial));
1274 Tcl_GetInt(interp, argv[3], &(test->test_params.control_in.packet_size_min));
1275 Tcl_GetInt(interp, argv[4], &(test->test_params.control_in.packet_size_max));
1276 Tcl_GetInt(interp, argv[5], &(test->test_params.control_in.packet_size_multiplier));
1277 Tcl_GetInt(interp, argv[6], &(test->test_params.control_in.packet_size_increment));
1278 Tcl_GetInt(interp, argv[7], &tmp);
1279 test->test_params.bulk.data.format = (usbtestdata) tmp;
1280 Tcl_GetInt(interp, argv[ 8], &(test->test_params.control_in.data.seed));
1281 Tcl_GetInt(interp, argv[ 9], &(test->test_params.control_in.data.multiplier));
1282 Tcl_GetInt(interp, argv[10], &(test->test_params.control_in.data.increment));
1283 Tcl_GetInt(interp, argv[11], &(test->test_params.control_in.data.transfer_seed_multiplier));
1284 Tcl_GetInt(interp, argv[12], &(test->test_params.control_in.data.transfer_seed_increment));
1285 Tcl_GetInt(interp, argv[13], &(test->test_params.control_in.data.transfer_multiplier_multiplier));
1286 Tcl_GetInt(interp, argv[14], &(test->test_params.control_in.data.transfer_multiplier_increment));
1287 Tcl_GetInt(interp, argv[15], &(test->test_params.control_in.data.transfer_increment_multiplier));
1288 Tcl_GetInt(interp, argv[16], &(test->test_params.control_in.data.transfer_increment_increment));
1290 // That is all the data converted from Tcl to C, and a local thread is set up to handle this
1291 // request. Also set up a thread on the target.
1293 pack_usbtest_control_in(&(test->test_params.control_in), request, &request_index);
1294 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_TEST_CONTROL_IN, 0, 0,
1295 request_index, request);
1296 remote_thread_count++;
1302 /*{{{ Cancel the current batch of tests */
1305 tcl_cancel(ClientData clientData __attribute__ ((unused)),
1308 char** argv __attribute__ ((unused)) )
1311 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::cancel\"", TCL_STATIC);
1315 // Send the request on to the target.
1316 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_CANCEL, 0, 0, 0, (void*)0);
1318 // Now cancel all the local tests. This can be done by resetting the counter
1319 // of allocated threads: no actual work will have been started yet.
1320 local_thread_count = 0;
1322 // And synchronise with the target
1323 if (!usb_sync(usb_master_fd, 30)) {
1324 fprintf(stderr, "usbhost: error, target has failed to process test cancel request.\n");
1328 remote_thread_count = 0;
1334 /*{{{ Run a batch of tests */
1336 // ----------------------------------------------------------------------------
1337 // This code does an awful lot of the hard work. Start with various utilities.
1339 // Has the current batch finished as far as the local threads are concerned?
1341 local_batch_finished(void)
1346 for (i = 0; i < local_thread_count; i++) {
1347 if (pool[i].running) {
1355 // Has the current batch finished as far as remote threads are concerned?
1357 remote_batch_finished(void)
1360 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_FINISHED,
1361 0, 0, 1, (void*) buf);
1365 // Perform recovery for a thread on the target. This involves asking the
1366 // target for recovery information, then performing an appropriate
1367 // action. If no data is returned then no recovery is needed for this thread.
1369 recover_remote(int index)
1371 unsigned char buffer[USBTEST_MAX_CONTROL_DATA];
1373 UsbTest_Recovery recovery;
1376 if (0 != usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN,
1377 USBTEST_GET_RECOVERY, 0, index, 12, buffer)) {
1378 // There is work to be done
1380 unpack_usbtest_recovery(&recovery, buffer, &buffer_index);
1382 // We have an endpoint, a protocol, and a size.
1383 if (0 == recovery.endpoint) {
1384 // The target just needs a dummy reserved control message
1385 usb_reliable_control_message(usb_master_fd, USB_TYPE_RESERVED | USB_RECIP_DEVICE, USBTEST_RESERVED_CONTROL_IN,
1386 0, 0, 0, (void*) 0);
1387 } else if (USB_ENDPOINT_XFER_BULK == recovery.protocol) {
1388 // Either we need to send some data to the target, or we need to accept some data.
1389 static unsigned char recovery_buffer[USBTEST_MAX_BULK_DATA + USBTEST_MAX_BULK_DATA_EXTRA];
1391 struct usbdevfs_bulktransfer transfer;
1392 transfer.ep = recovery.endpoint;
1393 transfer.timeout = 2000; // Two seconds. Should be plenty, even for a large bulk transfer.
1394 transfer.data = recovery_buffer;
1395 if (USB_DIR_IN == (recovery.endpoint & USB_ENDPOINT_DIR_MASK)) {
1396 transfer.len = recovery.size;
1401 i = ioctl(usb_master_fd, USBDEVFS_BULK, &transfer);
1404 // There is no recovery support yet for other protocols.
1408 // Perform recovery for a local thread. This involves extracting the
1409 // recovery information from the local thread and asking the target
1410 // to take appropriate action.
1412 recover_local(int index)
1414 unsigned char buffer[USBTEST_MAX_CONTROL_DATA];
1417 if (pool[index].running) {
1419 pack_usbtest_recovery(&(pool[index].test.recovery), buffer, &buffer_index);
1420 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_PERFORM_RECOVERY,
1421 0, 0, buffer_index, (void*) buffer);
1425 // All done, time for a clean-up on both target and host. The latter
1426 // is achieved simply by resetting the thread pool, which actually
1427 // just means resetting the counter since all the threads are blocked
1428 // waiting for the next batch.
1432 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_BATCH_DONE, 0, 0, 0, (void*) NULL);
1433 local_thread_count = 0;
1434 remote_thread_count = 0;
1437 // The main routine, as invoked from Tcl. This takes a single
1438 // argument, a timeout in seconds.
1440 tcl_run(ClientData clientData __attribute__ ((unused)),
1443 char** argv __attribute__ ((unused)) )
1445 struct timespec delay;
1450 unsigned char result_buf[USBTEST_MAX_CONTROL_DATA];
1454 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_run <timeout>\"", TCL_STATIC);
1457 if (TCL_OK != Tcl_GetInt(interp, argv[1], &timeout)) {
1458 Tcl_SetResult(interp, "invalid argument: timeout should be numeric", TCL_STATIC);
1462 VERBOSE(2, "Starting a testrun, timeout %d seconds\n", timeout);
1464 // Start the tests running on the target. The target USB hardware
1465 // will not actually do anything except in response to packets
1466 // from the host, so it is better to start the target before the
1468 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_START, 0, 0, 0, (void*) 0);
1470 // Now the local threads can get going.
1471 current_tests_terminated = 0;
1474 // Now leave the various testing threads to do their thing until
1475 // either side believes that the batch has finished, or until the
1476 // timeout expires. Note that if one side decides that the batch
1477 // has finished but the other disagrees, that in itself indicates
1478 // a test failure of sorts.
1480 // There is a question of polling frequency. Once a second avoids
1481 // excessive polling traffic on the USB bus, and should not impose
1482 // intolerable delays for short-duration tests.
1485 VERBOSE(3, "The tests are running, waiting for termination\n");
1488 nanosleep(&delay, NULL);
1490 } while (((start + timeout) > now) && !local_batch_finished() && !remote_batch_finished());
1492 VERBOSE(2, "Termination detected, time elapsed %ld\n", (long) now - start);
1494 // If either side believes that testing is not complete, things
1495 // get messy. Start by setting the terminated flag. Any tests that
1496 // are actually still running happily but have not finished within
1497 // the timeout should detect this and stop.
1498 if (!local_batch_finished() || !remote_batch_finished()) {
1499 VERBOSE(2, "Testing is not yet complete, setting TERMINATED flag\n");
1500 current_tests_terminated = 1;
1501 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_SET_TERMINATED, 0, 0, 0, (void*) 0);
1502 // And another delay, to give threads a chance to detect the
1506 nanosleep(&delay, NULL);
1509 // If there is still are unfinished threads, recovery action
1510 // is needed. It is not clear whether it is better to unlock
1511 // the local threads first, or the remote threads. For now the
1512 // latter approach is taken.
1513 if (!remote_batch_finished()) {
1515 VERBOSE(2, "Remote threads still running, performing remote recovery\n");
1516 for (i = 0; i < remote_thread_count; i++) {
1519 // Allow the recovery actions to take effect
1522 nanosleep(&delay, NULL);
1525 if (!local_batch_finished()) {
1527 VERBOSE(2, "Local threads still running, performing local recovery\n");
1528 for (i = 0; i < local_thread_count; i++) {
1531 // Allow the recovery actions to take effect
1534 nanosleep(&delay, NULL);
1537 // One last check to make sure that everything is finished. If not,
1538 // testing has broken down and it is necessary to abort.
1539 if (!local_batch_finished() || !remote_batch_finished()) {
1540 VERBOSE(2, "Giving local and remote threads another chance to finish.\n");
1541 // Allow the recovery actions to take effect
1544 nanosleep(&delay, NULL);
1545 if (!local_batch_finished() || !remote_batch_finished()) {
1546 // OK, normality has not been restored.
1547 // It would be nice to get hold of and display any error messages.
1548 usb_abort(usb_master_fd);
1549 fprintf(stderr, "Fatal error: the host test program and the remote target are out of synch.\n");
1550 fprintf(stderr, " recovery has been attempted, without success.\n");
1551 fprintf(stderr, " USB testing cannot continue.\n");
1556 VERBOSE(2, "Local and remote threads are in synch, collecting results.\n");
1558 // The world is in a coherent state. Time to collect the results.
1559 // The return value of this function is a simple boolean. More
1560 // detailed results will be held in a Tcl variable as a list of
1561 // messages. It is desirable to keep both local and remote results
1563 for (i = 0; i < ((local_thread_count < remote_thread_count) ? local_thread_count : remote_thread_count); i++) {
1564 if (!pool[i].test.result_pass) {
1565 Tcl_SetVar(interp, "usbtest::results", pool[i].test.result_message,
1566 all_ok ? (TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT) : (TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT));
1569 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_GET_RESULT,
1570 0, i, USBTEST_MAX_CONTROL_DATA, (void*) result_buf);
1571 if (!result_buf[0]) {
1572 Tcl_SetVar(interp, "usbtest::results", (char *)&(result_buf[1]),
1573 all_ok ? TCL_GLOBAL_ONLY : (TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT));
1577 for (j = i; j < local_thread_count; j++) {
1578 if (!pool[j].test.result_pass) {
1579 Tcl_SetVar(interp, "usbtest::results", pool[j].test.result_message,
1580 all_ok ? TCL_GLOBAL_ONLY : (TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT));
1584 for (j = i; j < remote_thread_count; j++) {
1585 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_GET_RESULT,
1586 0, i, USBTEST_MAX_CONTROL_DATA, (void*) result_buf);
1587 if (!result_buf[0]) {
1588 Tcl_SetVar(interp, "usbtest::results", (char *)&(result_buf[1]),
1589 all_ok ? TCL_GLOBAL_ONLY : (TCL_GLOBAL_ONLY | TCL_APPEND_VALUE | TCL_LIST_ELEMENT));
1593 VERBOSE(2, "Overall test result %d\n", all_ok);
1595 Tcl_SetResult(interp, all_ok ? "1" : "0", TCL_STATIC);
1603 /*{{{ Set verbosity */
1605 // ----------------------------------------------------------------------------
1606 // Allow Tcl scripts to control verbosity levels for both host and target
1608 tcl_host_verbose(ClientData clientData __attribute__ ((unused)),
1616 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::host_verbose <level>\"", TCL_STATIC);
1619 if (TCL_OK != Tcl_GetInt(interp, argv[1], &level)) {
1620 Tcl_SetResult(interp, "invalid argument: verbosity level should be numeric", TCL_STATIC);
1629 tcl_target_verbose(ClientData clientData __attribute__ ((unused)),
1637 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_verbose <level>\"", TCL_STATIC);
1640 if (TCL_OK != Tcl_GetInt(interp, argv[1], &level)) {
1641 Tcl_SetResult(interp, "invalid argument: verbosity level should be numeric", TCL_STATIC);
1645 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_VERBOSE, level, 0, 0, NULL);
1646 usb_sync(usb_master_fd, -1);
1656 // ----------------------------------------------------------------------------
1657 // Application-specific initialization. We have a bare Tcl interpreter ready
1658 // to start executing scripts that define various test cases. However some
1659 // additional functions will have to be added to the interpreter, plus
1660 // information about the various endpoints.
1663 usbhost_appinit(Tcl_Interp* interp)
1665 unsigned char buf[USBTEST_MAX_CONTROL_DATA];
1666 int number_of_endpoints;
1670 // Start by creating a usbtest namespace, for use by the various functions
1672 if (TCL_OK != Tcl_Eval(interp,
1673 "namespace eval usbtest {\n"
1674 " variable number_of_endpoints 0\n"
1675 " array set endpoint [list]\n"
1677 fprintf(stderr, "usbhost: internal error, failed to create Tcl usbtest:: namespace\n");
1678 fprintf(stderr, " Please check Tcl version (8.0b1 or later required).\n");
1682 // Add some information about the install path so that the
1683 // main Tcl script can find and execute test scripts.
1684 location = getenv("USBHOSTDIR");
1685 if (NULL == location) {
1686 location = USBAUXDIR;
1688 Tcl_SetVar(interp, "usbtest::USBAUXDIR", location, TCL_GLOBAL_ONLY);
1690 // Also set the verbosity level correctly
1691 Tcl_SetVar2Ex(interp, "usbtest::verbose", NULL, Tcl_NewIntObj(verbose), TCL_GLOBAL_ONLY);
1693 // Next we need to know the number of endpoints, and for each
1694 // endpoint we want additional information such as type. The
1695 // results are placed in a Tcl array.
1696 usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_ENDPOINT_COUNT,
1698 number_of_endpoints = buf[0];
1699 Tcl_SetVar2Ex(interp, "usbtest::endpoint_count", NULL, Tcl_NewIntObj(number_of_endpoints), TCL_GLOBAL_ONLY);
1701 for (i = 0; i < number_of_endpoints; i++) {
1704 int endpoint_min_size;
1705 int endpoint_max_size;
1708 memset(buf, 0, USBTEST_MAX_CONTROL_DATA);
1709 result = usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN,
1710 USBTEST_ENDPOINT_DETAILS, 0, i, USBTEST_MAX_CONTROL_DATA, buf);
1712 fprintf(stderr, "usbhost: error, received insufficient endpoint data back from the target.\n");
1716 // See protocol.h for the encoding used.
1717 sprintf(varname, "usbtest::endpoint_data(%d,type)", i);
1719 case USB_ENDPOINT_XFER_CONTROL : Tcl_SetVar(interp, varname, "control", TCL_GLOBAL_ONLY); break;
1720 case USB_ENDPOINT_XFER_ISOC : Tcl_SetVar(interp, varname, "isochronous", TCL_GLOBAL_ONLY); break;
1721 case USB_ENDPOINT_XFER_BULK : Tcl_SetVar(interp, varname, "bulk", TCL_GLOBAL_ONLY); break;
1722 case USB_ENDPOINT_XFER_INT : Tcl_SetVar(interp, varname, "interrupt", TCL_GLOBAL_ONLY); break;
1725 sprintf(varname, "usbtest::endpoint_data(%d,number)", i);
1726 Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj((int) buf[1]), TCL_GLOBAL_ONLY);
1728 sprintf(varname, "usbtest::endpoint_data(%d,direction)", i);
1729 if (USB_DIR_OUT == buf[2]) {
1730 Tcl_SetVar(interp, varname, "out", TCL_GLOBAL_ONLY);
1732 Tcl_SetVar(interp, varname, "in", TCL_GLOBAL_ONLY);
1735 sprintf(varname, "usbtest::endpoint_data(%d,max_in_padding)", i);
1736 Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj((int) buf[3]), TCL_GLOBAL_ONLY);
1738 sprintf(varname, "usbtest::endpoint_data(%d,min_size)", i);
1740 endpoint_min_size = unpack_int(buf, &index);
1741 Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj(endpoint_min_size), TCL_GLOBAL_ONLY);
1743 sprintf(varname, "usbtest::endpoint_data(%d,max_size)", i);
1744 endpoint_max_size = unpack_int(buf, &index);
1745 if (USB_ENDPOINT_XFER_CONTROL == buf[0]) {
1746 if (endpoint_max_size > USBTEST_MAX_CONTROL_DATA) {
1747 endpoint_max_size = USBTEST_MAX_CONTROL_DATA;
1750 if ((-1 == endpoint_max_size) || (endpoint_max_size > USBTEST_MAX_BULK_DATA)) {
1751 endpoint_max_size = USBTEST_MAX_BULK_DATA;
1754 Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj(endpoint_max_size), TCL_GLOBAL_ONLY);
1756 sprintf(varname, "usbtest::endpoint_data(%d,devtab)", i);
1757 Tcl_SetVar(interp, varname, (char*) &(buf[12]), TCL_GLOBAL_ONLY);
1759 // Perform any additional endpoint-specific initialization to make
1760 // sure host and target can actually communicate via this endpoint.
1762 case USB_ENDPOINT_XFER_CONTROL :
1764 usb_initialise_control_endpoint(endpoint_min_size, endpoint_max_size);
1767 case USB_ENDPOINT_XFER_ISOC :
1769 if (USB_DIR_OUT == buf[2]) {
1770 usb_initialise_isochronous_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1772 usb_initialise_isochronous_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1776 case USB_ENDPOINT_XFER_BULK :
1778 if (USB_DIR_OUT == buf[2]) {
1779 usb_initialise_bulk_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1781 usb_initialise_bulk_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size, buf[3]);
1786 case USB_ENDPOINT_XFER_INT :
1788 if (USB_DIR_OUT == buf[2]) {
1789 usb_initialise_interrupt_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1791 usb_initialise_interrupt_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1798 // Register appropriate commands with the Tcl interpreter
1799 Tcl_CreateCommand(interp, "usbtest::target_pass", &tcl_target_pass, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1800 Tcl_CreateCommand(interp, "usbtest::target_pass_exit", &tcl_target_pass_exit, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1801 Tcl_CreateCommand(interp, "usbtest::target_fail", &tcl_target_fail, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1802 Tcl_CreateCommand(interp, "usbtest::target_fail_exit", &tcl_target_fail_exit, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1803 Tcl_CreateCommand(interp, "usbtest::target_abort", &tcl_target_abort, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1804 Tcl_CreateCommand(interp, "usbtest::_test_bulk", &tcl_test_bulk, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1805 Tcl_CreateCommand(interp, "usbtest::_test_control_in", &tcl_test_control_in, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1806 Tcl_CreateCommand(interp, "usbtest::_cancel", &tcl_cancel, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1807 Tcl_CreateCommand(interp, "usbtest::_run", &tcl_run, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1808 Tcl_CreateCommand(interp, "usbtest::host_verbose", &tcl_host_verbose, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1809 Tcl_CreateCommand(interp, "usbtest::target_verbose", &tcl_target_verbose, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
1817 // ----------------------------------------------------------------------------
1818 // System start-up. After argument processing this code checks that
1819 // there is a suitable USB target attached - if not then there is no
1820 // point in proceeding. Otherwise further initialization is performed
1821 // and then control is passed to a Tcl interpreter.
1826 printf("usbhost: usage, usbhost [-V|--verbose] [-v|--version] [-h|--help] <test> [args]\n");
1827 printf(" -V, --verbose Make the host-side output additional information\n");
1828 printf(" during test runs. This argument can be repeated to\n");
1829 printf(" increase verbosity.\n");
1830 printf(" -v, --version Output version information for usbhost.\n");
1831 printf(" -h, --help Output this help information.\n");
1832 printf(" <test> The name of a USB test case, for example list.tcl\n");
1833 printf(" [args] Optional additional arguments for the testcase.\n");
1840 printf("usbhost: version %s\n", USBHOST_VERSION);
1841 printf(" : built from USB slave package version %s\n", PKGVERSION);
1842 printf(" : support files installed in %s\n", USBAUXDIR);
1847 main(int argc, char** argv)
1849 char* interpreter = argv[0];
1851 char path[_POSIX_PATH_MAX];
1855 // Argument processing
1856 for (i = 1; i < argc; i++) {
1857 if ((0 == strcmp("-h", argv[i])) || (0 == strcmp("-H", argv[i])) || (0 == strcmp("--help", argv[i]))) {
1860 if ((0 == strcmp("-v", argv[i])) || (0 == strcmp("--version", argv[i]))) {
1863 if ((0 == strcmp("-V", argv[i])) || (0 == strcmp("--verbose", argv[i]))) {
1868 // The first unrecognised argument should correspond to the test script.
1871 argc = (argc - i) + 1;
1872 argv = (argv + i) - 1;
1875 fprintf(stderr, "usbhost: at least one test script must be specified on the command line.\n");
1879 usb_master_fd = usb_open_device();
1880 if (-1 == usb_master_fd) {
1881 return EXIT_FAILURE;
1884 // There is a valid USB target. Initialize the pool of threads etc.
1887 // Now start a Tcl interpreter. Tcl_Main() will interpret the
1888 // first argument as the name of a Tcl script to execute,
1889 // i.e. usbhost.tcl. This can be found in the install tree,
1890 // but during development it is inconvenient to run
1891 // "make install" every time the Tcl script is edited so an
1892 // environment variable can be used to override the location.
1893 new_argv = malloc((argc + 2) * sizeof(char*));
1894 if (NULL == new_argv) {
1895 fprintf(stderr, "usbhost: internal error, out of memory.\n");
1898 new_argv[0] = interpreter;
1900 location = getenv("USBHOSTDIR");
1901 if (NULL == location) {
1902 location = USBAUXDIR;
1904 snprintf(path, _POSIX_PATH_MAX, "%s/usbhost.tcl", location);
1905 if (0 != access(path, R_OK)) {
1906 fprintf(stderr, "usbhost: cannot find or access required Tcl script\n");
1907 fprintf(stderr, " : %s\n", path);
1912 for (i = 1; i < argc; i++) {
1913 new_argv[i+1] = argv[i];
1915 new_argv[i+1] = NULL;
1917 Tcl_Main(i+1, new_argv, &usbhost_appinit);
1919 return EXIT_SUCCESS;