Browse Source

Merge branch 'nanofury' into bfgminer

Luke Dashjr 12 years ago
parent
commit
00f905584e
7 changed files with 63 additions and 71 deletions
  1. 1 0
      configure.ac
  2. 5 3
      driver-nanofury.c
  3. 45 58
      fpgautils.c
  4. 3 0
      fpgautils.h
  5. 4 1
      lowlevel.c
  6. 2 0
      lowlevel.h
  7. 3 9
      mcp2210.c

+ 1 - 0
configure.ac

@@ -656,6 +656,7 @@ elif test "x$bitfury" = "xyes"; then
 	fi
 elif test "x$nanofury" = "xyes"; then
 	AC_MSG_ERROR([You explicitly disabled Bitfury and explicitly enabled NanoFury])
+	need_fpgautils=yes
 else
 	nanofury=no
 fi

+ 5 - 3
driver-nanofury.c

@@ -14,6 +14,7 @@
 
 #include "deviceapi.h"
 #include "driver-bitfury.h"
+#include "fpgautils.h"
 #include "libbitfury.h"
 #include "logging.h"
 #include "lowlevel.h"
@@ -189,7 +190,8 @@ bool nanofury_foundlowl(struct lowlevel_device_info * const info)
 	nanofury_device_off(mcp);
 	mcp2210_close(mcp);
 	
-	// TODO: claim device
+	if (bfg_claim_hid(&nanofury_drv, true, info->path))
+		return false;
 	
 	struct cgpu_info *cgpu;
 	cgpu = malloc(sizeof(*cgpu));
@@ -198,8 +200,8 @@ bool nanofury_foundlowl(struct lowlevel_device_info * const info)
 		.device_data = info,
 		.threads = 1,
 		// TODO: .name
-		// TODO: .device_path
-		// TODO: .dev_manufacturer
+		.device_path = strdup(info->path),
+		.dev_manufacturer = strdup(info->manufacturer),
 		.dev_product = strdup(product),
 		.dev_serial = strdup(serial),
 		.deven = DEV_ENABLED,

+ 45 - 58
fpgautils.c

@@ -114,7 +114,7 @@ bool search_needles(const char *haystack, va_list needles)
 static
 int _detectone_wrap(const detectone_func_t detectone, const char * const param, const char *fname)
 {
-	if (bfg_claim_serial(NULL, false, param))
+	if (bfg_claim_serial(NULL, true, param))
 	{
 		applog(LOG_DEBUG, "%s: %s is already claimed, skipping probe", fname, param);
 		return 0;
@@ -494,46 +494,37 @@ int _serial_autodetect(detectone_func_t detectone, ...)
 	return rv;
 }
 
-enum bfg_device_bus {
-	BDB_SERIAL,
-	BDB_USB,
-};
-
-// TODO: claim USB side of USB-Serial devices
-typedef
-struct my_dev_t {
-	enum bfg_device_bus bus;
-	union {
-		struct {
-			uint8_t usbbus;
-			uint8_t usbaddr;
-		};
-#ifndef WIN32
-		dev_t dev;
-#else
-		int com;
-#endif
-	};
-} my_dev_t;
 
 struct _device_claim {
 	struct device_drv *drv;
-	my_dev_t dev;
+	char *devpath;
 	UT_hash_handle hh;
 };
 
-static
-struct device_drv *bfg_claim_any(struct device_drv * const api, const char * const verbose, const my_dev_t * const dev)
+struct device_drv *bfg_claim_any(struct device_drv * const api, const char *verbose, const char * const devpath)
 {
 	static struct _device_claim *claims = NULL;
 	struct _device_claim *c;
 	
-	HASH_FIND(hh, claims, dev, sizeof(*dev), c);
+	HASH_FIND_STR(claims, devpath, c);
 	if (c)
 	{
-		if (verbose)
-			applog(LOG_DEBUG, "%s device %s already claimed by other driver: %s",
-			       api->dname, verbose, c->drv->dname);
+		if (verbose && opt_debug)
+		{
+			char logbuf[LOGBUFSIZ];
+			logbuf[0] = '\0';
+			if (api)
+				tailsprintf(logbuf, sizeof(logbuf), "%s device ", api->dname);
+			if (verbose[0])
+				tailsprintf(logbuf, sizeof(logbuf), "%s (%s)", verbose, devpath);
+			else
+				tailsprintf(logbuf, sizeof(logbuf), "%s", devpath);
+			tailsprintf(logbuf, sizeof(logbuf), " already claimed by ");
+			if (api)
+				tailsprintf(logbuf, sizeof(logbuf), "other ");
+			tailsprintf(logbuf, sizeof(logbuf), "driver: %s", c->drv->dname);
+			_applog(LOG_DEBUG, logbuf);
+		}
 		return c->drv;
 	}
 	
@@ -541,59 +532,55 @@ struct device_drv *bfg_claim_any(struct device_drv * const api, const char * con
 		return NULL;
 	
 	c = malloc(sizeof(*c));
-	c->dev = *dev;
+	c->devpath = strdup(devpath);
 	c->drv = api;
-	HASH_ADD(hh, claims, dev, sizeof(*dev), c);
+	HASH_ADD_KEYPTR(hh, claims, c->devpath, strlen(devpath), c);
 	return NULL;
 }
 
+struct device_drv *bfg_claim_any2(struct device_drv * const api, const char * const verbose, const char * const llname, const char * const path)
+{
+	const size_t llnamesz = strlen(llname);
+	const size_t pathsz = strlen(path);
+	char devpath[llnamesz + 1 + pathsz + 1];
+	memcpy(devpath, llname, llnamesz);
+	devpath[llnamesz] = ':';
+	memcpy(&devpath[llnamesz+1], path, pathsz + 1);
+	return bfg_claim_any(api, verbose, devpath);
+}
+
 struct device_drv *bfg_claim_serial(struct device_drv * const api, const bool verbose, const char * const devpath)
 {
-	my_dev_t dev;
-	
-	memset(&dev, 0, sizeof(dev));
-	dev.bus = BDB_SERIAL;
 #ifndef WIN32
+	char devs[6 + (sizeof(dev_t) * 2) + 1];
 	{
 		struct stat my_stat;
 		if (stat(devpath, &my_stat))
 			return NULL;
-		dev.dev = my_stat.st_rdev;
+		memcpy(devs, "dev_t:", 6);
+		bin2hex(&devs[6], &my_stat.st_rdev, sizeof(dev_t));
 	}
 #else
-	{
 		char *p = strstr(devpath, "COM"), *p2;
 		if (!p)
 			return NULL;
-		dev.com = strtol(&p[3], &p2, 10);
+		const int com = strtol(&p[3], &p2, 10);
 		if (p2 == p)
 			return NULL;
-	}
+	char dummy;
+	const int sz = snprintf(&dummy, 1, "%d", com);
+	char devs[4 + sz + 1];
+	sprintf(devs, "com:%d", com);
 #endif
 	
-	return bfg_claim_any(api, (verbose ? devpath : NULL), &dev);
+	return bfg_claim_any(api, (verbose ? devpath : NULL), devs);
 }
 
 struct device_drv *bfg_claim_usb(struct device_drv * const api, const bool verbose, const uint8_t usbbus, const uint8_t usbaddr)
 {
-	my_dev_t dev;
-	char *desc = NULL;
-	
-	// We should be able to just initialize a const my_dev_t for this, but Xcode's clang is broken
-	// Affected: Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) AKA Xcode 4.6.3
-	// Works with const: GCC 4.6.3, LLVM 3.1
-	memset(&dev, 0, sizeof(dev));
-	dev.bus = BDB_USB;
-	dev.usbbus = usbbus;
-	dev.usbaddr = usbaddr;
-	
-	if (verbose)
-	{
-		desc = alloca(3 + 1 + 3 + 1);
-		sprintf(desc, "%03u:%03u", (unsigned)usbbus, (unsigned)usbaddr);
-	}
-	
-	return bfg_claim_any(api, desc, &dev);
+	char devpath[12];
+	sprintf(devpath, "usb:%03u:%03u", (unsigned)usbbus, (unsigned)usbaddr);
+	return bfg_claim_any(api, verbose ? "" : NULL, devpath);
 }
 
 #ifdef HAVE_LIBUSB

+ 3 - 0
fpgautils.h

@@ -28,11 +28,14 @@ extern void clear_detectone_meta_info(void);
 extern int _serial_autodetect(detectone_func_t, ...);
 #define serial_autodetect(...)  _serial_autodetect(__VA_ARGS__, NULL)
 
+extern struct device_drv *bfg_claim_any(struct device_drv *, const char *verbose, const char *devpath);
+extern struct device_drv *bfg_claim_any2(struct device_drv *, const char *verbose, const char *llname, const char *path);
 extern struct device_drv *bfg_claim_serial(struct device_drv * const, const bool verbose, const char * const devpath);
 #define serial_claim(devpath, drv)    bfg_claim_serial(drv, false, devpath)
 #define serial_claim_v(devpath, drv)  bfg_claim_serial(drv, true , devpath)
 extern struct device_drv *bfg_claim_usb(struct device_drv * const, const bool verbose, const uint8_t usbbus, const uint8_t usbaddr);
 #define bfg_claim_libusb(api, verbose, dev)  bfg_claim_usb(api, verbose, libusb_get_bus_number(dev), libusb_get_device_address(dev))
+#define bfg_claim_hid(api, verbose, path)  bfg_claim_any2(api, (verbose)?"":NULL, "hid", path)
 
 #ifdef HAVE_LIBUSB
 extern void cgpu_copy_libusb_strings(struct cgpu_info *, libusb_device *);

+ 4 - 1
lowlevel.c

@@ -20,9 +20,12 @@ static struct lowlevel_device_info *devinfo_list;
 
 void lowlevel_devinfo_free(struct lowlevel_device_info * const info)
 {
-	info->lowl->devinfo_free(info);
+	if (info->lowl->devinfo_free)
+		info->lowl->devinfo_free(info);
+	free(info->manufacturer);
 	free(info->product);
 	free(info->serial);
+	free(info->path);
 	free(info);
 }
 

+ 2 - 0
lowlevel.h

@@ -13,8 +13,10 @@ struct lowlevel_driver {
 };
 
 struct lowlevel_device_info {
+	char *manufacturer;
 	char *product;
 	char *serial;
+	char *path;
 	
 	struct lowlevel_driver *lowl;
 	void *lowl_data;

+ 3 - 9
mcp2210.c

@@ -134,12 +134,6 @@ bool hidapi_load_library()
 	return false;
 }
 
-static
-void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
-{
-	free(info->lowl_data);
-}
-
 static
 char *wcs2str_dup(wchar_t *ws)
 {
@@ -182,7 +176,8 @@ struct lowlevel_device_info *mcp2210_devinfo_scan()
 		info = malloc(sizeof(struct lowlevel_device_info));
 		*info = (struct lowlevel_device_info){
 			.lowl = &lowl_mcp2210,
-			.lowl_data = strdup(hid_item->path),
+			.path = strdup(hid_item->path),
+			.manufacturer = wcs2str_dup(hid_item->manufacturer_string),
 			.product = wcs2str_dup(hid_item->product_string),
 			.serial  = wcs2str_dup(hid_item->serial_number),
 		};
@@ -242,7 +237,7 @@ bool mcp2210_get_configs(struct mcp2210_device * const h)
 struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *info)
 {
 	struct mcp2210_device *h;
-	char * const path = info->lowl_data;
+	char * const path = info->path;
 	hid_device * const hid = hid_open_path(path);
 	
 	if (unlikely(!hid))
@@ -455,5 +450,4 @@ enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h,
 
 struct lowlevel_driver lowl_mcp2210 = {
 	.devinfo_scan = mcp2210_devinfo_scan,
-	.devinfo_free = mcp2210_devinfo_free,
 };