Browse Source

Merge branch 'nanofury' into bfgminer

Luke Dashjr 12 years ago
parent
commit
97be4d16d9
3 changed files with 35 additions and 21 deletions
  1. 7 5
      driver-nanofury.c
  2. 27 16
      mcp2210.c
  3. 1 0
      mcp2210.h

+ 7 - 5
driver-nanofury.c

@@ -183,11 +183,11 @@ bool nanofury_foundlowl(struct lowlevel_device_info * const info)
 	{
 		applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
 		       __func__, product, serial);
-		// TODO: mcp2210_close(mcp);
+		mcp2210_close(mcp);
 		return false;
 	}
 	nanofury_device_off(mcp);
-	// TODO: mcp2210_close(mcp);
+	mcp2210_close(mcp);
 	
 	// TODO: claim device
 	
@@ -199,7 +199,9 @@ bool nanofury_foundlowl(struct lowlevel_device_info * const info)
 		.threads = 1,
 		// TODO: .name
 		// TODO: .device_path
-		// TODO: .dev_manufacturer/.dev_product/.dev_serial
+		// TODO: .dev_manufacturer
+		.dev_product = strdup(product),
+		.dev_serial = strdup(serial),
 		.deven = DEV_ENABLED,
 		// TODO: .cutofftemp
 	};
@@ -241,7 +243,7 @@ bool nanofury_init(struct thr_info * const thr)
 	if (!nanofury_checkport(mcp))
 	{
 		applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
-		// TODO: mcp2210_close(mcp);
+		mcp2210_close(mcp);
 		return false;
 	}
 	
@@ -253,7 +255,7 @@ bool nanofury_init(struct thr_info * const thr)
 		applog(LOG_ERR, "%"PRIpreprv": Failed to allocate spi_port and bitfury_device structures", cgpu->proc_repr);
 		free(port);
 		free(bitfury);
-		// TODO: mcp2210_close(mcp);
+		mcp2210_close(mcp);
 		return false;
 	}
 	

+ 27 - 16
mcp2210.c

@@ -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);
 	

+ 1 - 0
mcp2210.h

@@ -17,6 +17,7 @@ enum mcp2210_gpio_value {
 struct mcp2210_device;
 
 extern struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *);
+extern void mcp2210_close(struct mcp2210_device *);
 
 extern bool mcp2210_configure_spi(struct mcp2210_device *, uint32_t bitrate, uint16_t idlechipsel, uint16_t activechipsel, uint16_t chipseltodatadelay, uint16_t lastbytetocsdelay, uint16_t midbytedelay);
 extern bool mcp2210_set_spimode(struct mcp2210_device *, uint8_t spimode);