aboutsummaryrefslogtreecommitdiffstats
path: root/tools/usb
diff options
context:
space:
mode:
authorDavid Brownell <dbrownell@users.sourceforge.net>2010-05-05 06:53:17 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2010-05-20 16:21:43 -0400
commit2201d6b1620a1d9feac78e9ff12b7246227c8b17 (patch)
tree0d655fa6a26ebdce962525906e776160f650d93f /tools/usb
parent93f2aa4ddd25caac2b9a09538da54308dbda44e2 (diff)
USB: testusb: an USB testing application
The testusb program just issues ioctls to perform the tests implemented by the kernel driver. It can generate a variety of transfer patterns; you should make sure to test both regular streaming and mixes of transfer sizes (including short transfers). For more information on how this can be used and on USB testing refer to <URL:http://www.linux-usb.org/usbtest/>. Signed-off-by: Michal Nazarewicz <m.nazarewicz@samsung.com> Cc: Kyungmin Park <kyungmin.park@samsung.com> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'tools/usb')
-rw-r--r--tools/usb/testusb.c427
1 files changed, 427 insertions, 0 deletions
diff --git a/tools/usb/testusb.c b/tools/usb/testusb.c
new file mode 100644
index 000000000000..1f372983be6d
--- /dev/null
+++ b/tools/usb/testusb.c
@@ -0,0 +1,427 @@
1/* $(CROSS_COMPILE)cc -Wall -g -lpthread -o testusb testusb.c */
2
3/*
4 * Copyright (c) 2002 by David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <stdio.h>
22#include <string.h>
23#include <ftw.h>
24#include <stdlib.h>
25#include <pthread.h>
26#include <unistd.h>
27#include <errno.h>
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32
33#include <sys/ioctl.h>
34#include <linux/usbdevice_fs.h>
35
36/*-------------------------------------------------------------------------*/
37
38#define TEST_CASES 30
39
40// FIXME make these public somewhere; usbdevfs.h?
41
42struct usbtest_param {
43 // inputs
44 unsigned test_num; /* 0..(TEST_CASES-1) */
45 unsigned iterations;
46 unsigned length;
47 unsigned vary;
48 unsigned sglen;
49
50 // outputs
51 struct timeval duration;
52};
53#define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param)
54
55/*-------------------------------------------------------------------------*/
56
57/* #include <linux/usb_ch9.h> */
58
59struct usb_device_descriptor {
60 __u8 bLength;
61 __u8 bDescriptorType;
62 __u16 bcdUSB;
63 __u8 bDeviceClass;
64 __u8 bDeviceSubClass;
65 __u8 bDeviceProtocol;
66 __u8 bMaxPacketSize0;
67 __u16 idVendor;
68 __u16 idProduct;
69 __u16 bcdDevice;
70 __u8 iManufacturer;
71 __u8 iProduct;
72 __u8 iSerialNumber;
73 __u8 bNumConfigurations;
74} __attribute__ ((packed));
75
76enum usb_device_speed {
77 USB_SPEED_UNKNOWN = 0, /* enumerating */
78 USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
79 USB_SPEED_HIGH /* usb 2.0 */
80};
81
82/*-------------------------------------------------------------------------*/
83
84static char *speed (enum usb_device_speed s)
85{
86 switch (s) {
87 case USB_SPEED_UNKNOWN: return "unknown";
88 case USB_SPEED_LOW: return "low";
89 case USB_SPEED_FULL: return "full";
90 case USB_SPEED_HIGH: return "high";
91 default: return "??";
92 }
93}
94
95struct testdev {
96 struct testdev *next;
97 char *name;
98 pthread_t thread;
99 enum usb_device_speed speed;
100 unsigned ifnum : 8;
101 unsigned forever : 1;
102 int test;
103
104 struct usbtest_param param;
105};
106static struct testdev *testdevs;
107
108static int is_testdev (struct usb_device_descriptor *dev)
109{
110 /* FX2 with (tweaked) bulksrc firmware */
111 if (dev->idVendor == 0x0547 && dev->idProduct == 0x1002)
112 return 1;
113
114 /*----------------------------------------------------*/
115
116 /* devices that start up using the EZ-USB default device and
117 * which we can use after loading simple firmware. hotplug
118 * can fxload it, and then run this test driver.
119 *
120 * we return false positives in two cases:
121 * - the device has a "real" driver (maybe usb-serial) that
122 * renumerates. the device should vanish quickly.
123 * - the device doesn't have the test firmware installed.
124 */
125
126 /* generic EZ-USB FX controller */
127 if (dev->idVendor == 0x0547 && dev->idProduct == 0x2235)
128 return 1;
129
130 /* generic EZ-USB FX2 controller */
131 if (dev->idVendor == 0x04b4 && dev->idProduct == 0x8613)
132 return 1;
133
134 /* CY3671 development board with EZ-USB FX */
135 if (dev->idVendor == 0x0547 && dev->idProduct == 0x0080)
136 return 1;
137
138 /* Keyspan 19Qi uses an21xx (original EZ-USB) */
139 if (dev->idVendor == 0x06cd && dev->idProduct == 0x010b)
140 return 1;
141
142 /*----------------------------------------------------*/
143
144 /* "gadget zero", Linux-USB test software */
145 if (dev->idVendor == 0x0525 && dev->idProduct == 0xa4a0)
146 return 1;
147
148 /* user mode subset of that */
149 if (dev->idVendor == 0x0525 && dev->idProduct == 0xa4a4)
150 return 1;
151
152 /* iso version of usermode code */
153 if (dev->idVendor == 0x0525 && dev->idProduct == 0xa4a3)
154 return 1;
155
156 /* some GPL'd test firmware uses these IDs */
157
158 if (dev->idVendor == 0xfff0 && dev->idProduct == 0xfff0)
159 return 1;
160
161 /*----------------------------------------------------*/
162
163 /* iBOT2 high speed webcam */
164 if (dev->idVendor == 0x0b62 && dev->idProduct == 0x0059)
165 return 1;
166
167 return 0;
168}
169
170static int find_testdev (const char *name, const struct stat *sb, int flag)
171{
172 int fd;
173 struct usb_device_descriptor dev;
174
175 if (flag != FTW_F)
176 return 0;
177 /* ignore /proc/bus/usb/{devices,drivers} */
178 if (strrchr (name, '/')[1] == 'd')
179 return 0;
180
181 if ((fd = open (name, O_RDONLY)) < 0) {
182 perror ("can't open dev file r/o");
183 return 0;
184 }
185 if (read (fd, &dev, sizeof dev) != sizeof dev)
186 fputs ("short devfile read!\n", stderr);
187 else if (is_testdev (&dev)) {
188 struct testdev *entry;
189
190 if ((entry = calloc (1, sizeof *entry)) == 0) {
191 fputs ("no mem!\n", stderr);
192 goto done;
193 }
194 entry->name = strdup (name);
195 if (!entry->name) {
196 free (entry);
197 goto done;
198 }
199
200 // FIXME better to look at each interface and ask if it's
201 // bound to 'usbtest', rather than assume interface 0
202 entry->ifnum = 0;
203
204 // FIXME ask usbfs what speed; update USBDEVFS_CONNECTINFO
205 // so it tells about high speed etc
206
207 fprintf (stderr, "%s speed\t%s\n",
208 speed (entry->speed), entry->name);
209
210 entry->next = testdevs;
211 testdevs = entry;
212 }
213
214done:
215 close (fd);
216 return 0;
217}
218
219static int
220usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
221{
222 struct usbdevfs_ioctl wrapper;
223
224 wrapper.ifno = ifno;
225 wrapper.ioctl_code = request;
226 wrapper.data = param;
227
228 return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
229}
230
231static void *handle_testdev (void *arg)
232{
233 struct testdev *dev = arg;
234 int fd, i;
235 int status;
236
237 if ((fd = open (dev->name, O_RDWR)) < 0) {
238 perror ("can't open dev file r/w");
239 return 0;
240 }
241
242restart:
243 for (i = 0; i < TEST_CASES; i++) {
244 if (dev->test != -1 && dev->test != i)
245 continue;
246 dev->param.test_num = i;
247
248 status = usbdev_ioctl (fd, dev->ifnum,
249 USBTEST_REQUEST, &dev->param);
250 if (status < 0 && errno == EOPNOTSUPP)
251 continue;
252
253 /* FIXME need a "syslog it" option for background testing */
254
255 /* NOTE: each thread emits complete lines; no fragments! */
256 if (status < 0) {
257 char buf [80];
258 int err = errno;
259
260 if (strerror_r (errno, buf, sizeof buf)) {
261 snprintf (buf, sizeof buf, "error %d", err);
262 errno = err;
263 }
264 printf ("%s test %d --> %d (%s)\n",
265 dev->name, i, errno, buf);
266 } else
267 printf ("%s test %d, %4d.%.06d secs\n", dev->name, i,
268 (int) dev->param.duration.tv_sec,
269 (int) dev->param.duration.tv_usec);
270
271 fflush (stdout);
272 }
273 if (dev->forever)
274 goto restart;
275
276 close (fd);
277 return arg;
278}
279
280int main (int argc, char **argv)
281{
282 int c;
283 struct testdev *entry;
284 char *device;
285 int all = 0, forever = 0, not = 0;
286 int test = -1 /* all */;
287 struct usbtest_param param;
288
289 /* pick defaults that works with all speeds, without short packets.
290 *
291 * Best per-frame data rates:
292 * high speed, bulk 512 * 13 * 8 = 53248
293 * interrupt 1024 * 3 * 8 = 24576
294 * full speed, bulk/intr 64 * 19 = 1216
295 * interrupt 64 * 1 = 64
296 * low speed, interrupt 8 * 1 = 8
297 */
298 param.iterations = 1000;
299 param.length = 512;
300 param.vary = 512;
301 param.sglen = 32;
302
303 /* for easy use when hotplugging */
304 device = getenv ("DEVICE");
305
306 while ((c = getopt (argc, argv, "D:ac:g:hns:t:v:")) != EOF)
307 switch (c) {
308 case 'D': /* device, if only one */
309 device = optarg;
310 continue;
311 case 'a': /* use all devices */
312 device = 0;
313 all = 1;
314 continue;
315 case 'c': /* count iterations */
316 param.iterations = atoi (optarg);
317 if (param.iterations < 0)
318 goto usage;
319 continue;
320 case 'g': /* scatter/gather entries */
321 param.sglen = atoi (optarg);
322 if (param.sglen < 0)
323 goto usage;
324 continue;
325 case 'l': /* loop forever */
326 forever = 1;
327 continue;
328 case 'n': /* no test running! */
329 not = 1;
330 continue;
331 case 's': /* size of packet */
332 param.length = atoi (optarg);
333 if (param.length < 0)
334 goto usage;
335 continue;
336 case 't': /* run just one test */
337 test = atoi (optarg);
338 if (test < 0)
339 goto usage;
340 continue;
341 case 'v': /* vary packet size by ... */
342 param.vary = atoi (optarg);
343 if (param.vary < 0)
344 goto usage;
345 continue;
346 case '?':
347 case 'h':
348 default:
349usage:
350 fprintf (stderr, "usage: %s [-an] [-D dev]\n"
351 "\t[-c iterations] [-t testnum]\n"
352 "\t[-s packetsize] [-g sglen] [-v vary]\n",
353 argv [0]);
354 return 1;
355 }
356 if (optind != argc)
357 goto usage;
358 if (!all && !device) {
359 fprintf (stderr, "must specify '-a' or '-D dev', "
360 "or DEVICE=/proc/bus/usb/BBB/DDD in env\n");
361 goto usage;
362 }
363
364 if ((c = open ("/proc/bus/usb/devices", O_RDONLY)) < 0) {
365 fputs ("usbfs files are missing\n", stderr);
366 return -1;
367 }
368
369 /* collect and list the test devices */
370 if (ftw ("/proc/bus/usb", find_testdev, 3) != 0) {
371 fputs ("ftw failed; is usbfs missing?\n", stderr);
372 return -1;
373 }
374
375 /* quit, run single test, or create test threads */
376 if (!testdevs && !device) {
377 fputs ("no test devices recognized\n", stderr);
378 return -1;
379 }
380 if (not)
381 return 0;
382 if (testdevs && testdevs->next == 0 && !device)
383 device = testdevs->name;
384 for (entry = testdevs; entry; entry = entry->next) {
385 int status;
386
387 entry->param = param;
388 entry->forever = forever;
389 entry->test = test;
390
391 if (device) {
392 if (strcmp (entry->name, device))
393 continue;
394 return handle_testdev (entry) != entry;
395 }
396 status = pthread_create (&entry->thread, 0, handle_testdev, entry);
397 if (status) {
398 perror ("pthread_create");
399 continue;
400 }
401 }
402 if (device) {
403 struct testdev dev;
404
405 /* kernel can recognize test devices we don't */
406 fprintf (stderr, "%s: %s may see only control tests\n",
407 argv [0], device);
408
409 memset (&dev, 0, sizeof dev);
410 dev.name = device;
411 dev.param = param;
412 dev.forever = forever;
413 dev.test = test;
414 return handle_testdev (&dev) != &dev;
415 }
416
417 /* wait for tests to complete */
418 for (entry = testdevs; entry; entry = entry->next) {
419 void *retval;
420
421 if (pthread_join (entry->thread, &retval))
422 perror ("pthread_join");
423 /* testing errors discarded! */
424 }
425
426 return 0;
427}