1 /* vi: set sw=4 ts=4: */
3 * Minimal i2c-tools implementation for busybox.
4 * Parts of code ported from i2c-tools:
5 * http://www.lm-sensors.org/wiki/I2CTools.
7 * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //config:config I2CGET
12 //config: bool "i2cget (5.6 kb)"
14 //config: select PLATFORM_LINUX
16 //config: Read from I2C/SMBus chip registers.
18 //config:config I2CSET
19 //config: bool "i2cset (6.9 kb)"
21 //config: select PLATFORM_LINUX
23 //config: Set I2C registers.
25 //config:config I2CDUMP
26 //config: bool "i2cdump (7.2 kb)"
28 //config: select PLATFORM_LINUX
30 //config: Examine I2C registers.
32 //config:config I2CDETECT
33 //config: bool "i2cdetect (7.2 kb)"
35 //config: select PLATFORM_LINUX
37 //config: Detect I2C chips.
40 //applet:IF_I2CGET(APPLET(i2cget, BB_DIR_USR_SBIN, BB_SUID_DROP))
41 //applet:IF_I2CSET(APPLET(i2cset, BB_DIR_USR_SBIN, BB_SUID_DROP))
42 //applet:IF_I2CDUMP(APPLET(i2cdump, BB_DIR_USR_SBIN, BB_SUID_DROP))
43 //applet:IF_I2CDETECT(APPLET(i2cdetect, BB_DIR_USR_SBIN, BB_SUID_DROP))
44 /* not NOEXEC: if hw operation stalls, use less memory in "hung" process */
46 //kbuild:lib-$(CONFIG_I2CGET) += i2c_tools.o
47 //kbuild:lib-$(CONFIG_I2CSET) += i2c_tools.o
48 //kbuild:lib-$(CONFIG_I2CDUMP) += i2c_tools.o
49 //kbuild:lib-$(CONFIG_I2CDETECT) += i2c_tools.o
54 * - upstream i2c-tools can also look-up i2c busses by name, we only accept
56 * - bank and bankreg parameters for i2cdump are not supported because of
57 * their limited usefulness (see i2cdump manual entry for more info),
58 * - i2cdetect doesn't look for bus info in /proc as it does in upstream, but
59 * it shouldn't be a problem in modern kernels.
64 #include <linux/i2c.h>
66 #define I2CDUMP_NUM_REGS 256
68 #define I2CDETECT_MODE_AUTO 0
69 #define I2CDETECT_MODE_QUICK 1
70 #define I2CDETECT_MODE_READ 2
72 /* linux/i2c-dev.h from i2c-tools overwrites the one from linux uapi
73 * and defines symbols already defined by linux/i2c.h.
74 * Also, it defines a bunch of static inlines which we would rather NOT
75 * inline. What a mess.
76 * We need only these definitions from linux/i2c-dev.h:
78 #define I2C_SLAVE 0x0703
79 #define I2C_SLAVE_FORCE 0x0706
80 #define I2C_FUNCS 0x0705
81 #define I2C_PEC 0x0708
82 #define I2C_SMBUS 0x0720
83 struct i2c_smbus_ioctl_data {
87 union i2c_smbus_data *data;
89 /* end linux/i2c-dev.h */
92 * This is needed for ioctl_or_perror_and_die() since it only accepts pointers.
94 static ALWAYS_INLINE void *itoptr(int i)
96 return (void*)(intptr_t)i;
99 static int32_t i2c_smbus_access(int fd, char read_write, uint8_t cmd,
100 int size, union i2c_smbus_data *data)
102 struct i2c_smbus_ioctl_data args;
104 args.read_write = read_write;
109 return ioctl(fd, I2C_SMBUS, &args);
112 static int32_t i2c_smbus_read_byte(int fd)
114 union i2c_smbus_data data;
117 err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
124 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
125 static int32_t i2c_smbus_write_byte(int fd, uint8_t val)
127 return i2c_smbus_access(fd, I2C_SMBUS_WRITE,
128 val, I2C_SMBUS_BYTE, NULL);
131 static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd)
133 union i2c_smbus_data data;
136 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
137 I2C_SMBUS_BYTE_DATA, &data);
144 static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd)
146 union i2c_smbus_data data;
149 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
150 I2C_SMBUS_WORD_DATA, &data);
156 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
159 static int32_t i2c_smbus_write_byte_data(int file,
160 uint8_t cmd, uint8_t value)
162 union i2c_smbus_data data;
166 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
167 I2C_SMBUS_BYTE_DATA, &data);
170 static int32_t i2c_smbus_write_word_data(int file, uint8_t cmd, uint16_t value)
172 union i2c_smbus_data data;
176 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
177 I2C_SMBUS_WORD_DATA, &data);
180 static int32_t i2c_smbus_write_block_data(int file, uint8_t cmd,
181 uint8_t length, const uint8_t *values)
183 union i2c_smbus_data data;
185 if (length > I2C_SMBUS_BLOCK_MAX)
186 length = I2C_SMBUS_BLOCK_MAX;
188 memcpy(data.block+1, values, length);
189 data.block[0] = length;
191 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
192 I2C_SMBUS_BLOCK_DATA, &data);
195 static int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t cmd,
196 uint8_t length, const uint8_t *values)
198 union i2c_smbus_data data;
200 if (length > I2C_SMBUS_BLOCK_MAX)
201 length = I2C_SMBUS_BLOCK_MAX;
203 memcpy(data.block+1, values, length);
204 data.block[0] = length;
206 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
207 I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
209 #endif /* ENABLE_I2CSET */
213 * Returns the number of bytes read, vals must hold at
214 * least I2C_SMBUS_BLOCK_MAX bytes.
216 static int32_t i2c_smbus_read_block_data(int fd, uint8_t cmd, uint8_t *vals)
218 union i2c_smbus_data data;
221 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
222 I2C_SMBUS_BLOCK_DATA, &data);
226 for (i = 1; i <= data.block[0]; i++)
227 *vals++ = data.block[i];
228 return data.block[0];
231 static int32_t i2c_smbus_read_i2c_block_data(int fd, uint8_t cmd,
232 uint8_t len, uint8_t *vals)
234 union i2c_smbus_data data;
237 if (len > I2C_SMBUS_BLOCK_MAX)
238 len = I2C_SMBUS_BLOCK_MAX;
241 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
242 len == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
243 I2C_SMBUS_I2C_BLOCK_DATA, &data);
247 for (i = 1; i <= data.block[0]; i++)
248 *vals++ = data.block[i];
249 return data.block[0];
251 #endif /* ENABLE_I2CDUMP */
254 static int32_t i2c_smbus_write_quick(int fd, uint8_t val)
256 return i2c_smbus_access(fd, val, 0, I2C_SMBUS_QUICK, NULL);
258 #endif /* ENABLE_I2CDETECT */
260 static int i2c_bus_lookup(const char *bus_str)
262 return xstrtou_range(bus_str, 10, 0, 0xfffff);
265 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
266 static int i2c_parse_bus_addr(const char *addr_str)
268 /* Slave address must be in range 0x03 - 0x77. */
269 return xstrtou_range(addr_str, 16, 0x03, 0x77);
272 static void i2c_set_pec(int fd, int pec)
274 ioctl_or_perror_and_die(fd, I2C_PEC,
279 static void i2c_set_slave_addr(int fd, int addr, int force)
281 ioctl_or_perror_and_die(fd, force ? I2C_SLAVE_FORCE : I2C_SLAVE,
283 "can't set address to 0x%02x", addr);
285 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
287 #if ENABLE_I2CGET || ENABLE_I2CSET
288 static int i2c_parse_data_addr(const char *data_addr)
290 /* Data address must be an 8 bit integer. */
291 return xstrtou_range(data_addr, 16, 0, 0xff);
293 #endif /* ENABLE_I2CGET || ENABLE_I2CSET */
296 * Opens the device file associated with given i2c bus.
298 * Upstream i2c-tools also support opening devices by i2c bus name
299 * but we drop it here for size reduction.
301 static int i2c_dev_open(int i2cbus)
303 char filename[sizeof("/dev/i2c-%d") + sizeof(int)*3];
306 sprintf(filename, "/dev/i2c-%d", i2cbus);
307 fd = open(filename, O_RDWR);
309 if (errno == ENOENT) {
310 filename[8] = '/'; /* change to "/dev/i2c/%d" */
311 fd = xopen(filename, O_RDWR);
313 bb_perror_msg_and_die("can't open '%s'", filename);
320 /* Size reducing helpers for xxx_check_funcs(). */
321 static void get_funcs_matrix(int fd, unsigned long *funcs)
323 ioctl_or_perror_and_die(fd, I2C_FUNCS, funcs,
324 "can't get adapter functionality matrix");
327 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
328 static void check_funcs_test_end(int funcs, int pec, const char *err)
330 if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
331 bb_error_msg("warning: adapter does not support PEC");
334 bb_error_msg_and_die(
335 "adapter has no %s capability", err);
337 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
340 * The below functions emit an error message and exit if the adapter doesn't
341 * support desired functionalities.
343 #if ENABLE_I2CGET || ENABLE_I2CDUMP
344 static void check_read_funcs(int fd, int mode, int data_addr, int pec)
347 const char *err = NULL;
349 get_funcs_matrix(fd, &funcs);
352 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
353 err = "SMBus receive byte";
356 if (data_addr >= 0 && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
357 err = "SMBus send byte";
359 case I2C_SMBUS_BYTE_DATA:
360 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
361 err = "SMBus read byte";
363 case I2C_SMBUS_WORD_DATA:
364 if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA))
365 err = "SMBus read word";
368 case I2C_SMBUS_BLOCK_DATA:
369 if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA))
370 err = "SMBus block read";
373 case I2C_SMBUS_I2C_BLOCK_DATA:
374 if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK))
375 err = "I2C block read";
377 #endif /* ENABLE_I2CDUMP */
379 bb_error_msg_and_die("internal error");
381 check_funcs_test_end(funcs, pec, err);
383 #endif /* ENABLE_I2CGET || ENABLE_I2CDUMP */
386 static void check_write_funcs(int fd, int mode, int pec)
389 const char *err = NULL;
391 get_funcs_matrix(fd, &funcs);
394 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
395 err = "SMBus send byte";
398 case I2C_SMBUS_BYTE_DATA:
399 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
400 err = "SMBus write byte";
403 case I2C_SMBUS_WORD_DATA:
404 if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA))
405 err = "SMBus write word";
408 case I2C_SMBUS_BLOCK_DATA:
409 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
410 err = "SMBus block write";
412 case I2C_SMBUS_I2C_BLOCK_DATA:
413 if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
414 err = "I2C block write";
417 check_funcs_test_end(funcs, pec, err);
419 #endif /* ENABLE_I2CSET */
421 static void confirm_or_abort(void)
423 fprintf(stderr, "Continue? [y/N] ");
425 if (!bb_ask_confirmation())
426 bb_error_msg_and_die("aborting");
430 * Return only if user confirms the action, abort otherwise.
432 * The messages displayed here are much less elaborate than their i2c-tools
433 * counterparts - this is done for size reduction.
435 static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
437 bb_error_msg("WARNING! This program can confuse your I2C bus");
439 /* Don't let the user break his/her EEPROMs */
440 if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
441 bb_error_msg_and_die("this is I2C not smbus - using PEC on I2C "
442 "devices may result in data loss, aborting");
445 if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec)
446 bb_error_msg("WARNING! May interpret a write byte command "
447 "with PEC as a write byte data command");
450 bb_error_msg("PEC checking enabled");
456 //usage:#define i2cget_trivial_usage
457 //usage: "[-f] [-y] BUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]"
458 //usage:#define i2cget_full_usage "\n\n"
459 //usage: "Read from I2C/SMBus chip registers\n"
460 //usage: "\n I2CBUS i2c bus number"
461 //usage: "\n ADDRESS 0x03 - 0x77"
462 //usage: "\nMODE is:"
463 //usage: "\n b read byte data (default)"
464 //usage: "\n w read word data"
465 //usage: "\n c write byte/read byte"
466 //usage: "\n Append p for SMBus PEC"
468 //usage: "\n -f force access"
469 //usage: "\n -y disable interactive mode"
470 int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
471 int i2cget_main(int argc UNUSED_PARAM, char **argv)
473 const unsigned opt_f = (1 << 0), opt_y = (1 << 1);
475 int bus_num, bus_addr, data_addr = -1, status;
476 int mode = I2C_SMBUS_BYTE, pec = 0, fd;
479 opts = getopt32(argv, "^" "fy" "\0" "-2:?4"/*from 2 to 4 args*/);
482 bus_num = i2c_bus_lookup(argv[0]);
483 bus_addr = i2c_parse_bus_addr(argv[1]);
486 data_addr = i2c_parse_data_addr(argv[2]);
487 mode = I2C_SMBUS_BYTE_DATA;
489 switch (argv[3][0]) {
490 case 'b': /* Already set */ break;
491 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
492 case 'c': mode = I2C_SMBUS_BYTE; break;
494 bb_error_msg("invalid mode");
497 pec = argv[3][1] == 'p';
501 fd = i2c_dev_open(bus_num);
502 check_read_funcs(fd, mode, data_addr, pec);
503 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
506 confirm_action(bus_addr, mode, data_addr, pec);
513 if (data_addr >= 0) {
514 status = i2c_smbus_write_byte(fd, data_addr);
516 bb_error_msg("warning - write failed");
518 status = i2c_smbus_read_byte(fd);
520 case I2C_SMBUS_WORD_DATA:
521 status = i2c_smbus_read_word_data(fd, data_addr);
523 default: /* I2C_SMBUS_BYTE_DATA */
524 status = i2c_smbus_read_byte_data(fd, data_addr);
529 bb_perror_msg_and_die("read failed");
531 printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
535 #endif /* ENABLE_I2CGET */
538 //usage:#define i2cset_trivial_usage
539 //usage: "[-f] [-y] [-m MASK] BUS CHIP-ADDR DATA-ADDR [VALUE] ... [MODE]"
540 //usage:#define i2cset_full_usage "\n\n"
541 //usage: "Set I2C registers\n"
542 //usage: "\n I2CBUS i2c bus number"
543 //usage: "\n ADDRESS 0x03 - 0x77"
544 //usage: "\nMODE is:"
545 //usage: "\n c byte, no value"
546 //usage: "\n b byte data (default)"
547 //usage: "\n w word data"
548 //usage: "\n i I2C block data"
549 //usage: "\n s SMBus block data"
550 //usage: "\n Append p for SMBus PEC"
552 //usage: "\n -f force access"
553 //usage: "\n -y disable interactive mode"
554 //usage: "\n -r read back and compare the result"
555 //usage: "\n -m MASK mask specifying which bits to write"
556 int i2cset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
557 int i2cset_main(int argc, char **argv)
559 const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
560 opt_m = (1 << 2), opt_r = (1 << 3);
562 int bus_num, bus_addr, data_addr, mode = I2C_SMBUS_BYTE, pec = 0;
563 int val, blen = 0, mask = 0, fd, status;
564 unsigned char block[I2C_SMBUS_BLOCK_MAX];
565 char *opt_m_arg = NULL;
568 opts = getopt32(argv, "^" "fym:r" "\0" "-3"/*from 3 to ? args*/, &opt_m_arg);
572 bus_num = i2c_bus_lookup(argv[0]);
573 bus_addr = i2c_parse_bus_addr(argv[1]);
574 data_addr = i2c_parse_data_addr(argv[2]);
577 if (!argv[4] && argv[3][0] != 'c') {
578 mode = I2C_SMBUS_BYTE_DATA; /* Implicit b */
580 switch (argv[argc-1][0]) {
581 case 'c': /* Already set */ break;
582 case 'b': mode = I2C_SMBUS_BYTE_DATA; break;
583 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
584 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
585 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
587 bb_error_msg("invalid mode");
591 pec = argv[argc-1][1] == 'p';
592 if (mode == I2C_SMBUS_BLOCK_DATA ||
593 mode == I2C_SMBUS_I2C_BLOCK_DATA) {
594 if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA)
595 bb_error_msg_and_die(
596 "PEC not supported for I2C "
599 bb_error_msg_and_die(
600 "mask not supported for block "
606 /* Prepare the value(s) to be written according to current mode. */
608 case I2C_SMBUS_BYTE_DATA:
609 val = xstrtou_range(argv[3], 0, 0, 0xff);
611 case I2C_SMBUS_WORD_DATA:
612 val = xstrtou_range(argv[3], 0, 0, 0xffff);
614 case I2C_SMBUS_BLOCK_DATA:
615 case I2C_SMBUS_I2C_BLOCK_DATA:
616 for (blen = 3; blen < (argc - 1); blen++)
617 block[blen] = xstrtou_range(argv[blen], 0, 0, 0xff);
626 mask = xstrtou_range(opt_m_arg, 0, 0,
627 (mode == I2C_SMBUS_BYTE ||
628 mode == I2C_SMBUS_BYTE_DATA) ? 0xff : 0xffff);
631 fd = i2c_dev_open(bus_num);
632 check_write_funcs(fd, mode, pec);
633 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
636 confirm_action(bus_addr, mode, data_addr, pec);
639 * If we're using mask - read the current value here and adjust the
640 * value to be written.
647 tmpval = i2c_smbus_read_byte(fd);
649 case I2C_SMBUS_WORD_DATA:
650 tmpval = i2c_smbus_read_word_data(fd, data_addr);
653 tmpval = i2c_smbus_read_byte_data(fd, data_addr);
657 bb_perror_msg_and_die("can't read old value");
659 val = (val & mask) | (tmpval & ~mask);
661 if (!(opts & opt_y)) {
662 bb_error_msg("old value 0x%0*x, write mask "
663 "0x%0*x, will write 0x%0*x to register "
665 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, tmpval,
666 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, mask,
667 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
678 status = i2c_smbus_write_byte(fd, data_addr);
680 case I2C_SMBUS_WORD_DATA:
681 status = i2c_smbus_write_word_data(fd, data_addr, val);
683 case I2C_SMBUS_BLOCK_DATA:
684 status = i2c_smbus_write_block_data(fd, data_addr,
687 case I2C_SMBUS_I2C_BLOCK_DATA:
688 status = i2c_smbus_write_i2c_block_data(fd, data_addr,
691 default: /* I2C_SMBUS_BYTE_DATA */
692 status = i2c_smbus_write_byte_data(fd, data_addr, val);
696 bb_perror_msg_and_die("write failed");
699 i2c_set_pec(fd, 0); /* Clear PEC. */
701 /* No readback required - we're done. */
707 status = i2c_smbus_read_byte(fd);
710 case I2C_SMBUS_WORD_DATA:
711 status = i2c_smbus_read_word_data(fd, data_addr);
713 default: /* I2C_SMBUS_BYTE_DATA */
714 status = i2c_smbus_read_byte_data(fd, data_addr);
718 puts("Warning - readback failed");
721 printf("Warning - data mismatch - wrote "
722 "0x%0*x, read back 0x%0*x\n",
723 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
724 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
726 printf("Value 0x%0*x written, readback matched\n",
727 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val);
732 #endif /* ENABLE_I2CSET */
735 static int read_block_data(int buf_fd, int mode, int *block)
737 uint8_t cblock[I2C_SMBUS_BLOCK_MAX + I2CDUMP_NUM_REGS];
738 int res, blen = 0, tmp, i;
740 if (mode == I2C_SMBUS_BLOCK_DATA) {
741 blen = i2c_smbus_read_block_data(buf_fd, 0, cblock);
745 for (res = 0; res < I2CDUMP_NUM_REGS; res += tmp) {
746 tmp = i2c_smbus_read_i2c_block_data(
747 buf_fd, res, I2C_SMBUS_BLOCK_MAX,
755 if (res >= I2CDUMP_NUM_REGS)
756 res = I2CDUMP_NUM_REGS;
758 for (i = 0; i < res; i++)
759 block[i] = cblock[i];
761 if (mode != I2C_SMBUS_BLOCK_DATA)
762 for (i = res; i < I2CDUMP_NUM_REGS; i++)
769 bb_error_msg_and_die("block read failed: %d", blen);
772 /* Dump all but word data. */
773 static void dump_data(int bus_fd, int mode, unsigned first,
774 unsigned last, int *block, int blen)
778 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
779 " 0123456789abcdef");
781 for (i = 0; i < I2CDUMP_NUM_REGS; i += 0x10) {
782 if (mode == I2C_SMBUS_BLOCK_DATA && i >= blen)
790 for (j = 0; j < 16; j++) {
792 /* Skip unwanted registers */
793 if (i+j < first || i+j > last) {
795 if (mode == I2C_SMBUS_WORD_DATA) {
803 case I2C_SMBUS_BYTE_DATA:
804 res = i2c_smbus_read_byte_data(bus_fd, i+j);
807 case I2C_SMBUS_WORD_DATA:
808 res = i2c_smbus_read_word_data(bus_fd, i+j);
813 block[i+j] = res & 0xff;
814 block[i+j+1] = res >> 8;
818 res = i2c_smbus_read_byte(bus_fd);
825 if (mode == I2C_SMBUS_BLOCK_DATA &&
828 } else if (res < 0) {
830 if (mode == I2C_SMBUS_WORD_DATA)
833 printf("%02x ", block[i+j]);
834 if (mode == I2C_SMBUS_WORD_DATA)
835 printf("%02x ", block[i+j+1]);
838 if (mode == I2C_SMBUS_WORD_DATA)
843 for (j = 0; j < 16; j++) {
844 if (mode == I2C_SMBUS_BLOCK_DATA && i+j >= blen)
846 /* Skip unwanted registers */
847 if (i+j < first || i+j > last) {
855 } else if (res == 0x00 || res == 0xff) {
857 } else if (res < 32 || res >= 127) {
867 static void dump_word_data(int bus_fd, unsigned first, unsigned last)
872 puts(" 0,8 1,9 2,a 3,b 4,c 5,d 6,e 7,f");
873 for (i = 0; i < 256; i += 8) {
880 for (j = 0; j < 8; j++) {
881 /* Skip unwanted registers. */
882 if (i+j < first || i+j > last) {
887 rv = i2c_smbus_read_word_data(bus_fd, i+j);
891 printf("%04x ", rv & 0xffff);
897 //usage:#define i2cdump_trivial_usage
898 //usage: "[-f] [-r FIRST-LAST] [-y] BUS ADDR [MODE]"
899 //usage:#define i2cdump_full_usage "\n\n"
900 //usage: "Examine I2C registers\n"
901 //usage: "\n I2CBUS i2c bus number"
902 //usage: "\n ADDRESS 0x03 - 0x77"
903 //usage: "\nMODE is:"
904 //usage: "\n b byte (default)"
906 //usage: "\n W word on even register addresses"
907 //usage: "\n i I2C block"
908 //usage: "\n s SMBus block"
909 //usage: "\n c consecutive byte"
910 //usage: "\n Append p for SMBus PEC"
912 //usage: "\n -f force access"
913 //usage: "\n -y disable interactive mode"
914 //usage: "\n -r limit the number of registers being accessed"
915 int i2cdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
916 int i2cdump_main(int argc UNUSED_PARAM, char **argv)
918 const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
921 int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0;
922 unsigned first = 0x00, last = 0xff, opts;
923 int block[I2CDUMP_NUM_REGS];
924 char *opt_r_str, *dash;
927 opts = getopt32(argv, "^"
929 "\0" "-2:?3" /* from 2 to 3 args */,
934 bus_num = i2c_bus_lookup(argv[0]);
935 bus_addr = i2c_parse_bus_addr(argv[1]);
938 switch (argv[2][0]) {
939 case 'b': /* Already set. */ break;
940 case 'c': mode = I2C_SMBUS_BYTE; break;
941 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
943 mode = I2C_SMBUS_WORD_DATA;
946 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
947 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
949 bb_error_msg_and_die("invalid mode");
952 if (argv[2][1] == 'p') {
953 if (argv[2][0] == 'W' || argv[2][0] == 'i') {
954 bb_error_msg_and_die(
955 "pec not supported for -W and -i");
963 first = strtol(opt_r_str, &dash, 0);
964 if (dash == opt_r_str || *dash != '-' || first > 0xff)
965 bb_error_msg_and_die("invalid range");
966 last = xstrtou_range(++dash, 0, first, 0xff);
968 /* Range is not available for every mode. */
971 case I2C_SMBUS_BYTE_DATA:
973 case I2C_SMBUS_WORD_DATA:
974 if (!even || (!(first % 2) && last % 2))
978 bb_error_msg_and_die(
979 "range not compatible with selected mode");
983 fd = i2c_dev_open(bus_num);
984 check_read_funcs(fd, mode, -1 /* data_addr */, pec);
985 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
991 confirm_action(bus_addr, mode, -1 /* data_addr */, pec);
993 /* All but word data. */
994 if (mode != I2C_SMBUS_WORD_DATA || even) {
997 if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA)
998 blen = read_block_data(fd, mode, block);
1000 if (mode == I2C_SMBUS_BYTE) {
1001 res = i2c_smbus_write_byte(fd, first);
1003 bb_perror_msg_and_die("write start address");
1006 dump_data(fd, mode, first, last, block, blen);
1008 dump_word_data(fd, first, last);
1013 #endif /* ENABLE_I2CDUMP */
1015 #if ENABLE_I2CDETECT
1028 static const struct adap_desc adap_descs[] = {
1030 .algo = "Dummy bus", },
1032 .algo = "ISA bus", },
1034 .algo = "I2C adapter", },
1036 .algo = "SMBus adapter", },
1045 static const struct i2c_func i2c_funcs_tab[] = {
1046 { .value = I2C_FUNC_I2C,
1048 { .value = I2C_FUNC_SMBUS_QUICK,
1049 .name = "SMBus quick command" },
1050 { .value = I2C_FUNC_SMBUS_WRITE_BYTE,
1051 .name = "SMBus send byte" },
1052 { .value = I2C_FUNC_SMBUS_READ_BYTE,
1053 .name = "SMBus receive byte" },
1054 { .value = I2C_FUNC_SMBUS_WRITE_BYTE_DATA,
1055 .name = "SMBus write byte" },
1056 { .value = I2C_FUNC_SMBUS_READ_BYTE_DATA,
1057 .name = "SMBus read byte" },
1058 { .value = I2C_FUNC_SMBUS_WRITE_WORD_DATA,
1059 .name = "SMBus write word" },
1060 { .value = I2C_FUNC_SMBUS_READ_WORD_DATA,
1061 .name = "SMBus read word" },
1062 { .value = I2C_FUNC_SMBUS_PROC_CALL,
1063 .name = "SMBus process call" },
1064 { .value = I2C_FUNC_SMBUS_WRITE_BLOCK_DATA,
1065 .name = "SMBus block write" },
1066 { .value = I2C_FUNC_SMBUS_READ_BLOCK_DATA,
1067 .name = "SMBus block read" },
1068 { .value = I2C_FUNC_SMBUS_BLOCK_PROC_CALL,
1069 .name = "SMBus block process call" },
1070 { .value = I2C_FUNC_SMBUS_PEC,
1071 .name = "SMBus PEC" },
1072 { .value = I2C_FUNC_SMBUS_WRITE_I2C_BLOCK,
1073 .name = "I2C block write" },
1074 { .value = I2C_FUNC_SMBUS_READ_I2C_BLOCK,
1075 .name = "I2C block read" },
1076 { .value = 0, .name = NULL }
1079 static enum adapter_type i2cdetect_get_funcs(int bus)
1081 enum adapter_type ret;
1082 unsigned long funcs;
1085 fd = i2c_dev_open(bus);
1087 get_funcs_matrix(fd, &funcs);
1088 if (funcs & I2C_FUNC_I2C)
1090 else if (funcs & (I2C_FUNC_SMBUS_BYTE |
1091 I2C_FUNC_SMBUS_BYTE_DATA |
1092 I2C_FUNC_SMBUS_WORD_DATA))
1102 static void NORETURN list_i2c_busses_and_exit(void)
1104 const char *const i2cdev_path = "/sys/class/i2c-dev";
1106 char path[NAME_MAX], name[128];
1107 struct dirent *de, *subde;
1108 enum adapter_type adt;
1115 * XXX Upstream i2cdetect also looks for i2c bus info in /proc/bus/i2c,
1116 * but we won't bother since it's only useful on older kernels (before
1117 * 2.6.5). We expect sysfs to be present and mounted at /sys/.
1120 dir = xopendir(i2cdev_path);
1121 while ((de = readdir(dir))) {
1122 if (de->d_name[0] == '.')
1125 /* Simple version for ISA chips. */
1126 snprintf(path, NAME_MAX, "%s/%s/name",
1127 i2cdev_path, de->d_name);
1128 fp = fopen(path, "r");
1130 snprintf(path, NAME_MAX,
1131 "%s/%s/device/name",
1132 i2cdev_path, de->d_name);
1133 fp = fopen(path, "r");
1136 /* Non-ISA chips require the hard-way. */
1138 snprintf(path, NAME_MAX,
1139 "%s/%s/device/name",
1140 i2cdev_path, de->d_name);
1141 subdir = opendir(path);
1145 while ((subde = readdir(subdir))) {
1146 if (subde->d_name[0] == '.')
1149 if (is_prefixed_with(subde->d_name, "i2c-")) {
1150 snprintf(path, NAME_MAX,
1151 "%s/%s/device/%s/name",
1152 i2cdev_path, de->d_name,
1154 fp = fopen(path, "r");
1162 * Get the rest of the info and display a line
1165 memset(name, 0, sizeof(name));
1166 pos = fgets(name, sizeof(name), fp);
1171 pos = strchr(name, '\n');
1175 rv = sscanf(de->d_name, "i2c-%d", &bus);
1179 if (is_prefixed_with(name, "ISA"))
1182 adt = i2cdetect_get_funcs(bus);
1185 "i2c-%d\t%-10s\t%-32s\t%s\n",
1186 bus, adap_descs[adt].funcs,
1187 name, adap_descs[adt].algo);
1194 static void NORETURN no_support(const char *cmd)
1196 bb_error_msg_and_die("bus doesn't support %s", cmd);
1199 static void will_skip(const char *cmd)
1202 "warning: can't use %s command, "
1203 "will skip some addresses", cmd);
1206 //usage:#define i2cdetect_trivial_usage
1207 //usage: "-l | -F I2CBUS | [-ya] [-q|-r] I2CBUS [FIRST LAST]"
1208 //usage:#define i2cdetect_full_usage "\n\n"
1209 //usage: "Detect I2C chips"
1211 //usage: "\n -l Print list of installed buses"
1212 //usage: "\n -F BUS# Print list of functionalities on this bus"
1213 //usage: "\n -y Disable interactive mode"
1214 //usage: "\n -a Force scanning of non-regular addresses"
1215 //usage: "\n -q Use smbus quick write commands for probing (default)"
1216 //usage: "\n -r Use smbus read byte commands for probing"
1217 //usage: "\n FIRST and LAST limit probing range"
1218 int i2cdetect_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1219 int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
1221 const unsigned opt_y = (1 << 0), opt_a = (1 << 1),
1222 opt_q = (1 << 2), opt_r = (1 << 3),
1223 opt_F = (1 << 4), opt_l = (1 << 5);
1225 int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status, cmd;
1226 unsigned first = 0x03, last = 0x77, opts;
1227 unsigned long funcs;
1229 opts = getopt32(argv, "^"
1232 "q--r:r--q:"/*mutually exclusive*/
1233 "?3"/*up to 3 args*/
1238 list_i2c_busses_and_exit();
1243 bus_num = i2c_bus_lookup(argv[0]);
1244 fd = i2c_dev_open(bus_num);
1245 get_funcs_matrix(fd, &funcs);
1248 /* Only list the functionalities. */
1249 printf("Functionalities implemented by bus #%d\n", bus_num);
1250 for (i = 0; i2c_funcs_tab[i].value; i++) {
1251 printf("%-32s %s\n", i2c_funcs_tab[i].name,
1252 funcs & i2c_funcs_tab[i].value ? "yes" : "no");
1255 return EXIT_SUCCESS;
1259 mode = I2CDETECT_MODE_READ;
1260 else if (opts & opt_q)
1261 mode = I2CDETECT_MODE_QUICK;
1268 /* Read address range. */
1270 first = xstrtou_range(argv[1], 16, first, last);
1272 last = xstrtou_range(argv[2], 16, first, last);
1275 if (!(funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE))) {
1276 no_support("detection commands");
1278 if (mode == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)) {
1279 no_support("SMBus quick write");
1281 if (mode == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
1282 no_support("SMBus receive byte");
1285 if (mode == I2CDETECT_MODE_AUTO) {
1286 if (!(funcs & I2C_FUNC_SMBUS_QUICK))
1287 will_skip("SMBus quick write");
1288 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1289 will_skip("SMBus receive byte");
1292 if (!(opts & opt_y))
1293 confirm_action(-1, -1, -1, 0);
1295 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
1296 for (i = 0; i < 128; i += 16) {
1297 printf("%02x: ", i);
1298 for (j = 0; j < 16; j++) {
1302 if (mode == I2CDETECT_MODE_AUTO) {
1303 if ((i+j >= 0x30 && i+j <= 0x37) ||
1304 (i+j >= 0x50 && i+j <= 0x5F))
1305 cmd = I2CDETECT_MODE_READ;
1307 cmd = I2CDETECT_MODE_QUICK;
1310 /* Skip unwanted addresses. */
1313 || (cmd == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1314 || (cmd == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
1320 status = ioctl(fd, I2C_SLAVE, itoptr(i + j));
1322 if (errno == EBUSY) {
1327 bb_perror_msg_and_die(
1328 "can't set address to 0x%02x", i + j);
1332 case I2CDETECT_MODE_READ:
1334 * This is known to lock SMBus on various
1335 * write-only chips (mainly clock chips).
1337 status = i2c_smbus_read_byte(fd);
1339 default: /* I2CDETECT_MODE_QUICK: */
1341 * This is known to corrupt the Atmel
1344 status = i2c_smbus_write_quick(fd,
1352 printf("%02x ", i+j);
1359 #endif /* ENABLE_I2CDETECT */