Merge pull request #147 from luke-jr/libudev_detect Use libudev to autodetect BitFORCE GPUs, if available
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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"