Baruch Siach | 17 Dec 23:27 2014
Picon

[PATCH] nanddump: don't show --bb in usage when disabled

The --bb options now depends on LONG_OPTS. Omit mentions of --bb from usage
text when LONG_OPTS is disabled.

Signed-off-by: Baruch Siach <baruch <at> tkos.co.il>
---
 miscutils/nandwrite.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/miscutils/nandwrite.c b/miscutils/nandwrite.c
index c825fc31519d..7b9c8daf541d 100644
--- a/miscutils/nandwrite.c
+++ b/miscutils/nandwrite.c
 <at>  <at>  -36,16 +36,18  <at>  <at> 
 //usage:     "\n	-s ADDR	Start address"

 //usage:#define nanddump_trivial_usage
-//usage:	"[-o] [--bb=padbad|skipbad] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
+//usage:	"[-o] " IF_LONG_OPTS("[--bb=padbad|skipbad] ") "[-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
 //usage:#define nanddump_full_usage "\n\n"
 //usage:	"Dump MTD_DEVICE\n"
 //usage:     "\n	-o	Dump oob data"
 //usage:     "\n	-s ADDR	Start address"
 //usage:     "\n	-l LEN	Length"
 //usage:     "\n	-f FILE	Dump to file ('-' for stdout)"
+//usage:     IF_LONG_OPTS(
 //usage:     "\n	--bb=METHOD:"
 //usage:     "\n		skipbad: skip bad blocks"
 //usage:     "\n		padbad: substitute bad blocks by 0xff (default)"
+//usage:     )

(Continue reading)

Bartosz Golaszewski | 17 Dec 17:58 2014
Picon

[PATCH] Makefile: fix cscope target

This target doesn't work with current directory layout. Just make cscope
index all .c and .h files.

While we're at it: add cscope output files to .gitignore.

Signed-off-by: Bartosz Golaszewski <bartekgola <at> gmail.com>
---
 .gitignore |  8 ++++++++
 Makefile   | 19 +------------------
 2 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/.gitignore b/.gitignore
index 73e88fb..be1d461 100644
--- a/.gitignore
+++ b/.gitignore
 <at>  <at>  -42,3 +42,11  <at>  <at>  core
 /busybox.links
 /runtest-tempdir-links
 /testsuite/echo-ne
+
+#
+# cscope output
+#
+cscope.files
+cscope.in.out
+cscope.out
+cscope.po.out
diff --git a/Makefile b/Makefile
index 778a02f..8c125d2 100644
--- a/Makefile
(Continue reading)

Guillermo Rodriguez | 17 Dec 11:53 2014
Picon

[PATCH] free: Add cached memory information

Hello all,

This is a small patch adding cached memory information to the free
command, just like the procps version. This information is parsed
from /proc/meminfo (see the parse_cached_kb function).

Without this, the amount of free memory is very misleading.

Any comments welcome.

Guillermo Rodriguez

---
 procps/free.c |   51 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/procps/free.c b/procps/free.c
index 47f2fc3..0991713 100644
--- a/procps/free.c
+++ b/procps/free.c
 <at>  <at>  -44,11 +44,27  <at>  <at>  static unsigned long long scale(unsigned long d)
 	return ((unsigned long long)d * G.mem_unit) >> G_unit_steps;
 }

+static unsigned long parse_cached_kb(void)
+{
+	char buf[60]; /* actual lines we expect are ~30 chars or less */
+	FILE *f;
+	unsigned long cached = 0;
+
(Continue reading)

Bartosz Golaszewski | 16 Dec 11:21 2014
Picon

[RFC][PATCH v3 0/2] new applets: i2c-tools

This is the last RFC before the real patch.

Any suggestions, testing and bug reports are appreciated.

v2:
http://lists.busybox.net/pipermail/busybox/2014-December/082086.html

Bartosz Golaszewski (2):
  fdisk: move read_line() to libbb
  new applets: i2c-tools

 include/libbb.h       |    2 +
 libbb/lineedit.c      |   30 ++
 miscutils/i2c_tools.c | 1052 +++++++++++++++++++++++++++++++++++++++++++++++++
 util-linux/fdisk.c    |   23 +-
 4 files changed, 1086 insertions(+), 21 deletions(-)
 create mode 100644 miscutils/i2c_tools.c

--

-- 
2.1.3
shengyong | 15 Dec 07:55 2014

problem of memory leak in re-direction (shell/ash.c: expredir)

hi, all
I meet the memory leak problem when use re-direction in a reading-loop, like:

	while true
	do
		while true
		do
			break
		done < /dev/null
	done

There is some discussion on this issue:
* http://lists.busybox.net/pipermail/busybox/2012-December/078738.html
* https://bugs.busybox.net/show_bug.cgi?id=5822

But it is still not fixed in the later versions?

thanks & regards
Ricardo W | 13 Dec 18:53 2014

★ Busybox, Ricardo W te mandou uma mensagem

Ricardo W te mandou uma mensagem
 
 
Responda agora mesmo! Experimente o serviço de mensagens exclusivas do Badoo.




E-mail enviado por Badoo Trading Limited. Se não quer mais receber e-mails do Badoo, por favor clique aqui para sair.
Badoo Trading Limited é uma sociedade limitada registrada na Inglaterra e País de Gales sob o CRN ​​7540255 e firma registrada em 131 - 151 Great Titchfield Street, Londres, W1W 5BB.

_______________________________________________
busybox mailing list
busybox <at> busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
Ron Yorston | 12 Dec 09:29 2014
Picon

[PATCH] lineedit: don't block when looking for escape sequence in vi-mode

When in vi-mode lineedit tries to detect some escape sequences.
After an escape it reads the next character to check for certain
values.  This read should have a timeout or a user-entered ESC to
switch to command mode doesn't properly handle the next character.

Signed-off-by: Ron Yorston <rmy <at> tigress.co.uk>
---
 libbb/lineedit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 3961b1d..720a495 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
 <at>  <at>  -2611,7 +2611,7  <at>  <at>  int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman
 			 * standard readline bindings (IOW: bash) do.
 			 * Often, Alt-<key> generates ESC-<key>.
 			 */
-			ic = lineedit_read_key(read_key_buffer, timeout);
+			ic = lineedit_read_key(read_key_buffer, 50);
 			switch (ic) {
 				//case KEYCODE_LEFT: - bash doesn't do this
 				case 'b':
--

-- 
1.9.3
Bartosz Golaszewski | 11 Dec 18:01 2014
Picon

[PATCH] mktemp: don't use mktemp() function

The linker emits this warning:

  warning: the use of `mktemp' is dangerous, better use `mkstemp' or `mkdtemp'

Fix it by using mkstemp() instead of mktemp().

function                                             old     new   delta
mktemp_main                                          214     233     +19
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 19/0)               Total: 19 bytes

Signed-off-by: Bartosz Golaszewski <bartekgola <at> gmail.com>
---
 debianutils/mktemp.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c
index 983d7a2..5ef557a 100644
--- a/debianutils/mktemp.c
+++ b/debianutils/mktemp.c
 <at>  <at>  -59,6 +59,8  <at>  <at>  int mktemp_main(int argc UNUSED_PARAM, char **argv)
 	const char *path;
 	char *chp;
 	unsigned opts;
+	int fd;
+
 	enum {
 		OPT_d = 1 << 0,
 		OPT_q = 1 << 1,
 <at>  <at>  -93,8 +95,9  <at>  <at>  int mktemp_main(int argc UNUSED_PARAM, char **argv)
 		chp = concat_path_file(path, chp);

 	if (opts & OPT_u) {
-		chp = mktemp(chp);
-		if (chp[0] == '\0')
+		close(fd = mkstemp(chp));
+		unlink(chp);
+		if (fd < 0)
 			goto error;
 	} else if (opts & OPT_d) {
 		if (mkdtemp(chp) == NULL)
--

-- 
2.1.3
Bartosz Golaszewski | 11 Dec 16:36 2014
Picon

[RFC][PATCHv2] new applets: i2c-tools

This version includes fixes suggested for v1 and implements i2cset. Any
testing and suggestions for size reduction are welcome.

Next: i2cdump and i2cdetect.

v1:
http://lists.busybox.net/pipermail/busybox/2014-December/082071.html

Signed-off-by: Bartosz Golaszewski <bartekgola <at> gmail.com>
---
 miscutils/i2c_tools.c | 727 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 727 insertions(+)
 create mode 100644 miscutils/i2c_tools.c

diff --git a/miscutils/i2c_tools.c b/miscutils/i2c_tools.c
new file mode 100644
index 0000000..ad04e0d
--- /dev/null
+++ b/miscutils/i2c_tools.c
 <at>  <at>  -0,0 +1,727  <at>  <at> 
+/* vi: set sw=4 ts=4: */
+/*
+ * Minimal i2c-tools implementation for busybox.
+ * Parts of code ported from i2c-tools:
+ * 		http://www.lm-sensors.org/wiki/I2CTools.
+ *
+ * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola <at> gmail.com>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//config:config I2CGET
+//config:	bool "i2cget"
+//config:	default y
+//config:	select PLATFORM_LINUX
+//config:	help
+//config:	  Read from I2C/SMBus chip registers.
+//config:
+//config:config I2CSET
+//config:	bool "i2cset"
+//config:	default y
+//config:	select PLATFORM_LINUX
+//config:	help
+//config:	  Set I2C registers.
+//config:
+
+//applet:IF_I2CGET(APPLET(i2cget, BB_DIR_USR_SBIN, BB_SUID_DROP))
+//applet:IF_I2CSET(APPLET(i2cset, BB_DIR_USR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_I2CGET) += i2c_tools.o
+//kbuild:lib-$(CONFIG_I2CSET) += i2c_tools.o
+
+//usage:#define i2cget_trivial_usage
+//usage:       "[-f] [-y] BUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]"
+//usage:#define i2cget_full_usage "\n\n"
+//usage:       "Read from I2C/SMBus chip registers\n"
+//usage:     "\n  I2CBUS is an i2c bus number"
+//usage:     "\n  ADDRESS is an integer (0x03 - 0x77)"
+//usage:     "\n  MODE is one of:"
+//usage:     "\n    b (read byte data, default)"
+//usage:     "\n    w (read word data)"
+//usage:     "\n    c (write byte/read byte)"
+//usage:   "\n\n  Append p for SMBus PEC"
+//usage:   "\n\n  -f - force access to device"
+//usage:     "\n  -y - disable interactive mode"
+//usage:
+//usage:#define i2cset_trivial_usage
+//usage:       "[-f] [-y] [-m MASK] BUS CHIP-ADDR DATA-ADDR [VALUE] ... [MODE]"
+//usage:#define i2cset_full_usage "\n\n"
+//usage:       "Set I2C registers\n"
+//usage:     "\n  I2CBUS is an i2c bus number"
+//usage:     "\n  ADDRESS is an integer (0x03 - 0x77)"
+//usage:     "\n  MODE is one of:"
+//usage:     "\n    c (byte, no value)"
+//usage:     "\n    b (byte data, default)"
+//usage:     "\n    w (word data)"
+//usage:     "\n    i (I2C block data)"
+//usage:     "\n    s (SMBus block data)"
+//usage:   "\n\n  Append p for SMBus PEC"
+//usage:   "\n\n  -f - force access to device"
+//usage:     "\n  -y - disable interactive mode"
+//usage:     "\n  -r - read back and compare the result"
+//usage:     "\n  -m - mask specifying which bits to write"
+//usage:
+
+#include "libbb.h"
+
+/*
+ * /dev/i2c-X ioctl commands. The ioctl's parameter is always an unsigned long,
+ * except for:
+ *    - I2C_FUNCS, takes pointer to an unsigned long
+ *    - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data
+ *    - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data
+ */
+
+/* Number of times a device address should be polled when not acknowledging */
+#define I2C_RETRIES	0x0701
+/* Set timeout in units of 10 ms */
+#define I2C_TIMEOUT	0x0702
+
+/*
+ * NOTE: Slave address is 7 or 10 bits, but 10-bit addresses
+ * are NOT supported! (due to code brokenness)
+ */
+
+/* Use this slave address */
+#define I2C_SLAVE	0x0703
+/* Use this slave address, even if it is already in use by a driver! */
+#define I2C_SLAVE_FORCE	0x0706
+/* 0 for 7 bit addrs, != 0 for 10 bit */
+#define I2C_TENBIT	0x0704
+/* Get the adapter functionality mask */
+#define I2C_FUNCS	0x0705
+/* Combined R/W transfer (one STOP only) */
+#define I2C_RDWR	0x0707
+/* != 0 to use PEC with SMBus */
+#define I2C_PEC		0x0708
+/* SMBus transfer */
+#define I2C_SMBUS	0x0720
+
+/* This is the structure as used in the I2C_SMBUS ioctl call */
+struct i2c_smbus_ioctl_data {
+	uint8_t read_write;
+	uint8_t command;
+	uint32_t size;
+	union i2c_smbus_data *data;
+};
+
+/* This is the structure as used in the I2C_RDWR ioctl call */
+struct i2c_rdwr_ioctl_data {
+	struct i2c_msg *msgs;	/* pointers to i2c_msgs */
+	uint32_t nmsgs;			/* number of i2c_msgs */
+};
+
+/* As specified in SMBus standard */
+#define I2C_SMBUS_BLOCK_MAX		32
+/* Not specified but we use same structure */
+#define I2C_SMBUS_I2C_BLOCK_MAX		32
+
+/*
+ * Data for SMBus Messages
+ */
+union i2c_smbus_data {
+	uint8_t byte;
+	uint16_t word;
+	/* block[0] is used for length and one more for PEC */
+	uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
+};
+
+#define  I2C_RDRW_IOCTL_MAX_MSGS	42
+
+/* smbus_access read or write markers */
+#define I2C_SMBUS_READ	1
+#define I2C_SMBUS_WRITE	0
+
+/* SMBus transaction types (size parameter in the below functions). */
+#define I2C_SMBUS_QUICK			0
+#define I2C_SMBUS_BYTE			1
+#define I2C_SMBUS_BYTE_DATA		2
+#define I2C_SMBUS_WORD_DATA		3
+#define I2C_SMBUS_PROC_CALL		4
+#define I2C_SMBUS_BLOCK_DATA		5
+#define I2C_SMBUS_I2C_BLOCK_BROKEN	6
+#define I2C_SMBUS_BLOCK_PROC_CALL	7
+#define I2C_SMBUS_I2C_BLOCK_DATA	8
+
+/* Defines to determine what functionality is present */
+#define I2C_FUNC_I2C				0x00000001
+#define I2C_FUNC_10BIT_ADDR			0x00000002
+#define I2C_FUNC_PROTOCOL_MANGLING		0x00000004
+#define I2C_FUNC_SMBUS_PEC			0x00000008
+#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL		0x00008000
+#define I2C_FUNC_SMBUS_QUICK			0x00010000
+#define I2C_FUNC_SMBUS_READ_BYTE		0x00020000
+#define I2C_FUNC_SMBUS_WRITE_BYTE		0x00040000
+#define I2C_FUNC_SMBUS_READ_BYTE_DATA		0x00080000
+#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA		0x00100000
+#define I2C_FUNC_SMBUS_READ_WORD_DATA		0x00200000
+#define I2C_FUNC_SMBUS_WRITE_WORD_DATA		0x00400000
+#define I2C_FUNC_SMBUS_PROC_CALL		0x00800000
+#define I2C_FUNC_SMBUS_READ_BLOCK_DATA		0x01000000
+#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 	0x02000000
+#define I2C_FUNC_SMBUS_READ_I2C_BLOCK		0x04000000
+#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK		0x08000000
+
+/*
+ * This is needed for ioctl_or_perror_and_die() since it only accepts pointer.
+ */
+#define INT_TO_PTR(i) ((void*)((long)i))
+
+static int32_t i2c_smbus_access(int fd, char read_write, uint8_t cmd,
+				int size, union i2c_smbus_data *data)
+{
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = read_write;
+	args.command = cmd;
+	args.size = size;
+	args.data = data;
+
+	return ioctl(fd, I2C_SMBUS, &args);
+}
+
+static int32_t i2c_smbus_read_byte(int fd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
+	if (err < 0)
+		return err;
+
+	return 0xff & data.byte;
+}
+
+static int32_t i2c_smbus_write_byte(int fd, uint8_t val)
+{
+	return i2c_smbus_access(fd, I2C_SMBUS_WRITE,
+				val, I2C_SMBUS_BYTE, NULL);
+}
+
+static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
+			       I2C_SMBUS_BYTE_DATA, &data);
+	if (err < 0)
+		return err;
+
+	return 0xff & data.byte;
+}
+
+static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
+			       I2C_SMBUS_WORD_DATA, &data);
+	if (err < 0)
+		return err;
+
+	return 0xffff & data.word;
+}
+
+#if ENABLE_I2CSET
+static int32_t i2c_smbus_write_byte_data(int file,
+					 uint8_t cmd, uint8_t value)
+{
+	union i2c_smbus_data data;
+
+	data.byte = value;
+
+	return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
+				I2C_SMBUS_BYTE_DATA, &data);
+}
+
+static int32_t i2c_smbus_write_word_data(int file, uint8_t cmd, uint16_t value)
+{
+	union i2c_smbus_data data;
+
+	data.word = value;
+
+	return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
+				I2C_SMBUS_WORD_DATA, &data);
+}
+
+static int32_t i2c_smbus_write_block_data(int file, uint8_t cmd,
+				   uint8_t length, const uint8_t *values)
+{
+	union i2c_smbus_data data;
+	int i;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+	for (i = 1; i <= length; i++)
+		data.block[i] = values[i-1];
+	data.block[0] = length;
+
+	return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
+				I2C_SMBUS_BLOCK_DATA, &data);
+}
+
+static int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t cmd,
+				       uint8_t length, const uint8_t *values)
+{
+	union i2c_smbus_data data;
+	int i;
+
+	if (length > I2C_SMBUS_BLOCK_MAX)
+		length = I2C_SMBUS_BLOCK_MAX;
+	for (i = 1; i <= length; i++)
+		data.block[i] = values[i-1];
+	data.block[0] = length;
+
+	return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
+				I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
+}
+#endif /* ENABLE_I2CSET */
+
+static int i2c_bus_lookup(const char *bus_str)
+{
+	return xstrtoi_range(bus_str, 10, 0, 0xfffff);
+}
+
+static int i2c_parse_bus_addr(const char *addr_str)
+{
+	return xstrtoi_range(addr_str, 16, 0x03, 0x77);
+}
+
+static int i2c_parse_data_addr(const char *data_addr)
+{
+	return xstrtoi_range(data_addr, 16, 0, 0xff);
+}
+
+/* Opens the device file associated with given i2c bus. */
+static int i2c_dev_open(int i2cbus)
+{
+	char filename[32];
+	int fd;
+
+	snprintf(filename, sizeof(filename), "/dev/i2c-%d", i2cbus);
+	fd = open_or_warn(filename, O_RDWR);
+	if (fd < 0 && errno == ENOENT) {
+		snprintf(filename, sizeof(filename), "/dev/i2c/%d", i2cbus);
+		fd = xopen(filename, O_RDWR);
+	}
+
+	return fd;
+}
+
+static void i2c_set_slave_addr(int fd, int addr, int force)
+{
+	ioctl_or_perror_and_die(fd, force ? I2C_SLAVE_FORCE : I2C_SLAVE,
+				INT_TO_PTR(addr),
+				"Could not set address to 0x%02x", addr);
+}
+
+static void i2c_set_pec(int fd, int pec)
+{
+	ioctl_or_perror_and_die(fd, I2C_PEC,
+				INT_TO_PTR(pec ? 1 : 0),
+				"Could not set PEC");
+}
+
+/* Size reducing helpers for xxx_check_funcs(). */
+static void get_funcs_matrix(int fd, unsigned long *funcs)
+{
+	ioctl_or_perror_and_die(fd, I2C_FUNCS, funcs,
+			"Could not get the adapter functionality matrix");
+}
+
+static void check_funcs_test_end(int funcs, int pec, const char *err)
+{
+	if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
+		fprintf(stderr,
+			"Warning: Adapter does not seem to support PEC\n");
+
+	if (err)
+		bb_error_msg_and_die("Adapter does not have %s capability",
+				     err);
+}
+
+/*
+ * The below functions emit an error message and exit if the adapter doesn't
+ * support desired functionalities.
+ */
+#if ENABLE_I2CGET
+static void i2cget_check_funcs(int fd, int mode, int data_addr, int pec)
+{
+	unsigned long funcs;
+	const char *err = NULL;
+
+	get_funcs_matrix(fd, &funcs);
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
+			err = "SMBus receive byte";
+			break;
+		}
+		if (data_addr >= 0 && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
+			err = "SMBus send byte";
+		break;
+	case I2C_SMBUS_BYTE_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
+			err = "SMBus read byte";
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA))
+			err = "SMBus read word";
+		break;
+	}
+	check_funcs_test_end(funcs, pec, err);
+}
+#endif /* ENABLE_I2CGET */
+
+#if ENABLE_I2CSET
+static void i2cset_check_funcs(int fd, int mode, int pec)
+{
+	unsigned long funcs;
+	const char *err = NULL;
+
+	get_funcs_matrix(fd, &funcs);
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
+			err = "SMBus send byte";
+		break;
+
+	case I2C_SMBUS_BYTE_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
+			err = "SMBus write byte";
+		break;
+
+	case I2C_SMBUS_WORD_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA))
+			err = "SMBus write word";
+		break;
+
+	case I2C_SMBUS_BLOCK_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
+			err = "SMBus block write";
+		break;
+	case I2C_SMBUS_I2C_BLOCK_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
+			err = "I2C block write";
+		break;
+	}
+	check_funcs_test_end(funcs, pec, err);
+}
+#endif /* ENABLE_I2CSET */
+
+/*********************************** XXXX ***********************************/
+/*
+ * XXX read_line() exists already in util-linux/fdisk.c - move it to
+ * libbb and reuse here in the final version of the patch. This will save
+ * additional 116 bytes.
+ */
+static char *line_ptr;
+static char line_buffer[128];
+
+static int read_line(const char *prompt)
+{
+	int sz;
+
+	sz = read_line_input(NULL, prompt, line_buffer,
+			     sizeof(line_buffer), /*timeout*/ -1);
+	if (sz <= 0)
+		exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */
+
+	if (line_buffer[sz-1] == '\n')
+		line_buffer[--sz] = '\0';
+
+	line_ptr = line_buffer;
+	while (*line_ptr != '\0' && (unsigned char)*line_ptr <= ' ')
+		line_ptr++;
+	return *line_ptr;
+}
+
+static void user_confirm(void)
+{
+	if (tolower(read_line("Continue? [y/N] ")) != 'y') {
+		fprintf(stderr, "Aborting.\n");
+		xfunc_die();
+	}
+}
+/*********************************** XXXX ***********************************/
+
+/*
+ * Return only if user confirms the action, abort otherwise.
+ *
+ * The messages displayed here are much less elaborate than their i2c-tools
+ * counterparts - this is done for size reduction.
+ */
+static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
+{
+	fprintf(stderr, "WARNING! This program can confuse your I2C bus\n");
+
+	/* Don't let the user break his/her EEPROMs */
+	if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
+		fprintf(stderr, "This is I2C not smbus - using PEC on I2C "
+			"devices may result in data loss. Aborting.\n");
+		xfunc_die();
+	}
+
+	if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec)
+		fprintf(stderr, "WARNING! May interpret a write byte command "
+			"with PEC as a write byte data command!\n");
+
+	if (pec)
+		fprintf(stderr, "PEC checking enabled.\n");
+
+	user_confirm();
+}
+
+#if ENABLE_I2CGET
+int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int i2cget_main(int argc, char **argv)
+{
+	static const unsigned opt_f = (1 << 0), opt_y = (1 << 1);
+	static const char *const optstr = "fy";
+
+	int bus_num, bus_addr, data_addr = -1, status;
+	int mode = I2C_SMBUS_BYTE, pec = 0, fd;
+	unsigned opts;
+
+	opts = getopt32(argv, optstr);
+	argv += optind;
+	argc -= optind;
+
+	if (argc < 2)
+		bb_show_usage();
+
+	bus_num = i2c_bus_lookup(argv[0]);
+	bus_addr = i2c_parse_bus_addr(argv[1]);
+
+	if (argc > 2) {
+		data_addr = i2c_parse_data_addr(argv[2]);
+		mode = I2C_SMBUS_BYTE_DATA;
+	}
+
+	if (argc > 3) {
+		switch (argv[3][0]) {
+		case 'b':	/* Already set */		break;
+		case 'w':	mode = I2C_SMBUS_WORD_DATA;	break;
+		case 'c':	mode = I2C_SMBUS_BYTE;		break;
+		default:
+			bb_error_msg("Invalid mode");
+			bb_show_usage();
+		}
+		pec = argv[3][1] == 'p';
+	}
+
+	fd = i2c_dev_open(bus_num);
+	i2cget_check_funcs(fd, mode, data_addr, pec);
+	i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
+
+	if (!(opts & opt_y))
+		confirm_action(bus_addr, mode, data_addr, pec);
+
+	if (pec)
+		i2c_set_pec(fd, 1);
+
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		if (data_addr >= 0) {
+			status = i2c_smbus_write_byte(fd, data_addr);
+			if (status < 0)
+				fprintf(stderr, "Warning - write failed\n");
+		}
+		status = i2c_smbus_read_byte(fd);
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		status = i2c_smbus_read_word_data(fd, data_addr);
+		break;
+	default: /* I2C_SMBUS_BYTE_DATA */
+		status = i2c_smbus_read_byte_data(fd, data_addr);
+	}
+	close(fd);
+
+	if (status < 0)
+		bb_perror_msg_and_die("Read failed");
+
+	printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
+
+	return 0;
+}
+#endif /* ENABLE_I2CGET */
+
+#if ENABLE_I2CSET
+int i2cset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int i2cset_main(int argc, char **argv)
+{
+	static const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
+			      opt_m = (1 << 2), opt_r = (1 << 3);
+	static const char *const optstr = "fym:r";
+
+	int bus_num, bus_addr, data_addr, mode = I2C_SMBUS_BYTE, pec = 0;
+	int val, blen = 0, mask = 0, fd, status;
+	unsigned char block[I2C_SMBUS_BLOCK_MAX];
+	char *opt_m_arg = NULL;
+	unsigned opts;
+
+	opts = getopt32(argv, optstr, &opt_m_arg);
+	argv += optind;
+	argc -= optind;
+
+	if (argc < 3)
+		bb_show_usage();
+
+	bus_num = i2c_bus_lookup(argv[0]);
+	bus_addr = i2c_parse_bus_addr(argv[1]);
+	data_addr = i2c_parse_data_addr(argv[2]);
+
+	if (argc > 3) {
+		switch (argv[argc-1][0]) {
+		case 'c': mode = I2C_SMBUS_BYTE;		break;
+		case 'b': mode = I2C_SMBUS_BYTE_DATA;		break;
+		case 'w': mode = I2C_SMBUS_WORD_DATA;		break;
+		case 's': mode = I2C_SMBUS_BLOCK_DATA;		break;
+		case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA;	break;
+		default:
+			bb_error_msg("Invalid mode");
+			bb_show_usage();
+		}
+		pec = argv[argc-1][1] == 'p';
+		if (mode == I2C_SMBUS_BLOCK_DATA ||
+				mode == I2C_SMBUS_I2C_BLOCK_DATA) {
+			if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA)
+				bb_error_msg_and_die("PEC not supported for "
+						     "I2C block writes!");
+			if (opts & opt_m)
+				bb_error_msg_and_die("Mask not supported "
+						     "for block writes!\n");
+		}
+	}
+
+	/* Prepare the value(s) to be written according to current mode. */
+	switch (mode) {
+	case I2C_SMBUS_BYTE_DATA:
+		val = xstrtoi_range(argv[3], 0, 0, 0xff);
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		val = xstrtoi_range(argv[3], 0, 0, 0xffff);
+		break;
+	case I2C_SMBUS_BLOCK_DATA:
+	case I2C_SMBUS_I2C_BLOCK_DATA:
+		for (blen = 3; blen < (argc - 1); blen++)
+			block[blen] = xstrtoi_range(argv[blen], 0, 0, 0xff);
+		val = -1;
+		break;
+	default:
+		val = -1;
+		break;
+	}
+
+	if (opts & opt_m) {
+		mask = xstrtoi_range(opt_m_arg, 0, 0,
+				(mode == I2C_SMBUS_BYTE ||
+				 mode == I2C_SMBUS_BYTE_DATA) ? 0xff : 0xffff);
+	}
+
+	fd = i2c_dev_open(bus_num);
+	i2cset_check_funcs(fd, mode, pec);
+	i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
+
+	if (!(opts & opt_y))
+		confirm_action(bus_addr, mode, data_addr, pec);
+
+	/*
+	 * If we're using mask - read the current value here and adjust the
+	 * value to be written.
+	 */
+	if (opts & opt_m) {
+		int tmpval;
+
+		switch (mode) {
+		case I2C_SMBUS_BYTE:
+			tmpval = i2c_smbus_read_byte(fd);
+			break;
+		case I2C_SMBUS_WORD_DATA:
+			tmpval = i2c_smbus_read_word_data(fd, data_addr);
+			break;
+		default:
+			tmpval = i2c_smbus_read_byte_data(fd, data_addr);
+		}
+
+		if (tmpval < 0)
+			bb_perror_msg_and_die("Failed to read old value");
+
+		val = (val & mask) | (tmpval & ~mask);
+
+		if (!(opts & opt_y)) {
+			fprintf(stderr, "Old value 0x%0*x, write mask "
+				"0x%0*x: Will write 0x%0*x to register "
+				"0x%02x\n",
+				mode == I2C_SMBUS_WORD_DATA ? 4 : 2, tmpval,
+				mode == I2C_SMBUS_WORD_DATA ? 4 : 2, mask,
+				mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
+				data_addr);
+
+			user_confirm();
+		}
+	}
+
+	if (pec)
+		i2c_set_pec(fd, 1);
+
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		status = i2c_smbus_write_byte(fd, data_addr);
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		status = i2c_smbus_write_word_data(fd, data_addr, val);
+		break;
+	case I2C_SMBUS_BLOCK_DATA:
+		status = i2c_smbus_write_block_data(fd, data_addr,
+						    blen, block);
+		break;
+	case I2C_SMBUS_I2C_BLOCK_DATA:
+		status = i2c_smbus_write_i2c_block_data(fd, data_addr,
+							blen, block);
+		break;
+	default: /* I2C_SMBUS_BYTE_DATA */
+		status = i2c_smbus_write_byte_data(fd, data_addr, val);
+		break;
+	}
+	if (status < 0)
+		bb_perror_msg_and_die("Write failed\n");
+
+	if (pec)
+		i2c_set_pec(fd, 0); /* Clear PEC */
+
+	/* No readback required - we're done. */
+	if (!(opts & opt_r))
+		return 0;
+
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		status = i2c_smbus_read_byte(fd);
+		val = data_addr;
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		status = i2c_smbus_read_word_data(fd, data_addr);
+		break;
+	default: /* I2C_SMBUS_BYTE_DATA */
+		status = i2c_smbus_read_byte_data(fd, data_addr);
+	}
+
+	if (status < 0) {
+		printf("Warning - readback failed\n");
+	} else
+	if (status != val) {
+		printf("Warning - data mismatch - wrote "
+		       "0x%0*x, read back 0x%0*x\n",
+		       mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
+		       mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
+	} else {
+		printf("Value 0x%0*x written, readback matched\n",
+		       mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val);
+	}
+
+	return 0;
+}
+#endif /* ENABLE_I2CSET */
--

-- 
2.1.3
Bartosz Golaszewski | 10 Dec 13:34 2014
Picon

[PATCH] Config: select PLATFORM_LINUX if using sendfile()

Man entry for sendfile:

  Not specified in POSIX.1-2001, or other standards.

  Other UNIX systems implement sendfile() with different  semantics  and
  prototypes. It should not be used in portable programs.

Select PLATFORM_LINUX if enabling FEATURE_USE_SENDFILE.

Signed-off-by: Bartosz Golaszewski <bartekgola <at> gmail.com>
---
 Config.in | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Config.in b/Config.in
index 285fe0a..07b4bf3 100644
--- a/Config.in
+++ b/Config.in
 <at>  <at>  -267,6 +267,7  <at>  <at>  config PAM
 config FEATURE_USE_SENDFILE
 	bool "Use sendfile system call"
 	default y
+	select PLATFORM_LINUX
 	help
 	  When enabled, busybox will use the kernel sendfile() function
 	  instead of read/write loops to copy data between file descriptors
--

-- 
2.1.3
Bartosz Golaszewski | 9 Dec 15:51 2014
Picon

[RFC] new applets: i2c-tools

Hi,

I've started porting i2c-tools to busybox.

This patch implements i2cget together with necessary library functions - a lot
of this code can be reused in other i2c utils.

Bloatcheck:

function                                             old     new   delta
i2cget_main                                            -    1464   +1464
.rodata                                           138898  139779    +881
packed_usage                                       29706   29825    +119
i2c_smbus_access                                       -      48     +48
run_applet_no_and_exit                               588     596      +8
err_msg_and_usage_and_die                              -       8      +8
applet_names                                        2451    2458      +7
applet_main                                         1428    1432      +4
applet_nameofs                                       714     716      +2
------------------------------------------------------------------------------
(add/remove: 4/0 grow/shrink: 6/0 up/down: 2541/0)           Total: 2541 bytes

I'd like to get some comments before implementing the rest.

Signed-off-by: Bartosz Golaszewski <bartekgola <at> gmail.com>
---
 miscutils/i2c_tools.c | 451 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 451 insertions(+)
 create mode 100644 miscutils/i2c_tools.c

diff --git a/miscutils/i2c_tools.c b/miscutils/i2c_tools.c
new file mode 100644
index 0000000..3036ae6
--- /dev/null
+++ b/miscutils/i2c_tools.c
 <at>  <at>  -0,0 +1,451  <at>  <at> 
+/* vi: set sw=4 ts=4: */
+/*
+ * Minimal i2c-tools implementation for busybox.
+ * Parts of code ported from i2c-tools:
+ * 		http://www.lm-sensors.org/wiki/I2CTools.
+ *
+ * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola <at> gmail.com>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//config:config I2CGET
+//config:	bool "i2cget"
+//config:	default y
+//config:	select PLATFORM_LINUX
+//config:	help
+//config:	  Read from I2C/SMBus chip registers.
+
+//applet:IF_I2CGET(APPLET(i2cget, BB_DIR_USR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_I2CGET) += i2c_tools.o
+
+//usage:#define i2cget_trivial_usage
+//usage:       "[-f] [-y] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]"
+//usage:#define i2cget_full_usage "\n\n"
+//usage:       "Read from I2C/SMBus chip registers\n"
+//usage:     "\n  I2CBUS is an i2c bus number"
+//usage:     "\n  ADDRESS is an integer (0x03 - 0x77)"
+//usage:     "\n  MODE is one of:"
+//usage:     "\n    b (read byte data, default)"
+//usage:     "\n    w (read word data)"
+//usage:     "\n    c (write byte/read byte)"
+//usage:   "\n\n  -f - force access to device"
+//usage:     "\n  -y - disable interactive mode"
+
+#include "libbb.h"
+
+/*
+ * /dev/i2c-X ioctl commands. The ioctl's parameter is always an unsigned long,
+ * except for:
+ *    - I2C_FUNCS, takes pointer to an unsigned long
+ *    - I2C_RDWR, takes pointer to struct i2c_rdwr_ioctl_data
+ *    - I2C_SMBUS, takes pointer to struct i2c_smbus_ioctl_data
+ */
+
+/* Number of times a device address should be polled when not acknowledging */
+#define I2C_RETRIES	0x0701
+/* Set timeout in units of 10 ms */
+#define I2C_TIMEOUT	0x0702
+
+/*
+ * NOTE: Slave address is 7 or 10 bits, but 10-bit addresses
+ * are NOT supported! (due to code brokenness)
+ */
+
+/* Use this slave address */
+#define I2C_SLAVE	0x0703
+/* Use this slave address, even if it is already in use by a driver! */
+#define I2C_SLAVE_FORCE	0x0706
+/* 0 for 7 bit addrs, != 0 for 10 bit */
+#define I2C_TENBIT	0x0704
+/* Get the adapter functionality mask */
+#define I2C_FUNCS	0x0705
+/* Combined R/W transfer (one STOP only) */
+#define I2C_RDWR	0x0707
+/* != 0 to use PEC with SMBus */
+#define I2C_PEC		0x0708
+/* SMBus transfer */
+#define I2C_SMBUS	0x0720
+
+/* This is the structure as used in the I2C_SMBUS ioctl call */
+struct i2c_smbus_ioctl_data {
+	uint8_t read_write;
+	uint8_t command;
+	uint32_t size;
+	union i2c_smbus_data *data;
+};
+
+/* This is the structure as used in the I2C_RDWR ioctl call */
+struct i2c_rdwr_ioctl_data {
+	struct i2c_msg *msgs;	/* pointers to i2c_msgs */
+	uint32_t nmsgs;			/* number of i2c_msgs */
+};
+
+/* As specified in SMBus standard */
+#define I2C_SMBUS_BLOCK_MAX		32
+/* Not specified but we use same structure */
+#define I2C_SMBUS_I2C_BLOCK_MAX		32
+
+/*
+ * Data for SMBus Messages
+ */
+union i2c_smbus_data {
+	uint8_t byte;
+	uint16_t word;
+	/* block[0] is used for length and one more for PEC */
+	uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
+};
+
+#define  I2C_RDRW_IOCTL_MAX_MSGS	42
+
+/* smbus_access read or write markers */
+#define I2C_SMBUS_READ	1
+#define I2C_SMBUS_WRITE	0
+
+/* SMBus transaction types (size parameter in the below functions). */
+#define I2C_SMBUS_QUICK			0
+#define I2C_SMBUS_BYTE			1
+#define I2C_SMBUS_BYTE_DATA		2
+#define I2C_SMBUS_WORD_DATA		3
+#define I2C_SMBUS_PROC_CALL		4
+#define I2C_SMBUS_BLOCK_DATA		5
+#define I2C_SMBUS_I2C_BLOCK_BROKEN	6
+#define I2C_SMBUS_BLOCK_PROC_CALL	7
+#define I2C_SMBUS_I2C_BLOCK_DATA	8
+
+/* Defines to determine what functionality is present */
+#define I2C_FUNC_I2C				0x00000001
+#define I2C_FUNC_10BIT_ADDR			0x00000002
+#define I2C_FUNC_PROTOCOL_MANGLING		0x00000004
+#define I2C_FUNC_SMBUS_PEC			0x00000008
+#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL		0x00008000
+#define I2C_FUNC_SMBUS_QUICK			0x00010000
+#define I2C_FUNC_SMBUS_READ_BYTE		0x00020000
+#define I2C_FUNC_SMBUS_WRITE_BYTE		0x00040000
+#define I2C_FUNC_SMBUS_READ_BYTE_DATA		0x00080000
+#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA		0x00100000
+#define I2C_FUNC_SMBUS_READ_WORD_DATA		0x00200000
+#define I2C_FUNC_SMBUS_WRITE_WORD_DATA		0x00400000
+#define I2C_FUNC_SMBUS_PROC_CALL		0x00800000
+#define I2C_FUNC_SMBUS_READ_BLOCK_DATA		0x01000000
+#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 	0x02000000
+#define I2C_FUNC_SMBUS_READ_I2C_BLOCK		0x04000000
+#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK		0x08000000
+
+/*
+ * This is needed for ioctl_or_perror_and_die() since it only accepts pointer.
+ */
+#define INT_TO_PTR(i) ((void*)((long)i))
+
+static int32_t i2c_smbus_access(int fd, char read_write, uint8_t command,
+				int size, union i2c_smbus_data *data)
+{
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = read_write;
+	args.command = command;
+	args.size = size;
+	args.data = data;
+
+	return ioctl(fd, I2C_SMBUS, &args);
+}
+
+static int32_t i2c_smbus_read_byte(int fd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
+	if (err < 0)
+		return err;
+
+	return 0x0FF & data.byte;
+}
+
+static int32_t i2c_smbus_write_byte(int fd, uint8_t val)
+{
+	return i2c_smbus_access(fd, I2C_SMBUS_WRITE,
+				val, I2C_SMBUS_BYTE, NULL);
+}
+
+static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
+			       I2C_SMBUS_BYTE_DATA, &data);
+	if (err < 0)
+		return err;
+
+	return 0x0FF & data.byte;
+}
+
+static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd)
+{
+	union i2c_smbus_data data;
+	int err;
+
+	err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
+			       I2C_SMBUS_WORD_DATA, &data);
+	if (err < 0)
+		return err;
+
+	return 0x0FFFF & data.word;
+}
+
+static void NORETURN err_msg_and_usage_and_die(const char *msg)
+{
+	bb_error_msg(msg);
+	bb_show_usage();
+}
+
+static int i2c_bus_lookup(const char *bus_str)
+{
+	unsigned long i2cbus;
+	char *end;
+
+	i2cbus = bb_strtoul(bus_str, &end, 10);
+	if (*end || !*bus_str)
+		err_msg_and_usage_and_die("Bus address is not a number");
+	if (i2cbus > 0xfffff)
+		err_msg_and_usage_and_die("Bus address out of range");
+
+	return i2cbus;
+}
+
+static int i2c_parse_addr(const char *addr_str)
+{
+	long addr;
+	char *end;
+
+	addr = bb_strtol(addr_str, &end, 16);
+	if (*end || !*addr_str)
+		err_msg_and_usage_and_die("Chip address is not a number");
+	if (addr < 0x03 || addr > 0x77)
+		err_msg_and_usage_and_die("Chip address out of range");
+
+	return addr;
+}
+
+/*
+ * Opens the device file associated with given i2c bus.
+ *
+ * i2cbus - number of the i2c bus
+ * filename - buffer to store the filename
+ * len - buffer length
+ */
+static int i2c_dev_open(int i2cbus, char *filename, size_t len)
+{
+	int fd;
+
+	snprintf(filename, len, "/dev/i2c-%d", i2cbus);
+
+	fd = open(filename, O_RDWR);
+	if (fd < 0 && errno == ENOENT) {
+		/* Older kernels register the devices files this way. */
+		snprintf(filename, len, "/dev/i2c/%d", i2cbus);
+		fd = open(filename, O_RDWR);
+	}
+
+	if (fd < 0)
+		bb_perror_msg_and_die("Could not open file: "
+				      "/dev/i2c-%d or /dev/i2c/%d",
+				      i2cbus, i2cbus);
+
+	return fd;
+}
+
+static void i2c_set_slave_addr(int fd, int addr, int force)
+{
+	ioctl_or_perror_and_die(fd, force ? I2C_SLAVE_FORCE : I2C_SLAVE,
+				INT_TO_PTR(addr),
+				"Could not set address to 0x%02x", addr);
+}
+
+static void check_funcs(int fd, int mode, int data_addr, int pec)
+{
+	unsigned long funcs;
+	const char *err;
+
+	ioctl_or_perror_and_die(fd, I2C_FUNCS, &funcs,
+			"Could not get the adapter functionality matrix");
+
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
+			err = "SMBus receive byte";
+			goto err_out;
+		}
+		if (data_addr >= 0 && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) {
+			err = "SMBus send byte";
+			goto err_out;
+		}
+		break;
+	case I2C_SMBUS_BYTE_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
+			err = "SMBus read byte";
+			goto err_out;
+		}
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
+			err = "SMBus read word";
+			goto err_out;
+		}
+		break;
+	}
+
+	if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
+		fprintf(stderr,
+			"Warning: Adapter does not seem to support PEC\n");
+
+	return;
+
+err_out:
+	bb_error_msg_and_die("Adapter does not have %s capability", err);
+}
+
+/* Return 1 if we should continue, 0 if we should abort */
+static int user_ack(int def)
+{
+	char s[2];
+	int ret;
+
+	if (!fgets(s, 2, stdin))
+		return 0; /* Nack by default */
+
+	switch (s[0]) {
+	case 'y':
+	case 'Y':
+		ret = 1;
+		break;
+	case 'n':
+	case 'N':
+		ret = 0;
+		break;
+	default:
+		ret = def;
+	}
+
+	/* Flush extra characters */
+	while (s[0] != '\n') {
+		int c = fgetc(stdin);
+		if (c == EOF) {
+			ret = 0;
+			break;
+		}
+		s[0] = c;
+	}
+
+	return ret;
+}
+
+/*
+ * The messages displayed here are much less elaborate than their i2c-tools
+ * counterparts - this is done for size reduction.
+ */
+static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
+{
+	int dont = 0;
+
+	fprintf(stderr, "WARNING! This program can confuse your I2C bus\n");
+
+	/* Don't let the user break his/her EEPROMs */
+	if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
+		fprintf(stderr, "This is I2C not smbus - using PEC on I2C "
+			"devices may result in data loss. Aborting.\n");
+		xfunc_die();
+	}
+
+	if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec) {
+		fprintf(stderr, "WARNING! May interpret a write byte command "
+			"with PEC as a write byte data command!\n");
+		dont++;
+	}
+
+	if (pec)
+		fprintf(stderr, "PEC checking enabled.\n");
+
+	fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n");
+	fflush(stderr);
+	if (!user_ack(!dont)) {
+		fprintf(stderr, "Aborting.\n");
+		xfunc_die();
+	}
+}
+
+int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int i2cget_main(int argc, char **argv)
+{
+	static const unsigned opt_f = (1 << 0), opt_y = (1 << 1);
+	static const char *const optstr = "fy";
+
+	unsigned opts;
+	int bus_num, bus_addr, data_addr = -1;
+	int mode = I2C_SMBUS_BYTE, pec = 0, fd, status;
+	char *end;
+	char filename[32];
+
+	opts = getopt32(argv, optstr);
+	argv += optind;
+	argc -= optind;
+
+	if (argc < 2)
+		bb_show_usage();
+
+	bus_num = i2c_bus_lookup(argv[0]);
+	bus_addr = i2c_parse_addr(argv[1]);
+
+	if (argc > 2) {
+		data_addr = bb_strtol(argv[2], &end, 16);
+		if (*end || data_addr < 0 || data_addr > 0xff)
+			err_msg_and_usage_and_die("Data address invalid");
+	}
+
+	if (argc > 3) {
+		switch (argv[3][0]) {
+		case 'b':	mode = I2C_SMBUS_BYTE_DATA;	break;
+		case 'w':	mode = I2C_SMBUS_WORD_DATA;	break;
+		case 'c':	/* Already set */		break;
+		default:	err_msg_and_usage_and_die("Invalid mode");
+		}
+		pec = argv[3][1] == 'p';
+	}
+
+	fd = i2c_dev_open(bus_num, filename, sizeof(filename));
+	check_funcs(fd, mode, data_addr, pec);
+	i2c_set_slave_addr(fd, bus_addr, opts && opt_f);
+
+	if (!(opts && opt_y))
+		confirm_action(bus_addr, mode, data_addr, pec);
+
+	if (pec)
+		ioctl_or_perror_and_die(fd, I2C_PEC, INT_TO_PTR(1),
+					"Could not set PEC");
+
+	switch (mode) {
+	case I2C_SMBUS_BYTE:
+		if (data_addr >= 0) {
+			status = i2c_smbus_write_byte(fd, data_addr);
+			if (status < 0)
+				fprintf(stderr, "Warning - write failed\n");
+		}
+		status = i2c_smbus_read_byte(fd);
+		break;
+	case I2C_SMBUS_WORD_DATA:
+		status = i2c_smbus_read_word_data(fd, data_addr);
+		break;
+	default: /* I2C_SMBUS_BYTE_DATA */
+		status = i2c_smbus_read_byte_data(fd, data_addr);
+	}
+	close(fd);
+
+	if (status < 0)
+		bb_error_msg_and_die("Read failed");
+
+	printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
+
+	return 0;
+}
--

-- 
2.1.3

Gmane