i2cscan.c revision 1.1 1 1.1 jmcneill /* $NetBSD: i2cscan.c,v 1.1 2011/10/02 16:48:47 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 jmcneill * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
6 1.1 jmcneill * All rights reserved.
7 1.1 jmcneill *
8 1.1 jmcneill * Written by Jason R. Thorpe for Wasabi Systems, Inc.
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 * 3. All advertising materials mentioning features or use of this software
19 1.1 jmcneill * must display the following acknowledgement:
20 1.1 jmcneill * This product includes software developed for the NetBSD Project by
21 1.1 jmcneill * Wasabi Systems, Inc.
22 1.1 jmcneill * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 1.1 jmcneill * or promote products derived from this software without specific prior
24 1.1 jmcneill * written permission.
25 1.1 jmcneill *
26 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jmcneill */
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/cdefs.h>
40 1.1 jmcneill __RCSID("$NetBSD: i2cscan.c,v 1.1 2011/10/02 16:48:47 jmcneill Exp $");
41 1.1 jmcneill
42 1.1 jmcneill #include <sys/types.h>
43 1.1 jmcneill #include <sys/ioctl.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <err.h>
46 1.1 jmcneill #include <errno.h>
47 1.1 jmcneill #include <fcntl.h>
48 1.1 jmcneill #include <stdio.h>
49 1.1 jmcneill #include <stdlib.h>
50 1.1 jmcneill #include <unistd.h>
51 1.1 jmcneill
52 1.1 jmcneill #include <dev/i2c/i2c_io.h>
53 1.1 jmcneill
54 1.1 jmcneill static void
55 1.1 jmcneill usage(const char *pn)
56 1.1 jmcneill {
57 1.1 jmcneill fprintf(stderr, "usage: %s <i2cdev>\n", pn);
58 1.1 jmcneill exit(EXIT_FAILURE);
59 1.1 jmcneill }
60 1.1 jmcneill
61 1.1 jmcneill static int
62 1.1 jmcneill iic_smbus_quick_write(int fd, i2c_addr_t addr, int flags)
63 1.1 jmcneill {
64 1.1 jmcneill i2c_ioctl_exec_t iie;
65 1.1 jmcneill
66 1.1 jmcneill iie.iie_op = I2C_OP_WRITE_WITH_STOP;
67 1.1 jmcneill iie.iie_addr = addr;
68 1.1 jmcneill iie.iie_cmd = NULL;
69 1.1 jmcneill iie.iie_cmdlen = 0;
70 1.1 jmcneill iie.iie_buf = NULL;
71 1.1 jmcneill iie.iie_buflen = 0;
72 1.1 jmcneill
73 1.1 jmcneill if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
74 1.1 jmcneill return errno;
75 1.1 jmcneill return 0;
76 1.1 jmcneill }
77 1.1 jmcneill
78 1.1 jmcneill static int
79 1.1 jmcneill iic_smbus_receive_byte(int fd, i2c_addr_t addr, uint8_t *valp, int flags)
80 1.1 jmcneill {
81 1.1 jmcneill i2c_ioctl_exec_t iie;
82 1.1 jmcneill
83 1.1 jmcneill iie.iie_op = I2C_OP_READ_WITH_STOP;
84 1.1 jmcneill iie.iie_addr = addr;
85 1.1 jmcneill iie.iie_cmd = NULL;
86 1.1 jmcneill iie.iie_cmdlen = 0;
87 1.1 jmcneill iie.iie_buf = valp;
88 1.1 jmcneill iie.iie_buflen = 1;
89 1.1 jmcneill
90 1.1 jmcneill if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
91 1.1 jmcneill return errno;
92 1.1 jmcneill return 0;
93 1.1 jmcneill
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill static void
97 1.1 jmcneill do_i2c_scan(const char *dname, int fd)
98 1.1 jmcneill {
99 1.1 jmcneill int error;
100 1.1 jmcneill int found = 0;
101 1.1 jmcneill i2c_addr_t addr;
102 1.1 jmcneill uint8_t val;
103 1.1 jmcneill
104 1.1 jmcneill for (addr = 0x09; addr < 0x78; addr++) {
105 1.1 jmcneill /*
106 1.1 jmcneill * Skip certain i2c addresses:
107 1.1 jmcneill * 0x00 General Call / START
108 1.1 jmcneill * 0x01 CBUS Address
109 1.1 jmcneill * 0x02 Different Bus format
110 1.1 jmcneill * 0x03 - 0x07 Reserved
111 1.1 jmcneill * 0x08 Host Address
112 1.1 jmcneill * 0x0c Alert Response Address
113 1.1 jmcneill * 0x28 ACCESS.Bus host
114 1.1 jmcneill * 0x37 ACCESS.Bus default address
115 1.1 jmcneill * 0x48 - 0x4b Prototypes
116 1.1 jmcneill * 0x61 Device Default Address
117 1.1 jmcneill * 0x78 - 0x7b 10-bit addresses
118 1.1 jmcneill * 0x7c - 0x7f Reserved
119 1.1 jmcneill *
120 1.1 jmcneill * Some of these are skipped by judicious selection
121 1.1 jmcneill * of the range of the above for (;;) statement.
122 1.1 jmcneill *
123 1.1 jmcneill * if (addr <= 0x08 || addr >= 0x78)
124 1.1 jmcneill * continue;
125 1.1 jmcneill */
126 1.1 jmcneill if (addr == 0x0c || addr == 0x28 || addr == 0x37 ||
127 1.1 jmcneill addr == 0x61 || (addr & 0x7c) == 0x48)
128 1.1 jmcneill continue;
129 1.1 jmcneill
130 1.1 jmcneill /*
131 1.1 jmcneill * Use SMBus quick_write command to detect most
132 1.1 jmcneill * addresses; should avoid hanging the bus on
133 1.1 jmcneill * some write-only devices (like clocks that show
134 1.1 jmcneill * up at address 0x69)
135 1.1 jmcneill *
136 1.1 jmcneill * XXX The quick_write() is allegedly known to
137 1.1 jmcneill * XXX corrupt the Atmel AT24RF08 EEPROM found
138 1.1 jmcneill * XXX on some IBM Thinkpads!
139 1.1 jmcneill */
140 1.1 jmcneill printf("\r%s: scanning 0x%02x", dname, addr);
141 1.1 jmcneill fflush(stdout);
142 1.1 jmcneill if ((addr & 0xf8) == 0x30 ||
143 1.1 jmcneill (addr & 0xf0) == 0x50)
144 1.1 jmcneill error = iic_smbus_receive_byte(fd, addr, &val, 0);
145 1.1 jmcneill else
146 1.1 jmcneill error = iic_smbus_quick_write(fd, addr, 0);
147 1.1 jmcneill if (error == 0) {
148 1.1 jmcneill printf("\r%s: found device at 0x%02x\n",
149 1.1 jmcneill dname, addr);
150 1.1 jmcneill ++found;
151 1.1 jmcneill }
152 1.1 jmcneill }
153 1.1 jmcneill if (found == 0)
154 1.1 jmcneill printf("\r%s: no devices found\n", dname);
155 1.1 jmcneill else
156 1.1 jmcneill printf("\r%s: %d devices found\n", dname, found);
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill int
160 1.1 jmcneill main(int argc, char *argv[])
161 1.1 jmcneill {
162 1.1 jmcneill int fd;
163 1.1 jmcneill
164 1.1 jmcneill if (argc != 2)
165 1.1 jmcneill usage(argv[0]);
166 1.1 jmcneill
167 1.1 jmcneill fd = open(argv[1], O_RDWR);
168 1.1 jmcneill if (fd == -1)
169 1.1 jmcneill err(EXIT_FAILURE, "couldn't open %s", argv[1]);
170 1.1 jmcneill
171 1.1 jmcneill do_i2c_scan(argv[1], fd);
172 1.1 jmcneill
173 1.1 jmcneill close(fd);
174 1.1 jmcneill
175 1.1 jmcneill return EXIT_SUCCESS;
176 1.1 jmcneill }
177