blob: 6d221e9fc30f5a3fbb168c071bd0103014cf2592 [file] [log] [blame]
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001/* 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
Bartosz Golaszewskid9892fa2015-09-04 10:35:22 +020064#include <linux/i2c.h>
65#include <linux/i2c-dev.h>
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010066
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +020067#define I2CDUMP_NUM_REGS 256
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010068
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +020069#define I2CDETECT_MODE_AUTO 0
70#define I2CDETECT_MODE_QUICK 1
71#define I2CDETECT_MODE_READ 2
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010072
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010073/*
74 * This is needed for ioctl_or_perror_and_die() since it only accepts pointers.
75 */
76static ALWAYS_INLINE void *itoptr(int i)
77{
78 return (void*)(intptr_t)i;
79}
80
81static 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
94static 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
107static 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
113static 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
126static 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
141static 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
152static 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
162static 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
177static 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 */
198static 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
213static 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
236static 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
242static 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
248static 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
254static 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}
Bartosz Golaszewski31474272015-06-05 10:27:28 +0200260
261static 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}
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100267#endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
268
269#if ENABLE_I2CGET || ENABLE_I2CSET
270static 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 */
283static 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) {
Bartosz Golaszewski7ca5c512015-05-11 17:26:27 +0200291 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 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100297 }
298
299 return fd;
300}
301
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100302/* Size reducing helpers for xxx_check_funcs(). */
303static 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
310static 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
326static 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:
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200361 bb_error_msg_and_die("internal error");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100362 }
363 check_funcs_test_end(funcs, pec, err);
364}
365#endif /* ENABLE_I2CGET || ENABLE_I2CDUMP */
366
367#if ENABLE_I2CSET
368static 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
403static 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 */
417static 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"
452int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
453int 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"
540int i2cset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
541int 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) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200704 puts("Warning - readback failed");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100705 } 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
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200721static int read_block_data(int buf_fd, int mode, int *block)
722{
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200723 uint8_t cblock[I2C_SMBUS_BLOCK_MAX + I2CDUMP_NUM_REGS];
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200724 int res, blen = 0, tmp, i;
725
Bartosz Golaszewski1fe75b82015-10-27 17:15:03 +0100726 if (mode == I2C_SMBUS_BLOCK_DATA) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200727 res = i2c_smbus_read_block_data(buf_fd, 0, cblock);
728 blen = res;
729 } else {
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200730 for (res = 0; res < I2CDUMP_NUM_REGS; res += tmp) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200731 tmp = i2c_smbus_read_i2c_block_data(
732 buf_fd, res, I2C_SMBUS_BLOCK_MAX,
733 cblock + res);
734 if (tmp < 0) {
735 bb_error_msg_and_die("block read failed");
736 }
737 }
738
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200739 if (res >= I2CDUMP_NUM_REGS)
740 res = I2CDUMP_NUM_REGS;
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200741
742 for (i = 0; i < res; i++)
743 block[i] = cblock[i];
744
745 if (mode != I2C_SMBUS_BLOCK_DATA)
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200746 for (i = res; i < I2CDUMP_NUM_REGS; i++)
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200747 block[i] = -1;
748 }
749
750 return blen;
751}
752
753/* Dump all but word data. */
754static void dump_data(int bus_fd, int mode, unsigned first,
755 unsigned last, int *block, int blen)
756{
757 int i, j, res;
758
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200759 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
760 " 0123456789abcdef");
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200761
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200762 for (i = 0; i < I2CDUMP_NUM_REGS; i += 0x10) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200763 if (mode == I2C_SMBUS_BLOCK_DATA && i >= blen)
764 break;
765 if (i/16 < first/16)
766 continue;
767 if (i/16 > last/16)
768 break;
769
770 printf("%02x: ", i);
771 for (j = 0; j < 16; j++) {
772 fflush_all();
773 /* Skip unwanted registers */
774 if (i+j < first || i+j > last) {
775 printf(" ");
776 if (mode == I2C_SMBUS_WORD_DATA) {
777 printf(" ");
778 j++;
779 }
780 continue;
781 }
782
783 switch (mode) {
784 case I2C_SMBUS_BYTE_DATA:
785 res = i2c_smbus_read_byte_data(bus_fd, i+j);
786 block[i+j] = res;
787 break;
788 case I2C_SMBUS_WORD_DATA:
789 res = i2c_smbus_read_word_data(bus_fd, i+j);
790 if (res < 0) {
791 block[i+j] = res;
792 block[i+j+1] = res;
793 } else {
794 block[i+j] = res & 0xff;
795 block[i+j+1] = res >> 8;
796 }
797 break;
798 case I2C_SMBUS_BYTE:
799 res = i2c_smbus_read_byte(bus_fd);
800 block[i+j] = res;
801 break;
802 default:
803 res = block[i+j];
804 }
805
806 if (mode == I2C_SMBUS_BLOCK_DATA &&
807 i+j >= blen) {
808 printf(" ");
809 } else if (res < 0) {
810 printf("XX ");
811 if (mode == I2C_SMBUS_WORD_DATA)
812 printf("XX ");
813 } else {
814 printf("%02x ", block[i+j]);
815 if (mode == I2C_SMBUS_WORD_DATA)
816 printf("%02x ", block[i+j+1]);
817 }
818
819 if (mode == I2C_SMBUS_WORD_DATA)
820 j++;
821 }
822 printf(" ");
823
824 for (j = 0; j < 16; j++) {
825 if (mode == I2C_SMBUS_BLOCK_DATA && i+j >= blen)
826 break;
827 /* Skip unwanted registers */
828 if (i+j < first || i+j > last) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200829 bb_putchar(' ');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200830 continue;
831 }
832
833 res = block[i+j];
834 if (res < 0) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200835 bb_putchar('X');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200836 } else if (res == 0x00 || res == 0xff) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200837 bb_putchar('.');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200838 } else if (res < 32 || res >= 127) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200839 bb_putchar('?');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200840 } else {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200841 bb_putchar(res);
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200842 }
843 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200844 bb_putchar('\n');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200845 }
846}
847
848static void dump_word_data(int bus_fd, unsigned first, unsigned last)
849{
850 int i, j, rv;
851
852 /* Word data. */
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200853 puts(" 0,8 1,9 2,a 3,b 4,c 5,d 6,e 7,f");
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200854 for (i = 0; i < 256; i += 8) {
855 if (i/8 < first/8)
856 continue;
857 if (i/8 > last/8)
858 break;
859
860 printf("%02x: ", i);
861 for (j = 0; j < 8; j++) {
862 /* Skip unwanted registers. */
863 if (i+j < first || i+j > last) {
864 printf(" ");
865 continue;
866 }
867
868 rv = i2c_smbus_read_word_data(bus_fd, i+j);
869 if (rv < 0)
870 printf("XXXX ");
871 else
872 printf("%04x ", rv & 0xffff);
873 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200874 bb_putchar('\n');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200875 }
876}
877
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100878//usage:#define i2cdump_trivial_usage
879//usage: "[-f] [-r FIRST-LAST] [-y] BUS ADDR [MODE]"
880//usage:#define i2cdump_full_usage "\n\n"
881//usage: "Examine I2C registers\n"
882//usage: "\n I2CBUS i2c bus number"
883//usage: "\n ADDRESS 0x03 - 0x77"
884//usage: "\nMODE is:"
885//usage: "\n b byte (default)"
886//usage: "\n w word"
887//usage: "\n W word on even register addresses"
888//usage: "\n i I2C block"
889//usage: "\n s SMBus block"
890//usage: "\n c consecutive byte"
891//usage: "\n Append p for SMBus PEC"
892//usage: "\n"
893//usage: "\n -f force access"
894//usage: "\n -y disable interactive mode"
895//usage: "\n -r limit the number of registers being accessed"
896int i2cdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
897int i2cdump_main(int argc UNUSED_PARAM, char **argv)
898{
899 const unsigned opt_f = (1 << 0), opt_y = (1 << 1),
900 opt_r = (1 << 2);
901 const char *const optstr = "fyr:";
902
903 int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0;
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200904 unsigned first = 0x00, last = 0xff, opts;
905 int *block = (int *)bb_common_bufsiz1;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100906 char *opt_r_str, *dash;
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200907 int fd, res, blen;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100908
909 opt_complementary = "-2:?3"; /* from 2 to 3 args */
910 opts = getopt32(argv, optstr, &opt_r_str);
911 argv += optind;
912
913 bus_num = i2c_bus_lookup(argv[0]);
914 bus_addr = i2c_parse_bus_addr(argv[1]);
915
916 if (argv[2]) {
917 switch (argv[2][0]) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200918 case 'b': /* Already set. */ break;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100919 case 'c': mode = I2C_SMBUS_BYTE; break;
920 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
921 case 'W':
922 mode = I2C_SMBUS_WORD_DATA;
923 even = 1;
924 break;
925 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
926 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
927 default:
928 bb_error_msg_and_die("invalid mode");
929 }
930
931 if (argv[2][1] == 'p') {
932 if (argv[2][0] == 'W' || argv[2][0] == 'i') {
933 bb_error_msg_and_die(
934 "pec not supported for -W and -i");
935 } else {
936 pec = 1;
937 }
938 }
939 }
940
941 if (opts & opt_r) {
942 first = strtol(opt_r_str, &dash, 0);
943 if (dash == opt_r_str || *dash != '-' || first > 0xff)
944 bb_error_msg_and_die("invalid range");
945 last = xstrtou_range(++dash, 0, first, 0xff);
946
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200947 /* Range is not available for every mode. */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100948 switch (mode) {
949 case I2C_SMBUS_BYTE:
950 case I2C_SMBUS_BYTE_DATA:
951 break;
952 case I2C_SMBUS_WORD_DATA:
953 if (!even || (!(first % 2) && last % 2))
954 break;
955 /* Fall through */
956 default:
957 bb_error_msg_and_die(
958 "range not compatible with selected mode");
959 }
960 }
961
962 fd = i2c_dev_open(bus_num);
963 check_read_funcs(fd, mode, -1 /* data_addr */, pec);
964 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
965
966 if (pec)
967 i2c_set_pec(fd, 1);
968
969 if (!(opts & opt_y))
970 confirm_action(bus_addr, mode, -1 /* data_addr */, pec);
971
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200972 /* All but word data. */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100973 if (mode != I2C_SMBUS_WORD_DATA || even) {
Bartosz Golaszewski1cde5f72015-10-27 17:15:02 +0100974 if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA)
975 blen = read_block_data(fd, mode, block);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100976
977 if (mode == I2C_SMBUS_BYTE) {
978 res = i2c_smbus_write_byte(fd, first);
979 if (res < 0)
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200980 bb_perror_msg_and_die("write start address");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100981 }
982
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200983 dump_data(fd, mode, first, last, block, blen);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100984 } else {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200985 dump_word_data(fd, first, last);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100986 }
987
988 return 0;
989}
990#endif /* ENABLE_I2CDUMP */
991
992#if ENABLE_I2CDETECT
993enum adapter_type {
994 ADT_DUMMY = 0,
995 ADT_ISA,
996 ADT_I2C,
997 ADT_SMBUS,
998};
999
1000struct adap_desc {
1001 const char *funcs;
1002 const char *algo;
1003};
1004
1005static const struct adap_desc adap_descs[] = {
1006 { .funcs = "dummy",
1007 .algo = "Dummy bus", },
1008 { .funcs = "isa",
1009 .algo = "ISA bus", },
1010 { .funcs = "i2c",
1011 .algo = "I2C adapter", },
1012 { .funcs = "smbus",
1013 .algo = "SMBus adapter", },
1014};
1015
1016struct i2c_func
1017{
1018 long value;
1019 const char* name;
1020};
1021
1022static const struct i2c_func i2c_funcs_tab[] = {
1023 { .value = I2C_FUNC_I2C,
1024 .name = "I2C" },
1025 { .value = I2C_FUNC_SMBUS_QUICK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001026 .name = "SMBus quick command" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001027 { .value = I2C_FUNC_SMBUS_WRITE_BYTE,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001028 .name = "SMBus send byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001029 { .value = I2C_FUNC_SMBUS_READ_BYTE,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001030 .name = "SMBus receive byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001031 { .value = I2C_FUNC_SMBUS_WRITE_BYTE_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001032 .name = "SMBus write byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001033 { .value = I2C_FUNC_SMBUS_READ_BYTE_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001034 .name = "SMBus read byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001035 { .value = I2C_FUNC_SMBUS_WRITE_WORD_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001036 .name = "SMBus write word" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001037 { .value = I2C_FUNC_SMBUS_READ_WORD_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001038 .name = "SMBus read word" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001039 { .value = I2C_FUNC_SMBUS_PROC_CALL,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001040 .name = "SMBus process call" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001041 { .value = I2C_FUNC_SMBUS_WRITE_BLOCK_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001042 .name = "SMBus block write" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001043 { .value = I2C_FUNC_SMBUS_READ_BLOCK_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001044 .name = "SMBus block read" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001045 { .value = I2C_FUNC_SMBUS_BLOCK_PROC_CALL,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001046 .name = "SMBus block process call" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001047 { .value = I2C_FUNC_SMBUS_PEC,
1048 .name = "SMBus PEC" },
1049 { .value = I2C_FUNC_SMBUS_WRITE_I2C_BLOCK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001050 .name = "I2C block write" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001051 { .value = I2C_FUNC_SMBUS_READ_I2C_BLOCK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001052 .name = "I2C block read" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001053 { .value = 0, .name = NULL }
1054};
1055
1056static enum adapter_type i2cdetect_get_funcs(int bus)
1057{
1058 enum adapter_type ret;
1059 unsigned long funcs;
1060 int fd;
1061
1062 fd = i2c_dev_open(bus);
1063
1064 get_funcs_matrix(fd, &funcs);
1065 if (funcs & I2C_FUNC_I2C)
1066 ret = ADT_I2C;
1067 else if (funcs & (I2C_FUNC_SMBUS_BYTE |
1068 I2C_FUNC_SMBUS_BYTE_DATA |
1069 I2C_FUNC_SMBUS_WORD_DATA))
1070 ret = ADT_SMBUS;
1071 else
1072 ret = ADT_DUMMY;
1073
1074 close(fd);
1075
1076 return ret;
1077}
1078
1079static void NORETURN list_i2c_busses_and_exit(void)
1080{
1081 const char *const i2cdev_path = "/sys/class/i2c-dev";
1082
1083 char path[NAME_MAX], name[128];
1084 struct dirent *de, *subde;
1085 enum adapter_type adt;
1086 DIR *dir, *subdir;
1087 int rv, bus;
1088 char *pos;
1089 FILE *fp;
1090
1091 /*
1092 * XXX Upstream i2cdetect also looks for i2c bus info in /proc/bus/i2c,
1093 * but we won't bother since it's only useful on older kernels (before
1094 * 2.6.5). We expect sysfs to be present and mounted at /sys/.
1095 */
1096
1097 dir = xopendir(i2cdev_path);
1098 while ((de = readdir(dir))) {
1099 if (de->d_name[0] == '.')
1100 continue;
1101
1102 /* Simple version for ISA chips. */
1103 snprintf(path, NAME_MAX, "%s/%s/name",
1104 i2cdev_path, de->d_name);
1105 fp = fopen(path, "r");
1106 if (fp == NULL) {
1107 snprintf(path, NAME_MAX,
1108 "%s/%s/device/name",
1109 i2cdev_path, de->d_name);
1110 fp = fopen(path, "r");
1111 }
1112
1113 /* Non-ISA chips require the hard-way. */
1114 if (fp == NULL) {
1115 snprintf(path, NAME_MAX,
1116 "%s/%s/device/name",
1117 i2cdev_path, de->d_name);
1118 subdir = opendir(path);
1119 if (subdir == NULL)
1120 continue;
1121
1122 while ((subde = readdir(subdir))) {
1123 if (subde->d_name[0] == '.')
1124 continue;
1125
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001126 if (is_prefixed_with(subde->d_name, "i2c-")) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001127 snprintf(path, NAME_MAX,
1128 "%s/%s/device/%s/name",
1129 i2cdev_path, de->d_name,
1130 subde->d_name);
1131 fp = fopen(path, "r");
Bartosz Golaszewskid9089922015-06-05 10:27:32 +02001132 break;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001133 }
1134 }
1135 }
1136
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001137 if (fp != NULL) {
1138 /*
1139 * Get the rest of the info and display a line
1140 * for a single bus.
1141 */
1142 memset(name, 0, sizeof(name));
1143 pos = fgets(name, sizeof(name), fp);
1144 fclose(fp);
1145 if (pos == NULL)
1146 continue;
1147
1148 pos = strchr(name, '\n');
1149 if (pos != NULL)
1150 *pos = '\0';
1151
1152 rv = sscanf(de->d_name, "i2c-%d", &bus);
1153 if (rv != 1)
1154 continue;
1155
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001156 if (is_prefixed_with(name, "ISA"))
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001157 adt = ADT_ISA;
1158 else
1159 adt = i2cdetect_get_funcs(bus);
1160
1161 printf(
1162 "i2c-%d\t%-10s\t%-32s\t%s\n",
1163 bus, adap_descs[adt].funcs,
1164 name, adap_descs[adt].algo);
1165 }
1166 }
1167
1168 exit(EXIT_SUCCESS);
1169}
1170
1171static void NORETURN no_support(const char *cmd)
1172{
1173 bb_error_msg_and_die("bus doesn't support %s", cmd);
1174}
1175
1176static void will_skip(const char *cmd)
1177{
1178 bb_error_msg(
1179 "warning: can't use %s command, "
1180 "will skip some addresses", cmd);
1181}
1182
1183//usage:#define i2cdetect_trivial_usage
1184//usage: "[-F I2CBUS] [-l] [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]"
1185//usage:#define i2cdetect_full_usage "\n\n"
1186//usage: "Detect I2C chips.\n"
1187//usage: "\n I2CBUS i2c bus number"
1188//usage: "\n FIRST and LAST limit the probing range"
1189//usage: "\n"
1190//usage: "\n -l output list of installed busses"
1191//usage: "\n -y disable interactive mode"
1192//usage: "\n -a force scanning of non-regular addresses"
1193//usage: "\n -q use smbus quick write commands for probing (default)"
1194//usage: "\n -r use smbus read byte commands for probing"
1195//usage: "\n -F display list of functionalities"
1196int i2cdetect_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1197int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
1198{
1199 const unsigned opt_y = (1 << 0), opt_a = (1 << 1),
1200 opt_q = (1 << 2), opt_r = (1 << 3),
1201 opt_F = (1 << 4), opt_l = (1 << 5);
1202 const char *const optstr = "yaqrFl";
1203
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001204 int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status, cmd;
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001205 unsigned first = 0x03, last = 0x77, opts;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001206 unsigned long funcs;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001207
1208 opt_complementary = "q--r:r--q:" /* mutually exclusive */
Bartosz Golaszewskib2cca322015-05-11 17:22:10 +02001209 "?3"; /* up to 3 args */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001210 opts = getopt32(argv, optstr);
1211 argv += optind;
1212
1213 if (opts & opt_l)
1214 list_i2c_busses_and_exit();
1215
Bartosz Golaszewskib2cca322015-05-11 17:22:10 +02001216 if (!argv[0])
1217 bb_show_usage();
1218
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001219 bus_num = i2c_bus_lookup(argv[0]);
1220 fd = i2c_dev_open(bus_num);
1221 get_funcs_matrix(fd, &funcs);
1222
1223 if (opts & opt_F) {
1224 /* Only list the functionalities. */
1225 printf("Functionalities implemented by bus #%d\n", bus_num);
1226 for (i = 0; i2c_funcs_tab[i].value; i++) {
1227 printf("%-32s %s\n", i2c_funcs_tab[i].name,
1228 funcs & i2c_funcs_tab[i].value ? "yes" : "no");
1229 }
1230
1231 return EXIT_SUCCESS;
1232 }
1233
1234 if (opts & opt_r)
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001235 mode = I2CDETECT_MODE_READ;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001236 else if (opts & opt_q)
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001237 mode = I2CDETECT_MODE_QUICK;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001238
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001239 if (opts & opt_a) {
1240 first = 0x00;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001241 last = 0x7f;
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001242 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001243
1244 /* Read address range. */
1245 if (argv[1]) {
1246 first = xstrtou_range(argv[1], 16, first, last);
1247 if (argv[2])
1248 last = xstrtou_range(argv[2], 16, first, last);
1249 }
1250
1251 if (!(funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE))) {
1252 no_support("detection commands");
1253 } else
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001254 if (mode == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001255 no_support("SMBus quick write");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001256 } else
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001257 if (mode == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001258 no_support("SMBus receive byte");
Bartosz Golaszewski22044722015-06-05 10:27:30 +02001259 }
1260
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001261 if (mode == I2CDETECT_MODE_AUTO) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001262 if (!(funcs & I2C_FUNC_SMBUS_QUICK))
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001263 will_skip("SMBus quick write");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001264 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE))
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001265 will_skip("SMBus receive byte");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001266 }
1267
1268 if (!(opts & opt_y))
1269 confirm_action(-1, -1, -1, 0);
1270
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001271 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001272 for (i = 0; i < 128; i += 16) {
1273 printf("%02x: ", i);
Bartosz Golaszewskifc8eb052015-10-27 17:15:01 +01001274 for (j = 0; j < 16; j++) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001275 fflush_all();
1276
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001277 cmd = mode;
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001278 if (mode == I2CDETECT_MODE_AUTO) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001279 if ((i+j >= 0x30 && i+j <= 0x37) ||
1280 (i+j >= 0x50 && i+j <= 0x5F))
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001281 cmd = I2CDETECT_MODE_READ;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001282 else
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001283 cmd = I2CDETECT_MODE_QUICK;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001284 }
1285
1286 /* Skip unwanted addresses. */
1287 if (i+j < first
1288 || i+j > last
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001289 || (cmd == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1290 || (cmd == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001291 {
1292 printf(" ");
1293 continue;
1294 }
1295
Bartosz Golaszewski31474272015-06-05 10:27:28 +02001296 status = ioctl(fd, I2C_SLAVE, itoptr(i + j));
1297 if (status < 0) {
1298 if (errno == EBUSY) {
1299 printf("UU ");
1300 continue;
1301 }
1302
1303 bb_perror_msg_and_die(
1304 "can't set address to 0x%02x", i + j);
1305 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001306
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001307 switch (cmd) {
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001308 case I2CDETECT_MODE_READ:
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001309 /*
1310 * This is known to lock SMBus on various
1311 * write-only chips (mainly clock chips).
1312 */
1313 status = i2c_smbus_read_byte(fd);
1314 break;
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001315 default: /* I2CDETECT_MODE_QUICK: */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001316 /*
1317 * This is known to corrupt the Atmel
1318 * AT24RF08 EEPROM.
1319 */
1320 status = i2c_smbus_write_quick(fd,
1321 I2C_SMBUS_WRITE);
1322 break;
1323 }
1324
1325 if (status < 0)
1326 printf("-- ");
1327 else
1328 printf("%02x ", i+j);
1329 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001330 bb_putchar('\n');
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001331 }
1332
1333 return 0;
1334}
1335#endif /* ENABLE_I2CDETECT */