Blame SOURCES/i2c-tools-3.1.0-load-i2cdev.patch

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