2 * Copyright (C) 2008 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static unsigned arg_size = 4096;
39 static unsigned arg_count = 4096;
46 return (((long long) tv.tv_sec) * ((long long) 1000000)) +
47 (((long long) tv.tv_usec));
50 int printifc(usb_ifc_info *info)
52 printf("dev: csp=%02x/%02x/%02x v=%04x p=%04x ",
53 info->dev_class, info->dev_subclass, info->dev_protocol,
54 info->dev_vendor, info->dev_product);
55 printf("ifc: csp=%02x/%02x/%02x%s%s\n",
56 info->ifc_class, info->ifc_subclass, info->ifc_protocol,
57 info->has_bulk_in ? " in" : "",
58 info->has_bulk_out ? " out" : "");
62 int match_null(usb_ifc_info *info)
64 if(info->dev_vendor != 0x18d1) return -1;
65 if(info->ifc_class != 0xff) return -1;
66 if(info->ifc_subclass != 0xfe) return -1;
67 if(info->ifc_protocol != 0x01) return -1;
71 int match_zero(usb_ifc_info *info)
73 if(info->dev_vendor != 0x18d1) return -1;
74 if(info->ifc_class != 0xff) return -1;
75 if(info->ifc_subclass != 0xfe) return -1;
76 if(info->ifc_protocol != 0x02) return -1;
80 int match_loop(usb_ifc_info *info)
82 if(info->dev_vendor != 0x18d1) return -1;
83 if(info->ifc_class != 0xff) return -1;
84 if(info->ifc_subclass != 0xfe) return -1;
85 if(info->ifc_protocol != 0x03) return -1;
89 int test_null(usb_handle *usb)
92 unsigned char buf[4096];
93 memset(buf, 0xee, 4096);
97 for(i = 0; i < arg_count; i++) {
98 if(usb_write(usb, buf, arg_size) != arg_size) {
99 fprintf(stderr,"write failed (%s)\n", strerror(errno));
104 fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0));
108 int test_zero(usb_handle *usb)
111 unsigned char buf[4096];
115 for(i = 0; i < arg_count; i++) {
116 if(usb_read(usb, buf, arg_size) != arg_size) {
117 fprintf(stderr,"read failed (%s)\n", strerror(errno));
122 fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0));
129 ifc_match_func match;
130 int (*test)(usb_handle *usb);
133 { "list", printifc, 0, "list interfaces" },
134 { "send", match_null, test_null, "send to null interface" },
135 { "recv", match_zero, test_zero, "recv from zero interface" },
136 { "loop", match_loop, 0, "exercise loopback interface" },
144 fprintf(stderr,"usage: usbtest <testname>\n\navailable tests:\n");
145 for(i = 0; tests[i].cmd; i++) {
146 fprintf(stderr," %-8s %s\n", tests[i].cmd, tests[i].help);
151 int process_args(int argc, char **argv)
155 if(!strncmp(arg,"count=",6)) {
156 arg_count = atoi(arg + 6);
157 } else if(!strncmp(arg,"size=",5)) {
158 arg_size = atoi(arg + 5);
160 fprintf(stderr,"unknown argument: %s\n", arg);
166 fprintf(stderr,"count may not be zero\n");
170 if(arg_size > 4096) {
171 fprintf(stderr,"size may not be greater than 4096\n");
178 int main(int argc, char **argv)
187 if(process_args(argc - 2, argv + 2))
191 for(i = 0; tests[i].cmd; i++) {
192 if(!strcmp(argv[1], tests[i].cmd)) {
193 usb = usb_open(tests[i].match);
196 fprintf(stderr,"usbtest: %s: could not find interface\n",
200 if(tests[i].test(usb)) {
201 fprintf(stderr,"usbtest: %s: FAIL\n", tests[i].cmd);
204 fprintf(stderr,"usbtest: %s: OKAY\n", tests[i].cmd);