aio_simple.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * This is free and unencumbered software released into the public domain.
  3. *
  4. * Anyone is free to copy, modify, publish, use, compile, sell, or
  5. * distribute this software, either in source code form or as a compiled
  6. * binary, for any purpose, commercial or non-commercial, and by any
  7. * means.
  8. *
  9. * In jurisdictions that recognize copyright laws, the author or authors
  10. * of this software dedicate any and all copyright interest in the
  11. * software to the public domain. We make this dedication for the benefit
  12. * of the public at large and to the detriment of our heirs and
  13. * successors. We intend this dedication to be an overt act of
  14. * relinquishment in perpetuity of all present and future rights to this
  15. * software under copyright law.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * For more information, please refer to <http://unlicense.org/>
  26. */
  27. #define _BSD_SOURCE /* for endian.h */
  28. #include <endian.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/ioctl.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include <sys/poll.h>
  39. #include <unistd.h>
  40. #include <stdbool.h>
  41. #include <sys/eventfd.h>
  42. #include "libaio.h"
  43. #define IOCB_FLAG_RESFD (1 << 0)
  44. #include <linux/usb/functionfs.h>
  45. #define BUF_LEN 8192
  46. /******************** Descriptors and Strings *******************************/
  47. static const struct {
  48. struct usb_functionfs_descs_head_v2 header;
  49. __le32 fs_count;
  50. __le32 hs_count;
  51. struct {
  52. struct usb_interface_descriptor intf;
  53. struct usb_endpoint_descriptor_no_audio bulk_sink;
  54. struct usb_endpoint_descriptor_no_audio bulk_source;
  55. } __attribute__ ((__packed__)) fs_descs, hs_descs;
  56. } __attribute__ ((__packed__)) descriptors = {
  57. .header = {
  58. .magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
  59. .flags = htole32(FUNCTIONFS_HAS_FS_DESC |
  60. FUNCTIONFS_HAS_HS_DESC),
  61. .length = htole32(sizeof(descriptors)),
  62. },
  63. .fs_count = htole32(3),
  64. .fs_descs = {
  65. .intf = {
  66. .bLength = sizeof(descriptors.fs_descs.intf),
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. .bNumEndpoints = 2,
  69. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  70. .iInterface = 1,
  71. },
  72. .bulk_sink = {
  73. .bLength = sizeof(descriptors.fs_descs.bulk_sink),
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bEndpointAddress = 1 | USB_DIR_IN,
  76. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  77. },
  78. .bulk_source = {
  79. .bLength = sizeof(descriptors.fs_descs.bulk_source),
  80. .bDescriptorType = USB_DT_ENDPOINT,
  81. .bEndpointAddress = 2 | USB_DIR_OUT,
  82. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  83. },
  84. },
  85. .hs_count = htole32(3),
  86. .hs_descs = {
  87. .intf = {
  88. .bLength = sizeof(descriptors.hs_descs.intf),
  89. .bDescriptorType = USB_DT_INTERFACE,
  90. .bNumEndpoints = 2,
  91. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  92. .iInterface = 1,
  93. },
  94. .bulk_sink = {
  95. .bLength = sizeof(descriptors.hs_descs.bulk_sink),
  96. .bDescriptorType = USB_DT_ENDPOINT,
  97. .bEndpointAddress = 1 | USB_DIR_IN,
  98. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  99. },
  100. .bulk_source = {
  101. .bLength = sizeof(descriptors.hs_descs.bulk_source),
  102. .bDescriptorType = USB_DT_ENDPOINT,
  103. .bEndpointAddress = 2 | USB_DIR_OUT,
  104. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  105. },
  106. },
  107. };
  108. #define STR_INTERFACE "AIO Test"
  109. static const struct {
  110. struct usb_functionfs_strings_head header;
  111. struct {
  112. __le16 code;
  113. const char str1[sizeof(STR_INTERFACE)];
  114. } __attribute__ ((__packed__)) lang0;
  115. } __attribute__ ((__packed__)) strings = {
  116. .header = {
  117. .magic = htole32(FUNCTIONFS_STRINGS_MAGIC),
  118. .length = htole32(sizeof(strings)),
  119. .str_count = htole32(1),
  120. .lang_count = htole32(1),
  121. },
  122. .lang0 = {
  123. htole16(0x0409), /* en-us */
  124. STR_INTERFACE,
  125. },
  126. };
  127. /******************** Endpoints handling *******************************/
  128. static void display_event(struct usb_functionfs_event *event)
  129. {
  130. static const char *const names[] = {
  131. [FUNCTIONFS_BIND] = "BIND",
  132. [FUNCTIONFS_UNBIND] = "UNBIND",
  133. [FUNCTIONFS_ENABLE] = "ENABLE",
  134. [FUNCTIONFS_DISABLE] = "DISABLE",
  135. [FUNCTIONFS_SETUP] = "SETUP",
  136. [FUNCTIONFS_SUSPEND] = "SUSPEND",
  137. [FUNCTIONFS_RESUME] = "RESUME",
  138. };
  139. switch (event->type) {
  140. case FUNCTIONFS_BIND:
  141. case FUNCTIONFS_UNBIND:
  142. case FUNCTIONFS_ENABLE:
  143. case FUNCTIONFS_DISABLE:
  144. case FUNCTIONFS_SETUP:
  145. case FUNCTIONFS_SUSPEND:
  146. case FUNCTIONFS_RESUME:
  147. printf("Event %s\n", names[event->type]);
  148. }
  149. }
  150. static void handle_ep0(int ep0, bool *ready)
  151. {
  152. struct usb_functionfs_event event;
  153. int ret;
  154. struct pollfd pfds[1];
  155. pfds[0].fd = ep0;
  156. pfds[0].events = POLLIN;
  157. ret = poll(pfds, 1, 0);
  158. if (ret && (pfds[0].revents & POLLIN)) {
  159. ret = read(ep0, &event, sizeof(event));
  160. if (!ret) {
  161. perror("unable to read event from ep0");
  162. return;
  163. }
  164. display_event(&event);
  165. switch (event.type) {
  166. case FUNCTIONFS_SETUP:
  167. if (event.u.setup.bRequestType & USB_DIR_IN)
  168. write(ep0, NULL, 0);
  169. else
  170. read(ep0, NULL, 0);
  171. break;
  172. case FUNCTIONFS_ENABLE:
  173. *ready = true;
  174. break;
  175. case FUNCTIONFS_DISABLE:
  176. *ready = false;
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. int main(int argc, char *argv[])
  184. {
  185. int i, ret;
  186. char *ep_path;
  187. int ep0;
  188. int ep[2];
  189. io_context_t ctx;
  190. int evfd;
  191. fd_set rfds;
  192. char *buf_in, *buf_out;
  193. struct iocb *iocb_in, *iocb_out;
  194. int req_in = 0, req_out = 0;
  195. bool ready;
  196. if (argc != 2) {
  197. printf("ffs directory not specified!\n");
  198. return 1;
  199. }
  200. ep_path = malloc(strlen(argv[1]) + 4 /* "/ep#" */ + 1 /* '\0' */);
  201. if (!ep_path) {
  202. perror("malloc");
  203. return 1;
  204. }
  205. /* open endpoint files */
  206. sprintf(ep_path, "%s/ep0", argv[1]);
  207. ep0 = open(ep_path, O_RDWR);
  208. if (ep0 < 0) {
  209. perror("unable to open ep0");
  210. return 1;
  211. }
  212. if (write(ep0, &descriptors, sizeof(descriptors)) < 0) {
  213. perror("unable do write descriptors");
  214. return 1;
  215. }
  216. if (write(ep0, &strings, sizeof(strings)) < 0) {
  217. perror("unable to write strings");
  218. return 1;
  219. }
  220. for (i = 0; i < 2; ++i) {
  221. sprintf(ep_path, "%s/ep%d", argv[1], i+1);
  222. ep[i] = open(ep_path, O_RDWR);
  223. if (ep[i] < 0) {
  224. printf("unable to open ep%d: %s\n", i+1,
  225. strerror(errno));
  226. return 1;
  227. }
  228. }
  229. free(ep_path);
  230. memset(&ctx, 0, sizeof(ctx));
  231. /* setup aio context to handle up to 2 requests */
  232. if (io_setup(2, &ctx) < 0) {
  233. perror("unable to setup aio");
  234. return 1;
  235. }
  236. evfd = eventfd(0, 0);
  237. if (evfd < 0) {
  238. perror("unable to open eventfd");
  239. return 1;
  240. }
  241. /* alloc buffers and requests */
  242. buf_in = malloc(BUF_LEN);
  243. buf_out = malloc(BUF_LEN);
  244. iocb_in = malloc(sizeof(*iocb_in));
  245. iocb_out = malloc(sizeof(*iocb_out));
  246. while (1) {
  247. FD_ZERO(&rfds);
  248. FD_SET(ep0, &rfds);
  249. FD_SET(evfd, &rfds);
  250. ret = select(((ep0 > evfd) ? ep0 : evfd)+1,
  251. &rfds, NULL, NULL, NULL);
  252. if (ret < 0) {
  253. if (errno == EINTR)
  254. continue;
  255. perror("select");
  256. break;
  257. }
  258. if (FD_ISSET(ep0, &rfds))
  259. handle_ep0(ep0, &ready);
  260. /* we are waiting for function ENABLE */
  261. if (!ready)
  262. continue;
  263. /* if something was submitted we wait for event */
  264. if (FD_ISSET(evfd, &rfds)) {
  265. uint64_t ev_cnt;
  266. ret = read(evfd, &ev_cnt, sizeof(ev_cnt));
  267. if (ret < 0) {
  268. perror("unable to read eventfd");
  269. break;
  270. }
  271. struct io_event e[2];
  272. /* we wait for one event */
  273. ret = io_getevents(ctx, 1, 2, e, NULL);
  274. /* if we got event */
  275. for (i = 0; i < ret; ++i) {
  276. if (e[i].obj->aio_fildes == ep[0]) {
  277. printf("ev=in; ret=%lu\n", e[i].res);
  278. req_in = 0;
  279. } else if (e[i].obj->aio_fildes == ep[1]) {
  280. printf("ev=out; ret=%lu\n", e[i].res);
  281. req_out = 0;
  282. }
  283. }
  284. }
  285. if (!req_in) { /* if IN transfer not requested*/
  286. /* prepare write request */
  287. io_prep_pwrite(iocb_in, ep[0], buf_in, BUF_LEN, 0);
  288. /* enable eventfd notification */
  289. iocb_in->u.c.flags |= IOCB_FLAG_RESFD;
  290. iocb_in->u.c.resfd = evfd;
  291. /* submit table of requests */
  292. ret = io_submit(ctx, 1, &iocb_in);
  293. if (ret >= 0) { /* if ret > 0 request is queued */
  294. req_in = 1;
  295. printf("submit: in\n");
  296. } else
  297. perror("unable to submit request");
  298. }
  299. if (!req_out) { /* if OUT transfer not requested */
  300. /* prepare read request */
  301. io_prep_pread(iocb_out, ep[1], buf_out, BUF_LEN, 0);
  302. /* enable eventfs notification */
  303. iocb_out->u.c.flags |= IOCB_FLAG_RESFD;
  304. iocb_out->u.c.resfd = evfd;
  305. /* submit table of requests */
  306. ret = io_submit(ctx, 1, &iocb_out);
  307. if (ret >= 0) { /* if ret > 0 request is queued */
  308. req_out = 1;
  309. printf("submit: out\n");
  310. } else
  311. perror("unable to submit request");
  312. }
  313. }
  314. /* free resources */
  315. io_destroy(ctx);
  316. free(buf_in);
  317. free(buf_out);
  318. free(iocb_in);
  319. free(iocb_out);
  320. for (i = 0; i < 2; ++i)
  321. close(ep[i]);
  322. close(ep0);
  323. return 0;
  324. }