SCSI2SD_HID.hh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef SCSI2SD_HID_H
  18. #define SCSI2SD_HID_H
  19. #include "hidapi.h"
  20. #if __cplusplus >= 201103L
  21. #include <cstdint>
  22. #else
  23. #include <stdint.h>
  24. #endif
  25. #include <string>
  26. #include <vector>
  27. namespace SCSI2SD
  28. {
  29. class HID
  30. {
  31. public:
  32. static const uint16_t VENDOR_ID = 0x04B4; // Cypress
  33. static const uint16_t PRODUCT_ID = 0x1337; // SCSI2SD application firmware
  34. static const int CONFIG_INTERFACE = 0;
  35. static const int DEBUG_INTERFACE = 1;
  36. static const size_t HID_PACKET_SIZE = 64;
  37. // HID intervals for 4.0.3 firmware: <= 128ms
  38. // > 4.0.3 = 32ms.
  39. static const size_t HID_TIMEOUT_MS = 256; // 2x HID Interval.
  40. static HID* Open();
  41. ~HID();
  42. uint16_t getFirmwareVersion() const { return myFirmwareVersion; }
  43. std::string getFirmwareVersionStr() const;
  44. uint32_t getSDCapacity() const { return mySDCapacity; }
  45. std::vector<uint8_t> getSD_CSD();
  46. std::vector<uint8_t> getSD_CID();
  47. void enterBootloader();
  48. void readFlashRow(int array, int row, std::vector<uint8_t>& out);
  49. void writeFlashRow(int array, int row, const std::vector<uint8_t>& in);
  50. bool ping();
  51. bool readSCSIDebugInfo(std::vector<uint8_t>& buf);
  52. private:
  53. HID(hid_device_info* hidInfo);
  54. void destroy();
  55. void readDebugData();
  56. void readHID(uint8_t* buffer, size_t len);
  57. void sendHIDPacket(
  58. const std::vector<uint8_t>& cmd,
  59. std::vector<uint8_t>& out,
  60. size_t responseLength
  61. );
  62. hid_device_info* myHidInfo;
  63. hid_device* myConfigHandle;
  64. hid_device* myDebugHandle;
  65. // Read-only data from the debug interface.
  66. uint16_t myFirmwareVersion;
  67. uint32_t mySDCapacity;
  68. };
  69. } // namespace
  70. #endif