]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/usb/slave/v2_0/host/usbhost.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / io / usb / slave / v2_0 / host / usbhost.c
1 /*{{{  Banner                                                   */
2
3 //=================================================================
4 //
5 //        host.c
6 //
7 //        USB testing - host-side
8 //
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.
15 //
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.
19 //
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
23 // for more details.
24 //
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.
28 //
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.
35 //
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.
38 //
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####
45 //
46 // Author(s):     bartv
47 // Date:          2001-07-04
48 //####DESCRIPTIONEND####
49 //==========================================================================
50
51 // The overall architecture is as follows.
52 //
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).
58 //
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
65 // via ioctl()'s.
66
67 /*}}}*/
68 /*{{{  #include's                                               */
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <ctype.h>
74 #include <limits.h>
75 #include <errno.h>
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #include <unistd.h>
79 #include <fcntl.h>
80 #include <sys/ioctl.h>
81 #include <time.h>
82 #include <pthread.h>
83 #include <semaphore.h>
84 // Avoid compatibility problems with Tcl 8.4 vs. earlier
85 #define USE_NON_CONST
86 #include <tcl.h>
87 #include <linux/usb.h>
88 #include <linux/usbdevice_fs.h>
89 #include "../tests/protocol.h"
90 #include "config.h"
91
92 /*}}}*/
93
94 /*{{{  Backwards compatibility                                  */
95
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
104 #endif
105
106 /*}}}*/
107 /*{{{  Statics                                                  */
108
109 // ----------------------------------------------------------------------------
110 // Statics.
111
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;
117
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;
122
123 // A similar counter for remote threads.
124 static int          remote_thread_count     = 0;
125
126 // A file handle for manipulating the USB device at a low level
127 static int          usb_master_fd  = -1;
128
129 /*}}}*/
130 /*{{{  Logging                                                  */
131
132 // ----------------------------------------------------------------------------
133 // The user can provide one or more -V/--verbose arguments to increase
134 // the amount of output generated.
135
136 static int verbose = 0;
137
138 #define VERBOSE(_level_, _format_, _args_...)   \
139     do {                                        \
140         if (verbose >= _level_) {               \
141             printf(_format_, ## _args_);        \
142         }                                       \
143     } while (0);
144
145 /*}}}*/
146 /*{{{  Low-level USB access                                     */
147
148 // ----------------------------------------------------------------------------
149 // Low-level access to a USB device.
150 //
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.
156 //
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.
162
163 // How to access USB devices from userland
164 #define USB_ROOT        "/proc/bus/usb/"
165
166 // How to identify the eCos test case
167 #define PRODUCT_STRING    "Red Hat eCos USB test"
168
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
173 // line:
174 //   T:  Bus=<abc> ... Dev#= <def> ...
175 //
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.
178 //
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.
182
183 static int
184 usb_scan_devices(int* bus, int* dev)
185 {
186     FILE*       devs_file;
187     int         current_bus     = -1;
188     int         current_dev     = -1;
189     int         ch;
190
191     *bus = -1;
192     *dev = -1;
193
194     VERBOSE(1, "Searching " USB_ROOT "devices for the eCos USB test code\n");
195     
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");
199         return 0;
200     }
201     ch = getc(devs_file);
202     while (EOF != ch) {
203         if ('T' == ch) {
204             if (2 !=fscanf(devs_file, ":  Bus=%d %*[^D\n]Dev#=%d", &current_bus, &current_dev)) { 
205                 current_bus = -1;
206                 current_dev = -1;
207             }
208         } else if ('S' == ch) {
209             int start = 0, end = 0;
210             if (EOF != fscanf(devs_file, ":  Product=%n" PRODUCT_STRING "%n", &start, &end)) {
211                 if (start < end) {
212                     *bus = current_bus;
213                     *dev = current_dev;
214                     break;
215                 }
216             } 
217         }
218         // Move to the end of the current line.
219         while ((EOF != ch) && ('\n' != ch)) {
220             ch = getc(devs_file);
221         } 
222         if (EOF != ch) {
223             ch = getc(devs_file);
224         }
225     }
226     
227     fclose(devs_file);
228     if ((-1 != *bus) && (-1 != *dev)) {
229         VERBOSE(1, "Found eCos USB test code on bus %d, device %d\n", *bus, *dev);
230         return 1;
231     }
232     fprintf(stderr, "usbhost: error, failed to find a USB device \"" PRODUCT_STRING "\"\n");
233     return 0;
234 }
235
236 // Actually open the USB device, allowing subsequent ioctl() operations.
237 //
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.
242 static int
243 usb_open_device(void)
244 {
245     char devname[_POSIX_PATH_MAX];
246     static int  bus = -1;
247     static int  dev = -1;
248     int         result;
249     
250     if ((-1 == bus) || (-1 == dev)) {
251         if (!usb_scan_devices(&bus, &dev)) {
252             return -1;
253         }
254     }
255     
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");
258         exit(EXIT_FAILURE);
259     }
260
261     VERBOSE(1, "Attempting to access USB target via %s\n", devname);
262     
263     result = open(devname, O_RDWR);
264     if (-1 == result) {
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];
271
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");
275                 exit(EXIT_FAILURE);
276             }
277             (void) system(command_name);
278             result = open(devname, O_RDWR);
279         }
280     }
281     if (-1 == result) {
282         fprintf(stderr, "usbhost: error, failed to open \"%s\", errno %d\n", devname, errno);
283         exit(EXIT_FAILURE);
284     }
285
286     VERBOSE(1, "USB device now accessible via file descriptor %d\n", result);
287     
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);
292     return result;
293 }
294
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.
298 //
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.
304 static int
305 usb_control_message(int fd, int request_type, int request, int value, int index, int length, void* data)
306 {
307     struct usbdevfs_ctrltransfer        transfer;
308     int         result = 0;
309
310     VERBOSE(3, "usb_control_message, request %02x, len %d\n", request, length);
311     
312     if (length > USBTEST_MAX_CONTROL_DATA) {
313         fprintf(stderr, "usbhost: internal error, control message involves too much data.\n");
314         exit(EXIT_FAILURE);
315     }
316
317 #if 1
318     // Workaround - send additional data in the index and length fields.
319     if ((length > 0) && (USB_DIR_OUT == (USB_ENDPOINT_DIR_MASK & request_type))) {
320         int i;
321         unsigned char*  buf = (unsigned char*) data;
322         
323         for (i = 0; i < length; i+= 4) {
324             int this_len = length - 1;
325             int ioctl_result;
326             
327             transfer.bRequestType   = USB_TYPE_CLASS | USB_RECIP_DEVICE;
328             if (this_len > 4) {
329                 this_len = 4;
330             }
331             switch (this_len) {
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;
336               default:
337                 fprintf(stderr, "usbhost: internal error, confusion about transfer length.\n");
338                 exit(EXIT_FAILURE);
339             }
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;
345
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");
351                 exit(EXIT_FAILURE);
352             }
353         }
354         // There is no more data to be transferred.
355         length = 0;
356     }
357 #endif    
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;
365
366     result = ioctl(fd, USBDEVFS_CONTROL, &transfer);
367     return result;
368 }
369
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.
372 static int
373 usb_reliable_control_message(int fd, int request_type, int request, int value, int index, int length, void* data)
374 {
375     int result = usb_control_message(fd, request_type, request, value, index, length, data);
376     if (-1 == result) {
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));
379         exit(EXIT_FAILURE);
380     }
381     return result;
382 }
383
384     
385 // Either send or receive a single bulk message. The top bit of the endpoint
386 // number indicates the direction.
387 static int
388 usb_bulk_message(int fd, int endpoint, unsigned char* buffer, int length)
389 {
390     struct  usbdevfs_bulktransfer    transfer;
391     int     result;
392     
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
402     // code was invoked.
403     transfer.data       = buffer;
404     errno               = 0;
405     result = ioctl(fd, USBDEVFS_BULK, &transfer);
406     return result;
407 }
408
409
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.
413 //
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.
416 static int
417 usb_sync(int fd, int limit)
418 {
419     unsigned char   buf[1];
420     struct timespec delay;
421     int             loops   = 0;
422     int             result  = 0;
423
424     VERBOSE(2, "Synchronizing with target\n");
425     
426     while (1) {
427         buf[0]  = 0;
428         usb_reliable_control_message(fd, USB_TYPE_CLASS | USB_RECIP_DEVICE | USB_DIR_IN, USBTEST_SYNCH, 0, 0, 1, buf);
429         if (buf[0]) {
430             result = 1;
431             break;
432         } else {
433             if ((-1 != limit) && (++loops > limit)) {
434                 break;
435             } else {
436                 VERBOSE(3, "Not yet synchronized, sleeping\n");
437                 delay.tv_sec    = 0;
438                 delay.tv_nsec   = 100000000;    // 100 ms
439                 nanosleep(&delay, NULL);
440             }
441         }
442     }
443     VERBOSE(2, "%s\n", result ? "Synchronized" : "Not synchronized");
444     return result;
445 }
446
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.
449 static void
450 usb_abort(int fd)
451 {
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);
454 }
455
456 /*}}}*/
457 /*{{{  Initialise endpoints                                     */
458
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.
466 //
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.
472
473 static void
474 usb_initialise_control_endpoint(int min_size, int max_size)
475 {
476     // At this time there are no known problems on any hardware
477     // that would need to be addressed
478 }
479
480 static void
481 usb_initialise_isochronous_in_endpoint(int number, int min_size, int max_size)
482 {
483     // At this time there are no known problems on any hardware
484     // that would need to be addressed
485 }
486
487 static void
488 usb_initialise_isochronous_out_endpoint(int number, int min_size, int max_size)
489 {
490     // At this time there are no known problems on any hardware
491     // that would need to be addressed
492 }
493
494 static void
495 usb_initialise_bulk_in_endpoint(int number, int min_size, int max_size, int padding)
496 {
497     // At this time there are no known problems on any hardware
498     // that would need to be addressed
499 }
500
501 static void
502 usb_initialise_bulk_out_endpoint(int number, int min_size, int max_size)
503 {
504     unsigned char buf[1];
505
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
511     // sync.
512     VERBOSE(2, "Performing bulk OUT initialization on endpoint %d\n", number);
513     
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);
518 }
519
520 static void
521 usb_initialise_interrupt_in_endpoint(int number, int min_size, int max_size)
522 {
523     // At this time there are no known problems on any hardware
524     // that would need to be addressed
525 }
526
527 static void
528 usb_initialise_interrupt_out_endpoint(int number, int min_size, int max_size)
529 {
530     // At this time there are no known problems on any hardware
531     // that would need to be addressed
532 }
533
534 /*}}}*/
535 /*{{{  Host/target common code                                  */
536
537 #define HOST
538 #include "../tests/common.c"
539
540 /*}}}*/
541 /*{{{  The test cases themselves                                */
542
543 /*{{{  UsbTest definition                                       */
544
545 // ----------------------------------------------------------------------------
546 // All the data associated with a single test.
547
548 typedef struct UsbTest {
549
550     // A "unique" identifier to make verbose output easier to understand.
551     int                 id;          
552     // Which file descriptor should be used to access USB.
553     int                 fd;
554     
555     // Which test should be run.
556     usbtest             which_test;
557
558     // Test-specific details.
559     union {
560         UsbTest_Bulk        bulk;
561         UsbTest_ControlIn   control_in;
562     } test_params;
563
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
566     // side.
567     UsbTest_Recovery    recovery;
568
569     int                 result_pass;
570     char                result_message[USBTEST_MAX_MESSAGE];
571     unsigned char       buffer[USBTEST_MAX_BULK_DATA + USBTEST_MAX_BULK_DATA_EXTRA];
572 } UsbTest;
573
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.
577 static void
578 reset_usbtest(UsbTest* test)
579 {
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';
586 }
587
588 /*}}}*/
589 /*{{{  bulk OUT                                                 */
590
591 static void
592 run_test_bulk_out(UsbTest* test)
593 {
594     unsigned char*  buf     = test->buffer;
595     int             i;
596
597     VERBOSE(1, "Starting test %d, bulk OUT on endpoint %d\n", test->id, test->test_params.bulk.endpoint);
598     
599     for (i = 0; i < test->test_params.bulk.number_packets; i++) {
600         int transferred;
601         int packet_size = test->test_params.bulk.tx_size;
602
603         test->recovery.endpoint     = test->test_params.bulk.endpoint;
604         test->recovery.protocol     = USB_ENDPOINT_XFER_BULK;
605         test->recovery.size         = packet_size;
606         
607         usbtest_fill_buffer(&(test->test_params.bulk.data), buf, packet_size);
608         if (verbose < 3) {
609             VERBOSE(2, "Bulk OUT test %d: iteration %d, packet size %d\n", test->id, i, packet_size);
610         } else {
611             // Output the first 32 bytes of data as well.
612             char msg[256];
613             int  index;
614             int  j;
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)" : "");
618
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]);
622             }
623             if (j < 32) {
624                 index += snprintf(msg+index, 255-index, " ");
625                 for ( ; j < packet_size; j++) {
626                     index += snprintf(msg+index, 255-index, "%02x", buf[j]);
627                 }
628                 
629             }
630             VERBOSE(3, "%s\n", msg);
631         }
632
633         transferred = usb_bulk_message(test->fd, test->test_params.bulk.endpoint, buf, packet_size);
634         
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);
642             return;
643         }
644
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);
654             break;
655         }
656
657         if (0 != test->test_params.bulk.tx_delay) {
658             struct timespec delay;
659             
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);
669         }
670
671         // Now move on to the next transfer
672         USBTEST_BULK_NEXT(test->test_params.bulk);
673     }
674
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;
678     }
679     
680     VERBOSE(1, "Test %d bulk OUT on endpoint %d, result %d\n", test->id, test->test_params.bulk.endpoint, test->result_pass);
681 }
682
683 /*}}}*/
684 /*{{{  bulk IN                                                  */
685
686 static void
687 run_test_bulk_in(UsbTest* test)
688 {
689     unsigned char*  buf     = test->buffer;
690     int             i;
691
692     VERBOSE(1, "Starting test %d bulk IN on endpoint %d\n", test->id, test->test_params.bulk.endpoint);
693     
694     for (i = 0; i < test->test_params.bulk.number_packets; i++) {
695         int transferred;
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;
699         
700         VERBOSE(2, "Bulk IN test %d: iteration %d, rx size %d, tx size %d\n", test->id, i, rx_size, tx_size);
701         
702         if (rx_size < tx_size) {
703             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);
706         }
707         test->recovery.endpoint     = test->test_params.bulk.endpoint;
708         test->recovery.protocol     = USB_ENDPOINT_XFER_BULK;
709         test->recovery.size         = rx_size;
710
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);
714         }
715
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;
720         }
721         do {
722             transferred = usb_bulk_message(test->fd, test->test_params.bulk.endpoint, buf, size_plus_padding);
723         } while (0 == transferred);
724         
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);
731             return;
732         }
733
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);
743             break;
744         }
745
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);
753             break;
754         }
755         
756         if (verbose >= 3) {
757             // Output the first 32 bytes of data
758             char msg[256];
759             int  index;
760             int  j;
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)" : "");
764
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]);
768             }
769             if (j < 32) {
770                 index += snprintf(msg+index, 255-index, " ");
771                 for ( ; j < transferred; j++) {
772                     index += snprintf(msg+index, 255-index, "%02x", buf[j]);
773                 }
774                 
775             }
776             VERBOSE(3, "%s\n", msg);
777         }
778         
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);
786             break;
787         }
788         
789         if (0 != test->test_params.bulk.rx_delay) {
790             struct timespec delay;
791             
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);
801         }
802         
803         USBTEST_BULK_NEXT(test->test_params.bulk);
804     }
805
806     
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;
810     }
811
812     VERBOSE(1, "Test %d bulk IN on endpoint %d, result %d\n", test->id, test->test_params.bulk.endpoint, test->result_pass);
813 }
814
815 /*}}}*/
816 /*{{{  control IN                                               */
817
818 // Receive appropriate packets via the control endpoint. This is somewhat
819 // different from bulk transfers. It is implemented using reserved control
820 // messages.
821 //
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
826 // operations.
827
828 static void
829 run_test_control_in(UsbTest* test)
830 {
831     unsigned char*  buf     = test->buffer;
832     int             packet_size;
833     int             i;
834
835     packet_size = test->test_params.control_in.packet_size_initial;
836     for (i = 0; i < test->test_params.control_in.number_packets; i++) {
837         int transferred;
838         
839         test->recovery.endpoint     = 0;
840         test->recovery.protocol     = USB_ENDPOINT_XFER_CONTROL;
841         test->recovery.size         = packet_size;
842
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);
846         }
847
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);
851
852         // Has this test run been aborted for some reason?
853         if (current_tests_terminated) {
854             return;
855         }
856
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));
864             break;
865         }
866
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);
873             break;
874         }
875         
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");
881             break;
882         }
883         
884         USBTEST_CONTROL_NEXT_PACKET_SIZE(packet_size, test->test_params.control_in);
885     }
886
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;
890     }
891 }
892
893 /*}}}*/
894
895 // FIXME: add more tests
896
897 /*{{{  run_test()                                               */
898
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.
901 static void
902 run_test(UsbTest* test)
903 {
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;
908       default:
909         fprintf(stderr, "usbhost: internal error, attempt to execute an unknown test.\n");
910         exit(EXIT_FAILURE);
911     }
912 }
913
914 /*}}}*/
915
916 /*}}}*/
917 /*{{{  The thread pool                                          */
918
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.
923 //
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
929 // the USB device.
930
931 typedef struct PoolEntry {
932     pthread_t       thread;
933     sem_t           wakeup;
934     int             running;
935     UsbTest         test;
936 } PoolEntry;
937
938 static PoolEntry pool[USBTEST_MAX_CONCURRENT_TESTS];
939
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.
943 static void*
944 pool_function(void* arg)
945 {
946     PoolEntry*  pool_entry  = (PoolEntry*) arg;
947     int ret;
948     
949     for ( ; ; ) {
950         do {
951              ret = sem_wait(&(pool_entry->wakeup));
952              if (ret != 0 && errno != EINTR) {
953                   perror("sem_wait");
954                   exit(1);
955              }
956         } while (ret != 0);
957         run_test(&(pool_entry->test));
958         pool_entry->running = 0;
959     }
960
961     return NULL;
962 }
963
964 // Initialize all threads in the pool.
965 static void
966 pool_initialize(void)
967 {
968     int i;
969     for (i = 0; i < USBTEST_MAX_CONCURRENT_TESTS; i++) {
970         pool[i].running = 0;
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");
974             exit(EXIT_FAILURE);
975         }
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");
978             exit(EXIT_FAILURE);
979         }
980     }
981 }
982
983 // Allocate a single entry in the thread pool.
984 static UsbTest*
985 pool_allocate(void)
986 {
987     UsbTest* result = (UsbTest*) 0;
988
989     if (local_thread_count == USBTEST_MAX_CONCURRENT_TESTS) {
990         fprintf(stderr, "usbhost: internal error, thread resource exhausted.\n");
991         exit(EXIT_FAILURE);
992     }
993
994     result = &(pool[local_thread_count].test);
995     local_thread_count++;
996     reset_usbtest(result);
997     return result;
998 }
999
1000 // Start all the threads that are supposed to be running tests.
1001 static void
1002 pool_start(void)
1003 {
1004     int i;
1005     for (i = 0; i < local_thread_count; i++) {
1006         pool[i].running = 1;
1007         sem_post(&(pool[i].wakeup));
1008     }
1009 }
1010
1011 /*}}}*/
1012 /*{{{  Tcl routines                                             */
1013
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
1020 // cpu-intensive.
1021
1022 /*{{{  pass/fail/abort                                          */
1023
1024 // ----------------------------------------------------------------------------
1025 // Some simple routines accessible from Tcl to get the target to report pass/fail or
1026 // to make the target abort.
1027
1028 static int
1029 tcl_target_pass(ClientData     clientData  __attribute__ ((unused)),
1030                 Tcl_Interp*    interp,
1031                 int            argc,
1032                 char**         argv)
1033 {
1034     if (2 != argc) {
1035         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_pass <message>\"", TCL_STATIC);
1036         return TCL_ERROR;
1037     }
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);
1040     return TCL_OK;
1041 }
1042
1043 static int
1044 tcl_target_fail(ClientData     clientData  __attribute__ ((unused)),
1045                 Tcl_Interp*    interp,
1046                 int            argc,
1047                 char**         argv)
1048 {
1049     if (2 != argc) {
1050         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_fail <message>\"", TCL_STATIC);
1051         return TCL_ERROR;
1052     }
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);
1055     return TCL_OK;
1056 }
1057
1058 // The next three routines cause the target to exit, so a usb_sync() is inappropriate.
1059 static int
1060 tcl_target_pass_exit(ClientData     clientData  __attribute__ ((unused)),
1061                      Tcl_Interp*    interp,
1062                      int            argc,
1063                      char**         argv)
1064 {
1065     if (2 != argc) {
1066         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_pass_exit <message>\"", TCL_STATIC);
1067         return TCL_ERROR;
1068     }
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]);
1071     return TCL_OK;
1072 }
1073
1074
1075 static int
1076 tcl_target_fail_exit(ClientData     clientData  __attribute__ ((unused)),
1077                      Tcl_Interp*    interp,
1078                      int            argc,
1079                      char**         argv)
1080 {
1081     if (2 != argc) {
1082         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_fail_exit <message>\"", TCL_STATIC);
1083         return TCL_ERROR;
1084     }
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]);
1087     return TCL_OK;
1088 }
1089
1090 static int
1091 tcl_target_abort(ClientData     clientData  __attribute__ ((unused)),
1092                  Tcl_Interp*    interp,
1093                  int            argc,
1094                  char**         argv        __attribute__ ((unused)) )
1095 {
1096     if (1 != argc) {
1097         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_abort\"", TCL_STATIC);
1098         return TCL_ERROR;
1099     }
1100     usb_abort(usb_master_fd);
1101     return TCL_OK;
1102 }
1103
1104 /*}}}*/
1105 /*{{{  start bulk test                                          */
1106
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.
1113
1114 static int
1115 tcl_test_bulk(ClientData     clientData  __attribute__ ((unused)),
1116               Tcl_Interp*    interp,
1117               int            argc,
1118               char**         argv)
1119 {
1120     int             i;
1121     int             tmp;
1122     UsbTest*        test;
1123     unsigned char   request[USBTEST_MAX_CONTROL_DATA];
1124     int             request_index;
1125
1126     // The data consists of 28 numbers for UsbTest_Bulk itself, and
1127     // another 10 numbers for the test data definition.
1128     if (39 != argc) {
1129         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_test_bulk <message>\"", TCL_STATIC);
1130         return TCL_ERROR;
1131     }
1132     for (i = 1; i < 39; i++) {
1133         int discard;
1134         if (TCL_OK != Tcl_GetInt(interp, argv[i], &discard)) {
1135             Tcl_SetResult(interp, "invalid argument: all arguments should be numbers", TCL_STATIC);
1136             return TCL_ERROR;
1137         }
1138     }
1139
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));
1183
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);
1222             
1223     
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.
1226     request_index = 0;
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++;
1230     
1231     return TCL_OK;
1232 }
1233
1234 /*}}}*/
1235 /*{{{  start control-in test                                    */
1236
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.
1243
1244 static int
1245 tcl_test_control_in(ClientData     clientData  __attribute__ ((unused)),
1246                     Tcl_Interp*    interp,
1247                     int            argc,
1248                     char**         argv)
1249 {
1250     int             i;
1251     int             tmp;
1252     UsbTest*        test;
1253     unsigned char   request[USBTEST_MAX_CONTROL_DATA];
1254     int             request_index;
1255         
1256     // The data consists of 6 numbers for UsbTest_ControlIn itself, and
1257     // another 10 numbers for the test data definition.
1258     if (17 != argc) {
1259         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_test_control_in <message>\"", TCL_STATIC);
1260         return TCL_ERROR;
1261     }
1262     for (i = 1; i < 17; i++) {
1263         int discard;
1264         if (TCL_OK != Tcl_GetInt(interp, argv[i], &discard)) {
1265             Tcl_SetResult(interp, "invalid argument: all arguments should be numbers", TCL_STATIC);
1266             return TCL_ERROR;
1267         }
1268     }
1269
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));
1289
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.
1292     request_index = 0;
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++;
1297     
1298     return TCL_OK;
1299 }
1300
1301 /*}}}*/
1302 /*{{{  Cancel the current batch of tests                        */
1303
1304 static int
1305 tcl_cancel(ClientData     clientData    __attribute__ ((unused)),
1306            Tcl_Interp*    interp,
1307            int            argc,
1308            char**         argv          __attribute__ ((unused)) )
1309 {
1310     if (1 != argc) {
1311         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::cancel\"", TCL_STATIC);
1312         return TCL_ERROR;
1313     }
1314
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);
1317
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;
1321
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");
1325         exit(EXIT_FAILURE);
1326         
1327     }
1328     remote_thread_count     = 0;
1329     
1330     return TCL_OK;
1331 }
1332
1333 /*}}}*/
1334 /*{{{  Run a batch of tests                                     */
1335
1336 // ----------------------------------------------------------------------------
1337 // This code does an awful lot of the hard work. Start with various utilities.
1338
1339 // Has the current batch finished as far as the local threads are concerned?
1340 static int
1341 local_batch_finished(void)
1342 {
1343     int result = 1;
1344     int i;
1345
1346     for (i = 0; i < local_thread_count; i++) {
1347         if (pool[i].running) {
1348             result = 0;
1349             break;
1350         }
1351     }
1352     return result;
1353 }
1354
1355 // Has the current batch finished as far as remote threads are concerned?
1356 static int
1357 remote_batch_finished(void)
1358 {
1359     char buf[1];
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);
1362     return buf[0];
1363 }
1364
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.
1368 static void
1369 recover_remote(int index)
1370 {
1371     unsigned char       buffer[USBTEST_MAX_CONTROL_DATA];
1372     int                 buffer_index;
1373     UsbTest_Recovery    recovery;
1374     int                 i;
1375
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
1379         buffer_index = 0;
1380         unpack_usbtest_recovery(&recovery, buffer, &buffer_index);
1381
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];
1390             
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;
1397             } else {
1398                 transfer.len = 1;
1399             }
1400             errno = 0;
1401             i = ioctl(usb_master_fd, USBDEVFS_BULK, &transfer);
1402         }
1403
1404         // There is no recovery support yet for other protocols.
1405     }
1406 }
1407
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.
1411 static void
1412 recover_local(int index)
1413 {
1414     unsigned char   buffer[USBTEST_MAX_CONTROL_DATA];
1415     int             buffer_index;
1416
1417     if (pool[index].running) {
1418         buffer_index = 0;
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);
1422     }
1423 }
1424
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.
1429 static void
1430 run_done(void)
1431 {
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;
1435 }
1436
1437 // The main routine, as invoked from Tcl. This takes a single
1438 // argument, a timeout in seconds.
1439 static int
1440 tcl_run(ClientData     clientData    __attribute__ ((unused)),
1441         Tcl_Interp*    interp,
1442         int            argc,
1443         char**         argv          __attribute__ ((unused)) )
1444 {
1445     struct timespec delay;
1446     int             timeout;
1447     time_t          start;
1448     time_t          now;
1449     int             i, j;
1450     unsigned char   result_buf[USBTEST_MAX_CONTROL_DATA];
1451     int             all_ok;
1452     
1453     if (2 != argc) {
1454         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_run <timeout>\"", TCL_STATIC);
1455         return TCL_ERROR;
1456     }
1457     if (TCL_OK != Tcl_GetInt(interp, argv[1], &timeout)) {
1458         Tcl_SetResult(interp, "invalid argument: timeout should be numeric", TCL_STATIC);
1459         return TCL_ERROR;
1460     }
1461     
1462     VERBOSE(2, "Starting a testrun, timeout %d seconds\n", timeout);
1463     
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
1467     // local threads.
1468     usb_reliable_control_message(usb_master_fd, USB_TYPE_CLASS | USB_RECIP_DEVICE, USBTEST_START, 0, 0, 0, (void*) 0);
1469
1470     // Now the local threads can get going.
1471     current_tests_terminated = 0;
1472     pool_start();
1473
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.
1479     //
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.
1483     start = time(NULL);
1484     do {
1485         VERBOSE(3, "The tests are running, waiting for termination\n");
1486         delay.tv_sec    = 1;
1487         delay.tv_nsec   = 0;
1488         nanosleep(&delay, NULL);
1489         now = time(NULL);
1490     } while (((start + timeout) > now) && !local_batch_finished() && !remote_batch_finished());
1491
1492     VERBOSE(2, "Termination detected, time elapsed %ld\n", (long) now - start);
1493
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
1503         // flag's update
1504         delay.tv_sec    = 1;
1505         delay.tv_nsec   = 0;
1506         nanosleep(&delay, NULL);
1507     }
1508
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()) {
1514         int i;
1515         VERBOSE(2, "Remote threads still running, performing remote recovery\n");
1516         for (i = 0; i < remote_thread_count; i++) {
1517             recover_remote(i);
1518         }
1519         // Allow the recovery actions to take effect
1520         delay.tv_sec    = 1;
1521         delay.tv_nsec   = 0;
1522         nanosleep(&delay, NULL);
1523     }
1524
1525     if (!local_batch_finished()) {
1526         int i;
1527         VERBOSE(2, "Local threads still running, performing local recovery\n");
1528         for (i = 0; i < local_thread_count; i++) {
1529             recover_local(i);
1530         }
1531         // Allow the recovery actions to take effect
1532         delay.tv_sec    = 1;
1533         delay.tv_nsec   = 0;
1534         nanosleep(&delay, NULL);
1535     }
1536
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
1542         delay.tv_sec    = 5;
1543         delay.tv_nsec   = 0;
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");
1552             exit(EXIT_FAILURE);
1553         }
1554     }
1555
1556     VERBOSE(2, "Local and remote threads are in synch, collecting results.\n");
1557     
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
1562     // in order.
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));
1567             all_ok = 0;
1568         }
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));
1574             all_ok = 0;
1575         }
1576     }
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));
1581             all_ok = 0;
1582         }
1583     }
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));
1590             all_ok = 0;
1591         }
1592     }
1593     VERBOSE(2, "Overall test result %d\n", all_ok);
1594     
1595     Tcl_SetResult(interp, all_ok ? "1" : "0", TCL_STATIC);
1596     
1597     run_done();
1598     
1599     return TCL_OK;
1600 }
1601
1602 /*}}}*/
1603 /*{{{  Set verbosity                                            */
1604
1605 // ----------------------------------------------------------------------------
1606 // Allow Tcl scripts to control verbosity levels for both host and target
1607 static int
1608 tcl_host_verbose(ClientData     clientData    __attribute__ ((unused)),
1609                   Tcl_Interp*    interp,
1610                   int            argc,
1611                   char**         argv)
1612 {
1613     int level;
1614     
1615     if (2 != argc) {
1616         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::host_verbose <level>\"", TCL_STATIC);
1617         return TCL_ERROR;
1618     }
1619     if (TCL_OK != Tcl_GetInt(interp, argv[1], &level)) {
1620         Tcl_SetResult(interp, "invalid argument: verbosity level should be numeric", TCL_STATIC);
1621         return TCL_ERROR;
1622     }
1623
1624     verbose = level;
1625     return TCL_OK;
1626 }
1627
1628 static int
1629 tcl_target_verbose(ClientData     clientData    __attribute__ ((unused)),
1630                    Tcl_Interp*    interp,
1631                    int            argc,
1632                    char**         argv)
1633 {
1634     int level;
1635     
1636     if (2 != argc) {
1637         Tcl_SetResult(interp, "wrong # args: should be \"usbtest::target_verbose <level>\"", TCL_STATIC);
1638         return TCL_ERROR;
1639     }
1640     if (TCL_OK != Tcl_GetInt(interp, argv[1], &level)) {
1641         Tcl_SetResult(interp, "invalid argument: verbosity level should be numeric", TCL_STATIC);
1642         return TCL_ERROR;
1643     }
1644     
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);
1647
1648     return TCL_OK;
1649 }
1650
1651 /*}}}*/
1652
1653 /*}}}*/
1654 /*{{{  AppInit()                                                */
1655
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.
1661
1662 static int
1663 usbhost_appinit(Tcl_Interp* interp)
1664 {
1665     unsigned char   buf[USBTEST_MAX_CONTROL_DATA];
1666     int             number_of_endpoints;
1667     int             i;
1668     char*           location;
1669     
1670     // Start by creating a usbtest namespace, for use by the various functions
1671     // and variables.
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"
1676                            "}\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");
1679         exit(EXIT_FAILURE);
1680     }
1681
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;
1687     }
1688     Tcl_SetVar(interp, "usbtest::USBAUXDIR", location, TCL_GLOBAL_ONLY);
1689
1690     // Also set the verbosity level correctly
1691     Tcl_SetVar2Ex(interp, "usbtest::verbose", NULL, Tcl_NewIntObj(verbose), TCL_GLOBAL_ONLY);
1692     
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,
1697                                  0, 0, 1, buf);
1698     number_of_endpoints = buf[0];
1699     Tcl_SetVar2Ex(interp, "usbtest::endpoint_count", NULL, Tcl_NewIntObj(number_of_endpoints), TCL_GLOBAL_ONLY);
1700     
1701     for (i = 0; i < number_of_endpoints; i++) {
1702         char            varname[256];
1703         int             result;
1704         int             endpoint_min_size;
1705         int             endpoint_max_size;
1706         int             index;
1707         
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);
1711         if (result < 13) {
1712             fprintf(stderr, "usbhost: error, received insufficient endpoint data back from the target.\n");
1713             exit(EXIT_FAILURE);
1714         }
1715
1716         // See protocol.h for the encoding used.
1717         sprintf(varname, "usbtest::endpoint_data(%d,type)", i);
1718         switch(buf[0]) {
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;
1723         }
1724
1725         sprintf(varname, "usbtest::endpoint_data(%d,number)", i);
1726         Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj((int) buf[1]), TCL_GLOBAL_ONLY);
1727
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);
1731         } else {
1732             Tcl_SetVar(interp, varname, "in", TCL_GLOBAL_ONLY);
1733         }
1734
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);
1737
1738         sprintf(varname, "usbtest::endpoint_data(%d,min_size)", i);
1739         index               = 4;
1740         endpoint_min_size   = unpack_int(buf, &index);
1741         Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj(endpoint_min_size), TCL_GLOBAL_ONLY);
1742             
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;
1748             }
1749         } else {
1750             if ((-1 == endpoint_max_size) || (endpoint_max_size > USBTEST_MAX_BULK_DATA)) {
1751                 endpoint_max_size = USBTEST_MAX_BULK_DATA;
1752             }
1753         }
1754         Tcl_SetVar2Ex(interp, varname, NULL, Tcl_NewIntObj(endpoint_max_size), TCL_GLOBAL_ONLY);
1755
1756         sprintf(varname, "usbtest::endpoint_data(%d,devtab)", i);
1757         Tcl_SetVar(interp, varname, (char*) &(buf[12]), TCL_GLOBAL_ONLY);
1758
1759         // Perform any additional endpoint-specific initialization to make
1760         // sure host and target can actually communicate via this endpoint.
1761         switch(buf[0]) {
1762           case USB_ENDPOINT_XFER_CONTROL    :
1763           {
1764               usb_initialise_control_endpoint(endpoint_min_size, endpoint_max_size);
1765               break;
1766           }
1767           case USB_ENDPOINT_XFER_ISOC       :
1768           {
1769               if (USB_DIR_OUT == buf[2]) {
1770                   usb_initialise_isochronous_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1771               } else {
1772                   usb_initialise_isochronous_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1773               }
1774               break;
1775           }
1776           case USB_ENDPOINT_XFER_BULK       :
1777           {
1778               if (USB_DIR_OUT == buf[2]) {
1779                   usb_initialise_bulk_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1780               } else {
1781                   usb_initialise_bulk_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size, buf[3]);
1782               }
1783               
1784               break;
1785           }
1786           case USB_ENDPOINT_XFER_INT        :
1787           {
1788               if (USB_DIR_OUT == buf[2]) {
1789                   usb_initialise_interrupt_out_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1790               } else {
1791                   usb_initialise_interrupt_in_endpoint(buf[1], endpoint_min_size, endpoint_max_size);
1792               }
1793               break;
1794           }
1795         }
1796     }
1797
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);
1810
1811     return TCL_OK;
1812 }
1813
1814 /*}}}*/
1815 /*{{{  main()                                                   */
1816
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.
1822
1823 static void
1824 usage(void)
1825 {
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");
1834     exit(0);
1835 }
1836
1837 static void
1838 version(void)
1839 {
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);
1843     exit(0);
1844 }
1845
1846 int
1847 main(int argc, char** argv)
1848 {
1849     char*   interpreter = argv[0];
1850     char**  new_argv;
1851     char    path[_POSIX_PATH_MAX];
1852     char*   location;
1853     int     i;
1854
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]))) {
1858             usage();
1859         }
1860         if ((0 == strcmp("-v", argv[i])) || (0 == strcmp("--version", argv[i]))) {
1861             version();
1862         }
1863         if ((0 == strcmp("-V", argv[i])) || (0 == strcmp("--verbose", argv[i]))) {
1864             verbose++;
1865             continue;
1866         }
1867
1868         // The first unrecognised argument should correspond to the test script.
1869         break;
1870     }
1871     argc  = (argc - i) + 1;
1872     argv  = (argv + i) - 1;
1873
1874     if (1 == argc) {
1875         fprintf(stderr, "usbhost: at least one test script must be specified on the command line.\n");
1876         exit(EXIT_FAILURE);
1877     }
1878
1879     usb_master_fd = usb_open_device();
1880     if (-1 == usb_master_fd) {
1881         return EXIT_FAILURE;
1882     }
1883
1884     // There is a valid USB target. Initialize the pool of threads etc.
1885     pool_initialize();
1886
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");
1896         exit(EXIT_FAILURE);
1897     }
1898     new_argv[0] = interpreter;
1899
1900     location = getenv("USBHOSTDIR");
1901     if (NULL == location) {
1902         location = USBAUXDIR;
1903     }
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);
1908         exit(EXIT_FAILURE);
1909     }
1910     new_argv[1] = path;
1911
1912     for (i = 1; i < argc; i++) {
1913         new_argv[i+1] = argv[i];
1914     }
1915     new_argv[i+1]   = NULL;
1916
1917     Tcl_Main(i+1, new_argv, &usbhost_appinit);
1918     
1919     return EXIT_SUCCESS;
1920 }
1921
1922 /*}}}*/