mcp2210.c 9.7 KB

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