From: Chris Packham <chris.packham@alliedtelesis.co.nz>
To: jdelvare@suse.de, wsa@kernel.org
Cc: linux-i2c@vger.kernel.org,
Chris Packham <chris.packham@alliedtelesis.co.nz>
Subject: [i2c-tools PATCH] tools: i2cbusses: Handle bus names like /dev/i2c-0
Date: Tue, 25 May 2021 21:06:12 +1200 [thread overview]
Message-ID: <20210525090612.26157-1-chris.packham@alliedtelesis.co.nz> (raw)
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@alliedtelesis.co.nz>
---
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
next reply other threads:[~2021-05-25 9:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-25 9:06 Chris Packham [this message]
2021-05-26 7:39 ` [i2c-tools PATCH] tools: i2cbusses: Handle bus names like /dev/i2c-0 Jean Delvare
2021-05-26 21:23 ` Chris Packham
2021-06-02 7:25 ` Jean Delvare
2021-06-03 22:57 ` Chris Packham
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210525090612.26157-1-chris.packham@alliedtelesis.co.nz \
--to=chris.packham@alliedtelesis.co.nz \
--cc=jdelvare@suse.de \
--cc=linux-i2c@vger.kernel.org \
--cc=wsa@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).