mcp2210.c 10 KB

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