forked from myriadrf/libusb3380
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibusb3380.c
2171 lines (1818 loc) · 52 KB
/
libusb3380.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
/*
* Core functions for libusb3380
* Copyright (c) 2017 Sergey Kostanbaev <sergey.kostanbaev@fairwaves.co>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "xtrx_port.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <signal.h>
#include <stdarg.h>
#include "libusb3380.h"
#include "usb3380.h"
typedef struct pcibar {
uint32_t addr;
uint32_t length;
} pcibar_t;
struct libusb3380_pcidev {
libusb3380_context_t *ctx;
uint16_t devid; /**< { bus | dev | func } */
uint16_t vid;
uint16_t did;
uint8_t cfg_msi_numcap;
uint8_t cfg_msi_addr;
pcibar_t bars[6];
};
struct libusb3380_context {
libusb_device_handle* handle;
libusb_context* context;
unsigned int default_to;
uint32_t gpio_dirs;
uint32_t mem_addr_msi;
struct libusb3380_pcidev dev;
};
enum {
MSI_DEF_ADDR = 0xfee02000,
MSI_DEF_ADDR_HIGH = 0,
MSI_DEF_DATA = 0x4080,
};
typedef enum libusb3380_pci_master_stage {
STAGE_OUT,
STAGE_IN
} libusb3380_pci_master_stage_t;
/** Internal queue element */
typedef struct libusb3380_data_qbase {
union {
libusb3380_as_base_t* pbase;
libusb3380_qgpep_t *pgpep;
libusb3380_qpci_master_t *ppcims;
libusb3380_qmsi_int_t *pmsi;
} blk;
} libusb3380_data_qbase_t;
struct libusb3380_async_manager;
typedef struct libusb3380_queue_header {
libusb3380_data_qbase_t queue;
union {
pthread_mutex_t qmutex;
unsigned gpep_idx[2];
};
struct libusb_transfer *transfer;
} libusb3380_queue_header_t;
enum {
MAX_MSI_INTERRUPTS = 32,
};
typedef struct libusb3380_async_manager {
/* currently we supports only one device */
struct libusb3380_pcidev *dev;
libusb3380_configuration_t cfg;
unsigned total_gpeps;
unsigned gpep_in_idx_st[LIBUSB3380_GPEP_COUNT];
unsigned gpep_out_idx_st[LIBUSB3380_GPEP_COUNT];
libusb3380_queue_header_t *q_gpep;
libusb3380_qgpep_t *gpep;
libusb3380_queue_header_t q_pciout;
libusb3380_queue_header_t q_msi;
libusb3380_queue_header_t q_csrout;
/* Root complex master */
libusb3380_qpci_master_t pciout;
sem_t pciout_notify;
/* MSI */
libusb3380_qmsi_int_t msi;
sem_t msi_notify;
libusb3380_qcsr_t csr;
sem_t csr_notify;
/* IO thread + (monitoring) */
pthread_t io_thread;
/* allocated resources */
unsigned msi_ints[MAX_MSI_INTERRUPTS];
/* interrupt RCIN */
uint32_t msi_data[4];
on_msi_cb_t msi_cb;
void* msi_param;
bool stop;
} libusb3380_async_manager_t;
static void def_log(libusb3380_loglevel_t level,
void* obj,
const char* func,
const char* file,
int line,
const char* message, ...) __attribute__ ((format (printf, 6, 7)));
void def_log(libusb3380_loglevel_t level,
void* obj,
const char* func,
const char* file,
int line,
const char* message, ...)
{
(void)obj;
const char* sevirity;
switch (level) {
case USB3380_LOG_ERROR: sevirity = "[ERROR]"; break;
case USB3380_LOG_WARNING: sevirity = "[WARN] "; break;
case USB3380_LOG_INFO: sevirity = "[INFO] "; break;
case USB3380_LOG_DEBUG: sevirity = "[DEBUG]"; break;
case USB3380_LOG_DUMP: sevirity = "[DUMP] "; break;
}
char tmp_buf[1024];
va_list ap;
va_start(ap, message);
vsnprintf(tmp_buf, sizeof(tmp_buf), message, ap);
va_end(ap);
fprintf(stderr, "%s %-16s %s\n", sevirity, func, tmp_buf);
}
static usb3380_logfunc_t s_logfunc = def_log;
static void* s_logobj = NULL;
static libusb3380_loglevel_t s_loglevel = USB3380_LOG_INFO;
#define LOG(x, ...) do { \
if (s_loglevel >= (x)) s_logfunc((x), s_logobj, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); \
} while(0)
#define LOG_ERR(...) LOG(USB3380_LOG_ERROR, __VA_ARGS__)
#define LOG_WARN(...) LOG(USB3380_LOG_WARNING, __VA_ARGS__)
#define LOG_INFO(...) LOG(USB3380_LOG_INFO, __VA_ARGS__)
#define LOG_DEBUG(...) LOG(USB3380_LOG_DEBUG, __VA_ARGS__)
#define LOG_DUMP(...) LOG(USB3380_LOG_DUMP, __VA_ARGS__)
void usb3380_set_logfunc(usb3380_logfunc_t func, void *param)
{
s_logobj = param;
s_logfunc = func;
}
void usb3380_set_loglevel(libusb3380_loglevel_t level)
{
s_loglevel = level;
}
static unsigned char convert_gpep_no(unsigned no)
{
switch (no) {
case LIBUSB3380_GPEP0: return EP_GPEP0;
case LIBUSB3380_GPEP1: return EP_GPEP1;
case LIBUSB3380_GPEP2: return EP_GPEP2;
case LIBUSB3380_GPEP3: return EP_GPEP3;
default: return 0xff;
}
}
int usb3380_gpio_dir(libusb3380_context_t* ctx, uint8_t diroutbits)
{
ctx->gpio_dirs = diroutbits & 0xf;
uint32_t reg;
usb3380_csr_mm_cfg_read(ctx, 0x6C0, ®);
LOG_DEBUG("USBPM = %08x", reg);
reg |= (1<<11);
return usb3380_csr_mm_cfg_write(ctx, 0x6C0, reg);
}
int usb3380_gpio_out(libusb3380_context_t* ctx, uint8_t out)
{
uint32_t reg = (ctx->gpio_dirs << 4) | ((out) & 0xf);
LOG_DEBUG("GPIO OUT: %d%d%d%d (%08x)",
(out & 8) ? 1 : 0,
(out & 4) ? 1 : 0,
(out & 2) ? 1 : 0,
(out & 1) ? 1 : 0,
reg);
return usb3380_csr_mm_cfg_write(ctx, GPIOCTRL, reg);
}
int usb3380_gpio_in(libusb3380_context_t* ctx, uint8_t *in)
{
uint32_t reg;
int res = usb3380_csr_mm_cfg_read(ctx, GPIOCTRL, ®);
if (res) {
return res;
}
LOG_DEBUG("GPIO IN: %08x", reg);
*in = (reg & 0xf);
return 0;
}
static int usb3380_int_queue_init(libusb3380_queue_header_t *q)
{
int res;
res = pthread_mutex_init(&q->qmutex, NULL);
if (res)
return -res;
q->transfer = libusb_alloc_transfer(0);
if (q->transfer == NULL)
return -ENOMEM;
q->queue.blk.pbase = NULL;
return 0;
}
static int usb3380_int_queue_gpep_init(libusb3380_queue_header_t *q,
unsigned gpepno, unsigned idx)
{
q->gpep_idx[0] = gpepno;
q->gpep_idx[1] = idx;
q->transfer = libusb_alloc_transfer(0);
if (q->transfer == NULL)
return -ENOMEM;
q->queue.blk.pbase = NULL;
return 0;
}
static void usb3380_int_queue_deinit(libusb3380_queue_header_t *q)
{
pthread_mutex_destroy(&q->qmutex);
libusb_free_transfer(q->transfer);
}
static void usb3380_int_queue_gpep_deinit(libusb3380_queue_header_t *q)
{
libusb_free_transfer(q->transfer);
}
static void* usb3380_io_thread(void *arg)
{
struct libusb3380_async_manager* mgr = (struct libusb3380_async_manager*)arg;
int res = 0;
#if defined(__linux) || defined(__APPLE__)
sigset_t set;
pthread_setname_np(pthread_self(), "usb3380_io");
sigfillset(&set);
pthread_sigmask(SIG_SETMASK, &set, NULL);
struct sched_param shed;
shed.sched_priority = 2;
res = pthread_setschedparam(pthread_self(), SCHED_FIFO, &shed);
if (res) {
LOG_WARN("IO thread: Unable to set realtime priority: error %d", res);
}
#endif
LOG_INFO("IO thread started");
while (!mgr->stop) {
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
res = libusb_handle_events_timeout(mgr->dev->ctx->context, &tv);
}
LOG_INFO("IO thread termitaed with result %d", res);
return (void*)((intptr_t)res);
}
static void fill_base_in_cb(libusb3380_as_base_t* base,
const struct libusb_transfer *transfer)
{
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
if (transfer->actual_length == transfer->length)
base->status = DQS_SUCCESS;
else
base->status = DQS_PARTIAL;
break;
case LIBUSB_TRANSFER_TIMED_OUT:
base->status = DQS_TIMEOUT;
break;
case LIBUSB_TRANSFER_CANCELLED:
base->status = DQS_CANCELLED;
break;
case LIBUSB_TRANSFER_STALL:
/* TODO clear EP */
LOG_ERR("GPEP 0x%02x ERROR: STALL", transfer->endpoint);
base->status = DQS_ABORT;
break;
case LIBUSB_TRANSFER_NO_DEVICE:
LOG_ERR("GPEP 0x%02x ERROR: NO DEVICE", transfer->endpoint);
base->status = DQS_ABORT;
break;
case LIBUSB_TRANSFER_OVERFLOW:
LOG_ERR("GPEP 0x%02x ERROR: OVERFLOW", transfer->endpoint);
base->status = DQS_ABORT;
break;
default:
case LIBUSB_TRANSFER_ERROR:
LOG_ERR("GPEP 0x%02x ERROR: %d", transfer->endpoint, transfer->status);
base->status = DQS_ABORT;
break;
}
base->written = (unsigned)transfer->actual_length;
}
static void notify_pciout(libusb3380_async_manager_t* mgr)
{
int res;
fill_base_in_cb(&mgr->pciout.base, mgr->q_pciout.transfer);
res = sem_post(&mgr->pciout_notify);
assert(res == 0);
}
static LIBUSB_CALL void on_pciout_cb(struct libusb_transfer *transfer)
{
libusb3380_async_manager_t* mgr = (libusb3380_async_manager_t*)transfer->user_data;
libusb3380_qpci_master_t* pciout = &mgr->pciout;
LOG_DUMP("on_pciout_cb addr=%x data=%x:%x STAT=%d!",
pciout->out.addr,
pciout->data[0],
pciout->data[1],
transfer->status);
if (transfer->status == LIBUSB_TRANSFER_COMPLETED &&
transfer->actual_length == transfer->length &&
pciout->type == RC_PCIE_READ &&
transfer->endpoint == (EP_PCIOUT | LIBUSB_ENDPOINT_OUT)) {
// Submit PCIIN request
transfer->endpoint = EP_PCIIN | LIBUSB_ENDPOINT_IN;
transfer->length = pciout->base.size;
int res = libusb_submit_transfer(transfer);
if (!res) {
return;
}
// Notify abortion
transfer->status = LIBUSB_TRANSFER_ERROR;
}
// Notify waiting thread
notify_pciout(mgr);
}
static void prepare_ptransfer_pciout(libusb3380_async_manager_t* mgr)
{
libusb_fill_bulk_transfer(mgr->q_pciout.transfer,
mgr->dev->ctx->handle,
EP_PCIOUT | LIBUSB_ENDPOINT_OUT,
(uint8_t*)mgr->pciout.data,
8,
on_pciout_cb,
mgr,
1000);
mgr->q_pciout.transfer->flags = 0;
}
static LIBUSB_CALL void on_rcin_cb(struct libusb_transfer *transfer)
{
libusb3380_async_manager_t* mgr = (libusb3380_async_manager_t*)transfer->user_data;
int res;
int msi_num = -1;
LOG_DUMP("on_rcin_cb addr=%08x:%08x:%08x:%08x!",
mgr->msi_data[0], mgr->msi_data[1], mgr->msi_data[2],
mgr->msi_data[3]);
if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
msi_num = mgr->msi_data[3] - MSI_DEF_DATA;
if (msi_num >= 0 && msi_num < 32) {
mgr->msi_ints[msi_num]++;
} else {
LOG_DUMP("on_rcin_cb got unexpected msi %08x:%08x:%08x:%08x!",
mgr->msi_data[0], mgr->msi_data[1], mgr->msi_data[2],
mgr->msi_data[3]);
}
}
// TODO: determine interrupt queue based on msi_num received
fill_base_in_cb(&mgr->msi.base, mgr->q_msi.transfer);
res = sem_post(&mgr->msi_notify);
assert(res == 0);
}
static void prepare_ptransfer_rcin(libusb3380_async_manager_t* mgr,
libusb_transfer_cb_fn callback,
unsigned timeout)
{
libusb_fill_interrupt_transfer(mgr->q_msi.transfer,
mgr->dev->ctx->handle,
EP_RCIN | LIBUSB_ENDPOINT_IN,
(uint8_t*)mgr->msi_data,
16,
callback,
mgr,
timeout);
mgr->q_msi.transfer->flags = 0;
}
static int async_pciout_post(libusb3380_async_manager_t* mgr,
libusb3380_pci_tlp_type_t type,
uint32_t addr,
uint32_t *data,
unsigned dw_count)
{
int res;
libusb3380_qpci_master_t* pciout = &mgr->pciout;
res = pthread_mutex_lock(&mgr->q_pciout.qmutex);
if (res) {
// TODO CHECKME FIXME
return res;
}
restart:
pciout->base.size = dw_count * 4;
pciout->base.timeout = 1000;
pciout->base.status = DQS_ABORT;
pciout->base.written = 0;
pciout->out.ctrl = 0xf | PCIMSTCTL_CMD_MEMORY | PCIMSTCTL_MASTER_START
| (dw_count << PCIMSTCTL_PCIE_DW_LEN_OFF_BITS);
if (type == RC_PCIE_READ) {
pciout->out.ctrl |= PCIMSTCTL_PCIE_READ;
} else {
memcpy(pciout->out.data, data, dw_count * 4);
pciout->out.ctrl |= PCIMSTCTL_PCIE_WRITE;
}
pciout->out.addr = addr;
pciout->type = type;
mgr->q_pciout.transfer->endpoint = EP_PCIOUT | LIBUSB_ENDPOINT_OUT;
mgr->q_pciout.transfer->length = (type == RC_PCIE_READ) ? 8 : (int)(8 + dw_count * 4);
res = libusb_submit_transfer(mgr->q_pciout.transfer);
if (res) {
goto failed_in_lock; // TODO error code
}
// Wait for transaction finalization
res = sem_wait(&mgr->pciout_notify);
if (res) {
goto failed_in_lock;
}
if (pciout->base.status == DQS_PARTIAL) {
if (type == RC_PCIE_READ) {
LOG_WARN("Restart read transaction! addr=%x size=%d", addr,
pciout->base.size);
goto restart;
} else {
LOG_WARN("Writr failed! addr=%x", addr);
}
}
if (pciout->base.status == DQS_SUCCESS && type == RC_PCIE_READ) {
memcpy(data, pciout->data, dw_count * 4);
}
if (pciout->base.status == DQS_TIMEOUT)
res = -ETIMEDOUT;
else if (pciout->base.status == DQS_ABORT)
res = -EIO;
else if (pciout->base.status != DQS_SUCCESS)
res = -EFAULT;
failed_in_lock:
pthread_mutex_unlock(&mgr->q_pciout.qmutex);
return res;
}
int usb3380_async_pci_write32(struct libusb3380_async_manager* mgr,
uint32_t addr,
const uint32_t *data,
unsigned dw_count)
{
return async_pciout_post(mgr, RC_PCIE_WRITE, addr, (uint32_t*)data,
dw_count);
}
int usb3380_async_pci_read32(struct libusb3380_async_manager* mgr,
uint32_t addr,
uint32_t *data,
unsigned dw_count)
{
return async_pciout_post(mgr, RC_PCIE_READ, addr, data, dw_count);
}
int usb3380_async_await_msi(struct libusb3380_async_manager* mgr,
unsigned num)
{
int res;
libusb3380_qmsi_int_t* rcin = &mgr->msi;
res = pthread_mutex_lock(&mgr->q_msi.qmutex);
if (res) {
// TODO CHECKME FIXME
return res;
}
prepare_ptransfer_rcin(mgr, on_rcin_cb, 100);
res = libusb_submit_transfer(mgr->q_msi.transfer);
if (res) {
goto failed_in_lock; // TODO error code
}
// Wait for transaction finalization
res = sem_wait(&mgr->msi_notify);
if (res) {
goto failed_in_lock;
}
if (rcin->base.status == DQS_TIMEOUT)
res = -ETIMEDOUT;
else if (rcin->base.status == DQS_ABORT)
res = -EIO;
failed_in_lock:
pthread_mutex_unlock(&mgr->q_msi.qmutex);
LOG_DUMP("usb3380_async_await_msi failed!");
return res;
}
static int libusb_to_errno(int libusberr)
{
switch (libusberr) {
case LIBUSB_SUCCESS:
return 0;
case LIBUSB_ERROR_IO:
return -EIO;
case LIBUSB_ERROR_INVALID_PARAM:
return -EINVAL;
case LIBUSB_ERROR_ACCESS:
return -EPERM;
case LIBUSB_ERROR_NO_DEVICE:
return -ENODEV;
case LIBUSB_ERROR_NOT_FOUND:
return -ENXIO;
case LIBUSB_ERROR_BUSY:
return -EBUSY;
case LIBUSB_ERROR_TIMEOUT:
return -ETIMEDOUT;
case LIBUSB_ERROR_OVERFLOW:
return -EOVERFLOW;
case LIBUSB_ERROR_PIPE:
return -EPIPE;
case LIBUSB_ERROR_INTERRUPTED:
return -EINTR;
case LIBUSB_ERROR_NO_MEM:
return -ENOMEM;
case LIBUSB_ERROR_NOT_SUPPORTED:
return -EOPNOTSUPP;
case LIBUSB_ERROR_OTHER:
return -ESRCH; //TODO find better;
};
return -EFAULT;
}
static int gpep_transfer_post(libusb3380_queue_header_t* q,
libusb3380_qgpep_t *d)
{
q->transfer->length = d->base.size;
q->transfer->buffer = d->pdata;
return libusb_to_errno(libusb_submit_transfer(q->transfer));
}
static LIBUSB_CALL void on_gpep_cb(struct libusb_transfer *transfer)
{
libusb3380_queue_header_t* qh = (libusb3380_queue_header_t*)transfer->user_data;
LOG_DUMP("TransferCB EP:%02x [%d;%d] done", transfer->endpoint,
qh->gpep_idx[0], qh->gpep_idx[1]);
fill_base_in_cb(qh->queue.blk.pbase, qh->transfer);
qh->queue.blk.pgpep->cb_done(qh->queue.blk.pgpep, qh->gpep_idx[0], qh->gpep_idx[1]);
}
static void prepare_ptransfer_gpep(libusb3380_async_manager_t* mgr,
libusb3380_queue_header_t* hdr,
libusb3380_qgpep_t* queue,
unsigned char ep_no,
unsigned int timeout)
{
hdr->queue.blk.pgpep = queue;
libusb_fill_bulk_transfer(hdr->transfer,
mgr->dev->ctx->handle,
ep_no,
(uint8_t*)NULL,
0,
on_gpep_cb,
hdr,
timeout);
hdr->transfer->flags = 0;
}
int usb3380_async_gpep_out_post(struct libusb3380_async_manager* mgr,
libusb3380_gpep_t ep_no,
unsigned idx,
const uint8_t* data, unsigned size,
on_gpep_cb_t cb, void* param)
{
if (ep_no > LIBUSB3380_GPEP3 || ep_no < LIBUSB3380_GPEP0)
return -EINVAL;
if (mgr->cfg.gp_out_cnts[ep_no] <= idx)
return -EINVAL;
unsigned gidx = mgr->gpep_out_idx_st[ep_no] + idx;
libusb3380_qgpep_t *gpep_out = &mgr->gpep[gidx];
// TODO check multi thread access
gpep_out->base.size = size;
gpep_out->pdata = (uint8_t*)data;
gpep_out->cb_done = cb;
gpep_out->param = param;
return gpep_transfer_post(&mgr->q_gpep[gidx], gpep_out);
}
int usb3380_async_gpep_in_post(struct libusb3380_async_manager* mgr,
libusb3380_gpep_t ep_no,
unsigned idx,
uint8_t* data, unsigned size,
on_gpep_cb_t cb, void* param)
{
if (ep_no > LIBUSB3380_GPEP3 || ep_no < LIBUSB3380_GPEP0)
return -EINVAL;
if (mgr->cfg.gp_in_cnts[ep_no] <= idx)
return -EINVAL;
unsigned gidx = mgr->gpep_in_idx_st[ep_no] + idx;
libusb3380_qgpep_t *gpep_in = &mgr->gpep[gidx];
// TODO check multi thread access
gpep_in->base.size = size;
gpep_in->pdata = data;
gpep_in->cb_done = cb;
gpep_in->param = param;
return gpep_transfer_post(&mgr->q_gpep[gidx], gpep_in);
}
int usb3380_async_gpep_cancel(struct libusb3380_async_manager* mgr,
bool ep_in,
libusb3380_gpep_t gpep,
unsigned idx)
{
if (gpep > LIBUSB3380_GPEP3 || gpep < LIBUSB3380_GPEP0)
return -EINVAL;
unsigned gidx;
if (ep_in) {
if (mgr->cfg.gp_in_cnts[gpep] <= idx)
return -EINVAL;
gidx = mgr->gpep_in_idx_st[gpep] + idx;
} else {
if (mgr->cfg.gp_out_cnts[gpep] <= idx)
return -EINVAL;
gidx = mgr->gpep_out_idx_st[gpep] + idx;
}
return libusb_to_errno(libusb_cancel_transfer(mgr->q_gpep[gidx].transfer));
}
int usb3380_async_set_gpep_timeout(struct libusb3380_async_manager* mgr,
bool ep_in, libusb3380_gpep_t gpep,
unsigned idx,
unsigned to_ms)
{
if (gpep > LIBUSB3380_GPEP3 || gpep < LIBUSB3380_GPEP0)
return -EINVAL;
unsigned gidx;
if (ep_in) {
if (mgr->cfg.gp_in_cnts[gpep] <= idx)
return -EINVAL;
gidx = mgr->gpep_in_idx_st[gpep] + idx;
} else {
if (mgr->cfg.gp_out_cnts[gpep] <= idx)
return -EINVAL;
gidx = mgr->gpep_out_idx_st[gpep] + idx;
}
mgr->q_gpep[gidx].transfer->timeout = to_ms;
return 0;
}
int usb3380_async_start(struct libusb3380_pcidev *dev,
const struct libusb3380_configuration *configuration,
struct libusb3380_async_manager **out)
{
libusb3380_async_manager_t* mgr;
int res;
const unsigned def_timeout = 1000;
unsigned idx;
mgr = (libusb3380_async_manager_t*)malloc(sizeof(libusb3380_async_manager_t));
if (!mgr)
return -ENOMEM;
memset(mgr, 0, sizeof(*mgr));
mgr->dev = dev;
mgr->stop = false;
mgr->cfg = *configuration;
for (unsigned k = 0; k < LIBUSB3380_GPEP_COUNT; k++) {
mgr->total_gpeps += configuration->gp_in_cnts[k];
mgr->total_gpeps += configuration->gp_out_cnts[k];
}
if (mgr->total_gpeps > 0) {
mgr->q_gpep = (libusb3380_queue_header_t*)malloc(sizeof(libusb3380_queue_header_t) * mgr->total_gpeps);
if (mgr->q_gpep == NULL) {
res = -ENOMEM;
goto q_gpep_alloc_fail;
}
mgr->gpep = (libusb3380_qgpep_t*)malloc(sizeof(libusb3380_qgpep_t) * mgr->total_gpeps);
if (mgr->gpep == NULL) {
res = -ENOMEM;
goto gpep_alloc_fail;
}
memset(mgr->q_gpep, 0, sizeof(libusb3380_queue_header_t) * mgr->total_gpeps);
memset(mgr->gpep, 0, sizeof(libusb3380_qgpep_t) * mgr->total_gpeps);
idx = 0;
for (unsigned l = 0; l < LIBUSB3380_GPEP_COUNT; l++) {
mgr->gpep_in_idx_st[l] = idx;
for (unsigned m = 0; m < mgr->cfg.gp_in_cnts[l]; m++, idx++) {
res = usb3380_int_queue_gpep_init(&mgr->q_gpep[idx], l, m);
if (res)
goto failed_pciout;
prepare_ptransfer_gpep(mgr,
&mgr->q_gpep[idx],
&mgr->gpep[idx],
convert_gpep_no(l) | LIBUSB_ENDPOINT_IN,
def_timeout);
}
}
for (unsigned l = LIBUSB3380_GPEP_COUNT; l < 2 * LIBUSB3380_GPEP_COUNT; l++) {
mgr->gpep_out_idx_st[l - LIBUSB3380_GPEP_COUNT] = idx;
for (unsigned m = 0; m < mgr->cfg.gp_out_cnts[l - LIBUSB3380_GPEP_COUNT]; m++, idx++) {
res = usb3380_int_queue_gpep_init(&mgr->q_gpep[idx], l, m);
if (res)
goto failed_pciout;
prepare_ptransfer_gpep(mgr,
&mgr->q_gpep[idx],
&mgr->gpep[idx],
convert_gpep_no(l - LIBUSB3380_GPEP_COUNT) | LIBUSB_ENDPOINT_OUT,
def_timeout);
}
}
assert(mgr->total_gpeps == idx);
}
res = usb3380_int_queue_init(&mgr->q_pciout);
if (res)
goto failed_pciout;
res = usb3380_int_queue_init(&mgr->q_msi);
if (res)
goto failed_msi;
prepare_ptransfer_pciout(mgr);
res = sem_init(&mgr->pciout_notify, 0, 0);
if (res) {
res = -errno;
goto failed_sem_pci;
}
res = sem_init(&mgr->msi_notify, 0, 0);
if (res) {
res = -errno;
goto failed_sem_rc;
}
res = pthread_create(&mgr->io_thread, NULL, usb3380_io_thread, mgr);
if (res) {
res = -res;
goto failed_thread;
}
*out = mgr;
return 0;
failed_thread:
sem_destroy(&mgr->msi_notify);
failed_sem_rc:
sem_destroy(&mgr->pciout_notify);
failed_sem_pci:
usb3380_int_queue_deinit(&mgr->q_msi);
failed_msi:
usb3380_int_queue_deinit(&mgr->q_pciout);
failed_pciout:
for (idx = 0; idx < mgr->total_gpeps; idx++) {
if (mgr->q_gpep[idx].transfer) {
usb3380_int_queue_gpep_deinit(&mgr->q_gpep[idx]);
}
}
free(mgr->q_gpep);
gpep_alloc_fail:
free(mgr->gpep);
q_gpep_alloc_fail:
free(mgr);
return res;
}
int usb3380_async_stop(struct libusb3380_async_manager* mgr)
{
mgr->stop = true;
pthread_join(mgr->io_thread, NULL);
sem_destroy(&mgr->msi_notify);
sem_destroy(&mgr->pciout_notify);
usb3380_int_queue_deinit(&mgr->q_msi);
usb3380_int_queue_deinit(&mgr->q_pciout);
for (unsigned idx = 0; idx < mgr->total_gpeps; idx++) {
usb3380_int_queue_gpep_deinit(&mgr->q_gpep[idx]);
}
free(mgr->q_gpep);
free(mgr->gpep);
free(mgr);
return 0;
}
static int usb3380_int_csrout_sync(libusb3380_context_t* ctx, uint32_t csrctl,
uint32_t csrdata)
{
CSROUT_ep_t pkt = { csrctl, csrdata };
int written = 0;
int res = libusb_bulk_transfer(ctx->handle,
EP_CSROUT | LIBUSB_ENDPOINT_OUT,
(unsigned char*)&pkt,
(csrctl & CSR_READ) ? 4 : 8,
&written,
ctx->default_to);
return res;
}
static int usb3380_int_csrin_sync(libusb3380_context_t* ctx, uint32_t* csrdata)
{
int written = 0;
int res = libusb_bulk_transfer(ctx->handle,
EP_CSRIN | LIBUSB_ENDPOINT_IN,
(unsigned char*)csrdata,
4,
&written,
ctx->default_to);
return res;
}
static int usb3380_int_csr_cfg_read(libusb3380_context_t* ctx, uint32_t ctrl,
uint32_t* data)
{
int res;
res = usb3380_int_csrout_sync(ctx, ctrl, 0);
if (res) {
LOG_ERR("Unable to usb3380_int_csrout_sync!");
return res;
}
res = usb3380_int_csrin_sync(ctx, data);
if (res) {
LOG_ERR("Unable to usb3380_int_csrin_sync!");
return res;
}
return 0;
}
int usb3380_csr_pcie_cfg_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data)
{
return usb3380_int_csr_cfg_read(ctx, CSR_READ | CSR_SS_PCIE_CFG |
CSR_START | (addr << CSR_ADDR_OFF_BITS),
data);
}
int usb3380_csr_pcie_cfg_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data)
{
return usb3380_int_csrout_sync(ctx, CSR_WRITE | CSR_BYTE_EN_ALL |
CSR_SS_PCIE_CFG | CSR_START |
(addr << CSR_ADDR_OFF_BITS), data);
}
int usb3380_csr_mm_cfg_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data)
{
return usb3380_int_csr_cfg_read(ctx, CSR_READ | CSR_SS_MM_CFG | CSR_START |
(addr << CSR_ADDR_OFF_BITS), data);
}
int usb3380_csr_mm_cfg_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data)
{
return usb3380_int_csrout_sync(ctx, CSR_WRITE | CSR_BYTE_EN_ALL |
CSR_SS_MM_CFG | CSR_START |
(addr << CSR_ADDR_OFF_BITS), data);
}
int usb3380_csr_mm_mcu_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data)