35 #include <sys/ioctl.h>
37 #include <sys/types.h>
41 #include "../../include/linux/usb/functionfs.h"
46 #define cpu_to_le16(x) htole16(x)
47 #define cpu_to_le32(x) htole32(x)
48 #define le32_to_cpu(x) le32toh(x)
49 #define le16_to_cpu(x) le16toh(x)
54 static const char argv0[] =
"ffs-test";
56 static unsigned verbosity = 7;
58 static void _msg(
unsigned level,
const char *
fmt, ...)
65 if (level <= verbosity) {
66 static const char levels[8][6] = {
78 fprintf(stderr,
"%s: %s ", argv0, levels[level]);
83 if (fmt[
strlen(fmt) - 1] !=
'\n') {
85 strerror_r(_errno, buffer,
sizeof buffer);
86 fprintf(stderr,
": (-%d) %s\n", _errno, buffer);
93 #define die(...) (_msg(2, __VA_ARGS__), exit(1))
94 #define err(...) _msg(3, __VA_ARGS__)
95 #define warn(...) _msg(4, __VA_ARGS__)
96 #define note(...) _msg(5, __VA_ARGS__)
97 #define info(...) _msg(6, __VA_ARGS__)
98 #define debug(...) _msg(7, __VA_ARGS__)
100 #define die_on(cond, ...) do { \
108 static const struct {
124 .bLength =
sizeof descriptors.fs_descs.intf,
131 .bLength =
sizeof descriptors.fs_descs.sink,
138 .bLength =
sizeof descriptors.fs_descs.source,
147 .bLength =
sizeof descriptors.fs_descs.intf,
154 .bLength =
sizeof descriptors.hs_descs.sink,
161 .bLength =
sizeof descriptors.hs_descs.source,
172 #define STR_INTERFACE_ "Source/Sink"
174 static const struct {
193 #define STR_INTERFACE strings.lang0.str1
225 ep0_consume, "<consume>",
237 empty_out_buf,
"<out>",
243 static void init_thread(
struct thread *
t)
252 static void cleanup_thread(
void *
arg)
283 static void *start_thread_helper(
void *arg)
293 pthread_cleanup_push(cleanup_thread, arg);
296 pthread_testcancel();
300 ret = t->
out(t, t->
buf, ret);
311 debug(
"%s: %s: EOF", name, op);
314 debug(
"%s: %s", name, op);
316 warn(
"%s: %s", name, op);
321 pthread_cleanup_pop(1);
332 die_on(pthread_create(&t->
id,
NULL, start_thread_helper, t) < 0,
336 static void join_thread(
struct thread *t)
338 int ret = pthread_join(t->
id,
NULL);
349 return read(t->
fd, buf, nbytes);
354 return write(t->
fd, buf, nbytes);
378 for (p = buf, i = 0; i <
nbytes; ++
i, ++
p)
383 return fread(buf, 1, nbytes, stdin);
390 empty_out_buf(
struct thread *ignore,
const void *buf,
size_t nbytes)
402 for (p = buf, len = 0; len <
nbytes; ++
p, ++len)
408 for (p = buf, len = 0; len <
nbytes; ++
p, ++len)
409 if (*p != len % 63) {
416 ret = fwrite(buf, nbytes, 1, stdout);
422 err(
"bad OUT byte %zd, expected %02x got %02x\n",
424 for (p = buf, len = 0; len <
nbytes; ++
p, ++len) {
428 if (31 == (len % 32))
452 ep0_consume(
struct thread *ignore,
const void *buf,
size_t nbytes)
454 static const char *
const names[] = {
470 switch (
event->type) {
480 handle_setup(&
event->u.setup);
490 static void ep0_init(
struct thread *t)
499 ret =
write(t->
fd, &strings,
sizeof strings);