7be489036f32cbe7da0e7042d6999fe2031d75fc
[oweals/busybox.git] / miscutils / i2c_tools.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Minimal i2c-tools implementation for busybox.
4  * Parts of code ported from i2c-tools:
5  *              http://www.lm-sensors.org/wiki/I2CTools.
6  *
7  * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10  */
11
12 //config:config I2CGET
13 //config:       bool "i2cget"
14 //config:       default y
15 //config:       select PLATFORM_LINUX
16 //config:       help
17 //config:         Read from I2C/SMBus chip registers.
18 //config:
19 //config:config I2CSET
20 //config:       bool "i2cset"
21 //config:       default y
22 //config:       select PLATFORM_LINUX
23 //config:       help
24 //config:         Set I2C registers.
25 //config:
26 //config:config I2CDUMP
27 //config:       bool "i2cdump"
28 //config:       default y
29 //config:       select PLATFORM_LINUX
30 //config:       help
31 //config:         Examine I2C registers.
32 //config:
33 //config:config I2CDETECT
34 //config:       bool "i2cdetect"
35 //config:       default y
36 //config:       select PLATFORM_LINUX
37 //config:       help
38 //config:         Detect I2C chips.
39 //config:
40
41 //applet:IF_I2CGET(APPLET(i2cget, BB_DIR_USR_SBIN, BB_SUID_DROP))
42 //applet:IF_I2CSET(APPLET(i2cset, BB_DIR_USR_SBIN, BB_SUID_DROP))
43 //applet:IF_I2CDUMP(APPLET(i2cdump, BB_DIR_USR_SBIN, BB_SUID_DROP))
44 //applet:IF_I2CDETECT(APPLET(i2cdetect, BB_DIR_USR_SBIN, BB_SUID_DROP))
45
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
50
51 /*
52  * Unsupported stuff:
53  *
54  * - upstream i2c-tools can also look-up i2c busses by name, we only accept
55  *   numbers,
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.
60  */
61
62 #include "libbb.h"
63
64 #include <linux/i2c.h>
65 #include <linux/i2c-dev.h>
66
67 #define I2CDUMP_NUM_REGS                256
68
69 #define I2CDETECT_MODE_AUTO             0
70 #define I2CDETECT_MODE_QUICK            1
71 #define I2CDETECT_MODE_READ             2
72
73 /*
74  * This is needed for ioctl_or_perror_and_die() since it only accepts pointers.
75  */
76 static ALWAYS_INLINE void *itoptr(int i)
77 {
78         return (void*)(intptr_t)i;
79 }
80
81 static int32_t i2c_smbus_access(int fd, char read_write, uint8_t cmd,
82                                 int size, union i2c_smbus_data *data)
83 {
84         struct i2c_smbus_ioctl_data args;
85
86         args.read_write = read_write;
87         args.command = cmd;
88         args.size = size;
89         args.data = data;
90
91         return ioctl(fd, I2C_SMBUS, &args);
92 }
93
94 static int32_t i2c_smbus_read_byte(int fd)
95 {
96         union i2c_smbus_data data;
97         int err;
98
99         err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
100         if (err < 0)
101                 return err;
102
103         return data.byte;
104 }
105
106 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
107 static int32_t i2c_smbus_write_byte(int fd, uint8_t val)
108 {
109         return i2c_smbus_access(fd, I2C_SMBUS_WRITE,
110                                 val, I2C_SMBUS_BYTE, NULL);
111 }
112
113 static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd)
114 {
115         union i2c_smbus_data data;
116         int err;
117
118         err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
119                                I2C_SMBUS_BYTE_DATA, &data);
120         if (err < 0)
121                 return err;
122
123         return data.byte;
124 }
125
126 static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd)
127 {
128         union i2c_smbus_data data;
129         int err;
130
131         err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
132                                I2C_SMBUS_WORD_DATA, &data);
133         if (err < 0)
134                 return err;
135
136         return data.word;
137 }
138 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
139
140 #if ENABLE_I2CSET
141 static int32_t i2c_smbus_write_byte_data(int file,
142                                          uint8_t cmd, uint8_t value)
143 {
144         union i2c_smbus_data data;
145
146         data.byte = value;
147
148         return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
149                                 I2C_SMBUS_BYTE_DATA, &data);
150 }
151
152 static int32_t i2c_smbus_write_word_data(int file, uint8_t cmd, uint16_t value)
153 {
154         union i2c_smbus_data data;
155
156         data.word = value;
157
158         return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
159                                 I2C_SMBUS_WORD_DATA, &data);
160 }
161
162 static int32_t i2c_smbus_write_block_data(int file, uint8_t cmd,
163                                    uint8_t length, const uint8_t *values)
164 {
165         union i2c_smbus_data data;
166
167         if (length > I2C_SMBUS_BLOCK_MAX)
168                 length = I2C_SMBUS_BLOCK_MAX;
169
170         memcpy(data.block+1, values, length);
171         data.block[0] = length;
172
173         return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
174                                 I2C_SMBUS_BLOCK_DATA, &data);
175 }
176
177 static int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t cmd,
178                                        uint8_t length, const uint8_t *values)
179 {
180         union i2c_smbus_data data;
181
182         if (length > I2C_SMBUS_BLOCK_MAX)
183                 length = I2C_SMBUS_BLOCK_MAX;
184
185         memcpy(data.block+1, values, length);
186         data.block[0] = length;
187
188         return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
189                                 I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
190 }
191 #endif /* ENABLE_I2CSET */
192
193 #if ENABLE_I2CDUMP
194 /*
195  * Returns the number of bytes read, vals must hold at
196  * least I2C_SMBUS_BLOCK_MAX bytes.
197  */
198 static int32_t i2c_smbus_read_block_data(int fd, uint8_t cmd, uint8_t *vals)
199 {
200         union i2c_smbus_data data;
201         int i, err;
202
203         err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
204                                I2C_SMBUS_BLOCK_DATA, &data);
205         if (err < 0)
206                 return err;
207
208         for (i = 1; i <= data.block[0]; i++)
209                 *vals++ = data.block[i];
210         return data.block[0];
211 }
212
213 static int32_t i2c_smbus_read_i2c_block_data(int fd, uint8_t cmd,
214                                              uint8_t len, uint8_t *vals)
215 {
216         union i2c_smbus_data data;
217         int i, err;
218
219         if (len > I2C_SMBUS_BLOCK_MAX)
220                 len = I2C_SMBUS_BLOCK_MAX;
221         data.block[0] = len;
222
223         err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
224                                len == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
225                                            I2C_SMBUS_I2C_BLOCK_DATA, &data);
226         if (err < 0)
227                 return err;
228
229         for (i = 1; i <= data.block[0]; i++)
230                 *vals++ = data.block[i];
231         return data.block[0];
232 }
233 #endif /* ENABLE_I2CDUMP */
234
235 #if ENABLE_I2CDETECT
236 static int32_t i2c_smbus_write_quick(int fd, uint8_t val)
237 {
238         return i2c_smbus_access(fd, val, 0, I2C_SMBUS_QUICK, NULL);
239 }
240 #endif /* ENABLE_I2CDETECT */
241
242 static int i2c_bus_lookup(const char *bus_str)
243 {
244         return xstrtou_range(bus_str, 10, 0, 0xfffff);
245 }
246
247 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
248 static int i2c_parse_bus_addr(const char *addr_str)
249 {
250         /* Slave address must be in range 0x03 - 0x77. */
251         return xstrtou_range(addr_str, 16, 0x03, 0x77);
252 }
253
254 static void i2c_set_pec(int fd, int pec)
255 {
256         ioctl_or_perror_and_die(fd, I2C_PEC,
257                                 itoptr(pec ? 1 : 0),
258                                 "can't set PEC");
259 }
260
261 static void i2c_set_slave_addr(int fd, int addr, int force)
262 {
263         ioctl_or_perror_and_die(fd, force ? I2C_SLAVE_FORCE : I2C_SLAVE,
264                                 itoptr(addr),
265                                 "can't set address to 0x%02x", addr);
266 }
267 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
268
269 #if ENABLE_I2CGET || ENABLE_I2CSET
270 static int i2c_parse_data_addr(const char *data_addr)
271 {
272         /* Data address must be an 8 bit integer. */
273         return xstrtou_range(data_addr, 16, 0, 0xff);
274 }
275 #endif /* ENABLE_I2CGET || ENABLE_I2CSET */
276
277 /*
278  * Opens the device file associated with given i2c bus.
279  *
280  * Upstream i2c-tools also support opening devices by i2c bus name
281  * but we drop it here for size reduction.
282  */
283 static int i2c_dev_open(int i2cbus)
284 {
285         char filename[sizeof("/dev/i2c-%d") + sizeof(int)*3];
286         int fd;
287
288         sprintf(filename, "/dev/i2c-%d", i2cbus);
289         fd = open(filename, O_RDWR);
290         if (fd < 0) {
291                 if (errno == ENOENT) {
292                         filename[8] = '/'; /* change to "/dev/i2c/%d" */
293                         fd = xopen(filename, O_RDWR);
294                 } else {
295                         bb_perror_msg_and_die("can't open '%s'", filename);
296                 }
297         }
298
299         return fd;
300 }
301
302 /* Size reducing helpers for xxx_check_funcs(). */
303 static void get_funcs_matrix(int fd, unsigned long *funcs)
304 {
305         ioctl_or_perror_and_die(fd, I2C_FUNCS, funcs,
306                         "can't get adapter functionality matrix");
307 }
308
309 #if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
310 static void check_funcs_test_end(int funcs, int pec, const char *err)
311 {
312         if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
313                 bb_error_msg("warning: adapter does not support PEC");
314
315         if (err)
316                 bb_error_msg_and_die(
317                         "adapter has no %s capability", err);
318 }
319 #endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
320
321 /*
322  * The below functions emit an error message and exit if the adapter doesn't
323  * support desired functionalities.
324  */
325 #if ENABLE_I2CGET || ENABLE_I2CDUMP
326 static void check_read_funcs(int fd, int mode, int data_addr, int pec)
327 {
328         unsigned long funcs;
329         const char *err = NULL;
330
331         get_funcs_matrix(fd, &funcs);
332         switch (mode) {
333         case I2C_SMBUS_BYTE:
334                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
335                         err = "SMBus receive byte";
336                         break;
337                 }
338                 if (data_addr >= 0 && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
339                         err = "SMBus send byte";
340                 break;
341         case I2C_SMBUS_BYTE_DATA:
342                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
343                         err = "SMBus read byte";
344                 break;
345         case I2C_SMBUS_WORD_DATA:
346                 if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA))
347                         err = "SMBus read word";
348                 break;
349 #if ENABLE_I2CDUMP
350         case I2C_SMBUS_BLOCK_DATA:
351                 if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA))
352                         err = "SMBus block read";
353                 break;
354
355         case I2C_SMBUS_I2C_BLOCK_DATA:
356                 if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK))
357                         err = "I2C block read";
358                 break;
359 #endif /* ENABLE_I2CDUMP */
360         default:
361                 bb_error_msg_and_die("internal error");
362         }
363         check_funcs_test_end(funcs, pec, err);
364 }
365 #endif /* ENABLE_I2CGET || ENABLE_I2CDUMP */
366
367 #if ENABLE_I2CSET
368 static void check_write_funcs(int fd, int mode, int pec)
369 {
370         unsigned long funcs;
371         const char *err = NULL;
372
373         get_funcs_matrix(fd, &funcs);
374         switch (mode) {
375         case I2C_SMBUS_BYTE:
376                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
377                         err = "SMBus send byte";
378                 break;
379
380         case I2C_SMBUS_BYTE_DATA:
381                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
382                         err = "SMBus write byte";
383                 break;
384
385         case I2C_SMBUS_WORD_DATA:
386                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA))
387                         err = "SMBus write word";
388                 break;
389
390         case I2C_SMBUS_BLOCK_DATA:
391                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
392                         err = "SMBus block write";
393                 break;
394         case I2C_SMBUS_I2C_BLOCK_DATA:
395                 if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
396                         err = "I2C block write";
397                 break;
398         }
399         check_funcs_test_end(funcs, pec, err);
400 }
401 #endif /* ENABLE_I2CSET */
402
403 static void confirm_or_abort(void)
404 {
405         fprintf(stderr, "Continue? [y/N] ");
406         fflush_all();
407         if (!bb_ask_confirmation())
408                 bb_error_msg_and_die("aborting");
409 }
410
411 /*
412  * Return only if user confirms the action, abort otherwise.
413  *
414  * The messages displayed here are much less elaborate than their i2c-tools
415  * counterparts - this is done for size reduction.
416  */
417 static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
418 {
419         bb_error_msg("WARNING! This program can confuse your I2C bus");
420
421         /* Don't let the user break his/her EEPROMs */
422         if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
423                 bb_error_msg_and_die("this is I2C not smbus - using PEC on I2C "
424                         "devices may result in data loss, aborting");
425         }
426
427         if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec)
428                 bb_error_msg("WARNING! May interpret a write byte command "
429                         "with PEC as a write byte data command");
430
431         if (pec)
432                 bb_error_msg("PEC checking enabled");
433
434         confirm_or_abort();
435 }
436
437 #if ENABLE_I2CGET
438 //usage:#define i2cget_trivial_usage
439 //usage:       "[-f] [-y] BUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]"
440 //usage:#define i2cget_full_usage "\n\n"
441 //usage:       "Read from I2C/SMBus chip registers\n"
442 //usage:     "\n        I2CBUS  i2c bus number"
443 //usage:     "\n        ADDRESS 0x03 - 0x77"
444 //usage:     "\nMODE is:"
445 //usage:     "\n        b       read byte data (default)"
446 //usage:     "\n        w       read word data"
447 //usage:     "\n        c       write byte/read byte"
448 //usage:     "\n        Append p for SMBus PEC"
449 //usage:     "\n"
450 //usage:     "\n        -f      force access"
451 //usage:     "\n        -y      disable interactive mode"
452 int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
453 int i2cget_main(int argc UNUSED_PARAM, char **argv)
454 {
455         const unsigned opt_f = (1 << 0), opt_y = (1 << 1);
456         const char *const optstr = "fy";
457
458         int bus_num, bus_addr, data_addr = -1, status;
459         int mode = I2C_SMBUS_BYTE, pec = 0, fd;
460         unsigned opts;
461
462         opt_complementary = "-2:?4"; /* from 2 to 4 args */
463         opts = getopt32(argv, optstr);
464         argv += optind;
465
466         bus_num = i2c_bus_lookup(argv[0]);
467         bus_addr = i2c_parse_bus_addr(argv[1]);
468
469         if (argv[2]) {
470                 data_addr = i2c_parse_data_addr(argv[2]);
471                 mode = I2C_SMBUS_BYTE_DATA;
472                 if (argv[3]) {
473                         switch (argv[3][0]) {
474                         case 'b':       /* Already set */               break;
475                         case 'w':       mode = I2C_SMBUS_WORD_DATA;     break;
476                         case 'c':       mode = I2C_SMBUS_BYTE;          break;
477                         default:
478                                 bb_error_msg("invalid mode");
479                                 bb_show_usage();
480                         }
481                         pec = argv[3][1] == 'p';
482                 }
483         }
484
485         fd = i2c_dev_open(bus_num);
486         check_read_funcs(fd, mode, data_addr, pec);
487         i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
488
489         if (!(opts & opt_y))
490                 confirm_action(bus_addr, mode, data_addr, pec);
491
492         if (pec)
493                 i2c_set_pec(fd, 1);
494
495         switch (mode) {
496         case I2C_SMBUS_BYTE:
497                 if (data_addr >= 0) {
498                         status = i2c_smbus_write_byte(fd, data_addr);
499                         if (status < 0)
500                                 bb_error_msg("warning - write failed");
501                 }
502                 status = i2c_smbus_read_byte(fd);
503                 break;
504         case I2C_SMBUS_WORD_DATA:
505                 status = i2c_smbus_read_word_data(fd, data_addr);
506                 break;
507         default: /* I2C_SMBUS_BYTE_DATA */
508                 status = i2c_smbus_read_byte_data(fd, data_addr);
509         }
510         close(fd);
511
512         if (status < 0)
513                 bb_perror_msg_and_die("read failed");
514
515         printf("0x%0*x\n", mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
516
517         return 0;
518 }
519 #endif /* ENABLE_I2CGET */
520
521 #if ENABLE_I2CSET
522 //usage:#define i2cset_trivial_usage
523 //usage:       "[-f] [-y] [-m MASK] BUS CHIP-ADDR DATA-ADDR [VALUE] ... [MODE]"
524 //usage:#define i2cset_full_usage "\n\n"
525 //usage:       "Set I2C registers\n"
526 //usage:     "\n        I2CBUS  i2c bus number"
527 //usage:     "\n        ADDRESS 0x03 - 0x77"
528 //usage:     "\nMODE is:"
529 //usage:     "\n        c       byte, no value"
530 //usage:     "\n        b       byte data (default)"
531 //usage:     "\n        w       word data"
532 //usage:     "\n        i       I2C block data"
533 //usage:     "\n        s       SMBus block data"
534 //usage:     "\n        Append p for SMBus PEC"
535 //usage:     "\n"
536 //usage:     "\n        -f      force access"
537 //usage:     "\n        -y      disable interactive mode"
538 //usage:     "\n        -r      read back and compare the result"
539 //usage:     "\n        -m MASK mask specifying which bits to write"
540 int i2cset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
541 int i2cset_main(int argc, char **argv)
542 {
543         const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
544                               opt_m = (1 << 2), opt_r = (1 << 3);
545         const char *const optstr = "fym:r";
546
547         int bus_num, bus_addr, data_addr, mode = I2C_SMBUS_BYTE, pec = 0;
548         int val, blen = 0, mask = 0, fd, status;
549         unsigned char block[I2C_SMBUS_BLOCK_MAX];
550         char *opt_m_arg = NULL;
551         unsigned opts;
552
553         opt_complementary = "-3"; /* from 3 to ? args */
554         opts = getopt32(argv, optstr, &opt_m_arg);
555         argv += optind;
556         argc -= optind;
557
558         bus_num = i2c_bus_lookup(argv[0]);
559         bus_addr = i2c_parse_bus_addr(argv[1]);
560         data_addr = i2c_parse_data_addr(argv[2]);
561
562         if (argv[3]) {
563                 if (!argv[4] && argv[3][0] != 'c') {
564                         mode = I2C_SMBUS_BYTE_DATA; /* Implicit b */
565                 } else {
566                         switch (argv[argc-1][0]) {
567                         case 'c': /* Already set */                     break;
568                         case 'b': mode = I2C_SMBUS_BYTE_DATA;           break;
569                         case 'w': mode = I2C_SMBUS_WORD_DATA;           break;
570                         case 's': mode = I2C_SMBUS_BLOCK_DATA;          break;
571                         case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA;      break;
572                         default:
573                                 bb_error_msg("invalid mode");
574                                 bb_show_usage();
575                         }
576
577                         pec = argv[argc-1][1] == 'p';
578                         if (mode == I2C_SMBUS_BLOCK_DATA ||
579                                         mode == I2C_SMBUS_I2C_BLOCK_DATA) {
580                                 if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA)
581                                         bb_error_msg_and_die(
582                                                 "PEC not supported for I2C "
583                                                 "block writes");
584                                 if (opts & opt_m)
585                                         bb_error_msg_and_die(
586                                                 "mask not supported for block "
587                                                 "writes");
588                         }
589                 }
590         }
591
592         /* Prepare the value(s) to be written according to current mode. */
593         switch (mode) {
594         case I2C_SMBUS_BYTE_DATA:
595                 val = xstrtou_range(argv[3], 0, 0, 0xff);
596                 break;
597         case I2C_SMBUS_WORD_DATA:
598                 val = xstrtou_range(argv[3], 0, 0, 0xffff);
599                 break;
600         case I2C_SMBUS_BLOCK_DATA:
601         case I2C_SMBUS_I2C_BLOCK_DATA:
602                 for (blen = 3; blen < (argc - 1); blen++)
603                         block[blen] = xstrtou_range(argv[blen], 0, 0, 0xff);
604                 val = -1;
605                 break;
606         default:
607                 val = -1;
608                 break;
609         }
610
611         if (opts & opt_m) {
612                 mask = xstrtou_range(opt_m_arg, 0, 0,
613                                 (mode == I2C_SMBUS_BYTE ||
614                                  mode == I2C_SMBUS_BYTE_DATA) ? 0xff : 0xffff);
615         }
616
617         fd = i2c_dev_open(bus_num);
618         check_write_funcs(fd, mode, pec);
619         i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
620
621         if (!(opts & opt_y))
622                 confirm_action(bus_addr, mode, data_addr, pec);
623
624         /*
625          * If we're using mask - read the current value here and adjust the
626          * value to be written.
627          */
628         if (opts & opt_m) {
629                 int tmpval;
630
631                 switch (mode) {
632                 case I2C_SMBUS_BYTE:
633                         tmpval = i2c_smbus_read_byte(fd);
634                         break;
635                 case I2C_SMBUS_WORD_DATA:
636                         tmpval = i2c_smbus_read_word_data(fd, data_addr);
637                         break;
638                 default:
639                         tmpval = i2c_smbus_read_byte_data(fd, data_addr);
640                 }
641
642                 if (tmpval < 0)
643                         bb_perror_msg_and_die("can't read old value");
644
645                 val = (val & mask) | (tmpval & ~mask);
646
647                 if (!(opts & opt_y)) {
648                         bb_error_msg("old value 0x%0*x, write mask "
649                                 "0x%0*x, will write 0x%0*x to register "
650                                 "0x%02x",
651                                 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, tmpval,
652                                 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, mask,
653                                 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
654                                 data_addr);
655                         confirm_or_abort();
656                 }
657         }
658
659         if (pec)
660                 i2c_set_pec(fd, 1);
661
662         switch (mode) {
663         case I2C_SMBUS_BYTE:
664                 status = i2c_smbus_write_byte(fd, data_addr);
665                 break;
666         case I2C_SMBUS_WORD_DATA:
667                 status = i2c_smbus_write_word_data(fd, data_addr, val);
668                 break;
669         case I2C_SMBUS_BLOCK_DATA:
670                 status = i2c_smbus_write_block_data(fd, data_addr,
671                                                     blen, block);
672                 break;
673         case I2C_SMBUS_I2C_BLOCK_DATA:
674                 status = i2c_smbus_write_i2c_block_data(fd, data_addr,
675                                                         blen, block);
676                 break;
677         default: /* I2C_SMBUS_BYTE_DATA */
678                 status = i2c_smbus_write_byte_data(fd, data_addr, val);
679                 break;
680         }
681         if (status < 0)
682                 bb_perror_msg_and_die("write failed");
683
684         if (pec)
685                 i2c_set_pec(fd, 0); /* Clear PEC. */
686
687         /* No readback required - we're done. */
688         if (!(opts & opt_r))
689                 return 0;
690
691         switch (mode) {
692         case I2C_SMBUS_BYTE:
693                 status = i2c_smbus_read_byte(fd);
694                 val = data_addr;
695                 break;
696         case I2C_SMBUS_WORD_DATA:
697                 status = i2c_smbus_read_word_data(fd, data_addr);
698                 break;
699         default: /* I2C_SMBUS_BYTE_DATA */
700                 status = i2c_smbus_read_byte_data(fd, data_addr);
701         }
702
703         if (status < 0) {
704                 puts("Warning - readback failed");
705         } else
706         if (status != val) {
707                 printf("Warning - data mismatch - wrote "
708                        "0x%0*x, read back 0x%0*x\n",
709                        mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
710                        mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
711         } else {
712                 printf("Value 0x%0*x written, readback matched\n",
713                        mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val);
714         }
715
716         return 0;
717 }
718 #endif /* ENABLE_I2CSET */
719
720 #if ENABLE_I2CDUMP
721 static int read_block_data(int buf_fd, int mode, int *block)
722 {
723         uint8_t cblock[I2C_SMBUS_BLOCK_MAX + I2CDUMP_NUM_REGS];
724         int res, blen = 0, tmp, i;
725
726         if (mode == I2C_SMBUS_BLOCK_DATA) {
727                 blen = i2c_smbus_read_block_data(buf_fd, 0, cblock);
728                 if (blen <= 0)
729                         goto fail;
730         } else {
731                 for (res = 0; res < I2CDUMP_NUM_REGS; res += tmp) {
732                         tmp = i2c_smbus_read_i2c_block_data(
733                                         buf_fd, res, I2C_SMBUS_BLOCK_MAX,
734                                         cblock + res);
735                         if (tmp <= 0)
736                                 goto fail;
737                 }
738
739                 if (res >= I2CDUMP_NUM_REGS)
740                         res = I2CDUMP_NUM_REGS;
741
742                 for (i = 0; i < res; i++)
743                         block[i] = cblock[i];
744
745                 if (mode != I2C_SMBUS_BLOCK_DATA)
746                         for (i = res; i < I2CDUMP_NUM_REGS; i++)
747                                 block[i] = -1;
748         }
749
750         return blen;
751
752  fail:
753         bb_error_msg_and_die("block read failed");
754 }
755
756 /* Dump all but word data. */
757 static void dump_data(int bus_fd, int mode, unsigned first,
758                       unsigned last, int *block, int blen)
759 {
760         int i, j, res;
761
762         puts("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f"
763              "    0123456789abcdef");
764
765         for (i = 0; i < I2CDUMP_NUM_REGS; i += 0x10) {
766                 if (mode == I2C_SMBUS_BLOCK_DATA && i >= blen)
767                         break;
768                 if (i/16 < first/16)
769                         continue;
770                 if (i/16 > last/16)
771                         break;
772
773                 printf("%02x: ", i);
774                 for (j = 0; j < 16; j++) {
775                         fflush_all();
776                         /* Skip unwanted registers */
777                         if (i+j < first || i+j > last) {
778                                 printf("   ");
779                                 if (mode == I2C_SMBUS_WORD_DATA) {
780                                         printf("   ");
781                                         j++;
782                                 }
783                                 continue;
784                         }
785
786                         switch (mode) {
787                         case I2C_SMBUS_BYTE_DATA:
788                                 res = i2c_smbus_read_byte_data(bus_fd, i+j);
789                                 block[i+j] = res;
790                                 break;
791                         case I2C_SMBUS_WORD_DATA:
792                                 res = i2c_smbus_read_word_data(bus_fd, i+j);
793                                 if (res < 0) {
794                                         block[i+j] = res;
795                                         block[i+j+1] = res;
796                                 } else {
797                                         block[i+j] = res & 0xff;
798                                         block[i+j+1] = res >> 8;
799                                 }
800                                 break;
801                         case I2C_SMBUS_BYTE:
802                                 res = i2c_smbus_read_byte(bus_fd);
803                                 block[i+j] = res;
804                                 break;
805                         default:
806                                 res = block[i+j];
807                         }
808
809                         if (mode == I2C_SMBUS_BLOCK_DATA &&
810                             i+j >= blen) {
811                                 printf("   ");
812                         } else if (res < 0) {
813                                 printf("XX ");
814                                 if (mode == I2C_SMBUS_WORD_DATA)
815                                         printf("XX ");
816                         } else {
817                                 printf("%02x ", block[i+j]);
818                                 if (mode == I2C_SMBUS_WORD_DATA)
819                                         printf("%02x ", block[i+j+1]);
820                         }
821
822                         if (mode == I2C_SMBUS_WORD_DATA)
823                                 j++;
824                 }
825                 printf("   ");
826
827                 for (j = 0; j < 16; j++) {
828                         if (mode == I2C_SMBUS_BLOCK_DATA && i+j >= blen)
829                                 break;
830                         /* Skip unwanted registers */
831                         if (i+j < first || i+j > last) {
832                                 bb_putchar(' ');
833                                 continue;
834                         }
835
836                         res = block[i+j];
837                         if (res < 0) {
838                                 bb_putchar('X');
839                         } else if (res == 0x00 || res == 0xff) {
840                                 bb_putchar('.');
841                         } else if (res < 32 || res >= 127) {
842                                 bb_putchar('?');
843                         } else {
844                                 bb_putchar(res);
845                         }
846                 }
847                 bb_putchar('\n');
848         }
849 }
850
851 static void dump_word_data(int bus_fd, unsigned first, unsigned last)
852 {
853         int i, j, rv;
854
855         /* Word data. */
856         puts("     0,8  1,9  2,a  3,b  4,c  5,d  6,e  7,f");
857         for (i = 0; i < 256; i += 8) {
858                 if (i/8 < first/8)
859                         continue;
860                 if (i/8 > last/8)
861                         break;
862
863                 printf("%02x: ", i);
864                 for (j = 0; j < 8; j++) {
865                         /* Skip unwanted registers. */
866                         if (i+j < first || i+j > last) {
867                                 printf("     ");
868                                 continue;
869                         }
870
871                         rv = i2c_smbus_read_word_data(bus_fd, i+j);
872                         if (rv < 0)
873                                 printf("XXXX ");
874                         else
875                                 printf("%04x ", rv & 0xffff);
876                 }
877                 bb_putchar('\n');
878         }
879 }
880
881 //usage:#define i2cdump_trivial_usage
882 //usage:       "[-f] [-r FIRST-LAST] [-y] BUS ADDR [MODE]"
883 //usage:#define i2cdump_full_usage "\n\n"
884 //usage:       "Examine I2C registers\n"
885 //usage:     "\n        I2CBUS  i2c bus number"
886 //usage:     "\n        ADDRESS 0x03 - 0x77"
887 //usage:     "\nMODE is:"
888 //usage:     "\n        b       byte (default)"
889 //usage:     "\n        w       word"
890 //usage:     "\n        W       word on even register addresses"
891 //usage:     "\n        i       I2C block"
892 //usage:     "\n        s       SMBus block"
893 //usage:     "\n        c       consecutive byte"
894 //usage:     "\n        Append p for SMBus PEC"
895 //usage:     "\n"
896 //usage:     "\n        -f      force access"
897 //usage:     "\n        -y      disable interactive mode"
898 //usage:     "\n        -r      limit the number of registers being accessed"
899 int i2cdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
900 int i2cdump_main(int argc UNUSED_PARAM, char **argv)
901 {
902         const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
903                               opt_r = (1 << 2);
904         const char *const optstr = "fyr:";
905
906         int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0;
907         unsigned first = 0x00, last = 0xff, opts;
908         int *block = (int *)bb_common_bufsiz1;
909         char *opt_r_str, *dash;
910         int fd, res, blen;
911
912         opt_complementary = "-2:?3"; /* from 2 to 3 args */
913         opts = getopt32(argv, optstr, &opt_r_str);
914         argv += optind;
915
916         bus_num = i2c_bus_lookup(argv[0]);
917         bus_addr = i2c_parse_bus_addr(argv[1]);
918
919         if (argv[2]) {
920                 switch (argv[2][0]) {
921                 case 'b': /* Already set. */                    break;
922                 case 'c': mode = I2C_SMBUS_BYTE;                break;
923                 case 'w': mode = I2C_SMBUS_WORD_DATA;           break;
924                 case 'W':
925                         mode = I2C_SMBUS_WORD_DATA;
926                         even = 1;
927                         break;
928                 case 's': mode = I2C_SMBUS_BLOCK_DATA;          break;
929                 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA;      break;
930                 default:
931                         bb_error_msg_and_die("invalid mode");
932                 }
933
934                 if (argv[2][1] == 'p') {
935                         if (argv[2][0] == 'W' || argv[2][0] == 'i') {
936                                 bb_error_msg_and_die(
937                                         "pec not supported for -W and -i");
938                         } else {
939                                 pec = 1;
940                         }
941                 }
942         }
943
944         if (opts & opt_r) {
945                 first = strtol(opt_r_str, &dash, 0);
946                 if (dash == opt_r_str || *dash != '-' || first > 0xff)
947                         bb_error_msg_and_die("invalid range");
948                 last = xstrtou_range(++dash, 0, first, 0xff);
949
950                 /* Range is not available for every mode. */
951                 switch (mode) {
952                 case I2C_SMBUS_BYTE:
953                 case I2C_SMBUS_BYTE_DATA:
954                         break;
955                 case I2C_SMBUS_WORD_DATA:
956                         if (!even || (!(first % 2) && last % 2))
957                                 break;
958                         /* Fall through */
959                 default:
960                         bb_error_msg_and_die(
961                                 "range not compatible with selected mode");
962                 }
963         }
964
965         fd = i2c_dev_open(bus_num);
966         check_read_funcs(fd, mode, -1 /* data_addr */, pec);
967         i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
968
969         if (pec)
970                 i2c_set_pec(fd, 1);
971
972         if (!(opts & opt_y))
973                 confirm_action(bus_addr, mode, -1 /* data_addr */, pec);
974
975         /* All but word data. */
976         if (mode != I2C_SMBUS_WORD_DATA || even) {
977                 if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA)
978                         blen = read_block_data(fd, mode, block);
979
980                 if (mode == I2C_SMBUS_BYTE) {
981                         res = i2c_smbus_write_byte(fd, first);
982                         if (res < 0)
983                                 bb_perror_msg_and_die("write start address");
984                 }
985
986                 dump_data(fd, mode, first, last, block, blen);
987         } else {
988                 dump_word_data(fd, first, last);
989         }
990
991         return 0;
992 }
993 #endif /* ENABLE_I2CDUMP */
994
995 #if ENABLE_I2CDETECT
996 enum adapter_type {
997         ADT_DUMMY = 0,
998         ADT_ISA,
999         ADT_I2C,
1000         ADT_SMBUS,
1001 };
1002
1003 struct adap_desc {
1004         const char *funcs;
1005         const char *algo;
1006 };
1007
1008 static const struct adap_desc adap_descs[] = {
1009         { .funcs        = "dummy",
1010           .algo         = "Dummy bus", },
1011         { .funcs        = "isa",
1012           .algo         = "ISA bus", },
1013         { .funcs        = "i2c",
1014           .algo         = "I2C adapter", },
1015         { .funcs        = "smbus",
1016           .algo         = "SMBus adapter", },
1017 };
1018
1019 struct i2c_func
1020 {
1021         long value;
1022         const char* name;
1023 };
1024
1025 static const struct i2c_func i2c_funcs_tab[] = {
1026         { .value = I2C_FUNC_I2C,
1027           .name = "I2C" },
1028         { .value = I2C_FUNC_SMBUS_QUICK,
1029           .name = "SMBus quick command" },
1030         { .value = I2C_FUNC_SMBUS_WRITE_BYTE,
1031           .name = "SMBus send byte" },
1032         { .value = I2C_FUNC_SMBUS_READ_BYTE,
1033           .name = "SMBus receive byte" },
1034         { .value = I2C_FUNC_SMBUS_WRITE_BYTE_DATA,
1035           .name = "SMBus write byte" },
1036         { .value = I2C_FUNC_SMBUS_READ_BYTE_DATA,
1037           .name = "SMBus read byte" },
1038         { .value = I2C_FUNC_SMBUS_WRITE_WORD_DATA,
1039           .name = "SMBus write word" },
1040         { .value = I2C_FUNC_SMBUS_READ_WORD_DATA,
1041           .name = "SMBus read word" },
1042         { .value = I2C_FUNC_SMBUS_PROC_CALL,
1043           .name = "SMBus process call" },
1044         { .value = I2C_FUNC_SMBUS_WRITE_BLOCK_DATA,
1045           .name = "SMBus block write" },
1046         { .value = I2C_FUNC_SMBUS_READ_BLOCK_DATA,
1047           .name = "SMBus block read" },
1048         { .value = I2C_FUNC_SMBUS_BLOCK_PROC_CALL,
1049           .name = "SMBus block process call" },
1050         { .value = I2C_FUNC_SMBUS_PEC,
1051           .name = "SMBus PEC" },
1052         { .value = I2C_FUNC_SMBUS_WRITE_I2C_BLOCK,
1053           .name = "I2C block write" },
1054         { .value = I2C_FUNC_SMBUS_READ_I2C_BLOCK,
1055           .name = "I2C block read" },
1056         { .value = 0, .name = NULL }
1057 };
1058
1059 static enum adapter_type i2cdetect_get_funcs(int bus)
1060 {
1061         enum adapter_type ret;
1062         unsigned long funcs;
1063         int fd;
1064
1065         fd = i2c_dev_open(bus);
1066
1067         get_funcs_matrix(fd, &funcs);
1068         if (funcs & I2C_FUNC_I2C)
1069                 ret = ADT_I2C;
1070         else if (funcs & (I2C_FUNC_SMBUS_BYTE |
1071                           I2C_FUNC_SMBUS_BYTE_DATA |
1072                           I2C_FUNC_SMBUS_WORD_DATA))
1073                 ret = ADT_SMBUS;
1074         else
1075                 ret = ADT_DUMMY;
1076
1077         close(fd);
1078
1079         return ret;
1080 }
1081
1082 static void NORETURN list_i2c_busses_and_exit(void)
1083 {
1084         const char *const i2cdev_path = "/sys/class/i2c-dev";
1085
1086         char path[NAME_MAX], name[128];
1087         struct dirent *de, *subde;
1088         enum adapter_type adt;
1089         DIR *dir, *subdir;
1090         int rv, bus;
1091         char *pos;
1092         FILE *fp;
1093
1094         /*
1095          * XXX Upstream i2cdetect also looks for i2c bus info in /proc/bus/i2c,
1096          * but we won't bother since it's only useful on older kernels (before
1097          * 2.6.5). We expect sysfs to be present and mounted at /sys/.
1098          */
1099
1100         dir = xopendir(i2cdev_path);
1101         while ((de = readdir(dir))) {
1102                 if (de->d_name[0] == '.')
1103                         continue;
1104
1105                 /* Simple version for ISA chips. */
1106                 snprintf(path, NAME_MAX, "%s/%s/name",
1107                          i2cdev_path, de->d_name);
1108                 fp = fopen(path, "r");
1109                 if (fp == NULL) {
1110                         snprintf(path, NAME_MAX,
1111                                  "%s/%s/device/name",
1112                                  i2cdev_path, de->d_name);
1113                         fp = fopen(path, "r");
1114                 }
1115
1116                 /* Non-ISA chips require the hard-way. */
1117                 if (fp == NULL) {
1118                         snprintf(path, NAME_MAX,
1119                                  "%s/%s/device/name",
1120                                  i2cdev_path, de->d_name);
1121                         subdir = opendir(path);
1122                         if (subdir == NULL)
1123                                 continue;
1124
1125                         while ((subde = readdir(subdir))) {
1126                                 if (subde->d_name[0] == '.')
1127                                         continue;
1128
1129                                 if (is_prefixed_with(subde->d_name, "i2c-")) {
1130                                         snprintf(path, NAME_MAX,
1131                                                  "%s/%s/device/%s/name",
1132                                                  i2cdev_path, de->d_name,
1133                                                  subde->d_name);
1134                                         fp = fopen(path, "r");
1135                                         break;
1136                                 }
1137                         }
1138                 }
1139
1140                 if (fp != NULL) {
1141                         /*
1142                          * Get the rest of the info and display a line
1143                          * for a single bus.
1144                          */
1145                         memset(name, 0, sizeof(name));
1146                         pos = fgets(name, sizeof(name), fp);
1147                         fclose(fp);
1148                         if (pos == NULL)
1149                                 continue;
1150
1151                         pos = strchr(name, '\n');
1152                         if (pos != NULL)
1153                                 *pos = '\0';
1154
1155                         rv = sscanf(de->d_name, "i2c-%d", &bus);
1156                         if (rv != 1)
1157                                 continue;
1158
1159                         if (is_prefixed_with(name, "ISA"))
1160                                 adt = ADT_ISA;
1161                         else
1162                                 adt = i2cdetect_get_funcs(bus);
1163
1164                         printf(
1165                                 "i2c-%d\t%-10s\t%-32s\t%s\n",
1166                                 bus, adap_descs[adt].funcs,
1167                                 name, adap_descs[adt].algo);
1168                 }
1169         }
1170
1171         exit(EXIT_SUCCESS);
1172 }
1173
1174 static void NORETURN no_support(const char *cmd)
1175 {
1176         bb_error_msg_and_die("bus doesn't support %s", cmd);
1177 }
1178
1179 static void will_skip(const char *cmd)
1180 {
1181         bb_error_msg(
1182                 "warning: can't use %s command, "
1183                 "will skip some addresses", cmd);
1184 }
1185
1186 //usage:#define i2cdetect_trivial_usage
1187 //usage:       "[-F I2CBUS] [-l] [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]"
1188 //usage:#define i2cdetect_full_usage "\n\n"
1189 //usage:       "Detect I2C chips.\n"
1190 //usage:     "\n        I2CBUS  i2c bus number"
1191 //usage:     "\n        FIRST and LAST limit the probing range"
1192 //usage:     "\n"
1193 //usage:     "\n        -l      output list of installed busses"
1194 //usage:     "\n        -y      disable interactive mode"
1195 //usage:     "\n        -a      force scanning of non-regular addresses"
1196 //usage:     "\n        -q      use smbus quick write commands for probing (default)"
1197 //usage:     "\n        -r      use smbus read byte commands for probing"
1198 //usage:     "\n        -F      display list of functionalities"
1199 int i2cdetect_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1200 int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
1201 {
1202         const unsigned opt_y = (1 << 0), opt_a = (1 << 1),
1203                               opt_q = (1 << 2), opt_r = (1 << 3),
1204                               opt_F = (1 << 4), opt_l = (1 << 5);
1205         const char *const optstr = "yaqrFl";
1206
1207         int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status, cmd;
1208         unsigned first = 0x03, last = 0x77, opts;
1209         unsigned long funcs;
1210
1211         opt_complementary = "q--r:r--q:" /* mutually exclusive */
1212                         "?3"; /* up to 3 args */
1213         opts = getopt32(argv, optstr);
1214         argv += optind;
1215
1216         if (opts & opt_l)
1217                 list_i2c_busses_and_exit();
1218
1219         if (!argv[0])
1220                 bb_show_usage();
1221
1222         bus_num = i2c_bus_lookup(argv[0]);
1223         fd = i2c_dev_open(bus_num);
1224         get_funcs_matrix(fd, &funcs);
1225
1226         if (opts & opt_F) {
1227                 /* Only list the functionalities. */
1228                 printf("Functionalities implemented by bus #%d\n", bus_num);
1229                 for (i = 0; i2c_funcs_tab[i].value; i++) {
1230                         printf("%-32s %s\n", i2c_funcs_tab[i].name,
1231                                funcs & i2c_funcs_tab[i].value ? "yes" : "no");
1232                 }
1233
1234                 return EXIT_SUCCESS;
1235         }
1236
1237         if (opts & opt_r)
1238                 mode = I2CDETECT_MODE_READ;
1239         else if (opts & opt_q)
1240                 mode = I2CDETECT_MODE_QUICK;
1241
1242         if (opts & opt_a) {
1243                 first = 0x00;
1244                 last = 0x7f;
1245         }
1246
1247         /* Read address range. */
1248         if (argv[1]) {
1249                 first = xstrtou_range(argv[1], 16, first, last);
1250                 if (argv[2])
1251                         last = xstrtou_range(argv[2], 16, first, last);
1252         }
1253
1254         if (!(funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE))) {
1255                 no_support("detection commands");
1256         } else
1257         if (mode == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)) {
1258                 no_support("SMBus quick write");
1259         } else
1260         if (mode == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
1261                 no_support("SMBus receive byte");
1262         }
1263
1264         if (mode == I2CDETECT_MODE_AUTO) {
1265                 if (!(funcs & I2C_FUNC_SMBUS_QUICK))
1266                         will_skip("SMBus quick write");
1267                 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1268                         will_skip("SMBus receive byte");
1269         }
1270
1271         if (!(opts & opt_y))
1272                 confirm_action(-1, -1, -1, 0);
1273
1274         puts("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f");
1275         for (i = 0; i < 128; i += 16) {
1276                 printf("%02x: ", i);
1277                 for (j = 0; j < 16; j++) {
1278                         fflush_all();
1279
1280                         cmd = mode;
1281                         if (mode == I2CDETECT_MODE_AUTO) {
1282                                 if ((i+j >= 0x30 && i+j <= 0x37) ||
1283                                     (i+j >= 0x50 && i+j <= 0x5F))
1284                                         cmd = I2CDETECT_MODE_READ;
1285                                 else
1286                                         cmd = I2CDETECT_MODE_QUICK;
1287                         }
1288
1289                         /* Skip unwanted addresses. */
1290                         if (i+j < first
1291                          || i+j > last
1292                          || (cmd == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1293                          || (cmd == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
1294                         {
1295                                 printf("   ");
1296                                 continue;
1297                         }
1298
1299                         status = ioctl(fd, I2C_SLAVE, itoptr(i + j));
1300                         if (status < 0) {
1301                                 if (errno == EBUSY) {
1302                                         printf("UU ");
1303                                         continue;
1304                                 }
1305
1306                                 bb_perror_msg_and_die(
1307                                         "can't set address to 0x%02x", i + j);
1308                         }
1309
1310                         switch (cmd) {
1311                         case I2CDETECT_MODE_READ:
1312                                 /*
1313                                  * This is known to lock SMBus on various
1314                                  * write-only chips (mainly clock chips).
1315                                  */
1316                                 status = i2c_smbus_read_byte(fd);
1317                                 break;
1318                         default: /* I2CDETECT_MODE_QUICK: */
1319                                 /*
1320                                  * This is known to corrupt the Atmel
1321                                  * AT24RF08 EEPROM.
1322                                  */
1323                                 status = i2c_smbus_write_quick(fd,
1324                                                                I2C_SMBUS_WRITE);
1325                                 break;
1326                         }
1327
1328                         if (status < 0)
1329                                 printf("-- ");
1330                         else
1331                                 printf("%02x ", i+j);
1332                 }
1333                 bb_putchar('\n');
1334         }
1335
1336         return 0;
1337 }
1338 #endif /* ENABLE_I2CDETECT */