-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathusb.c
More file actions
492 lines (405 loc) · 11.9 KB
/
usb.c
File metadata and controls
492 lines (405 loc) · 11.9 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// SPDX-License-Identifier: BSD-3-Clause
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libusb.h>
#include "oscompat.h"
#include "qdl.h"
#define DEFAULT_OUT_CHUNK_SIZE (1024 * 1024)
struct qdl_device_usb {
struct qdl_device base;
struct libusb_device_handle *usb_handle;
int in_ep;
int out_ep;
size_t in_maxpktsize;
size_t out_maxpktsize;
size_t out_chunk_size;
};
/*
* libusb commit f0cce43f882d ("core: Fix definition and use of enum
* libusb_transfer_type") split transfer type and endpoint transfer types.
* Provide an alias in order to make the code compile with the old (non-split)
* definition.
*/
#ifndef LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK
#define LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK LIBUSB_TRANSFER_TYPE_BULK
#endif
static bool usb_read_serial(struct libusb_device_handle *handle,
const struct libusb_device_descriptor *desc,
char *out, size_t out_len)
{
char buf[128];
char *p;
int ret;
ret = libusb_get_string_descriptor_ascii(handle, desc->iProduct, (unsigned char *)buf, sizeof(buf));
if (ret < 0) {
warnx("failed to read iProduct descriptor: %s", libusb_strerror(ret));
return false;
}
p = strstr(buf, "_SN:");
if (!p)
return false;
p += strlen("_SN:");
p[strcspn(p, " _")] = '\0';
snprintf(out, out_len, "%s", p);
return true;
}
static bool usb_match_usb_serial(struct libusb_device_handle *handle, const char *serial,
const struct libusb_device_descriptor *desc)
{
char buf[64];
/* If no serial is requested, consider everything a match */
if (!serial)
return true;
if (!usb_read_serial(handle, desc, buf, sizeof(buf)))
return false;
return strcmp(buf, serial) == 0;
}
static int usb_try_open(libusb_device *dev, struct qdl_device_usb *qdl, const char *serial)
{
const struct libusb_endpoint_descriptor *endpoint;
const struct libusb_interface_descriptor *ifc;
struct libusb_config_descriptor *config;
struct libusb_device_descriptor desc;
struct libusb_device_handle *handle;
size_t out_size;
size_t in_size;
uint8_t type;
int ret;
int out;
int in;
int k;
int l;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
warnx("failed to get USB device descriptor");
return -1;
}
/* Consider only devices with vid 0x0506 and known product id */
if (desc.idVendor != 0x05c6)
return 0;
if (desc.idProduct != 0x9008 && desc.idProduct != 0x900e && desc.idProduct != 0x901d)
return 0;
ret = libusb_get_active_config_descriptor(dev, &config);
if (ret < 0) {
warnx("failed to acquire USB device's active config descriptor");
return -1;
}
for (k = 0; k < config->bNumInterfaces; k++) {
ifc = config->interface[k].altsetting;
in = -1;
out = -1;
in_size = 0;
out_size = 0;
for (l = 0; l < ifc->bNumEndpoints; l++) {
endpoint = &ifc->endpoint[l];
type = endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK;
if (type != LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK)
continue;
if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
in = endpoint->bEndpointAddress;
in_size = endpoint->wMaxPacketSize;
} else {
out = endpoint->bEndpointAddress;
out_size = endpoint->wMaxPacketSize;
}
}
if (ifc->bInterfaceClass != 0xff)
continue;
if (ifc->bInterfaceSubClass != 0xff)
continue;
/* bInterfaceProtocol of 0xff, 0x10 and 0x11 has been seen */
if (ifc->bInterfaceProtocol != 0xff &&
ifc->bInterfaceProtocol != 16 &&
ifc->bInterfaceProtocol != 17)
continue;
ret = libusb_open(dev, &handle);
if (ret < 0) {
warnx("unable to open USB device");
continue;
}
if (!usb_match_usb_serial(handle, serial, &desc)) {
libusb_close(handle);
continue;
}
libusb_detach_kernel_driver(handle, ifc->bInterfaceNumber);
ret = libusb_claim_interface(handle, ifc->bInterfaceNumber);
if (ret < 0) {
warnx("failed to claim USB interface");
libusb_close(handle);
continue;
}
qdl->usb_handle = handle;
qdl->in_ep = in;
qdl->out_ep = out;
qdl->in_maxpktsize = in_size;
qdl->out_maxpktsize = out_size;
if (qdl->out_chunk_size && qdl->out_chunk_size % out_size) {
ux_err("WARNING: requested out-chunk-size must be multiple of the device's wMaxPacketSize %ld, using %ld\n",
out_size, out_size);
qdl->out_chunk_size = out_size;
} else if (!qdl->out_chunk_size) {
qdl->out_chunk_size = DEFAULT_OUT_CHUNK_SIZE;
}
ux_debug("USB: using out-chunk-size of %ld\n", qdl->out_chunk_size);
break;
}
libusb_free_config_descriptor(config);
return !!qdl->usb_handle;
}
static bool usb_is_edl_pid(uint16_t pid)
{
return pid == 0x9008 || pid == 0x900e || pid == 0x901d;
}
static int usb_open(struct qdl_device *qdl, const char *serial)
{
struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base);
struct libusb_device_descriptor desc;
struct libusb_device **devs;
struct libusb_device *dev;
char matched_serial[64];
uint16_t matched_pid = 0;
int visible_prev = -1;
bool found = false;
int visible;
ssize_t n;
int ret;
int i;
for (;;) {
ret = libusb_init(NULL);
if (ret < 0)
ux_err("failed to initialize libusb: %s\n", libusb_strerror(ret));
n = libusb_get_device_list(NULL, &devs);
if (n < 0) {
ux_err("failed to list USB devices: %s\n", libusb_strerror(n));
libusb_exit(NULL);
return -1;
}
visible = 0;
for (i = 0; devs[i]; i++) {
dev = devs[i];
if (libusb_get_device_descriptor(dev, &desc) < 0)
continue;
if (desc.idVendor != 0x05c6 || !usb_is_edl_pid(desc.idProduct))
continue;
visible++;
ret = usb_try_open(dev, qdl_usb, serial);
if (ret == 1) {
found = true;
matched_pid = desc.idProduct;
if (!usb_read_serial(qdl_usb->usb_handle, &desc,
matched_serial, sizeof(matched_serial)))
matched_serial[0] = '\0';
break;
}
}
if (found) {
const char *action = matched_pid == 0x900e ? "Collecting crash dump from" : "Flashing";
libusb_free_device_list(devs, 1);
if (matched_serial[0])
ux_info("%s device (PID 0x%04x, serial: %s)\n",
action, matched_pid, matched_serial);
else
ux_info("%s device (PID 0x%04x)\n", action, matched_pid);
return 0;
}
if (visible != visible_prev) {
if (visible == 0) {
ux_info("Waiting for EDL device\n");
} else {
if (serial)
ux_info("%d EDL device(s) visible, none match serial \"%s\":\n",
visible, serial);
else
ux_info("%d EDL device(s) visible, none could be opened:\n",
visible);
for (i = 0; devs[i]; i++) {
dev = devs[i];
if (libusb_get_device_descriptor(dev, &desc) < 0)
continue;
if (desc.idVendor != 0x05c6 || !usb_is_edl_pid(desc.idProduct))
continue;
ux_info(" [bus %u, addr %u] PID 0x%04x\n",
libusb_get_bus_number(dev),
libusb_get_device_address(dev),
desc.idProduct);
}
}
visible_prev = visible;
}
libusb_free_device_list(devs, 1);
/*
* Tear down libusb before retrying so the next iteration
* builds a fresh device list from scratch. Without this,
* libusb's udev-based backend caches the enumeration and
* never notices newly attached devices - a problem in
* containerised environments where udev events are not
* available.
*/
libusb_exit(NULL);
usleep(250000);
}
return -1;
}
struct qdl_device_desc *usb_list(unsigned int *devices_found)
{
struct libusb_device_descriptor desc;
struct libusb_device_handle *handle;
struct qdl_device_desc *result;
struct libusb_device **devices;
struct libusb_device *dev;
unsigned long serial_len;
unsigned char buf[128];
ssize_t device_count;
unsigned int count = 0;
char *serial;
int ret;
int i;
ret = libusb_init(NULL);
if (ret < 0) {
ux_err("failed to initialize libusb: %s\n", libusb_strerror(ret));
return NULL;
}
device_count = libusb_get_device_list(NULL, &devices);
if (device_count < 0) {
ux_err("failed to list USB devices: %s\n", libusb_strerror(device_count));
libusb_exit(NULL);
return NULL;
}
if (device_count == 0)
return NULL;
result = calloc(device_count, sizeof(struct qdl_device_desc));
if (!result) {
ux_err("failed to allocate devices array\n");
libusb_free_device_list(devices, 1);
libusb_exit(NULL);
return NULL;
}
for (i = 0; i < device_count; i++) {
dev = devices[i];
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
warnx("failed to get USB device descriptor");
continue;
}
if (desc.idVendor != 0x05c6)
continue;
if (desc.idProduct != 0x9008 && desc.idProduct != 0x900e && desc.idProduct != 0x901d)
continue;
ret = libusb_open(dev, &handle);
if (ret < 0) {
warnx("unable to open USB device");
continue;
}
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf, sizeof(buf) - 1);
if (ret < 0) {
warnx("failed to read iProduct descriptor: %s", libusb_strerror(ret));
libusb_close(handle);
continue;
}
buf[ret] = '\0';
serial = strstr((char *)buf, "_SN:");
if (!serial) {
ux_err("ignoring device with no serial number\n");
libusb_close(handle);
continue;
}
serial += strlen("_SN:");
serial_len = strcspn(serial, " _");
if (serial_len + 1 > sizeof(result[count].serial)) {
ux_err("ignoring device with unexpectedly long serial number\n");
libusb_close(handle);
continue;
}
memcpy(result[count].serial, serial, serial_len);
result[count].serial[serial_len] = '\0';
result[count].vid = desc.idVendor;
result[count].pid = desc.idProduct;
libusb_close(handle);
count++;
}
libusb_free_device_list(devices, 1);
*devices_found = count;
return result;
}
static void usb_close(struct qdl_device *qdl)
{
struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base);
libusb_close(qdl_usb->usb_handle);
libusb_exit(NULL);
}
static int usb_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout)
{
struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base);
int actual;
int ret;
ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->in_ep, buf, len, &actual, timeout);
if (ret != 0 && ret != LIBUSB_ERROR_TIMEOUT)
return -EIO;
if (ret == LIBUSB_ERROR_TIMEOUT && actual == 0)
return -ETIMEDOUT;
/* If what we read equals the endpoint's Max Packet Size, consume the ZLP explicitly */
if (len == (size_t)actual && !(actual % qdl_usb->in_maxpktsize)) {
ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->in_ep,
NULL, 0, NULL, timeout);
if (ret)
warnx("Unable to read ZLP: %s", libusb_strerror(ret));
}
return actual;
}
static int usb_write(struct qdl_device *qdl, const void *buf, size_t len, unsigned int timeout)
{
unsigned char *data = (unsigned char *)buf;
struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base);
unsigned int count = 0;
size_t len_orig = len;
int actual;
int xfer;
int ret;
while (len > 0) {
xfer = (len > qdl_usb->out_chunk_size) ? qdl_usb->out_chunk_size : len;
ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->out_ep, data,
xfer, &actual, timeout);
if (ret != 0 && ret != LIBUSB_ERROR_TIMEOUT) {
warnx("bulk write failed: %s", libusb_strerror(ret));
return -EIO;
}
if (ret == LIBUSB_ERROR_TIMEOUT && actual == 0)
return -ETIMEDOUT;
count += actual;
len -= actual;
data += actual;
}
if (len_orig % qdl_usb->out_maxpktsize == 0) {
ret = libusb_bulk_transfer(qdl_usb->usb_handle, qdl_usb->out_ep, NULL,
0, &actual, timeout);
if (ret < 0)
return -EIO;
}
return count;
}
static void usb_set_out_chunk_size(struct qdl_device *qdl, long size)
{
struct qdl_device_usb *qdl_usb = container_of(qdl, struct qdl_device_usb, base);
qdl_usb->out_chunk_size = size;
}
struct qdl_device *usb_init(void)
{
struct qdl_device *qdl = malloc(sizeof(struct qdl_device_usb));
if (!qdl)
return NULL;
memset(qdl, 0, sizeof(struct qdl_device_usb));
qdl->dev_type = QDL_DEVICE_USB;
qdl->open = usb_open;
qdl->read = usb_read;
qdl->write = usb_write;
qdl->close = usb_close;
qdl->set_out_chunk_size = usb_set_out_chunk_size;
qdl->max_payload_size = 1048576;
return qdl;
}