-
Notifications
You must be signed in to change notification settings - Fork 318
/
usb.c
1243 lines (1025 loc) · 30.3 KB
/
usb.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
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
493
494
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
537
538
539
540
541
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2015 - 2020 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "iio-config.h"
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <iio/iio-lock.h>
#include <iio/iiod-client.h>
#include <ctype.h>
#ifdef __linux__
#include <dirent.h>
#endif
#include <errno.h>
#include <libusb.h>
#include <stdbool.h>
#include <string.h>
/* Endpoint for non-streaming operations */
#define EP_OPS 1
#define IIO_INTERFACE_NAME "IIO"
struct iio_usb_ep_couple {
unsigned char addr_in, addr_out;
uint16_t pipe_id;
bool in_use;
const struct iio_device *dev;
};
struct iiod_client_pdata {
struct iio_usb_ep_couple *ep;
struct iiod_client *iiod_client;
struct iio_mutex *lock;
bool cancelled;
struct libusb_transfer *transfer;
struct iio_context_pdata *ctx_pdata;
};
struct iio_context_pdata {
libusb_context *ctx;
libusb_device_handle *hdl;
uint16_t intrfc;
/* Lock for endpoint reservation */
struct iio_mutex *ep_lock;
struct iio_usb_ep_couple *io_endpoints;
uint16_t nb_ep_couples;
struct iiod_client_pdata io_ctx;
};
struct iio_buffer_pdata {
struct iiod_client_pdata io_ctx;
const struct iio_device *dev;
struct iiod_client_buffer_pdata *pdata;
};
static const unsigned int libusb_to_errno_codes[] = {
[- LIBUSB_ERROR_INVALID_PARAM] = EINVAL,
[- LIBUSB_ERROR_ACCESS] = EACCES,
[- LIBUSB_ERROR_NO_DEVICE] = ENODEV,
[- LIBUSB_ERROR_NOT_FOUND] = ENXIO,
[- LIBUSB_ERROR_BUSY] = EBUSY,
[- LIBUSB_ERROR_TIMEOUT] = ETIMEDOUT,
[- LIBUSB_ERROR_OVERFLOW] = EIO,
[- LIBUSB_ERROR_PIPE] = EPIPE,
[- LIBUSB_ERROR_INTERRUPTED] = EINTR,
[- LIBUSB_ERROR_NO_MEM] = ENOMEM,
[- LIBUSB_ERROR_NOT_SUPPORTED] = ENOSYS,
};
static unsigned int libusb_to_errno(int error)
{
switch ((enum libusb_error) error) {
case LIBUSB_ERROR_INVALID_PARAM:
case LIBUSB_ERROR_ACCESS:
case LIBUSB_ERROR_NO_DEVICE:
case LIBUSB_ERROR_NOT_FOUND:
case LIBUSB_ERROR_BUSY:
case LIBUSB_ERROR_TIMEOUT:
case LIBUSB_ERROR_PIPE:
case LIBUSB_ERROR_INTERRUPTED:
case LIBUSB_ERROR_NO_MEM:
case LIBUSB_ERROR_NOT_SUPPORTED:
return libusb_to_errno_codes[- (int) error];
case LIBUSB_ERROR_IO:
case LIBUSB_ERROR_OTHER:
case LIBUSB_ERROR_OVERFLOW:
default:
return EIO;
}
}
/* Forward declarations */
static struct iio_context *
usb_create_context_from_args(const struct iio_context_params *params,
const char *args);
static int usb_context_scan(const struct iio_context_params *params,
struct iio_scan *scan, const char *args);
static ssize_t write_data_sync(struct iiod_client_pdata *ep, const char *data,
size_t len, unsigned int timeout_ms);
static ssize_t read_data_sync(struct iiod_client_pdata *ep, char *buf,
size_t len, unsigned int timeout_ms);
static void usb_cancel(struct iiod_client_pdata *io_ctx);
static int usb_io_context_init(struct iiod_client_pdata *io_ctx)
{
io_ctx->lock = iio_mutex_create();
return iio_err(io_ctx->lock);
}
static void usb_io_context_exit(struct iiod_client_pdata *io_ctx)
{
if (io_ctx->lock) {
iio_mutex_destroy(io_ctx->lock);
io_ctx->lock = NULL;
}
}
#define USB_PIPE_CTRL_TIMEOUT 1000 /* These should not take long */
#define IIO_USD_CMD_RESET_PIPES 0
#define IIO_USD_CMD_OPEN_PIPE 1
#define IIO_USD_CMD_CLOSE_PIPE 2
static int usb_reset_pipes(struct iio_context_pdata *pdata)
{
int ret;
/*
int libusb_control_transfer(libusb_device_handle *devh,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int timeout)
*/
ret = libusb_control_transfer(pdata->hdl,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE,
IIO_USD_CMD_RESET_PIPES,
0,
pdata->intrfc,
NULL,
0,
USB_PIPE_CTRL_TIMEOUT);
if (ret < 0)
return -(int) libusb_to_errno(ret);
return 0;
}
static int usb_open_pipe(struct iio_context_pdata *pdata, uint16_t pipe_id)
{
int ret;
/*
libusb_device_handle *devh,
uint8_t bmRequestType,
uint8_t bRequest,
uint16_t wValue,
uint16_t wIndex,
unsigned char *data,
uint16_t wLength,
unsigned int timeout)
*/
ret = libusb_control_transfer(
pdata->hdl,
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE,
IIO_USD_CMD_OPEN_PIPE,
pipe_id,
pdata->intrfc,
NULL,
0,
USB_PIPE_CTRL_TIMEOUT);
if (ret < 0)
return -(int) libusb_to_errno(ret);
return 0;
}
static int usb_close_pipe(struct iio_context_pdata *pdata, uint16_t pipe_id)
{
int ret;
ret = libusb_control_transfer(pdata->hdl, LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_INTERFACE, IIO_USD_CMD_CLOSE_PIPE,
pipe_id, pdata->intrfc, NULL, 0, USB_PIPE_CTRL_TIMEOUT);
if (ret < 0)
return -(int) libusb_to_errno(ret);
return 0;
}
static int usb_reserve_ep_unlocked(const struct iio_device *dev,
struct iiod_client_pdata *io_ctx)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
unsigned int i;
for (i = 0; i < pdata->nb_ep_couples; i++) {
struct iio_usb_ep_couple *ep = &pdata->io_endpoints[i];
if (!ep->in_use) {
ep->in_use = true;
ep->dev = dev;
io_ctx->ep = ep;
return 0;
}
}
return -EBUSY;
}
static void usb_free_ep_unlocked(const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
unsigned int i;
for (i = 0; i < pdata->nb_ep_couples; i++) {
struct iio_usb_ep_couple *ep = &pdata->io_endpoints[i];
if (ep->dev == dev) {
ep->in_use = false;
ep->dev = NULL;
return;
}
}
}
static const struct iiod_client_ops usb_iiod_client_ops = {
.write = write_data_sync,
.read = read_data_sync,
.read_line = read_data_sync,
.cancel = usb_cancel,
};
static ssize_t
usb_read_attr(const struct iio_attr *attr, char *dst, size_t len)
{
const struct iio_device *dev = iio_attr_get_device(attr);
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
struct iiod_client *client = pdata->io_ctx.iiod_client;
return iiod_client_attr_read(client, attr, dst, len);
}
static ssize_t
usb_write_attr(const struct iio_attr *attr, const char *src, size_t len)
{
const struct iio_device *dev = iio_attr_get_device(attr);
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
struct iiod_client *client = pdata->io_ctx.iiod_client;
return iiod_client_attr_write(client, attr, src, len);
}
static int usb_set_timeout(struct iio_context *ctx, unsigned int timeout)
{
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
return iiod_client_set_timeout(pdata->io_ctx.iiod_client, timeout);
}
static const struct iio_device * usb_get_trigger(const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
struct iiod_client *client = pdata->io_ctx.iiod_client;
return iiod_client_get_trigger(client, dev);
}
static int usb_set_trigger(const struct iio_device *dev,
const struct iio_device *trigger)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
struct iiod_client *client = pdata->io_ctx.iiod_client;
return iiod_client_set_trigger(client, dev, trigger);
}
static void usb_shutdown(struct iio_context *ctx)
{
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
iiod_client_destroy(pdata->io_ctx.iiod_client);
/* TODO: free buffers? */
usb_io_context_exit(&pdata->io_ctx);
iio_mutex_destroy(pdata->ep_lock);
free(pdata->io_endpoints);
usb_reset_pipes(pdata); /* Close everything */
libusb_close(pdata->hdl);
libusb_exit(pdata->ctx);
}
static int iio_usb_match_interface(const struct libusb_config_descriptor *desc,
struct libusb_device_handle *hdl, unsigned int intrfc)
{
const struct libusb_interface *iface;
unsigned int i;
if (intrfc >= desc->bNumInterfaces)
return -EINVAL;
iface = &desc->interface[intrfc];
for (i = 0; i < (unsigned int) iface->num_altsetting; i++) {
const struct libusb_interface_descriptor *idesc =
&iface->altsetting[i];
char name[64];
int ret;
if (idesc->iInterface == 0)
continue;
ret = libusb_get_string_descriptor_ascii(hdl, idesc->iInterface,
(unsigned char *) name, sizeof(name));
if (ret < 0)
return -(int) libusb_to_errno(ret);
if (!strcmp(name, IIO_INTERFACE_NAME))
return (int) i;
}
return -EPERM;
}
static int iio_usb_match_device(struct libusb_device *dev,
struct libusb_device_handle *hdl,
unsigned int *intrfc)
{
struct libusb_config_descriptor *desc;
unsigned int i;
int ret;
ret = libusb_get_active_config_descriptor(dev, &desc);
if (ret)
return -(int) libusb_to_errno(ret);
for (i = 0, ret = -EPERM; ret == -EPERM &&
i < desc->bNumInterfaces; i++)
ret = iio_usb_match_interface(desc, hdl, i);
libusb_free_config_descriptor(desc);
if (ret < 0)
return ret;
prm_dbg(NULL, "Found IIO interface on device %u:%u using interface %u\n",
libusb_get_bus_number(dev),
libusb_get_device_address(dev), i - 1);
*intrfc = i - 1;
return ret;
}
static void usb_cancel(struct iiod_client_pdata *io_ctx)
{
iio_mutex_lock(io_ctx->lock);
if (io_ctx->transfer && !io_ctx->cancelled)
libusb_cancel_transfer(io_ctx->transfer);
io_ctx->cancelled = true;
iio_mutex_unlock(io_ctx->lock);
}
static void usb_cancel_buffer(struct iio_buffer_pdata *pdata)
{
usb_cancel(&pdata->io_ctx);
}
static ssize_t
usb_readbuf(struct iio_buffer_pdata *pdata, void *dst, size_t len)
{
return iiod_client_readbuf(pdata->pdata, dst, len);
}
static ssize_t
usb_writebuf(struct iio_buffer_pdata *pdata, const void *src, size_t len)
{
return iiod_client_writebuf(pdata->pdata, src, len);
}
static struct iio_buffer_pdata *
usb_create_buffer(const struct iio_device *dev, unsigned int idx,
struct iio_channels_mask *mask)
{
const struct iio_context *ctx = iio_device_get_context(dev);
const struct iio_context_params *params = iio_context_get_params(ctx);
struct iio_context_pdata *ctx_pdata = iio_context_get_pdata(ctx);
struct iio_buffer_pdata *buf;
int ret;
buf = zalloc(sizeof(*buf));
if (!buf)
return iio_ptr(-ENOMEM);
ret = usb_io_context_init(&buf->io_ctx);
if (ret)
goto err_free_buf;
iio_mutex_lock(ctx_pdata->ep_lock);
buf->dev = dev;
buf->io_ctx.cancelled = false;
buf->io_ctx.ctx_pdata = ctx_pdata;
ret = usb_reserve_ep_unlocked(dev, &buf->io_ctx);
if (ret)
goto err_unlock;
ret = usb_open_pipe(ctx_pdata, buf->io_ctx.ep->pipe_id);
if (ret) {
dev_perror(dev, ret, "Failed to open pipe");
goto err_free_ep;
}
buf->io_ctx.iiod_client = iiod_client_new(params, &buf->io_ctx,
&usb_iiod_client_ops);
ret = iio_err(buf->io_ctx.iiod_client);
if (ret) {
dev_perror(dev, ret, "Failed to created iiod-client");
goto err_close_pipe;
}
buf->pdata = iiod_client_create_buffer(buf->io_ctx.iiod_client,
ctx_pdata->io_ctx.iiod_client,
dev, idx, mask);
ret = iio_err(buf->pdata);
if (ret) {
dev_perror(dev, ret, "Unable to create iiod-client buffer");
goto err_free_iiod_client;
}
iio_mutex_unlock(ctx_pdata->ep_lock);
return buf;
err_free_iiod_client:
iiod_client_destroy(buf->io_ctx.iiod_client);
err_close_pipe:
usb_close_pipe(ctx_pdata, buf->io_ctx.ep->pipe_id);
err_free_ep:
usb_free_ep_unlocked(dev);
err_unlock:
iio_mutex_unlock(ctx_pdata->ep_lock);
usb_io_context_exit(&buf->io_ctx);
err_free_buf:
free(buf);
return iio_ptr(ret);
}
static void usb_free_buffer(struct iio_buffer_pdata *buf)
{
const struct iio_context *ctx = iio_device_get_context(buf->dev);
struct iio_context_pdata *ctx_pdata = iio_context_get_pdata(ctx);
iiod_client_free_buffer(buf->pdata);
iio_mutex_lock(ctx_pdata->ep_lock);
usb_close_pipe(ctx_pdata, buf->io_ctx.ep->pipe_id);
usb_free_ep_unlocked(buf->dev);
iio_mutex_unlock(ctx_pdata->ep_lock);
iiod_client_destroy(buf->io_ctx.iiod_client);
usb_io_context_exit(&buf->io_ctx);
free(buf);
}
static int usb_enable_buffer(struct iio_buffer_pdata *pdata,
size_t nb_samples, bool enable, bool cyclic)
{
return iiod_client_enable_buffer(pdata->pdata, nb_samples, enable, cyclic);
}
static struct iio_block_pdata *
usb_create_block(struct iio_buffer_pdata *pdata, size_t size, void **data)
{
return iiod_client_create_block(pdata->pdata, size, data);
}
static struct iio_event_stream_pdata *
usb_open_events_fd(const struct iio_device *dev)
{
const struct iio_context *ctx = iio_device_get_context(dev);
struct iio_context_pdata *pdata = iio_context_get_pdata(ctx);
return iiod_client_open_event_stream(pdata->io_ctx.iiod_client, dev);
}
static const struct iio_backend_ops usb_ops = {
.scan = usb_context_scan,
.create = usb_create_context_from_args,
.read_attr = usb_read_attr,
.write_attr = usb_write_attr,
.get_trigger = usb_get_trigger,
.set_trigger = usb_set_trigger,
.set_timeout = usb_set_timeout,
.shutdown = usb_shutdown,
.create_buffer = usb_create_buffer,
.free_buffer = usb_free_buffer,
.enable_buffer = usb_enable_buffer,
.cancel_buffer = usb_cancel_buffer,
.readbuf = usb_readbuf,
.writebuf = usb_writebuf,
.create_block = usb_create_block,
.free_block = iiod_client_free_block,
.enqueue_block = iiod_client_enqueue_block,
.dequeue_block = iiod_client_dequeue_block,
.open_ev = usb_open_events_fd,
.close_ev = iiod_client_close_event_stream,
.read_ev = iiod_client_read_event,
};
__api_export_if(WITH_USB_BACKEND_DYNAMIC)
const struct iio_backend iio_usb_backend = {
.api_version = IIO_BACKEND_API_V1,
.name = "usb",
.uri_prefix = "usb:",
.ops = &usb_ops,
.default_timeout_ms = 5000,
};
static void LIBUSB_CALL sync_transfer_cb(struct libusb_transfer *transfer)
{
int *completed = transfer->user_data;
*completed = 1;
}
static int usb_sync_transfer(struct iio_context_pdata *pdata,
struct iiod_client_pdata *io_ctx,
unsigned int ep_type, char *data, size_t len,
int *transferred, unsigned int timeout_ms)
{
unsigned char ep;
struct libusb_transfer *transfer = NULL;
int completed = 0;
int ret;
/*
* If the size of the data to transfer is too big, the
* IOCTL_USBFS_SUBMITURB ioctl (called by libusb) might fail with
* errno set to ENOMEM, as the kernel might use contiguous allocation
* for the URB if the driver doesn't support scatter-gather.
* To prevent that, we support URBs of 1 MiB maximum. The iiod-client
* code will handle this properly and ask for a new transfer.
*/
if (len > 1 * 1024 * 1024)
len = 1 * 1024 * 1024;
if (ep_type == LIBUSB_ENDPOINT_IN)
ep = io_ctx->ep->addr_in;
else
ep = io_ctx->ep->addr_out;
/*
* For cancellation support the check whether the buffer has already been
* cancelled and the allocation as well as the assignment of the new
* transfer needs to happen in one atomic step. Otherwise it is possible
* that the cancellation is missed and transfer is not aborted.
*/
iio_mutex_lock(io_ctx->lock);
if (io_ctx->cancelled) {
ret = -EBADF;
goto unlock;
}
transfer = libusb_alloc_transfer(0);
if (!transfer) {
ret = -ENOMEM;
goto unlock;
}
transfer->user_data = &completed;
libusb_fill_bulk_transfer(transfer, pdata->hdl, ep,
(unsigned char *) data, (int) len, sync_transfer_cb,
&completed, timeout_ms);
transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
ret = libusb_submit_transfer(transfer);
if (ret) {
ret = -(int) libusb_to_errno(ret);
libusb_free_transfer(transfer);
goto unlock;
}
io_ctx->transfer = transfer;
unlock:
iio_mutex_unlock(io_ctx->lock);
if (ret)
return ret;
while (!completed) {
ret = libusb_handle_events_completed(pdata->ctx, &completed);
if (ret < 0) {
if (ret == LIBUSB_ERROR_INTERRUPTED)
continue;
libusb_cancel_transfer(transfer);
continue;
}
}
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
*transferred = transfer->actual_length;
ret = 0;
break;
case LIBUSB_TRANSFER_TIMED_OUT:
ret = -ETIMEDOUT;
break;
case LIBUSB_TRANSFER_STALL:
ret = -EPIPE;
break;
case LIBUSB_TRANSFER_NO_DEVICE:
ret = -ENODEV;
break;
case LIBUSB_TRANSFER_CANCELLED:
ret = -EBADF;
break;
default:
ret = -EIO;
break;
}
/* Same as above. This needs to be atomic in regards to usb_cancel(). */
iio_mutex_lock(io_ctx->lock);
io_ctx->transfer = NULL;
iio_mutex_unlock(io_ctx->lock);
libusb_free_transfer(transfer);
return ret;
}
static ssize_t write_data_sync(struct iiod_client_pdata *ep,
const char *data, size_t len,
unsigned int timeout_ms)
{
int transferred, ret;
ret = usb_sync_transfer(ep->ctx_pdata, ep, LIBUSB_ENDPOINT_OUT,
(char *) data, len, &transferred, timeout_ms);
if (ret)
return ret;
else
return (ssize_t) transferred;
}
static ssize_t read_data_sync(struct iiod_client_pdata *ep,
char *buf, size_t len, unsigned int timeout_ms)
{
int transferred, ret;
ret = usb_sync_transfer(ep->ctx_pdata, ep, LIBUSB_ENDPOINT_IN,
buf, len, &transferred, timeout_ms);
if (ret)
return ret;
else
return transferred;
}
static int usb_verify_eps(const struct libusb_interface_descriptor *iface)
{
unsigned int i, eps = iface->bNumEndpoints;
/* Check that we have an even number of endpoints, and that input/output
* endpoints are interleaved */
if (eps < 2 || eps % 2)
return -EINVAL;
for (i = 0; i < eps; i += 2) {
if (!(iface->endpoint[i + 0].bEndpointAddress
& LIBUSB_ENDPOINT_IN))
return -EINVAL;
if (iface->endpoint[i + 1].bEndpointAddress
& LIBUSB_ENDPOINT_IN)
return -EINVAL;
}
return 0;
}
static int usb_get_string(libusb_device_handle *hdl, uint8_t idx,
char *buffer, size_t length)
{
int ret;
ret = libusb_get_string_descriptor_ascii(hdl, idx,
(unsigned char *) buffer,
(int) length);
if (ret < 0) {
buffer[0] = '\0';
return -(int) libusb_to_errno(ret);
}
return 0;
}
static int usb_get_description(struct libusb_device_handle *hdl,
const struct libusb_device_descriptor *desc,
char *buffer, size_t length)
{
char manufacturer[64], product[64], serial[64];
ssize_t ret;
manufacturer[0] = '\0';
if (desc->iManufacturer > 0) {
usb_get_string(hdl, desc->iManufacturer,
manufacturer, sizeof(manufacturer));
}
product[0] = '\0';
if (desc->iProduct > 0) {
usb_get_string(hdl, desc->iProduct,
product, sizeof(product));
}
serial[0] = '\0';
if (desc->iSerialNumber > 0) {
usb_get_string(hdl, desc->iSerialNumber,
serial, sizeof(serial));
}
ret = iio_snprintf(buffer, length,
"%04x:%04x (%s %s), serial=%s", desc->idVendor,
desc->idProduct, manufacturer, product, serial);
if (ret < 0)
return (int) ret;
return 0;
}
static struct iio_context *
usb_create_context_with_attrs(libusb_device *usb_dev,
struct iio_context_pdata *pdata)
{
struct libusb_version const *libusb_version = libusb_get_version();
struct libusb_device_descriptor dev_desc;
char vendor[64], product[64], serial[64],
uri[sizeof("usb:127.255.255")],
idVendor[5], idProduct[5], version[4],
lib_version[16], description[256];
const char *attr_names[] = {
"uri",
"usb,vendor",
"usb,product",
"usb,serial",
"usb,idVendor",
"usb,idProduct",
"usb,release",
"usb,libusb",
};
char *attr_values[ARRAY_SIZE(attr_names)] = {
uri,
vendor,
product,
serial,
idVendor,
idProduct,
version,
lib_version,
};
struct iiod_client *client = pdata->io_ctx.iiod_client;
libusb_get_device_descriptor(usb_dev, &dev_desc);
usb_get_description(pdata->hdl, &dev_desc,
description, sizeof(description));
iio_snprintf(uri, sizeof(uri), "usb:%d.%d.%u",
libusb_get_bus_number(usb_dev),
libusb_get_device_address(usb_dev),
(uint8_t)pdata->intrfc);
usb_get_string(pdata->hdl, dev_desc.iManufacturer,
vendor, sizeof(vendor));
usb_get_string(pdata->hdl, dev_desc.iProduct,
product, sizeof(product));
usb_get_string(pdata->hdl, dev_desc.iSerialNumber,
serial, sizeof(serial));
iio_snprintf(idVendor, sizeof(idVendor), "%04hx", dev_desc.idVendor);
iio_snprintf(idProduct, sizeof(idProduct), "%04hx", dev_desc.idProduct);
iio_snprintf(version, sizeof(version), "%1hhx.%1hhx",
(unsigned char)((dev_desc.bcdUSB >> 8) & 0xf),
(unsigned char)((dev_desc.bcdUSB >> 4) & 0xf));
iio_snprintf(lib_version, sizeof(lib_version), "%i.%i.%i.%i%s",
libusb_version->major, libusb_version->minor,
libusb_version->micro, libusb_version->nano,
libusb_version->rc);
return iiod_client_create_context(client,
&iio_usb_backend, description,
attr_names,
(const char **) attr_values,
ARRAY_SIZE(attr_names));
}
static struct iio_context * usb_create_context(const struct iio_context_params *params,
unsigned int bus,
uint16_t address, uint16_t intrfc)
{
libusb_context *usb_ctx;
libusb_device_handle *hdl = NULL;
const struct libusb_interface_descriptor *iface;
libusb_device *usb_dev;
struct libusb_config_descriptor *conf_desc;
libusb_device **device_list;
struct iio_context *ctx;
struct iio_context_pdata *pdata;
uint16_t i;
int ret;
pdata = zalloc(sizeof(*pdata));
if (!pdata) {
prm_err(params, "Unable to allocate pdata\n");
ret = -ENOMEM;
goto err_set_errno;
}
pdata->ep_lock = iio_mutex_create();
ret = iio_err(pdata->ep_lock);
if (ret) {
prm_err(params, "Unable to create mutex\n");
goto err_free_pdata;
}
ret = libusb_init(&usb_ctx);
if (ret) {
ret = -(int) libusb_to_errno(ret);
prm_perror(params, ret, "Unable to init libusb");
goto err_destroy_ep_mutex;
}
ret = (int) libusb_get_device_list(usb_ctx, &device_list);
if (ret < 0) {
ret = -(int) libusb_to_errno(ret);
prm_perror(params, ret, "Unable to get usb device list");
goto err_libusb_exit;
}
usb_dev = NULL;
for (i = 0; device_list[i]; i++) {
libusb_device *dev = device_list[i];
if (bus == libusb_get_bus_number(dev) &&
address == libusb_get_device_address(dev)) {
usb_dev = dev;
ret = libusb_open(usb_dev, &hdl);
/*
* Workaround for libusb on Windows >= 8.1. A device
* might appear twice in the list with one device being
* bogus and only partially initialized. libusb_open()
* returns LIBUSB_ERROR_NOT_SUPPORTED for such devices,
* which should never happen for normal devices. So if
* we find such a device skip it and keep looking.
*/
if (ret == LIBUSB_ERROR_NOT_SUPPORTED) {
prm_warn(params, "Skipping broken USB device. Please upgrade libusb.\n");
usb_dev = NULL;
continue;
}
break;
}
}
libusb_free_device_list(device_list, true);
if (!usb_dev || !hdl) {
ret = -ENODEV;
goto err_libusb_exit;
}
if (ret) {
ret = -(int) libusb_to_errno(ret);
prm_perror(params, ret, "Unable to open device\n");
goto err_libusb_exit;
}
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000016)
libusb_set_auto_detach_kernel_driver(hdl, true);
#endif
ret = libusb_claim_interface(hdl, intrfc);
if (ret) {
ret = -(int) libusb_to_errno(ret);
prm_perror(params, ret, "Unable to claim interface %u:%u:%u",
bus, address, intrfc);
goto err_libusb_close;
}
ret = libusb_get_active_config_descriptor(usb_dev, &conf_desc);
if (ret) {
ret = -(int) libusb_to_errno(ret);
prm_perror(params, ret, "Unable to get config descriptor");
goto err_libusb_close;
}
iface = &conf_desc->interface[intrfc].altsetting[0];
ret = usb_verify_eps(iface);
if (ret) {
prm_perror(params, ret, "Invalid configuration of endpoints");
goto err_free_config_descriptor;
}
pdata->nb_ep_couples = iface->bNumEndpoints / 2;
prm_dbg(params, "Found %hu usable i/o endpoint couples\n",
pdata->nb_ep_couples);
pdata->io_endpoints = calloc(pdata->nb_ep_couples,
sizeof(*pdata->io_endpoints));
if (!pdata->io_endpoints) {
prm_err(params, "Unable to allocate endpoints\n");
ret = -ENOMEM;
goto err_free_config_descriptor;
}
for (i = 0; i < pdata->nb_ep_couples; i++) {
struct iio_usb_ep_couple *ep = &pdata->io_endpoints[i];
ep->addr_in = iface->endpoint[i * 2 + 0].bEndpointAddress;
ep->addr_out = iface->endpoint[i * 2 + 1].bEndpointAddress;
ep->pipe_id = i;
prm_dbg(params, "Couple %i with endpoints 0x%x / 0x%x\n", i,
ep->addr_in, ep->addr_out);
}
pdata->ctx = usb_ctx;
pdata->hdl = hdl;
pdata->intrfc = intrfc;
ret = usb_io_context_init(&pdata->io_ctx);
if (ret)
goto err_free_endpoints;
/* We reserve the first I/O endpoint couple for global operations */
pdata->io_ctx.ep = &pdata->io_endpoints[0];
pdata->io_ctx.ep->in_use = true;
pdata->io_ctx.ctx_pdata = pdata;
ret = usb_reset_pipes(pdata);
if (ret) {
prm_perror(params, ret, "Failed to reset pipes");
goto err_io_context_exit;
}
ret = usb_open_pipe(pdata, 0);
if (ret) {
prm_perror(params, ret, "Failed to open control pipe");
goto err_io_context_exit;
}
pdata->io_ctx.iiod_client = iiod_client_new(params, &pdata->io_ctx,
&usb_iiod_client_ops);
ret = iio_err(pdata->io_ctx.iiod_client);
if (ret) {
prm_perror(params, ret, "Unable to create IIOD client");
goto err_reset_pipes;
}
ctx = usb_create_context_with_attrs(usb_dev, pdata);
ret = iio_err(ctx);
if (ret)
goto err_free_iiod_client;