464363
diff -up i2c-tools-3.1.0/tools/i2cbusses.c.load-i2cdev i2c-tools-3.1.0/tools/i2cbusses.c
464363
--- i2c-tools-3.1.0/tools/i2cbusses.c.load-i2cdev	2010-11-26 11:25:32.000000000 +0100
464363
+++ i2c-tools-3.1.0/tools/i2cbusses.c	2017-04-05 08:43:08.135938481 +0200
464363
@@ -37,9 +37,16 @@
464363
 #include <dirent.h>
464363
 #include <fcntl.h>
464363
 #include <errno.h>
464363
+#ifdef USE_LIBKMOD
464363
+	#include <libkmod.h>
464363
+#endif
464363
 #include "i2cbusses.h"
464363
 #include <linux/i2c-dev.h>
464363
 
464363
+#define BUFLEN (NAME_MAX > 512 ? NAME_MAX : 512)
464363
+#define I2C_DEV_MOD_NAME "i2c_dev"
464363
+#define I2C_DEV_SUBPATH  "/class/i2c-dev"
464363
+
464363
 enum adt { adt_dummy, adt_isa, adt_i2c, adt_smbus, adt_unknown };
464363
 
464363
 struct adap_type {
464363
@@ -60,6 +67,145 @@ static struct adap_type adap_types[5] =
464363
 	  .algo		= "N/A", },
464363
 };
464363
 
464363
+/* Get and cache sysfs mount point. */
464363
+static const char *get_sysfs_mount_point(void) {
464363
+	static const char *mp = NULL;
464363
+
464363
+	if (mp == NULL) {
464363
+		char sysfs[NAME_MAX];
464363
+		char fstype[NAME_MAX];
464363
+		char line[BUFLEN];
464363
+		FILE *f;
464363
+
464363
+		if ((f = fopen("/proc/mounts", "r")) == NULL) {
464363
+			perror("failed to read /proc/mounts: ");
464363
+			goto done;
464363
+		}
464363
+		while (fgets(line, BUFLEN, f)) {
464363
+			sscanf(line, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfs, fstype);
464363
+			if (strcasecmp(fstype, "sysfs") == 0) {
464363
+				mp = strdup(sysfs);
464363
+				if (mp == NULL)
464363
+					perror("strdup: ");
464363
+				break;
464363
+			}
464363
+		}
464363
+		fclose(f);
464363
+	}
464363
+done:
464363
+	return mp;
464363
+}
464363
+
464363
+/* Get an absolute path of i2c device directory. Free the result when not
464363
+ * needed. */
464363
+static char *get_i2c_dev_path(void) {
464363
+	char *path = NULL;
464363
+	int mplen, splen;
464363
+	const char *mp = get_sysfs_mount_point();
464363
+
464363
+	if (mp != NULL) {
464363
+		mplen = strlen(mp);
464363
+		splen = strlen(I2C_DEV_SUBPATH);
464363
+		path = malloc(mplen + splen + 1);
464363
+		if (path == NULL) {
464363
+			perror("malloc: ");
464363
+		} else {
464363
+			strncpy(path, mp, mplen);
464363
+			strncpy(path + mplen, I2C_DEV_SUBPATH, splen);
464363
+			path[mplen+splen] = '\0';
464363
+		}
464363
+	}
464363
+	return path;
464363
+}
464363
+
464363
+/**
464363
+ * Try to load i2c_dev kernel mode. Do nothing if module is already loaded.
464363
+ * Returns 1 on success, 0 otherwise.
464363
+ */
464363
+static int try_load_i2c_dev_mod(void) {
464363
+	int err = 0, loaded = 0;
464363
+	char errbuf[BUFLEN] = { 0 };
464363
+#ifdef USE_LIBKMOD
464363
+	int flags = 0;
464363
+	struct kmod_ctx *ctx;
464363
+	struct kmod_list *l, *list = NULL;
464363
+
464363
+	ctx = kmod_new(NULL, NULL);
464363
+	if (!ctx) {
464363
+		snprintf(errbuf, BUFLEN, "kmod_new() failed!");
464363
+		goto done;
464363
+	}
464363
+	if (kmod_module_new_from_lookup(ctx, I2C_DEV_MOD_NAME, &list) < 0 || list == NULL) {
464363
+		snprintf(errbuf, BUFLEN, I2C_DEV_MOD_NAME " module lookup failed");
464363
+		goto ctx_unref;
464363
+	}
464363
+
464363
+	flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
464363
+	kmod_list_foreach(l, list) {
464363
+		struct kmod_module *mod = kmod_module_get_module(l);
464363
+		err = kmod_module_probe_insert_module(mod, flags, NULL, NULL, NULL, NULL);
464363
+		if (err == -ENOENT) {
464363
+			snprintf(errbuf, BUFLEN,
464363
+					"unknown symbol in module \"%s\", or unknown parameter (see dmesg)",
464363
+					kmod_module_get_name(mod));
464363
+		} else if (err < 0) {
464363
+			snprintf(errbuf, BUFLEN, "(module %s): %s",
464363
+					kmod_module_get_name(mod), strerror(-err));
464363
+		} else {
464363
+			kmod_module_unref(mod);
464363
+			++loaded;
464363
+			break;
464363
+		}
464363
+		kmod_module_unref(mod);
464363
+	}
464363
+
464363
+	kmod_module_unref_list(list);
464363
+ctx_unref:
464363
+	kmod_unref(ctx);
464363
+#else   /* Try to load the module with modprobe. */
464363
+	struct stat st;
464363
+	char *dev_path = get_i2c_dev_path();
464363
+
464363
+	/* First check whether the module is already loaded or built-in. */
464363
+	if (dev_path == NULL)
464363
+		goto done;
464363
+	err = stat(dev_path, &st);
464363
+	if (err < 0) {
464363
+		if (errno != ENOENT) {
464363
+			snprintf(errbuf, BUFLEN, "can not stat \"%s\": %s", dev_path,
464363
+				   	strerror(errno));
464363
+			goto err;
464363
+		} else {
464363
+			err = 0;
464363
+		}
464363
+	} else {
464363
+		++loaded;
464363
+		goto done;
464363
+	}
464363
+
464363
+	err = system("modprobe " I2C_DEV_MOD_NAME);
464363
+	if (err < 0) {
464363
+		snprintf(errbuf, BUFLEN, "failed to execute modprobe command");
464363
+	} else if (err > 0) {
464363
+		snprintf(errbuf, BUFLEN, "modprobe command exited with code %d",
464363
+				WEXITSTATUS(err));
464363
+	} else {
464363
+		++loaded;
464363
+		goto done;
464363
+	}
464363
+
464363
+err:
464363
+#endif
464363
+	if (errbuf[0])
464363
+		fprintf(stderr, "Failed to load required " I2C_DEV_MOD_NAME
464363
+			   	" kernel module: %s\n", errbuf);
464363
+done:
464363
+#ifndef USE_LIBKMOD
464363
+	free(dev_path);
464363
+#endif
464363
+	return loaded;
464363
+}
464363
+
464363
 static enum adt i2c_get_funcs(int i2cbus)
464363
 {
464363
 	unsigned long funcs;
464363
@@ -132,8 +278,7 @@ struct i2c_adap *gather_i2c_busses(void)
464363
 	struct dirent *de, *dde;
464363
 	DIR *dir, *ddir;
464363
 	FILE *f;
464363
-	char fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX];
464363
-	int foundsysfs = 0;
464363
+	char *sysfs = NULL, n[NAME_MAX];
464363
 	int count=0;
464363
 	struct i2c_adap *adapters;
464363
 
464363
@@ -185,29 +330,19 @@ struct i2c_adap *gather_i2c_busses(void)
464363
 		goto done;
464363
 	}
464363
 
464363
-	/* look in sysfs */
464363
-	/* First figure out where sysfs was mounted */
464363
-	if ((f = fopen("/proc/mounts", "r")) == NULL) {
464363
+	sysfs = get_i2c_dev_path();
464363
+	if (sysfs == NULL)
464363
 		goto done;
464363
-	}
464363
-	while (fgets(n, NAME_MAX, f)) {
464363
-		sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfs, fstype);
464363
-		if (strcasecmp(fstype, "sysfs") == 0) {
464363
-			foundsysfs++;
464363
-			break;
464363
-		}
464363
-	}
464363
-	fclose(f);
464363
-	if (! foundsysfs) {
464363
-		goto done;
464363
-	}
464363
 
464363
 	/* Bus numbers in i2c-adapter don't necessarily match those in
464363
 	   i2c-dev and what we really care about are the i2c-dev numbers.
464363
 	   Unfortunately the names are harder to get in i2c-dev */
464363
-	strcat(sysfs, "/class/i2c-dev");
464363
-	if(!(dir = opendir(sysfs)))
464363
-		goto done;
464363
+	if(!(dir = opendir(sysfs))) {
464363
+		if (!try_load_i2c_dev_mod())
464363
+			goto done;
464363
+		if ((!(dir = opendir(sysfs))))
464363
+			goto done;
464363
+	}
464363
 	/* go through the busses */
464363
 	while ((de = readdir(dir)) != NULL) {
464363
 		if (!strcmp(de->d_name, "."))
464363
@@ -272,14 +407,15 @@ found:
464363
 				/* We need more space */
464363
 				adapters = more_adapters(adapters, count + 1);
464363
 				if (!adapters)
464363
-					return NULL;
464363
+					goto done;
464363
 			}
464363
 
464363
 			adapters[count].nr = i2cbus;
464363
 			adapters[count].name = strdup(s);
464363
 			if (adapters[count].name == NULL) {
464363
 				free_adapters(adapters);
464363
-				return NULL;
464363
+				adapters = NULL;
464363
+				goto done;
464363
 			}
464363
 			adapters[count].funcs = adap_types[type].funcs;
464363
 			adapters[count].algo = adap_types[type].algo;
464363
@@ -289,6 +425,7 @@ found:
464363
 	closedir(dir);
464363
 
464363
 done:
464363
+	free(sysfs);
464363
 	return adapters;
464363
 }
464363
 
464363
@@ -380,8 +517,21 @@ int open_i2c_dev(int i2cbus, char *filen
464363
 	file = open(filename, O_RDWR);
464363
 
464363
 	if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
464363
+		if (!(try_load_i2c_dev_mod()))
464363
+			fprintf(stderr, "Cannot load i2c-dev module.\n");
464363
+		else
464363
+			file = open(filename, O_RDWR);
464363
+	}
464363
+
464363
+	if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
464363
 		sprintf(filename, "/dev/i2c-%d", i2cbus);
464363
 		file = open(filename, O_RDWR);
464363
+		if (file < 0 && (errno == ENOENT || errno == ENOTDIR)) {
464363
+			if (!(try_load_i2c_dev_mod()))
464363
+				fprintf(stderr, "Cannot load i2c-dev module.\n");
464363
+			else
464363
+				file = open(filename, O_RDWR);
464363
+		}
464363
 	}
464363
 
464363
 	if (file < 0 && !quiet) {
464363
diff -up i2c-tools-3.1.0/tools/Module.mk.load-i2cdev i2c-tools-3.1.0/tools/Module.mk
464363
--- i2c-tools-3.1.0/tools/Module.mk.load-i2cdev	2009-01-19 16:11:33.000000000 +0100
464363
+++ i2c-tools-3.1.0/tools/Module.mk	2017-04-05 08:41:10.554057368 +0200
464363
@@ -15,6 +15,11 @@ TOOLS_CFLAGS	:= -Wstrict-prototypes -Wsh
464363
 
464363
 TOOLS_TARGETS	:= i2cdetect i2cdump i2cset i2cget
464363
 
464363
+ifeq ($(shell pkg-config --exists libkmod && echo 1), 1)
464363
+    TOOLS_CFLAGS  += $(shell pkg-config --cflags libkmod) -DUSE_LIBKMOD
464363
+    LDFLAGS += $(shell pkg-config --libs libkmod)
464363
+endif
464363
+
464363
 #
464363
 # Programs
464363
 #