ffs-test.c 15 KB

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