2 * This is free and unencumbered software released into the public domain.
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
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.
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.
25 * For more information, please refer to <http://unlicense.org/>
28 #define _BSD_SOURCE /* for endian.h */
37 #include <sys/ioctl.h>
39 #include <sys/types.h>
43 #include <sys/eventfd.h>
46 #define IOCB_FLAG_RESFD (1 << 0)
48 #include <linux/usb/functionfs.h>
52 /******************** Descriptors and Strings *******************************/
55 struct usb_functionfs_descs_head_v2 header
;
59 struct usb_interface_descriptor intf
;
60 struct usb_endpoint_descriptor_no_audio bulk_sink
;
61 struct usb_endpoint_descriptor_no_audio bulk_source
;
62 } __attribute__ ((__packed__
)) fs_descs
, hs_descs
;
63 } __attribute__ ((__packed__
)) descriptors
= {
65 .magic
= htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2
),
66 .flags
= htole32(FUNCTIONFS_HAS_FS_DESC
|
67 FUNCTIONFS_HAS_HS_DESC
),
68 .length
= htole32(sizeof(descriptors
)),
70 .fs_count
= htole32(3),
73 .bLength
= sizeof(descriptors
.fs_descs
.intf
),
74 .bDescriptorType
= USB_DT_INTERFACE
,
76 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
80 .bLength
= sizeof(descriptors
.fs_descs
.bulk_sink
),
81 .bDescriptorType
= USB_DT_ENDPOINT
,
82 .bEndpointAddress
= 1 | USB_DIR_IN
,
83 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
86 .bLength
= sizeof(descriptors
.fs_descs
.bulk_source
),
87 .bDescriptorType
= USB_DT_ENDPOINT
,
88 .bEndpointAddress
= 2 | USB_DIR_OUT
,
89 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
92 .hs_count
= htole32(3),
95 .bLength
= sizeof(descriptors
.hs_descs
.intf
),
96 .bDescriptorType
= USB_DT_INTERFACE
,
98 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
102 .bLength
= sizeof(descriptors
.hs_descs
.bulk_sink
),
103 .bDescriptorType
= USB_DT_ENDPOINT
,
104 .bEndpointAddress
= 1 | USB_DIR_IN
,
105 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
108 .bLength
= sizeof(descriptors
.hs_descs
.bulk_source
),
109 .bDescriptorType
= USB_DT_ENDPOINT
,
110 .bEndpointAddress
= 2 | USB_DIR_OUT
,
111 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
116 #define STR_INTERFACE "AIO Test"
118 static const struct {
119 struct usb_functionfs_strings_head header
;
122 const char str1
[sizeof(STR_INTERFACE
)];
123 } __attribute__ ((__packed__
)) lang0
;
124 } __attribute__ ((__packed__
)) strings
= {
126 .magic
= htole32(FUNCTIONFS_STRINGS_MAGIC
),
127 .length
= htole32(sizeof(strings
)),
128 .str_count
= htole32(1),
129 .lang_count
= htole32(1),
132 htole16(0x0409), /* en-us */
137 /******************** Endpoints handling *******************************/
139 static void display_event(struct usb_functionfs_event
*event
)
141 static const char *const names
[] = {
142 [FUNCTIONFS_BIND
] = "BIND",
143 [FUNCTIONFS_UNBIND
] = "UNBIND",
144 [FUNCTIONFS_ENABLE
] = "ENABLE",
145 [FUNCTIONFS_DISABLE
] = "DISABLE",
146 [FUNCTIONFS_SETUP
] = "SETUP",
147 [FUNCTIONFS_SUSPEND
] = "SUSPEND",
148 [FUNCTIONFS_RESUME
] = "RESUME",
150 switch (event
->type
) {
151 case FUNCTIONFS_BIND
:
152 case FUNCTIONFS_UNBIND
:
153 case FUNCTIONFS_ENABLE
:
154 case FUNCTIONFS_DISABLE
:
155 case FUNCTIONFS_SETUP
:
156 case FUNCTIONFS_SUSPEND
:
157 case FUNCTIONFS_RESUME
:
158 printf("Event %s\n", names
[event
->type
]);
162 static void handle_ep0(int ep0
, bool *ready
)
164 struct usb_functionfs_event event
;
167 struct pollfd pfds
[1];
169 pfds
[0].events
= POLLIN
;
171 ret
= poll(pfds
, 1, 0);
173 if (ret
&& (pfds
[0].revents
& POLLIN
)) {
174 ret
= read(ep0
, &event
, sizeof(event
));
176 perror("unable to read event from ep0");
179 display_event(&event
);
180 switch (event
.type
) {
181 case FUNCTIONFS_SETUP
:
182 if (event
.u
.setup
.bRequestType
& USB_DIR_IN
)
188 case FUNCTIONFS_ENABLE
:
192 case FUNCTIONFS_DISABLE
:
202 int main(int argc
, char *argv
[])
215 char *buf_in
, *buf_out
;
216 struct iocb
*iocb_in
, *iocb_out
;
217 int req_in
= 0, req_out
= 0;
221 printf("ffs directory not specified!\n");
225 ep_path
= malloc(strlen(argv
[1]) + 4 /* "/ep#" */ + 1 /* '\0' */);
231 /* open endpoint files */
232 sprintf(ep_path
, "%s/ep0", argv
[1]);
233 ep0
= open(ep_path
, O_RDWR
);
235 perror("unable to open ep0");
238 if (write(ep0
, &descriptors
, sizeof(descriptors
)) < 0) {
239 perror("unable do write descriptors");
242 if (write(ep0
, &strings
, sizeof(strings
)) < 0) {
243 perror("unable to write strings");
246 for (i
= 0; i
< 2; ++i
) {
247 sprintf(ep_path
, "%s/ep%d", argv
[1], i
+1);
248 ep
[i
] = open(ep_path
, O_RDWR
);
250 printf("unable to open ep%d: %s\n", i
+1,
258 memset(&ctx
, 0, sizeof(ctx
));
259 /* setup aio context to handle up to 2 requests */
260 if (io_setup(2, &ctx
) < 0) {
261 perror("unable to setup aio");
265 evfd
= eventfd(0, 0);
267 perror("unable to open eventfd");
271 /* alloc buffers and requests */
272 buf_in
= malloc(BUF_LEN
);
273 buf_out
= malloc(BUF_LEN
);
274 iocb_in
= malloc(sizeof(*iocb_in
));
275 iocb_out
= malloc(sizeof(*iocb_out
));
282 ret
= select(((ep0
> evfd
) ? ep0
: evfd
)+1,
283 &rfds
, NULL
, NULL
, NULL
);
291 if (FD_ISSET(ep0
, &rfds
))
292 handle_ep0(ep0
, &ready
);
294 /* we are waiting for function ENABLE */
298 /* if something was submitted we wait for event */
299 if (FD_ISSET(evfd
, &rfds
)) {
301 ret
= read(evfd
, &ev_cnt
, sizeof(ev_cnt
));
303 perror("unable to read eventfd");
307 struct io_event e
[2];
308 /* we wait for one event */
309 ret
= io_getevents(ctx
, 1, 2, e
, NULL
);
310 /* if we got event */
311 for (i
= 0; i
< ret
; ++i
) {
312 if (e
[i
].obj
->aio_fildes
== ep
[0]) {
313 printf("ev=in; ret=%lu\n", e
[i
].res
);
315 } else if (e
[i
].obj
->aio_fildes
== ep
[1]) {
316 printf("ev=out; ret=%lu\n", e
[i
].res
);
322 if (!req_in
) { /* if IN transfer not requested*/
323 /* prepare write request */
324 io_prep_pwrite(iocb_in
, ep
[0], buf_in
, BUF_LEN
, 0);
325 /* enable eventfd notification */
326 iocb_in
->u
.c
.flags
|= IOCB_FLAG_RESFD
;
327 iocb_in
->u
.c
.resfd
= evfd
;
328 /* submit table of requests */
329 ret
= io_submit(ctx
, 1, &iocb_in
);
330 if (ret
>= 0) { /* if ret > 0 request is queued */
332 printf("submit: in\n");
334 perror("unable to submit request");
336 if (!req_out
) { /* if OUT transfer not requested */
337 /* prepare read request */
338 io_prep_pread(iocb_out
, ep
[1], buf_out
, BUF_LEN
, 0);
339 /* enable eventfs notification */
340 iocb_out
->u
.c
.flags
|= IOCB_FLAG_RESFD
;
341 iocb_out
->u
.c
.resfd
= evfd
;
342 /* submit table of requests */
343 ret
= io_submit(ctx
, 1, &iocb_out
);
344 if (ret
>= 0) { /* if ret > 0 request is queued */
346 printf("submit: out\n");
348 perror("unable to submit request");
361 for (i
= 0; i
< 2; ++i
)