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