SCSI2SD_HID.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "SCSI2SD_HID.hh"
  18. #include <cassert>
  19. #include <stdexcept>
  20. #include <sstream>
  21. #include <iostream>
  22. #include <string.h> // memcpy
  23. using namespace SCSI2SD;
  24. HID::HID(hid_device_info* hidInfo) :
  25. myHidInfo(hidInfo),
  26. myConfigHandle(NULL),
  27. myDebugHandle(NULL),
  28. myFirmwareVersion(0),
  29. mySDCapacity(0)
  30. {
  31. // hidInfo->interface_number not supported on mac, and interfaces
  32. // are enumerated in a random order. :-(
  33. // We rely on the watchdog value of the debug interface changing on each
  34. // read to differentiate the interfaces.
  35. while (hidInfo && !(myConfigHandle && myDebugHandle))
  36. {
  37. if ((hidInfo->interface_number == CONFIG_INTERFACE) ||
  38. (hidInfo->usage_page == 0xFF00))
  39. {
  40. myConfigHandle = hid_open_path(hidInfo->path);
  41. hidInfo = hidInfo->next;
  42. }
  43. else if ((hidInfo->interface_number == DEBUG_INTERFACE) ||
  44. (hidInfo->usage_page == 0xFF01))
  45. {
  46. myDebugHandle = hid_open_path(hidInfo->path);
  47. readDebugData();
  48. hidInfo = hidInfo->next;
  49. }
  50. else if (hidInfo->interface_number == -1)
  51. {
  52. // hidInfo->interface_number not supported on mac, and
  53. // interfaces are enumerated in a random order. :-(
  54. // We rely on the watchdog value of the debug interface
  55. // changing on each read to differentiate the interfaces.
  56. // Not necessary since firmware 3.5.2 as the usage_page can
  57. // be used instead.
  58. hid_device* dev = hid_open_path(hidInfo->path);
  59. if (!dev)
  60. {
  61. hidInfo = hidInfo->next;
  62. continue;
  63. }
  64. uint8_t buf[HID_PACKET_SIZE];
  65. int watchVal = -1;
  66. int configIntFound = 1;
  67. for (int i = 0; i < 4; ++i)
  68. {
  69. buf[0] = 0; // report id
  70. hid_read(dev, buf, HID_PACKET_SIZE);
  71. if (watchVal == -1) watchVal = buf[25];
  72. configIntFound = configIntFound && (buf[25] == watchVal);
  73. }
  74. if (configIntFound)
  75. {
  76. myConfigHandle = dev;
  77. }
  78. else
  79. {
  80. myDebugHandle = dev;
  81. readDebugData();
  82. }
  83. }
  84. }
  85. }
  86. HID::~HID()
  87. {
  88. if (myConfigHandle)
  89. {
  90. hid_close(myConfigHandle);
  91. }
  92. if (myDebugHandle)
  93. {
  94. hid_close(myDebugHandle);
  95. }
  96. hid_free_enumeration(myHidInfo);
  97. }
  98. HID*
  99. HID::Open()
  100. {
  101. hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);
  102. if (dev)
  103. {
  104. return new HID(dev);
  105. }
  106. else
  107. {
  108. return NULL;
  109. }
  110. }
  111. void
  112. HID::enterBootloader()
  113. {
  114. // Reboot commands added in firmware 3.5
  115. if (!myDebugHandle)
  116. {
  117. throw std::runtime_error(
  118. "Cannot enter SCSI2SD bootloader: debug interface not found");
  119. }
  120. else if (myFirmwareVersion == 0)
  121. {
  122. throw std::runtime_error(
  123. "Cannot enter SCSI2SD bootloader: old firmware version running.\n"
  124. "The SCSI2SD board cannot reset itself. Please disconnect and \n"
  125. "reconnect the USB cable.\n");
  126. }
  127. else
  128. {
  129. uint8_t hidBuf[HID_PACKET_SIZE + 1] =
  130. {
  131. 0x00, // Report ID;
  132. 0x01 // Reboot command
  133. // 63 bytes unused.
  134. };
  135. int result = hid_write(myDebugHandle, hidBuf, sizeof(hidBuf));
  136. if (result < 0)
  137. {
  138. const wchar_t* err = hid_error(myDebugHandle);
  139. std::stringstream ss;
  140. ss << "USB HID write failure: " << err;
  141. throw std::runtime_error(ss.str());
  142. }
  143. }
  144. }
  145. void
  146. HID::readConfig(uint8_t* buffer, size_t len)
  147. {
  148. assert(len >= 0);
  149. buffer[0] = 0; // report id
  150. int result = hid_read(myConfigHandle, buffer, len);
  151. if (result < 0)
  152. {
  153. const wchar_t* err = hid_error(myConfigHandle);
  154. std::stringstream ss;
  155. ss << "USB HID read failure: " << err;
  156. throw std::runtime_error(ss.str());
  157. }
  158. }
  159. void
  160. HID::saveConfig(uint8_t* buffer, size_t len)
  161. {
  162. assert(len >= 0 && len <= HID_PACKET_SIZE);
  163. uint8_t hidBuf[HID_PACKET_SIZE + 1] =
  164. {
  165. 0x00, // Report ID;
  166. };
  167. memcpy(&hidBuf[1], buffer, len);
  168. int result = hid_write(myConfigHandle, hidBuf, len + 1);
  169. if (result < 0)
  170. {
  171. const wchar_t* err = hid_error(myConfigHandle);
  172. std::stringstream ss;
  173. ss << "USB HID write failure: " << err;
  174. throw std::runtime_error(ss.str());
  175. }
  176. }
  177. void
  178. HID::readDebugData()
  179. {
  180. uint8_t buf[HID_PACKET_SIZE];
  181. buf[0] = 0; // report id
  182. int result = hid_read(myDebugHandle, buf, HID_PACKET_SIZE);
  183. if (result < 0)
  184. {
  185. const wchar_t* err = hid_error(myDebugHandle);
  186. std::stringstream ss;
  187. ss << "USB HID read failure: " << err;
  188. throw std::runtime_error(ss.str());
  189. }
  190. myFirmwareVersion = (((uint16_t)buf[62]) << 8) | buf[63];
  191. mySDCapacity =
  192. (((uint32_t)buf[58]) << 24) |
  193. (((uint32_t)buf[59]) << 16) |
  194. (((uint32_t)buf[60]) << 8) |
  195. ((uint32_t)buf[61]);
  196. }
  197. std::string
  198. HID::getFirmwareVersionStr() const
  199. {
  200. if (myFirmwareVersion == 0)
  201. {
  202. return "Unknown (3.0 - 3.4)";
  203. }
  204. else
  205. {
  206. std::stringstream ver;
  207. ver << std::hex <<
  208. (myFirmwareVersion >> 8) <<
  209. '.' << ((myFirmwareVersion & 0xF0) >> 4);
  210. int rev = myFirmwareVersion & 0xF;
  211. if (rev)
  212. {
  213. ver << "." << rev;
  214. }
  215. return ver.str();
  216. }
  217. }