mcp2210.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifndef WIN32
  11. #include <dlfcn.h>
  12. typedef void *dlh_t;
  13. #else
  14. #include <winsock2.h>
  15. #include <windows.h>
  16. #define dlopen(lib, flags) LoadLibrary(lib)
  17. #define dlsym(h, sym) ((void*)GetProcAddress(h, sym))
  18. #define dlerror() "unknown"
  19. #define dlclose(h) FreeLibrary(h)
  20. typedef HMODULE dlh_t;
  21. #endif
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <hidapi.h>
  27. #include <utlist.h>
  28. #include "logging.h"
  29. #include "lowlevel.h"
  30. #include "miner.h"
  31. #include "mcp2210.h"
  32. #define MCP2210_IDVENDOR 0x04d8
  33. #define MCP2210_IDPRODUCT 0x00de
  34. #ifdef WIN32
  35. #define HID_API_EXPORT __declspec(dllexport)
  36. #else
  37. #define HID_API_EXPORT /* */
  38. #endif
  39. struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
  40. void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
  41. hid_device * HID_API_EXPORT (*dlsym_hid_open_path)(const char *);
  42. int HID_API_EXPORT (*dlsym_hid_read)(hid_device *, unsigned char *, size_t);
  43. int HID_API_EXPORT (*dlsym_hid_write)(hid_device *, const unsigned char *, size_t);
  44. #define LOAD_SYM(sym) do { \
  45. if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
  46. applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
  47. goto fail; \
  48. } \
  49. } while(0)
  50. static
  51. bool hidapi_try_lib(const char * const dlname)
  52. {
  53. struct hid_device_info *hid_enum;
  54. dlh_t dlh;
  55. dlh = dlopen(dlname, RTLD_NOW);
  56. if (!dlh)
  57. {
  58. applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
  59. return false;
  60. }
  61. LOAD_SYM(hid_enumerate);
  62. LOAD_SYM(hid_free_enumeration);
  63. hid_enum = dlsym_hid_enumerate(0, 0);
  64. if (!hid_enum)
  65. {
  66. applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
  67. goto fail;
  68. }
  69. dlsym_hid_free_enumeration(hid_enum);
  70. LOAD_SYM(hid_open_path);
  71. LOAD_SYM(hid_read);
  72. LOAD_SYM(hid_write);
  73. applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
  74. return true;
  75. fail:
  76. dlclose(dlh);
  77. return false;
  78. }
  79. #define hid_enumerate dlsym_hid_enumerate
  80. #define hid_free_enumeration dlsym_hid_free_enumeration
  81. #define hid_open_path dlsym_hid_open_path
  82. #define hid_read dlsym_hid_read
  83. #define hid_write dlsym_hid_write
  84. static
  85. bool hidapi_load_library()
  86. {
  87. if (dlsym_hid_write)
  88. return true;
  89. const char **p;
  90. char dlname[23] = "libhidapi";
  91. const char *dltry[] = {
  92. "",
  93. "-0",
  94. "-hidraw",
  95. "-libusb",
  96. NULL
  97. };
  98. for (p = &dltry[0]; *p; ++p)
  99. {
  100. sprintf(&dlname[9], "%s.%s", *p,
  101. #ifdef WIN32
  102. "dll"
  103. #else
  104. "so"
  105. #endif
  106. );
  107. if (hidapi_try_lib(dlname))
  108. return true;
  109. }
  110. return false;
  111. }
  112. static
  113. void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
  114. {
  115. free(info->lowl_data);
  116. }
  117. static
  118. char *wcs2str_dup(wchar_t *ws)
  119. {
  120. char tmp, *rv;
  121. int clen;
  122. clen = snprintf(&tmp, 1, "%ls", ws);
  123. ++clen;
  124. rv = malloc(clen);
  125. snprintf(rv, clen, "%ls", ws);
  126. return rv;
  127. }
  128. static
  129. struct lowlevel_device_info *mcp2210_devinfo_scan()
  130. {
  131. if (!hidapi_load_library())
  132. {
  133. applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
  134. return NULL;
  135. }
  136. struct hid_device_info *hid_enum, *hid_item;
  137. struct lowlevel_device_info *info, *devinfo_list = NULL;
  138. hid_enum = hid_enumerate(MCP2210_IDVENDOR, MCP2210_IDPRODUCT);
  139. if (!hid_enum)
  140. {
  141. applog(LOG_DEBUG, "%s: No MCP2210 devices found", __func__);
  142. return NULL;
  143. }
  144. LL_FOREACH(hid_enum, hid_item)
  145. {
  146. info = malloc(sizeof(struct lowlevel_device_info));
  147. *info = (struct lowlevel_device_info){
  148. .lowl = &lowl_mcp2210,
  149. .lowl_data = strdup(hid_item->path),
  150. .product = wcs2str_dup(hid_item->product_string),
  151. .serial = wcs2str_dup(hid_item->serial_number),
  152. };
  153. LL_PREPEND(devinfo_list, info);
  154. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  155. __func__, info->product, info->serial);
  156. }
  157. hid_free_enumeration(hid_enum);
  158. return devinfo_list;
  159. }
  160. struct mcp2210_device {
  161. hid_device *hid;
  162. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 34
  163. uint8_t cfg_spi[0x11];
  164. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 40
  165. uint8_t cfg_gpio[0xf];
  166. };
  167. static
  168. bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
  169. {
  170. return likely(
  171. 64 == hid_write(hid, cmd, 64) &&
  172. 64 == hid_read(hid, buf, 64)
  173. );
  174. }
  175. static
  176. bool mcp2210_get_configs(struct mcp2210_device * const h)
  177. {
  178. hid_device * const hid = h->hid;
  179. uint8_t cmd[0x40] = {0x41}, buf[0x40];
  180. if (!mcp2210_io(hid, cmd, buf))
  181. {
  182. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
  183. return false;
  184. }
  185. memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
  186. cmd[0] = 0x20;
  187. if (!mcp2210_io(hid, cmd, buf))
  188. {
  189. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
  190. return false;
  191. }
  192. memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
  193. return true;
  194. }
  195. struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *info)
  196. {
  197. struct mcp2210_device *h;
  198. char * const path = info->lowl_data;
  199. hid_device * const hid = hid_open_path(path);
  200. if (unlikely(!hid))
  201. return NULL;
  202. h = malloc(sizeof(*h));
  203. h->hid = hid;
  204. if (!mcp2210_get_configs(h))
  205. goto fail;
  206. return h;
  207. fail:
  208. free(h);
  209. return NULL;
  210. }
  211. static
  212. bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
  213. {
  214. hid_device * const hid = h->hid;
  215. uint8_t cmd[0x40] = {0x40}, buf[0x40];
  216. memcpy(&cmd[4], h->cfg_spi, sizeof(h->cfg_spi));
  217. if (!mcp2210_io(hid, cmd, buf))
  218. {
  219. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
  220. return false;
  221. }
  222. if (buf[1] != 0)
  223. {
  224. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
  225. return false;
  226. }
  227. return true;
  228. }
  229. bool mcp2210_configure_spi(struct mcp2210_device * const h, const uint32_t bitrate, const uint16_t idlechipsel, const uint16_t activechipsel, const uint16_t chipseltodatadelay, const uint16_t lastbytetocsdelay, const uint16_t midbytedelay)
  230. {
  231. uint8_t * const cfg = h->cfg_spi;
  232. cfg[0] = (bitrate >> 0x00) & 0xff;
  233. cfg[1] = (bitrate >> 0x08) & 0xff;
  234. cfg[2] = (bitrate >> 0x10) & 0xff;
  235. cfg[3] = (bitrate >> 0x18) & 0xff;
  236. cfg[4] = ( idlechipsel >> 0) & 0xff;
  237. cfg[5] = ( idlechipsel >> 8) & 0xff;
  238. cfg[6] = (activechipsel >> 0) & 0xff;
  239. cfg[7] = (activechipsel >> 8) & 0xff;
  240. cfg[8] = (chipseltodatadelay >> 0) & 0xff;
  241. cfg[9] = (chipseltodatadelay >> 8) & 0xff;
  242. cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
  243. cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
  244. cfg[0xc] = (midbytedelay >> 0) & 0xff;
  245. cfg[0xd] = (midbytedelay >> 8) & 0xff;
  246. return mcp2210_set_cfg_spi(h);
  247. }
  248. bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
  249. {
  250. uint8_t * const cfg = h->cfg_spi;
  251. cfg[0x10] = spimode;
  252. return mcp2210_set_cfg_spi(h);
  253. }
  254. bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
  255. {
  256. hid_device * const hid = h->hid;
  257. uint8_t * const cfg = h->cfg_spi;
  258. uint8_t cmd[0x40] = {0x42}, buf[0x40];
  259. uint8_t *p = rx;
  260. if (unlikely(sz > 60))
  261. {
  262. applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
  263. return false;
  264. }
  265. cfg[0xe] = sz;
  266. cfg[0xf] = 0;
  267. if (!mcp2210_set_cfg_spi(h))
  268. return false;
  269. cmd[1] = sz;
  270. memcpy(&cmd[4], tx, sz);
  271. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  272. {
  273. applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
  274. return false;
  275. }
  276. while (true)
  277. {
  278. switch (buf[1])
  279. {
  280. case 0: // accepted
  281. cmd[1] = 0;
  282. break;
  283. case 0xf8: // transfer in progress
  284. applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
  285. cgsleep_ms(20);
  286. goto retry;
  287. default:
  288. applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
  289. return false;
  290. }
  291. if (buf[2] >= sz)
  292. {
  293. if (buf[2] > sz)
  294. applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
  295. memcpy(p, &buf[4], sz);
  296. return true;
  297. }
  298. memcpy(p, &buf[4], buf[2]);
  299. p += buf[2];
  300. sz -= buf[2];
  301. retry:
  302. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  303. {
  304. applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
  305. return false;
  306. }
  307. }
  308. }
  309. static
  310. bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
  311. {
  312. hid_device * const hid = h->hid;
  313. uint8_t cmd[0x40] = {0x21}, buf[0x40];
  314. // NOTE: NVRAM chip params access control is not set here
  315. memcpy(&cmd[4], h->cfg_gpio, 0xe);
  316. if (!mcp2210_io(hid, cmd, buf))
  317. {
  318. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
  319. return false;
  320. }
  321. if (buf[1] != 0)
  322. {
  323. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
  324. return false;
  325. }
  326. return true;
  327. }
  328. bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
  329. {
  330. const int bit = 1 << (pin % 8);
  331. const int byte = (pin / 8);
  332. // Set pin to GPIO mode
  333. h->cfg_gpio[pin] = 0;
  334. // Set GPIO to output mode
  335. h->cfg_gpio[byte + 0xb] &= ~bit;
  336. // Set value for GPIO output
  337. if (d == MGV_HIGH)
  338. h->cfg_gpio[byte + 9] |= bit;
  339. else
  340. h->cfg_gpio[byte + 9] &= ~bit;
  341. return mcp2210_set_cfg_gpio(h);
  342. }
  343. enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
  344. {
  345. hid_device * const hid = h->hid;
  346. uint8_t cmd[0x40] = {0x31}, buf[0x40];
  347. const int bit = 1 << (pin % 8);
  348. const int byte = (pin / 8);
  349. // Set pin to GPIO mode
  350. h->cfg_gpio[pin] = 0;
  351. // Set GPIO to input mode
  352. h->cfg_gpio[byte + 0xb] |= bit;
  353. if (!mcp2210_set_cfg_gpio(h))
  354. return MGV_ERROR;
  355. if (!mcp2210_io(hid, cmd, buf))
  356. {
  357. applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
  358. return MGV_ERROR;
  359. }
  360. if (buf[byte + 4] & bit)
  361. return MGV_HIGH;
  362. else
  363. return MGV_LOW;
  364. }
  365. struct lowlevel_driver lowl_mcp2210 = {
  366. .devinfo_scan = mcp2210_devinfo_scan,
  367. .devinfo_free = mcp2210_devinfo_free,
  368. };