ffs-test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * ffs-test.c -- user mode filesystem api for usb composite function
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. * Author: Michal Nazarewicz <mina86@mina86.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License 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
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* $(CROSS_COMPILE)cc -Wall -Wextra -g -o ffs-test ffs-test.c -lpthread */
  22. #define _DEFAULT_SOURCE /* for endian.h */
  23. #include <endian.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <pthread.h>
  27. #include <stdarg.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. #include <tools/le_byteshift.h>
  37. #include "../../include/uapi/linux/usb/functionfs.h"
  38. /******************** Little Endian Handling ********************************/
  39. #define cpu_to_le16(x) htole16(x)
  40. #define cpu_to_le32(x) htole32(x)
  41. #define le32_to_cpu(x) le32toh(x)
  42. #define le16_to_cpu(x) le16toh(x)
  43. /******************** Messages and Errors ***********************************/
  44. static const char argv0[] = "ffs-test";
  45. static unsigned verbosity = 7;
  46. static void _msg(unsigned level, const char *fmt, ...)
  47. {
  48. if (level < 2)
  49. level = 2;
  50. else if (level > 7)
  51. level = 7;
  52. if (level <= verbosity) {
  53. static const char levels[8][6] = {
  54. [2] = "crit:",
  55. [3] = "err: ",
  56. [4] = "warn:",
  57. [5] = "note:",
  58. [6] = "info:",
  59. [7] = "dbg: "
  60. };
  61. int _errno = errno;
  62. va_list ap;
  63. fprintf(stderr, "%s: %s ", argv0, levels[level]);
  64. va_start(ap, fmt);
  65. vfprintf(stderr, fmt, ap);
  66. va_end(ap);
  67. if (fmt[strlen(fmt) - 1] != '\n') {
  68. char buffer[128];
  69. strerror_r(_errno, buffer, sizeof buffer);
  70. fprintf(stderr, ": (-%d) %s\n", _errno, buffer);
  71. }
  72. fflush(stderr);
  73. }
  74. }
  75. #define die(...) (_msg(2, __VA_ARGS__), exit(1))
  76. #define err(...) _msg(3, __VA_ARGS__)
  77. #define warn(...) _msg(4, __VA_ARGS__)
  78. #define note(...) _msg(5, __VA_ARGS__)
  79. #define info(...) _msg(6, __VA_ARGS__)
  80. #define debug(...) _msg(7, __VA_ARGS__)
  81. #define die_on(cond, ...) do { \
  82. if (cond) \
  83. die(__VA_ARGS__); \
  84. } while (0)
  85. /******************** Descriptors and Strings *******************************/
  86. static const struct {
  87. struct usb_functionfs_descs_head_v2 header;
  88. __le32 fs_count;
  89. __le32 hs_count;
  90. __le32 ss_count;
  91. struct {
  92. struct usb_interface_descriptor intf;
  93. struct usb_endpoint_descriptor_no_audio sink;
  94. struct usb_endpoint_descriptor_no_audio source;
  95. } __attribute__((packed)) fs_descs, hs_descs;
  96. struct {
  97. struct usb_interface_descriptor intf;
  98. struct usb_endpoint_descriptor_no_audio sink;
  99. struct usb_ss_ep_comp_descriptor sink_comp;
  100. struct usb_endpoint_descriptor_no_audio source;
  101. struct usb_ss_ep_comp_descriptor source_comp;
  102. } ss_descs;
  103. } __attribute__((packed)) descriptors = {
  104. .header = {
  105. .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
  106. .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
  107. FUNCTIONFS_HAS_HS_DESC |
  108. FUNCTIONFS_HAS_SS_DESC),
  109. .length = cpu_to_le32(sizeof descriptors),
  110. },
  111. .fs_count = cpu_to_le32(3),
  112. .fs_descs = {
  113. .intf = {
  114. .bLength = sizeof descriptors.fs_descs.intf,
  115. .bDescriptorType = USB_DT_INTERFACE,
  116. .bNumEndpoints = 2,
  117. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  118. .iInterface = 1,
  119. },
  120. .sink = {
  121. .bLength = sizeof descriptors.fs_descs.sink,
  122. .bDescriptorType = USB_DT_ENDPOINT,
  123. .bEndpointAddress = 1 | USB_DIR_IN,
  124. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  125. /* .wMaxPacketSize = autoconfiguration (kernel) */
  126. },
  127. .source = {
  128. .bLength = sizeof descriptors.fs_descs.source,
  129. .bDescriptorType = USB_DT_ENDPOINT,
  130. .bEndpointAddress = 2 | USB_DIR_OUT,
  131. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  132. /* .wMaxPacketSize = autoconfiguration (kernel) */
  133. },
  134. },
  135. .hs_count = cpu_to_le32(3),
  136. .hs_descs = {
  137. .intf = {
  138. .bLength = sizeof descriptors.fs_descs.intf,
  139. .bDescriptorType = USB_DT_INTERFACE,
  140. .bNumEndpoints = 2,
  141. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  142. .iInterface = 1,
  143. },
  144. .sink = {
  145. .bLength = sizeof descriptors.hs_descs.sink,
  146. .bDescriptorType = USB_DT_ENDPOINT,
  147. .bEndpointAddress = 1 | USB_DIR_IN,
  148. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  149. .wMaxPacketSize = cpu_to_le16(512),
  150. },
  151. .source = {
  152. .bLength = sizeof descriptors.hs_descs.source,
  153. .bDescriptorType = USB_DT_ENDPOINT,
  154. .bEndpointAddress = 2 | USB_DIR_OUT,
  155. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  156. .wMaxPacketSize = cpu_to_le16(512),
  157. .bInterval = 1, /* NAK every 1 uframe */
  158. },
  159. },
  160. .ss_count = cpu_to_le32(5),
  161. .ss_descs = {
  162. .intf = {
  163. .bLength = sizeof descriptors.fs_descs.intf,
  164. .bDescriptorType = USB_DT_INTERFACE,
  165. .bNumEndpoints = 2,
  166. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  167. .iInterface = 1,
  168. },
  169. .sink = {
  170. .bLength = sizeof descriptors.hs_descs.sink,
  171. .bDescriptorType = USB_DT_ENDPOINT,
  172. .bEndpointAddress = 1 | USB_DIR_IN,
  173. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  174. .wMaxPacketSize = cpu_to_le16(1024),
  175. },
  176. .sink_comp = {
  177. .bLength = USB_DT_SS_EP_COMP_SIZE,
  178. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  179. .bMaxBurst = 0,
  180. .bmAttributes = 0,
  181. .wBytesPerInterval = 0,
  182. },
  183. .source = {
  184. .bLength = sizeof descriptors.hs_descs.source,
  185. .bDescriptorType = USB_DT_ENDPOINT,
  186. .bEndpointAddress = 2 | USB_DIR_OUT,
  187. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  188. .wMaxPacketSize = cpu_to_le16(1024),
  189. .bInterval = 1, /* NAK every 1 uframe */
  190. },
  191. .source_comp = {
  192. .bLength = USB_DT_SS_EP_COMP_SIZE,
  193. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  194. .bMaxBurst = 0,
  195. .bmAttributes = 0,
  196. .wBytesPerInterval = 0,
  197. },
  198. },
  199. };
  200. static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
  201. {
  202. const unsigned char *descs_end, *descs_start;
  203. __u32 length, fs_count = 0, hs_count = 0, count;
  204. /* Read v2 header */
  205. {
  206. const struct {
  207. const struct usb_functionfs_descs_head_v2 header;
  208. const __le32 counts[];
  209. } __attribute__((packed)) *const in = descriptors_v2;
  210. const __le32 *counts = in->counts;
  211. __u32 flags;
  212. if (le32_to_cpu(in->header.magic) !=
  213. FUNCTIONFS_DESCRIPTORS_MAGIC_V2)
  214. return 0;
  215. length = le32_to_cpu(in->header.length);
  216. if (length <= sizeof in->header)
  217. return 0;
  218. length -= sizeof in->header;
  219. flags = le32_to_cpu(in->header.flags);
  220. if (flags & ~(FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
  221. FUNCTIONFS_HAS_SS_DESC))
  222. return 0;
  223. #define GET_NEXT_COUNT_IF_FLAG(ret, flg) do { \
  224. if (!(flags & (flg))) \
  225. break; \
  226. if (length < 4) \
  227. return 0; \
  228. ret = le32_to_cpu(*counts); \
  229. length -= 4; \
  230. ++counts; \
  231. } while (0)
  232. GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
  233. GET_NEXT_COUNT_IF_FLAG(hs_count, FUNCTIONFS_HAS_HS_DESC);
  234. GET_NEXT_COUNT_IF_FLAG(count, FUNCTIONFS_HAS_SS_DESC);
  235. count = fs_count + hs_count;
  236. if (!count)
  237. return 0;
  238. descs_start = (const void *)counts;
  239. #undef GET_NEXT_COUNT_IF_FLAG
  240. }
  241. /*
  242. * Find the end of FS and HS USB descriptors. SS descriptors
  243. * are ignored since legacy format does not support them.
  244. */
  245. descs_end = descs_start;
  246. do {
  247. if (length < *descs_end)
  248. return 0;
  249. length -= *descs_end;
  250. descs_end += *descs_end;
  251. } while (--count);
  252. /* Allocate legacy descriptors and copy the data. */
  253. {
  254. #pragma GCC diagnostic push
  255. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  256. struct {
  257. struct usb_functionfs_descs_head header;
  258. __u8 descriptors[];
  259. } __attribute__((packed)) *out;
  260. #pragma GCC diagnostic pop
  261. length = sizeof out->header + (descs_end - descs_start);
  262. out = malloc(length);
  263. out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
  264. out->header.length = cpu_to_le32(length);
  265. out->header.fs_count = cpu_to_le32(fs_count);
  266. out->header.hs_count = cpu_to_le32(hs_count);
  267. memcpy(out->descriptors, descs_start, descs_end - descs_start);
  268. *legacy = out;
  269. }
  270. return length;
  271. }
  272. #define STR_INTERFACE_ "Source/Sink"
  273. static const struct {
  274. struct usb_functionfs_strings_head header;
  275. struct {
  276. __le16 code;
  277. const char str1[sizeof STR_INTERFACE_];
  278. } __attribute__((packed)) lang0;
  279. } __attribute__((packed)) strings = {
  280. .header = {
  281. .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
  282. .length = cpu_to_le32(sizeof strings),
  283. .str_count = cpu_to_le32(1),
  284. .lang_count = cpu_to_le32(1),
  285. },
  286. .lang0 = {
  287. cpu_to_le16(0x0409), /* en-us */
  288. STR_INTERFACE_,
  289. },
  290. };
  291. #define STR_INTERFACE strings.lang0.str1
  292. /******************** Files and Threads Handling ****************************/
  293. struct thread;
  294. static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
  295. static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
  296. static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
  297. static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
  298. static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
  299. static struct thread {
  300. const char *const filename;
  301. size_t buf_size;
  302. ssize_t (*in)(struct thread *, void *, size_t);
  303. const char *const in_name;
  304. ssize_t (*out)(struct thread *, const void *, size_t);
  305. const char *const out_name;
  306. int fd;
  307. pthread_t id;
  308. void *buf;
  309. ssize_t status;
  310. } threads[] = {
  311. {
  312. "ep0", 4 * sizeof(struct usb_functionfs_event),
  313. read_wrap, NULL,
  314. ep0_consume, "<consume>",
  315. 0, 0, NULL, 0
  316. },
  317. {
  318. "ep1", 8 * 1024,
  319. fill_in_buf, "<in>",
  320. write_wrap, NULL,
  321. 0, 0, NULL, 0
  322. },
  323. {
  324. "ep2", 8 * 1024,
  325. read_wrap, NULL,
  326. empty_out_buf, "<out>",
  327. 0, 0, NULL, 0
  328. },
  329. };
  330. static void init_thread(struct thread *t)
  331. {
  332. t->buf = malloc(t->buf_size);
  333. die_on(!t->buf, "malloc");
  334. t->fd = open(t->filename, O_RDWR);
  335. die_on(t->fd < 0, "%s", t->filename);
  336. }
  337. static void cleanup_thread(void *arg)
  338. {
  339. struct thread *t = arg;
  340. int ret, fd;
  341. fd = t->fd;
  342. if (t->fd < 0)
  343. return;
  344. t->fd = -1;
  345. /* test the FIFO ioctls (non-ep0 code paths) */
  346. if (t != threads) {
  347. ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
  348. if (ret < 0) {
  349. /* ENODEV reported after disconnect */
  350. if (errno != ENODEV)
  351. err("%s: get fifo status", t->filename);
  352. } else if (ret) {
  353. warn("%s: unclaimed = %d\n", t->filename, ret);
  354. if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
  355. err("%s: fifo flush", t->filename);
  356. }
  357. }
  358. if (close(fd) < 0)
  359. err("%s: close", t->filename);
  360. free(t->buf);
  361. t->buf = NULL;
  362. }
  363. static void *start_thread_helper(void *arg)
  364. {
  365. const char *name, *op, *in_name, *out_name;
  366. struct thread *t = arg;
  367. ssize_t ret;
  368. info("%s: starts\n", t->filename);
  369. in_name = t->in_name ? t->in_name : t->filename;
  370. out_name = t->out_name ? t->out_name : t->filename;
  371. pthread_cleanup_push(cleanup_thread, arg);
  372. for (;;) {
  373. pthread_testcancel();
  374. ret = t->in(t, t->buf, t->buf_size);
  375. if (ret > 0) {
  376. ret = t->out(t, t->buf, ret);
  377. name = out_name;
  378. op = "write";
  379. } else {
  380. name = in_name;
  381. op = "read";
  382. }
  383. if (ret > 0) {
  384. /* nop */
  385. } else if (!ret) {
  386. debug("%s: %s: EOF", name, op);
  387. break;
  388. } else if (errno == EINTR || errno == EAGAIN) {
  389. debug("%s: %s", name, op);
  390. } else {
  391. warn("%s: %s", name, op);
  392. break;
  393. }
  394. }
  395. pthread_cleanup_pop(1);
  396. t->status = ret;
  397. info("%s: ends\n", t->filename);
  398. return NULL;
  399. }
  400. static void start_thread(struct thread *t)
  401. {
  402. debug("%s: starting\n", t->filename);
  403. die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
  404. "pthread_create(%s)", t->filename);
  405. }
  406. static void join_thread(struct thread *t)
  407. {
  408. int ret = pthread_join(t->id, NULL);
  409. if (ret < 0)
  410. err("%s: joining thread", t->filename);
  411. else
  412. debug("%s: joined\n", t->filename);
  413. }
  414. static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
  415. {
  416. return read(t->fd, buf, nbytes);
  417. }
  418. static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
  419. {
  420. return write(t->fd, buf, nbytes);
  421. }
  422. /******************** Empty/Fill buffer routines ****************************/
  423. /* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
  424. enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
  425. static enum pattern pattern;
  426. static ssize_t
  427. fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
  428. {
  429. size_t i;
  430. __u8 *p;
  431. (void)ignore;
  432. switch (pattern) {
  433. case PAT_ZERO:
  434. memset(buf, 0, nbytes);
  435. break;
  436. case PAT_SEQ:
  437. for (p = buf, i = 0; i < nbytes; ++i, ++p)
  438. *p = i % 63;
  439. break;
  440. case PAT_PIPE:
  441. return fread(buf, 1, nbytes, stdin);
  442. }
  443. return nbytes;
  444. }
  445. static ssize_t
  446. empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
  447. {
  448. const __u8 *p;
  449. __u8 expected;
  450. ssize_t ret;
  451. size_t len;
  452. (void)ignore;
  453. switch (pattern) {
  454. case PAT_ZERO:
  455. expected = 0;
  456. for (p = buf, len = 0; len < nbytes; ++p, ++len)
  457. if (*p)
  458. goto invalid;
  459. break;
  460. case PAT_SEQ:
  461. for (p = buf, len = 0; len < nbytes; ++p, ++len)
  462. if (*p != len % 63) {
  463. expected = len % 63;
  464. goto invalid;
  465. }
  466. break;
  467. case PAT_PIPE:
  468. ret = fwrite(buf, nbytes, 1, stdout);
  469. if (ret > 0)
  470. fflush(stdout);
  471. break;
  472. invalid:
  473. err("bad OUT byte %zd, expected %02x got %02x\n",
  474. len, expected, *p);
  475. for (p = buf, len = 0; len < nbytes; ++p, ++len) {
  476. if (0 == (len % 32))
  477. fprintf(stderr, "%4zd:", len);
  478. fprintf(stderr, " %02x", *p);
  479. if (31 == (len % 32))
  480. fprintf(stderr, "\n");
  481. }
  482. fflush(stderr);
  483. errno = EILSEQ;
  484. return -1;
  485. }
  486. return len;
  487. }
  488. /******************** Endpoints routines ************************************/
  489. static void handle_setup(const struct usb_ctrlrequest *setup)
  490. {
  491. printf("bRequestType = %d\n", setup->bRequestType);
  492. printf("bRequest = %d\n", setup->bRequest);
  493. printf("wValue = %d\n", le16_to_cpu(setup->wValue));
  494. printf("wIndex = %d\n", le16_to_cpu(setup->wIndex));
  495. printf("wLength = %d\n", le16_to_cpu(setup->wLength));
  496. }
  497. static ssize_t
  498. ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
  499. {
  500. static const char *const names[] = {
  501. [FUNCTIONFS_BIND] = "BIND",
  502. [FUNCTIONFS_UNBIND] = "UNBIND",
  503. [FUNCTIONFS_ENABLE] = "ENABLE",
  504. [FUNCTIONFS_DISABLE] = "DISABLE",
  505. [FUNCTIONFS_SETUP] = "SETUP",
  506. [FUNCTIONFS_SUSPEND] = "SUSPEND",
  507. [FUNCTIONFS_RESUME] = "RESUME",
  508. };
  509. const struct usb_functionfs_event *event = buf;
  510. size_t n;
  511. (void)ignore;
  512. for (n = nbytes / sizeof *event; n; --n, ++event)
  513. switch (event->type) {
  514. case FUNCTIONFS_BIND:
  515. case FUNCTIONFS_UNBIND:
  516. case FUNCTIONFS_ENABLE:
  517. case FUNCTIONFS_DISABLE:
  518. case FUNCTIONFS_SETUP:
  519. case FUNCTIONFS_SUSPEND:
  520. case FUNCTIONFS_RESUME:
  521. printf("Event %s\n", names[event->type]);
  522. if (event->type == FUNCTIONFS_SETUP)
  523. handle_setup(&event->u.setup);
  524. break;
  525. default:
  526. printf("Event %03u (unknown)\n", event->type);
  527. }
  528. return nbytes;
  529. }
  530. static void ep0_init(struct thread *t, bool legacy_descriptors)
  531. {
  532. void *legacy;
  533. ssize_t ret;
  534. size_t len;
  535. if (legacy_descriptors) {
  536. info("%s: writing descriptors\n", t->filename);
  537. goto legacy;
  538. }
  539. info("%s: writing descriptors (in v2 format)\n", t->filename);
  540. ret = write(t->fd, &descriptors, sizeof descriptors);
  541. if (ret < 0 && errno == EINVAL) {
  542. warn("%s: new format rejected, trying legacy\n", t->filename);
  543. legacy:
  544. len = descs_to_legacy(&legacy, &descriptors);
  545. if (len) {
  546. ret = write(t->fd, legacy, len);
  547. free(legacy);
  548. }
  549. }
  550. die_on(ret < 0, "%s: write: descriptors", t->filename);
  551. info("%s: writing strings\n", t->filename);
  552. ret = write(t->fd, &strings, sizeof strings);
  553. die_on(ret < 0, "%s: write: strings", t->filename);
  554. }
  555. /******************** Main **************************************************/
  556. int main(int argc, char **argv)
  557. {
  558. bool legacy_descriptors;
  559. unsigned i;
  560. legacy_descriptors = argc > 2 && !strcmp(argv[1], "-l");
  561. init_thread(threads);
  562. ep0_init(threads, legacy_descriptors);
  563. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  564. init_thread(threads + i);
  565. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  566. start_thread(threads + i);
  567. start_thread_helper(threads);
  568. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  569. join_thread(threads + i);
  570. return 0;
  571. }