blob: 30f606e8e7933f3eb63ba131302d5d13d0b865d8 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "i2cget (5.6 kb)"
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010014//config: default y
15//config: select PLATFORM_LINUX
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: Read from I2C/SMBus chip registers.
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010018//config:
19//config:config I2CSET
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020020//config: bool "i2cset (6.9 kb)"
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010021//config: default y
22//config: select PLATFORM_LINUX
23//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020024//config: Set I2C registers.
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010025//config:
26//config:config I2CDUMP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020027//config: bool "i2cdump (7.2 kb)"
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010028//config: default y
29//config: select PLATFORM_LINUX
30//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//config: Examine I2C registers.
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010032//config:
33//config:config I2CDETECT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020034//config: bool "i2cdetect (7.2 kb)"
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010035//config: default y
36//config: select PLATFORM_LINUX
37//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020038//config: Detect I2C chips.
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010039//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))
Denys Vlasenko1a1203f2017-08-07 16:47:34 +020045/* not NOEXEC: if hw operation stalls, use less memory in "hung" process */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010046
47//kbuild:lib-$(CONFIG_I2CGET) += i2c_tools.o
48//kbuild:lib-$(CONFIG_I2CSET) += i2c_tools.o
49//kbuild:lib-$(CONFIG_I2CDUMP) += i2c_tools.o
50//kbuild:lib-$(CONFIG_I2CDETECT) += i2c_tools.o
51
52/*
53 * Unsupported stuff:
54 *
55 * - upstream i2c-tools can also look-up i2c busses by name, we only accept
56 * numbers,
57 * - bank and bankreg parameters for i2cdump are not supported because of
58 * their limited usefulness (see i2cdump manual entry for more info),
59 * - i2cdetect doesn't look for bus info in /proc as it does in upstream, but
60 * it shouldn't be a problem in modern kernels.
61 */
62
63#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020064#include "common_bufsiz.h"
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010065
Bartosz Golaszewskid9892fa2015-09-04 10:35:22 +020066#include <linux/i2c.h>
67#include <linux/i2c-dev.h>
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010068
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +020069#define I2CDUMP_NUM_REGS 256
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010070
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +020071#define I2CDETECT_MODE_AUTO 0
72#define I2CDETECT_MODE_QUICK 1
73#define I2CDETECT_MODE_READ 2
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010074
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +010075/*
76 * This is needed for ioctl_or_perror_and_die() since it only accepts pointers.
77 */
78static ALWAYS_INLINE void *itoptr(int i)
79{
80 return (void*)(intptr_t)i;
81}
82
83static int32_t i2c_smbus_access(int fd, char read_write, uint8_t cmd,
84 int size, union i2c_smbus_data *data)
85{
86 struct i2c_smbus_ioctl_data args;
87
88 args.read_write = read_write;
89 args.command = cmd;
90 args.size = size;
91 args.data = data;
92
93 return ioctl(fd, I2C_SMBUS, &args);
94}
95
96static int32_t i2c_smbus_read_byte(int fd)
97{
98 union i2c_smbus_data data;
99 int err;
100
101 err = i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data);
102 if (err < 0)
103 return err;
104
105 return data.byte;
106}
107
108#if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
109static int32_t i2c_smbus_write_byte(int fd, uint8_t val)
110{
111 return i2c_smbus_access(fd, I2C_SMBUS_WRITE,
112 val, I2C_SMBUS_BYTE, NULL);
113}
114
115static int32_t i2c_smbus_read_byte_data(int fd, uint8_t cmd)
116{
117 union i2c_smbus_data data;
118 int err;
119
120 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
121 I2C_SMBUS_BYTE_DATA, &data);
122 if (err < 0)
123 return err;
124
125 return data.byte;
126}
127
128static int32_t i2c_smbus_read_word_data(int fd, uint8_t cmd)
129{
130 union i2c_smbus_data data;
131 int err;
132
133 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
134 I2C_SMBUS_WORD_DATA, &data);
135 if (err < 0)
136 return err;
137
138 return data.word;
139}
140#endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
141
142#if ENABLE_I2CSET
143static int32_t i2c_smbus_write_byte_data(int file,
144 uint8_t cmd, uint8_t value)
145{
146 union i2c_smbus_data data;
147
148 data.byte = value;
149
150 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
151 I2C_SMBUS_BYTE_DATA, &data);
152}
153
154static int32_t i2c_smbus_write_word_data(int file, uint8_t cmd, uint16_t value)
155{
156 union i2c_smbus_data data;
157
158 data.word = value;
159
160 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
161 I2C_SMBUS_WORD_DATA, &data);
162}
163
164static int32_t i2c_smbus_write_block_data(int file, uint8_t cmd,
165 uint8_t length, const uint8_t *values)
166{
167 union i2c_smbus_data data;
168
169 if (length > I2C_SMBUS_BLOCK_MAX)
170 length = I2C_SMBUS_BLOCK_MAX;
171
172 memcpy(data.block+1, values, length);
173 data.block[0] = length;
174
175 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
176 I2C_SMBUS_BLOCK_DATA, &data);
177}
178
179static int32_t i2c_smbus_write_i2c_block_data(int file, uint8_t cmd,
180 uint8_t length, const uint8_t *values)
181{
182 union i2c_smbus_data data;
183
184 if (length > I2C_SMBUS_BLOCK_MAX)
185 length = I2C_SMBUS_BLOCK_MAX;
186
187 memcpy(data.block+1, values, length);
188 data.block[0] = length;
189
190 return i2c_smbus_access(file, I2C_SMBUS_WRITE, cmd,
191 I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
192}
193#endif /* ENABLE_I2CSET */
194
195#if ENABLE_I2CDUMP
196/*
197 * Returns the number of bytes read, vals must hold at
198 * least I2C_SMBUS_BLOCK_MAX bytes.
199 */
200static int32_t i2c_smbus_read_block_data(int fd, uint8_t cmd, uint8_t *vals)
201{
202 union i2c_smbus_data data;
203 int i, err;
204
205 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
206 I2C_SMBUS_BLOCK_DATA, &data);
207 if (err < 0)
208 return err;
209
210 for (i = 1; i <= data.block[0]; i++)
211 *vals++ = data.block[i];
212 return data.block[0];
213}
214
215static int32_t i2c_smbus_read_i2c_block_data(int fd, uint8_t cmd,
216 uint8_t len, uint8_t *vals)
217{
218 union i2c_smbus_data data;
219 int i, err;
220
221 if (len > I2C_SMBUS_BLOCK_MAX)
222 len = I2C_SMBUS_BLOCK_MAX;
223 data.block[0] = len;
224
225 err = i2c_smbus_access(fd, I2C_SMBUS_READ, cmd,
226 len == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
227 I2C_SMBUS_I2C_BLOCK_DATA, &data);
228 if (err < 0)
229 return err;
230
231 for (i = 1; i <= data.block[0]; i++)
232 *vals++ = data.block[i];
233 return data.block[0];
234}
235#endif /* ENABLE_I2CDUMP */
236
237#if ENABLE_I2CDETECT
238static int32_t i2c_smbus_write_quick(int fd, uint8_t val)
239{
240 return i2c_smbus_access(fd, val, 0, I2C_SMBUS_QUICK, NULL);
241}
242#endif /* ENABLE_I2CDETECT */
243
244static int i2c_bus_lookup(const char *bus_str)
245{
246 return xstrtou_range(bus_str, 10, 0, 0xfffff);
247}
248
249#if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
250static int i2c_parse_bus_addr(const char *addr_str)
251{
252 /* Slave address must be in range 0x03 - 0x77. */
253 return xstrtou_range(addr_str, 16, 0x03, 0x77);
254}
255
256static void i2c_set_pec(int fd, int pec)
257{
258 ioctl_or_perror_and_die(fd, I2C_PEC,
259 itoptr(pec ? 1 : 0),
260 "can't set PEC");
261}
Bartosz Golaszewski31474272015-06-05 10:27:28 +0200262
263static void i2c_set_slave_addr(int fd, int addr, int force)
264{
265 ioctl_or_perror_and_die(fd, force ? I2C_SLAVE_FORCE : I2C_SLAVE,
266 itoptr(addr),
267 "can't set address to 0x%02x", addr);
268}
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100269#endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
270
271#if ENABLE_I2CGET || ENABLE_I2CSET
272static int i2c_parse_data_addr(const char *data_addr)
273{
274 /* Data address must be an 8 bit integer. */
275 return xstrtou_range(data_addr, 16, 0, 0xff);
276}
277#endif /* ENABLE_I2CGET || ENABLE_I2CSET */
278
279/*
280 * Opens the device file associated with given i2c bus.
281 *
282 * Upstream i2c-tools also support opening devices by i2c bus name
283 * but we drop it here for size reduction.
284 */
285static int i2c_dev_open(int i2cbus)
286{
287 char filename[sizeof("/dev/i2c-%d") + sizeof(int)*3];
288 int fd;
289
290 sprintf(filename, "/dev/i2c-%d", i2cbus);
291 fd = open(filename, O_RDWR);
292 if (fd < 0) {
Bartosz Golaszewski7ca5c512015-05-11 17:26:27 +0200293 if (errno == ENOENT) {
294 filename[8] = '/'; /* change to "/dev/i2c/%d" */
295 fd = xopen(filename, O_RDWR);
296 } else {
297 bb_perror_msg_and_die("can't open '%s'", filename);
298 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100299 }
300
301 return fd;
302}
303
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100304/* Size reducing helpers for xxx_check_funcs(). */
305static void get_funcs_matrix(int fd, unsigned long *funcs)
306{
307 ioctl_or_perror_and_die(fd, I2C_FUNCS, funcs,
308 "can't get adapter functionality matrix");
309}
310
311#if ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP
312static void check_funcs_test_end(int funcs, int pec, const char *err)
313{
314 if (pec && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C)))
315 bb_error_msg("warning: adapter does not support PEC");
316
317 if (err)
318 bb_error_msg_and_die(
319 "adapter has no %s capability", err);
320}
321#endif /* ENABLE_I2CGET || ENABLE_I2CSET || ENABLE_I2CDUMP */
322
323/*
324 * The below functions emit an error message and exit if the adapter doesn't
325 * support desired functionalities.
326 */
327#if ENABLE_I2CGET || ENABLE_I2CDUMP
328static void check_read_funcs(int fd, int mode, int data_addr, int pec)
329{
330 unsigned long funcs;
331 const char *err = NULL;
332
333 get_funcs_matrix(fd, &funcs);
334 switch (mode) {
335 case I2C_SMBUS_BYTE:
336 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
337 err = "SMBus receive byte";
338 break;
339 }
340 if (data_addr >= 0 && !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
341 err = "SMBus send byte";
342 break;
343 case I2C_SMBUS_BYTE_DATA:
344 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
345 err = "SMBus read byte";
346 break;
347 case I2C_SMBUS_WORD_DATA:
348 if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA))
349 err = "SMBus read word";
350 break;
351#if ENABLE_I2CDUMP
352 case I2C_SMBUS_BLOCK_DATA:
353 if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA))
354 err = "SMBus block read";
355 break;
356
357 case I2C_SMBUS_I2C_BLOCK_DATA:
358 if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK))
359 err = "I2C block read";
360 break;
361#endif /* ENABLE_I2CDUMP */
362 default:
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200363 bb_error_msg_and_die("internal error");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100364 }
365 check_funcs_test_end(funcs, pec, err);
366}
367#endif /* ENABLE_I2CGET || ENABLE_I2CDUMP */
368
369#if ENABLE_I2CSET
370static void check_write_funcs(int fd, int mode, int pec)
371{
372 unsigned long funcs;
373 const char *err = NULL;
374
375 get_funcs_matrix(fd, &funcs);
376 switch (mode) {
377 case I2C_SMBUS_BYTE:
378 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE))
379 err = "SMBus send byte";
380 break;
381
382 case I2C_SMBUS_BYTE_DATA:
383 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
384 err = "SMBus write byte";
385 break;
386
387 case I2C_SMBUS_WORD_DATA:
388 if (!(funcs & I2C_FUNC_SMBUS_WRITE_WORD_DATA))
389 err = "SMBus write word";
390 break;
391
392 case I2C_SMBUS_BLOCK_DATA:
393 if (!(funcs & I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
394 err = "SMBus block write";
395 break;
396 case I2C_SMBUS_I2C_BLOCK_DATA:
397 if (!(funcs & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK))
398 err = "I2C block write";
399 break;
400 }
401 check_funcs_test_end(funcs, pec, err);
402}
403#endif /* ENABLE_I2CSET */
404
405static void confirm_or_abort(void)
406{
407 fprintf(stderr, "Continue? [y/N] ");
408 fflush_all();
409 if (!bb_ask_confirmation())
410 bb_error_msg_and_die("aborting");
411}
412
413/*
414 * Return only if user confirms the action, abort otherwise.
415 *
416 * The messages displayed here are much less elaborate than their i2c-tools
417 * counterparts - this is done for size reduction.
418 */
419static void confirm_action(int bus_addr, int mode, int data_addr, int pec)
420{
421 bb_error_msg("WARNING! This program can confuse your I2C bus");
422
423 /* Don't let the user break his/her EEPROMs */
424 if (bus_addr >= 0x50 && bus_addr <= 0x57 && pec) {
425 bb_error_msg_and_die("this is I2C not smbus - using PEC on I2C "
426 "devices may result in data loss, aborting");
427 }
428
429 if (mode == I2C_SMBUS_BYTE && data_addr >= 0 && pec)
430 bb_error_msg("WARNING! May interpret a write byte command "
431 "with PEC as a write byte data command");
432
433 if (pec)
434 bb_error_msg("PEC checking enabled");
435
436 confirm_or_abort();
437}
438
439#if ENABLE_I2CGET
440//usage:#define i2cget_trivial_usage
441//usage: "[-f] [-y] BUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]"
442//usage:#define i2cget_full_usage "\n\n"
443//usage: "Read from I2C/SMBus chip registers\n"
444//usage: "\n I2CBUS i2c bus number"
445//usage: "\n ADDRESS 0x03 - 0x77"
446//usage: "\nMODE is:"
447//usage: "\n b read byte data (default)"
448//usage: "\n w read word data"
449//usage: "\n c write byte/read byte"
450//usage: "\n Append p for SMBus PEC"
451//usage: "\n"
452//usage: "\n -f force access"
453//usage: "\n -y disable interactive mode"
454int i2cget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
455int i2cget_main(int argc UNUSED_PARAM, char **argv)
456{
457 const unsigned opt_f = (1 << 0), opt_y = (1 << 1);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100458
459 int bus_num, bus_addr, data_addr = -1, status;
460 int mode = I2C_SMBUS_BYTE, pec = 0, fd;
461 unsigned opts;
462
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200463 opts = getopt32(argv, "^" "fy" "\0" "-2:?4"/*from 2 to 4 args*/);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100464 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);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100545
546 int bus_num, bus_addr, data_addr, mode = I2C_SMBUS_BYTE, pec = 0;
547 int val, blen = 0, mask = 0, fd, status;
548 unsigned char block[I2C_SMBUS_BLOCK_MAX];
549 char *opt_m_arg = NULL;
550 unsigned opts;
551
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200552 opts = getopt32(argv, "^" "fym:r" "\0" "-3"/*from 3 to ? args*/, &opt_m_arg);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100553 argv += optind;
554 argc -= optind;
555
556 bus_num = i2c_bus_lookup(argv[0]);
557 bus_addr = i2c_parse_bus_addr(argv[1]);
558 data_addr = i2c_parse_data_addr(argv[2]);
559
560 if (argv[3]) {
561 if (!argv[4] && argv[3][0] != 'c') {
562 mode = I2C_SMBUS_BYTE_DATA; /* Implicit b */
563 } else {
564 switch (argv[argc-1][0]) {
565 case 'c': /* Already set */ break;
566 case 'b': mode = I2C_SMBUS_BYTE_DATA; break;
567 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
568 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
569 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
570 default:
571 bb_error_msg("invalid mode");
572 bb_show_usage();
573 }
574
575 pec = argv[argc-1][1] == 'p';
576 if (mode == I2C_SMBUS_BLOCK_DATA ||
577 mode == I2C_SMBUS_I2C_BLOCK_DATA) {
578 if (pec && mode == I2C_SMBUS_I2C_BLOCK_DATA)
579 bb_error_msg_and_die(
580 "PEC not supported for I2C "
581 "block writes");
582 if (opts & opt_m)
583 bb_error_msg_and_die(
584 "mask not supported for block "
585 "writes");
586 }
587 }
588 }
589
590 /* Prepare the value(s) to be written according to current mode. */
591 switch (mode) {
592 case I2C_SMBUS_BYTE_DATA:
593 val = xstrtou_range(argv[3], 0, 0, 0xff);
594 break;
595 case I2C_SMBUS_WORD_DATA:
596 val = xstrtou_range(argv[3], 0, 0, 0xffff);
597 break;
598 case I2C_SMBUS_BLOCK_DATA:
599 case I2C_SMBUS_I2C_BLOCK_DATA:
600 for (blen = 3; blen < (argc - 1); blen++)
601 block[blen] = xstrtou_range(argv[blen], 0, 0, 0xff);
602 val = -1;
603 break;
604 default:
605 val = -1;
606 break;
607 }
608
609 if (opts & opt_m) {
610 mask = xstrtou_range(opt_m_arg, 0, 0,
611 (mode == I2C_SMBUS_BYTE ||
612 mode == I2C_SMBUS_BYTE_DATA) ? 0xff : 0xffff);
613 }
614
615 fd = i2c_dev_open(bus_num);
616 check_write_funcs(fd, mode, pec);
617 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
618
619 if (!(opts & opt_y))
620 confirm_action(bus_addr, mode, data_addr, pec);
621
622 /*
623 * If we're using mask - read the current value here and adjust the
624 * value to be written.
625 */
626 if (opts & opt_m) {
627 int tmpval;
628
629 switch (mode) {
630 case I2C_SMBUS_BYTE:
631 tmpval = i2c_smbus_read_byte(fd);
632 break;
633 case I2C_SMBUS_WORD_DATA:
634 tmpval = i2c_smbus_read_word_data(fd, data_addr);
635 break;
636 default:
637 tmpval = i2c_smbus_read_byte_data(fd, data_addr);
638 }
639
640 if (tmpval < 0)
641 bb_perror_msg_and_die("can't read old value");
642
643 val = (val & mask) | (tmpval & ~mask);
644
645 if (!(opts & opt_y)) {
646 bb_error_msg("old value 0x%0*x, write mask "
647 "0x%0*x, will write 0x%0*x to register "
648 "0x%02x",
649 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, tmpval,
650 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, mask,
651 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
652 data_addr);
653 confirm_or_abort();
654 }
655 }
656
657 if (pec)
658 i2c_set_pec(fd, 1);
659
660 switch (mode) {
661 case I2C_SMBUS_BYTE:
662 status = i2c_smbus_write_byte(fd, data_addr);
663 break;
664 case I2C_SMBUS_WORD_DATA:
665 status = i2c_smbus_write_word_data(fd, data_addr, val);
666 break;
667 case I2C_SMBUS_BLOCK_DATA:
668 status = i2c_smbus_write_block_data(fd, data_addr,
669 blen, block);
670 break;
671 case I2C_SMBUS_I2C_BLOCK_DATA:
672 status = i2c_smbus_write_i2c_block_data(fd, data_addr,
673 blen, block);
674 break;
675 default: /* I2C_SMBUS_BYTE_DATA */
676 status = i2c_smbus_write_byte_data(fd, data_addr, val);
677 break;
678 }
679 if (status < 0)
680 bb_perror_msg_and_die("write failed");
681
682 if (pec)
683 i2c_set_pec(fd, 0); /* Clear PEC. */
684
685 /* No readback required - we're done. */
686 if (!(opts & opt_r))
687 return 0;
688
689 switch (mode) {
690 case I2C_SMBUS_BYTE:
691 status = i2c_smbus_read_byte(fd);
692 val = data_addr;
693 break;
694 case I2C_SMBUS_WORD_DATA:
695 status = i2c_smbus_read_word_data(fd, data_addr);
696 break;
697 default: /* I2C_SMBUS_BYTE_DATA */
698 status = i2c_smbus_read_byte_data(fd, data_addr);
699 }
700
701 if (status < 0) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200702 puts("Warning - readback failed");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100703 } else
704 if (status != val) {
705 printf("Warning - data mismatch - wrote "
706 "0x%0*x, read back 0x%0*x\n",
707 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val,
708 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, status);
709 } else {
710 printf("Value 0x%0*x written, readback matched\n",
711 mode == I2C_SMBUS_WORD_DATA ? 4 : 2, val);
712 }
713
714 return 0;
715}
716#endif /* ENABLE_I2CSET */
717
718#if ENABLE_I2CDUMP
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200719static int read_block_data(int buf_fd, int mode, int *block)
720{
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200721 uint8_t cblock[I2C_SMBUS_BLOCK_MAX + I2CDUMP_NUM_REGS];
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200722 int res, blen = 0, tmp, i;
723
Bartosz Golaszewski1fe75b82015-10-27 17:15:03 +0100724 if (mode == I2C_SMBUS_BLOCK_DATA) {
Bartosz Golaszewski74bb9d52015-10-27 17:15:04 +0100725 blen = i2c_smbus_read_block_data(buf_fd, 0, cblock);
726 if (blen <= 0)
727 goto fail;
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200728 } else {
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200729 for (res = 0; res < I2CDUMP_NUM_REGS; res += tmp) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200730 tmp = i2c_smbus_read_i2c_block_data(
731 buf_fd, res, I2C_SMBUS_BLOCK_MAX,
732 cblock + res);
Bartosz Golaszewski5ed69892015-10-27 17:15:05 +0100733 if (tmp <= 0) {
734 blen = tmp;
Bartosz Golaszewski74bb9d52015-10-27 17:15:04 +0100735 goto fail;
Bartosz Golaszewski5ed69892015-10-27 17:15:05 +0100736 }
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200737 }
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;
Bartosz Golaszewski74bb9d52015-10-27 17:15:04 +0100751
752 fail:
Bartosz Golaszewski5ed69892015-10-27 17:15:05 +0100753 bb_error_msg_and_die("block read failed: %d", blen);
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200754}
755
756/* Dump all but word data. */
757static void dump_data(int bus_fd, int mode, unsigned first,
758 unsigned last, int *block, int blen)
759{
760 int i, j, res;
761
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200762 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f"
763 " 0123456789abcdef");
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200764
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +0200765 for (i = 0; i < I2CDUMP_NUM_REGS; i += 0x10) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200766 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) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200832 bb_putchar(' ');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200833 continue;
834 }
835
836 res = block[i+j];
837 if (res < 0) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200838 bb_putchar('X');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200839 } else if (res == 0x00 || res == 0xff) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200840 bb_putchar('.');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200841 } else if (res < 32 || res >= 127) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200842 bb_putchar('?');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200843 } else {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200844 bb_putchar(res);
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200845 }
846 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200847 bb_putchar('\n');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200848 }
849}
850
851static void dump_word_data(int bus_fd, unsigned first, unsigned last)
852{
853 int i, j, rv;
854
855 /* Word data. */
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200856 puts(" 0,8 1,9 2,a 3,b 4,c 5,d 6,e 7,f");
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200857 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 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200877 bb_putchar('\n');
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200878 }
879}
880
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100881//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"
899int i2cdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
900int 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);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100904
905 int bus_num, bus_addr, mode = I2C_SMBUS_BYTE_DATA, even = 0, pec = 0;
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200906 unsigned first = 0x00, last = 0xff, opts;
Bartosz Golaszewski59f81972016-06-23 17:19:50 +0200907 int block[I2CDUMP_NUM_REGS];
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100908 char *opt_r_str, *dash;
Denys Vlasenko2cf45912015-11-01 20:57:34 +0100909 int fd, res;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100910
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200911 opts = getopt32(argv, "^"
912 "fyr:"
913 "\0" "-2:?3" /* from 2 to 3 args */,
914 &opt_r_str
915 );
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100916 argv += optind;
917
918 bus_num = i2c_bus_lookup(argv[0]);
919 bus_addr = i2c_parse_bus_addr(argv[1]);
920
921 if (argv[2]) {
922 switch (argv[2][0]) {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200923 case 'b': /* Already set. */ break;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100924 case 'c': mode = I2C_SMBUS_BYTE; break;
925 case 'w': mode = I2C_SMBUS_WORD_DATA; break;
926 case 'W':
927 mode = I2C_SMBUS_WORD_DATA;
928 even = 1;
929 break;
930 case 's': mode = I2C_SMBUS_BLOCK_DATA; break;
931 case 'i': mode = I2C_SMBUS_I2C_BLOCK_DATA; break;
932 default:
933 bb_error_msg_and_die("invalid mode");
934 }
935
936 if (argv[2][1] == 'p') {
937 if (argv[2][0] == 'W' || argv[2][0] == 'i') {
938 bb_error_msg_and_die(
939 "pec not supported for -W and -i");
940 } else {
941 pec = 1;
942 }
943 }
944 }
945
946 if (opts & opt_r) {
947 first = strtol(opt_r_str, &dash, 0);
948 if (dash == opt_r_str || *dash != '-' || first > 0xff)
949 bb_error_msg_and_die("invalid range");
950 last = xstrtou_range(++dash, 0, first, 0xff);
951
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200952 /* Range is not available for every mode. */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100953 switch (mode) {
954 case I2C_SMBUS_BYTE:
955 case I2C_SMBUS_BYTE_DATA:
956 break;
957 case I2C_SMBUS_WORD_DATA:
958 if (!even || (!(first % 2) && last % 2))
959 break;
960 /* Fall through */
961 default:
962 bb_error_msg_and_die(
963 "range not compatible with selected mode");
964 }
965 }
966
967 fd = i2c_dev_open(bus_num);
968 check_read_funcs(fd, mode, -1 /* data_addr */, pec);
969 i2c_set_slave_addr(fd, bus_addr, opts & opt_f);
970
971 if (pec)
972 i2c_set_pec(fd, 1);
973
974 if (!(opts & opt_y))
975 confirm_action(bus_addr, mode, -1 /* data_addr */, pec);
976
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200977 /* All but word data. */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100978 if (mode != I2C_SMBUS_WORD_DATA || even) {
Denys Vlasenko2cf45912015-11-01 20:57:34 +0100979 int blen = 0;
980
Bartosz Golaszewski1cde5f72015-10-27 17:15:02 +0100981 if (mode == I2C_SMBUS_BLOCK_DATA || mode == I2C_SMBUS_I2C_BLOCK_DATA)
982 blen = read_block_data(fd, mode, block);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100983
984 if (mode == I2C_SMBUS_BYTE) {
985 res = i2c_smbus_write_byte(fd, first);
986 if (res < 0)
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200987 bb_perror_msg_and_die("write start address");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100988 }
989
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200990 dump_data(fd, mode, first, last, block, blen);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100991 } else {
Bartosz Golaszewskiaeb11a92015-06-05 10:27:31 +0200992 dump_word_data(fd, first, last);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +0100993 }
994
995 return 0;
996}
997#endif /* ENABLE_I2CDUMP */
998
999#if ENABLE_I2CDETECT
1000enum adapter_type {
1001 ADT_DUMMY = 0,
1002 ADT_ISA,
1003 ADT_I2C,
1004 ADT_SMBUS,
1005};
1006
1007struct adap_desc {
1008 const char *funcs;
1009 const char *algo;
1010};
1011
1012static const struct adap_desc adap_descs[] = {
1013 { .funcs = "dummy",
1014 .algo = "Dummy bus", },
1015 { .funcs = "isa",
1016 .algo = "ISA bus", },
1017 { .funcs = "i2c",
1018 .algo = "I2C adapter", },
1019 { .funcs = "smbus",
1020 .algo = "SMBus adapter", },
1021};
1022
1023struct i2c_func
1024{
1025 long value;
1026 const char* name;
1027};
1028
1029static const struct i2c_func i2c_funcs_tab[] = {
1030 { .value = I2C_FUNC_I2C,
1031 .name = "I2C" },
1032 { .value = I2C_FUNC_SMBUS_QUICK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001033 .name = "SMBus quick command" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001034 { .value = I2C_FUNC_SMBUS_WRITE_BYTE,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001035 .name = "SMBus send byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001036 { .value = I2C_FUNC_SMBUS_READ_BYTE,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001037 .name = "SMBus receive byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001038 { .value = I2C_FUNC_SMBUS_WRITE_BYTE_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001039 .name = "SMBus write byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001040 { .value = I2C_FUNC_SMBUS_READ_BYTE_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001041 .name = "SMBus read byte" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001042 { .value = I2C_FUNC_SMBUS_WRITE_WORD_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001043 .name = "SMBus write word" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001044 { .value = I2C_FUNC_SMBUS_READ_WORD_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001045 .name = "SMBus read word" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001046 { .value = I2C_FUNC_SMBUS_PROC_CALL,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001047 .name = "SMBus process call" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001048 { .value = I2C_FUNC_SMBUS_WRITE_BLOCK_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001049 .name = "SMBus block write" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001050 { .value = I2C_FUNC_SMBUS_READ_BLOCK_DATA,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001051 .name = "SMBus block read" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001052 { .value = I2C_FUNC_SMBUS_BLOCK_PROC_CALL,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001053 .name = "SMBus block process call" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001054 { .value = I2C_FUNC_SMBUS_PEC,
1055 .name = "SMBus PEC" },
1056 { .value = I2C_FUNC_SMBUS_WRITE_I2C_BLOCK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001057 .name = "I2C block write" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001058 { .value = I2C_FUNC_SMBUS_READ_I2C_BLOCK,
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001059 .name = "I2C block read" },
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001060 { .value = 0, .name = NULL }
1061};
1062
1063static enum adapter_type i2cdetect_get_funcs(int bus)
1064{
1065 enum adapter_type ret;
1066 unsigned long funcs;
1067 int fd;
1068
1069 fd = i2c_dev_open(bus);
1070
1071 get_funcs_matrix(fd, &funcs);
1072 if (funcs & I2C_FUNC_I2C)
1073 ret = ADT_I2C;
1074 else if (funcs & (I2C_FUNC_SMBUS_BYTE |
1075 I2C_FUNC_SMBUS_BYTE_DATA |
1076 I2C_FUNC_SMBUS_WORD_DATA))
1077 ret = ADT_SMBUS;
1078 else
1079 ret = ADT_DUMMY;
1080
1081 close(fd);
1082
1083 return ret;
1084}
1085
1086static void NORETURN list_i2c_busses_and_exit(void)
1087{
1088 const char *const i2cdev_path = "/sys/class/i2c-dev";
1089
1090 char path[NAME_MAX], name[128];
1091 struct dirent *de, *subde;
1092 enum adapter_type adt;
1093 DIR *dir, *subdir;
1094 int rv, bus;
1095 char *pos;
1096 FILE *fp;
1097
1098 /*
1099 * XXX Upstream i2cdetect also looks for i2c bus info in /proc/bus/i2c,
1100 * but we won't bother since it's only useful on older kernels (before
1101 * 2.6.5). We expect sysfs to be present and mounted at /sys/.
1102 */
1103
1104 dir = xopendir(i2cdev_path);
1105 while ((de = readdir(dir))) {
1106 if (de->d_name[0] == '.')
1107 continue;
1108
1109 /* Simple version for ISA chips. */
1110 snprintf(path, NAME_MAX, "%s/%s/name",
1111 i2cdev_path, de->d_name);
1112 fp = fopen(path, "r");
1113 if (fp == NULL) {
1114 snprintf(path, NAME_MAX,
1115 "%s/%s/device/name",
1116 i2cdev_path, de->d_name);
1117 fp = fopen(path, "r");
1118 }
1119
1120 /* Non-ISA chips require the hard-way. */
1121 if (fp == NULL) {
1122 snprintf(path, NAME_MAX,
1123 "%s/%s/device/name",
1124 i2cdev_path, de->d_name);
1125 subdir = opendir(path);
1126 if (subdir == NULL)
1127 continue;
1128
1129 while ((subde = readdir(subdir))) {
1130 if (subde->d_name[0] == '.')
1131 continue;
1132
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001133 if (is_prefixed_with(subde->d_name, "i2c-")) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001134 snprintf(path, NAME_MAX,
1135 "%s/%s/device/%s/name",
1136 i2cdev_path, de->d_name,
1137 subde->d_name);
1138 fp = fopen(path, "r");
Bartosz Golaszewskid9089922015-06-05 10:27:32 +02001139 break;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001140 }
1141 }
1142 }
1143
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001144 if (fp != NULL) {
1145 /*
1146 * Get the rest of the info and display a line
1147 * for a single bus.
1148 */
1149 memset(name, 0, sizeof(name));
1150 pos = fgets(name, sizeof(name), fp);
1151 fclose(fp);
1152 if (pos == NULL)
1153 continue;
1154
1155 pos = strchr(name, '\n');
1156 if (pos != NULL)
1157 *pos = '\0';
1158
1159 rv = sscanf(de->d_name, "i2c-%d", &bus);
1160 if (rv != 1)
1161 continue;
1162
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001163 if (is_prefixed_with(name, "ISA"))
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001164 adt = ADT_ISA;
1165 else
1166 adt = i2cdetect_get_funcs(bus);
1167
1168 printf(
1169 "i2c-%d\t%-10s\t%-32s\t%s\n",
1170 bus, adap_descs[adt].funcs,
1171 name, adap_descs[adt].algo);
1172 }
1173 }
1174
1175 exit(EXIT_SUCCESS);
1176}
1177
1178static void NORETURN no_support(const char *cmd)
1179{
1180 bb_error_msg_and_die("bus doesn't support %s", cmd);
1181}
1182
1183static void will_skip(const char *cmd)
1184{
1185 bb_error_msg(
1186 "warning: can't use %s command, "
1187 "will skip some addresses", cmd);
1188}
1189
1190//usage:#define i2cdetect_trivial_usage
1191//usage: "[-F I2CBUS] [-l] [-y] [-a] [-q|-r] I2CBUS [FIRST LAST]"
1192//usage:#define i2cdetect_full_usage "\n\n"
1193//usage: "Detect I2C chips.\n"
1194//usage: "\n I2CBUS i2c bus number"
1195//usage: "\n FIRST and LAST limit the probing range"
1196//usage: "\n"
1197//usage: "\n -l output list of installed busses"
1198//usage: "\n -y disable interactive mode"
1199//usage: "\n -a force scanning of non-regular addresses"
1200//usage: "\n -q use smbus quick write commands for probing (default)"
1201//usage: "\n -r use smbus read byte commands for probing"
1202//usage: "\n -F display list of functionalities"
1203int i2cdetect_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1204int i2cdetect_main(int argc UNUSED_PARAM, char **argv)
1205{
1206 const unsigned opt_y = (1 << 0), opt_a = (1 << 1),
1207 opt_q = (1 << 2), opt_r = (1 << 3),
1208 opt_F = (1 << 4), opt_l = (1 << 5);
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001209
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001210 int fd, bus_num, i, j, mode = I2CDETECT_MODE_AUTO, status, cmd;
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001211 unsigned first = 0x03, last = 0x77, opts;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001212 unsigned long funcs;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001213
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001214 opts = getopt32(argv, "^"
1215 "yaqrFl"
1216 "\0"
1217 "q--r:r--q:"/*mutually exclusive*/ "?3"/*up to 3 args*/
1218 );
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001219 argv += optind;
1220
1221 if (opts & opt_l)
1222 list_i2c_busses_and_exit();
1223
Bartosz Golaszewskib2cca322015-05-11 17:22:10 +02001224 if (!argv[0])
1225 bb_show_usage();
1226
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001227 bus_num = i2c_bus_lookup(argv[0]);
1228 fd = i2c_dev_open(bus_num);
1229 get_funcs_matrix(fd, &funcs);
1230
1231 if (opts & opt_F) {
1232 /* Only list the functionalities. */
1233 printf("Functionalities implemented by bus #%d\n", bus_num);
1234 for (i = 0; i2c_funcs_tab[i].value; i++) {
1235 printf("%-32s %s\n", i2c_funcs_tab[i].name,
1236 funcs & i2c_funcs_tab[i].value ? "yes" : "no");
1237 }
1238
1239 return EXIT_SUCCESS;
1240 }
1241
1242 if (opts & opt_r)
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001243 mode = I2CDETECT_MODE_READ;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001244 else if (opts & opt_q)
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001245 mode = I2CDETECT_MODE_QUICK;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001246
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001247 if (opts & opt_a) {
1248 first = 0x00;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001249 last = 0x7f;
Bartosz Golaszewskie545bf62015-06-05 10:27:29 +02001250 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001251
1252 /* Read address range. */
1253 if (argv[1]) {
1254 first = xstrtou_range(argv[1], 16, first, last);
1255 if (argv[2])
1256 last = xstrtou_range(argv[2], 16, first, last);
1257 }
1258
1259 if (!(funcs & (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_READ_BYTE))) {
1260 no_support("detection commands");
1261 } else
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001262 if (mode == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001263 no_support("SMBus quick write");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001264 } else
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001265 if (mode == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001266 no_support("SMBus receive byte");
Bartosz Golaszewski22044722015-06-05 10:27:30 +02001267 }
1268
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001269 if (mode == I2CDETECT_MODE_AUTO) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001270 if (!(funcs & I2C_FUNC_SMBUS_QUICK))
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001271 will_skip("SMBus quick write");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001272 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE))
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +02001273 will_skip("SMBus receive byte");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001274 }
1275
1276 if (!(opts & opt_y))
1277 confirm_action(-1, -1, -1, 0);
1278
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001279 puts(" 0 1 2 3 4 5 6 7 8 9 a b c d e f");
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001280 for (i = 0; i < 128; i += 16) {
1281 printf("%02x: ", i);
Bartosz Golaszewskifc8eb052015-10-27 17:15:01 +01001282 for (j = 0; j < 16; j++) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001283 fflush_all();
1284
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001285 cmd = mode;
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001286 if (mode == I2CDETECT_MODE_AUTO) {
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001287 if ((i+j >= 0x30 && i+j <= 0x37) ||
1288 (i+j >= 0x50 && i+j <= 0x5F))
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001289 cmd = I2CDETECT_MODE_READ;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001290 else
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001291 cmd = I2CDETECT_MODE_QUICK;
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001292 }
1293
1294 /* Skip unwanted addresses. */
1295 if (i+j < first
1296 || i+j > last
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001297 || (cmd == I2CDETECT_MODE_READ && !(funcs & I2C_FUNC_SMBUS_READ_BYTE))
1298 || (cmd == I2CDETECT_MODE_QUICK && !(funcs & I2C_FUNC_SMBUS_QUICK)))
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001299 {
1300 printf(" ");
1301 continue;
1302 }
1303
Bartosz Golaszewski31474272015-06-05 10:27:28 +02001304 status = ioctl(fd, I2C_SLAVE, itoptr(i + j));
1305 if (status < 0) {
1306 if (errno == EBUSY) {
1307 printf("UU ");
1308 continue;
1309 }
1310
1311 bb_perror_msg_and_die(
1312 "can't set address to 0x%02x", i + j);
1313 }
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001314
Bartosz Golaszewski99f025a2015-10-27 17:15:00 +01001315 switch (cmd) {
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001316 case I2CDETECT_MODE_READ:
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001317 /*
1318 * This is known to lock SMBus on various
1319 * write-only chips (mainly clock chips).
1320 */
1321 status = i2c_smbus_read_byte(fd);
1322 break;
Bartosz Golaszewski2beb5242015-09-04 10:38:48 +02001323 default: /* I2CDETECT_MODE_QUICK: */
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001324 /*
1325 * This is known to corrupt the Atmel
1326 * AT24RF08 EEPROM.
1327 */
1328 status = i2c_smbus_write_quick(fd,
1329 I2C_SMBUS_WRITE);
1330 break;
1331 }
1332
1333 if (status < 0)
1334 printf("-- ");
1335 else
1336 printf("%02x ", i+j);
1337 }
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001338 bb_putchar('\n');
Bartosz Golaszewski622a7aa2015-02-10 03:16:25 +01001339 }
1340
1341 return 0;
1342}
1343#endif /* ENABLE_I2CDETECT */