-
Notifications
You must be signed in to change notification settings - Fork 303
/
usbd.c
410 lines (333 loc) · 9.25 KB
/
usbd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2016 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*/
#include "../debug.h"
#include "ops.h"
#include "thread-pool.h"
#include <fcntl.h>
#include <linux/usb/functionfs.h>
#include <stdint.h>
#include <string.h>
/* u8"IIO" for non-c11 compilers */
#define NAME "\x0049\x0049\x004F"
#define LE32(x) ((__BYTE_ORDER != __BIG_ENDIAN) ? (x) : __bswap_constant_32(x))
#define LE16(x) ((__BYTE_ORDER != __BIG_ENDIAN) ? (x) : __bswap_constant_16(x))
#define IIO_USD_CMD_RESET_PIPES 0
#define IIO_USD_CMD_OPEN_PIPE 1
#define IIO_USD_CMD_CLOSE_PIPE 2
struct usb_ffs_header {
struct usb_functionfs_descs_head_v2 header;
uint32_t nb_fs, nb_hs, nb_ss;
} __attribute__((packed));
struct usb_ffs_strings {
struct usb_functionfs_strings_head head;
uint16_t lang;
const char string[sizeof(NAME)];
} __attribute__((packed));
struct usbd_pdata {
struct iio_context *ctx;
char *ffs;
int ep0_fd;
bool debug, use_aio;
struct thread_pool **pool;
unsigned int nb_pipes;
const void *xml_zstd;
size_t xml_zstd_len;
};
struct usbd_client_pdata {
struct usbd_pdata *pdata;
int ep_in, ep_out;
};
static const struct usb_ffs_strings ffs_strings = {
.head = {
.magic = LE32(FUNCTIONFS_STRINGS_MAGIC),
.length = LE32(sizeof(ffs_strings)),
.str_count = LE32(1),
.lang_count = LE32(1),
},
.lang = LE16(0x409),
.string = NAME,
};
static void usbd_client_thread(struct thread_pool *pool, void *d)
{
struct usbd_client_pdata *pdata = d;
interpreter(pdata->pdata->ctx, pdata->ep_in, pdata->ep_out,
pdata->pdata->debug, false, true,
pdata->pdata->use_aio, pool,
pdata->pdata->xml_zstd, pdata->pdata->xml_zstd_len);
close(pdata->ep_in);
close(pdata->ep_out);
free(pdata);
}
static int usb_open_pipe(struct usbd_pdata *pdata, unsigned int pipe_id)
{
struct usbd_client_pdata *cpdata;
char buf[256];
int err;
if (pipe_id >= pdata->nb_pipes)
return -EINVAL;
cpdata = malloc(sizeof(*cpdata));
if (!cpdata)
return -ENOMEM;
/* Either we open this pipe for the first time, or it was closed before.
* In that case we called thread_pool_stop() without waiting for all the
* threads to finish. We do that here. Since the running thread might still
* have a open handle to the endpoints make sure that they have exited
* before opening the endpoints again. */
thread_pool_stop_and_wait(pdata->pool[pipe_id]);
snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 1);
cpdata->ep_out = open(buf, O_WRONLY);
if (cpdata->ep_out < 0) {
err = -errno;
goto err_free_cpdata;
}
snprintf(buf, sizeof(buf), "%s/ep%u", pdata->ffs, pipe_id * 2 + 2);
cpdata->ep_in = open(buf, O_RDONLY);
if (cpdata->ep_in < 0) {
err = -errno;
goto err_close_ep_out;
}
cpdata->pdata = pdata;
err = thread_pool_add_thread(pdata->pool[pipe_id],
usbd_client_thread, cpdata, "usbd_client_thd");
if (!err)
return 0;
close(cpdata->ep_in);
err_close_ep_out:
close(cpdata->ep_out);
err_free_cpdata:
free(cpdata);
return err;
}
static int usb_close_pipe(struct usbd_pdata *pdata, unsigned int pipe_id)
{
if (pipe_id >= pdata->nb_pipes)
return -EINVAL;
thread_pool_stop(pdata->pool[pipe_id]);
return 0;
}
static void usb_close_pipes(struct usbd_pdata *pdata)
{
unsigned int i;
for (i = 0; i < pdata->nb_pipes; i++)
usb_close_pipe(pdata, i);
}
static int handle_event(struct usbd_pdata *pdata,
const struct usb_functionfs_event *event)
{
int ret = 0;
if (event->type == FUNCTIONFS_SETUP) {
const struct usb_ctrlrequest *req = &event->u.setup;
switch (req->bRequest) {
case IIO_USD_CMD_RESET_PIPES:
usb_close_pipes(pdata);
break;
case IIO_USD_CMD_OPEN_PIPE:
ret = usb_open_pipe(pdata, le16toh(req->wValue));
break;
case IIO_USD_CMD_CLOSE_PIPE:
ret = usb_close_pipe(pdata, le16toh(req->wValue));
break;
}
}
return ret;
}
static void usbd_main(struct thread_pool *pool, void *d)
{
int stop_fd = thread_pool_get_poll_fd(pool);
struct usbd_pdata *pdata = d;
unsigned int i;
for (;;) {
struct usb_functionfs_event event;
struct pollfd pfd[2];
int ret;
pfd[0].fd = pdata->ep0_fd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
pfd[1].fd = stop_fd;
pfd[1].events = POLLIN;
pfd[1].revents = 0;
poll_nointr(pfd, 2);
if (pfd[1].revents & POLLIN) /* STOP event */
break;
if (!(pfd[0].revents & POLLIN)) /* Should never happen. */
continue;
ret = read(pdata->ep0_fd, &event, sizeof(event));
if (ret != sizeof(event)) {
IIO_WARNING("Short read!\n");
continue;
}
ret = handle_event(pdata, &event);
if (ret) {
IIO_ERROR("Unable to handle event: %i\n", ret);
break;
}
/* Clear out the errors on ep0 when we close endpoints */
ret = read(pdata->ep0_fd, NULL, 0);
}
for (i = 0; i < pdata->nb_pipes; i++) {
thread_pool_stop_and_wait(pdata->pool[i]);
thread_pool_destroy(pdata->pool[i]);
}
free(pdata->ffs);
free(pdata->pool);
free(pdata);
}
static struct usb_ffs_header * create_header(
unsigned int nb_pipes, uint32_t size)
{
/* Packet sizes for USB high-speed, full-speed, super-speed */
const unsigned int packet_sizes[3] = { 64, 512, 1024, };
struct usb_ffs_header *hdr;
unsigned int i, pipe_id;
uintptr_t ptr;
hdr = zalloc(size);
if (!hdr) {
errno = ENOMEM;
return NULL;
}
hdr->header.magic = LE32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
hdr->header.length = htole32(size);
hdr->header.flags = LE32(FUNCTIONFS_HAS_FS_DESC |
FUNCTIONFS_HAS_HS_DESC |
FUNCTIONFS_HAS_SS_DESC);
hdr->nb_fs = htole32(nb_pipes * 2 + 1);
hdr->nb_hs = htole32(nb_pipes * 2 + 1);
hdr->nb_ss = htole32(nb_pipes * 4 + 1);
ptr = ((uintptr_t) hdr) + sizeof(*hdr);
for (i = 0; i < 3; i++) {
struct usb_interface_descriptor *desc =
(struct usb_interface_descriptor *) ptr;
struct usb_endpoint_descriptor_no_audio *ep;
struct usb_ss_ep_comp_descriptor *comp;
desc->bLength = sizeof(*desc);
desc->bDescriptorType = USB_DT_INTERFACE;
desc->bNumEndpoints = nb_pipes * 2;
desc->bInterfaceClass = USB_CLASS_COMM;
desc->iInterface = 1;
ep = (struct usb_endpoint_descriptor_no_audio *)
(ptr + sizeof(*desc));
for (pipe_id = 0; pipe_id < nb_pipes; pipe_id++) {
ep->bLength = sizeof(*ep);
ep->bDescriptorType = USB_DT_ENDPOINT;
ep->bEndpointAddress = (pipe_id + 1) | USB_DIR_IN;
ep->bmAttributes = USB_ENDPOINT_XFER_BULK;
ep->wMaxPacketSize = htole16(packet_sizes[i]);
ep++;
if (i == 2) {
comp = (struct usb_ss_ep_comp_descriptor *) ep;
comp->bLength = USB_DT_SS_EP_COMP_SIZE;
comp->bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
comp++;
ep = (struct usb_endpoint_descriptor_no_audio *) comp;
}
ep->bLength = sizeof(*ep);
ep->bDescriptorType = USB_DT_ENDPOINT;
ep->bEndpointAddress = (pipe_id + 1) | USB_DIR_OUT;
ep->bmAttributes = USB_ENDPOINT_XFER_BULK;
ep->wMaxPacketSize = htole16(packet_sizes[i]);
ep++;
if (i == 2) {
comp = (struct usb_ss_ep_comp_descriptor *) ep;
comp->bLength = USB_DT_SS_EP_COMP_SIZE;
comp->bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
comp++;
ep = (struct usb_endpoint_descriptor_no_audio *) comp;
}
}
ptr += sizeof(*desc) + nb_pipes * 2 * sizeof(*ep);
}
return hdr;
}
static int write_header(int fd, unsigned int nb_pipes)
{
uint32_t size = sizeof(struct usb_ffs_header) +
3 * sizeof(struct usb_interface_descriptor) +
6 * nb_pipes * sizeof(struct usb_endpoint_descriptor_no_audio) +
2 * nb_pipes * sizeof(struct usb_ss_ep_comp_descriptor);
struct usb_ffs_header *hdr;
int ret;
hdr = create_header(nb_pipes, size);
if (!hdr)
return -errno;
ret = write(fd, hdr, size);
free(hdr);
if (ret < 0)
return -errno;
ret = write(fd, &ffs_strings, sizeof(ffs_strings));
if (ret < 0)
return -errno;
return 0;
}
int init_usb_daemon(const char *ffs, unsigned int nb_pipes)
{
char buf[256];
int ep0_fd, ret;
snprintf(buf, sizeof(buf), "%s/ep0", ffs);
ep0_fd = open(buf, O_RDWR);
if (ep0_fd < 0) {
return -errno;
}
ret = write_header(ep0_fd, nb_pipes);
if (ret < 0) {
close(ep0_fd);
return ret;
}
return ep0_fd;
}
int start_usb_daemon(struct iio_context *ctx, const char *ffs,
bool debug, bool use_aio, unsigned int nb_pipes,
int ep0_fd, struct thread_pool *pool,
const void *xml_zstd, size_t xml_zstd_len)
{
struct usbd_pdata *pdata;
unsigned int i;
int ret;
pdata = zalloc(sizeof(*pdata));
if (!pdata)
return -ENOMEM;
pdata->nb_pipes = nb_pipes;
pdata->pool = calloc(nb_pipes, sizeof(*pdata->pool));
if (!pdata->pool) {
ret = -ENOMEM;
goto err_free_pdata;
}
pdata->ffs = strdup(ffs);
if (!pdata->ffs) {
ret = -ENOMEM;
goto err_free_pdata_pool;
}
for (i = 0; i < nb_pipes; i++) {
pdata->pool[i] = thread_pool_new();
if (!pdata->pool[i]) {
ret = -errno;
goto err_free_pools;
}
}
pdata->ctx = ctx;
pdata->debug = debug;
pdata->use_aio = use_aio;
pdata->xml_zstd = xml_zstd;
pdata->xml_zstd_len = xml_zstd_len;
pdata->ep0_fd = ep0_fd;
ret = thread_pool_add_thread(pool, usbd_main, pdata, "usbd_main_thd");
if (!ret)
return 0;
err_free_pools:
/* If we get here, usbd_main was not started, so the pools can be
* destroyed directly */
for (i = 0; i < nb_pipes; i++) {
if (pdata->pool[i])
thread_pool_destroy(pdata->pool[i]);
}
free(pdata->ffs);
err_free_pdata_pool:
free(pdata->pool);
err_free_pdata:
free(pdata);
return ret;
}