1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
28
33
34
35
36
37
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
65
66
67
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
98
99
100
101
102
113
114
115
116
120
121
125
126
127
128
129
130
131
132
133
137
138
139
140
141
142
143
144
145
146
147
148
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
173
174
175
176
182
183
184
185
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
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
352
353
354
355
356
357
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
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
458
459
463
464
468
469
470
471
472
473
474
475
476
477
478
482
483
484
485
486
487
488
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
593
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* ... */
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "libjaylink.h"
#include "libjaylink-internal.h"
6 includes
/* ... */
#define USB_TIMEOUT 1000
/* ... */
#define NUM_TIMEOUTS 2
#define CHUNK_SIZE 2048
static int initialize_handle(struct jaylink_device_handle *devh)
{
int ret;
struct jaylink_context *ctx;
struct libusb_config_descriptor *config;
const struct libusb_interface *interface;
const struct libusb_interface_descriptor *desc;
const struct libusb_endpoint_descriptor *epdesc;
bool found_interface;
bool found_endpoint_in;
bool found_endpoint_out;
ctx = devh->dev->ctx;
devh->interface_number = 0;
/* ... */
ret = libusb_get_active_config_descriptor(devh->dev->usb_dev, &config);
if (ret != LIBUSB_SUCCESS) {
log_err(ctx, "Failed to get configuration descriptor: %s",
libusb_error_name(ret));
return JAYLINK_ERR;
}if (ret != LIBUSB_SUCCESS) { ... }
found_interface = false;
for (uint8_t i = 0; i < config->bNumInterfaces; i++) {
interface = &config->interface[i];
desc = &interface->altsetting[0];
if (desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC)
continue;
if (desc->bInterfaceSubClass != LIBUSB_CLASS_VENDOR_SPEC)
continue;
if (desc->bNumEndpoints < 2)
continue;
found_interface = true;
devh->interface_number = i;
break;
}for (uint8_t i = 0; i < config->bNumInterfaces; i++) { ... }
if (!found_interface) {
log_err(ctx, "No suitable interface found");
libusb_free_config_descriptor(config);
return JAYLINK_ERR;
}if (!found_interface) { ... }
found_endpoint_in = false;
found_endpoint_out = false;
for (uint8_t i = 0; i < desc->bNumEndpoints; i++) {
epdesc = &desc->endpoint[i];
if (epdesc->bEndpointAddress & LIBUSB_ENDPOINT_IN) {
devh->endpoint_in = epdesc->bEndpointAddress;
found_endpoint_in = true;
}if (epdesc->bEndpointAddress & LIBUSB_ENDPOINT_IN) { ... } else {
devh->endpoint_out = epdesc->bEndpointAddress;
found_endpoint_out = true;
}else { ... }
}for (uint8_t i = 0; i < desc->bNumEndpoints; i++) { ... }
libusb_free_config_descriptor(config);
if (!found_endpoint_in) {
log_err(ctx, "Interface IN endpoint not found");
return JAYLINK_ERR;
}if (!found_endpoint_in) { ... }
if (!found_endpoint_out) {
log_err(ctx, "Interface OUT endpoint not found");
return JAYLINK_ERR;
}if (!found_endpoint_out) { ... }
log_dbg(ctx, "Using endpoint %02x (IN) and %02x (OUT)",
devh->endpoint_in, devh->endpoint_out);
devh->buffer_size = CHUNK_SIZE;
devh->buffer = malloc(devh->buffer_size);
if (!devh->buffer) {
log_err(ctx, "Transport buffer malloc failed");
return JAYLINK_ERR_MALLOC;
}if (!devh->buffer) { ... }
devh->read_length = 0;
devh->bytes_available = 0;
devh->read_pos = 0;
devh->write_length = 0;
devh->write_pos = 0;
return JAYLINK_OK;
}{ ... }
static void cleanup_handle(struct jaylink_device_handle *devh)
{
free(devh->buffer);
}{ ... }
JAYLINK_PRIV int transport_usb_open(struct jaylink_device_handle *devh)
{
int ret;
struct jaylink_device *dev;
struct jaylink_context *ctx;
struct libusb_device_handle *usb_devh;
dev = devh->dev;
ctx = dev->ctx;
log_dbg(ctx, "Trying to open device (bus:address = %03u:%03u)",
libusb_get_bus_number(dev->usb_dev),
libusb_get_device_address(dev->usb_dev));
ret = initialize_handle(devh);
if (ret != JAYLINK_OK) {
log_err(ctx, "Initialize device handle failed");
return ret;
}if (ret != JAYLINK_OK) { ... }
ret = libusb_open(dev->usb_dev, &usb_devh);
if (ret != LIBUSB_SUCCESS) {
log_err(ctx, "Failed to open device: %s",
libusb_error_name(ret));
cleanup_handle(devh);
return JAYLINK_ERR;
}if (ret != LIBUSB_SUCCESS) { ... }
ret = libusb_claim_interface(usb_devh, devh->interface_number);
if (ret != LIBUSB_SUCCESS) {
log_err(ctx, "Failed to claim interface: %s",
libusb_error_name(ret));
cleanup_handle(devh);
libusb_close(usb_devh);
return JAYLINK_ERR;
}if (ret != LIBUSB_SUCCESS) { ... }
log_dbg(ctx, "Device opened successfully");
devh->usb_devh = usb_devh;
return JAYLINK_OK;
}{ ... }
JAYLINK_PRIV int transport_usb_close(struct jaylink_device_handle *devh)
{
int ret;
struct jaylink_device *dev;
struct jaylink_context *ctx;
dev = devh->dev;
ctx = dev->ctx;
log_dbg(ctx, "Closing device (bus:address = %03u:%03u)",
libusb_get_bus_number(dev->usb_dev),
libusb_get_device_address(dev->usb_dev));
ret = libusb_release_interface(devh->usb_devh, devh->interface_number);
libusb_close(devh->usb_devh);
cleanup_handle(devh);
if (ret != LIBUSB_SUCCESS) {
log_err(ctx, "Failed to release interface: %s",
libusb_error_name(ret));
return JAYLINK_ERR;
}if (ret != LIBUSB_SUCCESS) { ... }
log_dbg(ctx, "Device closed successfully");
return JAYLINK_OK;
}{ ... }
JAYLINK_PRIV int transport_usb_start_write(struct jaylink_device_handle *devh,
size_t length, bool has_command)
{
struct jaylink_context *ctx;
(void)has_command;
if (!length)
return JAYLINK_ERR_ARG;
ctx = devh->dev->ctx;
log_dbgio(ctx, "Starting write operation (length = %zu bytes)", length);
if (devh->write_pos > 0)
log_warn(ctx, "Last write operation left %zu bytes in the "
"buffer", devh->write_pos);
if (devh->write_length > 0)
log_warn(ctx, "Last write operation was not performed");
devh->write_length = length;
devh->write_pos = 0;
return JAYLINK_OK;
}{ ... }
JAYLINK_PRIV int transport_usb_start_read(struct jaylink_device_handle *devh,
size_t length)
{
struct jaylink_context *ctx;
if (!length)
return JAYLINK_ERR_ARG;
ctx = devh->dev->ctx;
log_dbgio(ctx, "Starting read operation (length = %zu bytes)",
length);
if (devh->bytes_available > 0)
log_dbg(ctx, "Last read operation left %zu bytes in the "
"buffer", devh->bytes_available);
if (devh->read_length > 0)
log_warn(ctx, "Last read operation left %zu bytes",
devh->read_length);
devh->read_length = length;
return JAYLINK_OK;
}{ ... }
JAYLINK_PRIV int transport_usb_start_write_read(
struct jaylink_device_handle *devh, size_t write_length,
size_t read_length, bool has_command)
{
struct jaylink_context *ctx;
(void)has_command;
if (!read_length || !write_length)
return JAYLINK_ERR_ARG;
ctx = devh->dev->ctx;
log_dbgio(ctx, "Starting write / read operation (length = "
"%zu / %zu bytes)", write_length, read_length);
if (devh->write_pos > 0)
log_warn(ctx, "Last write operation left %zu bytes in the "
"buffer", devh->write_pos);
if (devh->write_length > 0)
log_warn(ctx, "Last write operation was not performed");
if (devh->bytes_available > 0)
log_warn(ctx, "Last read operation left %zu bytes in the "
"buffer", devh->bytes_available);
if (devh->read_length > 0)
log_warn(ctx, "Last read operation left %zu bytes",
devh->read_length);
devh->write_length = write_length;
devh->write_pos = 0;
devh->read_length = read_length;
devh->bytes_available = 0;
devh->read_pos = 0;
return JAYLINK_OK;
}{ ... }
static int usb_recv(struct jaylink_device_handle *devh, uint8_t *buffer,
size_t *length)
{
int ret;
struct jaylink_context *ctx;
unsigned int tries;
int transferred;
ctx = devh->dev->ctx;
tries = NUM_TIMEOUTS;
transferred = 0;
while (tries > 0 && !transferred) {
ret = libusb_bulk_transfer(devh->usb_devh, devh->endpoint_in,
(unsigned char *)buffer, CHUNK_SIZE, &transferred,
USB_TIMEOUT);
if (ret == LIBUSB_ERROR_TIMEOUT) {
log_warn(ctx, "Failed to receive data from "
"device: %s", libusb_error_name(ret));
tries--;
continue;
}if (ret == LIBUSB_ERROR_TIMEOUT) { ... } else if (ret != LIBUSB_SUCCESS) {
log_err(ctx, "Failed to receive data from "
"device: %s", libusb_error_name(ret));
return JAYLINK_ERR;
}else if (ret != LIBUSB_SUCCESS) { ... }
log_dbgio(ctx, "Received %i bytes from device", transferred);
}while (tries > 0 && !transferred) { ... }
if (transferred > 0) {
*length = transferred;
return JAYLINK_OK;
}if (transferred > 0) { ... }
log_err(ctx, "Receiving data from device timed out");
return JAYLINK_ERR_TIMEOUT;
}{ ... }
static bool adjust_buffer(struct jaylink_device_handle *devh, size_t size)
{
struct jaylink_context *ctx;
size_t num_chunks;
uint8_t *buffer;
ctx = devh->dev->ctx;
num_chunks = size / CHUNK_SIZE;
if (size % CHUNK_SIZE > 0)
num_chunks++;
size = num_chunks * CHUNK_SIZE;
buffer = realloc(devh->buffer, size);
if (!buffer) {
log_err(ctx, "Failed to adjust buffer size to %zu bytes",
size);
return false;
}if (!buffer) { ... }
devh->buffer = buffer;
devh->buffer_size = size;
log_dbg(ctx, "Adjusted buffer size to %zu bytes", size);
return true;
}{ ... }
static int usb_send(struct jaylink_device_handle *devh, const uint8_t *buffer,
size_t length)
{
int ret;
struct jaylink_context *ctx;
unsigned int tries;
int transferred;
ctx = devh->dev->ctx;
tries = NUM_TIMEOUTS;
while (tries > 0 && length > 0) {
ret = libusb_bulk_transfer(devh->usb_devh, devh->endpoint_out,
(unsigned char *)buffer, MIN(CHUNK_SIZE, length),
&transferred, USB_TIMEOUT);
if (ret == LIBUSB_SUCCESS) {
tries = NUM_TIMEOUTS;
}if (ret == LIBUSB_SUCCESS) { ... } else if (ret == LIBUSB_ERROR_TIMEOUT) {
log_warn(ctx, "Failed to send data to device: %s",
libusb_error_name(ret));
tries--;
}else if (ret == LIBUSB_ERROR_TIMEOUT) { ... } else {
log_err(ctx, "Failed to send data to device: %s",
libusb_error_name(ret));
return JAYLINK_ERR;
}else { ... }
buffer += transferred;
length -= transferred;
log_dbgio(ctx, "Sent %i bytes to device", transferred);
}while (tries > 0 && length > 0) { ... }
if (!length)
return JAYLINK_OK;
log_err(ctx, "Sending data to device timed out");
return JAYLINK_ERR_TIMEOUT;
}{ ... }
JAYLINK_PRIV int transport_usb_write(struct jaylink_device_handle *devh,
const uint8_t *buffer, size_t length)
{
int ret;
struct jaylink_context *ctx;
size_t num_chunks;
size_t fill_bytes;
size_t tmp;
ctx = devh->dev->ctx;
if (length > devh->write_length) {
log_err(ctx, "Requested to write %zu bytes but only %zu bytes "
"are expected for the write operation", length,
devh->write_length);
return JAYLINK_ERR_ARG;
}if (length > devh->write_length) { ... }
/* ... */
if (length < devh->write_length) {
if (devh->write_pos + length > devh->buffer_size) {
if (!adjust_buffer(devh, devh->write_pos + length))
return JAYLINK_ERR_MALLOC;
}if (devh->write_pos + length > devh->buffer_size) { ... }
memcpy(devh->buffer + devh->write_pos, buffer, length);
devh->write_length -= length;
devh->write_pos += length;
log_dbgio(ctx, "Wrote %zu bytes into buffer", length);
return JAYLINK_OK;
}if (length < devh->write_length) { ... }
/* ... */
devh->write_length = 0;
if (!devh->write_pos)
return usb_send(devh, buffer, length);
/* ... */
num_chunks = devh->write_pos / CHUNK_SIZE;
if (devh->write_pos % CHUNK_SIZE)
num_chunks++;
fill_bytes = (num_chunks * CHUNK_SIZE) - devh->write_pos;
tmp = MIN(length, fill_bytes);
if (tmp > 0) {
memcpy(devh->buffer + devh->write_pos, buffer, tmp);
length -= tmp;
buffer += tmp;
log_dbgio(ctx, "Buffer filled up with %zu bytes", tmp);
}if (tmp > 0) { ... }
ret = usb_send(devh, devh->buffer, devh->write_pos + tmp);
devh->write_pos = 0;
if (ret != JAYLINK_OK)
return ret;
if (!length)
return JAYLINK_OK;
return usb_send(devh, buffer, length);
}{ ... }
JAYLINK_PRIV int transport_usb_read(struct jaylink_device_handle *devh,
uint8_t *buffer, size_t length)
{
int ret;
struct jaylink_context *ctx;
size_t bytes_received;
size_t tmp;
ctx = devh->dev->ctx;
if (length > devh->read_length) {
log_err(ctx, "Requested to read %zu bytes but only %zu bytes "
"are expected for the read operation", length,
devh->read_length);
return JAYLINK_ERR_ARG;
}if (length > devh->read_length) { ... }
if (length <= devh->bytes_available) {
memcpy(buffer, devh->buffer + devh->read_pos, length);
devh->read_length -= length;
devh->bytes_available -= length;
devh->read_pos += length;
log_dbgio(ctx, "Read %zu bytes from buffer", length);
return JAYLINK_OK;
}if (length <= devh->bytes_available) { ... }
if (devh->bytes_available) {
memcpy(buffer, devh->buffer + devh->read_pos,
devh->bytes_available);
buffer += devh->bytes_available;
length -= devh->bytes_available;
devh->read_length -= devh->bytes_available;
log_dbgio(ctx, "Read %zu bytes from buffer to flush it",
devh->bytes_available);
devh->bytes_available = 0;
devh->read_pos = 0;
}if (devh->bytes_available) { ... }
while (length > 0) {
/* ... */
if (length < CHUNK_SIZE) {
ret = usb_recv(devh, devh->buffer, &bytes_received);
if (ret != JAYLINK_OK)
return ret;
tmp = MIN(bytes_received, length);
memcpy(buffer, devh->buffer, tmp);
/* ... */
if (bytes_received > length) {
devh->bytes_available = bytes_received - tmp;
devh->read_pos = tmp;
}if (bytes_received > length) { ... }
buffer += tmp;
length -= tmp;
devh->read_length -= tmp;
log_dbgio(ctx, "Read %zu bytes from buffer", tmp);
}if (length < CHUNK_SIZE) { ... } else {
ret = usb_recv(devh, buffer, &bytes_received);
if (ret != JAYLINK_OK)
return ret;
buffer += bytes_received;
length -= bytes_received;
devh->read_length -= bytes_received;
log_dbgio(ctx, "Read %zu bytes from device",
bytes_received);
}else { ... }
}while (length > 0) { ... }
return JAYLINK_OK;
}{ ... }