mcp2210.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. .product = wcs2str_dup(hid_item->product_string),
  152. .serial = wcs2str_dup(hid_item->serial_number),
  153. };
  154. LL_PREPEND(devinfo_list, info);
  155. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  156. __func__, info->product, info->serial);
  157. }
  158. hid_free_enumeration(hid_enum);
  159. return devinfo_list;
  160. }
  161. struct mcp2210_device {
  162. hid_device *hid;
  163. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 34
  164. uint8_t cfg_spi[0x11];
  165. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 40
  166. uint8_t cfg_gpio[0xf];
  167. };
  168. static
  169. bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
  170. {
  171. return likely(
  172. 0x41 == hid_write(hid, cmd, 0x41) &&
  173. 64 == hid_read(hid, buf, 64)
  174. );
  175. }
  176. static
  177. bool mcp2210_get_configs(struct mcp2210_device * const h)
  178. {
  179. hid_device * const hid = h->hid;
  180. uint8_t cmd[0x41] = {0,0x41}, buf[0x40];
  181. if (!mcp2210_io(hid, cmd, buf))
  182. {
  183. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
  184. return false;
  185. }
  186. memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
  187. cmd[1] = 0x20;
  188. if (!mcp2210_io(hid, cmd, buf))
  189. {
  190. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
  191. return false;
  192. }
  193. memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
  194. return true;
  195. }
  196. struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *info)
  197. {
  198. struct mcp2210_device *h;
  199. char * const path = info->path;
  200. hid_device * const hid = hid_open_path(path);
  201. if (unlikely(!hid))
  202. return NULL;
  203. h = malloc(sizeof(*h));
  204. h->hid = hid;
  205. if (!mcp2210_get_configs(h))
  206. goto fail;
  207. return h;
  208. fail:
  209. free(h);
  210. return NULL;
  211. }
  212. void mcp2210_close(struct mcp2210_device * const h)
  213. {
  214. hid_close(h->hid);
  215. free(h);
  216. }
  217. static
  218. bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
  219. {
  220. hid_device * const hid = h->hid;
  221. uint8_t cmd[0x41] = {0,0x40}, buf[0x40];
  222. memcpy(&cmd[5], h->cfg_spi, sizeof(h->cfg_spi));
  223. if (!mcp2210_io(hid, cmd, buf))
  224. {
  225. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
  226. return false;
  227. }
  228. if (buf[1] != 0)
  229. {
  230. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
  231. return false;
  232. }
  233. return true;
  234. }
  235. 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)
  236. {
  237. uint8_t * const cfg = h->cfg_spi;
  238. cfg[0] = (bitrate >> 0x00) & 0xff;
  239. cfg[1] = (bitrate >> 0x08) & 0xff;
  240. cfg[2] = (bitrate >> 0x10) & 0xff;
  241. cfg[3] = (bitrate >> 0x18) & 0xff;
  242. cfg[4] = ( idlechipsel >> 0) & 0xff;
  243. cfg[5] = ( idlechipsel >> 8) & 0xff;
  244. cfg[6] = (activechipsel >> 0) & 0xff;
  245. cfg[7] = (activechipsel >> 8) & 0xff;
  246. cfg[8] = (chipseltodatadelay >> 0) & 0xff;
  247. cfg[9] = (chipseltodatadelay >> 8) & 0xff;
  248. cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
  249. cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
  250. cfg[0xc] = (midbytedelay >> 0) & 0xff;
  251. cfg[0xd] = (midbytedelay >> 8) & 0xff;
  252. return mcp2210_set_cfg_spi(h);
  253. }
  254. bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
  255. {
  256. uint8_t * const cfg = h->cfg_spi;
  257. cfg[0x10] = spimode;
  258. return mcp2210_set_cfg_spi(h);
  259. }
  260. bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
  261. {
  262. hid_device * const hid = h->hid;
  263. uint8_t * const cfg = h->cfg_spi;
  264. uint8_t cmd[0x41] = {0,0x42}, buf[0x40];
  265. uint8_t *p = rx;
  266. if (unlikely(sz > 60))
  267. {
  268. applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
  269. return false;
  270. }
  271. cfg[0xe] = sz;
  272. cfg[0xf] = 0;
  273. if (!mcp2210_set_cfg_spi(h))
  274. return false;
  275. cmd[2] = sz;
  276. memcpy(&cmd[5], tx, sz);
  277. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  278. {
  279. applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
  280. return false;
  281. }
  282. while (true)
  283. {
  284. switch (buf[1])
  285. {
  286. case 0: // accepted
  287. cmd[2] = 0;
  288. break;
  289. case 0xf8: // transfer in progress
  290. applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
  291. cgsleep_ms(20);
  292. goto retry;
  293. default:
  294. applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
  295. return false;
  296. }
  297. if (buf[2] >= sz)
  298. {
  299. if (buf[2] > sz)
  300. applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
  301. memcpy(p, &buf[4], sz);
  302. return true;
  303. }
  304. memcpy(p, &buf[4], buf[2]);
  305. p += buf[2];
  306. sz -= buf[2];
  307. retry:
  308. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  309. {
  310. applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
  311. return false;
  312. }
  313. }
  314. }
  315. static
  316. bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
  317. {
  318. hid_device * const hid = h->hid;
  319. uint8_t cmd[0x41] = {0,0x21}, buf[0x40];
  320. // NOTE: NVRAM chip params access control is not set here
  321. memcpy(&cmd[5], h->cfg_gpio, 0xe);
  322. if (!mcp2210_io(hid, cmd, buf))
  323. {
  324. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
  325. return false;
  326. }
  327. if (buf[1] != 0)
  328. {
  329. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
  330. return false;
  331. }
  332. return true;
  333. }
  334. bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
  335. {
  336. const int bit = 1 << (pin % 8);
  337. const int byte = (pin / 8);
  338. // Set pin to GPIO mode
  339. h->cfg_gpio[pin] = 0;
  340. // Set GPIO to output mode
  341. h->cfg_gpio[byte + 0xb] &= ~bit;
  342. // Set value for GPIO output
  343. if (d == MGV_HIGH)
  344. h->cfg_gpio[byte + 9] |= bit;
  345. else
  346. h->cfg_gpio[byte + 9] &= ~bit;
  347. return mcp2210_set_cfg_gpio(h);
  348. }
  349. enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
  350. {
  351. hid_device * const hid = h->hid;
  352. uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
  353. const int bit = 1 << (pin % 8);
  354. const int byte = (pin / 8);
  355. // Set pin to GPIO mode
  356. h->cfg_gpio[pin] = 0;
  357. // Set GPIO to input mode
  358. h->cfg_gpio[byte + 0xb] |= bit;
  359. if (!mcp2210_set_cfg_gpio(h))
  360. return MGV_ERROR;
  361. if (!mcp2210_io(hid, cmd, buf))
  362. {
  363. applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
  364. return MGV_ERROR;
  365. }
  366. if (buf[byte + 4] & bit)
  367. return MGV_HIGH;
  368. else
  369. return MGV_LOW;
  370. }
  371. struct lowlevel_driver lowl_mcp2210 = {
  372. .devinfo_scan = mcp2210_devinfo_scan,
  373. };