i2c_tools.c 33 KB

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