mcp2210.c 10 KB

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