forked from ueno/libusb-gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopback.c
More file actions
299 lines (260 loc) · 8.42 KB
/
Copy pathloopback.c
File metadata and controls
299 lines (260 loc) · 8.42 KB
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
/*
* Copyright (C) 2009 Daiki Ueno <ueno@unixuser.org>
* This file is part of libusb-gadget.
*
* libusb-gadget is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libusb-gadget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <getopt.h>
//#include "config.h"
#include <linux/usb/ch9.h>
#include <usb-gadget.h>
/* /dev/gadget/ep* doesn't support poll, we have to use an alternative
approach. */
#include <pthread.h>
#define STRING_MANUFACTURER 25
#define STRING_PRODUCT 45
#define STRING_SERIAL 101
#define STRING_LOOPBACK 250
static struct usb_gadget_string strings[] = {
{STRING_MANUFACTURER, "The manufacturer",},
{STRING_PRODUCT, "The product",},
{STRING_SERIAL, "0123456789.0123456789.0123456789",},
{STRING_LOOPBACK, "The loopback",},
};
static struct usb_gadget_strings loopback_strings = {
.language = 0x0409, /* en-us */
.strings = strings
};
#define CONFIG_LOOPBACK 2
static struct usb_device_descriptor loopback_device_descriptor = {
.bLength = sizeof(loopback_device_descriptor),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = usb_gadget_cpu_to_le16(0x0200),
.bDeviceClass = USB_CLASS_VENDOR_SPEC,
.iManufacturer = usb_gadget_cpu_to_le16(STRING_MANUFACTURER),
.iProduct = usb_gadget_cpu_to_le16(STRING_PRODUCT),
.iSerialNumber = usb_gadget_cpu_to_le16(STRING_SERIAL),
.bNumConfigurations = 1,
};
static struct usb_config_descriptor loopback_config_descriptor = {
.bLength = sizeof(loopback_config_descriptor),
.bDescriptorType = USB_DT_CONFIG,
.bNumInterfaces = 1,
.bConfigurationValue = CONFIG_LOOPBACK,
.iConfiguration = STRING_LOOPBACK,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 1, /* self-powered */
};
static const struct usb_interface_descriptor loopback_interface_descriptor = {
.bLength = sizeof(loopback_interface_descriptor),
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
.iInterface = STRING_LOOPBACK,
};
static struct usb_endpoint_descriptor loopback_ep_in_descriptor = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN | 7, /* number is mandatory for gadgetfs */
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = usb_gadget_cpu_to_le16(64), /* mandatory for gadgetfs */
};
static struct usb_endpoint_descriptor loopback_ep_out_descriptor = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT | 3, /* number is mandatory for gadgetfs */
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = usb_gadget_cpu_to_le16(64), /* mandatory for gadgetfs */
};
static struct usb_endpoint_descriptor loopback_hs_ep_in_descriptor = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN | 7, /* number is mandatory for gadgetfs */
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = usb_gadget_cpu_to_le16(512), /* mandatory for gadgetfs */
};
static struct usb_endpoint_descriptor loopback_hs_ep_out_descriptor = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT | 3, /* number is mandatory for gadgetfs */
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = usb_gadget_cpu_to_le16(512), /* mandatory for gadgetfs */
};
static struct usb_descriptor_header *loopback_config[] = {
(struct usb_descriptor_header *)&loopback_config_descriptor,
(struct usb_descriptor_header *)&loopback_interface_descriptor,
(struct usb_descriptor_header *)&loopback_ep_in_descriptor,
(struct usb_descriptor_header *)&loopback_ep_out_descriptor,
NULL,
};
static struct usb_descriptor_header *loopback_hs_config[] = {
(struct usb_descriptor_header *)&loopback_config_descriptor,
(struct usb_descriptor_header *)&loopback_interface_descriptor,
(struct usb_descriptor_header *)&loopback_hs_ep_in_descriptor,
(struct usb_descriptor_header *)&loopback_hs_ep_out_descriptor,
NULL,
};
static struct usb_gadget_endpoint *loopback_ep_in, *loopback_ep_out;
static pthread_t loopback_thread;
static void
loopback_stop_endpoints (void *data)
{
usb_gadget_endpoint_close (loopback_ep_in);
usb_gadget_endpoint_close (loopback_ep_out);
}
static void*
loopback_loop (void *data)
{
char buf[BUFSIZ];
int ret;
pthread_cleanup_push (loopback_stop_endpoints, NULL);
while (1)
{
int i;
pthread_testcancel ();
ret = usb_gadget_endpoint_read (loopback_ep_out, buf, 64, 100);
if (ret < 0)
{
perror ("usb_gadget_endpoint_read");
break;
}
for (i = 0; i < ret / 2; i++)
{
char c;
c = buf[i];
buf[i] = buf[ret - i - 1];
buf[ret - i - 1] = c;
}
if (usb_gadget_endpoint_write (loopback_ep_in, buf, ret, 100) < 0)
{
perror ("usb_gadget_endpoint_write");
break;
}
}
pthread_cleanup_pop (1);
}
static void
loopback_event_cb (usb_gadget_dev_handle *handle, struct usb_gadget_event *event, void *arg)
{
switch (event->type)
{
case USG_EVENT_ENDPOINT_ENABLE:
if (event->u.number == (loopback_ep_in_descriptor.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK))
loopback_ep_in = usb_gadget_endpoint (handle, event->u.number);
else if (event->u.number == (loopback_ep_out_descriptor.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK))
loopback_ep_out = usb_gadget_endpoint (handle, event->u.number);
if (!loopback_ep_in || !loopback_ep_out)
return;
if (pthread_create (&loopback_thread, 0, loopback_loop, NULL) != 0)
perror ("pthread_create");
break;
case USG_EVENT_ENDPOINT_DISABLE:
if (event->u.number == (loopback_ep_in_descriptor.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK))
loopback_ep_in = NULL;
else if (event->u.number == (loopback_ep_out_descriptor.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK))
loopback_ep_out = NULL;
case USG_EVENT_DISCONNECT: /* FALLTHROUGH */
if (loopback_thread)
pthread_cancel (loopback_thread);
break;
}
}
static char *program_name;
static void
usage (FILE *out)
{
fprintf (out, "Usage: %s [OPTIONS] VEND:PROD\n"
"Options are:\n"
"\t--debug=LEVEL, -d\tSpecify debug level\n"
"\t--help, -h\tShow this help\n",
program_name);
}
int main (int argc, char **argv)
{
struct usb_gadget_device device = {
.device = &loopback_device_descriptor,
.config = loopback_config,
.hs_config = loopback_hs_config,
.strings = &loopback_strings,
};
usb_gadget_dev_handle *handle;
struct usb_gadget_endpoint *ep0;
struct pollfd fds;
int vendor_id, product_id, c, debug_level = 0;
struct option long_options[] = {
{"debug", 1, 0, 'd'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
program_name = argv[0];
while (1)
{
int option_index = 0;
c = getopt_long (argc, argv, "hd:", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'd':
debug_level = atoi (optarg);
break;
case 'h':
usage (stdout);
exit (0);
default:
usage (stderr);
exit (1);
}
}
if ((argc - optind) != 1
|| sscanf (argv[optind], "%X:%X", &vendor_id, &product_id) != 2)
{
usage (stderr);
exit (1);
}
loopback_device_descriptor.idVendor = vendor_id;
loopback_device_descriptor.idProduct = product_id;
handle = usb_gadget_open (&device);
if (!handle)
{
fprintf (stderr, "Couldn't open device.\n");
exit (1);
}
usb_gadget_set_event_cb (handle, loopback_event_cb, NULL);
usb_gadget_set_debug_level (handle, debug_level);
ep0 = usb_gadget_endpoint (handle, 0);
fds.fd = usb_gadget_control_fd (handle);
fds.events = POLLIN;
while (1)
{
if (poll (&fds, 1, -1) < 0)
{
perror ("poll");
break;
}
if (fds.revents & POLLIN)
usb_gadget_handle_control_event (handle);
}
usb_gadget_close (handle);
return 0;
}