aio_simple.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. .wMaxPacketSize = htole16(512),
  100. },
  101. .bulk_source = {
  102. .bLength = sizeof(descriptors.hs_descs.bulk_source),
  103. .bDescriptorType = USB_DT_ENDPOINT,
  104. .bEndpointAddress = 2 | USB_DIR_OUT,
  105. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  106. .wMaxPacketSize = htole16(512),
  107. },
  108. },
  109. };
  110. #define STR_INTERFACE "AIO Test"
  111. static const struct {
  112. struct usb_functionfs_strings_head header;
  113. struct {
  114. __le16 code;
  115. const char str1[sizeof(STR_INTERFACE)];
  116. } __attribute__ ((__packed__)) lang0;
  117. } __attribute__ ((__packed__)) strings = {
  118. .header = {
  119. .magic = htole32(FUNCTIONFS_STRINGS_MAGIC),
  120. .length = htole32(sizeof(strings)),
  121. .str_count = htole32(1),
  122. .lang_count = htole32(1),
  123. },
  124. .lang0 = {
  125. htole16(0x0409), /* en-us */
  126. STR_INTERFACE,
  127. },
  128. };
  129. /******************** Endpoints handling *******************************/
  130. static void display_event(struct usb_functionfs_event *event)
  131. {
  132. static const char *const names[] = {
  133. [FUNCTIONFS_BIND] = "BIND",
  134. [FUNCTIONFS_UNBIND] = "UNBIND",
  135. [FUNCTIONFS_ENABLE] = "ENABLE",
  136. [FUNCTIONFS_DISABLE] = "DISABLE",
  137. [FUNCTIONFS_SETUP] = "SETUP",
  138. [FUNCTIONFS_SUSPEND] = "SUSPEND",
  139. [FUNCTIONFS_RESUME] = "RESUME",
  140. };
  141. switch (event->type) {
  142. case FUNCTIONFS_BIND:
  143. case FUNCTIONFS_UNBIND:
  144. case FUNCTIONFS_ENABLE:
  145. case FUNCTIONFS_DISABLE:
  146. case FUNCTIONFS_SETUP:
  147. case FUNCTIONFS_SUSPEND:
  148. case FUNCTIONFS_RESUME:
  149. printf("Event %s\n", names[event->type]);
  150. }
  151. }
  152. static void handle_ep0(int ep0, bool *ready)
  153. {
  154. struct usb_functionfs_event event;
  155. int ret;
  156. struct pollfd pfds[1];
  157. pfds[0].fd = ep0;
  158. pfds[0].events = POLLIN;
  159. ret = poll(pfds, 1, 0);
  160. if (ret && (pfds[0].revents & POLLIN)) {
  161. ret = read(ep0, &event, sizeof(event));
  162. if (!ret) {
  163. perror("unable to read event from ep0");
  164. return;
  165. }
  166. display_event(&event);
  167. switch (event.type) {
  168. case FUNCTIONFS_SETUP:
  169. if (event.u.setup.bRequestType & USB_DIR_IN)
  170. write(ep0, NULL, 0);
  171. else
  172. read(ep0, NULL, 0);
  173. break;
  174. case FUNCTIONFS_ENABLE:
  175. *ready = true;
  176. break;
  177. case FUNCTIONFS_DISABLE:
  178. *ready = false;
  179. break;
  180. default:
  181. break;
  182. }
  183. }
  184. }
  185. int main(int argc, char *argv[])
  186. {
  187. int i, ret;
  188. char *ep_path;
  189. int ep0;
  190. int ep[2];
  191. io_context_t ctx;
  192. int evfd;
  193. fd_set rfds;
  194. char *buf_in, *buf_out;
  195. struct iocb *iocb_in, *iocb_out;
  196. int req_in = 0, req_out = 0;
  197. bool ready;
  198. if (argc != 2) {
  199. printf("ffs directory not specified!\n");
  200. return 1;
  201. }
  202. ep_path = malloc(strlen(argv[1]) + 4 /* "/ep#" */ + 1 /* '\0' */);
  203. if (!ep_path) {
  204. perror("malloc");
  205. return 1;
  206. }
  207. /* open endpoint files */
  208. sprintf(ep_path, "%s/ep0", argv[1]);
  209. ep0 = open(ep_path, O_RDWR);
  210. if (ep0 < 0) {
  211. perror("unable to open ep0");
  212. return 1;
  213. }
  214. if (write(ep0, &descriptors, sizeof(descriptors)) < 0) {
  215. perror("unable do write descriptors");
  216. return 1;
  217. }
  218. if (write(ep0, &strings, sizeof(strings)) < 0) {
  219. perror("unable to write strings");
  220. return 1;
  221. }
  222. for (i = 0; i < 2; ++i) {
  223. sprintf(ep_path, "%s/ep%d", argv[1], i+1);
  224. ep[i] = open(ep_path, O_RDWR);
  225. if (ep[i] < 0) {
  226. printf("unable to open ep%d: %s\n", i+1,
  227. strerror(errno));
  228. return 1;
  229. }
  230. }
  231. free(ep_path);
  232. memset(&ctx, 0, sizeof(ctx));
  233. /* setup aio context to handle up to 2 requests */
  234. if (io_setup(2, &ctx) < 0) {
  235. perror("unable to setup aio");
  236. return 1;
  237. }
  238. evfd = eventfd(0, 0);
  239. if (evfd < 0) {
  240. perror("unable to open eventfd");
  241. return 1;
  242. }
  243. /* alloc buffers and requests */
  244. buf_in = malloc(BUF_LEN);
  245. buf_out = malloc(BUF_LEN);
  246. iocb_in = malloc(sizeof(*iocb_in));
  247. iocb_out = malloc(sizeof(*iocb_out));
  248. while (1) {
  249. FD_ZERO(&rfds);
  250. FD_SET(ep0, &rfds);
  251. FD_SET(evfd, &rfds);
  252. ret = select(((ep0 > evfd) ? ep0 : evfd)+1,
  253. &rfds, NULL, NULL, NULL);
  254. if (ret < 0) {
  255. if (errno == EINTR)
  256. continue;
  257. perror("select");
  258. break;
  259. }
  260. if (FD_ISSET(ep0, &rfds))
  261. handle_ep0(ep0, &ready);
  262. /* we are waiting for function ENABLE */
  263. if (!ready)
  264. continue;
  265. /* if something was submitted we wait for event */
  266. if (FD_ISSET(evfd, &rfds)) {
  267. uint64_t ev_cnt;
  268. ret = read(evfd, &ev_cnt, sizeof(ev_cnt));
  269. if (ret < 0) {
  270. perror("unable to read eventfd");
  271. break;
  272. }
  273. struct io_event e[2];
  274. /* we wait for one event */
  275. ret = io_getevents(ctx, 1, 2, e, NULL);
  276. /* if we got event */
  277. for (i = 0; i < ret; ++i) {
  278. if (e[i].obj->aio_fildes == ep[0]) {
  279. printf("ev=in; ret=%lu\n", e[i].res);
  280. req_in = 0;
  281. } else if (e[i].obj->aio_fildes == ep[1]) {
  282. printf("ev=out; ret=%lu\n", e[i].res);
  283. req_out = 0;
  284. }
  285. }
  286. }
  287. if (!req_in) { /* if IN transfer not requested*/
  288. /* prepare write request */
  289. io_prep_pwrite(iocb_in, ep[0], buf_in, BUF_LEN, 0);
  290. /* enable eventfd notification */
  291. iocb_in->u.c.flags |= IOCB_FLAG_RESFD;
  292. iocb_in->u.c.resfd = evfd;
  293. /* submit table of requests */
  294. ret = io_submit(ctx, 1, &iocb_in);
  295. if (ret >= 0) { /* if ret > 0 request is queued */
  296. req_in = 1;
  297. printf("submit: in\n");
  298. } else
  299. perror("unable to submit request");
  300. }
  301. if (!req_out) { /* if OUT transfer not requested */
  302. /* prepare read request */
  303. io_prep_pread(iocb_out, ep[1], buf_out, BUF_LEN, 0);
  304. /* enable eventfs notification */
  305. iocb_out->u.c.flags |= IOCB_FLAG_RESFD;
  306. iocb_out->u.c.resfd = evfd;
  307. /* submit table of requests */
  308. ret = io_submit(ctx, 1, &iocb_out);
  309. if (ret >= 0) { /* if ret > 0 request is queued */
  310. req_out = 1;
  311. printf("submit: out\n");
  312. } else
  313. perror("unable to submit request");
  314. }
  315. }
  316. /* free resources */
  317. io_destroy(ctx);
  318. free(buf_in);
  319. free(buf_out);
  320. free(iocb_in);
  321. free(iocb_out);
  322. for (i = 0; i < 2; ++i)
  323. close(ep[i]);
  324. close(ep0);
  325. return 0;
  326. }