pureusb.c 25 KB
Newer Older
David Košenina's avatar
rc4  
David Košenina committed
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
////////////////////////////////////////////////////////////////////////////////
//    ScopeFun Oscilloscope ( http://www.scopefun.com )
//    Copyright (C) 2016 David Košenina
//
//    This file is part of ScopeFun Oscilloscope.
//
//    ScopeFun Oscilloscope 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 3 of the License, or
//    (at your option) any later version.
//
//    ScopeFun Oscilloscope 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 ScopeFun Oscilloscope.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
#include<core/purec/purec.h>
#include<core/purec/pureusb.h>
#include<libusb.h>
#include<ezusb.h>
#include<stdio.h>
#include<stdarg.h>

////////////////////////////////////////////////////////////////////////////////
//
// log
//
////////////////////////////////////////////////////////////////////////////////
void logerror(const char* format, ...)
{
    char str[2049] = { 0 };
    va_list argptr;
    va_start(argptr, format);
    vsprintf(str, format, argptr);
    va_end(argptr);
    printf(str);
}

////////////////////////////////////////////////////////////////////////////////
//
// usbRegisterCallback
//
////////////////////////////////////////////////////////////////////////////////
int usbRegisterCallback(UsbContext* ctx, usbCallback callback)
{
    ctx->callback = callback;
    return PUREUSB_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
//
//  hotplug
//
////////////////////////////////////////////////////////////////////////////////
int LIBUSB_CALL hotplug_callback(libusb_context* ctx, libusb_device* device, libusb_hotplug_event event, void* user_data)
{
    return LIBUSB_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
//
// FX3
//
////////////////////////////////////////////////////////////////////////////////

int usbFx3CheckFirmwareID(UsbContext* ctx, char* data)
{
    if(usbFxxIsConnected(ctx))
    {
        // bReqType: 0xC0, bRequest : 0xB0, wLength : 0x10
        int length = 0x10;
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          0xC0,
                                          0xB0,
                                          0,
                                          0,
                                          (unsigned char*)data,
                                          length,
                                          1000);
        if(ret == length)
        {
            return PUREUSB_SUCCESS;
        }
    }
    return PUREUSB_FAILURE;
}

unsigned short endianSwapLoHi(unsigned short value)
{
    unsigned short low  = (value & 0x00FF);
    unsigned short high = (value & 0xFF00);
    return (low << 8) | (high >> 8);
}

int writeData(UsbContext* ctx, int address, char* buffer, int len)
{
    unsigned short hi = endianSwapLoHi((address >> 16) & 0xffff);
    unsigned short lo = endianSwapLoHi(address & 0xffff);
    int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                      0x40,
                                      0xA0,
                                      lo,
                                      hi,
                                      buffer,
                                      len,
                                      10000);
    char verify[4096] = {0};
    ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                  0xC0,
                                  0xA0,
                                  lo,
                                  hi,
                                  verify,
                                  len,
                                  10000);
    ret = memcmp((void*)buffer, (void*)verify, len);
    if(ret != 0)
    {
        int debug = 1;
    }
}

int usbFx3UploadFirmwareToFx3(UsbContext* ctx, const char* path)
{
    if(usbFxxIsConnected(ctx))
    {
        int res = 0;
        res = ezusb_load_ram((libusb_device_handle*)ctx->device, path, FX_TYPE_FX3, IMG_TYPE_IMG, 0);
    }
David Košenina's avatar
David Košenina committed
134
    return 0;
David Košenina's avatar
rc4  
David Košenina committed
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
}
int usbFx3UploadFirmwareToFpga(UsbContext* ctx, unsigned char* buffer, int size, int swapBits)
{
    int data[4] = { 0 };
    if(usbFxxIsConnected(ctx))
    {
        data[0] = 0x7c320500;
        int length = 0x10;
        // bReqType: 0x40, bRequest : 0xB2, wLength : 0x10
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          0x40,
                                          0xB2,
                                          0,
                                          0,
                                          (unsigned char*)data,
                                          length,
                                          1000);
        if(ret != length)
        {
            return PUREUSB_FAILURE;
        }
        // upload
        if(swapBits)
        {
            for(int i = 0; i < size; i++)
            {
                unsigned char temp = buffer[i];
                unsigned char bit0 = temp & 0x1;
                unsigned char bit1 = (temp >> 1) & 0x1;
                unsigned char bit2 = (temp >> 2) & 0x1;
                unsigned char bit3 = (temp >> 3) & 0x1;
                unsigned char bit4 = (temp >> 4) & 0x1;
                unsigned char bit5 = (temp >> 5) & 0x1;
                unsigned char bit6 = (temp >> 6) & 0x1;
                unsigned char bit7 = (temp >> 7) & 0x1;
                buffer[i] = (bit0 << 7) | (bit1 << 6) | (bit2 << 5) | (bit3 << 4) | (bit4 << 3) | (bit5 << 2) | (bit6 << 1) | (bit7 << 0);
            }
        }
        int transfered = 0;
        ret = libusb_bulk_transfer((libusb_device_handle*)ctx->device, 2 | LIBUSB_ENDPOINT_OUT, buffer, size, &transfered, 30000);
        if(ret > 0)
        {
            return PUREUSB_FAILURE;
        }
        // wait for the upload to complete ... todo: check
        cSleep(1000);
        // bReqType: 0xC0, bRequest : 0xB1, wLength : 0x1
        data[0] = 0;
        length  = 1;
        ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                      0xC0,
                                      0xB1,
                                      0,
                                      0,
                                      (unsigned char*)data,
                                      length,
                                      10000);
        if(ret != length)
        {
            return PUREUSB_FAILURE;
        }
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
int usbFx3ReadEEPROMFirmwareID(UsbContext* ctx, unsigned char* buffer, int size, int readadress)
{
   int transfered = 0;
   if (usbFxxIsConnected(ctx))
   {
      // bReqType: 0xC0, bRequest : 0xB0, wLength - MAX : 0x1000
      int read = libusb_control_transfer((libusb_device_handle*)ctx->device,
            0xC0,
            0xB0,
            (readadress >> 16) & 0xffff,
            (readadress) & 0xffff,
            buffer,
            size,
            100000);
      if (read == size)
      {
         return PUREUSB_SUCCESS;
      }
   }
   return PUREUSB_FAILURE;
}
David Košenina's avatar
rc4  
David Košenina committed
222 223 224 225 226 227
int usbFx3ReadEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int readadress)
{
    int transfered = 0;
    if(usbFxxIsConnected(ctx))
    {
        // bReqType: 0xC0, bRequest : 0xBB, wLength - MAX : 0x1000
David Košenina's avatar
David Košenina committed
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
        int packet = 1024;
        int count  = size / packet;
        int left   = size % packet;
        int read   = 0;
        for (int i = 0; i < count; i++)
        {
           read += libusb_control_transfer((libusb_device_handle*)ctx->device,
              0xC0,
              0xBB,
              (readadress >> 16) & 0xffff,
              (readadress) & 0xffff,
              buffer + i * packet,
              packet,
              100000);
           readadress += packet;
        }
        read += libusb_control_transfer((libusb_device_handle*)ctx->device,
           0xC0,
           0xBB,
           (readadress >> 16) & 0xffff,
           (readadress) & 0xffff,
           buffer + count * packet,
           left,
           100000);
        if(read == size)
David Košenina's avatar
rc4  
David Košenina committed
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
        {
            return PUREUSB_SUCCESS;
        }
    }
    return PUREUSB_FAILURE;
}

int usbFx3Reset(UsbContext* ctx)
{
    if(usbFxxIsConnected(ctx))
    {
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          0x40,
                                          0xEE,
                                          0,
                                          0,
                                          0,
                                          0,
                                          10000);
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

int writeDataEeprom(UsbContext* ctx, int address, char* buffer, int len)
{
    unsigned short hi = (address >> 16) & 0xffff;
    unsigned short lo = address & 0xffff;
    int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                      LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
                                      0xBA,
                                      hi,
                                      lo,
                                      buffer,
                                      len,
                                      10000);
    return ret == len ? 0 : 1;
}
int usbFx3WriteEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int writeadress)
{
    if(usbFxxIsConnected(ctx))
    {
        // write eeprom
David Košenina's avatar
David Košenina committed
296
        int chunk = 1024;
David Košenina's avatar
rc4  
David Košenina committed
297 298
        int loop = size / chunk;
        int left = size % chunk;
David Košenina's avatar
David Košenina committed
299
        int ret  = 0;
David Košenina's avatar
rc4  
David Košenina committed
300 301
        for(int i = 0; i < loop; i++)
        {
David Košenina's avatar
David Košenina committed
302
            ret += writeDataEeprom(ctx,
David Košenina's avatar
rc4  
David Košenina committed
303 304 305 306
                                      writeadress + (chunk * i),
                                      buffer + (chunk * i),
                                      chunk);
        }
David Košenina's avatar
David Košenina committed
307 308 309 310
        ret += writeDataEeprom(ctx,
                               writeadress + (chunk * loop),
                               buffer + (chunk * loop),
                               left);
David Košenina's avatar
rc4  
David Košenina committed
311 312 313 314 315 316 317 318
        if(ret == 0)
        {
            return PUREUSB_SUCCESS;
        }
    }
    return PUREUSB_FAILURE;
}

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
int usbFx3WriteLockableEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int writeadress)
{
   if (usbFxxIsConnected(ctx))
   {
      unsigned short hi = 8;
      unsigned short lo = writeadress & 0x00ff;
      int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
         LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
         0xBA,
         hi,
         lo,
         buffer,
         size,
         10000);
      return ret == size ? 0 : 1;
      if (ret == 0)
      {
         return PUREUSB_SUCCESS;
      }
   }
   return PUREUSB_FAILURE;
}

int usbFx3ReadLockableEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int readadress)
{
   int transfered = 0;
   if (usbFxxIsConnected(ctx))
   {
      unsigned short hi = 8;
      unsigned short lo = readadress & 0x00ff;
      int read = libusb_control_transfer((libusb_device_handle*)ctx->device,
         0xC0,
         0xBB,
         hi,
         lo,
         buffer,
         size,
         100000);
      if (read == size)
      {
         return PUREUSB_SUCCESS;
      }
   }
   return PUREUSB_FAILURE;
}

int usbFx3LockLockableEEPROM(UsbContext* ctx)
{
   int transfered = 0;
   if (usbFxxIsConnected(ctx))
   {
      unsigned short hi = 8;
      unsigned short lo = 0x400;
      char byte = 0x2;
      int read = libusb_control_transfer((libusb_device_handle*)ctx->device,
         LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
         0xBA,
         hi,
         lo,
         &byte,
         1,
         100000);
      if (read == 1)
      {
         return PUREUSB_SUCCESS;
      }
   }
   return PUREUSB_FAILURE;
}

David Košenina's avatar
rc4  
David Košenina committed
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
////////////////////////////////////////////////////////////////////////////////
//
// FX2
//
////////////////////////////////////////////////////////////////////////////////
int usbFx2UploadFirmwareToFx2(UsbContext* ctx, const char* path)
{
    if(usbFxxIsConnected(ctx))
    {
        int res = 0;
        res = ezusb_load_ram((libusb_device_handle*)ctx->device, path, FX_TYPE_FX2LP, IMG_TYPE_HEX, 0);
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

int usbFx2UploadFirmwareToFpga(UsbContext* ctx, unsigned char* buffer, int size, int swapBits)
{
    if(usbFxxIsConnected(ctx))
    {
        unsigned char data   = 0;
        int           length = 0;
        int ret = 0;
        // go to reset mode
        ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                      LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
                                      0xB2,
                                      0,
                                      0,
                                      &data,
                                      length,
                                      1000);
        if(ret != length)
        {
            return PUREUSB_FAILURE;
        }
        // upload
        if(swapBits)
        {
            for(int i = 0; i < size; i++)
            {
                unsigned char temp = buffer[i];
                unsigned char bit0 = temp & 0x1;
                unsigned char bit1 = (temp >> 1) & 0x1;
                unsigned char bit2 = (temp >> 2) & 0x1;
                unsigned char bit3 = (temp >> 3) & 0x1;
                unsigned char bit4 = (temp >> 4) & 0x1;
                unsigned char bit5 = (temp >> 5) & 0x1;
                unsigned char bit6 = (temp >> 6) & 0x1;
                unsigned char bit7 = (temp >> 7) & 0x1;
                buffer[i] = (bit0 << 7) | (bit1 << 6) | (bit2 << 5) | (bit3 << 4) | (bit4 << 3) | (bit5 << 2) | (bit6 << 1) | (bit7 << 0);
            }
        }
        int transfered = 0;
        ret = libusb_bulk_transfer((libusb_device_handle*)ctx->device, 2 | LIBUSB_ENDPOINT_OUT, buffer, size, &transfered, 1000);
        if(ret > 0)
        {
            return PUREUSB_FAILURE;
        }
        // out of reset mode
        ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                      LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
                                      0xB3,
                                      0,
                                      0,
                                      &data,
                                      length,
                                      10000);
        if(ret != length)
        {
            return PUREUSB_FAILURE;
        }
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

int usbFx2ReadEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int readaddress)
{
    int transfered = 0;
    if(usbFxxIsConnected(ctx))
    {
        // read eeprom
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_DEVICE,
                                          0xB4,
                                          (readaddress >> 16) & 0xffff,
                                          (readaddress) & 0xffff,
                                          buffer,
                                          size,
                                          100000);
        if(ret == size)
        {
            return PUREUSB_SUCCESS;
        }
    }
    return PUREUSB_FAILURE;
}

int usbFx2WriteEEPROM(UsbContext* ctx, unsigned char* buffer, int size, int writeaddress)
{
    if(usbFxxIsConnected(ctx))
    {
        // write eeprom
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT | LIBUSB_RECIPIENT_DEVICE,
                                          0xB4,
                                          (writeaddress >> 16) & 0xffff,
                                          (writeaddress) & 0xffff,
                                          buffer,
                                          size,
                                          10000);
        if(ret == size)
        {
            return PUREUSB_SUCCESS;
        }
    }
    return PUREUSB_FAILURE;
}
int usbFx2Reset(UsbContext* ctx)
{
    if(usbFxxIsConnected(ctx))
    {
        int ret = libusb_control_transfer((libusb_device_handle*)ctx->device,
                                          0x40,
                                          0xEE,
                                          0,
                                          0,
                                          0,
                                          0,
                                          10000);
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

////////////////////////////////////////////////////////////////////////////////
//
// Fxx
//
////////////////////////////////////////////////////////////////////////////////

int usbFxxIsConnected(UsbContext* ctx)
{
    return ctx->device > 0 ? 1 : 0;
}

void usbFxxInit(UsbContext* ctx)
{
    libusb_init(0);
}

int usbFxxTransferDataIn(UsbContext* ctx, int endPoint, char* dest, int size, int swapBytes, int timeout, int* transfered)
{
    int ret = 0;
    if(usbFxxIsConnected(ctx))
    {
        *transfered = 0;
        #if defined(MAC)
        int ret = 0;
        int chunkBytes     = 64 * 1024;
        int iterations     = size / chunkBytes;
        int wholePartBytes = chunkBytes * iterations;
        int leftoverBytes  = size % chunkBytes;
        int t = 0;
        for(int i = 0; i < iterations; i++)
        {
            ret += libusb_bulk_transfer((libusb_device_handle*)ctx->device, endPoint | LIBUSB_ENDPOINT_IN, dest, chunkBytes, &t, timeout);
            dest += t;
            *transfered += t;
        }
        if(leftoverBytes > 0 || ((*transfered) < size))
        {
            ret += libusb_bulk_transfer((libusb_device_handle*)ctx->device, endPoint | LIBUSB_ENDPOINT_IN, dest, leftoverBytes + (wholePartBytes - (*transfered)), &t, timeout);
            *transfered += t;
        }
        if(ret > 0)
        {
            return PUREUSB_FAILURE;
        }
        #else
        ret += libusb_bulk_transfer((libusb_device_handle*)ctx->device, endPoint | LIBUSB_ENDPOINT_IN, dest, size, transfered, timeout);
        #endif
        if(ret > 0)
        {
            return PUREUSB_FAILURE;
        }
        if(swapBytes)
        {
            int                count = size / 4;
            unsigned int* swapBuffer = (unsigned int*)dest;
            for(int i = 0; i < count; i++)
            {
                swapBuffer[i] = cSwap32(&swapBuffer[i]);
            }
        }
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

int usbFxxTransferDataOut(UsbContext* ctx, int endPoint, char* src, int size, int swapBytes, int timeOut, int* transfered)
{
    if(usbFxxIsConnected(ctx))
    {
        *transfered = 0;
        if(swapBytes)
        {
            int count = size / 2;
            unsigned short* swapBuffer = (unsigned short*)src;
            for(int i = 0; i < count; i++)
            {
                swapBuffer[i] = cSwap16(&swapBuffer[i]);
            }
        }
        int ret = libusb_bulk_transfer((libusb_device_handle*)ctx->device, endPoint | LIBUSB_ENDPOINT_OUT, src, size, transfered, timeOut);
        if(ret > 0)
        {
            return PUREUSB_FAILURE;
        }
        return PUREUSB_SUCCESS;
    }
    return PUREUSB_FAILURE;
}

David Košenina's avatar
David Košenina committed
614
void usbFxxGuidVidPid(UsbContext* ctx, struct UsbGuid guid, int vendorId, int productId,char serialId)
David Košenina's avatar
rc4  
David Košenina committed
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
{
    ctx->guid      = guid;
    ctx->vendorId  = vendorId;
    ctx->productId = productId;
    ctx->serialId  = serialId;
}

int usbFxxFindList(UsbContext* ctx, usbDevice** foundList, int maxCount)
{
    if(ctx->device)
    {
        return PUREUSB_FAILURE;
    }
    if(ctx->deviceList)
    {
        return PUREUSB_FAILURE;
    }
    ctx->deviceListCnt = (int)libusb_get_device_list(0, (libusb_device***)&ctx->deviceList);
    if(ctx->deviceListCnt < 0)
    {
        return PUREUSB_FAILURE;
    }
    int index = 0;
    for(unsigned int i = 0; i < ctx->deviceListCnt; i++)
    {
        libusb_device* device = (libusb_device*)ctx->deviceList[i];
        if(device)
        {
            struct libusb_device_descriptor desc;
            libusb_get_device_descriptor(device, &desc);
            if(desc.idVendor == ctx->vendorId && desc.idProduct == ctx->productId)
            {
647
                ctx->serialId = desc.iSerialNumber;
David Košenina's avatar
rc4  
David Košenina committed
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
                foundList[index] = (usbDevice*)device;
                index++;
                if(index < 0)
                {
                    index = 0;
                }
                if(index > maxCount - 1)
                {
                    index = maxCount - 1;
                }
            }
        }
    }
    if(index == 0)
    {
        return PUREUSB_FAILURE;
    }
    return PUREUSB_SUCCESS;
}

int usbFxxFreeList(UsbContext* ctx)
{
    libusb_free_device_list((libusb_device**)ctx->deviceList, ctx->deviceListCnt);
    ctx->deviceList    = 0;
    ctx->deviceListCnt = 0;
    return PUREUSB_SUCCESS;
}

int usbFxxOpenSerial(UsbContext* ctx, char* path, char checkSerial, usbDevice** foundList, int maxCount)
{
    for(int i = 0; i < maxCount; i++)
    {
        libusb_device* device = (libusb_device*)foundList[i];
        if(device)
        {
            if(checkSerial > 0)
            {
                usbFxxOpen(ctx, (usbDevice*)device);
                usbFx2UploadFirmwareToFx2(ctx, path);
                usbFxxClose(ctx);
                // 3s, delay
                cSleep(3000);
                usbFxxOpen(ctx, (usbDevice*)device);
                struct UsbEEPROM data;
                usbFx2ReadEEPROM(ctx, data.bytes, EEPROM_BYTE_COUNT, 0);
                if(data.bytes[5] == ctx->serialId)
                {
                    return PUREUSB_SUCCESS;
                }
                else
                {
                    usbFxxClose(ctx);
                }
            }
            else
            {
                usbFxxOpen(ctx, (usbDevice*)device);
                usbFx2UploadFirmwareToFx2(ctx, path);
                usbFxxClose(ctx);
                // 3s, delay
                cSleep(3000);
                usbFxxOpen(ctx, (usbDevice*)device);
                return PUREUSB_SUCCESS;
            }
        }
    }
    return PUREUSB_FAILURE;
}


int usbFxxOpenNormal(UsbContext* ctx, usbDevice** foundList, int maxCount)
{
    for(int i = 0; i < maxCount; i++)
    {
        libusb_device* device = (libusb_device*)foundList[i];
        if(device)
        {
725 726 727
            int ret = usbFxxOpen(ctx, (usbDevice*)device);
            ctx->serialBufferSize = libusb_get_string_descriptor_ascii(ctx->device, ctx->serialId, ctx->serialBuffer, 1024);
            return ret;
David Košenina's avatar
rc4  
David Košenina committed
728 729 730 731 732 733
        }
    }
    return PUREUSB_FAILURE;
}


734
int usbFxxOpen(UsbContext* ctx, usbDevice* device)
David Košenina's avatar
rc4  
David Košenina committed
735 736 737
{
    if(ctx->device)
    {
738
        return PUREUSB_FAILURE;
David Košenina's avatar
rc4  
David Košenina committed
739 740 741 742
    }
    int err = libusb_open((libusb_device*)device, (libusb_device_handle**)&ctx->device);
    if(err)
    {
743
        return PUREUSB_FAILURE;
David Košenina's avatar
rc4  
David Košenina committed
744 745 746 747
    }
    int speed = libusb_get_device_speed((libusb_device*)device);
    // interface
    usbFxxClaimInterface(ctx, 0);
748 749

    return PUREUSB_SUCCESS;
David Košenina's avatar
rc4  
David Košenina committed
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
}

void usbFxxClose(UsbContext* ctx)
{
    if(!ctx->device)
    {
        return;
    }
    // release
    usbFxxReleaseInterface(ctx, 0);
    // close
    libusb_close((libusb_device_handle*)ctx->device);
    // null
    ctx->device = 0;
}

void usbFxxClaimInterface(UsbContext* ctx, int interface)
{
    if(!ctx->device)
    {
        return;
    }
    libusb_claim_interface((libusb_device_handle*)ctx->device, interface);
    libusb_clear_halt((libusb_device_handle*)ctx->device, interface);
}

void usbFxxReleaseInterface(UsbContext* ctx, int interface)
{
    if(!ctx->device)
    {
        return;
    }
    libusb_release_interface((libusb_device_handle*)ctx->device, interface);
}

void usbFxxReset(UsbContext* ctx)
{
    if(!ctx->device)
    {
        return;
    }
    libusb_reset_device((libusb_device_handle*)ctx->device);
}

void usbFxxUpdate(UsbContext* ctx)
{
}

void usbFxxExit(UsbContext* ctx)
{
    libusb_exit(0);
}


////////////////////////////////////////////////////////////////////////////////
//
// test
//
////////////////////////////////////////////////////////////////////////////////

int testUsbTransferDataIn(UsbContext* ctx, int endPoint, char* dest, int size, int swapBytes, int timeOut, int* transfered)
{
    return PUREUSB_SUCCESS;
}

int testUsbTransferDataOut(UsbContext* ctx, int endPoint, char* src, int size, int swapBytes, int timeOut)
{
    return PUREUSB_SUCCESS;
}

////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////