| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /*
- * Copyright 2012-2013 Luke Dashjr
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version. See COPYING for more details.
- */
- #include "config.h"
- #include <dlfcn.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <hidapi/hidapi.h> /* FIXME */
- #include <utlist.h>
- #include "logging.h"
- #include "lowlevel.h"
- #include "miner.h"
- #include "mcp2210.h"
- #define MCP2210_IDVENDOR 0x04d8
- #define MCP2210_IDPRODUCT 0x00de
- #ifdef WIN32
- #define HID_API_EXPORT __declspec(dllexport)
- #else
- #define HID_API_EXPORT /* */
- #endif
- struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
- void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
- hid_device * HID_API_EXPORT (*dlsym_hid_open_path)(const char *);
- int HID_API_EXPORT (*dlsym_hid_read)(hid_device *, unsigned char *, size_t);
- int HID_API_EXPORT (*dlsym_hid_write)(hid_device *, const unsigned char *, size_t);
- #define LOAD_SYM(sym) do { \
- if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
- applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
- goto fail; \
- } \
- } while(0)
- static
- bool hidapi_try_lib(const char * const dlname)
- {
- struct hid_device_info *hid_enum;
- void *dlh;
-
- dlh = dlopen(dlname, RTLD_NOW);
- if (!dlh)
- {
- applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
- return false;
- }
-
- LOAD_SYM(hid_enumerate);
- LOAD_SYM(hid_free_enumeration);
-
- hid_enum = dlsym_hid_enumerate(0, 0);
- if (!hid_enum)
- {
- applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
- goto fail;
- }
- dlsym_hid_free_enumeration(hid_enum);
-
- LOAD_SYM(hid_open_path);
- LOAD_SYM(hid_read);
- LOAD_SYM(hid_write);
-
- applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
-
- return true;
- fail:
- dlclose(dlh);
- return false;
- }
- #define hid_enumerate dlsym_hid_enumerate
- #define hid_free_enumeration dlsym_hid_free_enumeration
- #define hid_open_path dlsym_hid_open_path
- #define hid_read dlsym_hid_read
- #define hid_write dlsym_hid_write
- static
- bool hidapi_load_library()
- {
- if (dlsym_hid_write)
- return true;
-
- const char **p;
- char dlname[23] = "libhidapi";
- const char *dltry[] = {
- "",
- "-0",
- "-hidraw",
- "-libusb",
- NULL
- };
- for (p = &dltry[0]; *p; ++p)
- {
- sprintf(&dlname[9], "%s.%s", *p,
- #ifdef WIN32
- "dll"
- #else
- "so"
- #endif
- );
- if (hidapi_try_lib(dlname))
- return true;
- }
-
- return false;
- }
- static
- void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
- {
- free(info->lowl_data);
- }
- static
- char *wcs2str_dup(wchar_t *ws)
- {
- char tmp, *rv;
- int clen;
-
- clen = snprintf(&tmp, 1, "%ls", ws);
- ++clen;
- rv = malloc(clen);
- snprintf(rv, clen, "%ls", ws);
- return rv;
- }
- static
- struct lowlevel_device_info *mcp2210_devinfo_scan()
- {
- if (!hidapi_load_library())
- {
- applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
- return NULL;
- }
-
- struct hid_device_info *hid_enum, *hid_item;
- struct lowlevel_device_info *info, *devinfo_list = NULL;
-
- hid_enum = hid_enumerate(MCP2210_IDVENDOR, MCP2210_IDPRODUCT);
- if (!hid_enum)
- {
- applog(LOG_DEBUG, "%s: No MCP2210 devices found", __func__);
- return NULL;
- }
-
- LL_FOREACH(hid_enum, hid_item)
- {
- info = malloc(sizeof(struct lowlevel_device_info));
- *info = (struct lowlevel_device_info){
- .lowl = &lowl_mcp2210,
- .lowl_data = strdup(hid_item->path),
- .product = wcs2str_dup(hid_item->product_string),
- .serial = wcs2str_dup(hid_item->serial_number),
- };
- LL_PREPEND(devinfo_list, info);
- applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
- __func__, info->product, info->serial);
- }
-
- hid_free_enumeration(hid_enum);
-
- return devinfo_list;
- }
- struct mcp2210_device {
- hid_device *hid;
-
- // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 34
- uint8_t cfg_spi[0x11];
- // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 40
- uint8_t cfg_gpio[0xf];
- };
- static
- bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
- {
- return likely(
- 64 == hid_write(hid, cmd, 64) &&
- 64 == hid_read(hid, buf, 64)
- );
- }
- static
- bool mcp2210_get_configs(struct mcp2210_device * const h)
- {
- hid_device * const hid = h->hid;
- uint8_t cmd[0x40] = {0x41}, buf[0x40];
-
- if (!mcp2210_io(hid, cmd, buf))
- {
- applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
- return false;
- }
- memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
-
- cmd[0] = 0x20;
- if (!mcp2210_io(hid, cmd, buf))
- {
- applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
- return false;
- }
- memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
-
- return true;
- }
- struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *info)
- {
- struct mcp2210_device *h;
- char * const path = info->lowl_data;
- hid_device * const hid = hid_open_path(path);
-
- if (unlikely(!hid))
- return NULL;
-
- h = malloc(sizeof(*h));
- h->hid = hid;
-
- if (!mcp2210_get_configs(h))
- goto fail;
-
- return h;
- fail:
- free(h);
- return NULL;
- }
- static
- bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
- {
- hid_device * const hid = h->hid;
- uint8_t cmd[0x40] = {0x40}, buf[0x40];
- memcpy(&cmd[4], h->cfg_spi, sizeof(h->cfg_spi));
- if (!mcp2210_io(hid, cmd, buf))
- {
- applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
- return false;
- }
-
- if (buf[1] != 0)
- {
- applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
- return false;
- }
-
- return true;
- }
- 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)
- {
- uint8_t * const cfg = h->cfg_spi;
-
- cfg[0] = (bitrate >> 0x00) & 0xff;
- cfg[1] = (bitrate >> 0x08) & 0xff;
- cfg[2] = (bitrate >> 0x10) & 0xff;
- cfg[3] = (bitrate >> 0x18) & 0xff;
-
- cfg[4] = ( idlechipsel >> 0) & 0xff;
- cfg[5] = ( idlechipsel >> 8) & 0xff;
-
- cfg[6] = (activechipsel >> 0) & 0xff;
- cfg[7] = (activechipsel >> 8) & 0xff;
-
- cfg[8] = (chipseltodatadelay >> 0) & 0xff;
- cfg[9] = (chipseltodatadelay >> 8) & 0xff;
-
- cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
- cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
-
- cfg[0xc] = (midbytedelay >> 0) & 0xff;
- cfg[0xd] = (midbytedelay >> 8) & 0xff;
-
- return mcp2210_set_cfg_spi(h);
- }
- bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
- {
- uint8_t * const cfg = h->cfg_spi;
- cfg[0x10] = spimode;
- return mcp2210_set_cfg_spi(h);
- }
- bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
- {
- hid_device * const hid = h->hid;
- uint8_t * const cfg = h->cfg_spi;
- uint8_t cmd[0x40] = {0x42}, buf[0x40];
- uint8_t *p = rx;
-
- if (unlikely(sz > 60))
- {
- applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
- return false;
- }
-
- cfg[0xe] = sz;
- cfg[0xf] = 0;
- if (!mcp2210_set_cfg_spi(h))
- return false;
-
- cmd[1] = sz;
- memcpy(&cmd[4], tx, sz);
- if (unlikely(!mcp2210_io(hid, cmd, buf)))
- {
- applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
- return false;
- }
-
- while (true)
- {
- switch (buf[1])
- {
- case 0: // accepted
- cmd[1] = 0;
- break;
- case 0xf8: // transfer in progress
- applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
- cgsleep_ms(20);
- goto retry;
- default:
- applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
- return false;
- }
- if (buf[2] >= sz)
- {
- if (buf[2] > sz)
- applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
- memcpy(p, &buf[4], sz);
- return true;
- }
- memcpy(p, &buf[4], buf[2]);
- p += buf[2];
- sz -= buf[2];
- retry:
- if (unlikely(!mcp2210_io(hid, cmd, buf)))
- {
- applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
- return false;
- }
- }
- }
- static
- bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
- {
- hid_device * const hid = h->hid;
- uint8_t cmd[0x40] = {0x21}, buf[0x40];
-
- // NOTE: NVRAM chip params access control is not set here
- memcpy(&cmd[4], h->cfg_gpio, 0xe);
- if (!mcp2210_io(hid, cmd, buf))
- {
- applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
- return false;
- }
-
- if (buf[1] != 0)
- {
- applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
- return false;
- }
-
- return true;
- }
- static
- bool mcp2210_set_gpio_(struct mcp2210_device * const h, const int pin, const int byteoffset, const bool value)
- {
- const int bit = 1 << (pin % 8);
- const int byte = (pin / 8) + byteoffset;
-
- if (value)
- h->cfg_gpio[byte] |= bit;
- else
- h->cfg_gpio[byte] &= ~bit;
-
- return mcp2210_set_cfg_gpio(h);
- }
- bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
- {
- h->cfg_gpio[pin] = 0;
- if (!mcp2210_set_gpio_(h, pin, 0xb, false))
- return false;
- return mcp2210_set_gpio_(h, pin, 0x9, d == MGV_HIGH);
- }
- enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
- {
- hid_device * const hid = h->hid;
- uint8_t cmd[0x40] = {0x31}, buf[0x40];
- const int bit = 1 << (pin % 8);
- const int byte = (pin / 8) + 4;
-
- h->cfg_gpio[pin] = 0;
- if (!mcp2210_set_gpio_(h, pin, 0xb, true))
- return MGV_ERROR;
-
- if (!mcp2210_io(hid, cmd, buf))
- {
- applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
- return MGV_ERROR;
- }
-
- if (buf[byte] & bit)
- return MGV_HIGH;
- else
- return MGV_LOW;
- }
- struct lowlevel_driver lowl_mcp2210 = {
- .devinfo_scan = mcp2210_devinfo_scan,
- .devinfo_free = mcp2210_devinfo_free,
- };
|