testusb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 <m.nazarewicz@samsung.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. /* ignore /proc/bus/usb/{devices,drivers} */
  206. if (strrchr(name, '/')[1] == 'd')
  207. return 0;
  208. fd = fopen(name, "rb");
  209. if (!fd) {
  210. perror(name);
  211. return 0;
  212. }
  213. ifnum = testdev_ifnum(fd);
  214. fclose(fd);
  215. if (ifnum < 0)
  216. return 0;
  217. entry = calloc(1, sizeof *entry);
  218. if (!entry)
  219. goto nomem;
  220. entry->name = strdup(name);
  221. if (!entry->name) {
  222. free(entry);
  223. nomem:
  224. perror("malloc");
  225. return 0;
  226. }
  227. entry->ifnum = ifnum;
  228. /* FIXME ask usbfs what speed; update USBDEVFS_CONNECTINFO so
  229. * it tells about high speed etc */
  230. fprintf(stderr, "%s speed\t%s\t%u\n",
  231. speed(entry->speed), entry->name, entry->ifnum);
  232. entry->next = testdevs;
  233. testdevs = entry;
  234. return 0;
  235. }
  236. static int
  237. usbdev_ioctl (int fd, int ifno, unsigned request, void *param)
  238. {
  239. struct usbdevfs_ioctl wrapper;
  240. wrapper.ifno = ifno;
  241. wrapper.ioctl_code = request;
  242. wrapper.data = param;
  243. return ioctl (fd, USBDEVFS_IOCTL, &wrapper);
  244. }
  245. static void *handle_testdev (void *arg)
  246. {
  247. struct testdev *dev = arg;
  248. int fd, i;
  249. int status;
  250. if ((fd = open (dev->name, O_RDWR)) < 0) {
  251. perror ("can't open dev file r/w");
  252. return 0;
  253. }
  254. restart:
  255. for (i = 0; i < TEST_CASES; i++) {
  256. if (dev->test != -1 && dev->test != i)
  257. continue;
  258. dev->param.test_num = i;
  259. status = usbdev_ioctl (fd, dev->ifnum,
  260. USBTEST_REQUEST, &dev->param);
  261. if (status < 0 && errno == EOPNOTSUPP)
  262. continue;
  263. /* FIXME need a "syslog it" option for background testing */
  264. /* NOTE: each thread emits complete lines; no fragments! */
  265. if (status < 0) {
  266. char buf [80];
  267. int err = errno;
  268. if (strerror_r (errno, buf, sizeof buf)) {
  269. snprintf (buf, sizeof buf, "error %d", err);
  270. errno = err;
  271. }
  272. printf ("%s test %d --> %d (%s)\n",
  273. dev->name, i, errno, buf);
  274. } else
  275. printf ("%s test %d, %4d.%.06d secs\n", dev->name, i,
  276. (int) dev->param.duration.tv_sec,
  277. (int) dev->param.duration.tv_usec);
  278. fflush (stdout);
  279. }
  280. if (dev->forever)
  281. goto restart;
  282. close (fd);
  283. return arg;
  284. }
  285. static const char *usbfs_dir_find(void)
  286. {
  287. static char usbfs_path_0[] = "/dev/usb/devices";
  288. static char usbfs_path_1[] = "/proc/bus/usb/devices";
  289. static char *const usbfs_paths[] = {
  290. usbfs_path_0, usbfs_path_1
  291. };
  292. static char *const *
  293. end = usbfs_paths + sizeof usbfs_paths / sizeof *usbfs_paths;
  294. char *const *it = usbfs_paths;
  295. do {
  296. int fd = open(*it, O_RDONLY);
  297. close(fd);
  298. if (fd >= 0) {
  299. strrchr(*it, '/')[0] = '\0';
  300. return *it;
  301. }
  302. } while (++it != end);
  303. return NULL;
  304. }
  305. static int parse_num(unsigned *num, const char *str)
  306. {
  307. unsigned long val;
  308. char *end;
  309. errno = 0;
  310. val = strtoul(str, &end, 0);
  311. if (errno || *end || val > UINT_MAX)
  312. return -1;
  313. *num = val;
  314. return 0;
  315. }
  316. int main (int argc, char **argv)
  317. {
  318. int c;
  319. struct testdev *entry;
  320. char *device;
  321. const char *usbfs_dir = NULL;
  322. int all = 0, forever = 0, not = 0;
  323. int test = -1 /* all */;
  324. struct usbtest_param param;
  325. /* pick defaults that works with all speeds, without short packets.
  326. *
  327. * Best per-frame data rates:
  328. * high speed, bulk 512 * 13 * 8 = 53248
  329. * interrupt 1024 * 3 * 8 = 24576
  330. * full speed, bulk/intr 64 * 19 = 1216
  331. * interrupt 64 * 1 = 64
  332. * low speed, interrupt 8 * 1 = 8
  333. */
  334. param.iterations = 1000;
  335. param.length = 512;
  336. param.vary = 512;
  337. param.sglen = 32;
  338. /* for easy use when hotplugging */
  339. device = getenv ("DEVICE");
  340. while ((c = getopt (argc, argv, "D:aA:c:g:hns:t:v:")) != EOF)
  341. switch (c) {
  342. case 'D': /* device, if only one */
  343. device = optarg;
  344. continue;
  345. case 'A': /* use all devices with specified usbfs dir */
  346. usbfs_dir = optarg;
  347. /* FALL THROUGH */
  348. case 'a': /* use all devices */
  349. device = NULL;
  350. all = 1;
  351. continue;
  352. case 'c': /* count iterations */
  353. if (parse_num(&param.iterations, optarg))
  354. goto usage;
  355. continue;
  356. case 'g': /* scatter/gather entries */
  357. if (parse_num(&param.sglen, optarg))
  358. goto usage;
  359. continue;
  360. case 'l': /* loop forever */
  361. forever = 1;
  362. continue;
  363. case 'n': /* no test running! */
  364. not = 1;
  365. continue;
  366. case 's': /* size of packet */
  367. if (parse_num(&param.length, optarg))
  368. goto usage;
  369. continue;
  370. case 't': /* run just one test */
  371. test = atoi (optarg);
  372. if (test < 0)
  373. goto usage;
  374. continue;
  375. case 'v': /* vary packet size by ... */
  376. if (parse_num(&param.vary, optarg))
  377. goto usage;
  378. continue;
  379. case '?':
  380. case 'h':
  381. default:
  382. usage:
  383. fprintf (stderr, "usage: %s [-n] [-D dev | -a | -A usbfs-dir]\n"
  384. "\t[-c iterations] [-t testnum]\n"
  385. "\t[-s packetsize] [-g sglen] [-v vary]\n",
  386. argv [0]);
  387. return 1;
  388. }
  389. if (optind != argc)
  390. goto usage;
  391. if (!all && !device) {
  392. fprintf (stderr, "must specify '-a' or '-D dev', "
  393. "or DEVICE=/proc/bus/usb/BBB/DDD in env\n");
  394. goto usage;
  395. }
  396. /* Find usbfs mount point */
  397. if (!usbfs_dir) {
  398. usbfs_dir = usbfs_dir_find();
  399. if (!usbfs_dir) {
  400. fputs ("usbfs files are missing\n", stderr);
  401. return -1;
  402. }
  403. }
  404. /* collect and list the test devices */
  405. if (ftw (usbfs_dir, find_testdev, 3) != 0) {
  406. fputs ("ftw failed; is usbfs missing?\n", stderr);
  407. return -1;
  408. }
  409. /* quit, run single test, or create test threads */
  410. if (!testdevs && !device) {
  411. fputs ("no test devices recognized\n", stderr);
  412. return -1;
  413. }
  414. if (not)
  415. return 0;
  416. if (testdevs && testdevs->next == 0 && !device)
  417. device = testdevs->name;
  418. for (entry = testdevs; entry; entry = entry->next) {
  419. int status;
  420. entry->param = param;
  421. entry->forever = forever;
  422. entry->test = test;
  423. if (device) {
  424. if (strcmp (entry->name, device))
  425. continue;
  426. return handle_testdev (entry) != entry;
  427. }
  428. status = pthread_create (&entry->thread, 0, handle_testdev, entry);
  429. if (status) {
  430. perror ("pthread_create");
  431. continue;
  432. }
  433. }
  434. if (device) {
  435. struct testdev dev;
  436. /* kernel can recognize test devices we don't */
  437. fprintf (stderr, "%s: %s may see only control tests\n",
  438. argv [0], device);
  439. memset (&dev, 0, sizeof dev);
  440. dev.name = device;
  441. dev.param = param;
  442. dev.forever = forever;
  443. dev.test = test;
  444. return handle_testdev (&dev) != &dev;
  445. }
  446. /* wait for tests to complete */
  447. for (entry = testdevs; entry; entry = entry->next) {
  448. void *retval;
  449. if (pthread_join (entry->thread, &retval))
  450. perror ("pthread_join");
  451. /* testing errors discarded! */
  452. }
  453. return 0;
  454. }