autoload i2c-dev module when needed
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
we've got following request (viz [1]):
i2cdetect requires the i2c-dev module be loaded so the appropriate /dev entries are created. This is not done automatically, so when i2cdetect is initially run it gives the false impression that no buses/devices exist:
# i2cdetect -l # modprobe i2c-dev # i2cdetect -l i2c-0 i2c i915 gmbus ssc I2C adapter i2c-1 i2c i915 gmbus vga I2C adapter i2c-2 i2c i915 gmbus panel I2C adapter i2c-3 i2c i915 gmbus dpc I2C adapter i2c-4 i2c i915 gmbus dpb I2C adapter i2c-5 i2c i915 gmbus dpd I2C adapter i2c-6 i2c DPDDC-C I2C adapter i2c-7 i2c DPDDC-D I2C adapter
Attached is a patch that autoloads i2c-dev module when neccessary. If you like it and have commit rights, push it please instead of me. If you don't like it, don't hold back and tell me.
Best regards,
Michal Minar
[1] https://bugzilla.redhat.com/show_bug.cgi?id=913203
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQEcBAEBAgAGBQJTkCedAAoJEPezUylxRFUDExAIAJzpGhv0O0U4zFCkzz9wH1cM mJOzW6Pu/Xnxk/x9e5f5RVG3BIrieJueRbzU/nh+KHqWbvEV/8DOtQ6KVV3c4Sau Deocg/QqdpMH1UasVIio2iUyuBJS/RvhclJOzo05cDhLfyo5bSp7ixLBwwcjlY4c UeXcTq7OPID5B4bpZzs2R7T2M81FG3wdI8GcqTNthQahLnCa0XbxEbyGZn2XaiVD 51AeADzmD5OTCPfQFmYq87MW/S101O+GEJJwXb/DngHh2mIVnur0JiDnABdB+Pl4 ysgqooMOMol2blcUQmkwwLzkJ3ArE3MGvjGVtqFBOsI5VSpWsapGa6TdFeYm0UY= =4tFG
-----END PGP SIGNATURE-----
tools/i2cbusses.c +76 -2
@@ -37,9 +37,15 @@#include <dirent.h> #include <fcntl.h> #include <errno.h> +#ifdef USE_LIBKMOD +#include <libkmod.h> +#endif #include "i2cbusses.h" #include <linux/i2c-dev.h> +#define BUFLEN 512 +#define I2C_DEV_MOD_NAME "i2c_dev" +enum adt { adt_dummy, adt_isa, adt_i2c, adt_smbus, adt_unknown };struct adap_type {
@@ -60,6 +66,70 @@ static struct adap_type adap_types[5] = .algo = "N/A", }, }; +/** +* Try to load i2c_dev kernel mode. Do nothing, if module is already loaded. +* Returns 1 on success, 0 otherwise. +*/ +static int try_load_i2c_dev_mod (void ) { +int err =0 , loaded =0 ; +char errbuf[BUFLEN ] = {0 }; +#ifdef USE_LIBKMOD +int flags =0 ; +struct kmod_ctx *ctx; +struct kmod_list *l, *list =NULL ; + + ctx =kmod_new (NULL ,NULL ); +if (!ctx) { +snprintf (errbuf,BUFLEN ,"kmod_new() failed!" ); + goto done; + } +if (kmod_module_new_from_lookup (ctx,I2C_DEV_MOD_NAME , &list) <0 || list ==NULL ) { +snprintf (errbuf,BUFLEN ,I2C_DEV_MOD_NAME " module lookup failed" ); + goto ctx_unref; + } + + flags |=KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY ; + kmod_list_foreach(l, list) { +struct kmod_module *mod =kmod_module_get_module (l); + err =kmod_module_probe_insert_module (mod, flags,NULL ,NULL ,NULL ,NULL ); +if (err == -ENOENT ) { +snprintf (errbuf,BUFLEN , +"unknown symbol in module \"%s\", or unknown parameter (see dmesg)" , +kmod_module_get_name (mod)); + }else if (err <0 ) { +snprintf (errbuf,BUFLEN ,"(module %s): %s" , +kmod_module_get_name (mod),strerror (-err)); + }else { +kmod_module_unref (mod); + ++loaded; +break ; + } +kmod_module_unref (mod); + } + +list_unref: +kmod_module_unref_list (list); +ctx_unref: +kmod_unref (ctx); +#else + err =system ("modprobe " I2C_DEV_MOD_NAME ); +if (err <0 ) { +snprintf (errbuf,BUFLEN ,"failed to execute modprobe command" ); + }else if (err >0 ) { +snprintf (errbuf,BUFLEN ,"modprobe command exited with code %d" , +WEXITSTATUS (err)); + }else { + ++loaded; + goto done; + } +#endif +if (errbuf[0 ]) +fprintf (stderr,"Failed to load required " I2C_DEV_MOD_NAME +" kernel module: %s\n" , errbuf); +done: +return loaded; +} +static enum adt i2c_get_funcs (int i2cbus) {unsigned long funcs;
@@ -206,8 +276,12 @@ struct i2c_adap *gather_i2c_busses(void)i2c-dev and what we really care about are the i2c-dev numbers. Unfortunately the names are harder to get in i2c-dev */ strcat (sysfs,"/class/i2c-dev" ); -if (!(dir =opendir (sysfs))) - goto done; +if (!(dir =opendir (sysfs))) { +if (!try_load_i2c_dev_mod ()) + goto done; +if ((!(dir =opendir (sysfs)))) + goto done; + }/* go through the busses */ while ((de =readdir (dir)) !=NULL ) {if (!strcmp (de->d_name,"." ))
tools/Module.mk +5 -0
@@ -15,6 +15,11 @@ TOOLS_CFLAGS := -Wstrict-prototypes -Wsh TOOLS_TARGETS := i2cdetect i2cdump i2cset i2cget +ifeq ($(shell pkg-config --exists libkmod && echo 1), 1) + TOOLS_CFLAGS += $(shell pkg-config --cflags libkmod) -DUSE_LIBKMOD + LDFLAGS += $(shell pkg-config --libs libkmod) +endif + # # Programs #
Adding Jean Delvare who maintains i2c-tools to CC...
Quoted 168 lines · Michal Minář
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Hello,
we've got following request (viz [1]):
i2cdetect requires the i2c-dev module be loaded so the appropriate /dev entries are created. This is not done automatically, so when i2cdetect is initially run it gives the false impression that no buses/devices exist:
# i2cdetect -l # modprobe i2c-dev # i2cdetect -l i2c-0 i2c i915 gmbus ssc I2C adapter i2c-1 i2c i915 gmbus vga I2C adapter i2c-2 i2c i915 gmbus panel I2C adapter i2c-3 i2c i915 gmbus dpc I2C adapter i2c-4 i2c i915 gmbus dpb I2C adapter i2c-5 i2c i915 gmbus dpd I2C adapter i2c-6 i2c DPDDC-C I2C adapter i2c-7 i2c DPDDC-D I2C adapterAttached is a patch that autoloads i2c-dev module when neccessary. If you like it and have commit rights, push it please instead of me. If you don't like it, don't hold back and tell me.
Best regards,
Michal Minar[1] https://bugzilla.redhat.com/show_bug.cgi?id=913203
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/iQEcBAEBAgAGBQJTkCedAAoJEPezUylxRFUDExAIAJzpGhv0O0U4zFCkzz9wH1cM mJOzW6Pu/Xnxk/x9e5f5RVG3BIrieJueRbzU/nh+KHqWbvEV/8DOtQ6KVV3c4Sau Deocg/QqdpMH1UasVIio2iUyuBJS/RvhclJOzo05cDhLfyo5bSp7ixLBwwcjlY4c UeXcTq7OPID5B4bpZzs2R7T2M81FG3wdI8GcqTNthQahLnCa0XbxEbyGZn2XaiVD 51AeADzmD5OTCPfQFmYq87MW/S101O+GEJJwXb/DngHh2mIVnur0JiDnABdB+Pl4 ysgqooMOMol2blcUQmkwwLzkJ3ArE3MGvjGVtqFBOsI5VSpWsapGa6TdFeYm0UY= =4tFG
-----END PGP SIGNATURE-----Index:
i2c-tools-3.1.0/tools/i2cbusses.c
===================================================================tools/i2cbusses.c +76
-2@@ -37,9 +37,15 @@#include <dirent.h> #include <fcntl.h> #include <errno.h> +#ifdef USE_LIBKMOD +#include <libkmod.h> +#endif #include "i2cbusses.h" #include <linux/i2c-dev.h> +#define BUFLEN 512 +#define I2C_DEV_MOD_NAME "i2c_dev" +enum adt { adt_dummy, adt_isa, adt_i2c, adt_smbus, adt_unknown };struct adap_type {@@ -60,6 +66,70 @@ static struct adap_type adap_types[5] = .algo = "N/A", }, }; +/** +* Try to load i2c_dev kernel mode. Do nothing, if module is already loaded. +* Returns 1 on success, 0 otherwise. +*/ +static int try_load_i2c_dev_mod (void ) { +int err =0 , loaded =0 ; +char errbuf[BUFLEN ] = {0 }; +#ifdef USE_LIBKMOD +int flags =0 ; +struct kmod_ctx *ctx; +struct kmod_list *l, *list =NULL ; + + ctx =kmod_new (NULL ,NULL ); +if (!ctx) { +snprintf (errbuf,BUFLEN ,"kmod_new() failed!" ); + goto done; + } +if (kmod_module_new_from_lookup (ctx,I2C_DEV_MOD_NAME , &list) <0 || list ==NULL ) { +snprintf (errbuf,BUFLEN ,I2C_DEV_MOD_NAME " module lookup failed" ); + goto ctx_unref; + } + + flags |=KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY ; + kmod_list_foreach(l, list) { +struct kmod_module *mod =kmod_module_get_module (l); + err =kmod_module_probe_insert_module (mod, flags,NULL ,NULL ,NULL ,NULL ); +if (err == -ENOENT ) { +snprintf (errbuf,BUFLEN , +"unknown symbol in module \"%s\", or unknown parameter (see dmesg)" , +kmod_module_get_name (mod)); + }else if (err <0 ) { +snprintf (errbuf,BUFLEN ,"(module %s): %s" , +kmod_module_get_name (mod),strerror (-err)); + }else { +kmod_module_unref (mod); + ++loaded; +break ; + } +kmod_module_unref (mod); + } + +list_unref: +kmod_module_unref_list (list); +ctx_unref: +kmod_unref (ctx); +#else + err =system ("modprobe " I2C_DEV_MOD_NAME ); +if (err <0 ) { +snprintf (errbuf,BUFLEN ,"failed to execute modprobe command" ); + }else if (err >0 ) { +snprintf (errbuf,BUFLEN ,"modprobe command exited with code %d" , +WEXITSTATUS (err)); + }else { + ++loaded; + goto done; + } +#endif +if (errbuf[0 ]) +fprintf (stderr,"Failed to load required " I2C_DEV_MOD_NAME +" kernel module: %s\n" , errbuf); +done: +return loaded; +} +static enum adt i2c_get_funcs (int i2cbus) {unsigned long funcs;@@ -206,8 +276,12 @@ struct i2c_adap *gather_i2c_busses(void)i2c-dev and what we really care about are the i2c-dev numbers. Unfortunately the names are harder to get in i2c-dev */ strcat (sysfs,"/class/i2c-dev" ); -if (!(dir =opendir (sysfs))) - goto done; +if (!(dir =opendir (sysfs))) { +if (!try_load_i2c_dev_mod ()) + goto done; +if ((!(dir =opendir (sysfs)))) + goto done; + }/* go through the busses */ while ((de =readdir (dir)) !=NULL ) {if (!strcmp (de->d_name,"." ))Index:
i2c-tools-3.1.0/tools/Module.mk
===================================================================tools/Module.mk +5
-0@@ -15,6 +15,11 @@ TOOLS_CFLAGS := -Wstrict-prototypes -Wsh TOOLS_TARGETS := i2cdetect i2cdump i2cset i2cget +ifeq ($(shell pkg-config --exists libkmod && echo 1), 1) + TOOLS_CFLAGS += $(shell pkg-config --cflags libkmod) -DUSE_LIBKMOD + LDFLAGS += $(shell pkg-config --libs libkmod) +endif + # # Programs #
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Thanks Dan!
Updated and a bit refactored patch is attached.
Michal
Quoted 40 lines · Danielle Costantino
you need to add this after the #else , before the err = system... to accommodate for systems that have i2c-dev.ko built in, not as a module, on older kernels.
struct stat st;
err =
stat("/sys/class/i2c-dev", &st);if (err < 0) { if (errno != ENOENT) { err = -errno; goto done; } else err = 0; } else return 1;
Quoted 4 lines · Wolfram Sang
Adding Jean Delvare who maintains i2c-tools to CC...
On Thu, Jun 05, 2014 at 10:17:33AM +0200, Michal Minář wrote:
Hello,
we've got following request (viz [1]):
i2cdetect requires the i2c-dev module be loaded so the appropriate /dev entries are created. This is not done automatically, so when i2cdetect is initially run it gives the false impression that no buses/devices exist:
# i2cdetect -l # modprobe i2c-dev # i2cdetect -l i2c-0 i2c i915 gmbus ssc I2C adapter i2c-1 i2c i915 gmbus vga I2C adapter i2c-2 i2c i915 gmbus panel I2C adapter i2c-3 i2c i915 gmbus dpc I2C adapter i2c-4 i2c i915 gmbus dpb I2C adapter i2c-5 i2c i915 gmbus dpd I2C adapter i2c-6 i2c DPDDC-C I2C adapter i2c-7 i2c DPDDC-D I2C adapter
Quoted 12 lines
Attached is a patch that autoloads i2c-dev module when neccessary. If you like it and have commit rights, push it please instead of me. If you don't like it, don't hold back and tell me.
Best regards, Michal Minar
[1] https://bugzilla.redhat.com/show_bug.cgi?id=913203
Quoted 5 lines
Quoted 5 lines
Index:
i2c-tools-3.1.0/tools/i2cbusses.c
===================================================================
- --- i2c-tools-3.1.0.orig/tools/i2cbusses.c
Quoted 65 lines
Quoted 65 lines
Quoted 5 lines
+++
i2c-tools-3.1.0/tools/i2cbusses.c@@ -37,9 +37,15 @@ #include <dirent.h> #include <fcntl.h> #include <errno.h>
+#ifdefUSE_LIBKMOD+ #include <libkmod.h> +#endif #include
"i2cbusses.h" #include <linux/i2c-dev.h>Quoted 8 lines
+#define BUFLEN 512 +#define
I2C_DEV_MOD_NAME"i2c_dev" + enum adt {adt_dummy,adt_isa,adt_i2c,adt_smbus,adt_unknown};struct
adap_type{ @@ -60,6 +66,70 @@ static structadap_type
adap_types[5] = .algo = "N/A", }, };+/** + * Try to load
i2c_devkernel mode. Do nothing, if module is alreadyloaded.
Quoted 8 lines
+ * Returns 1 on success, 0 otherwise. + */ +static int
try_load_i2c_dev_mod(void){ + int err = 0, loaded = 0; +
char errbuf[BUFLEN] = { 0 }; +#ifdefUSE_LIBKMOD+ int
flags = 0; + structkmod_ctx*ctx; + structkmod_list
*l, *list = NULL; + + ctx =kmod_new(NULL, NULL); + if
(!ctx) { + snprintf(errbuf, BUFLEN, "kmod_new()
failed!"); + goto done; + } + if
(kmod_module_new_from_lookup(ctx, I2C_DEV_MOD_NAME, &list)< 0|| list == NULL) { > + snprintf(errbuf, BUFLEN, I2C_DEV_MOD_NAME "Quoted 1 line
module lookup
failed");
Quoted 5 lines
+ goto
ctx_unref; + } + + flags |=KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY; +kmod_list_foreach(l, list){ + structkmod_module
*mod =kmod_module_get_module(l); + err =
kmod_module_probe_insert_module(mod, flags, NULL,NULL, NULL, NULL);
Quoted 3 lines
+ if (err ==
-ENOENT) { +
snprintf(errbuf, BUFLEN, +
"unknown symbol in module \"%s\",or unknown parameter (see dmesg)",
Quoted 5 lines
+
kmod_module_get_name(mod)); + } else if (err < 0)
{ + snprintf(errbuf, BUFLEN, "(module %s):
%s", +kmod_module_get_name(mod),strerror(-err));
Quoted 7 lines
+ } else { +
kmod_module_unref(mod); + ++loaded; +
break; + } +kmod_module_unref(mod); +
} + +list_unref: +kmod_module_unref_list(list);
+ctx_unref: +kmod_unref(ctx); +#else + err =system("modprobe " I2C_DEV_MOD_NAME); + if (err < 0) { +
snprintf(errbuf, BUFLEN, "failed to execute modprobecommand");
Quoted 2 lines
+ } else if (err > 0) { + snprintf(errbuf,
BUFLEN, "modprobe command exited withcode %d",
Quoted 12 lines
+ WEXITSTATUS(err)); + } else
{ + ++loaded; + goto done; + }
+#endif + if (errbuf[0]) + fprintf(stderr,
"Failed to load required "I2C_DEV_MOD_NAME+
" kernel module: %s\n", errbuf); +done: + return loaded;
+} + static enum adti2c_get_funcs(int i2cbus){ unsigned long funcs; @@ -206,8 +276,12 @@ structi2c_adap*gather_i2c_busses(void)i2c-dev and what we really care about are the i2c-dev numbers. Unfortunately the names are harder to get in i2c-dev */strcat(sysfs, "/class/i2c-dev"); -
if(!(dir = opendir(sysfs))) - goto done; +
if(!(dir = opendir(sysfs))) { + if
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
(!try_load_i2c_dev_mod()) + goto done; +
if ((!(dir = opendir(sysfs)))) + goto
done; + } /* go through the busses */ while ((de =
readdir(dir)) != NULL) { if (!strcmp(de->d_name, ".")) Index:i2c-tools-3.1.0/tools/Module.mk
===================================================================
- --- i2c-tools-3.1.0.orig/tools/Module.mk
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+++
i2c-tools-3.1.0/tools/Module.mk@@ -15,6 +15,11 @@TOOLS_CFLAGS:= -Wstrict-prototypes -Wsh
TOOLS_TARGETS:= i2cdetect i2cdump i2cset i2cget
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
+ifeq ($(shell pkg-config --exists libkmod && echo 1), 1) +
TOOLS_CFLAGS+= $(shell pkg-config --cflags libkmod)
-DUSE_LIBKMOD + LDFLAGS += $(shell pkg-config --libs
libkmod) +endif + # # Programs #
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQEcBAEBAgAGBQJTx4/fAAoJEPezUylxRFUDtDAIAKffFl7jF2CfBOuDLazAnks7 laBW63KrI+j7Qk1VAiIHzu9eu7B4bbPwJ8PC9d3yUKvRmFnVOu4VefZ/Xm2cQQyo 3dqzuMjZX2zlmM3gYpSBH0Ri+BJq2iEme/JfZm+IoaieQCxhNd0Dc6SxFiKfHUrK G2pV0Nw5o/mmbXumXHOhmj5Hi1/7YM1uT9O0qNo6AtX0XDk+Jd4+6z0fAjkjG6i4 ozepg/8jrFLK208yLQoSbXcjg5G8L3ef72zibc6QRZJcoz4N/vA2SsQ3airMoOM0 CubyVrDuYZ3hbiayleu3Cs5nnH82jPUDMV18jtUYMvddAtoXCCZU/nuVg/H32MA= =2aNn
-----END PGP SIGNATURE-----
tools/i2cbusses.c +156 -22
@@ -38,10 +38,17 @@#include <dirent.h> #include <fcntl.h> #include <errno.h> +#ifdef USE_LIBKMOD +#include <libkmod.h> +#endif #include "i2cbusses.h" #include <linux/i2c.h> #include <linux/i2c-dev.h> +#define BUFLEN (NAME_MAX > 512 ? NAME_MAX : 512) +#define I2C_DEV_MOD_NAME "i2c_dev" +#define I2C_DEV_SUBPATH "/class/i2c-dev" +enum adt { adt_dummy, adt_isa, adt_i2c, adt_smbus, adt_unknown };struct adap_type {
@@ -62,6 +69,142 @@ static struct adap_type adap_types[5] = .algo = "N/A", }, }; +/* Get and cache sysfs mount point. */ +static const char *get_sysfs_mount_point (void ) { +static const char *mp =NULL ; + +if (mp ==NULL ) { +char sysfs[NAME_MAX ]; +char fstype[NAME_MAX ]; +char line[BUFLEN ]; +FILE *f; + +if ((f =fopen ("/proc/mounts" ,"r" )) ==NULL ) { +perror ("failed to read /proc/mounts: " ); + goto done; + } +while (fgets (line,BUFLEN , f)) { +sscanf (line,"%*[^ ] %[^ ] %[^ ] %*s\n" , sysfs, fstype); +if (strcasecmp (fstype,"sysfs" ) ==0 ) { + mp =strdup (sysfs); +if (mp ==NULL ) +perror ("strdup: " ); +break ; + } + } +fclose (f); + } +done: +return mp; +} + +/* Get an absolute path of i2c device directory. Free the result when not +* needed. */ +static char *get_i2c_dev_path (void ) { +char *path =NULL ; +int mplen, splen; +const char *mp =get_sysfs_mount_point (); + +if (mp !=NULL ) { + mplen =strlen (mp); + splen =strlen (I2C_DEV_SUBPATH ); + path =malloc (mplen + splen +1 ); +if (path ==NULL ) { +perror ("malloc: " ); + }else { +strncpy (path, mp, mplen); +strncpy (path + mplen,I2C_DEV_SUBPATH , splen); + path[mplen+splen] ='\0' ; + } + } +return path; +} + +/** +* Try to load i2c_dev kernel mode. Do nothing if module is already loaded. +* Returns 1 on success, 0 otherwise. +*/ +static int try_load_i2c_dev_mod (void ) { +int err =0 , loaded =0 ; +char errbuf[BUFLEN ] = {0 }; +#ifdef USE_LIBKMOD +int flags =0 ; +struct kmod_ctx *ctx; +struct kmod_list *l, *list =NULL ; + + ctx =kmod_new (NULL ,NULL ); +if (!ctx) { +snprintf (errbuf,BUFLEN ,"kmod_new() failed!" ); + goto done; + } +if (kmod_module_new_from_lookup (ctx,I2C_DEV_MOD_NAME , &list) <0 || list ==NULL ) { +snprintf (errbuf,BUFLEN ,I2C_DEV_MOD_NAME " module lookup failed" ); + goto ctx_unref; + } + + flags |=KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY ; + kmod_list_foreach(l, list) { +struct kmod_module *mod =kmod_module_get_module (l); + err =kmod_module_probe_insert_module (mod, flags,NULL ,NULL ,NULL ,NULL ); +if (err == -ENOENT ) { +snprintf (errbuf,BUFLEN , +"unknown symbol in module \"%s\", or unknown parameter (see dmesg)" , +kmod_module_get_name (mod)); + }else if (err <0 ) { +snprintf (errbuf,BUFLEN ,"(module %s): %s" , +kmod_module_get_name (mod),strerror (-err)); + }else { +kmod_module_unref (mod); + ++loaded; +break ; + } +kmod_module_unref (mod); + } + +kmod_module_unref_list (list); +ctx_unref: +kmod_unref (ctx); +#else /* Try to load the module with modprobe. */ +struct stat st; +char *dev_path =get_i2c_dev_path (); + +/* First check whether the module is already loaded or built-in. */ +if (dev_path ==NULL ) + goto done; + err =stat (dev_path, &st); +if (err <0 ) { +if (errno !=ENOENT ) { +snprintf (errbuf,BUFLEN ,"can not stat \"%s\": %s" , dev_path, +strerror (errno)); + goto err; + }else { + err =0 ; + } + }else { + ++loaded; + goto done; + } + + err =system ("modprobe " I2C_DEV_MOD_NAME ); +if (err <0 ) { +snprintf (errbuf,BUFLEN ,"failed to execute modprobe command" ); + }else if (err >0 ) { +snprintf (errbuf,BUFLEN ,"modprobe command exited with code %d" , +WEXITSTATUS (err)); + }else { + ++loaded; + goto done; + } + +err: +#endif +if (errbuf[0 ]) +fprintf (stderr,"Failed to load required " I2C_DEV_MOD_NAME +" kernel module: %s\n" , errbuf); +done: +return loaded; +} +static enum adt i2c_get_funcs (int i2cbus) {unsigned long funcs;
@@ -134,8 +277,7 @@ struct i2c_adap *gather_i2c_busses(void)struct dirent *de, *dde;DIR *dir, *ddir;FILE *f; -char fstype[NAME_MAX ], sysfs[NAME_MAX ], n[NAME_MAX ]; -int foundsysfs =0 ; +char *sysfs =NULL , n[NAME_MAX ];int count=0 ;struct i2c_adap *adapters;
@@ -187,29 +329,19 @@ struct i2c_adap *gather_i2c_busses(void) goto done; } -/* look in sysfs */ -/* First figure out where sysfs was mounted */ -if ((f =fopen ("/proc/mounts" ,"r" )) ==NULL ) { + sysfs =get_i2c_dev_path (); +if (sysfs ==NULL ) goto done; - } -while (fgets (n,NAME_MAX , f)) { -sscanf (n,"%*[^ ] %[^ ] %[^ ] %*s\n" , sysfs, fstype); -if (strcasecmp (fstype,"sysfs" ) ==0 ) { - foundsysfs++; -break ; - } - } -fclose (f); -if (! foundsysfs) { - goto done; - }/* Bus numbers in i2c-adapter don't necessarily match those in i2c-dev and what we really care about are the i2c-dev numbers. Unfortunately the names are harder to get in i2c-dev */ -strcat (sysfs,"/class/i2c-dev" ); -if (!(dir =opendir (sysfs))) - goto done; +if (!(dir =opendir (sysfs))) { +if (!try_load_i2c_dev_mod ()) + goto done; +if ((!(dir =opendir (sysfs)))) + goto done; + }/* go through the busses */ while ((de =readdir (dir)) !=NULL ) {if (!strcmp (de->d_name,"." ))
@@ -274,14 +406,15 @@ found:/* We need more space */ adapters =more_adapters (adapters, count +1 );if (!adapters) -return NULL ; + goto done; } adapters[count].nr = i2cbus; adapters[count].name =strdup (s);if (adapters[count].name ==NULL ) {free_adapters (adapters); -return NULL ; + adapters =NULL ; + goto done; } adapters[count].funcs = adap_types[type].funcs; adapters[count].algo = adap_types[type].algo;
@@ -291,6 +424,7 @@ found:closedir (dir); done: +free (sysfs);return adapters; }
tools/Module.mk +5 -0
@@ -16,6 +16,11 @@ TOOLS_LDFLAGS := -Llib -li2c TOOLS_TARGETS := i2cdetect i2cdump i2cset i2cget +ifeq ($(shell pkg-config --exists libkmod && echo 1), 1) + TOOLS_CFLAGS += $(shell pkg-config --cflags libkmod) -DUSE_LIBKMOD + LDFLAGS += $(shell pkg-config --libs libkmod) +endif + # # Programs #
Quoted 2 lines
Updated and a bit refactored patch is attached.
Ping Jean if you are still interested in this patch. He is maintaining the i2c-tools. I'll mark it as deferred for now.