Commit 97ab111ec924e8812eaeff2f4f71940811a3cf1b

Con Kolivas 2012-02-27T01:22:07

Merge pull request #147 from luke-jr/libudev_detect Use libudev to autodetect BitFORCE GPUs, if available

diff --git a/Makefile.am b/Makefile.am
index 5262d52..d319329 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -22,6 +22,7 @@ bin_SCRIPTS	= *.cl
 cgminer_LDFLAGS	= $(PTHREAD_FLAGS)
 cgminer_LDADD	= $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
 		  @OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
+		  @UDEV_LIBS@ \
 		  @MATH_LIBS@ lib/libgnu.a ccan/libccan.a
 cgminer_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib @OPENCL_FLAGS@
 
diff --git a/bitforce.c b/bitforce.c
index 29aacbc..e063664 100644
--- a/bitforce.c
+++ b/bitforce.c
@@ -28,6 +28,12 @@
 #endif
 #include <unistd.h>
 
+#include "config.h"
+
+#ifdef HAVE_LIBUDEV
+#include <libudev.h>
+#endif
+
 #include "elist.h"
 #include "miner.h"
 
@@ -121,7 +127,43 @@ static bool bitforce_detect_one(const char *devpath)
 	return true;
 }
 
-static void bitforce_detect_auto()
+static bool bitforce_detect_auto_udev()
+{
+#ifdef HAVE_LIBUDEV
+	struct udev *udev = udev_new();
+	struct udev_enumerate *enumerate = udev_enumerate_new(udev);
+	struct udev_list_entry *list_entry;
+	bool foundany = false;
+	
+	udev_enumerate_add_match_subsystem(enumerate, "tty");
+	udev_enumerate_add_match_property(enumerate, "ID_MODEL", "BitFORCE*SHA256");
+	udev_enumerate_scan_devices(enumerate);
+	udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
+		struct udev_device *device = udev_device_new_from_syspath(
+			udev_enumerate_get_udev(enumerate),
+			udev_list_entry_get_name(list_entry)
+		);
+		if (!device)
+			continue;
+		
+		const char *devpath = udev_device_get_devnode(device);
+		if (devpath) {
+			foundany = true;
+			bitforce_detect_one(devpath);
+		}
+		
+		udev_device_unref(device);
+	}
+	udev_enumerate_unref(enumerate);
+	udev_unref(udev);
+	
+	return foundany;
+#else
+	return false;
+#endif
+}
+
+static bool bitforce_detect_auto_devserial()
 {
 #ifndef WIN32
 	DIR *D;
@@ -129,22 +171,35 @@ static void bitforce_detect_auto()
 	const char udevdir[] = "/dev/serial/by-id";
 	char devpath[sizeof(udevdir) + 1 + NAME_MAX];
 	char *devfile = devpath + sizeof(udevdir);
-
+	bool foundany = false;
+	
 	D = opendir(udevdir);
 	if (!D)
-		return;
+		return false;
 	memcpy(devpath, udevdir, sizeof(udevdir) - 1);
 	devpath[sizeof(udevdir) - 1] = '/';
 	while ( (de = readdir(D)) ) {
 		if (!strstr(de->d_name, "BitFORCE_SHA256"))
 			continue;
+		foundany = true;
 		strcpy(devfile, de->d_name);
 		bitforce_detect_one(devpath);
 	}
 	closedir(D);
+	
+	return foundany;
+#else
+	return false;
 #endif
 }
 
+static void bitforce_detect_auto()
+{
+	bitforce_detect_auto_udev() ?:
+	bitforce_detect_auto_devserial() ?:
+	0;
+}
+
 static void bitforce_detect()
 {
 	struct string_elist *iter, *tmp;
diff --git a/configure.ac b/configure.ac
index 67847c7..f505383 100644
--- a/configure.ac
+++ b/configure.ac
@@ -260,6 +260,26 @@ fi
 
 AM_CONDITIONAL([HAS_YASM], [test x$has_yasm = xtrue])
 
+if test "x$bitforce" != xno; then
+	AC_ARG_WITH([libudev], [AC_HELP_STRING([--with-libudev], [Autodetect FPGAs using libudev])],
+		[libudev=$enableval],
+		[libudev=auto]
+		)
+	if test "x$libudev" != "xno"; then
+		AC_CHECK_LIB([udev], [udev_device_get_devnode], [
+			libudev=yes
+			UDEV_LIBS=-ludev
+			AC_DEFINE([HAVE_LIBUDEV], [1], [Defined to 1 if libudev is wanted])
+		], [
+			if test "x$libudev" = "xyes"; then
+				AC_MSG_ERROR([libudev not found])
+			fi
+			libudev=no
+		])
+	fi
+fi
+AM_CONDITIONAL([HAVE_LIBUDEV], [test x$libudev != xno])
+
 PKG_PROG_PKG_CONFIG()
 
 PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.15.6], [AC_DEFINE([CURL_HAS_SOCKOPT], [1], [Defined if version of curl supports sockopts.])],
@@ -320,6 +340,7 @@ AC_SUBST(NCURSES_LIBS)
 AC_SUBST(PDCURSES_LIBS)
 AC_SUBST(WS2_LIBS)
 AC_SUBST(MATH_LIBS)
+AC_SUBST(UDEV_LIBS)
 
 AC_CONFIG_FILES([
 	Makefile
@@ -383,6 +404,10 @@ else
 	echo "  Icarus.FPGAs.........: Disabled"
 fi
 
+if test "x$bitforce" != xno; then
+	echo "  libudev.detection....: $libudev"
+fi
+
 echo
 if test "x$cpumining" = xyes; then
 	echo "  CPU Mining...........: Enabled"
@@ -396,7 +421,7 @@ echo "Compilation............: make (or gmake)"
 echo "  CPPFLAGS.............: $CPPFLAGS"
 echo "  CFLAGS...............: $CFLAGS"
 echo "  LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS"
-echo "  LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS"
+echo "  LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS"
 echo
 echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
 echo "  prefix...............: $prefix"