1 1.5 hubertf /* $NetBSD: i2cscan.c,v 1.5 2015/11/26 17:31:56 hubertf 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.5 hubertf __RCSID("$NetBSD: i2cscan.c,v 1.5 2015/11/26 17:31:56 hubertf 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.5 hubertf #include <paths.h> 42 1.1 jmcneill #include <stdio.h> 43 1.1 jmcneill #include <stdlib.h> 44 1.5 hubertf #include <string.h> 45 1.1 jmcneill #include <unistd.h> 46 1.1 jmcneill 47 1.1 jmcneill #include <dev/i2c/i2c_io.h> 48 1.1 jmcneill 49 1.4 tcort #define MODE_DEFAULT 0 50 1.4 tcort #define MODE_READ 1 51 1.4 tcort 52 1.2 joerg __dead static void 53 1.4 tcort usage(void) 54 1.1 jmcneill { 55 1.5 hubertf fprintf(stderr, "usage: %s [-r] i2cdev\n", getprogname()); 56 1.1 jmcneill exit(EXIT_FAILURE); 57 1.1 jmcneill } 58 1.1 jmcneill 59 1.1 jmcneill static int 60 1.1 jmcneill iic_smbus_quick_write(int fd, i2c_addr_t addr, int flags) 61 1.1 jmcneill { 62 1.1 jmcneill i2c_ioctl_exec_t iie; 63 1.1 jmcneill 64 1.1 jmcneill iie.iie_op = I2C_OP_WRITE_WITH_STOP; 65 1.1 jmcneill iie.iie_addr = addr; 66 1.1 jmcneill iie.iie_cmd = NULL; 67 1.1 jmcneill iie.iie_cmdlen = 0; 68 1.1 jmcneill iie.iie_buf = NULL; 69 1.1 jmcneill iie.iie_buflen = 0; 70 1.1 jmcneill 71 1.1 jmcneill if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1) 72 1.1 jmcneill return errno; 73 1.1 jmcneill return 0; 74 1.1 jmcneill } 75 1.1 jmcneill 76 1.1 jmcneill static int 77 1.1 jmcneill iic_smbus_receive_byte(int fd, i2c_addr_t addr, uint8_t *valp, int flags) 78 1.1 jmcneill { 79 1.1 jmcneill i2c_ioctl_exec_t iie; 80 1.1 jmcneill 81 1.1 jmcneill iie.iie_op = I2C_OP_READ_WITH_STOP; 82 1.1 jmcneill iie.iie_addr = addr; 83 1.1 jmcneill iie.iie_cmd = NULL; 84 1.1 jmcneill iie.iie_cmdlen = 0; 85 1.1 jmcneill iie.iie_buf = valp; 86 1.1 jmcneill iie.iie_buflen = 1; 87 1.1 jmcneill 88 1.1 jmcneill if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1) 89 1.1 jmcneill return errno; 90 1.1 jmcneill return 0; 91 1.1 jmcneill 92 1.1 jmcneill } 93 1.1 jmcneill 94 1.1 jmcneill static void 95 1.4 tcort do_i2c_scan(const char *dname, int fd, int mode) 96 1.1 jmcneill { 97 1.1 jmcneill int error; 98 1.1 jmcneill int found = 0; 99 1.1 jmcneill i2c_addr_t addr; 100 1.1 jmcneill uint8_t val; 101 1.1 jmcneill 102 1.1 jmcneill for (addr = 0x09; addr < 0x78; addr++) { 103 1.1 jmcneill /* 104 1.1 jmcneill * Skip certain i2c addresses: 105 1.1 jmcneill * 0x00 General Call / START 106 1.1 jmcneill * 0x01 CBUS Address 107 1.1 jmcneill * 0x02 Different Bus format 108 1.1 jmcneill * 0x03 - 0x07 Reserved 109 1.1 jmcneill * 0x08 Host Address 110 1.1 jmcneill * 0x0c Alert Response Address 111 1.1 jmcneill * 0x28 ACCESS.Bus host 112 1.1 jmcneill * 0x37 ACCESS.Bus default address 113 1.1 jmcneill * 0x48 - 0x4b Prototypes 114 1.1 jmcneill * 0x61 Device Default Address 115 1.1 jmcneill * 0x78 - 0x7b 10-bit addresses 116 1.1 jmcneill * 0x7c - 0x7f Reserved 117 1.1 jmcneill * 118 1.1 jmcneill * Some of these are skipped by judicious selection 119 1.1 jmcneill * of the range of the above for (;;) statement. 120 1.1 jmcneill * 121 1.1 jmcneill * if (addr <= 0x08 || addr >= 0x78) 122 1.1 jmcneill * continue; 123 1.1 jmcneill */ 124 1.1 jmcneill if (addr == 0x0c || addr == 0x28 || addr == 0x37 || 125 1.1 jmcneill addr == 0x61 || (addr & 0x7c) == 0x48) 126 1.1 jmcneill continue; 127 1.1 jmcneill 128 1.1 jmcneill /* 129 1.1 jmcneill * Use SMBus quick_write command to detect most 130 1.1 jmcneill * addresses; should avoid hanging the bus on 131 1.1 jmcneill * some write-only devices (like clocks that show 132 1.1 jmcneill * up at address 0x69) 133 1.1 jmcneill * 134 1.1 jmcneill * XXX The quick_write() is allegedly known to 135 1.1 jmcneill * XXX corrupt the Atmel AT24RF08 EEPROM found 136 1.1 jmcneill * XXX on some IBM Thinkpads! 137 1.1 jmcneill */ 138 1.1 jmcneill printf("\r%s: scanning 0x%02x", dname, addr); 139 1.1 jmcneill fflush(stdout); 140 1.1 jmcneill if ((addr & 0xf8) == 0x30 || 141 1.4 tcort (addr & 0xf0) == 0x50 || 142 1.4 tcort mode == MODE_READ) 143 1.1 jmcneill error = iic_smbus_receive_byte(fd, addr, &val, 0); 144 1.1 jmcneill else 145 1.1 jmcneill error = iic_smbus_quick_write(fd, addr, 0); 146 1.1 jmcneill if (error == 0) { 147 1.1 jmcneill printf("\r%s: found device at 0x%02x\n", 148 1.1 jmcneill dname, addr); 149 1.1 jmcneill ++found; 150 1.1 jmcneill } 151 1.1 jmcneill } 152 1.1 jmcneill if (found == 0) 153 1.1 jmcneill printf("\r%s: no devices found\n", dname); 154 1.1 jmcneill else 155 1.1 jmcneill printf("\r%s: %d devices found\n", dname, found); 156 1.1 jmcneill } 157 1.1 jmcneill 158 1.1 jmcneill int 159 1.1 jmcneill main(int argc, char *argv[]) 160 1.1 jmcneill { 161 1.1 jmcneill int fd; 162 1.4 tcort int ch, rflag; 163 1.4 tcort int mode; 164 1.5 hubertf char *dev; 165 1.5 hubertf char devn[32]; 166 1.4 tcort 167 1.4 tcort setprogname(*argv); 168 1.4 tcort 169 1.4 tcort rflag = 0; 170 1.4 tcort 171 1.4 tcort while ((ch = getopt(argc, argv, "r")) != -1) 172 1.4 tcort switch (ch) { 173 1.4 tcort case 'r': 174 1.4 tcort rflag = 1; 175 1.4 tcort break; 176 1.4 tcort default: 177 1.4 tcort break; 178 1.4 tcort } 179 1.4 tcort argv += optind; 180 1.4 tcort argc -= optind; 181 1.4 tcort 182 1.4 tcort if (rflag) 183 1.4 tcort mode = MODE_READ; 184 1.4 tcort else 185 1.4 tcort mode = MODE_DEFAULT; 186 1.1 jmcneill 187 1.4 tcort if (*argv == NULL) 188 1.4 tcort usage(); 189 1.5 hubertf dev = argv[0]; 190 1.1 jmcneill 191 1.5 hubertf if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 192 1.5 hubertf (void)snprintf(devn, sizeof(devn), "%s%s", _PATH_DEV, dev); 193 1.5 hubertf dev = devn; 194 1.5 hubertf } 195 1.5 hubertf 196 1.5 hubertf fd = open(dev, O_RDWR); 197 1.1 jmcneill if (fd == -1) 198 1.4 tcort err(EXIT_FAILURE, "couldn't open %s", *argv); 199 1.1 jmcneill 200 1.4 tcort do_i2c_scan(*argv, fd, mode); 201 1.1 jmcneill 202 1.1 jmcneill close(fd); 203 1.1 jmcneill 204 1.1 jmcneill return EXIT_SUCCESS; 205 1.1 jmcneill } 206