mcp2210.c 10 KB

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