-
Notifications
You must be signed in to change notification settings - Fork 48
/
usbdriver.c
executable file
·628 lines (530 loc) · 16.4 KB
/
usbdriver.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
#include "usbdriver.h"
#include "FlashCommand.h"
#include "project.h"
#ifdef __FreeBSD__
#include <libusb.h>
#else
#include <libusb-1.0/libusb.h>
#endif
#include <string.h>
unsigned int m_nbDeviceDetected = 0;
unsigned char DevIndex = 0;
extern volatile bool g_bIsSF600[16];
extern volatile bool g_bIsSF700[16];
extern volatile bool g_bIsSF600PG2[16];
extern int g_CurrentSeriase;
extern char g_board_type[8];
extern int g_firmversion;
extern CHIP_INFO Chip_Info;
extern unsigned int g_uiDevNum;
extern bool isSendFFsequence;
#define SerialFlash_FALSE -1
#define SerialFlash_TRUE 1
static int dev_index;
static usb_device_entry_t usb_device_entry[MAX_Dev_Index];
static libusb_device_handle* dediprog_handle[MAX_Dev_Index];
static libusb_context* ctx = NULL;
/*
* Filter based on a bus:device combo, to make sure we *only* touch
* that device
*/
unsigned g_usb_devnum = -1;
unsigned g_usb_busnum = -1;
bool Is_NewUSBCommand(int Index)
{
if (is_SF100nBoardVersionGreaterThan_5_5_0(Index) || is_SF600nBoardVersionGreaterThan_6_9_0(Index) || is_SF700_Or_SF600PG2(Index)) {
return true;
}
return false;
}
extern unsigned char GetFPGAVersion(int Index);
void usb_dev_init(void)
{
int r;
r = libusb_init(&ctx);
if (r < 0) {
printf("initialization failed!");
return;
}
#if LIBUSB_API_VERSION < 0x01000106
libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO);
#else
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
#endif
}
void usb_db_init(void)
{
int i;
for (i = 0; i < MAX_Dev_Index; i++) {
usb_device_entry[i].valid = 0;
dediprog_handle[i] = NULL;
}
}
void AssignSF600orSF700var(int Index)
{
if (Index == -1)
Index = DevIndex;
g_bIsSF600[Index] = false;
g_bIsSF700[Index] = false;
g_bIsSF600PG2[Index] = false;
CNTRPIPE_RQ rq;
unsigned char vBuffer[32];
int fw[3];
memset(vBuffer, '\0', 32);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = 0x08;
rq.Value = 0;
rq.Index = 0;
rq.Length = 32;
if (InCtrlRequest(&rq, vBuffer, 32, Index) == SerialFlash_FALSE)
return;
memcpy(g_board_type, &vBuffer[0], 8);
//memcpy(g_firmversion,&vBuffer[10],8);
sscanf((char*)&vBuffer[8], "V:%d.%d.%d", &fw[0], &fw[1], &fw[2]);
g_firmversion = ((fw[0] << 16) | (fw[1] << 8) | fw[2]);
if (strstr(g_board_type, "SF700") != NULL)
g_bIsSF700[Index] = true;
else if (strstr(g_board_type, "SF600") != NULL)
{
if (strstr(g_board_type, "SF600PG2") != NULL)
{
g_bIsSF600PG2[Index] = true;
Sleep(2);
}
else
{
g_bIsSF600[Index] = true;
}
}
GetFPGAVersion(Index);
}
int get_usb_dev_cnt(void)
{
return dev_index;
}
/* Might be useful for other USB devices as well. static for now. */
static int FindUSBDevice(void)
{
unsigned int vid = 0x0483;
unsigned int pid = 0xdada;
usb_db_init();
ssize_t count;
libusb_device** devs;
count = libusb_get_device_list(ctx, &devs);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device* device = devs[idx];
struct libusb_device_descriptor desc = { 0 };
int rc = libusb_get_device_descriptor(device, &desc);
if (rc < 0) {
printf("Error getting device descriptor\n");
break;
}
if (desc.idVendor == vid && desc.idProduct == pid) {
usb_device_entry[dev_index].usb_device = device;
usb_device_entry[dev_index].valid = 1;
dev_index++;
}
}
printf(" \n");
return dev_index;
}
int OutCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index)
{
int requesttype;
int ret = 0;
if (Index == -1)
Index = DevIndex;
if ((rq->Function != URB_FUNCTION_VENDOR_ENDPOINT) && ((g_bIsSF600[Index] == true) || is_SF700_Or_SF600PG2(Index) == true))
return true;
requesttype = 0x00;
if (rq->Direction == VENDOR_DIRECTION_IN)
requesttype |= 0x80;
if (rq->Function == URB_FUNCTION_VENDOR_DEVICE)
requesttype |= 0x40;
if (rq->Function == URB_FUNCTION_VENDOR_INTERFACE)
requesttype |= 0x41;
if (rq->Function == URB_FUNCTION_VENDOR_ENDPOINT)
requesttype |= 0x42;
if (rq->Function == URB_FUNCTION_VENDOR_OTHER)
requesttype |= 0x43;
if (dediprog_handle[Index]) {
ret = libusb_control_transfer(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, buf, buf_size, DEFAULT_TIMEOUT);
} else {
printf("no device");
}
if (ret != buf_size) {
printf("Error: %s\n", libusb_strerror(ret));
return -1;
}
return ret;
}
int InCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index)
{
unsigned int requesttype;
unsigned int ret = 0;
if ((rq->Function != URB_FUNCTION_VENDOR_ENDPOINT) && ((g_bIsSF600[Index] == true) || is_SF700_Or_SF600PG2(Index) == true))
return true;
if (Index == -1)
Index = DevIndex;
if (sizeof(buf) == 0)
return 0;
requesttype = 0x00;
if (rq->Direction == VENDOR_DIRECTION_IN)
requesttype |= 0x80;
if (rq->Function == URB_FUNCTION_VENDOR_DEVICE)
requesttype |= 0x40;
if (rq->Function == URB_FUNCTION_VENDOR_INTERFACE)
requesttype |= 0x41;
if (rq->Function == URB_FUNCTION_VENDOR_ENDPOINT)
requesttype |= 0x42;
if (rq->Function == URB_FUNCTION_VENDOR_OTHER)
requesttype |= 0x43;
if (dediprog_handle[Index]) {
ret = libusb_control_transfer(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, buf, buf_size, DEFAULT_TIMEOUT);
} else {
printf("no device");
}
if (ret != 0 /*!= buf_size*/)
{
return ret;
}
else
{
#if 0
printf("InCtrlRequest ---ret=%X\n",ret);
printf("evInCtrlRequesty ---Control Pipe output error!\n");
printf("InCtrlRequest ---rq->Direction=%lX\n",rq->Direction);
printf("InCtrlRequest ---rq->Function=%X\n",rq->Function);
printf("InCtrlRequest ---rq->Request=%X\n",rq->Request);
printf("InCtrlRequest ---rq->Value=%X\n",rq->Value);
printf("InCtrlRequest ---rq->Index=%X\n",rq->Index);
printf("InCtrlRequest ---rq->Length=%lX\n",rq->Length);
printf("InCtrlRequest ---buf_size=%lX\n",buf_size);
printf("InCtrlRequest ---buf[0]=%X\n",buf[0]);
//printf("g_bIsSF600=%d\n",g_bIsSF600);
#endif
printf("Control Pipe input error!\n");
return -1;
}
return ret;
}
// part of USB driver , open usb pipes for data transfor
// should be called after usb successfully opens pipes.
int dediprog_start_appli(int Index)
{
// if((g_bIsSF600[Index] != false) || is_SF700_Or_SF600PG2(Index)!=false)
// return 1;
CNTRPIPE_RQ rq;
int ret;
unsigned char vInstruction;
// special instruction
vInstruction = 0;
rq.Function = URB_FUNCTION_VENDOR_OTHER;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = 0xb;
rq.Value = 0x00;
rq.Index = 0x00;
rq.Length = 0x01;
ret = OutCtrlRequest(&rq, &vInstruction, 1, Index);
return ret;
}
int dediprog_get_chipid(int Index)
{
//printf("\n===>usbdrive.c --- dediprog_get_chipid\n");
CNTRPIPE_RQ rq;
int ret;
unsigned char vInstruction[3];
memset(vInstruction, 0, 3);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = 0x1;
rq.Value = 0xff;
if (Is_NewUSBCommand(Index))
rq.Index = 0;
else
rq.Index = 0x1;
rq.Length = 0x1;
vInstruction[0] = 0x9f;
ret = OutCtrlRequest(&rq, vInstruction, 1, Index);
// special instruction
memset(vInstruction, 0, 3);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = 0x1;
rq.Value = 0x0;
rq.Index = 0x00;
rq.Length = 0x03;
ret = InCtrlRequest(&rq, vInstruction, 3, Index);
return ret;
}
// return size read
// if fail , return -1
//long CUSB::BulkPipeRead(PBYTE pBuff, UINT sz, UINT timeOut) const
int BulkPipeRead(unsigned char* pBuff, unsigned int timeOut, int Index)
{
int ret, actual_length;
if (Index == -1)
Index = DevIndex;
unsigned long cnRead = 512;
ret = libusb_bulk_transfer(dediprog_handle[Index], 2 | LIBUSB_ENDPOINT_IN, pBuff, cnRead, &actual_length, DEFAULT_TIMEOUT);
if (ret != 0) //libusb_bulk_transfer return false
return 0;
return cnRead;
}
//long CUSB::BulkPipeWrite(PBYTE pBuff, UINT sz, UINT timeOut) const
int BulkPipeWrite(unsigned char* pBuff, unsigned int size, unsigned int timeOut, int Index)
{
int ret, actual_length;
int nWrite = 512;
unsigned char pData[512];
memset(pData, 0xFF, 512); // fill buffer with 0xFF
memcpy(pData, pBuff, size);
if (Index == -1)
Index = DevIndex;
ret = libusb_bulk_transfer(dediprog_handle[Index], ((g_bIsSF600[Index] == true) || is_SF700_Or_SF600PG2(Index) == true) ? 0x01 : 0x02, pData, nWrite, &actual_length, DEFAULT_TIMEOUT);
nWrite = ret;
return nWrite;
}
int dediprog_set_spi_voltage(int v, int Index)
{
int ret = 0;
// int voltage_selector;
CNTRPIPE_RQ rq;
// unsigned char vBuffer[12];
if (0 == v)
Sleep(200);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = SET_VCC;
rq.Length = 0;
if (Is_NewUSBCommand(Index)) {
rq.Value = v;
rq.Index = 0;
} else {
rq.Value = v;
rq.Index = 0x04 | g_CurrentSeriase; // ID detect mode
}
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if (0 != v) {
Sleep(200);
if (isSendFFsequence) {
unsigned char v[4] = { 0xff, 0xff, 0xff, 0xff };
FlashCommand_SendCommand_OutOnlyInstruction(v, 4, Index);
}
}
return ret;
}
int dediprog_set_vpp_voltage(int volt, int Index)
{
int ret;
int voltage_selector;
CNTRPIPE_RQ rq;
switch (volt) {
case 0:
/* Admittedly this one is an assumption. */
voltage_selector = 0x0;
break;
case 9:
voltage_selector = 0x1;
break;
case 12:
voltage_selector = 0x2;
break;
default:
return 1;
}
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = 0x03;
rq.Value = voltage_selector;
rq.Index = 0;
rq.Length = 0x0;
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if (ret == SerialFlash_FALSE) {
// printf("Command Set VPP Voltage 0x%x failed!\n", voltage_selector);
return 1;
}
return 0;
}
int dediprog_set_spi_clk(int khz, int Index)
{
return 0;
int ret;
int hz_selector;
CNTRPIPE_RQ rq;
switch (khz) {
case 24000:
/* Admittedly this one is an assumption. */
hz_selector = 0x0;
break;
case 8000:
hz_selector = 0x1;
break;
case 12000:
hz_selector = 0x2;
break;
case 3000:
hz_selector = 0x3;
break;
case 2180:
hz_selector = 0x4;
break;
case 1500:
hz_selector = 0x5;
break;
case 750:
hz_selector = 0x6;
break;
case 375:
hz_selector = 0x7;
break;
default:
printf("Unknown clk %i KHz! Aborting.\n", khz);
return 1;
}
// printf("Setting SPI clk to %u.%03u MHz\n", khz / 1000,
// khz % 1000);
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = 0x61;
rq.Value = hz_selector;
rq.Index = 0;
rq.Length = 0x0;
ret = OutCtrlRequest(&rq, NULL, 0, Index);
if (ret == SerialFlash_FALSE) {
// printf("Command Set SPI clk 0x%x failed!\n", hz_selector);
return 1;
}
return 0;
}
int usb_driver_init(void)
{
//printf("\n===>usbdrive.c --- usb_driver_init\n");
// struct usb_bus *bus;
// struct usb_device *dev;
bool result = false;
int device_cnt = 0;
int ret;
for (int i = 0; i < MAX_Dev_Index; i++) {
dediprog_handle[i] = NULL;
}
usb_dev_init();
device_cnt = FindUSBDevice();
if (g_uiDevNum == 0) {
for (int i = 0; i < device_cnt; i++) {
if (usb_device_entry[i].valid == 0) {
printf("Error: Programmers are not connected.\n");
return 0;
}
ret = libusb_open(usb_device_entry[i].usb_device, &dediprog_handle[i]);
if (dediprog_handle[i] == NULL) {
printf("Error: Programmers are not connected.\n");
return 0;
}
ret = libusb_set_configuration(dediprog_handle[i], 1);
if (ret) {
printf("Error: Programmers USB set configuration: 0x%x.\n", ret);
return 0;
}
ret = libusb_claim_interface(dediprog_handle[i], 0);
if (ret) {
printf("Error: Programmers USB claim interface: 0x%x.\n", ret);
return 0;
}
dediprog_start_appli(i);
AssignSF600orSF700var(i);
result = (dediprog_handle[i] != NULL);
}
} else {
if (usb_device_entry[g_uiDevNum - 1].valid == 0) {
printf("Error: Programmers are not connected.\n");
return 0;
}
ret = libusb_open(usb_device_entry[g_uiDevNum - 1].usb_device, &dediprog_handle[g_uiDevNum - 1]);
if (dediprog_handle[g_uiDevNum - 1] == NULL) {
printf("Error: Programmers are not connected.\n");
return 0;
}
//printf("dediprog_handle[%d]=%p\n", g_uiDevNum - 1, dediprog_handle[g_uiDevNum - 1]);
ret = libusb_set_configuration(dediprog_handle[g_uiDevNum - 1], 1);
if (ret) {
printf("Error: Programmers USB set configuration: 0x%x.\n", ret);
return 0;
}
ret = libusb_claim_interface(dediprog_handle[g_uiDevNum - 1], 0);
if (ret) {
printf("Error: Programmers USB claim interface: 0x%x.\n", ret);
return 0;
}
dediprog_start_appli(g_uiDevNum - 1);
AssignSF600orSF700var(g_uiDevNum - 1);
result = (dediprog_handle[g_uiDevNum - 1] != NULL);
}
return result; //((dediprog_handle[i] != NULL)? 1:0);
}
int usb_driver_release(void)
{
for (int i = 0; i < dev_index; i++) {
if (dediprog_handle[i] == NULL)
return 0;
libusb_release_interface(dediprog_handle[i], 0);
libusb_close(dediprog_handle[i]);
}
return 0;
}
bool Is_usbworking(int Index)
{
usleep(1000); // unknow reson
return ((dediprog_handle[Index] != NULL) ? true : false);
}
//long long flash_ReadId(boost::tuple<unsigned int /*RDID code*/, unsigned int/*inByteCount*/, unsigned int/*outByteCount*/> command,int Index)
long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size, int Index)
{
// read status
// if(! m_usb.is_open() )
// return 0 ;
// RDID is not decoded when in DP mode
// so , first release from Deep Power Down mode or read signature
// DoRDP() ;
// send request
CNTRPIPE_RQ rq;
unsigned char vInstruction[8];
unsigned long rc = 0;
int i;
// first control packet
vInstruction[0] = read_id_code;
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_OUT;
rq.Request = TRANSCEIVE;
if (Is_NewUSBCommand(Index)) {
rq.Value = RESULT_IN;
rq.Index = 0;
} else {
rq.Value = RFU;
rq.Index = RESULT_IN;
}
rq.Length = 1;
if (OutCtrlRequest(&rq, vInstruction, (unsigned long)1, Index) == SerialFlash_FALSE)
return rc; //OutCtrlRequest() return error
// second control packet : fetch data
memset(vInstruction, 0, sizeof(vInstruction));
rq.Function = URB_FUNCTION_VENDOR_ENDPOINT;
rq.Direction = VENDOR_DIRECTION_IN;
rq.Request = TRANSCEIVE;
if (Is_NewUSBCommand(Index)) {
rq.Value = 0x01;
rq.Index = 0;
} else {
rq.Value = CTRL_TIMEOUT;
rq.Index = NO_REGISTER;
}
rq.Length = out_data_size;
if (InCtrlRequest(&rq, vInstruction, (unsigned long)out_data_size, Index) == SerialFlash_FALSE)
return rc;
for (i = 0; i < out_data_size; i++) {
rc = (rc << 8) + vInstruction[i];
}
// printf("\n(Simon)Flash ID 0x%x (0x%x, %d)\n",rc, read_id_code, out_data_size);
return rc;
}