ffs-test.c 15 KB

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