1*3c59273cSThomas Cort /* $NetBSD: i2cscan.c,v 1.4 2013/07/10 15:18:54 tcort Exp $ */
2*3c59273cSThomas Cort
3*3c59273cSThomas Cort /*-
4*3c59273cSThomas Cort * Copyright (c) 2011, 2013 The NetBSD Foundation, Inc.
5*3c59273cSThomas Cort * All rights reserved.
6*3c59273cSThomas Cort *
7*3c59273cSThomas Cort * This code is derived from software contributed to The NetBSD Foundation
8*3c59273cSThomas Cort * by Paul Goyette and Jared McNeill
9*3c59273cSThomas Cort *
10*3c59273cSThomas Cort * Redistribution and use in source and binary forms, with or without
11*3c59273cSThomas Cort * modification, are permitted provided that the following conditions
12*3c59273cSThomas Cort * are met:
13*3c59273cSThomas Cort * 1. Redistributions of source code must retain the above copyright
14*3c59273cSThomas Cort * notice, this list of conditions and the following disclaimer.
15*3c59273cSThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
16*3c59273cSThomas Cort * notice, this list of conditions and the following disclaimer in the
17*3c59273cSThomas Cort * documentation and/or other materials provided with the distribution.
18*3c59273cSThomas Cort *
19*3c59273cSThomas Cort * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*3c59273cSThomas Cort * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*3c59273cSThomas Cort * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*3c59273cSThomas Cort * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*3c59273cSThomas Cort * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*3c59273cSThomas Cort * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*3c59273cSThomas Cort * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*3c59273cSThomas Cort * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*3c59273cSThomas Cort * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*3c59273cSThomas Cort * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*3c59273cSThomas Cort * POSSIBILITY OF SUCH DAMAGE.
30*3c59273cSThomas Cort */
31*3c59273cSThomas Cort
32*3c59273cSThomas Cort #include <sys/cdefs.h>
33*3c59273cSThomas Cort __RCSID("$NetBSD: i2cscan.c,v 1.4 2013/07/10 15:18:54 tcort Exp $");
34*3c59273cSThomas Cort
35*3c59273cSThomas Cort #include <sys/types.h>
36*3c59273cSThomas Cort #include <sys/ioctl.h>
37*3c59273cSThomas Cort
38*3c59273cSThomas Cort #include <err.h>
39*3c59273cSThomas Cort #include <errno.h>
40*3c59273cSThomas Cort #include <fcntl.h>
41*3c59273cSThomas Cort #include <stdio.h>
42*3c59273cSThomas Cort #include <stdlib.h>
43*3c59273cSThomas Cort #include <unistd.h>
44*3c59273cSThomas Cort
45*3c59273cSThomas Cort #include <dev/i2c/i2c_io.h>
46*3c59273cSThomas Cort
47*3c59273cSThomas Cort #define MODE_DEFAULT 0
48*3c59273cSThomas Cort #define MODE_READ 1
49*3c59273cSThomas Cort
50*3c59273cSThomas Cort __dead static void
usage(void)51*3c59273cSThomas Cort usage(void)
52*3c59273cSThomas Cort {
53*3c59273cSThomas Cort fprintf(stderr, "usage: %s [-r] <i2cdev>\n", getprogname());
54*3c59273cSThomas Cort exit(EXIT_FAILURE);
55*3c59273cSThomas Cort }
56*3c59273cSThomas Cort
57*3c59273cSThomas Cort static int
iic_smbus_quick_write(int fd,i2c_addr_t addr,int flags)58*3c59273cSThomas Cort iic_smbus_quick_write(int fd, i2c_addr_t addr, int flags)
59*3c59273cSThomas Cort {
60*3c59273cSThomas Cort i2c_ioctl_exec_t iie;
61*3c59273cSThomas Cort
62*3c59273cSThomas Cort iie.iie_op = I2C_OP_WRITE_WITH_STOP;
63*3c59273cSThomas Cort iie.iie_addr = addr;
64*3c59273cSThomas Cort iie.iie_cmd = NULL;
65*3c59273cSThomas Cort iie.iie_cmdlen = 0;
66*3c59273cSThomas Cort iie.iie_buf = NULL;
67*3c59273cSThomas Cort iie.iie_buflen = 0;
68*3c59273cSThomas Cort
69*3c59273cSThomas Cort if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
70*3c59273cSThomas Cort return errno;
71*3c59273cSThomas Cort return 0;
72*3c59273cSThomas Cort }
73*3c59273cSThomas Cort
74*3c59273cSThomas Cort static int
iic_smbus_receive_byte(int fd,i2c_addr_t addr,uint8_t * valp,int flags)75*3c59273cSThomas Cort iic_smbus_receive_byte(int fd, i2c_addr_t addr, uint8_t *valp, int flags)
76*3c59273cSThomas Cort {
77*3c59273cSThomas Cort i2c_ioctl_exec_t iie;
78*3c59273cSThomas Cort
79*3c59273cSThomas Cort iie.iie_op = I2C_OP_READ_WITH_STOP;
80*3c59273cSThomas Cort iie.iie_addr = addr;
81*3c59273cSThomas Cort iie.iie_cmd = NULL;
82*3c59273cSThomas Cort iie.iie_cmdlen = 0;
83*3c59273cSThomas Cort iie.iie_buf = valp;
84*3c59273cSThomas Cort iie.iie_buflen = 1;
85*3c59273cSThomas Cort
86*3c59273cSThomas Cort if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
87*3c59273cSThomas Cort return errno;
88*3c59273cSThomas Cort return 0;
89*3c59273cSThomas Cort
90*3c59273cSThomas Cort }
91*3c59273cSThomas Cort
92*3c59273cSThomas Cort static void
do_i2c_scan(const char * dname,int fd,int mode)93*3c59273cSThomas Cort do_i2c_scan(const char *dname, int fd, int mode)
94*3c59273cSThomas Cort {
95*3c59273cSThomas Cort int error;
96*3c59273cSThomas Cort int found = 0;
97*3c59273cSThomas Cort i2c_addr_t addr;
98*3c59273cSThomas Cort uint8_t val;
99*3c59273cSThomas Cort
100*3c59273cSThomas Cort for (addr = 0x09; addr < 0x78; addr++) {
101*3c59273cSThomas Cort /*
102*3c59273cSThomas Cort * Skip certain i2c addresses:
103*3c59273cSThomas Cort * 0x00 General Call / START
104*3c59273cSThomas Cort * 0x01 CBUS Address
105*3c59273cSThomas Cort * 0x02 Different Bus format
106*3c59273cSThomas Cort * 0x03 - 0x07 Reserved
107*3c59273cSThomas Cort * 0x08 Host Address
108*3c59273cSThomas Cort * 0x0c Alert Response Address
109*3c59273cSThomas Cort * 0x28 ACCESS.Bus host
110*3c59273cSThomas Cort * 0x37 ACCESS.Bus default address
111*3c59273cSThomas Cort * 0x48 - 0x4b Prototypes
112*3c59273cSThomas Cort * 0x61 Device Default Address
113*3c59273cSThomas Cort * 0x78 - 0x7b 10-bit addresses
114*3c59273cSThomas Cort * 0x7c - 0x7f Reserved
115*3c59273cSThomas Cort *
116*3c59273cSThomas Cort * Some of these are skipped by judicious selection
117*3c59273cSThomas Cort * of the range of the above for (;;) statement.
118*3c59273cSThomas Cort *
119*3c59273cSThomas Cort * if (addr <= 0x08 || addr >= 0x78)
120*3c59273cSThomas Cort * continue;
121*3c59273cSThomas Cort */
122*3c59273cSThomas Cort if (addr == 0x0c || addr == 0x28 || addr == 0x37 ||
123*3c59273cSThomas Cort addr == 0x61 || (addr & 0x7c) == 0x48)
124*3c59273cSThomas Cort continue;
125*3c59273cSThomas Cort
126*3c59273cSThomas Cort /*
127*3c59273cSThomas Cort * Use SMBus quick_write command to detect most
128*3c59273cSThomas Cort * addresses; should avoid hanging the bus on
129*3c59273cSThomas Cort * some write-only devices (like clocks that show
130*3c59273cSThomas Cort * up at address 0x69)
131*3c59273cSThomas Cort *
132*3c59273cSThomas Cort * XXX The quick_write() is allegedly known to
133*3c59273cSThomas Cort * XXX corrupt the Atmel AT24RF08 EEPROM found
134*3c59273cSThomas Cort * XXX on some IBM Thinkpads!
135*3c59273cSThomas Cort */
136*3c59273cSThomas Cort printf("\r%s: scanning 0x%02x", dname, addr);
137*3c59273cSThomas Cort fflush(stdout);
138*3c59273cSThomas Cort if ((addr & 0xf8) == 0x30 ||
139*3c59273cSThomas Cort (addr & 0xf0) == 0x50 ||
140*3c59273cSThomas Cort mode == MODE_READ)
141*3c59273cSThomas Cort error = iic_smbus_receive_byte(fd, addr, &val, 0);
142*3c59273cSThomas Cort else
143*3c59273cSThomas Cort error = iic_smbus_quick_write(fd, addr, 0);
144*3c59273cSThomas Cort if (error == 0) {
145*3c59273cSThomas Cort printf("\r%s: found device at 0x%02x\n",
146*3c59273cSThomas Cort dname, addr);
147*3c59273cSThomas Cort ++found;
148*3c59273cSThomas Cort }
149*3c59273cSThomas Cort }
150*3c59273cSThomas Cort if (found == 0)
151*3c59273cSThomas Cort printf("\r%s: no devices found\n", dname);
152*3c59273cSThomas Cort else
153*3c59273cSThomas Cort printf("\r%s: %d devices found\n", dname, found);
154*3c59273cSThomas Cort }
155*3c59273cSThomas Cort
156*3c59273cSThomas Cort int
main(int argc,char * argv[])157*3c59273cSThomas Cort main(int argc, char *argv[])
158*3c59273cSThomas Cort {
159*3c59273cSThomas Cort int fd;
160*3c59273cSThomas Cort int ch, rflag;
161*3c59273cSThomas Cort int mode;
162*3c59273cSThomas Cort
163*3c59273cSThomas Cort setprogname(*argv);
164*3c59273cSThomas Cort
165*3c59273cSThomas Cort rflag = 0;
166*3c59273cSThomas Cort
167*3c59273cSThomas Cort while ((ch = getopt(argc, argv, "r")) != -1)
168*3c59273cSThomas Cort switch (ch) {
169*3c59273cSThomas Cort case 'r':
170*3c59273cSThomas Cort rflag = 1;
171*3c59273cSThomas Cort break;
172*3c59273cSThomas Cort default:
173*3c59273cSThomas Cort break;
174*3c59273cSThomas Cort }
175*3c59273cSThomas Cort argv += optind;
176*3c59273cSThomas Cort argc -= optind;
177*3c59273cSThomas Cort
178*3c59273cSThomas Cort if (rflag)
179*3c59273cSThomas Cort mode = MODE_READ;
180*3c59273cSThomas Cort else
181*3c59273cSThomas Cort mode = MODE_DEFAULT;
182*3c59273cSThomas Cort
183*3c59273cSThomas Cort if (*argv == NULL)
184*3c59273cSThomas Cort usage();
185*3c59273cSThomas Cort
186*3c59273cSThomas Cort fd = open(*argv, O_RDWR);
187*3c59273cSThomas Cort if (fd == -1)
188*3c59273cSThomas Cort err(EXIT_FAILURE, "couldn't open %s", *argv);
189*3c59273cSThomas Cort
190*3c59273cSThomas Cort do_i2c_scan(*argv, fd, mode);
191*3c59273cSThomas Cort
192*3c59273cSThomas Cort close(fd);
193*3c59273cSThomas Cort
194*3c59273cSThomas Cort return EXIT_SUCCESS;
195*3c59273cSThomas Cort }
196