/
XLinkPlatform.c
722 lines (633 loc) · 17.4 KB
/
XLinkPlatform.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
// Copyright (C) 2018-2019 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/timeb.h>
#include <errno.h>
#include <assert.h>
#include "XLinkPlatform.h"
#include "usb_boot.h"
#include "pcie_host.h"
#if (defined(_WIN32) || defined(_WIN64))
#include "usb_winusb.h"
#include "gettime.h"
#include "win_pthread.h"
extern void initialize_usb_boot();
#else
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <termios.h>
#include <libusb.h>
#endif /*defined(_WIN32) || defined(_WIN64)*/
#ifdef USE_LINK_JTAG
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif /*USE_LINK_JTAG*/
#define USBLINK_ERROR_PRINT
#ifdef USBLINK_ERROR_PRINT
#define USBLINK_ERROR(...) printf(__VA_ARGS__)
#else
#define USBLINK_ERROR(...) (void)0
#endif /*USBLINK_ERROR_PRINT*/
#ifdef USBLINKDEBUG
#define USBLINK_PRINT(...) printf(__VA_ARGS__)
#else
#define USBLINK_PRINT(...) (void)0
#endif /*USBLINKDEBUG*/
#ifdef USBLINKWARN
#define USBLINK_WARN(...) printf(__VA_ARGS__)
#else
#define USBLINK_WARN(...) (void)0
#endif /*USBLINKWARN*/
#define USB_LINK_SOCKET_PORT 5678
#define MAX_EVENTS 64
#define USB_ENDPOINT_IN 0x81
#define USB_ENDPOINT_OUT 0x01
#ifndef USE_USB_VSC
int usbFdWrite = -1;
int usbFdRead = -1;
#endif /*USE_USB_VSC*/
static int statuswaittimeout = 5;
#include <assert.h>
pthread_t readerThreadId;
// Communication protocol used
// Few functions could be called without XLink initialization
#ifdef USE_PCIE
int gl_protocol = PCIE;
#else // use USB as default
int gl_protocol = USB_VSC;
#endif
#define MAX_EVENTS 64
#if (defined(_WIN32) || defined(_WIN64))
extern void initialize_usb_boot();
#endif
static xLinkPlatformErrorCode_t parseUsbBootError(usbBootError_t rc) {
switch (rc) {
case USB_BOOT_SUCCESS:
return X_LINK_PLATFORM_SUCCESS;
case USB_BOOT_DEVICE_NOT_FOUND:
return X_LINK_PLATFORM_DEVICE_NOT_FOUND;
case USB_BOOT_TIMEOUT:
return X_LINK_PLATFORM_TIMEOUT;
default:
return X_LINK_PLATFORM_ERROR;
}
}
static int usb_write(libusb_device_handle *f, const void *data, size_t size, unsigned int timeout)
{
while(size > 0)
{
int bt, ss = size;
if(ss > 1024*1024*5)
ss = 1024*1024*5;
#if (defined(_WIN32) || defined(_WIN64) )
int rc = usb_bulk_write(f, USB_ENDPOINT_OUT, (unsigned char *)data, ss, &bt, timeout);
#else
int rc = libusb_bulk_transfer(f, USB_ENDPOINT_OUT, (unsigned char *)data, ss, &bt, timeout);
#endif
if(rc)
return rc;
data = (char *)data + bt;
size -= bt;
}
return 0;
}
static int usb_read(libusb_device_handle *f, void *data, size_t size, unsigned int timeout)
{
while(size > 0)
{
int bt, ss = size;
if(ss > 1024*1024*5)
ss = 1024*1024*5;
#if (defined(_WIN32) || defined(_WIN64))
int rc = usb_bulk_read(f, USB_ENDPOINT_IN, (unsigned char *)data, ss, &bt, timeout);
#else
int rc = libusb_bulk_transfer(f, USB_ENDPOINT_IN,(unsigned char *)data, ss, &bt, timeout);
#endif
if(rc)
return rc;
data = ((char *)data) + bt;
size -= bt;
}
return 0;
}
static double seconds()
{
static double s;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
if(!s)
s = ts.tv_sec + ts.tv_nsec * 1e-9;
return ts.tv_sec + ts.tv_nsec * 1e-9 - s;
}
libusb_device_handle *usblink_open(const char *path)
{
if (path == NULL) {
return 0;
}
usbBootError_t rc = USB_BOOT_DEVICE_NOT_FOUND;
libusb_device_handle *h = NULL;
libusb_device *dev = NULL;
double waittm = seconds() + statuswaittimeout;
while(seconds() < waittm){
int size = strlen(path);
#if (!defined(_WIN32) && !defined(_WIN64))
uint16_t bcdusb = -1;
rc = usb_find_device_with_bcd(0, (char *)path, size, (void **)&dev, DEFAULT_OPENVID, DEFAULT_OPENPID,&bcdusb);
#else
rc = usb_find_device(0, (char *)path, size, (void **)&dev, DEFAULT_OPENVID, DEFAULT_OPENPID);
#endif
if(rc == USB_BOOT_SUCCESS)
break;
usleep(1000);
}
if (rc == USB_BOOT_TIMEOUT || rc == USB_BOOT_DEVICE_NOT_FOUND) // Timeout
return 0;
#if (defined(_WIN32) || defined(_WIN64) )
h = usb_open_device(dev, NULL, 0, stderr);
int libusb_rc = ((h != NULL) ? (0) : (-1));
if (libusb_rc < 0)
{
usb_close_device(h);
usb_free_device(dev);
return 0;
}
usb_free_device(dev);
#else
int libusb_rc = libusb_open(dev, &h);
if (libusb_rc < 0)
{
libusb_unref_device(dev);
return 0;
}
libusb_unref_device(dev);
libusb_detach_kernel_driver(h, 0);
libusb_rc = libusb_claim_interface(h, 0);
if(libusb_rc < 0)
{
libusb_close(h);
return 0;
}
#endif
return h;
}
void usblink_close(libusb_device_handle *f)
{
#if (defined(_WIN32) || defined(_WIN64))
usb_close_device(f);
#else
libusb_release_interface(f, 0);
libusb_close(f);
#endif
}
int USBLinkWrite(void* fd, void* data, int size, unsigned int timeout)
{
int rc = 0;
#ifndef USE_USB_VSC
int byteCount = 0;
#ifdef USE_LINK_JTAG
while (byteCount < size){
byteCount += write(usbFdWrite, &((char*)data)[byteCount], size - byteCount);
printf("write %d %d\n", byteCount, size);
}
#else
if(usbFdWrite < 0)
{
return -1;
}
while(byteCount < size)
{
int toWrite = (PACKET_LENGTH && (size - byteCount > PACKET_LENGTH)) \
? PACKET_LENGTH:size - byteCount;
int wc = write(usbFdWrite, ((char*)data) + byteCount, toWrite);
if ( wc != toWrite)
{
return -2;
}
byteCount += toWrite;
unsigned char acknowledge;
int rc;
rc = read(usbFdWrite, &acknowledge, sizeof(acknowledge));
if ( rc < 0)
{
return -2;
}
if (acknowledge != 0xEF)
{
return -2;
}
}
#endif /*USE_LINK_JTAG*/
#else
rc = usb_write((libusb_device_handle *) fd, data, size, timeout);
#endif /*USE_USB_VSC*/
return rc;
}
int USBLinkRead(void* fd, void* data, int size, unsigned int timeout)
{
// FIXME USE_LINK_JTAG not compiled
#ifndef USE_PCIE
int rc = 0;
#ifndef USE_USB_VSC
int nread = 0;
#ifdef USE_LINK_JTAG
while (nread < size){
nread += read(usbFdWrite, &((char*)data)[nread], size - nread);
printf("read %d %d\n", nread, size);
}
#else
if(usbFdRead < 0)
{
return -1;
}
while(nread < size)
{
int toRead = (PACKET_LENGTH && (size - nread > PACKET_LENGTH)) \
? PACKET_LENGTH : size - nread;
while(toRead > 0)
{
rc = read(usbFdRead, &((char*)gl_protocoldata)[nread], toRead);
if ( rc < 0)
{
return -2;
}
toRead -=rc;
nread += rc;
}
unsigned char acknowledge = 0xEF;
int wc = write(usbFdRead, &acknowledge, sizeof(acknowledge));
if (wc != sizeof(acknowledge))
{
return -2;
}
}
#endif /*USE_LINK_JTAG*/
#else
rc = usb_read((libusb_device_handle *) fd, data, size, timeout);
#endif /*USE_USB_VSC*/
return rc;
#endif // USE_PCIE
return 0;
}
int USBLinkPlatformResetRemote(void* fd)
{
#ifndef USE_USB_VSC
#ifdef USE_LINK_JTAG
/*Nothing*/
#else
if (usbFdRead != -1){
close(usbFdRead);
usbFdRead = -1;
}
if (usbFdWrite != -1){
close(usbFdWrite);
usbFdWrite = -1;
}
#endif /*USE_LINK_JTAG*/
#else
usblink_close((libusb_device_handle *) fd);
#endif /*USE_USB_VSC*/
return -1;
}
int UsbLinkPlatformConnect(const char* devPathRead, const char* devPathWrite, void** fd)
{
#if (!defined(USE_USB_VSC) && !defined(USE_PCIE))
#ifdef USE_LINK_JTAG
struct sockaddr_in serv_addr;
usbFdWrite = socket(AF_INET, SOCK_STREAM, 0);
usbFdRead = socket(AF_INET, SOCK_STREAM, 0);
assert(usbFdWrite >=0);
assert(usbFdRead >=0);
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(USB_LINK_SOCKET_PORT);
if (connect(usbFdWrite, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
exit(1);
}
printf("this is working\n");
return 0;
#else
usbFdRead= open(devPathRead, O_RDWR);
if(usbFdRead < 0)
{
return -1;
}
// set tty to raw mode
struct termios tty;
speed_t spd;
int rc;
rc = tcgetattr(usbFdRead, &tty);
if (rc < 0) {
usbFdRead = -1;
return -2;
}
spd = B115200;
cfsetospeed(&tty, (speed_t)spd);
cfsetispeed(&tty, (speed_t)spd);
cfmakeraw(&tty);
rc = tcsetattr(usbFdRead, TCSANOW, &tty);
if (rc < 0) {
usbFdRead = -1;
return -2;
}
usbFdWrite= open(devPathWrite, O_RDWR);
if(usbFdWrite < 0)
{
usbFdWrite = -1;
return -2;
}
// set tty to raw mode
rc = tcgetattr(usbFdWrite, &tty);
if (rc < 0) {
usbFdWrite = -1;
return -2;
}
spd = B115200;
cfsetospeed(&tty, (speed_t)spd);
cfsetispeed(&tty, (speed_t)spd);
cfmakeraw(&tty);
rc = tcsetattr(usbFdWrite, TCSANOW, &tty);
if (rc < 0) {
usbFdWrite = -1;
return -2;
}
return 0;
#endif /*USE_LINK_JTAG*/
#else
*fd = usblink_open(devPathWrite);
if (*fd == 0)
{
/* could fail due to port name change */
return -1;
}
if(*fd)
return 0;
else
return -1;
#endif /*USE_USB_VSC*/
}
int UsbLinkPlatformInit(int loglevel)
{
usb_loglevel = loglevel;
#if (defined(_WIN32) || defined(_WIN64))
initialize_usb_boot();
#endif
return 0;
}
void deallocateData(void* ptr,uint32_t size, uint32_t alignment)
{
if (!ptr)
return;
#if (defined(_WIN32) || defined(_WIN64) )
_aligned_free(ptr);
#else
free(ptr);
#endif
}
void* allocateData(uint32_t size, uint32_t alignment)
{
void* ret = NULL;
#if (defined(_WIN32) || defined(_WIN64) )
ret = _aligned_malloc(size, alignment);
#else
if (posix_memalign(&ret, alignment, size) != 0) {
perror("memalign failed");
}
#endif
return ret;
}
/*#################################################################################
################################### PCIe FUNCTIONS ################################
##################################################################################*/
static int write_pending = 0;
static int read_pending = 0;
static int pcie_host_write(void *f,
void *data, int size,
unsigned int timeout)
{
#define CHUNK_SIZE_BYTES (5ULL * 1024ULL * 1024ULL)
while (size)
{
write_pending = 1;
size_t chunk = size < CHUNK_SIZE_BYTES ? size : CHUNK_SIZE_BYTES;
int num_written = pcie_write(f, data, chunk, timeout);
write_pending = 0;
if (num_written == -EAGAIN) {
// Let read commands be submitted
if (read_pending > 0) {
usleep(1000);
}
continue;
}
if (num_written < 0) {
return num_written;
}
data = ((char*) data) + num_written;
/**
* num_written is always not greater than size
*/
size -= num_written;
}
return 0;
#undef CHUNK_SIZE_BYTES
}
static int pcie_host_read(void *f,
void *data, int size,
unsigned int timeout)
{
while (size)
{
read_pending = 1;
int num_read = pcie_read(f, data, size, timeout);
read_pending = 0;
if (num_read == -EAGAIN) {
// Let write commands be submitted
if (write_pending > 0) {
usleep(1000);
}
continue;
}
if(num_read < 0) {
return num_read;
}
data = ((char *)data) + num_read;
/**
* num_read is always not greater than size
*/
size -= num_read;
}
return 0;
}
static int pcie_host_open(const char* devPathRead,
const char* devPathWrite,
void** fd )
{
return pcie_init(devPathWrite, fd);
}
static int pcie_host_close(void *f)
{
pcie_close(f);
return 0;
}
/*############################### FUNCTION ARRAYS #################################*/
/*These arrays hold the write/read/open/close operation functions
specific for each communication protocol.
Add more functions if adding another protocol*/
int (*write_fcts[NMB_OF_PROTOCOLS])(void*, void*, int, unsigned int) = \
{USBLinkWrite, USBLinkWrite, pcie_host_write};
int (*read_fcts[NMB_OF_PROTOCOLS])(void*, void*, int, unsigned int) = \
{USBLinkRead, XLinkRead, pcie_host_read};
int (*open_fcts[NMB_OF_PROTOCOLS])(const char*, const char*, void**) = \
{UsbLinkPlatformConnect, UsbLinkPlatformConnect, pcie_host_open};
int (*close_fcts[NMB_OF_PROTOCOLS])(void*) = \
{USBLinkPlatformResetRemote, USBLinkPlatformResetRemote, pcie_host_close};
/*#################################################################################
###################################################################################
###################################### EXTERNAL ###################################
###################################################################################
##################################################################################*/
int XLinkPlatformConnect(const char* devPathRead, const char* devPathWrite, void** fd)
{
return open_fcts[gl_protocol](devPathRead, devPathWrite, fd);
}
int XLinkWrite(void* fd, void* data, int size, unsigned int timeout)
{
return write_fcts[gl_protocol](fd, data, size, timeout);
}
int XLinkRead(void* fd, void* data, int size, unsigned int timeout)
{
return read_fcts[gl_protocol](fd, data, size, timeout);
}
int XLinkPlatformCloseRemote(void *fd)
{
return close_fcts[gl_protocol](fd);
}
int XLinkPlatformInit(XLinkProtocol_t protocol, int loglevel)
{
gl_protocol = protocol;
usb_loglevel = loglevel;
#if (defined(_WIN32) || defined(_WIN64))
initialize_usb_boot();
#endif
return 0;
}
static int getDeviceName(int index, char* name, int nameSize , int pid)
{
if (index < 0 ) {
perror("Incorrect index value\n");
return X_LINK_PLATFORM_ERROR;
}
switch (gl_protocol) {
case PCIE: {
return pcie_find_device_port(index, name, nameSize);
}
case IPC:
perror("IPC not supported, switched to USB\n");
break;
case USB_CDC:
perror("USB_CDC not supported, switch to USB\n");
break;
case USB_VSC:
/*should have common device(temporary moved to 'default')*/
default:
{
// At the moment there is no situation where you may need a non standard vid
int vid = AUTO_PID;
#if (!defined(_WIN32) && !defined(_WIN64))
uint16_t bcdusb = -1;
usbBootError_t rc = usb_find_device_with_bcd(index, name, nameSize, 0, vid, pid, &bcdusb);
#else
usbBootError_t rc = usb_find_device(index, name, nameSize, 0, vid, pid);
#endif
// TODO convert usb error to xLinkPlatformErrorCode
return parseUsbBootError(rc);
}
}
return X_LINK_PLATFORM_SUCCESS;
}
int XLinkPlatformGetDeviceName(int index, char* name, int nameSize)
{
return getDeviceName(index, name, nameSize, AUTO_PID);
}
int XLinkPlatformGetDeviceNameExtended(int index, char* name, int nameSize, int pid)
{
return getDeviceName(index, name, nameSize, pid);
}
int XLinkPlatformBootRemote(const char* deviceName, const char* binaryPath)
{
/* Don't try to boot FW if PCIe */
#ifdef USE_PCIE
return 0;
#else
long filesize;
FILE *fp;
char *tx_buf;
char subaddr[28+2];
int rc;
#ifndef USE_USB_VSC
if (usbFdRead != -1){
close(usbFdRead);
usbFdRead = -1;
}
if (usbFdWrite != -1){
close(usbFdWrite);
usbFdWrite = -1;
}
#endif /*USE_USB_VSC*/
// Load the executable
fp = fopen(binaryPath, "rb");
if(fp == NULL)
{
if(usb_loglevel)
perror(binaryPath);
return -7;
}
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);
if(filesize <= 0 || !(tx_buf = (char*)malloc(filesize)))
{
if(usb_loglevel)
perror("buffer");
fclose(fp);
return -3;
}
if(fread(tx_buf, 1, filesize, fp) != filesize)
{
if(usb_loglevel)
perror(binaryPath);
fclose(fp);
free(tx_buf);
return -7;
}
fclose(fp);
// This will be the string to search for in /sys/dev/char links
int chars_to_write = snprintf(subaddr, 28, "-%s:", deviceName);
if(chars_to_write >= 28) {
printf("Path to your boot util is too long for the char array here!\n");
}
// Boot it
rc = usb_boot(deviceName, tx_buf, filesize);
free(tx_buf);
if(rc)
{
return rc;
}
if(usb_loglevel > 1)
fprintf(stderr, "Boot successful, device address %s\n", deviceName);
return 0;
#endif // USE_PCIE
}