forked from projectara/gbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionfs.c
379 lines (330 loc) · 7.84 KB
/
functionfs.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
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/usb/functionfs.h>
#include "gbsim.h"
#include "config.h"
#define FFS_PREFIX "/dev/ffs-gbsim/"
#define FFS_GBEMU_EP0 FFS_PREFIX"ep0"
#define FFS_GBEMU_SVC FFS_PREFIX"ep1"
#define FFS_GBEMU_IN FFS_PREFIX"ep2"
#define FFS_GBEMU_OUT FFS_PREFIX"ep3"
#define STR_INTERFACE "gbsim"
#define NEVENT 5
int control = -ENXIO;
int to_ap = -ENXIO;
int from_ap = -ENXIO;
static pthread_t recv_pthread;
#define GBSIM_LEGACY_DESCRIPTORS
/*
* Descriptors:
*
* EP0 [control] - Ch9 and SVC inbound messages
* EP1 [interrupt in] - SVC outbound events/messages
* EP2 [bulk in] - CPort outbound messages
* EP3 [bulk out] - CPort inbound messages
*/
static const struct {
struct {
__le32 magic;
__le32 length;
#ifndef GBSIM_LEGACY_DESCRIPTORS
__le32 flags;
#endif
__le32 fs_count;
__le32 hs_count;
} __attribute__((packed)) header;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio svc_in;
struct usb_endpoint_descriptor_no_audio to_ap;
struct usb_endpoint_descriptor_no_audio from_ap;
} __attribute__((packed)) fs_descs, hs_descs;
} __attribute__((packed)) descriptors = {
.header = {
#ifdef GBSIM_LEGACY_DESCRIPTORS
.magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC),
#else
.magic = htole32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
.flags = htole32(FUNCTIONFS_HAS_FS_DESC |
FUNCTIONFS_HAS_HS_DESC),
#endif
.length = htole32(sizeof descriptors),
.fs_count = htole32(4),
.hs_count = htole32(4),
},
.fs_descs = {
.intf = {
.bLength = sizeof descriptors.fs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.iInterface = 1,
},
.svc_in = {
.bLength = sizeof descriptors.fs_descs.svc_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.bInterval = 10,
.wMaxPacketSize = 64
},
.to_ap = {
.bLength = sizeof descriptors.fs_descs.to_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 64
},
.from_ap = {
.bLength = sizeof descriptors.fs_descs.from_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 64
},
},
.hs_descs = {
.intf = {
.bLength = sizeof descriptors.hs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.iInterface = 1,
},
.svc_in = {
.bLength = sizeof descriptors.hs_descs.svc_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.bInterval = 10,
.wMaxPacketSize = 512,
},
.to_ap = {
.bLength = sizeof descriptors.hs_descs.to_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
},
.from_ap = {
.bLength = sizeof descriptors.hs_descs.from_ap,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
},
},
};
static const struct {
struct usb_functionfs_strings_head header;
struct {
__le16 code;
const char str1[sizeof STR_INTERFACE];
} __attribute__((packed)) lang0;
} __attribute__((packed)) strings = {
.header = {
.magic = htole32(FUNCTIONFS_STRINGS_MAGIC),
.length = htole32(sizeof strings),
.str_count = htole32(1),
.lang_count = htole32(1),
},
.lang0 = {
htole16(0x0409), /* en-us */
STR_INTERFACE,
},
};
/*
* Endpoint handling
*/
void cleanup_endpoint(int ep_fd, char *ep_name)
{
int ret;
if (ep_fd < 0)
return;
ret = ioctl(ep_fd, FUNCTIONFS_FIFO_STATUS);
if (ret < 0) {
/* ENODEV reported after disconnect */
if(errno != ENODEV)
gbsim_error("get fifo status(%s): %s \n", ep_name, strerror(errno));
} else if(ret) {
gbsim_error("%s: unclaimed = %d \n", ep_name, ret);
if(ioctl(ep_fd, FUNCTIONFS_FIFO_FLUSH) < 0)
gbsim_error("%s: fifo flush \n", ep_name);
}
if(close(ep_fd) < 0)
gbsim_error("%s: close \n", ep_name);
}
static int enable_endpoints(void)
{
int ret;
/* Start SVC/CPort endpoints here */
gbsim_debug("Start SVC/CPort endpoints\n");
to_ap = open(FFS_GBEMU_IN, O_RDWR);
if (to_ap < 0)
return to_ap;
from_ap = open(FFS_GBEMU_OUT, O_RDWR);
if (from_ap < 0)
return from_ap;
ret = pthread_create(&recv_pthread, NULL, recv_thread, NULL);
if (ret < 0) {
perror("can't create cport thread");
return ret;
}
/*
* Start communication with the AP in following sequence:
* - Send a svc protocol version request
* - For a valid response, send the 'hello' message.
*/
ret = svc_request_send(GB_SVC_TYPE_PROTOCOL_VERSION, AP_INTF_ID);
if (ret) {
gbsim_error("Failed to send svc version request (%d)\n", ret);
return ret;
}
return 0;
}
static void disable_endpoints(void)
{
gbsim_debug("Disable SVC/CPort endpoints\n");
if (to_ap < 0 || from_ap < 0)
return;
pthread_cancel(recv_pthread);
pthread_join(recv_pthread, NULL);
close(from_ap);
from_ap = -EINVAL;
close(to_ap);
to_ap = -EINVAL;
}
static int read_control(void)
{
struct usb_functionfs_event event[NEVENT];
int i, nevent, ret;
static const char *const names[] = {
[FUNCTIONFS_BIND] = "BIND",
[FUNCTIONFS_UNBIND] = "UNBIND",
[FUNCTIONFS_ENABLE] = "ENABLE",
[FUNCTIONFS_DISABLE] = "DISABLE",
[FUNCTIONFS_SETUP] = "SETUP",
[FUNCTIONFS_SUSPEND] = "SUSPEND",
[FUNCTIONFS_RESUME] = "RESUME",
};
ret = read(control, &event, sizeof(event));
if (ret < 0) {
if (errno == EAGAIN) {
sleep(1);
return ret;
}
perror("ep0 read after poll");
return ret;
}
nevent = ret/ sizeof event[0];
for (i = 0; i < nevent; i++) {
gbsim_debug("USB %s\n", names[event->type]);
switch (event[i].type) {
case FUNCTIONFS_BIND:
break;
case FUNCTIONFS_UNBIND:
break;
case FUNCTIONFS_ENABLE:
enable_endpoints();
break;
case FUNCTIONFS_DISABLE:
disable_endpoints();
break;
case FUNCTIONFS_SETUP:
break;
case FUNCTIONFS_SUSPEND:
break;
case FUNCTIONFS_RESUME:
break;
default:
gbsim_error("unknown event %d\n", event[i].type);
}
}
return ret;
}
static void functionfs_init_gb(void)
{
int ret;
control = open(FFS_GBEMU_EP0, O_RDWR);
if (control < 0) {
perror(FFS_GBEMU_EP0);
control = -errno;
return;
}
ret = write(control, &descriptors, sizeof(descriptors));
if (ret < 0) {
perror("write dev descriptors");
close(control);
control = -errno;
return;
}
ret = write(control, &strings, sizeof(strings));
if (ret < 0) {
perror("write dev strings");
close(control);
control = -errno;
return;
}
return;
}
int functionfs_loop(void)
{
struct pollfd ep_poll[1];
int ret;
do {
/* Always listen on control */
ep_poll[0].fd = control;
ep_poll[0].events = POLLIN | POLLHUP;
ret = poll(ep_poll, 1, -1);
if (ret < 0) {
perror("poll");
break;
}
/* TODO: What to do with HUP? */
if (ep_poll[0].revents & POLLIN) {
ret = read_control();
if (ret < 0) {
if (errno == EAGAIN)
continue;
goto done;
}
}
} while (1);
return 0;
done:
return ret;
}
int functionfs_init(void)
{
/* Mount functionfs */
mkdir(FFS_PREFIX, S_IRWXU|S_IRWXG|S_IRWXO);
mount("gbsim", FFS_PREFIX, "functionfs", 0, NULL);
/* Configure the Greybus emulator */
functionfs_init_gb();
return 0;
}
int functionfs_cleanup(void)
{
recv_thread_cleanup(NULL);
return 0;
}