ffs-test.c 12 KB

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