ffs-test.c 16 KB

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