|
|
@@ -47,6 +47,7 @@ typedef HMODULE dlh_t;
|
|
|
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 *);
|
|
|
+void HID_API_EXPORT (*dlsym_hid_close)(hid_device *);
|
|
|
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);
|
|
|
|
|
|
@@ -82,6 +83,7 @@ bool hidapi_try_lib(const char * const dlname)
|
|
|
dlsym_hid_free_enumeration(hid_enum);
|
|
|
|
|
|
LOAD_SYM(hid_open_path);
|
|
|
+ LOAD_SYM(hid_close);
|
|
|
LOAD_SYM(hid_read);
|
|
|
LOAD_SYM(hid_write);
|
|
|
|
|
|
@@ -97,6 +99,7 @@ fail:
|
|
|
#define hid_enumerate dlsym_hid_enumerate
|
|
|
#define hid_free_enumeration dlsym_hid_free_enumeration
|
|
|
#define hid_open_path dlsym_hid_open_path
|
|
|
+#define hid_close dlsym_hid_close
|
|
|
#define hid_read dlsym_hid_read
|
|
|
#define hid_write dlsym_hid_write
|
|
|
|
|
|
@@ -140,13 +143,15 @@ void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
|
|
|
static
|
|
|
char *wcs2str_dup(wchar_t *ws)
|
|
|
{
|
|
|
- char tmp, *rv;
|
|
|
- int clen;
|
|
|
+ char *rv;
|
|
|
+ int clen, i;
|
|
|
|
|
|
- clen = snprintf(&tmp, 1, "%ls", ws);
|
|
|
+ clen = wcslen(ws);
|
|
|
++clen;
|
|
|
rv = malloc(clen);
|
|
|
- snprintf(rv, clen, "%ls", ws);
|
|
|
+ for (i = 0; i < clen; ++i)
|
|
|
+ rv[i] = ws[i];
|
|
|
+
|
|
|
return rv;
|
|
|
}
|
|
|
|
|
|
@@ -202,7 +207,7 @@ static
|
|
|
bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
|
|
|
{
|
|
|
return likely(
|
|
|
- 64 == hid_write(hid, cmd, 64) &&
|
|
|
+ 0x41 == hid_write(hid, cmd, 0x41) &&
|
|
|
64 == hid_read(hid, buf, 64)
|
|
|
);
|
|
|
}
|
|
|
@@ -211,7 +216,7 @@ static
|
|
|
bool mcp2210_get_configs(struct mcp2210_device * const h)
|
|
|
{
|
|
|
hid_device * const hid = h->hid;
|
|
|
- uint8_t cmd[0x40] = {0x41}, buf[0x40];
|
|
|
+ uint8_t cmd[0x41] = {0,0x41}, buf[0x40];
|
|
|
|
|
|
if (!mcp2210_io(hid, cmd, buf))
|
|
|
{
|
|
|
@@ -220,7 +225,7 @@ bool mcp2210_get_configs(struct mcp2210_device * const h)
|
|
|
}
|
|
|
memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
|
|
|
|
|
|
- cmd[0] = 0x20;
|
|
|
+ cmd[1] = 0x20;
|
|
|
if (!mcp2210_io(hid, cmd, buf))
|
|
|
{
|
|
|
applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
|
|
|
@@ -253,12 +258,18 @@ fail:
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
+void mcp2210_close(struct mcp2210_device * const h)
|
|
|
+{
|
|
|
+ hid_close(h->hid);
|
|
|
+ free(h);
|
|
|
+}
|
|
|
+
|
|
|
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));
|
|
|
+ uint8_t cmd[0x41] = {0,0x40}, buf[0x40];
|
|
|
+ memcpy(&cmd[5], 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");
|
|
|
@@ -312,7 +323,7 @@ bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx
|
|
|
{
|
|
|
hid_device * const hid = h->hid;
|
|
|
uint8_t * const cfg = h->cfg_spi;
|
|
|
- uint8_t cmd[0x40] = {0x42}, buf[0x40];
|
|
|
+ uint8_t cmd[0x41] = {0,0x42}, buf[0x40];
|
|
|
uint8_t *p = rx;
|
|
|
|
|
|
if (unlikely(sz > 60))
|
|
|
@@ -326,8 +337,8 @@ bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx
|
|
|
if (!mcp2210_set_cfg_spi(h))
|
|
|
return false;
|
|
|
|
|
|
- cmd[1] = sz;
|
|
|
- memcpy(&cmd[4], tx, sz);
|
|
|
+ cmd[2] = sz;
|
|
|
+ memcpy(&cmd[5], tx, sz);
|
|
|
if (unlikely(!mcp2210_io(hid, cmd, buf)))
|
|
|
{
|
|
|
applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
|
|
|
@@ -339,7 +350,7 @@ bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx
|
|
|
switch (buf[1])
|
|
|
{
|
|
|
case 0: // accepted
|
|
|
- cmd[1] = 0;
|
|
|
+ cmd[2] = 0;
|
|
|
break;
|
|
|
case 0xf8: // transfer in progress
|
|
|
applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
|
|
|
@@ -372,10 +383,10 @@ static
|
|
|
bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
|
|
|
{
|
|
|
hid_device * const hid = h->hid;
|
|
|
- uint8_t cmd[0x40] = {0x21}, buf[0x40];
|
|
|
+ uint8_t cmd[0x41] = {0,0x21}, buf[0x40];
|
|
|
|
|
|
// NOTE: NVRAM chip params access control is not set here
|
|
|
- memcpy(&cmd[4], h->cfg_gpio, 0xe);
|
|
|
+ memcpy(&cmd[5], h->cfg_gpio, 0xe);
|
|
|
if (!mcp2210_io(hid, cmd, buf))
|
|
|
{
|
|
|
applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
|
|
|
@@ -414,7 +425,7 @@ bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, con
|
|
|
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];
|
|
|
+ uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
|
|
|
const int bit = 1 << (pin % 8);
|
|
|
const int byte = (pin / 8);
|
|
|
|