aboutsummaryrefslogtreecommitdiffstats
path: root/src/usb.c
blob: 3b77b948f44dda3ce3ef6fa226c24041db16394e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
/*
 * usb.c
 *
 * (C) 2019 by Sylvain Munaut <tnt@246tNt.com>
 * (C) 2022 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <talloc.h>

#include <osmocom/core/isdnhdlc.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/bit32gen.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/e1d/proto.h>
#include <osmocom/e1d/proto_srv.h>

#include <libusb.h>

#include "e1d.h"
#include "log.h"
#include "ice1usb_proto.h"


#define USB_VID		0x1d50
#define USB_PID		0x6145
#define USB_PID_TRACER	0x6151

libusb_context *g_usb = NULL;


struct e1_usb_flow;


/* Driver data */

struct e1_usb_line_data {
	/* Interface */
	uint8_t if_num;

	/* End Points */
	uint8_t ep_in;
	uint8_t ep_out;
	uint8_t ep_fb;
	uint8_t ep_int;

	/* Max packet size */
	int pkt_size;

	/* Flow */
	struct e1_usb_flow *flow_in;
	struct e1_usb_flow *flow_out;
	struct e1_usb_flow *flow_fb;

	/* Interrupt */
	struct {
		uint8_t buf[10];
		struct ice1usb_irq_err last_errcnt;
	} irq;

	/* Rate regulation */
	uint32_t r_acc;
	uint32_t r_sw;

	/* list of in-progress CTRL operations */
	struct llist_head ctrl_inprogress;
};

struct e1_usb_intf_data {
	libusb_device_handle *devh;

	struct {
		uint8_t if_num;
		struct osmo_timer_list poll_timer;
		struct e1usb_gpsdo_status last_status;
	} gpsdo;

	/* list of in-progress CTRL operations */
	struct llist_head ctrl_inprogress;
};


/* Flow */

struct e1_usb_flow_entry {
	uint8_t *buf;
	struct libusb_transfer *xfr;
};

/* len == actual length, size == underlying size of buffer */
typedef int (*xfer_cb_t)(struct e1_usb_flow *flow, uint8_t *buf, int len, size_t size);

struct e1_usb_flow {
	struct e1_line *line;
	xfer_cb_t cb;

	uint8_t ep;

	int count;
	int size;
	int ppx;

	struct e1_usb_flow_entry *entries;
};

// ---------------------------------------------------------------------------
// USB data transfer
// ---------------------------------------------------------------------------

static int
e1_usb_xfer_in(struct e1_usb_flow *flow, uint8_t *buf, int len, size_t size)
{
	if (len == 0)
		return 0;

	if (len < 4) {
		LOGPLI(flow->line, DE1D, LOGL_ERROR, "IN EP %02x ISO packet truncated: len = %u\n",
		       flow->ep, len);
		line_ctr_add(flow->line, LINE_CTR_USB_ISO_TRUNC, 1);
		return 0;
	}

	if (len > 4 && (len - 4) % 32) {
		/* some ISO IN packet was truncated. Apparently this
		 * does happen, see https://osmocom.org/issues/5490 -
		 * there is little we can do here, but instead of the
		 * earlier ASSERT, we just feed some garbage for the
		 * last few timeslots, resulting in bit errors etc. */
		LOGPLI(flow->line, DE1D, LOGL_ERROR, "IN EP %02x ISO packet truncated: len-4 = %u\n",
		       flow->ep, len - 4);
		line_ctr_add(flow->line, LINE_CTR_USB_ISO_TRUNC, 1);

		/* The assumption here is that only the last E1 frame is
		 * truncated, as we have no idea how many E1 frames the
		 * USB device firmware wanted to send us. */
		len += 32 - (len % 32);
		/* don't overflow the underlying buffer */
		if (len > (int) size)
			len = size;
	}

	return e1_line_demux_in(flow->line, buf + 4, len - 4, buf[3] & 0xf);
}

static int
e1_usb_xfer_out(struct e1_usb_flow *flow, uint8_t *buf, int len, size_t size)
{
	struct e1_line *line = flow->line;
	struct e1_usb_line_data *ld = (struct e1_usb_line_data *) line->drv_data;
	int fm, fts;

	if (len <= 0) {
		LOGPLI(line, DXFR, LOGL_ERROR, "OUT ERROR: %d\n", len);
		return -1;
	}

	/* Flow regulation */
	ld->r_acc += ld->r_sw;

	fm = (ld->pkt_size - 4) / 32;
	fts = ld->r_acc >> 10;
	if      (fts <  4) fts = 4;
	else if (fts > fm) fts = fm;

	ld->r_acc -= fts << 10;
	if (ld->r_acc & 0x80000000)
		ld->r_acc = 0;

	memset(buf, 0xff, 4);

	return e1_line_mux_out(line, buf+4, fts) + 4;
}

static int
e1_usb_xfer_fb(struct e1_usb_flow *flow, uint8_t *buf, int len, size_t size)
{
	struct e1_usb_line_data *ld = (struct e1_usb_line_data *) flow->line->drv_data;

	if (len < 0) {
		LOGPLI(flow->line, DE1D, LOGL_ERROR, "Feedback transfer error\n");
		return 0;
	} else if (len != 3) {
		LOGPLI(flow->line, DE1D, LOGL_ERROR, "Feedback packet invalid len (%d)\n", len);
		return 0;
	}

	ld->r_sw = (buf[2] << 16) | (buf[1] << 8) | buf[0];

	return 0;
}


// ---------------------------------------------------------------------------
// USB flow
// ---------------------------------------------------------------------------

/* strings for enum libusb_transfer_status */
static const struct value_string libusb_status_str[] = {
	{ LIBUSB_TRANSFER_COMPLETED,	"COMPLETED" },
	{ LIBUSB_TRANSFER_ERROR,	"ERROR" },
	{ LIBUSB_TRANSFER_TIMED_OUT,	"TIMED_OUT" },
	{ LIBUSB_TRANSFER_CANCELLED,	"CANCELLED" },
	{ LIBUSB_TRANSFER_STALL,	"STALL" },
	{ LIBUSB_TRANSFER_NO_DEVICE,	"NO_DEVICE" },
	{ LIBUSB_TRANSFER_OVERFLOW,	"OVERFLOW" },
	{ 0, NULL }
};

static void LIBUSB_CALL
_e1uf_xfr(struct libusb_transfer *xfr)
{
	struct e1_usb_flow *flow = (struct e1_usb_flow *) xfr->user_data;
	struct e1_usb_intf_data *id = (struct e1_usb_intf_data *) flow->line->intf->drv_data;
	int j, rv, len;

	len = 0;

	/* FIXME: Check transfer status ? Error handling ? */


	if (flow->ep & 0x80) {
		for (j = 0; j < flow->ppx; j++) {
			struct libusb_iso_packet_descriptor *iso_pd = &xfr->iso_packet_desc[j];
			if (iso_pd->status != LIBUSB_TRANSFER_COMPLETED) {
				LOGPLI(flow->line, DE1D, LOGL_ERROR, "IN EP %02x ISO packet %d failed with status %s\n",
					flow->ep, j, get_value_string(libusb_status_str, iso_pd->status));
			}
			flow->cb(flow,
				libusb_get_iso_packet_buffer_simple(xfr, j),
				(iso_pd->status == LIBUSB_TRANSFER_COMPLETED) ?  (int)iso_pd->actual_length : -1,
				iso_pd->length
			);
			len += (iso_pd->length = flow->size);
		}
	} else {
		for (j = 0; j < flow->ppx; j++) {
			struct libusb_iso_packet_descriptor *iso_pd = &xfr->iso_packet_desc[j];
			if (iso_pd->status != LIBUSB_TRANSFER_COMPLETED) {
				LOGPLI(flow->line, DE1D, LOGL_ERROR, "OUT EP %02x ISO packet %d failed with status %s\n",
					flow->ep, j, get_value_string(libusb_status_str, iso_pd->status));
			}
			len += (iso_pd->length = flow->cb(flow, &xfr->buffer[len], flow->size, flow->size));
		}
	}

	libusb_fill_iso_transfer(xfr, id->devh, flow->ep,
		xfr->buffer, len, flow->ppx,
		_e1uf_xfr, flow, 0
	);

	rv = libusb_submit_transfer(xfr);
	if (rv) {
		LOGPLI(flow->line, DE1D, LOGL_ERROR, "EP %02x Failed to resubmit buffer for transfer: %s\n",
		       flow->ep, libusb_strerror(rv));
	}
}

static struct e1_usb_flow *
e1uf_create(struct e1_line *line, xfer_cb_t cb,
            int ep, int count, int size, int ppx)
{
	void *ctx = line->intf->e1d->ctx;
	struct e1_usb_flow *flow;

	flow = talloc_zero(ctx, struct e1_usb_flow);
	OSMO_ASSERT(flow);

	flow->line  = line;
	flow->cb    = cb;
	flow->ep    = ep;
	flow->count = count;
	flow->size  = size;
	flow->ppx   = ppx;
	flow->entries = talloc_zero_size(ctx, count * sizeof(struct e1_usb_flow_entry));

	for (int i = 0; i < count; i++)
		flow->entries[i].buf = talloc_zero_size(ctx, size * ppx);

	return flow;
}

static void __attribute__((unused))
e1uf_destroy(struct e1_usb_flow *flow)
{
	if (!flow)
		return;

	/* FIXME: stop pending transfers */
	for (int i = 0; i < flow->count; i++)
		talloc_free(flow->entries[i].buf);

	talloc_free(flow->entries);
	talloc_free(flow);
}

static int
e1uf_start(struct e1_usb_flow *flow)
{
	struct e1_usb_intf_data *id = (struct e1_usb_intf_data *) flow->line->intf->drv_data;
	struct libusb_transfer *xfr;
	int i, j, rv, len;

	for (i = 0; i < flow->count; i++) {
		xfr = libusb_alloc_transfer(flow->ppx);
		if (!xfr)
			return -ENOMEM;

		len = 0;

		if (flow->ep & 0x80) {
			for (j = 0; j < flow->ppx; j++)
				len += (xfr->iso_packet_desc[j].length = flow->size);
		} else {
			for (j = 0; j < flow->ppx; j++)
				len += (xfr->iso_packet_desc[j].length = flow->cb(flow, &flow->entries[i].buf[len], flow->size, flow->size));
		}

		libusb_fill_iso_transfer(xfr, id->devh, flow->ep,
			flow->entries[i].buf, len, flow->ppx,
			_e1uf_xfr, flow, 0
		);

		rv = libusb_submit_transfer(xfr);
		if (rv) {
			LOGPLI(flow->line, DE1D, LOGL_ERROR, "EP %02x: Error submitting transfer %d: %s\n",
			       flow->ep, i, libusb_strerror(rv));
			return rv;
		}

		flow->entries[i].xfr = xfr;
	}

	return 0;
}

// ---------------------------------------------------------------------------
// USB interrupt
// ---------------------------------------------------------------------------

static int resubmit_irq(struct e1_line *line);

/* compute how much advanced 'cur' is copared to 'prev', in modulo-0xffff for wraps */
static uint32_t delta_mod_u16(uint32_t cur, uint32_t prev)
{
	return ((cur + 0xffff) - prev) % 0xffff;
}

static void rx_interrupt_errcnt(struct e1_line *line, const struct ice1usb_irq_err *errcnt)
{
	struct e1_usb_line_data *ld = (struct e1_usb_line_data *) line->drv_data;
	struct ice1usb_irq_err *last = &ld->irq.last_errcnt;

	if (errcnt->crc != last->crc) {
		LOGPLI(line, DE1D, LOGL_ERROR, "CRC error count %d (was %d)\n",
			errcnt->crc, last->crc);
		line_ctr_add(line, LINE_CTR_CRC_ERR, delta_mod_u16(errcnt->crc, last->crc));
	}

	if (errcnt->align != last->align) {
		LOGPLI(line, DE1D, LOGL_ERROR, "ALIGNMENT error count %d (was %d)\n",
			errcnt->align, last->align);
		line_ctr_add(line, LINE_CTR_LOA, delta_mod_u16(errcnt->align, last->align));
	}

	if (errcnt->ovfl != last->ovfl) {
		LOGPLI(line, DE1D, LOGL_ERROR, "OVERFLOW error count %d (was %d)\n",
			errcnt->ovfl, last->ovfl);
		line_ctr_add(line, LINE_CTR_RX_OVFL, delta_mod_u16(errcnt->ovfl, last->ovfl));
	}

	if (errcnt->unfl != last->unfl) {
		LOGPLI(line, DE1D, LOGL_ERROR, "UNDERFLOW error count %d (was %d)\n",
			errcnt->unfl, last->unfl);
		line_ctr_add(line, LINE_CTR_TX_UNFL, delta_mod_u16(errcnt->unfl, last->unfl));
	}

	if ((errcnt->flags & ICE1USB_ERR_F_ALIGN_ERR) != (last->flags & ICE1USB_ERR_F_ALIGN_ERR)) {
		LOGPLI(line, DE1D, LOGL_ERROR, "ALIGNMENT %s\n",
			errcnt->flags & ICE1USB_ERR_F_ALIGN_ERR ? "LOST" : "REGAINED");
		if ((errcnt->flags & ICE1USB_ERR_F_ALIGN_ERR)) {
			osmo_e1dp_server_event(line->intf->e1d->srv, E1DP_EVT_LOF_ON,
					       line->intf->id, line->id, 0, NULL, 0);
		} else {
			osmo_e1dp_server_event(line->intf->e1d->srv, E1DP_EVT_LOF_OFF,
					       line->intf->id, line->id, 0, NULL, 0);
		}
	}

	if ((errcnt->flags & ICE1USB_ERR_F_LOS) != (last->flags & ICE1USB_ERR_F_LOS)) {
		LOGPLI(line, DE1D, LOGL_ERROR, "Rx Clock %s\n",
			errcnt->flags & ICE1USB_ERR_F_LOS ? "LOST" : "REGAINED");
		if (errcnt->flags & ICE1USB_ERR_F_LOS)
			line_ctr_add(line, LINE_CTR_LOS, 1);
	}

	if ((errcnt->flags & ICE1USB_ERR_F_RAI) != (last->flags & ICE1USB_ERR_F_RAI)) {
		LOGPLI(line, DE1D, LOGL_ERROR, "Remote Alarm (YELLOW) %s\n",
			errcnt->flags & ICE1USB_ERR_F_RAI ? "PRESENT" : "ABSENT");
		/* don't increment counter here, our TS0 code in mux_demux.c does this */
	}

	ld->irq.last_errcnt = *errcnt;
}

static void interrupt_ep_cb(struct libusb_transfer *xfer)
{
	struct e1_line *line = (struct e1_line *) xfer->user_data;
	const struct ice1usb_irq *irq = (const struct ice1usb_irq *) xfer->buffer;

	if (xfer->status != LIBUSB_TRANSFER_COMPLETED) {
		LOGPLI(line, DE1D, LOGL_ERROR, "INT EP %02x transfer failed with status %s\n",
			xfer->endpoint, get_value_string(libusb_status_str, xfer->status));
		goto out;
	}

	if (!xfer->actual_length) {
		LOGPLI(line, DE1D, LOGL_ERROR, "Zero-Length Interrupt transfer\n");
		goto out;
	}

	switch (irq->type) {
	case ICE1USB_IRQ_T_ERRCNT:
		if (xfer->actual_length < (int)sizeof(*irq)) {
			LOGPLI(line, DE1D, LOGL_ERROR, "Short ERRCNT interrupt: %u<%zu\n",
				xfer->actual_length, sizeof(*irq));
			break;
		}
		rx_interrupt_errcnt(line, &irq->u.errors);
		break;
	default:
		LOGPLI(line, DE1D, LOGL_INFO, "Unsupported interrupt 0x%02x\n", irq->type);
		break;
	}

out:
	resubmit_irq(line);
}

static int resubmit_irq(struct e1_line *line)
{
	struct e1_usb_line_data *ld = (struct e1_usb_line_data *) line->drv_data;
	struct e1_usb_intf_data *id = (struct e1_usb_intf_data *) line->intf->drv_data;
	struct libusb_transfer *xfr = libusb_alloc_transfer(0);
	int rv;

	libusb_fill_interrupt_transfer(xfr, id->devh, ld->ep_int, ld->irq.buf, sizeof(ld->irq.buf),
					interrupt_ep_cb, line, 0);
	rv = libusb_submit_transfer(xfr);
	if (rv != LIBUSB_SUCCESS) {
		LOGPLI(line, DE1D, LOGL_ERROR, "EP %02x: Error submitting IRQ transfer: %s\n",
			ld->ep_int, libusb_strerror(rv));
	}

	return rv;
}

// ---------------------------------------------------------------------------
// Control transfers (USB interface == E1 line level)
// ---------------------------------------------------------------------------

struct e1_usb_ctrl_xfer {
	struct e1_line *line;
	struct llist_head list;
	/* 8 bytes control setup packet, remainder for data */
	uint8_t buffer[8 + 8];
};


static void
ctrl_xfer_compl_cb(struct libusb_transfer *xfr)
{
	struct e1_usb_ctrl_xfer *ucx = xfr->user_data;

	switch (xfr->status) {
	case LIBUSB_TRANSFER_COMPLETED:
		LOGPLI(ucx->line, DE1D, LOGL_INFO, "CTRL transfer completed successfully\n");
		break;
	default:
		LOGPLI(ucx->line, DE1D, LOGL_ERROR, "CTRL transfer completed unsuccessfully %d\n",
			xfr->status);
		break;
	}
	llist_del(&ucx->list);
	talloc_free(ucx);
	libusb_free_transfer(xfr);
}

// ---------------------------------------------------------------------------
// Control transfers (USB device == E1 interface level)
// ---------------------------------------------------------------------------

/* generic helper for async transmission of control endpoint requests */
static int
_e1_usb_line_send_ctrl(struct e1_line *line, uint8_t bmReqType, uint8_t bReq, uint16_t wValue,
		       const uint8_t *data, size_t data_len)
{
	struct e1_usb_ctrl_xfer *ucx = talloc_zero(line, struct e1_usb_ctrl_xfer);
	struct e1_usb_line_data *ld = (struct e1_usb_line_data *) line->drv_data;
	struct e1_usb_intf_data *id = (struct e1_usb_intf_data *) line->intf->drv_data;
	struct libusb_transfer *xfr;
	int rc;

	if (!ucx)
		return -ENOMEM;

	OSMO_ASSERT(sizeof(ucx->buffer) >= 8+data_len);
	ucx->line = line;
	libusb_fill_control_setup(ucx->buffer, bmReqType, bReq, wValue, ld->if_num, data_len);
	if (data && data_len)
		memcpy(ucx->buffer+8, data, data_len);

	xfr = libusb_alloc_transfer(0);
	if (!xfr) {
		rc = -ENOMEM;
		goto free_ucx;
	}

	libusb_fill_control_transfer(xfr, id->devh, ucx->buffer, ctrl_xfer_compl_cb, ucx, 3000);
	rc = libusb_submit_transfer(xfr);
	if (rc != 0) {
		LOGPLI(line, DE1D, LOGL_ERROR, "Error submitting control transfer: %s\n",
			libusb_strerror(rc));
		goto free_xfr;
	}

	llist_add_tail(&ucx->list, &ld->ctrl_inprogress);

	return 0;

free_xfr:
	libusb_free_transfer(xfr);
free_ucx:
	talloc_free(ucx);

	return rc;
}

int
e1_usb_ctrl_set_tx_cfg(struct e1_line *line, enum ice1usb_tx_mode mode, enum ice1usb_tx_timing timing,
			enum ice1usb_tx_ext_loopback ext_loop, uint8_t alarm)
{
	const uint16_t bmReqType = LIBUSB_RECIPIENT_INTERFACE | LIBUSB_REQUEST_TYPE_VENDOR |
				   LIBUSB_ENDPOINT_OUT;
	struct ice1usb_tx_config tx_cfg = {
		.mode = mode,
		.timing = timing,
		.ext_loopback = ext_loop,
		.alarm = alarm,
	};

	return _e1_usb_line_send_ctrl(line, bmReqType, ICE1USB_INTF_SET_TX_CFG, 0, (uint8_t *)&tx_cfg,
				      sizeof(tx_cfg));
}

int
e1_usb_ctrl_set_rx_cfg(struct e1_line *line, enum ice1usb_rx_mode mode)
{
	const uint16_t bmReqType = LIBUSB_RECIPIENT_INTERFACE | LIBUSB_REQUEST_TYPE_VENDOR |
				   LIBUSB_ENDPOINT_OUT;
	struct ice1usb_rx_config rx_cfg = {
		.mode = mode,
	};

	return _e1_usb_line_send_ctrl(line, bmReqType, ICE1USB_INTF_SET_RX_CFG, 0, (uint8_t *)&rx_cfg,
				      sizeof(rx_cfg));
}

struct e1_usb_ctrl_xfer_intf {
	struct e1_intf *intf;
	struct llist_head list;
	/* 8 bytes control setup packet, remainder for data */
	uint8_t buffer[8 + sizeof(struct e1usb_gpsdo_status)];
};

static void _e1_usb_intf_gpsdo_status_cb(struct e1_intf *intf, const uint8_t *data, size_t len);

static void
ctrl_xfer_intf_compl_cb(struct libusb_transfer *xfr)
{
	struct e1_usb_ctrl_xfer_intf *ucx = xfr->user_data;
	struct libusb_control_setup *setup;

	switch (xfr->status) {
	case LIBUSB_TRANSFER_COMPLETED:
		setup = (struct libusb_control_setup *) ucx->buffer;
		LOGPIF(ucx->intf, DE1D, LOGL_DEBUG, "CTRL transfer completed successfully: %s\n",
			osmo_hexdump(ucx->buffer, 8+xfr->actual_length));
		switch (setup->bRequest) {
		case ICE1USB_INTF_GET_GPSDO_STATUS:
			_e1_usb_intf_gpsdo_status_cb(ucx->intf, ucx->buffer+8, xfr->actual_length);
			break;
		default:
			break;
		}
		break;
	default:
		LOGPIF(ucx->intf, DE1D, LOGL_ERROR, "CTRL transfer completed unsuccessfully %d\n",
			xfr->status);
		break;
	}
	llist_del(&ucx->list);
	talloc_free(ucx);
	libusb_free_transfer(xfr);
}


/* generic helper for async transmission of control endpoint requests */
static int
_e1_usb_intf_send_ctrl(struct e1_intf *intf, uint8_t bmReqType, uint8_t bReq, uint16_t wValue,
		       const uint8_t *data, size_t data_len)
{
	struct e1_usb_ctrl_xfer_intf *ucx = talloc_zero(intf, struct e1_usb_ctrl_xfer_intf);
	struct e1_usb_intf_data *id = (struct e1_usb_intf_data *) intf->drv_data;
	struct libusb_transfer *xfr;
	int rc;

	if (!ucx)
		return -ENOMEM;

	OSMO_ASSERT(sizeof(ucx->buffer) >= 8+data_len);
	ucx->intf = intf;
	libusb_fill_control_setup(ucx->buffer, bmReqType, bReq, wValue, id->gpsdo.if_num, data_len);
	if (data && data_len)
		memcpy(ucx->buffer+8, data, data_len);

	xfr = libusb_alloc_transfer(0);
	if (!xfr) {
		rc = -ENOMEM;
		goto free_ucx;
	}

	libusb_fill_control_transfer(xfr, id->devh, ucx->buffer, ctrl_xfer_intf_compl_cb, ucx, 3000);
	rc = libusb_submit_transfer(xfr);
	if (rc != 0) {
		LOGPIF(intf, DE1D, LOGL_ERROR, "Error submitting control transfer: %s\n",
			libusb_strerror(rc));
		goto free_xfr;
	}

	llist_add_tail(&ucx->list, &id->ctrl_inprogress);

	return 0;

free_xfr:
	libusb_free_transfer(xfr);
free_ucx:
	talloc_free(ucx);

	return rc;
}

int
e1_usb_ctrl_set_gpsdo_mode(struct e1_intf *intf, enum ice1usb_gpsdo_mode gpsdo_mode)
{
	const uint16_t bmReqType = LIBUSB_RECIPIENT_INTERFACE | LIBUSB_REQUEST_TYPE_VENDOR |
				   LIBUSB_ENDPOINT_OUT;
	return _e1_usb_intf_send_ctrl(intf, bmReqType, ICE1USB_INTF_SET_GPSDO_MODE, gpsdo_mode,
				      NULL, 0);
}

int
e1_usb_ctrl_set_gpsdo_tune(struct e1_intf *intf, const struct e1usb_gpsdo_tune *gpsdo_tune)
{
	const uint16_t bmReqType = LIBUSB_RECIPIENT_INTERFACE | LIBUSB_REQUEST_TYPE_VENDOR |
				   LIBUSB_ENDPOINT_OUT;
	return _e1_usb_intf_send_ctrl(intf, bmReqType, ICE1USB_INTF_SET_GPSDO_TUNE, 0,
				      (uint8_t *)gpsdo_tune, sizeof(*gpsdo_tune));
}

int
e1_usb_ctrl_get_gpsdo_status(struct e1_intf *intf)
{
	const uint16_t bmReqType = LIBUSB_RECIPIENT_INTERFACE | LIBUSB_REQUEST_TYPE_VENDOR |
				   LIBUSB_ENDPOINT_IN;
	return _e1_usb_intf_send_ctrl(intf, bmReqType, ICE1USB_INTF_GET_GPSDO_STATUS, 0,
				      NULL, sizeof(struct e1usb_gpsdo_status));
}

// ---------------------------------------------------------------------------
// GPS-DO
// ---------------------------------------------------------------------------

static const struct value_string ice1usb_gpsdo_mode_str[] = {
	{ ICE1USB_GPSDO_MODE_DISABLED,		"DISABLED" },
	{ ICE1USB_GPSDO_MODE_AUTO,		"AUTO" },
	{ 0, NULL }
};

static const struct value_string ice1usb_gpsdo_antenna_state_str[] = {
	{ ICE1USB_GPSDO_ANT_UNKNOWN,		"UNKNOWN" },
	{ ICE1USB_GPSDO_ANT_OK,			"OK" },
	{ ICE1USB_GPSDO_ANT_OPEN,		"OPEN" },
	{ ICE1USB_GPSDO_ANT_SHORT,		"SHORT" },
	{ 0, NULL }
};

static const struct value_string ice1usb_gpsdo_state_str[] = {
	{ ICE1USB_GPSDO_STATE_DISABLED,		"DISABLED" },
	{ ICE1USB_GPSDO_STATE_CALIBRATE,	"CALIBRATE" },
	{ ICE1USB_GPSDO_STATE_HOLD_OVER,	"HOLD_OVER" },
	{ ICE1USB_GPSDO_STATE_TUNE_COARSE,	"TUNE_COARSE" },
	{ ICE1USB_GPSDO_STATE_TUNE_FINE,	"TUNE_FINE" },
	{ 0, NULL }
};

int
e1_usb_intf_gpsdo_state_string(char *buf, size_t len, const struct e1_intf *intf)
{
	struct e1_usb_intf_data *id = intf->drv_data;
	struct e1usb_gpsdo_status *last_st = &id->gpsdo.last_status;

	OSMO_ASSERT(intf->drv == E1_DRIVER_USB);

	if (!id) {
		/* This can happen for statically configured devices (config/vty) which are not
		 * currently present */
		return snprintf(buf, len, "unknown");
	}

	return snprintf(buf, len, "mode=%s, fix=%s, state=%s antenna=%s, tune=%u/%u, freq_est=%u",
			get_value_string(ice1usb_gpsdo_mode_str, last_st->mode),
			last_st->valid_fix ? "TRUE" : "FALSE",
			get_value_string(ice1usb_gpsdo_state_str, last_st->state),
			get_value_string(ice1usb_gpsdo_antenna_state_str, last_st->antenna_state),
			libusb_le16_to_cpu(last_st->tune.coarse), libusb_le16_to_cpu(last_st->tune.fine),
			osmo_load32le(&last_st->freq_est));
}

static void
_e1_usb_intf_gpsdo_status_cb(struct e1_intf *intf, const uint8_t *data, size_t len)
{
	struct e1_usb_intf_data *id = intf->drv_data;
	struct e1usb_gpsdo_status *last_st = &id->gpsdo.last_status;
	struct e1usb_gpsdo_status _st, *st = &_st;
	struct e1_line *line;

	if (len < sizeof(*st)) {
		/*
		 * Because some fields can be added to the structure by newer
		 * firmware revisions, this means we can potentially get a shorter
		 * struct than what we asked for. We simply set those fields to
		 * zero.
		 *
		 * The opposite case (newer firmware than e1d) means the structure
		 * could be larger, but because we limit the wLength to the struct
		 * we know, we can't receive a larger one and the new fields are
		 * just ignored by this e1d version
		 */
		LOGPIF(intf, DE1D, LOGL_DEBUG,
			"GPSDO status %zu < %zu ! Firmware probably outdated. "
			"Some values will be zeroed\n",
			len, sizeof(*st));
	}

	memset(st, 0x00, sizeof(*st));
	memcpy(st, data, len);

	if (st->state != last_st->state) {
		LOGPIF(intf, DE1D, LOGL_NOTICE, "GPSDO state change: %s -> %s\n",
			get_value_string(ice1usb_gpsdo_state_str, last_st->state),
			get_value_string(ice1usb_gpsdo_state_str, st->state));
	}

	if (st->antenna_state != last_st->antenna_state) {
		int level = LOGL_NOTICE;
		switch (st->antenna_state) {
		case ICE1USB_GPSDO_ANT_OPEN:
		case ICE1USB_GPSDO_ANT_SHORT:
			level = LOGL_ERROR;
			break;
		default:
			level = LOGL_NOTICE;
		}
		LOGPIF(intf, DE1D, level, "GPS antenna status change: %s -> %s\n",
			get_value_string(ice1usb_gpsdo_antenna_state_str, last_st->antenna_state),
			get_value_string(ice1usb_gpsdo_antenna_state_str, st->antenna_state));
	}

	if (st->valid_fix != last_st->valid_fix) {
		if (st->valid_fix)
			LOGPIF(intf, DE1D, LOGL_NOTICE, "GPS Fix achieved\n");
		else
			LOGPIF(intf, DE1D, LOGL_ERROR, "GPS Fix LOST\n");
	}

	/* update stat_items for statsd / monitoring */
	llist_for_each_entry(line, &intf->lines, list) {
		line_stat_set(line, LINE_GPSDO_STATE, st->state);
		line_stat_set(line, LINE_GPSDO_ANTENNA, st->antenna_state);
		line_stat_set(line, LINE_GPSDO_TUNE_COARSE, libusb_le16_to_cpu(st->tune.coarse));
		line_stat_set(line, LINE_GPSDO_TUNE_FINE, libusb_le16_to_cpu(st->tune.fine));
		line_stat_set(line, LINE_GPSDO_FREQ_EST, osmo_load32le(&st->freq_est));
		line_stat_set(line, LINE_GPSDO_ERR_ACC, (int16_t)libusb_le16_to_cpu(st->err_acc));
	}

	/* update our state */
	memcpy(last_st, st, sizeof(*last_st));
}

static void
_e1_usb_gpsdo_poll_cb(void *data)
{
	struct e1_intf *intf = (struct e1_intf *) data;
	struct e1_usb_intf_data *id = intf->drv_data;

	/* issue a control endpoint request, further processing is when it completes */
	e1_usb_ctrl_get_gpsdo_status(intf);

	osmo_timer_schedule(&id->gpsdo.poll_timer, 1, 0);
}

static void
_e1_usb_gpsdo_init(struct e1_intf *intf)
{
	struct e1_usb_intf_data *id = intf->drv_data;

	if (intf->usb.gpsdo.manual) {
		struct e1usb_gpsdo_tune tune = {
			.coarse = intf->usb.gpsdo.coarse,
			.fine   = intf->usb.gpsdo.fine,
		};
		e1_usb_ctrl_set_gpsdo_mode(intf, ICE1USB_GPSDO_MODE_DISABLED);
		e1_usb_ctrl_set_gpsdo_tune(intf, &tune);
	} else {
		e1_usb_ctrl_set_gpsdo_mode(intf, ICE1USB_GPSDO_MODE_AUTO);
	}

	osmo_timer_setup(&id->gpsdo.poll_timer, &_e1_usb_gpsdo_poll_cb, intf);
	osmo_timer_schedule(&id->gpsdo.poll_timer, 1, 0);
}

// ---------------------------------------------------------------------------
// Init / Probing
// ---------------------------------------------------------------------------

static int
_e1_usb_open_device(struct e1_daemon *e1d, struct libusb_device *dev, bool is_tracer)
{
	struct e1_intf *intf;
	struct e1_line *line;
	struct e1_usb_intf_data *intf_data;
	struct e1_usb_line_data *line_data;
	struct libusb_device_descriptor dd;
	struct libusb_config_descriptor *cd;
	const struct libusb_interface_descriptor *id;
	bool auto_create_lines;
	char serial_str[64];
	libusb_device_handle *devh;
	int line_nr = 0;
	int i, j, ret;
	const char *hwname = "icE1usb";

	if (is_tracer)
		hwname = "e1-tracer";

	ret = libusb_open(dev, &devh);
	if (ret) {
		LOGP(DE1D, LOGL_ERROR, "Failed to open usb device: %s\n", libusb_strerror(ret));
		return ret;
	}

	ret = libusb_get_device_descriptor(dev, &dd);
	if (ret) {
		LOGP(DE1D, LOGL_ERROR, "Failed to get device descriptor: %s\n", libusb_strerror(ret));
		libusb_close(devh);
		return ret;
	}

	/* this is actually a synchronous / blocking call, and if we detect a second icE1usb device while the
	 * first one is running, we might be delaying/blocking any important USB transfers.  However, as we
	 * still only call this probe function once at start-up and don't support hot-plugging yet, we can get
	 * away with it. */
	ret = libusb_get_string_descriptor_ascii(devh, dd.iSerialNumber, (uint8_t *)serial_str, sizeof(serial_str));
	if (ret < 0) {
		LOGP(DE1D, LOGL_ERROR, "Failed to get iSerialNumber string descriptor: %s\n", libusb_strerror(ret));
		libusb_close(devh);
		return ret;
	}

	/* try to find the matching interface config created by the vty */
	intf = e1d_find_intf_by_usb_serial(e1d, serial_str);
	if (intf) {
		LOGP(DE1D, LOGL_INFO, "Configuration for %s serial '%s' found\n", hwname, serial_str);
		auto_create_lines = false;
		if (intf->drv_data) {
			LOGP(DE1D, LOGL_ERROR, "New device with serial '%s', but E1 interface %u busy\n",
			     serial_str, intf->id);
			libusb_close(devh);
			return -EBUSY;
		}
		intf_data = talloc_zero(e1d->ctx, struct e1_usb_intf_data);
		intf_data->devh = devh;
		intf->drv_data = intf_data;
	} else {
		LOGP(DE1D, LOGL_NOTICE, "No configuration for %s serial '%s' found, "
		     "auto-generating it\n", hwname, serial_str);
		auto_create_lines = true;
		intf_data = talloc_zero(e1d->ctx, struct e1_usb_intf_data);
		intf_data->devh = devh;
		intf = e1_intf_new(e1d, -1, intf_data);
		intf->drv = E1_DRIVER_USB;
		osmo_talloc_replace_string(intf, &intf->usb.serial_str, serial_str);
	}

	/* we have prior knowledge that the e1-tracer firmware configuration 2 is the e1d compatible mode. */
	if (is_tracer) {
		if (libusb_set_configuration(devh, 2) != LIBUSB_SUCCESS) {
			LOGP(DE1D, LOGL_ERROR, "Cannot set configuration 2 of e1-tracer device. Maybe too old firmware?\n");
			libusb_close(devh);
			return -EIO;
		}
	}

	INIT_LLIST_HEAD(&intf_data->ctrl_inprogress);

	ret = libusb_get_active_config_descriptor(dev, &cd);
	if (ret) {
		LOGP(DE1D, LOGL_ERROR, "Failed to talk to %s usb device: %s\n", hwname, libusb_strerror(ret));
		intf_data->devh = NULL;
		talloc_free(intf_data);
		if (auto_create_lines)
			e1_intf_destroy(intf);
		libusb_close(devh);
		return ret;
	}

	for (i = 0; i < cd->bNumInterfaces; i++) {
		/* Expect 2 altsettings with proper class/subclass/eps */
		if (cd->interface[i].num_altsetting != 2)
			continue;

		id = &cd->interface[i].altsetting[1];
		if (is_tracer) {
			if ((id->bInterfaceClass != 0xff) || (id->bInterfaceSubClass != 0xe1) || (id->bNumEndpoints < 1))
				continue;
		} else {
			if ((id->bInterfaceClass != 0xff) || (id->bInterfaceSubClass != 0xe1) || (id->bNumEndpoints < 3))
				continue;
		}

		line = e1_intf_find_line(intf, line_nr);
		if (line) {
			OSMO_ASSERT(auto_create_lines == false);
			if (line->drv_data) {
				LOGPLI(line, DE1D, LOGL_ERROR, "line busy but we are trying to open it again?\n");
				goto next_interface;
			}
		}

		/* Setup driver data and find endpoints */
		line_data = talloc_zero(e1d->ctx, struct e1_usb_line_data);

		INIT_LLIST_HEAD(&line_data->ctrl_inprogress);
		line_data->if_num = id->bInterfaceNumber;
		line_data->r_acc  = 0;
		line_data->r_sw   = 8192;

		for (j = 0; j < id->bNumEndpoints; j++) {
			if (id->endpoint[j].bmAttributes == 0x11) {
				line_data->ep_fb = id->endpoint[j].bEndpointAddress;
			} else if (id->endpoint[j].bmAttributes == 0x05) {
				if (id->endpoint[j].bEndpointAddress & 0x80)
					line_data->ep_in = id->endpoint[j].bEndpointAddress;
				else
					line_data->ep_out = id->endpoint[j].bEndpointAddress;

				if (!line_data->pkt_size)
					line_data->pkt_size = id->endpoint[j].wMaxPacketSize;
				else if (line_data->pkt_size != id->endpoint[j].wMaxPacketSize)
					LOGP(DE1D, LOGL_ERROR, "Inconsistent max packet size %d vs %d\n",
						line_data->pkt_size, (int)id->endpoint[j].wMaxPacketSize);
			} else if (id->endpoint[j].bmAttributes == 0x03) {
				line_data->ep_int = id->endpoint[j].bEndpointAddress;
			} else {
				LOGP(DE1D, LOGL_ERROR, "Invalid EP %02x\n", id->endpoint[j].bEndpointAddress);
			}
		}

		if (is_tracer) {
			if (!line_data->ep_in || !line_data->pkt_size) {
				LOGP(DE1D, LOGL_ERROR, "Failed to use interface %d\n", id->bInterfaceNumber);
				goto next_interface;
			}
		} else {
			if (!line_data->ep_in || !line_data->ep_out || !line_data->ep_fb || !line_data->pkt_size) {
				LOGP(DE1D, LOGL_ERROR, "Failed to use interface %d\n", id->bInterfaceNumber);
				goto next_interface;
			}
		}

		if (!line) {
			if (!auto_create_lines) {
				LOGPIF(intf, DE1D, LOGL_ERROR, "No configuration for line %d "
					"iInterface=%d, skipping\n", line_nr, id->bInterfaceNumber);
				goto next_interface;
			}
			line = e1_line_new(intf, line_nr, line_data);
		} else {
			OSMO_ASSERT(auto_create_lines == false);
			line->drv_data = line_data;
		}

		/* Get interface and set it up */
		ret = libusb_claim_interface(devh, id->bInterfaceNumber);
		if (ret) {
			LOGP(DE1D, LOGL_ERROR, "Failed to claim interface %d:%s\n", id->bInterfaceNumber,
			     libusb_strerror(ret));
			goto next_interface;
		}

		ret = libusb_set_interface_alt_setting(devh, id->bInterfaceNumber, 1);
		if (ret) {
			LOGP(DE1D, LOGL_ERROR, "Failed to set interface %d altsetting:%s\n", id->bInterfaceNumber,
			     libusb_strerror(ret));
			goto next_interface;
		}

		/* Configure, create data flows and start the line */

		/* all supported devices have an IN endpoint */
		e1_usb_ctrl_set_rx_cfg(line,
			(line->usb.framing.rx == E1_FRAMING_MODE_CRC4) ?
				ICE1USB_RX_MODE_MULTIFRAME :
				ICE1USB_RX_MODE_FRAME
		);

		line_data->flow_in  = e1uf_create(line, e1_usb_xfer_in,  line_data->ep_in,  4, line_data->pkt_size, 4);
		e1uf_start(line_data->flow_in);

		/* e1-tracer has no OUT or FEEDBACK endpoint */
		if (!is_tracer) {
			e1_usb_ctrl_set_tx_cfg(line,
				(line->usb.framing.tx == E1_FRAMING_MODE_CRC4) ?
					ICE1USB_TX_MODE_TS0_CRC4_E :
					ICE1USB_TX_MODE_TS0,
				ICE1USB_TX_TIME_SRC_LOCAL,
				ICE1USB_TX_EXT_LOOPBACK_OFF,
				0x00
			);

			line_data->flow_out = e1uf_create(line, e1_usb_xfer_out, line_data->ep_out, 4, line_data->pkt_size, 4);
			e1uf_start(line_data->flow_out);
			line_data->flow_fb  = e1uf_create(line, e1_usb_xfer_fb,  line_data->ep_fb,  2, 3, 1);
			e1uf_start(line_data->flow_fb);
		}

		if (line_data->ep_int)
			resubmit_irq(line);

		e1_line_active(line);

next_interface:
		line_nr++;
	}

	/* find the GPS-DO interface (if any) */
	for (i = 0; i < cd->bNumInterfaces; i++) {
		if (cd->interface[i].num_altsetting != 1)
			continue;

		id = &cd->interface[i].altsetting[0];
		if ((id->bInterfaceClass == 0xff) && (id->bInterfaceSubClass == 0xe1) &&
		    (id->bInterfaceProtocol == 0xd0)) {
			intf_data->gpsdo.if_num = id->bInterfaceNumber;
			_e1_usb_gpsdo_init(intf);
			break;
		}
	}

	return 0;
}

int
e1_usb_probe(struct e1_daemon *e1d)
{
	struct libusb_device **dev_list;
	ssize_t n_dev;
	int i, ret;

	if (!g_usb) {
		ret = osmo_libusb_init(&g_usb);
		if (ret) {
			LOGP(DE1D, LOGL_ERROR, "Failed to initialize libusb\n");
			return -EIO;
		}
	}

	n_dev = libusb_get_device_list(g_usb, &dev_list);
	if (n_dev < 0) {
		LOGP(DE1D, LOGL_ERROR, "Failed to list devices\n");
		return -EIO;
	}

	for (i = 0; i < n_dev; i++) {
		struct libusb_device_descriptor desc;

		ret = libusb_get_device_descriptor(dev_list[i], &desc);
		if (ret)
			continue;

		if (desc.idVendor != USB_VID)
			continue;

		switch (desc.idProduct) {
		case USB_PID:
			_e1_usb_open_device(e1d, dev_list[i], false);
			break;
		case USB_PID_TRACER:
			_e1_usb_open_device(e1d, dev_list[i], true);
			break;
		default:
			continue;
		}
	}

	libusb_free_device_list(dev_list, 1);

	return 0;
}