ffs-test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * ffs-test.c.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 <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #include <tools/le_byteshift.h>
  36. #include "../../include/linux/usb/functionfs.h"
  37. /******************** Little Endian Handling ********************************/
  38. #define cpu_to_le16(x) htole16(x)
  39. #define cpu_to_le32(x) htole32(x)
  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 header;
  87. struct {
  88. struct usb_interface_descriptor intf;
  89. struct usb_endpoint_descriptor_no_audio sink;
  90. struct usb_endpoint_descriptor_no_audio source;
  91. } __attribute__((packed)) fs_descs, hs_descs;
  92. } __attribute__((packed)) descriptors = {
  93. .header = {
  94. .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
  95. .length = cpu_to_le32(sizeof descriptors),
  96. .fs_count = cpu_to_le32(3),
  97. .hs_count = cpu_to_le32(3),
  98. },
  99. .fs_descs = {
  100. .intf = {
  101. .bLength = sizeof descriptors.fs_descs.intf,
  102. .bDescriptorType = USB_DT_INTERFACE,
  103. .bNumEndpoints = 2,
  104. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  105. .iInterface = 1,
  106. },
  107. .sink = {
  108. .bLength = sizeof descriptors.fs_descs.sink,
  109. .bDescriptorType = USB_DT_ENDPOINT,
  110. .bEndpointAddress = 1 | USB_DIR_IN,
  111. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  112. /* .wMaxPacketSize = autoconfiguration (kernel) */
  113. },
  114. .source = {
  115. .bLength = sizeof descriptors.fs_descs.source,
  116. .bDescriptorType = USB_DT_ENDPOINT,
  117. .bEndpointAddress = 2 | USB_DIR_OUT,
  118. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  119. /* .wMaxPacketSize = autoconfiguration (kernel) */
  120. },
  121. },
  122. .hs_descs = {
  123. .intf = {
  124. .bLength = sizeof descriptors.fs_descs.intf,
  125. .bDescriptorType = USB_DT_INTERFACE,
  126. .bNumEndpoints = 2,
  127. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  128. .iInterface = 1,
  129. },
  130. .sink = {
  131. .bLength = sizeof descriptors.hs_descs.sink,
  132. .bDescriptorType = USB_DT_ENDPOINT,
  133. .bEndpointAddress = 1 | USB_DIR_IN,
  134. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  135. .wMaxPacketSize = cpu_to_le16(512),
  136. },
  137. .source = {
  138. .bLength = sizeof descriptors.hs_descs.source,
  139. .bDescriptorType = USB_DT_ENDPOINT,
  140. .bEndpointAddress = 2 | USB_DIR_OUT,
  141. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  142. .wMaxPacketSize = cpu_to_le16(512),
  143. .bInterval = 1, /* NAK every 1 uframe */
  144. },
  145. },
  146. };
  147. #define STR_INTERFACE_ "Source/Sink"
  148. static const struct {
  149. struct usb_functionfs_strings_head header;
  150. struct {
  151. __le16 code;
  152. const char str1[sizeof STR_INTERFACE_];
  153. } __attribute__((packed)) lang0;
  154. } __attribute__((packed)) strings = {
  155. .header = {
  156. .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
  157. .length = cpu_to_le32(sizeof strings),
  158. .str_count = cpu_to_le32(1),
  159. .lang_count = cpu_to_le32(1),
  160. },
  161. .lang0 = {
  162. cpu_to_le16(0x0409), /* en-us */
  163. STR_INTERFACE_,
  164. },
  165. };
  166. #define STR_INTERFACE strings.lang0.str1
  167. /******************** Files and Threads Handling ****************************/
  168. struct thread;
  169. static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
  170. static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
  171. static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
  172. static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
  173. static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
  174. static struct thread {
  175. const char *const filename;
  176. size_t buf_size;
  177. ssize_t (*in)(struct thread *, void *, size_t);
  178. const char *const in_name;
  179. ssize_t (*out)(struct thread *, const void *, size_t);
  180. const char *const out_name;
  181. int fd;
  182. pthread_t id;
  183. void *buf;
  184. ssize_t status;
  185. } threads[] = {
  186. {
  187. "ep0", 4 * sizeof(struct usb_functionfs_event),
  188. read_wrap, NULL,
  189. ep0_consume, "<consume>",
  190. 0, 0, NULL, 0
  191. },
  192. {
  193. "ep1", 8 * 1024,
  194. fill_in_buf, "<in>",
  195. write_wrap, NULL,
  196. 0, 0, NULL, 0
  197. },
  198. {
  199. "ep2", 8 * 1024,
  200. read_wrap, NULL,
  201. empty_out_buf, "<out>",
  202. 0, 0, NULL, 0
  203. },
  204. };
  205. static void init_thread(struct thread *t)
  206. {
  207. t->buf = malloc(t->buf_size);
  208. die_on(!t->buf, "malloc");
  209. t->fd = open(t->filename, O_RDWR);
  210. die_on(t->fd < 0, "%s", t->filename);
  211. }
  212. static void cleanup_thread(void *arg)
  213. {
  214. struct thread *t = arg;
  215. int ret, fd;
  216. fd = t->fd;
  217. if (t->fd < 0)
  218. return;
  219. t->fd = -1;
  220. /* test the FIFO ioctls (non-ep0 code paths) */
  221. if (t != threads) {
  222. ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
  223. if (ret < 0) {
  224. /* ENODEV reported after disconnect */
  225. if (errno != ENODEV)
  226. err("%s: get fifo status", t->filename);
  227. } else if (ret) {
  228. warn("%s: unclaimed = %d\n", t->filename, ret);
  229. if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
  230. err("%s: fifo flush", t->filename);
  231. }
  232. }
  233. if (close(fd) < 0)
  234. err("%s: close", t->filename);
  235. free(t->buf);
  236. t->buf = NULL;
  237. }
  238. static void *start_thread_helper(void *arg)
  239. {
  240. const char *name, *op, *in_name, *out_name;
  241. struct thread *t = arg;
  242. ssize_t ret;
  243. info("%s: starts\n", t->filename);
  244. in_name = t->in_name ? t->in_name : t->filename;
  245. out_name = t->out_name ? t->out_name : t->filename;
  246. pthread_cleanup_push(cleanup_thread, arg);
  247. for (;;) {
  248. pthread_testcancel();
  249. ret = t->in(t, t->buf, t->buf_size);
  250. if (ret > 0) {
  251. ret = t->out(t, t->buf, ret);
  252. name = out_name;
  253. op = "write";
  254. } else {
  255. name = in_name;
  256. op = "read";
  257. }
  258. if (ret > 0) {
  259. /* nop */
  260. } else if (!ret) {
  261. debug("%s: %s: EOF", name, op);
  262. break;
  263. } else if (errno == EINTR || errno == EAGAIN) {
  264. debug("%s: %s", name, op);
  265. } else {
  266. warn("%s: %s", name, op);
  267. break;
  268. }
  269. }
  270. pthread_cleanup_pop(1);
  271. t->status = ret;
  272. info("%s: ends\n", t->filename);
  273. return NULL;
  274. }
  275. static void start_thread(struct thread *t)
  276. {
  277. debug("%s: starting\n", t->filename);
  278. die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
  279. "pthread_create(%s)", t->filename);
  280. }
  281. static void join_thread(struct thread *t)
  282. {
  283. int ret = pthread_join(t->id, NULL);
  284. if (ret < 0)
  285. err("%s: joining thread", t->filename);
  286. else
  287. debug("%s: joined\n", t->filename);
  288. }
  289. static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
  290. {
  291. return read(t->fd, buf, nbytes);
  292. }
  293. static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
  294. {
  295. return write(t->fd, buf, nbytes);
  296. }
  297. /******************** Empty/Fill buffer routines ****************************/
  298. /* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
  299. enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
  300. static enum pattern pattern;
  301. static ssize_t
  302. fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
  303. {
  304. size_t i;
  305. __u8 *p;
  306. (void)ignore;
  307. switch (pattern) {
  308. case PAT_ZERO:
  309. memset(buf, 0, nbytes);
  310. break;
  311. case PAT_SEQ:
  312. for (p = buf, i = 0; i < nbytes; ++i, ++p)
  313. *p = i % 63;
  314. break;
  315. case PAT_PIPE:
  316. return fread(buf, 1, nbytes, stdin);
  317. }
  318. return nbytes;
  319. }
  320. static ssize_t
  321. empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
  322. {
  323. const __u8 *p;
  324. __u8 expected;
  325. ssize_t ret;
  326. size_t len;
  327. (void)ignore;
  328. switch (pattern) {
  329. case PAT_ZERO:
  330. expected = 0;
  331. for (p = buf, len = 0; len < nbytes; ++p, ++len)
  332. if (*p)
  333. goto invalid;
  334. break;
  335. case PAT_SEQ:
  336. for (p = buf, len = 0; len < nbytes; ++p, ++len)
  337. if (*p != len % 63) {
  338. expected = len % 63;
  339. goto invalid;
  340. }
  341. break;
  342. case PAT_PIPE:
  343. ret = fwrite(buf, nbytes, 1, stdout);
  344. if (ret > 0)
  345. fflush(stdout);
  346. break;
  347. invalid:
  348. err("bad OUT byte %zd, expected %02x got %02x\n",
  349. len, expected, *p);
  350. for (p = buf, len = 0; len < nbytes; ++p, ++len) {
  351. if (0 == (len % 32))
  352. fprintf(stderr, "%4zd:", len);
  353. fprintf(stderr, " %02x", *p);
  354. if (31 == (len % 32))
  355. fprintf(stderr, "\n");
  356. }
  357. fflush(stderr);
  358. errno = EILSEQ;
  359. return -1;
  360. }
  361. return len;
  362. }
  363. /******************** Endpoints routines ************************************/
  364. static void handle_setup(const struct usb_ctrlrequest *setup)
  365. {
  366. printf("bRequestType = %d\n", setup->bRequestType);
  367. printf("bRequest = %d\n", setup->bRequest);
  368. printf("wValue = %d\n", le16_to_cpu(setup->wValue));
  369. printf("wIndex = %d\n", le16_to_cpu(setup->wIndex));
  370. printf("wLength = %d\n", le16_to_cpu(setup->wLength));
  371. }
  372. static ssize_t
  373. ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
  374. {
  375. static const char *const names[] = {
  376. [FUNCTIONFS_BIND] = "BIND",
  377. [FUNCTIONFS_UNBIND] = "UNBIND",
  378. [FUNCTIONFS_ENABLE] = "ENABLE",
  379. [FUNCTIONFS_DISABLE] = "DISABLE",
  380. [FUNCTIONFS_SETUP] = "SETUP",
  381. [FUNCTIONFS_SUSPEND] = "SUSPEND",
  382. [FUNCTIONFS_RESUME] = "RESUME",
  383. };
  384. const struct usb_functionfs_event *event = buf;
  385. size_t n;
  386. (void)ignore;
  387. for (n = nbytes / sizeof *event; n; --n, ++event)
  388. switch (event->type) {
  389. case FUNCTIONFS_BIND:
  390. case FUNCTIONFS_UNBIND:
  391. case FUNCTIONFS_ENABLE:
  392. case FUNCTIONFS_DISABLE:
  393. case FUNCTIONFS_SETUP:
  394. case FUNCTIONFS_SUSPEND:
  395. case FUNCTIONFS_RESUME:
  396. printf("Event %s\n", names[event->type]);
  397. if (event->type == FUNCTIONFS_SETUP)
  398. handle_setup(&event->u.setup);
  399. break;
  400. default:
  401. printf("Event %03u (unknown)\n", event->type);
  402. }
  403. return nbytes;
  404. }
  405. static void ep0_init(struct thread *t)
  406. {
  407. ssize_t ret;
  408. info("%s: writing descriptors\n", t->filename);
  409. ret = write(t->fd, &descriptors, sizeof descriptors);
  410. die_on(ret < 0, "%s: write: descriptors", t->filename);
  411. info("%s: writing strings\n", t->filename);
  412. ret = write(t->fd, &strings, sizeof strings);
  413. die_on(ret < 0, "%s: write: strings", t->filename);
  414. }
  415. /******************** Main **************************************************/
  416. int main(void)
  417. {
  418. unsigned i;
  419. /* XXX TODO: Argument parsing missing */
  420. init_thread(threads);
  421. ep0_init(threads);
  422. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  423. init_thread(threads + i);
  424. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  425. start_thread(threads + i);
  426. start_thread_helper(threads);
  427. for (i = 1; i < sizeof threads / sizeof *threads; ++i)
  428. join_thread(threads + i);
  429. return 0;
  430. }