#!/bin/sh
#
# defaults, tweak to suit
# - control: any device can do this
# - out, in:  out needs 'bulk sink' firmware, in needs 'bulk src'
# - loop: needs firmware that will buffer N transfers
#
# the focus here is a steady load through the hcd to the device,
# so the tests that bang mostly on hcd code (like unlink tests)
# aren't used.
#
# "severe stress" loads should add some randomness.
#
TYPES='control out in'

declare -i COUNT BUFLEN

COUNT=50000
BUFLEN=2048


# NOTE:  the 'in' and 'out' cases are usually bulk, but can be
# set up to use interrupt transfers by 'usbtest' module options

do_test ()
{
    if ! ./testusb -a -s $BUFLEN -c $COUNT $* 2>/dev/null
    then
	echo "FAIL"
	exit 1
    fi
}

ARGS="$*"

if [ "$ARGS" = "" ];
then
    ARGS="$TYPES"
fi

CONFIG=''

check_config ()
{
    if [ "$CONFIG" = "" ]; then
	CONFIG=$1
	echo "assuming $CONFIG configuration"
	return
    fi
    if [ "$CONFIG" = $1 ]; then
	return
    fi

    echo "** device must be in $1 config, but it's $CONFIG instead"
    exit 1
}

while : true
do
    echo $(date)

    for TYPE in $ARGS
    do
	# restore defaults
	COUNT=50000
	BUFLEN=2048

	case $TYPE in
	control)
	    # any device, in any configuration, can use this.
	    echo '** Control test cases:'

	    echo "test 9: ch9 postconfig"
	    do_test -t 9 -c 5000
	    echo "test 10: control queueing (some faults omitted)"
	    do_test -t 10 -g 7 -c 50000

	    # FIXME test 8 can be troublesome -- investigate
	    ;;

	out)
	    check_config sink-src
	    echo '** Host Write (OUT) test cases:'

	    echo "test 1: $COUNT transfers, same size"
	    do_test -t 1
	    echo "test 3: $COUNT transfers, variable/short size"
	    do_test -t 3 -v 421

	    COUNT=2000
	    echo "test 5: $COUNT scatterlists, same size entries"
	    do_test -t 5
	    echo "test 7: $COUNT scatterlists, variable size/short entries"
	    do_test -t 7 -v 579
	    ;;

	in)
	    check_config sink-src
	    echo '** Host Read (IN) test cases:'

	    echo "test 2: $COUNT transfers, same size"
	    do_test -t 2
	    echo "test 4: $COUNT transfers, variable size"
	    do_test -t 4

	    COUNT=2000
	    echo "test 6: $COUNT scatterlists, same size entries"
	    do_test -t 6
	    echo "test 8: $COUNT scatterlists, variable size entries"
	    do_test -t 8
	    ;;

	loop)
	    # defaults need too much buffering for ez-usb devices
	    BUFLEN=2048
	    COUNT=32

	    # modprobe g_zero qlen=$COUNT buflen=$BUFLEN loopdefault
	    check_config loopback

	    # FIXME this test case isn't merged yet

	    echo "write $COUNT buffers of $BUFLEN bytes, read them back"
	    : do_test -t13 -v 0

	    echo "write $COUNT variable size buffers, read them back"
	    : do_test -t13 -v 512

	    # FIXME also need a test mode that doesn't set_interface
	    # to clear status after the previous test run

	    ;;

	*)
	    echo "Don't understand test type $TYPE"
	    exit 1;
	esac
	echo ''
    done
done

