[i2c-tools PATCH] tools: i2cbusses: Handle bus names like /dev/i2c-0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



File based tab completion means it's easy to do something like
i2cdump /dev/i2c-0 0x52. Accept this method of specifying the i2c bus
device.

Signed-off-by: Chris Packham <chris.packham@xxxxxxxxxxxxxxxxxxx>
---
 tools/i2cbusses.c | 12 ++++++++++--
 tools/i2cbusses.h |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/i2cbusses.c b/tools/i2cbusses.c
index b4f00ae..5cb6e93 100644
--- a/tools/i2cbusses.c
+++ b/tools/i2cbusses.c
@@ -25,6 +25,7 @@
 /* For strdup and snprintf */
 #define _BSD_SOURCE 1 /* for glibc <= 2.19 */
 #define _DEFAULT_SOURCE 1 /* for glibc >= 2.19 */
+#define _GNU_SOURCE 1 /* for asprintf */
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -104,8 +105,10 @@ void free_adapters(struct i2c_adap *adapters)
 {
 	int i;
 
-	for (i = 0; adapters[i].name; i++)
+	for (i = 0; adapters[i].name; i++) {
 		free(adapters[i].name);
+		free(adapters[i].devname);
+	}
 	free(adapters);
 }
 
@@ -306,6 +309,10 @@ found:
 				free_adapters(adapters);
 				return NULL;
 			}
+			if (asprintf(&adapters[count].devname, "/dev/i2c-%d", i2cbus) < 0) {
+				free_adapters(adapters);
+				return NULL;
+			}
 			adapters[count].funcs = adap_types[type].funcs;
 			adapters[count].algo = adap_types[type].algo;
 			count++;
@@ -331,7 +338,8 @@ static int lookup_i2c_bus_by_name(const char *bus_name)
 	/* Walk the list of i2c busses, looking for the one with the
 	   right name */
 	for (i = 0; adapters[i].name; i++) {
-		if (strcmp(adapters[i].name, bus_name) == 0) {
+		if (strcmp(adapters[i].name, bus_name) == 0 ||
+		    strcmp(adapters[i].devname, bus_name) == 0) {
 			if (i2cbus >= 0) {
 				fprintf(stderr,
 					"Error: I2C bus name is not unique!\n");
diff --git a/tools/i2cbusses.h b/tools/i2cbusses.h
index a192c7f..77e1b8e 100644
--- a/tools/i2cbusses.h
+++ b/tools/i2cbusses.h
@@ -27,6 +27,7 @@
 struct i2c_adap {
 	int nr;
 	char *name;
+	char *devname;
 	const char *funcs;
 	const char *algo;
 };
-- 
2.31.1




[Index of Archives]     [Linux GPIO]     [Linux SPI]     [Linux Hardward Monitoring]     [LM Sensors]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux