testusb.c 10 KB

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