2 * ffs-test.c -- user mode filesystem api for usb composite function
4 * Copyright (C) 2010 Samsung Electronics
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.
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.
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
22 /* $(CROSS_COMPILE)cc -Wall -Wextra -g -o ffs-test ffs-test.c -lpthread */
25 #define _DEFAULT_SOURCE /* for endian.h */
36 #include <sys/ioctl.h>
38 #include <sys/types.h>
40 #include <tools/le_byteshift.h>
42 #include "../../include/uapi/linux/usb/functionfs.h"
45 /******************** Little Endian Handling ********************************/
48 * cpu_to_le16/32 are used when initializing structures, a context where a
49 * function call is not allowed. To solve this, we code cpu_to_le16/32 in a way
50 * that allows them to be used when initializing structures.
53 #if __BYTE_ORDER == __LITTLE_ENDIAN
54 #define cpu_to_le16(x) (x)
55 #define cpu_to_le32(x) (x)
57 #define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
58 #define cpu_to_le32(x) \
59 ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
60 (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
63 #define le32_to_cpu(x) le32toh(x)
64 #define le16_to_cpu(x) le16toh(x)
66 /******************** Messages and Errors ***********************************/
68 static const char argv0[] = "ffs-test";
70 static unsigned verbosity = 7;
72 static void _msg(unsigned level, const char *fmt, ...)
79 if (level <= verbosity) {
80 static const char levels[8][6] = {
92 fprintf(stderr, "%s: %s ", argv0, levels[level]);
94 vfprintf(stderr, fmt, ap);
97 if (fmt[strlen(fmt) - 1] != '\n') {
99 strerror_r(_errno, buffer, sizeof buffer);
100 fprintf(stderr, ": (-%d) %s\n", _errno, buffer);
107 #define die(...) (_msg(2, __VA_ARGS__), exit(1))
108 #define err(...) _msg(3, __VA_ARGS__)
109 #define warn(...) _msg(4, __VA_ARGS__)
110 #define note(...) _msg(5, __VA_ARGS__)
111 #define info(...) _msg(6, __VA_ARGS__)
112 #define debug(...) _msg(7, __VA_ARGS__)
114 #define die_on(cond, ...) do { \
120 /******************** Descriptors and Strings *******************************/
122 static const struct {
123 struct usb_functionfs_descs_head_v2 header;
128 struct usb_interface_descriptor intf;
129 struct usb_endpoint_descriptor_no_audio sink;
130 struct usb_endpoint_descriptor_no_audio source;
131 } __attribute__((packed)) fs_descs, hs_descs;
133 struct usb_interface_descriptor intf;
134 struct usb_endpoint_descriptor_no_audio sink;
135 struct usb_ss_ep_comp_descriptor sink_comp;
136 struct usb_endpoint_descriptor_no_audio source;
137 struct usb_ss_ep_comp_descriptor source_comp;
139 } __attribute__((packed)) descriptors = {
141 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
142 .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
143 FUNCTIONFS_HAS_HS_DESC |
144 FUNCTIONFS_HAS_SS_DESC),
145 .length = cpu_to_le32(sizeof descriptors),
147 .fs_count = cpu_to_le32(3),
150 .bLength = sizeof descriptors.fs_descs.intf,
151 .bDescriptorType = USB_DT_INTERFACE,
153 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
157 .bLength = sizeof descriptors.fs_descs.sink,
158 .bDescriptorType = USB_DT_ENDPOINT,
159 .bEndpointAddress = 1 | USB_DIR_IN,
160 .bmAttributes = USB_ENDPOINT_XFER_BULK,
161 /* .wMaxPacketSize = autoconfiguration (kernel) */
164 .bLength = sizeof descriptors.fs_descs.source,
165 .bDescriptorType = USB_DT_ENDPOINT,
166 .bEndpointAddress = 2 | USB_DIR_OUT,
167 .bmAttributes = USB_ENDPOINT_XFER_BULK,
168 /* .wMaxPacketSize = autoconfiguration (kernel) */
171 .hs_count = cpu_to_le32(3),
174 .bLength = sizeof descriptors.fs_descs.intf,
175 .bDescriptorType = USB_DT_INTERFACE,
177 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
181 .bLength = sizeof descriptors.hs_descs.sink,
182 .bDescriptorType = USB_DT_ENDPOINT,
183 .bEndpointAddress = 1 | USB_DIR_IN,
184 .bmAttributes = USB_ENDPOINT_XFER_BULK,
185 .wMaxPacketSize = cpu_to_le16(512),
188 .bLength = sizeof descriptors.hs_descs.source,
189 .bDescriptorType = USB_DT_ENDPOINT,
190 .bEndpointAddress = 2 | USB_DIR_OUT,
191 .bmAttributes = USB_ENDPOINT_XFER_BULK,
192 .wMaxPacketSize = cpu_to_le16(512),
193 .bInterval = 1, /* NAK every 1 uframe */
196 .ss_count = cpu_to_le32(5),
199 .bLength = sizeof descriptors.fs_descs.intf,
200 .bDescriptorType = USB_DT_INTERFACE,
202 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
206 .bLength = sizeof descriptors.hs_descs.sink,
207 .bDescriptorType = USB_DT_ENDPOINT,
208 .bEndpointAddress = 1 | USB_DIR_IN,
209 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 .wMaxPacketSize = cpu_to_le16(1024),
213 .bLength = USB_DT_SS_EP_COMP_SIZE,
214 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
217 .wBytesPerInterval = 0,
220 .bLength = sizeof descriptors.hs_descs.source,
221 .bDescriptorType = USB_DT_ENDPOINT,
222 .bEndpointAddress = 2 | USB_DIR_OUT,
223 .bmAttributes = USB_ENDPOINT_XFER_BULK,
224 .wMaxPacketSize = cpu_to_le16(1024),
225 .bInterval = 1, /* NAK every 1 uframe */
228 .bLength = USB_DT_SS_EP_COMP_SIZE,
229 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
232 .wBytesPerInterval = 0,
237 static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
239 const unsigned char *descs_end, *descs_start;
240 __u32 length, fs_count = 0, hs_count = 0, count;
245 const struct usb_functionfs_descs_head_v2 header;
246 const __le32 counts[];
247 } __attribute__((packed)) *const in = descriptors_v2;
248 const __le32 *counts = in->counts;
251 if (le32_to_cpu(in->header.magic) !=
252 FUNCTIONFS_DESCRIPTORS_MAGIC_V2)
254 length = le32_to_cpu(in->header.length);
255 if (length <= sizeof in->header)
257 length -= sizeof in->header;
258 flags = le32_to_cpu(in->header.flags);
259 if (flags & ~(FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
260 FUNCTIONFS_HAS_SS_DESC))
263 #define GET_NEXT_COUNT_IF_FLAG(ret, flg) do { \
264 if (!(flags & (flg))) \
268 ret = le32_to_cpu(*counts); \
273 GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
274 GET_NEXT_COUNT_IF_FLAG(hs_count, FUNCTIONFS_HAS_HS_DESC);
275 GET_NEXT_COUNT_IF_FLAG(count, FUNCTIONFS_HAS_SS_DESC);
277 count = fs_count + hs_count;
280 descs_start = (const void *)counts;
282 #undef GET_NEXT_COUNT_IF_FLAG
286 * Find the end of FS and HS USB descriptors. SS descriptors
287 * are ignored since legacy format does not support them.
289 descs_end = descs_start;
291 if (length < *descs_end)
293 length -= *descs_end;
294 descs_end += *descs_end;
297 /* Allocate legacy descriptors and copy the data. */
299 #pragma GCC diagnostic push
300 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
302 struct usb_functionfs_descs_head header;
304 } __attribute__((packed)) *out;
305 #pragma GCC diagnostic pop
307 length = sizeof out->header + (descs_end - descs_start);
308 out = malloc(length);
309 out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
310 out->header.length = cpu_to_le32(length);
311 out->header.fs_count = cpu_to_le32(fs_count);
312 out->header.hs_count = cpu_to_le32(hs_count);
313 memcpy(out->descriptors, descs_start, descs_end - descs_start);
321 #define STR_INTERFACE_ "Source/Sink"
323 static const struct {
324 struct usb_functionfs_strings_head header;
327 const char str1[sizeof STR_INTERFACE_];
328 } __attribute__((packed)) lang0;
329 } __attribute__((packed)) strings = {
331 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
332 .length = cpu_to_le32(sizeof strings),
333 .str_count = cpu_to_le32(1),
334 .lang_count = cpu_to_le32(1),
337 cpu_to_le16(0x0409), /* en-us */
342 #define STR_INTERFACE strings.lang0.str1
345 /******************** Files and Threads Handling ****************************/
349 static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
350 static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
351 static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
352 static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
353 static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
356 static struct thread {
357 const char *const filename;
360 ssize_t (*in)(struct thread *, void *, size_t);
361 const char *const in_name;
363 ssize_t (*out)(struct thread *, const void *, size_t);
364 const char *const out_name;
372 "ep0", 4 * sizeof(struct usb_functionfs_event),
374 ep0_consume, "<consume>",
386 empty_out_buf, "<out>",
392 static void init_thread(struct thread *t)
394 t->buf = malloc(t->buf_size);
395 die_on(!t->buf, "malloc");
397 t->fd = open(t->filename, O_RDWR);
398 die_on(t->fd < 0, "%s", t->filename);
401 static void cleanup_thread(void *arg)
403 struct thread *t = arg;
411 /* test the FIFO ioctls (non-ep0 code paths) */
413 ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
415 /* ENODEV reported after disconnect */
417 err("%s: get fifo status", t->filename);
419 warn("%s: unclaimed = %d\n", t->filename, ret);
420 if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
421 err("%s: fifo flush", t->filename);
426 err("%s: close", t->filename);
432 static void *start_thread_helper(void *arg)
434 const char *name, *op, *in_name, *out_name;
435 struct thread *t = arg;
438 info("%s: starts\n", t->filename);
439 in_name = t->in_name ? t->in_name : t->filename;
440 out_name = t->out_name ? t->out_name : t->filename;
442 pthread_cleanup_push(cleanup_thread, arg);
445 pthread_testcancel();
447 ret = t->in(t, t->buf, t->buf_size);
449 ret = t->out(t, t->buf, ret);
460 debug("%s: %s: EOF", name, op);
462 } else if (errno == EINTR || errno == EAGAIN) {
463 debug("%s: %s", name, op);
465 warn("%s: %s", name, op);
470 pthread_cleanup_pop(1);
473 info("%s: ends\n", t->filename);
477 static void start_thread(struct thread *t)
479 debug("%s: starting\n", t->filename);
481 die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
482 "pthread_create(%s)", t->filename);
485 static void join_thread(struct thread *t)
487 int ret = pthread_join(t->id, NULL);
490 err("%s: joining thread", t->filename);
492 debug("%s: joined\n", t->filename);
496 static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
498 return read(t->fd, buf, nbytes);
501 static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
503 return write(t->fd, buf, nbytes);
507 /******************** Empty/Fill buffer routines ****************************/
509 /* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
510 enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
511 static enum pattern pattern;
514 fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
523 memset(buf, 0, nbytes);
527 for (p = buf, i = 0; i < nbytes; ++i, ++p)
532 return fread(buf, 1, nbytes, stdin);
539 empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
551 for (p = buf, len = 0; len < nbytes; ++p, ++len)
557 for (p = buf, len = 0; len < nbytes; ++p, ++len)
558 if (*p != len % 63) {
565 ret = fwrite(buf, nbytes, 1, stdout);
571 err("bad OUT byte %zd, expected %02x got %02x\n",
573 for (p = buf, len = 0; len < nbytes; ++p, ++len) {
575 fprintf(stderr, "%4zd:", len);
576 fprintf(stderr, " %02x", *p);
577 if (31 == (len % 32))
578 fprintf(stderr, "\n");
589 /******************** Endpoints routines ************************************/
591 static void handle_setup(const struct usb_ctrlrequest *setup)
593 printf("bRequestType = %d\n", setup->bRequestType);
594 printf("bRequest = %d\n", setup->bRequest);
595 printf("wValue = %d\n", le16_to_cpu(setup->wValue));
596 printf("wIndex = %d\n", le16_to_cpu(setup->wIndex));
597 printf("wLength = %d\n", le16_to_cpu(setup->wLength));
601 ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
603 static const char *const names[] = {
604 [FUNCTIONFS_BIND] = "BIND",
605 [FUNCTIONFS_UNBIND] = "UNBIND",
606 [FUNCTIONFS_ENABLE] = "ENABLE",
607 [FUNCTIONFS_DISABLE] = "DISABLE",
608 [FUNCTIONFS_SETUP] = "SETUP",
609 [FUNCTIONFS_SUSPEND] = "SUSPEND",
610 [FUNCTIONFS_RESUME] = "RESUME",
613 const struct usb_functionfs_event *event = buf;
618 for (n = nbytes / sizeof *event; n; --n, ++event)
619 switch (event->type) {
620 case FUNCTIONFS_BIND:
621 case FUNCTIONFS_UNBIND:
622 case FUNCTIONFS_ENABLE:
623 case FUNCTIONFS_DISABLE:
624 case FUNCTIONFS_SETUP:
625 case FUNCTIONFS_SUSPEND:
626 case FUNCTIONFS_RESUME:
627 printf("Event %s\n", names[event->type]);
628 if (event->type == FUNCTIONFS_SETUP)
629 handle_setup(&event->u.setup);
633 printf("Event %03u (unknown)\n", event->type);
639 static void ep0_init(struct thread *t, bool legacy_descriptors)
645 if (legacy_descriptors) {
646 info("%s: writing descriptors\n", t->filename);
650 info("%s: writing descriptors (in v2 format)\n", t->filename);
651 ret = write(t->fd, &descriptors, sizeof descriptors);
653 if (ret < 0 && errno == EINVAL) {
654 warn("%s: new format rejected, trying legacy\n", t->filename);
656 len = descs_to_legacy(&legacy, &descriptors);
658 ret = write(t->fd, legacy, len);
662 die_on(ret < 0, "%s: write: descriptors", t->filename);
664 info("%s: writing strings\n", t->filename);
665 ret = write(t->fd, &strings, sizeof strings);
666 die_on(ret < 0, "%s: write: strings", t->filename);
670 /******************** Main **************************************************/
672 int main(int argc, char **argv)
674 bool legacy_descriptors;
677 legacy_descriptors = argc > 2 && !strcmp(argv[1], "-l");
679 init_thread(threads);
680 ep0_init(threads, legacy_descriptors);
682 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
683 init_thread(threads + i);
685 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
686 start_thread(threads + i);
688 start_thread_helper(threads);
690 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
691 join_thread(threads + i);