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