Home | History | Annotate | Line # | Download | only in i2cscan
i2cscan.c revision 1.3
      1  1.3  pgoyette /* $NetBSD: i2cscan.c,v 1.3 2011/11/01 22:30:32 pgoyette Exp $ */
      2  1.1  jmcneill 
      3  1.3  pgoyette /*-
      4  1.3  pgoyette  * Copyright (c) 2011 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.3  pgoyette __RCSID("$NetBSD: i2cscan.c,v 1.3 2011/11/01 22:30:32 pgoyette 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.2     joerg __dead static void
     48  1.1  jmcneill usage(const char *pn)
     49  1.1  jmcneill {
     50  1.1  jmcneill 	fprintf(stderr, "usage: %s <i2cdev>\n", pn);
     51  1.1  jmcneill 	exit(EXIT_FAILURE);
     52  1.1  jmcneill }
     53  1.1  jmcneill 
     54  1.1  jmcneill static int
     55  1.1  jmcneill iic_smbus_quick_write(int fd, i2c_addr_t addr, int flags)
     56  1.1  jmcneill {
     57  1.1  jmcneill 	i2c_ioctl_exec_t iie;
     58  1.1  jmcneill 
     59  1.1  jmcneill 	iie.iie_op = I2C_OP_WRITE_WITH_STOP;
     60  1.1  jmcneill 	iie.iie_addr = addr;
     61  1.1  jmcneill 	iie.iie_cmd = NULL;
     62  1.1  jmcneill 	iie.iie_cmdlen = 0;
     63  1.1  jmcneill 	iie.iie_buf = NULL;
     64  1.1  jmcneill 	iie.iie_buflen = 0;
     65  1.1  jmcneill 
     66  1.1  jmcneill 	if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
     67  1.1  jmcneill 		return errno;
     68  1.1  jmcneill 	return 0;
     69  1.1  jmcneill }
     70  1.1  jmcneill 
     71  1.1  jmcneill static int
     72  1.1  jmcneill iic_smbus_receive_byte(int fd, i2c_addr_t addr, uint8_t *valp, int flags)
     73  1.1  jmcneill {
     74  1.1  jmcneill 	i2c_ioctl_exec_t iie;
     75  1.1  jmcneill 
     76  1.1  jmcneill 	iie.iie_op = I2C_OP_READ_WITH_STOP;
     77  1.1  jmcneill 	iie.iie_addr = addr;
     78  1.1  jmcneill 	iie.iie_cmd = NULL;
     79  1.1  jmcneill 	iie.iie_cmdlen = 0;
     80  1.1  jmcneill 	iie.iie_buf = valp;
     81  1.1  jmcneill 	iie.iie_buflen = 1;
     82  1.1  jmcneill 
     83  1.1  jmcneill 	if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
     84  1.1  jmcneill 		return errno;
     85  1.1  jmcneill 	return 0;
     86  1.1  jmcneill 
     87  1.1  jmcneill }
     88  1.1  jmcneill 
     89  1.1  jmcneill static void
     90  1.1  jmcneill do_i2c_scan(const char *dname, int fd)
     91  1.1  jmcneill {
     92  1.1  jmcneill 	int error;
     93  1.1  jmcneill 	int found = 0;
     94  1.1  jmcneill 	i2c_addr_t addr;
     95  1.1  jmcneill 	uint8_t val;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	for (addr = 0x09; addr < 0x78; addr++) {
     98  1.1  jmcneill 		/*
     99  1.1  jmcneill 		 * Skip certain i2c addresses:
    100  1.1  jmcneill 		 *	0x00		General Call / START
    101  1.1  jmcneill 		 *	0x01		CBUS Address
    102  1.1  jmcneill 		 *	0x02		Different Bus format
    103  1.1  jmcneill 		 *	0x03 - 0x07	Reserved
    104  1.1  jmcneill 		 *	0x08		Host Address
    105  1.1  jmcneill 		 *	0x0c		Alert Response Address
    106  1.1  jmcneill 		 *	0x28		ACCESS.Bus host
    107  1.1  jmcneill 		 *	0x37		ACCESS.Bus default address
    108  1.1  jmcneill 		 *	0x48 - 0x4b	Prototypes
    109  1.1  jmcneill 		 *	0x61		Device Default Address
    110  1.1  jmcneill 		 *	0x78 - 0x7b	10-bit addresses
    111  1.1  jmcneill 		 *	0x7c - 0x7f	Reserved
    112  1.1  jmcneill 		 *
    113  1.1  jmcneill 		 * Some of these are skipped by judicious selection
    114  1.1  jmcneill 		 * of the range of the above for (;;) statement.
    115  1.1  jmcneill 		 *
    116  1.1  jmcneill 		 * if (addr <= 0x08 || addr >= 0x78)
    117  1.1  jmcneill 		 *	continue;
    118  1.1  jmcneill 		 */
    119  1.1  jmcneill 		if (addr == 0x0c || addr == 0x28 || addr == 0x37 ||
    120  1.1  jmcneill 		    addr == 0x61 || (addr & 0x7c) == 0x48)
    121  1.1  jmcneill 			continue;
    122  1.1  jmcneill 
    123  1.1  jmcneill 		/*
    124  1.1  jmcneill 		 * Use SMBus quick_write command to detect most
    125  1.1  jmcneill 		 * addresses;  should avoid hanging the bus on
    126  1.1  jmcneill 		 * some write-only devices (like clocks that show
    127  1.1  jmcneill 		 * up at address 0x69)
    128  1.1  jmcneill 		 *
    129  1.1  jmcneill 		 * XXX The quick_write() is allegedly known to
    130  1.1  jmcneill 		 * XXX corrupt the Atmel AT24RF08 EEPROM found
    131  1.1  jmcneill 		 * XXX on some IBM Thinkpads!
    132  1.1  jmcneill 		 */
    133  1.1  jmcneill 		printf("\r%s: scanning 0x%02x", dname, addr);
    134  1.1  jmcneill 		fflush(stdout);
    135  1.1  jmcneill 		if ((addr & 0xf8) == 0x30 ||
    136  1.1  jmcneill 		    (addr & 0xf0) == 0x50)
    137  1.1  jmcneill 			error = iic_smbus_receive_byte(fd, addr, &val, 0);
    138  1.1  jmcneill 		else
    139  1.1  jmcneill 			error = iic_smbus_quick_write(fd, addr, 0);
    140  1.1  jmcneill 		if (error == 0) {
    141  1.1  jmcneill 			printf("\r%s: found device at 0x%02x\n",
    142  1.1  jmcneill 			    dname, addr);
    143  1.1  jmcneill 			++found;
    144  1.1  jmcneill 		}
    145  1.1  jmcneill 	}
    146  1.1  jmcneill 	if (found == 0)
    147  1.1  jmcneill 		printf("\r%s: no devices found\n", dname);
    148  1.1  jmcneill 	else
    149  1.1  jmcneill 		printf("\r%s: %d devices found\n", dname, found);
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.1  jmcneill int
    153  1.1  jmcneill main(int argc, char *argv[])
    154  1.1  jmcneill {
    155  1.1  jmcneill 	int fd;
    156  1.1  jmcneill 
    157  1.1  jmcneill 	if (argc != 2)
    158  1.1  jmcneill 		usage(argv[0]);
    159  1.1  jmcneill 
    160  1.1  jmcneill 	fd = open(argv[1], O_RDWR);
    161  1.1  jmcneill 	if (fd == -1)
    162  1.1  jmcneill 		err(EXIT_FAILURE, "couldn't open %s", argv[1]);
    163  1.1  jmcneill 
    164  1.1  jmcneill 	do_i2c_scan(argv[1], fd);
    165  1.1  jmcneill 
    166  1.1  jmcneill 	close(fd);
    167  1.1  jmcneill 
    168  1.1  jmcneill 	return EXIT_SUCCESS;
    169  1.1  jmcneill }
    170