testusb.c 13 KB

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