aio_simple.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #define _BSD_SOURCE /* for endian.h */
  2. #include <endian.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/poll.h>
  13. #include <unistd.h>
  14. #include <stdbool.h>
  15. #include <sys/eventfd.h>
  16. #include "libaio.h"
  17. #define IOCB_FLAG_RESFD (1 << 0)
  18. #include <linux/usb/functionfs.h>
  19. #define BUF_LEN 8192
  20. /******************** Descriptors and Strings *******************************/
  21. static const struct {
  22. struct usb_functionfs_descs_head header;
  23. struct {
  24. struct usb_interface_descriptor intf;
  25. struct usb_endpoint_descriptor_no_audio bulk_sink;
  26. struct usb_endpoint_descriptor_no_audio bulk_source;
  27. } __attribute__ ((__packed__)) fs_descs, hs_descs;
  28. } __attribute__ ((__packed__)) descriptors = {
  29. .header = {
  30. .magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC),
  31. .length = htole32(sizeof(descriptors)),
  32. .fs_count = 3,
  33. .hs_count = 3,
  34. },
  35. .fs_descs = {
  36. .intf = {
  37. .bLength = sizeof(descriptors.fs_descs.intf),
  38. .bDescriptorType = USB_DT_INTERFACE,
  39. .bNumEndpoints = 2,
  40. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  41. .iInterface = 1,
  42. },
  43. .bulk_sink = {
  44. .bLength = sizeof(descriptors.fs_descs.bulk_sink),
  45. .bDescriptorType = USB_DT_ENDPOINT,
  46. .bEndpointAddress = 1 | USB_DIR_IN,
  47. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  48. },
  49. .bulk_source = {
  50. .bLength = sizeof(descriptors.fs_descs.bulk_source),
  51. .bDescriptorType = USB_DT_ENDPOINT,
  52. .bEndpointAddress = 2 | USB_DIR_OUT,
  53. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  54. },
  55. },
  56. .hs_descs = {
  57. .intf = {
  58. .bLength = sizeof(descriptors.hs_descs.intf),
  59. .bDescriptorType = USB_DT_INTERFACE,
  60. .bNumEndpoints = 2,
  61. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  62. .iInterface = 1,
  63. },
  64. .bulk_sink = {
  65. .bLength = sizeof(descriptors.hs_descs.bulk_sink),
  66. .bDescriptorType = USB_DT_ENDPOINT,
  67. .bEndpointAddress = 1 | USB_DIR_IN,
  68. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  69. },
  70. .bulk_source = {
  71. .bLength = sizeof(descriptors.hs_descs.bulk_source),
  72. .bDescriptorType = USB_DT_ENDPOINT,
  73. .bEndpointAddress = 2 | USB_DIR_OUT,
  74. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  75. },
  76. },
  77. };
  78. #define STR_INTERFACE "AIO Test"
  79. static const struct {
  80. struct usb_functionfs_strings_head header;
  81. struct {
  82. __le16 code;
  83. const char str1[sizeof(STR_INTERFACE)];
  84. } __attribute__ ((__packed__)) lang0;
  85. } __attribute__ ((__packed__)) strings = {
  86. .header = {
  87. .magic = htole32(FUNCTIONFS_STRINGS_MAGIC),
  88. .length = htole32(sizeof(strings)),
  89. .str_count = htole32(1),
  90. .lang_count = htole32(1),
  91. },
  92. .lang0 = {
  93. htole16(0x0409), /* en-us */
  94. STR_INTERFACE,
  95. },
  96. };
  97. /******************** Endpoints handling *******************************/
  98. static void display_event(struct usb_functionfs_event *event)
  99. {
  100. static const char *const names[] = {
  101. [FUNCTIONFS_BIND] = "BIND",
  102. [FUNCTIONFS_UNBIND] = "UNBIND",
  103. [FUNCTIONFS_ENABLE] = "ENABLE",
  104. [FUNCTIONFS_DISABLE] = "DISABLE",
  105. [FUNCTIONFS_SETUP] = "SETUP",
  106. [FUNCTIONFS_SUSPEND] = "SUSPEND",
  107. [FUNCTIONFS_RESUME] = "RESUME",
  108. };
  109. switch (event->type) {
  110. case FUNCTIONFS_BIND:
  111. case FUNCTIONFS_UNBIND:
  112. case FUNCTIONFS_ENABLE:
  113. case FUNCTIONFS_DISABLE:
  114. case FUNCTIONFS_SETUP:
  115. case FUNCTIONFS_SUSPEND:
  116. case FUNCTIONFS_RESUME:
  117. printf("Event %s\n", names[event->type]);
  118. }
  119. }
  120. static void handle_ep0(int ep0, bool *ready)
  121. {
  122. struct usb_functionfs_event event;
  123. int ret;
  124. struct pollfd pfds[1];
  125. pfds[0].fd = ep0;
  126. pfds[0].events = POLLIN;
  127. ret = poll(pfds, 1, 0);
  128. if (ret && (pfds[0].revents & POLLIN)) {
  129. ret = read(ep0, &event, sizeof(event));
  130. if (!ret) {
  131. perror("unable to read event from ep0");
  132. return;
  133. }
  134. display_event(&event);
  135. switch (event.type) {
  136. case FUNCTIONFS_SETUP:
  137. if (event.u.setup.bRequestType & USB_DIR_IN)
  138. write(ep0, NULL, 0);
  139. else
  140. read(ep0, NULL, 0);
  141. break;
  142. case FUNCTIONFS_ENABLE:
  143. *ready = true;
  144. break;
  145. case FUNCTIONFS_DISABLE:
  146. *ready = false;
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. }
  153. int main(int argc, char *argv[])
  154. {
  155. int i, ret;
  156. char *ep_path;
  157. int ep0;
  158. int ep[2];
  159. io_context_t ctx;
  160. int evfd;
  161. fd_set rfds;
  162. char *buf_in, *buf_out;
  163. struct iocb *iocb_in, *iocb_out;
  164. int req_in = 0, req_out = 0;
  165. bool ready;
  166. if (argc != 2) {
  167. printf("ffs directory not specified!\n");
  168. return 1;
  169. }
  170. ep_path = malloc(strlen(argv[1]) + 4 /* "/ep#" */ + 1 /* '\0' */);
  171. if (!ep_path) {
  172. perror("malloc");
  173. return 1;
  174. }
  175. /* open endpoint files */
  176. sprintf(ep_path, "%s/ep0", argv[1]);
  177. ep0 = open(ep_path, O_RDWR);
  178. if (ep0 < 0) {
  179. perror("unable to open ep0");
  180. return 1;
  181. }
  182. if (write(ep0, &descriptors, sizeof(descriptors)) < 0) {
  183. perror("unable do write descriptors");
  184. return 1;
  185. }
  186. if (write(ep0, &strings, sizeof(strings)) < 0) {
  187. perror("unable to write strings");
  188. return 1;
  189. }
  190. for (i = 0; i < 2; ++i) {
  191. sprintf(ep_path, "%s/ep%d", argv[1], i+1);
  192. ep[i] = open(ep_path, O_RDWR);
  193. if (ep[i] < 0) {
  194. printf("unable to open ep%d: %s\n", i+1,
  195. strerror(errno));
  196. return 1;
  197. }
  198. }
  199. free(ep_path);
  200. memset(&ctx, 0, sizeof(ctx));
  201. /* setup aio context to handle up to 2 requests */
  202. if (io_setup(2, &ctx) < 0) {
  203. perror("unable to setup aio");
  204. return 1;
  205. }
  206. evfd = eventfd(0, 0);
  207. if (evfd < 0) {
  208. perror("unable to open eventfd");
  209. return 1;
  210. }
  211. /* alloc buffers and requests */
  212. buf_in = malloc(BUF_LEN);
  213. buf_out = malloc(BUF_LEN);
  214. iocb_in = malloc(sizeof(*iocb_in));
  215. iocb_out = malloc(sizeof(*iocb_out));
  216. while (1) {
  217. FD_ZERO(&rfds);
  218. FD_SET(ep0, &rfds);
  219. FD_SET(evfd, &rfds);
  220. ret = select(((ep0 > evfd) ? ep0 : evfd)+1,
  221. &rfds, NULL, NULL, NULL);
  222. if (ret < 0) {
  223. if (errno == EINTR)
  224. continue;
  225. perror("select");
  226. break;
  227. }
  228. if (FD_ISSET(ep0, &rfds))
  229. handle_ep0(ep0, &ready);
  230. /* we are waiting for function ENABLE */
  231. if (!ready)
  232. continue;
  233. /* if something was submitted we wait for event */
  234. if (FD_ISSET(evfd, &rfds)) {
  235. uint64_t ev_cnt;
  236. ret = read(evfd, &ev_cnt, sizeof(ev_cnt));
  237. if (ret < 0) {
  238. perror("unable to read eventfd");
  239. break;
  240. }
  241. struct io_event e[2];
  242. /* we wait for one event */
  243. ret = io_getevents(ctx, 1, 2, e, NULL);
  244. /* if we got event */
  245. for (i = 0; i < ret; ++i) {
  246. if (e[i].obj->aio_fildes == ep[0]) {
  247. printf("ev=in; ret=%lu\n", e[i].res);
  248. req_in = 0;
  249. } else if (e[i].obj->aio_fildes == ep[1]) {
  250. printf("ev=out; ret=%lu\n", e[i].res);
  251. req_out = 0;
  252. }
  253. }
  254. }
  255. if (!req_in) { /* if IN transfer not requested*/
  256. /* prepare write request */
  257. io_prep_pwrite(iocb_in, ep[0], buf_in, BUF_LEN, 0);
  258. /* enable eventfd notification */
  259. iocb_in->u.c.flags |= IOCB_FLAG_RESFD;
  260. iocb_in->u.c.resfd = evfd;
  261. /* submit table of requests */
  262. ret = io_submit(ctx, 1, &iocb_in);
  263. if (ret >= 0) { /* if ret > 0 request is queued */
  264. req_in = 1;
  265. printf("submit: in\n");
  266. } else
  267. perror("unable to submit request");
  268. }
  269. if (!req_out) { /* if OUT transfer not requested */
  270. /* prepare read request */
  271. io_prep_pread(iocb_out, ep[1], buf_out, BUF_LEN, 0);
  272. /* enable eventfs notification */
  273. iocb_out->u.c.flags |= IOCB_FLAG_RESFD;
  274. iocb_out->u.c.resfd = evfd;
  275. /* submit table of requests */
  276. ret = io_submit(ctx, 1, &iocb_out);
  277. if (ret >= 0) { /* if ret > 0 request is queued */
  278. req_out = 1;
  279. printf("submit: out\n");
  280. } else
  281. perror("unable to submit request");
  282. }
  283. }
  284. /* free resources */
  285. io_destroy(ctx);
  286. free(buf_in);
  287. free(buf_out);
  288. free(iocb_in);
  289. free(iocb_out);
  290. for (i = 0; i < 2; ++i)
  291. close(ep[i]);
  292. close(ep0);
  293. return 0;
  294. }