ffs-test.c 17 KB

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