setserial: make it NOEXEC
[oweals/busybox.git] / miscutils / i2c_tools.c
index d77e6bacf51876626f92bc60824f71d2db9ea3df..ca2580e92e6908667b46b5d1d9de2808ebe3e0de 100644 (file)
  */
 
 //config:config I2CGET
-//config:      bool "i2cget"
+//config:      bool "i2cget (5.6 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        Read from I2C/SMBus chip registers.
+//config:      Read from I2C/SMBus chip registers.
 //config:
 //config:config I2CSET
-//config:      bool "i2cset"
+//config:      bool "i2cset (6.9 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        Set I2C registers.
+//config:      Set I2C registers.
 //config:
 //config:config I2CDUMP
-//config:      bool "i2cdump"
+//config:      bool "i2cdump (7.2 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        Examine I2C registers.
+//config:      Examine I2C registers.
 //config:
 //config:config I2CDETECT
-//config:      bool "i2cdetect"
+//config:      bool "i2cdetect (7.2 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        Detect I2C chips.
+//config:      Detect I2C chips.
 //config:
 
 //applet:IF_I2CGET(APPLET(i2cget, BB_DIR_USR_SBIN, BB_SUID_DROP))
@@ -60,6 +60,7 @@
  */
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #include <linux/i2c.h>
 #include <linux/i2c-dev.h>
@@ -723,16 +724,18 @@ static int read_block_data(int buf_fd, int mode, int *block)
        uint8_t cblock[I2C_SMBUS_BLOCK_MAX + I2CDUMP_NUM_REGS];
        int res, blen = 0, tmp, i;
 
-       if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA) {
-               res = i2c_smbus_read_block_data(buf_fd, 0, cblock);
-               blen = res;
+       if (mode == I2C_SMBUS_BLOCK_DATA) {
+               blen = i2c_smbus_read_block_data(buf_fd, 0, cblock);
+               if (blen <= 0)
+                       goto fail;
        } else {
                for (res = 0; res < I2CDUMP_NUM_REGS; res += tmp) {
                        tmp = i2c_smbus_read_i2c_block_data(
                                        buf_fd, res, I2C_SMBUS_BLOCK_MAX,
                                        cblock + res);
-                       if (tmp < 0) {
-                               bb_error_msg_and_die("block read failed");
+                       if (tmp <= 0) {
+                               blen = tmp;
+                               goto fail;
                        }
                }
 
@@ -748,6 +751,9 @@ static int read_block_data(int buf_fd, int mode, int *block)
        }
 
        return blen;
+
+ fail:
+       bb_error_msg_and_die("block read failed: %d", blen);
 }
 
 /* Dump all but word data. */
@@ -902,9 +908,9 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
 
        int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0;
        unsigned first = 0x00, last = 0xff, opts;
-       int *block = (int *)bb_common_bufsiz1;
+       int block[I2CDUMP_NUM_REGS];
        char *opt_r_str, *dash;
-       int fd, res, blen;
+       int fd, res;
 
         opt_complementary = "-2:?3"; /* from 2 to 3 args */
        opts = getopt32(argv, optstr, &opt_r_str);
@@ -971,7 +977,10 @@ int i2cdump_main(int argc UNUSED_PARAM, char **argv)
 
        /* All but word data. */
        if (mode != I2C_SMBUS_WORD_DATA || even) {
-               blen = read_block_data(fd, mode, block);
+               int blen = 0;
+
+               if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA)
+                       blen = read_block_data(fd, mode, block);
 
                if (mode == I2C_SMBUS_BYTE) {
                        res = i2c_smbus_write_byte(fd, first);
@@ -1200,7 +1209,7 @@ int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
                              opt_F = (1 << 4), opt_l = (1 << 5);
        const char *const optstr = "yaqrFl";
 
-       int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status;
+       int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status, cmd;
        unsigned first = 0x03, last = 0x77, opts;
        unsigned long funcs;
 
@@ -1270,22 +1279,23 @@ int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
        puts("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f");
        for (i = 0; i < 128; i += 16) {
                printf("%02x: ", i);
-               for(j = 0; j < 16; j++) {
+               for (j = 0; j < 16; j++) {
                        fflush_all();
 
+                       cmd = mode;
                        if (mode == I2CDETECT_MODE_AUTO) {
                                if ((i+j >= 0x30 && i+j <= 0x37) ||
                                    (i+j >= 0x50 && i+j <= 0x5F))
-                                       mode = I2CDETECT_MODE_READ;
+                                       cmd = I2CDETECT_MODE_READ;
                                else
-                                       mode = I2CDETECT_MODE_QUICK;
+                                       cmd = I2CDETECT_MODE_QUICK;
                        }
 
                        /* Skip unwanted addresses. */
                        if (i+j < first
                         || i+j > last
-                        || (mode == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
-                        || (mode == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
+                        || (cmd == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
+                        || (cmd == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
                        {
                                printf("   ");
                                continue;
@@ -1302,7 +1312,7 @@ int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
                                        "can't set address to 0x%02x", i + j);
                        }
 
-                       switch (mode) {
+                       switch (cmd) {
                        case I2CDETECT_MODE_READ:
                                /*
                                 * This is known to lock SMBus on various