testusb.c 12 KB

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