3 #===============================================================================
7 # Support for USB testing
9 #===============================================================================
10 #####ECOSGPLCOPYRIGHTBEGIN####
11 ## -------------------------------------------
12 ## This file is part of eCos, the Embedded Configurable Operating System.
13 ## Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 ## eCos is free software; you can redistribute it and/or modify it under
16 ## the terms of the GNU General Public License as published by the Free
17 ## Software Foundation; either version 2 or (at your option) any later version.
19 ## eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 ## WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 ## You should have received a copy of the GNU General Public License along
25 ## with eCos; if not, write to the Free Software Foundation, Inc.,
26 ## 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 ## As a special exception, if other files instantiate templates or use macros
29 ## or inline functions from this file, or you compile this file and link it
30 ## with other works to produce a work based on this file, this file does not
31 ## by itself cause the resulting work to be covered by the GNU General Public
32 ## License. However the source code for this file must still be made available
33 ## in accordance with section (3) of the GNU General Public License.
35 ## This exception does not invalidate any other reasons why a work based on
36 ## this file might be covered by the GNU General Public License.
38 ## Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
39 ## at http://sources.redhat.com/ecos/ecos-license/
40 ## -------------------------------------------
41 #####ECOSGPLCOPYRIGHTEND####
42 #===============================================================================
43 ######DESCRIPTIONBEGIN####
47 # Purpose: To provide higher-level utility commands for performing
48 # USB testing, and to iterate through the various test scripts
49 # specified on the command line.
51 #####DESCRIPTIONEND####
52 #===============================================================================
59 # Given the raw endpoint data provided by the C code, turn
60 # it something more usable from inside Tcl scripts.
61 namespace eval usbtest {
64 variable bulk_in_endpoints [list]
66 variable bulk_out_endpoints [list]
69 variable isochronous_in_endpoints [list]
70 array set isochronous_in {}
71 variable isochronous_out_endpoints [list]
72 array set isochronous_out {}
74 variable interrupt_in_endpoints [list]
75 array set interrupt_in {}
76 variable interrupt_out_endpoints [list]
77 array set interrupt_out {}
79 for { set i 0 } { $i < $usbtest::endpoint_count } { incr i } {
80 switch -- $usbtest::endpoint_data($i,type) {
82 set usbtest::control(min_size) $usbtest::endpoint_data($i,min_size)
83 set usbtest::control(max_size) $usbtest::endpoint_data($i,max_size)
87 set number $usbtest::endpoint_data($i,number)
88 if { "in" == $usbtest::endpoint_data($i,direction) } {
89 lappend usbtest::bulk_in_endpoints $number
90 set usbtest::bulk_in($number,min_size) $usbtest::endpoint_data($i,min_size)
91 set usbtest::bulk_in($number,max_size) $usbtest::endpoint_data($i,max_size)
92 set usbtest::bulk_in($number,max_in_padding) $usbtest::endpoint_data($i,max_in_padding)
93 set usbtest::bulk_in($number,devtab) $usbtest::endpoint_data($i,devtab)
95 lappend usbtest::bulk_out_endpoints $number
96 set usbtest::bulk_out($number,min_size) $usbtest::endpoint_data($i,min_size)
97 set usbtest::bulk_out($number,max_size) $usbtest::endpoint_data($i,max_size)
98 set usbtest::bulk_out($number,devtab) $usbtest::endpoint_data($i,devtab)
103 set number $usbtest::endpoint_data($i,number)
104 if { "in" == $usbtest::endpoint_data($i,direction) } {
105 lappend usbtest::isochronous_in_endpoints $number
106 set usbtest::isochronous_in($number,min_size) $usbtest::endpoint_data($i,min_size)
107 set usbtest::isochronous_in($number,max_size) $usbtest::endpoint_data($i,max_size)
108 set usbtest::isochronous_in($number,devtab) $usbtest::endpoint_data($i,devtab)
110 lappend usbtest::isochronous_out_endpoints $number
111 set usbtest::isochronous_out($number,min_size) $usbtest::endpoint_data($i,min_size)
112 set usbtest::isochronous_out($number,max_size) $usbtest::endpoint_data($i,max_size)
113 set usbtest::isochronous_out($number,devtab) $usbtest::endpoint_data($i,devtab)
118 set number $usbtest::endpoint_data($i,number)
119 if { "in" == $usbtest::endpoint_data($i,direction) } {
120 lappend usbtest::interrupt_in_endpoints $number
121 set usbtest::interrupt_in($number,min_size) $usbtest::endpoint_data($i,min_size)
122 set usbtest::interrupt_in($number,max_size) $usbtest::endpoint_data($i,max_size)
123 set usbtest::interrupt_in($number,devtab) $usbtest::endpoint_data($i,devtab)
125 lappend usbtest::interrupt_out_endpoints $number
126 set usbtest::interrupt_out($number,min_size) $usbtest::endpoint_data($i,min_size)
127 set usbtest::interrupt_out($number,max_size) $usbtest::endpoint_data($i,max_size)
128 set usbtest::interrupt_out($number,devtab) $usbtest::endpoint_data($i,devtab)
133 puts stderr "Internal error: invalid endpoint type $usbtest::endpoint_data($i,type)"
143 # The C code expects to receive certain data as simple numbers,
144 # corresponding to #define's in common.c and elsewhere. Strictly
145 # speaking it would be better to pass strings to the C code and
146 # have it do the translation, thus ensuring that these constants
147 # exist in only one place.
149 namespace eval usbtest {
151 variable _USB_DIR_IN 0x0080
152 variable _DATA_NONE 0
153 variable _DATA_BYTE_FILL 1
154 variable _DATA_WORD_FILL 2
155 variable _DATA_BYTE_GEN 3
156 variable _DATA_WORD_GEN 4
157 variable _IO_MECHANISM_USB 1
158 variable _IO_MECHANISM_DEV 2
161 # It is also desirable to have some constants corresponding
162 # to common random number generators.
163 namespace eval usbtest {
164 variable MULTIPLIER 1103515245
165 variable INCREMENT 12345
169 # {{{ Argument processing
171 # ----------------------------------------------------------------------------
172 # Given a list of arguments of the form "xyzzy=123" or "xyzzy 123", and
173 # an array arguments containing entries such as arguments(xyzzy) and
174 # already filled in with default values, update the array using the
176 namespace eval usbtest {
178 proc process_arguments { list array_ref } {
179 upvar $array_ref array
180 array set defined_args [list]
182 set listlen [llength $list]
183 for { set index 0 } { $index < $listlen } { incr index } {
184 set arg [lindex $list $index]
186 foreach name [array names array] {
187 set len [string length $name]
188 if { [string equal -length $len $name $arg] } {
189 # Partial match found.
190 if { [string equal $name $arg] } {
191 # Exact match found, The value must be the next arg.
192 if { [info exists defined_args($name)] } {
193 error "Argument $name should be specified only once"
196 if { $index >= $listlen } {
197 error "Missing value after argument $name"
199 set array($name) [lindex $list $index]
204 # Not an exact match. Try looking for x=y
206 if { [string equal -length $len "$name=" $arg] } {
207 if { [info exists defined_args($name)] } {
208 error "Argument $name should be specified only once"
210 set array($name) [string range $arg $len end]
217 error "Invalid argument $arg"
224 # {{{ Starting and ending tests
226 # This section deals with starting tests, or cleaning up when the
227 # tests cannot actually proceed. Also there is some validation,
228 # for example to make sure that no endpoint number is used for
231 namespace eval usbtest {
233 variable _tests_submitted 0
234 variable _control_endpoint_in_use 0
235 variable _in_endpoints_in_use [list]
236 variable _out_endpoints_in_use [list]
239 if { 0 != $usbtest::_tests_submitted } {
241 set usbtest::_tests_submitted 0
244 set usbtest::_in_endpoints_in_use [list]
245 set usbtest::_out_endpoints_in_use [list]
248 proc use_endpoint { endpoint direction } {
249 if { 0 == $endpoint } {
250 if { $usbtest::_control_endpoint_in_use } {
251 error "Attempt to run multiple tests on the control endpoint"
253 set usbtest::_control_endpoint_in_use 1
255 switch -- $direction {
257 if { -1 != [lsearch -exact $usbtest::_in_endpoints_in_use $endpoint] } {
258 error "Attempt to run multiple IN tests on endpoint $endpoint"
260 lappend usbtest::_in_endpoints_in_use $endpoint
264 if { -1 != [lsearch -exact $usbtest::_out_endpoints_in_use $endpoint] } {
265 error "Attempt to run multiple OUT tests on endpoint $endpoint"
267 lappend usbtest::_out_endpoints_in_use $endpoint
271 error "Invalid direction passed to usbtest::use_endpoint"
277 proc test_submitted { } {
278 incr usbtest::_tests_submitted
281 proc start { timeout } {
283 if { 0 == $usbtest::_tests_submitted } {
284 error "Attempt to start tests when no tests are scheduled to run."
285 } elseif { ! [string is integer -strict $timeout] } {
286 error "Invalid timeout specified, it should be a simple number."
288 set usbtest::results [list]
289 set result [usbtest::_run $timeout]
290 set usbtest::_tests_submitted 0
291 set usbtest::_control_endpoint_in_use 0
292 array unset _in_endpoints_in_use
293 array unset _out_endpoints_in_use
302 # Prepare to run a bulk test.
304 # This test requires rather a lot of parameters, many of which
305 # will have sensible defaults.
307 namespace eval usbtest {
309 proc bulktest { endpoint direction number_packets args } {
312 # Parameters to be passed to the C code. Most are
313 # held in an array indexed by the option name,
314 # facilitating command-line parsing.
315 set arguments(format) "none"
316 set arguments(data1) 0
317 set arguments(data*) 1
318 set arguments(data+) 0
319 set arguments(data1*) 1
320 set arguments(data1+) 0
321 set arguments(data**) 1
322 set arguments(data*+) 0
323 set arguments(data+*) 1
324 set arguments(data++) 0
325 set arguments(mechanism) "usb"
326 set arguments(txsize1) 32
327 set arguments(txsize>=) 0
328 set arguments(txsize<=) -1
329 set arguments(txsize*) 1
330 set arguments(txsize/) 1
331 set arguments(txsize+) 0
332 set arguments(rxsize1) 0
333 set arguments(rxsize>=) 0
334 set arguments(rxsize<=) -1
335 set arguments(rxsize*) 1
336 set arguments(rxsize/) 1
337 set arguments(rxsize+) 0
338 set arguments(txdelay1) 0
339 set arguments(txdelay>=) 0
340 set arguments(txdelay<=) 1000000000
341 set arguments(txdelay*) 1
342 set arguments(txdelay/) 1
343 set arguments(txdelay+) 0
344 set arguments(rxdelay1) 0
345 set arguments(rxdelay>=) 0
346 set arguments(rxdelay<=) 1000000000
347 set arguments(rxdelay*) 1
348 set arguments(rxdelay/) 1
349 set arguments(rxdelay+) 0
351 set endpoint_param ""
354 set target_min_size 0
355 set target_max_size 0
359 # Start by validating the endpoint and direction arguments.
360 # Also check that the specified endpoint is not yet in use.
361 if { ![string is integer -strict $endpoint] } {
362 error "Invalid endpoint argument \"$endpoint\": should be a number"
364 if { ($endpoint < 1) || ($endpoint > 15) } {
365 error "Invalid bulk endpoint argument \"$endpoint\": should be between 1 and 15"
367 switch -- $direction {
372 if { -1 == [lsearch -exact $usbtest::bulk_in_endpoints $endpoint] } {
373 error "Invalid bulk endpoint argument \"$endpoint\": the target does not list that as a bulk IN endpoint"
375 set target_min_size $usbtest::bulk_in($endpoint,min_size)
376 set target_max_size $usbtest::bulk_in($endpoint,max_size)
377 set target_padding $usbtest::bulk_in($endpoint,max_in_padding)
378 set target_devtab $usbtest::bulk_in($endpoint,devtab);
379 set endpoint_param [expr $endpoint | $usbtest::_USB_DIR_IN]
386 if { -1 == [lsearch -exact $usbtest::bulk_out_endpoints $endpoint] } {
387 error "Invalid bulk endpoint argument \"$endpoint\": the target does not list that as a bulk OUT endpoint"
389 set target_min_size $usbtest::bulk_out($endpoint,min_size)
390 set target_max_size $usbtest::bulk_out($endpoint,max_size)
391 set target_devtab $usbtest::bulk_out($endpoint,devtab);
392 set target_padding 0; # Not applicable
393 set endpoint_param $endpoint
397 error "Invalid direction argument \"$direction\": should be \"in\" or \"out\""
401 # Now parse any remaining arguments
402 usbtest::process_arguments $args arguments
404 # Convert two of the arguments from strings to numbers, for the
405 # convenience of the C code
406 switch -- $arguments(format) {
407 "none" { set arguments(format) $usbtest::_DATA_NONE }
408 "bytefill" { set arguments(format) $usbtest::_DATA_BYTE_FILL }
409 "wordfill" { set arguments(format) $usbtest::_DATA_WORD_FILL }
410 "byteseq" { set arguments(format) $usbtest::_DATA_BYTE_GEN }
411 "wordseq" { set arguments(format) $usbtest::_DATA_WORD_GEN }
414 error "Invalid data format argument \"$arguments(data)\"\n \
415 Should be \"none\", \"bytefill\", \"wordfill\", \"byteseq\" or \"wordseq\""
418 switch -- $arguments(mechanism) {
419 "usb" { set arguments(mechanism) $usbtest::_IO_MECHANISM_USB }
420 "devtab" { set arguments(mechanism) $usbtest::_IO_MECHANISM_DEV }
423 error "Invalid mechanism argument \"$arguments(mechanism)\"\n \
424 Should be \"usb\" or \"devtab\""
428 puts "validating fields"
429 # Validate the remaining fields
430 foreach field [list data1 data* data+ data1* data1+ data** data*+ data+* data++ \
431 txsize1 txsize>= txsize<= txsize* txsize/ txsize+ \
432 rxsize1 rxsize>= rxsize<= rxsize* rxsize/ rxsize+ \
433 txdelay1 txdelay>= txdelay<= txdelay* txdelay/ txdelay+ \
434 rxdelay1 rxdelay>= rxdelay<= rxdelay* rxdelay/ rxdelay+] {
435 if { ![string is integer -strict $arguments($field)] } {
436 error "Invalid value \"$arguments($field)\" for argument $field, should be an integer."
440 if { $arguments(txsize>=) < $target_min_size } {
441 set arguments(txsize>=) $target_min_size
443 if { (-1 == $arguments(txsize<=) ) || ($arguments(txsize<=) > $target_max_size) } {
444 set arguments(txsize<=) $target_max_size
446 if { $arguments(rxsize<=) == -1 } {
447 set arguments(rxsize<=) $target_max_size
449 if { $arguments(txsize1) < $arguments(txsize>=) } {
450 set arguments(txsize1) $arguments(txsize>=)
452 # Make sure the endpoint is not already in use
453 usbtest::use_endpoint $endpoint $direction
455 puts "Submitting test"
456 # Now submit the test. This is handled by C code.
457 usbtest::_test_bulk \
460 $arguments(txsize1) \
461 $arguments(txsize>=) \
462 $arguments(txsize<=) \
463 $arguments(txsize*) \
464 $arguments(txsize/) \
465 $arguments(txsize+) \
466 $arguments(rxsize1) \
467 $arguments(rxsize>=) \
468 $arguments(rxsize<=) \
469 $arguments(rxsize*) \
470 $arguments(rxsize/) \
471 $arguments(rxsize+) \
473 $arguments(txdelay1) \
474 $arguments(txdelay>=) \
475 $arguments(txdelay<=) \
476 $arguments(txdelay*) \
477 $arguments(txdelay/) \
478 $arguments(txdelay+) \
479 $arguments(rxdelay1) \
480 $arguments(rxdelay>=) \
481 $arguments(rxdelay<=) \
482 $arguments(rxdelay*) \
483 $arguments(rxdelay/) \
484 $arguments(rxdelay+) \
485 $arguments(mechanism) \
502 # {{{ Execute the specified test script
504 # Interpret the arguments as a test script plus auxiliary data.
505 set script [lindex $::argv 0]
506 set ::argv [lrange $::argv 1 end]
509 set path [file join [pwd] $script]
510 if { ![file exists $path] } {
512 if { ![file exists $path] } {
513 set path [file join $usbtest::USBAUXDIR $script]
514 if { ![file exists $path] } {
516 if { ![file exists $path] } {
517 error "Error: unknown test script $script"
527 if { 0 != $result } {