]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/usb/ffs-test.c
tools: ffs-test: convert to new descriptor format
[mirror_ubuntu-bionic-kernel.git] / tools / usb / ffs-test.c
CommitLineData
93f2aa4d 1/*
51c208c7 2 * ffs-test.c -- user mode filesystem api for usb composite function
93f2aa4d
MN
3 *
4 * Copyright (C) 2010 Samsung Electronics
54b8360f 5 * Author: Michal Nazarewicz <mina86@mina86.com>
93f2aa4d
MN
6 *
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.
11 *
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.
16 *
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
20 */
21
22/* $(CROSS_COMPILE)cc -Wall -Wextra -g -o ffs-test ffs-test.c -lpthread */
23
24
25#define _BSD_SOURCE /* for endian.h */
26
27#include <endian.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <pthread.h>
31#include <stdarg.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/ioctl.h>
36#include <sys/stat.h>
37#include <sys/types.h>
38#include <unistd.h>
24fa9a9d 39#include <tools/le_byteshift.h>
93f2aa4d 40
a0f11ace 41#include "../../include/uapi/linux/usb/functionfs.h"
93f2aa4d
MN
42
43
44/******************** Little Endian Handling ********************************/
45
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)
50
93f2aa4d
MN
51
52/******************** Messages and Errors ***********************************/
53
54static const char argv0[] = "ffs-test";
55
56static unsigned verbosity = 7;
57
58static void _msg(unsigned level, const char *fmt, ...)
59{
60 if (level < 2)
61 level = 2;
62 else if (level > 7)
63 level = 7;
64
65 if (level <= verbosity) {
66 static const char levels[8][6] = {
67 [2] = "crit:",
68 [3] = "err: ",
69 [4] = "warn:",
70 [5] = "note:",
71 [6] = "info:",
72 [7] = "dbg: "
73 };
74
75 int _errno = errno;
76 va_list ap;
77
78 fprintf(stderr, "%s: %s ", argv0, levels[level]);
79 va_start(ap, fmt);
80 vfprintf(stderr, fmt, ap);
81 va_end(ap);
82
83 if (fmt[strlen(fmt) - 1] != '\n') {
84 char buffer[128];
85 strerror_r(_errno, buffer, sizeof buffer);
86 fprintf(stderr, ": (-%d) %s\n", _errno, buffer);
87 }
88
89 fflush(stderr);
90 }
91}
92
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__)
99
100#define die_on(cond, ...) do { \
101 if (cond) \
102 die(__VA_ARGS__); \
103 } while (0)
104
105
106/******************** Descriptors and Strings *******************************/
107
108static const struct {
51c208c7
MN
109 struct usb_functionfs_descs_head_v2 header;
110 __le32 fs_count;
111 __le32 hs_count;
93f2aa4d
MN
112 struct {
113 struct usb_interface_descriptor intf;
114 struct usb_endpoint_descriptor_no_audio sink;
115 struct usb_endpoint_descriptor_no_audio source;
116 } __attribute__((packed)) fs_descs, hs_descs;
117} __attribute__((packed)) descriptors = {
118 .header = {
51c208c7
MN
119 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
120 .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC |
121 FUNCTIONFS_HAS_HS_DESC),
93f2aa4d 122 .length = cpu_to_le32(sizeof descriptors),
93f2aa4d 123 },
51c208c7 124 .fs_count = cpu_to_le32(3),
93f2aa4d
MN
125 .fs_descs = {
126 .intf = {
127 .bLength = sizeof descriptors.fs_descs.intf,
128 .bDescriptorType = USB_DT_INTERFACE,
129 .bNumEndpoints = 2,
130 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
131 .iInterface = 1,
132 },
133 .sink = {
134 .bLength = sizeof descriptors.fs_descs.sink,
135 .bDescriptorType = USB_DT_ENDPOINT,
136 .bEndpointAddress = 1 | USB_DIR_IN,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 /* .wMaxPacketSize = autoconfiguration (kernel) */
139 },
140 .source = {
141 .bLength = sizeof descriptors.fs_descs.source,
142 .bDescriptorType = USB_DT_ENDPOINT,
143 .bEndpointAddress = 2 | USB_DIR_OUT,
144 .bmAttributes = USB_ENDPOINT_XFER_BULK,
145 /* .wMaxPacketSize = autoconfiguration (kernel) */
146 },
147 },
51c208c7 148 .hs_count = cpu_to_le32(3),
93f2aa4d
MN
149 .hs_descs = {
150 .intf = {
151 .bLength = sizeof descriptors.fs_descs.intf,
152 .bDescriptorType = USB_DT_INTERFACE,
153 .bNumEndpoints = 2,
154 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
155 .iInterface = 1,
156 },
157 .sink = {
158 .bLength = sizeof descriptors.hs_descs.sink,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bEndpointAddress = 1 | USB_DIR_IN,
161 .bmAttributes = USB_ENDPOINT_XFER_BULK,
162 .wMaxPacketSize = cpu_to_le16(512),
163 },
164 .source = {
165 .bLength = sizeof descriptors.hs_descs.source,
166 .bDescriptorType = USB_DT_ENDPOINT,
167 .bEndpointAddress = 2 | USB_DIR_OUT,
168 .bmAttributes = USB_ENDPOINT_XFER_BULK,
169 .wMaxPacketSize = cpu_to_le16(512),
170 .bInterval = 1, /* NAK every 1 uframe */
171 },
172 },
173};
174
175
176#define STR_INTERFACE_ "Source/Sink"
177
178static const struct {
179 struct usb_functionfs_strings_head header;
180 struct {
181 __le16 code;
182 const char str1[sizeof STR_INTERFACE_];
183 } __attribute__((packed)) lang0;
184} __attribute__((packed)) strings = {
185 .header = {
186 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
187 .length = cpu_to_le32(sizeof strings),
188 .str_count = cpu_to_le32(1),
189 .lang_count = cpu_to_le32(1),
190 },
191 .lang0 = {
192 cpu_to_le16(0x0409), /* en-us */
193 STR_INTERFACE_,
194 },
195};
196
197#define STR_INTERFACE strings.lang0.str1
198
199
200/******************** Files and Threads Handling ****************************/
201
202struct thread;
203
204static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes);
205static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes);
206static ssize_t ep0_consume(struct thread *t, const void *buf, size_t nbytes);
207static ssize_t fill_in_buf(struct thread *t, void *buf, size_t nbytes);
208static ssize_t empty_out_buf(struct thread *t, const void *buf, size_t nbytes);
209
210
211static struct thread {
212 const char *const filename;
213 size_t buf_size;
214
215 ssize_t (*in)(struct thread *, void *, size_t);
216 const char *const in_name;
217
218 ssize_t (*out)(struct thread *, const void *, size_t);
219 const char *const out_name;
220
221 int fd;
222 pthread_t id;
223 void *buf;
224 ssize_t status;
225} threads[] = {
226 {
227 "ep0", 4 * sizeof(struct usb_functionfs_event),
228 read_wrap, NULL,
229 ep0_consume, "<consume>",
230 0, 0, NULL, 0
231 },
232 {
233 "ep1", 8 * 1024,
234 fill_in_buf, "<in>",
235 write_wrap, NULL,
236 0, 0, NULL, 0
237 },
238 {
239 "ep2", 8 * 1024,
240 read_wrap, NULL,
241 empty_out_buf, "<out>",
242 0, 0, NULL, 0
243 },
244};
245
246
247static void init_thread(struct thread *t)
248{
249 t->buf = malloc(t->buf_size);
250 die_on(!t->buf, "malloc");
251
252 t->fd = open(t->filename, O_RDWR);
253 die_on(t->fd < 0, "%s", t->filename);
254}
255
256static void cleanup_thread(void *arg)
257{
258 struct thread *t = arg;
259 int ret, fd;
260
261 fd = t->fd;
262 if (t->fd < 0)
263 return;
264 t->fd = -1;
265
266 /* test the FIFO ioctls (non-ep0 code paths) */
267 if (t != threads) {
268 ret = ioctl(fd, FUNCTIONFS_FIFO_STATUS);
269 if (ret < 0) {
270 /* ENODEV reported after disconnect */
271 if (errno != ENODEV)
272 err("%s: get fifo status", t->filename);
273 } else if (ret) {
274 warn("%s: unclaimed = %d\n", t->filename, ret);
275 if (ioctl(fd, FUNCTIONFS_FIFO_FLUSH) < 0)
276 err("%s: fifo flush", t->filename);
277 }
278 }
279
280 if (close(fd) < 0)
281 err("%s: close", t->filename);
282
283 free(t->buf);
284 t->buf = NULL;
285}
286
287static void *start_thread_helper(void *arg)
288{
289 const char *name, *op, *in_name, *out_name;
290 struct thread *t = arg;
291 ssize_t ret;
292
293 info("%s: starts\n", t->filename);
294 in_name = t->in_name ? t->in_name : t->filename;
295 out_name = t->out_name ? t->out_name : t->filename;
296
297 pthread_cleanup_push(cleanup_thread, arg);
298
299 for (;;) {
300 pthread_testcancel();
301
302 ret = t->in(t, t->buf, t->buf_size);
303 if (ret > 0) {
eb9c5836 304 ret = t->out(t, t->buf, ret);
93f2aa4d
MN
305 name = out_name;
306 op = "write";
307 } else {
308 name = in_name;
309 op = "read";
310 }
311
312 if (ret > 0) {
313 /* nop */
314 } else if (!ret) {
315 debug("%s: %s: EOF", name, op);
316 break;
317 } else if (errno == EINTR || errno == EAGAIN) {
318 debug("%s: %s", name, op);
319 } else {
320 warn("%s: %s", name, op);
321 break;
322 }
323 }
324
325 pthread_cleanup_pop(1);
326
327 t->status = ret;
328 info("%s: ends\n", t->filename);
329 return NULL;
330}
331
332static void start_thread(struct thread *t)
333{
334 debug("%s: starting\n", t->filename);
335
336 die_on(pthread_create(&t->id, NULL, start_thread_helper, t) < 0,
337 "pthread_create(%s)", t->filename);
338}
339
340static void join_thread(struct thread *t)
341{
342 int ret = pthread_join(t->id, NULL);
343
344 if (ret < 0)
345 err("%s: joining thread", t->filename);
346 else
347 debug("%s: joined\n", t->filename);
348}
349
350
351static ssize_t read_wrap(struct thread *t, void *buf, size_t nbytes)
352{
353 return read(t->fd, buf, nbytes);
354}
355
356static ssize_t write_wrap(struct thread *t, const void *buf, size_t nbytes)
357{
358 return write(t->fd, buf, nbytes);
359}
360
361
362/******************** Empty/Fill buffer routines ****************************/
363
364/* 0 -- stream of zeros, 1 -- i % 63, 2 -- pipe */
365enum pattern { PAT_ZERO, PAT_SEQ, PAT_PIPE };
366static enum pattern pattern;
367
368static ssize_t
369fill_in_buf(struct thread *ignore, void *buf, size_t nbytes)
370{
371 size_t i;
372 __u8 *p;
373
374 (void)ignore;
375
376 switch (pattern) {
377 case PAT_ZERO:
378 memset(buf, 0, nbytes);
379 break;
380
381 case PAT_SEQ:
382 for (p = buf, i = 0; i < nbytes; ++i, ++p)
383 *p = i % 63;
384 break;
385
386 case PAT_PIPE:
387 return fread(buf, 1, nbytes, stdin);
388 }
389
390 return nbytes;
391}
392
393static ssize_t
394empty_out_buf(struct thread *ignore, const void *buf, size_t nbytes)
395{
396 const __u8 *p;
397 __u8 expected;
398 ssize_t ret;
399 size_t len;
400
401 (void)ignore;
402
403 switch (pattern) {
404 case PAT_ZERO:
405 expected = 0;
406 for (p = buf, len = 0; len < nbytes; ++p, ++len)
407 if (*p)
408 goto invalid;
409 break;
410
411 case PAT_SEQ:
412 for (p = buf, len = 0; len < nbytes; ++p, ++len)
413 if (*p != len % 63) {
414 expected = len % 63;
415 goto invalid;
416 }
417 break;
418
419 case PAT_PIPE:
420 ret = fwrite(buf, nbytes, 1, stdout);
421 if (ret > 0)
422 fflush(stdout);
423 break;
424
425invalid:
426 err("bad OUT byte %zd, expected %02x got %02x\n",
427 len, expected, *p);
428 for (p = buf, len = 0; len < nbytes; ++p, ++len) {
429 if (0 == (len % 32))
d105e74e 430 fprintf(stderr, "%4zd:", len);
93f2aa4d
MN
431 fprintf(stderr, " %02x", *p);
432 if (31 == (len % 32))
433 fprintf(stderr, "\n");
434 }
435 fflush(stderr);
436 errno = EILSEQ;
437 return -1;
438 }
439
440 return len;
441}
442
443
444/******************** Endpoints routines ************************************/
445
446static void handle_setup(const struct usb_ctrlrequest *setup)
447{
448 printf("bRequestType = %d\n", setup->bRequestType);
449 printf("bRequest = %d\n", setup->bRequest);
450 printf("wValue = %d\n", le16_to_cpu(setup->wValue));
451 printf("wIndex = %d\n", le16_to_cpu(setup->wIndex));
452 printf("wLength = %d\n", le16_to_cpu(setup->wLength));
453}
454
455static ssize_t
456ep0_consume(struct thread *ignore, const void *buf, size_t nbytes)
457{
458 static const char *const names[] = {
459 [FUNCTIONFS_BIND] = "BIND",
460 [FUNCTIONFS_UNBIND] = "UNBIND",
461 [FUNCTIONFS_ENABLE] = "ENABLE",
462 [FUNCTIONFS_DISABLE] = "DISABLE",
463 [FUNCTIONFS_SETUP] = "SETUP",
464 [FUNCTIONFS_SUSPEND] = "SUSPEND",
465 [FUNCTIONFS_RESUME] = "RESUME",
466 };
467
468 const struct usb_functionfs_event *event = buf;
469 size_t n;
470
471 (void)ignore;
472
473 for (n = nbytes / sizeof *event; n; --n, ++event)
474 switch (event->type) {
475 case FUNCTIONFS_BIND:
476 case FUNCTIONFS_UNBIND:
477 case FUNCTIONFS_ENABLE:
478 case FUNCTIONFS_DISABLE:
479 case FUNCTIONFS_SETUP:
480 case FUNCTIONFS_SUSPEND:
481 case FUNCTIONFS_RESUME:
482 printf("Event %s\n", names[event->type]);
483 if (event->type == FUNCTIONFS_SETUP)
484 handle_setup(&event->u.setup);
485 break;
486
487 default:
488 printf("Event %03u (unknown)\n", event->type);
489 }
490
491 return nbytes;
492}
493
494static void ep0_init(struct thread *t)
495{
496 ssize_t ret;
497
498 info("%s: writing descriptors\n", t->filename);
499 ret = write(t->fd, &descriptors, sizeof descriptors);
500 die_on(ret < 0, "%s: write: descriptors", t->filename);
501
502 info("%s: writing strings\n", t->filename);
503 ret = write(t->fd, &strings, sizeof strings);
504 die_on(ret < 0, "%s: write: strings", t->filename);
505}
506
507
508/******************** Main **************************************************/
509
510int main(void)
511{
512 unsigned i;
513
514 /* XXX TODO: Argument parsing missing */
515
516 init_thread(threads);
517 ep0_init(threads);
518
519 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
520 init_thread(threads + i);
521
522 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
523 start_thread(threads + i);
524
525 start_thread_helper(threads);
526
527 for (i = 1; i < sizeof threads / sizeof *threads; ++i)
528 join_thread(threads + i);
529
530 return 0;
531}