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