2 * ffs-test.c -- user mode filesystem api for usb composite function
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
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 _BSD_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;
127 struct usb_interface_descriptor intf;
128 struct usb_endpoint_descriptor_no_audio sink;
129 struct usb_endpoint_descriptor_no_audio source;
130 } __attribute__((packed)) fs_descs, hs_descs;
131 } __attribute__((packed)) descriptors = {
133 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
134 .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
135 FUNCTIONFS_HAS_HS_DESC),
136 .length = cpu_to_le32(sizeof descriptors),
138 .fs_count = cpu_to_le32(3),
141 .bLength = sizeof descriptors.fs_descs.intf,
142 .bDescriptorType = USB_DT_INTERFACE,
144 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
148 .bLength = sizeof descriptors.fs_descs.sink,
149 .bDescriptorType = USB_DT_ENDPOINT,
150 .bEndpointAddress = 1 | USB_DIR_IN,
151 .bmAttributes = USB_ENDPOINT_XFER_BULK,
152 /* .wMaxPacketSize = autoconfiguration (kernel) */
155 .bLength = sizeof descriptors.fs_descs.source,
156 .bDescriptorType = USB_DT_ENDPOINT,
157 .bEndpointAddress = 2 | USB_DIR_OUT,
158 .bmAttributes = USB_ENDPOINT_XFER_BULK,
159 /* .wMaxPacketSize = autoconfiguration (kernel) */
162 .hs_count = cpu_to_le32(3),
165 .bLength = sizeof descriptors.fs_descs.intf,
166 .bDescriptorType = USB_DT_INTERFACE,
168 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
172 .bLength = sizeof descriptors.hs_descs.sink,
173 .bDescriptorType = USB_DT_ENDPOINT,
174 .bEndpointAddress = 1 | USB_DIR_IN,
175 .bmAttributes = USB_ENDPOINT_XFER_BULK,
176 .wMaxPacketSize = cpu_to_le16(512),
179 .bLength = sizeof descriptors.hs_descs.source,
180 .bDescriptorType = USB_DT_ENDPOINT,
181 .bEndpointAddress = 2 | USB_DIR_OUT,
182 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 .wMaxPacketSize = cpu_to_le16(512),
184 .bInterval = 1, /* NAK every 1 uframe */
189 static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
191 const unsigned char *descs_end, *descs_start;
192 __u32 length, fs_count = 0, hs_count = 0, count;
197 const struct usb_functionfs_descs_head_v2 header;
198 const __le32 counts[];
199 } __attribute__((packed)) *const in = descriptors_v2;
200 const __le32 *counts = in->counts;
203 if (le32_to_cpu(in->header.magic) !=
204 FUNCTIONFS_DESCRIPTORS_MAGIC_V2)
206 length = le32_to_cpu(in->header.length);
207 if (length <= sizeof in->header)
209 length -= sizeof in->header;
210 flags = le32_to_cpu(in->header.flags);
211 if (flags & ~(FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC |
212 FUNCTIONFS_HAS_SS_DESC))
215 #define GET_NEXT_COUNT_IF_FLAG(ret, flg) do { \
216 if (!(flags & (flg))) \
220 ret = le32_to_cpu(*counts); \
225 GET_NEXT_COUNT_IF_FLAG(fs_count, FUNCTIONFS_HAS_FS_DESC);
226 GET_NEXT_COUNT_IF_FLAG(hs_count, FUNCTIONFS_HAS_HS_DESC);
227 GET_NEXT_COUNT_IF_FLAG(count, FUNCTIONFS_HAS_SS_DESC);
229 count = fs_count + hs_count;
232 descs_start = (const void *)counts;
234 #undef GET_NEXT_COUNT_IF_FLAG
238 * Find the end of FS and HS USB descriptors. SS descriptors
239 * are ignored since legacy format does not support them.
241 descs_end = descs_start;
243 if (length < *descs_end)
245 length -= *descs_end;
246 descs_end += *descs_end;
249 /* Allocate legacy descriptors and copy the data. */
251 #pragma GCC diagnostic push
252 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
254 struct usb_functionfs_descs_head header;
256 } __attribute__((packed)) *out;
257 #pragma GCC diagnostic pop
259 length = sizeof out->header + (descs_end - descs_start);
260 out = malloc(length);
261 out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
262 out->header.length = cpu_to_le32(length);
263 out->header.fs_count = cpu_to_le32(fs_count);
264 out->header.hs_count = cpu_to_le32(hs_count);
265 memcpy(out->descriptors, descs_start, descs_end - descs_start);
273 #define STR_INTERFACE_ "Source/Sink"
275 static const struct {
276 struct usb_functionfs_strings_head header;
279 const char str1[sizeof STR_INTERFACE_];
280 } __attribute__((packed)) lang0;
281 } __attribute__((packed)) strings = {
283 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
284 .length = cpu_to_le32(sizeof strings),
285 .str_count = cpu_to_le32(1),
286 .lang_count = cpu_to_le32(1),
289 cpu_to_le16(0x0409), /* en-us */
294 #define STR_INTERFACE strings.lang0.str1
297 /******************** Files and Threads Handling ****************************/
301 static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
302 static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
303 static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
304 static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
305 static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
308 static struct thread {
309 const char *const filename;
312 ssize_t (*in)(struct thread *, void *, size_t);
313 const char *const in_name;
315 ssize_t (*out)(struct thread *, const void *, size_t);
316 const char *const out_name;
324 "ep0", 4 * sizeof(struct usb_functionfs_event),
326 ep0_consume, "<consume>",
338 empty_out_buf, "<out>",
344 static void init_thread(struct thread *t)
346 t->buf = malloc(t->buf_size);
347 die_on(!t->buf, "malloc");
349 t->fd = open(t->filename, O_RDWR);
350 die_on(t->fd < 0, "%s", t->filename);
353 static void cleanup_thread(void *arg)
355 struct thread *t = arg;
363 /* test the FIFO ioctls (non-ep0 code paths) */
365 ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
367 /* ENODEV reported after disconnect */
369 err("%s: get fifo status", t->filename);
371 warn("%s: unclaimed = %d\n", t->filename, ret);
372 if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
373 err("%s: fifo flush", t->filename);
378 err("%s: close", t->filename);
384 static void *start_thread_helper(void *arg)
386 const char *name, *op, *in_name, *out_name;
387 struct thread *t = arg;
390 info("%s: starts\n", t->filename);
391 in_name = t->in_name ? t->in_name : t->filename;
392 out_name = t->out_name ? t->out_name : t->filename;
394 pthread_cleanup_push(cleanup_thread, arg);
397 pthread_testcancel();
399 ret = t->in(t, t->buf, t->buf_size);
401 ret = t->out(t, t->buf, ret);
412 debug("%s: %s: EOF", name, op);
414 } else if (errno == EINTR || errno == EAGAIN) {
415 debug("%s: %s", name, op);
417 warn("%s: %s", name, op);
422 pthread_cleanup_pop(1);
425 info("%s: ends\n", t->filename);
429 static void start_thread(struct thread *t)
431 debug("%s: starting\n", t->filename);
433 die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
434 "pthread_create(%s)", t->filename);
437 static void join_thread(struct thread *t)
439 int ret = pthread_join(t->id, NULL);
442 err("%s: joining thread", t->filename);
444 debug("%s: joined\n", t->filename);
448 static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
450 return read(t->fd, buf, nbytes);
453 static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
455 return write(t->fd, buf, nbytes);
459 /******************** Empty/Fill buffer routines ****************************/
461 /* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
462 enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
463 static enum pattern pattern;
466 fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
475 memset(buf, 0, nbytes);
479 for (p = buf, i = 0; i < nbytes; ++i, ++p)
484 return fread(buf, 1, nbytes, stdin);
491 empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
503 for (p = buf, len = 0; len < nbytes; ++p, ++len)
509 for (p = buf, len = 0; len < nbytes; ++p, ++len)
510 if (*p != len % 63) {
517 ret = fwrite(buf, nbytes, 1, stdout);
523 err("bad OUT byte %zd, expected %02x got %02x\n",
525 for (p = buf, len = 0; len < nbytes; ++p, ++len) {
527 fprintf(stderr, "%4zd:", len);
528 fprintf(stderr, " %02x", *p);
529 if (31 == (len % 32))
530 fprintf(stderr, "\n");
541 /******************** Endpoints routines ************************************/
543 static void handle_setup(const struct usb_ctrlrequest *setup)
545 printf("bRequestType = %d\n", setup->bRequestType);
546 printf("bRequest = %d\n", setup->bRequest);
547 printf("wValue = %d\n", le16_to_cpu(setup->wValue));
548 printf("wIndex = %d\n", le16_to_cpu(setup->wIndex));
549 printf("wLength = %d\n", le16_to_cpu(setup->wLength));
553 ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
555 static const char *const names[] = {
556 [FUNCTIONFS_BIND] = "BIND",
557 [FUNCTIONFS_UNBIND] = "UNBIND",
558 [FUNCTIONFS_ENABLE] = "ENABLE",
559 [FUNCTIONFS_DISABLE] = "DISABLE",
560 [FUNCTIONFS_SETUP] = "SETUP",
561 [FUNCTIONFS_SUSPEND] = "SUSPEND",
562 [FUNCTIONFS_RESUME] = "RESUME",
565 const struct usb_functionfs_event *event = buf;
570 for (n = nbytes / sizeof *event; n; --n, ++event)
571 switch (event->type) {
572 case FUNCTIONFS_BIND:
573 case FUNCTIONFS_UNBIND:
574 case FUNCTIONFS_ENABLE:
575 case FUNCTIONFS_DISABLE:
576 case FUNCTIONFS_SETUP:
577 case FUNCTIONFS_SUSPEND:
578 case FUNCTIONFS_RESUME:
579 printf("Event %s\n", names[event->type]);
580 if (event->type == FUNCTIONFS_SETUP)
581 handle_setup(&event->u.setup);
585 printf("Event %03u (unknown)\n", event->type);
591 static void ep0_init(struct thread *t, bool legacy_descriptors)
597 if (legacy_descriptors) {
598 info("%s: writing descriptors\n", t->filename);
602 info("%s: writing descriptors (in v2 format)\n", t->filename);
603 ret = write(t->fd, &descriptors, sizeof descriptors);
605 if (ret < 0 && errno == EINVAL) {
606 warn("%s: new format rejected, trying legacy\n", t->filename);
608 len = descs_to_legacy(&legacy, &descriptors);
610 ret = write(t->fd, legacy, len);
614 die_on(ret < 0, "%s: write: descriptors", t->filename);
616 info("%s: writing strings\n", t->filename);
617 ret = write(t->fd, &strings, sizeof strings);
618 die_on(ret < 0, "%s: write: strings", t->filename);
622 /******************** Main **************************************************/
624 int main(int argc, char **argv)
626 bool legacy_descriptors;
629 legacy_descriptors = argc > 2 && !strcmp(argv[1], "-l");
631 init_thread(threads);
632 ep0_init(threads, legacy_descriptors);
634 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
635 init_thread(threads + i);
637 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
638 start_thread(threads + i);
640 start_thread_helper(threads);
642 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
643 join_thread(threads + i);