testusb.c 12 KB

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