i2c_tools.c 33 KB

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