i2c_tools.c 37 KB

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