]> Zhao Yanbai Git Server - minix.git/commitdiff
Importing usr.sbin/i2cscan 77/677/1
authorThomas Cort <tcort@minix3.org>
Mon, 15 Jul 2013 14:29:16 +0000 (10:29 -0400)
committerThomas Cort <tcort@minix3.org>
Mon, 15 Jul 2013 15:11:13 +0000 (11:11 -0400)
Change-Id: I7b776d6e5f3fe4f7a1af353b995ef9e2e752c7ea

distrib/sets/lists/minix/mi
releasetools/nbsd_ports
usr.sbin/Makefile
usr.sbin/i2cscan/Makefile [new file with mode: 0644]
usr.sbin/i2cscan/i2cscan.8 [new file with mode: 0644]
usr.sbin/i2cscan/i2cscan.c [new file with mode: 0644]

index e4a0ce613af99f3500131d833a53e5d0d7780b13..fd849bda43e378df047f6dc44fe18855d938b7d9 100644 (file)
 ./usr/man/man8/halt.8                          minix-sys
 ./usr/man/man8/hgfs.8                          minix-sys
 ./usr/man/man8/httpd.8                         minix-sys
+./usr/man/man8/i2cscan.8                       minix-sys
 ./usr/man/man8/ifconfig.8                      minix-sys
 ./usr/man/man8/inet.8                          minix-sys
 ./usr/man/man8/in.httpd.8                      minix-sys
 ./usr/sbin/groupdel                    minix-sys
 ./usr/sbin/groupinfo                   minix-sys
 ./usr/sbin/groupmod                    minix-sys
+./usr/sbin/i2cscan                     minix-sys
 ./usr/sbin/inet                                minix-sys
 ./usr/sbin/init                                minix-sys
 ./usr/sbin/installboot_nbsd            minix-sys
index b2ee60e450d2ae1384cda7002550895f19961942..9fdc335588e4f9095804922d80f296d04f70b364 100644 (file)
 2012/10/17 12:00:00,usr.bin/xinstall
 2013/03/15 12:00:00,usr.bin/yes
 2012/02/10 16:16:12,usr.sbin/chroot
+2013/07/10 15:18:55,usr.sbin/i2cscan
 2011/11/03 20:46:41,usr.sbin/installboot
 2012/10/17 12:00:00,usr.sbin/Makefile
 2012/10/17 12:00:00,usr.sbin/Makefile.inc
index ff33db3df4d0c4304448f76d04b4ded7bad55912..1e5fe653b111440a16e42128c14ad990179d0d09 100644 (file)
@@ -5,7 +5,7 @@
 
 SUBDIR= \
        chroot \
-       installboot \
+       i2cscan installboot \
        mtree \
        pwd_mkdb postinstall \
        rdate user \
diff --git a/usr.sbin/i2cscan/Makefile b/usr.sbin/i2cscan/Makefile
new file mode 100644 (file)
index 0000000..f03d9f9
--- /dev/null
@@ -0,0 +1,6 @@
+# $NetBSD: Makefile,v 1.2 2011/10/06 12:38:58 wiz Exp $
+
+PROG=  i2cscan
+MAN=   i2cscan.8
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/i2cscan/i2cscan.8 b/usr.sbin/i2cscan/i2cscan.8
new file mode 100644 (file)
index 0000000..c41bda5
--- /dev/null
@@ -0,0 +1,66 @@
+.\"    $NetBSD: i2cscan.8,v 1.3 2013/07/10 15:18:54 tcort Exp $
+.\"
+.\" Copyright (c) 2011, 2013 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Thomas Klausner.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd July 10, 2013
+.Dt I2CSCAN 8
+.Os
+.Sh NAME
+.Nm i2cscan
+.Nd scan an IIC bus for devices
+.Sh SYNOPSIS
+.Nm
+.Op Fl r
+.Ar i2cdev
+.Sh DESCRIPTION
+The
+.Nm
+utility scans the IIC bus
+.Pq Xr iic 4
+specified by
+.Ar i2cdev
+to determine which addresses respond.
+.Pp
+Available options:
+.Bl -tag -width Ds
+.It Fl r
+scan using 1 byte reads instead of quick writes.
+.El
+.Pp
+.Em WARNING !
+Using this utility can access some devices in such a
+manner as to leave them in an unstable or unusable state.
+It can also lock up the entire
+.Xr iic 4
+bus.
+Don't use this utility unless you know what you're doing and can
+accept all sorts of unforeseen consequences.
+.Sh EXIT STATUS
+.Ex -std i2cscan
+.Sh SEE ALSO
+.Xr iic 4
diff --git a/usr.sbin/i2cscan/i2cscan.c b/usr.sbin/i2cscan/i2cscan.c
new file mode 100644 (file)
index 0000000..41d130a
--- /dev/null
@@ -0,0 +1,195 @@
+/* $NetBSD: i2cscan.c,v 1.4 2013/07/10 15:18:54 tcort Exp $ */
+
+/*-
+ * Copyright (c) 2011, 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Paul Goyette and Jared McNeill
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: i2cscan.c,v 1.4 2013/07/10 15:18:54 tcort Exp $");
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <dev/i2c/i2c_io.h>
+
+#define MODE_DEFAULT 0
+#define MODE_READ 1
+
+__dead static void
+usage(void)
+{
+       fprintf(stderr, "usage: %s [-r] <i2cdev>\n", getprogname());
+       exit(EXIT_FAILURE);
+}
+
+static int
+iic_smbus_quick_write(int fd, i2c_addr_t addr, int flags)
+{
+       i2c_ioctl_exec_t iie;
+
+       iie.iie_op = I2C_OP_WRITE_WITH_STOP;
+       iie.iie_addr = addr;
+       iie.iie_cmd = NULL;
+       iie.iie_cmdlen = 0;
+       iie.iie_buf = NULL;
+       iie.iie_buflen = 0;
+
+       if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
+               return errno;
+       return 0;
+}
+
+static int
+iic_smbus_receive_byte(int fd, i2c_addr_t addr, uint8_t *valp, int flags)
+{
+       i2c_ioctl_exec_t iie;
+
+       iie.iie_op = I2C_OP_READ_WITH_STOP;
+       iie.iie_addr = addr;
+       iie.iie_cmd = NULL;
+       iie.iie_cmdlen = 0;
+       iie.iie_buf = valp;
+       iie.iie_buflen = 1;
+
+       if (ioctl(fd, I2C_IOCTL_EXEC, &iie) == -1)
+               return errno;
+       return 0;
+       
+}
+
+static void
+do_i2c_scan(const char *dname, int fd, int mode)
+{
+       int error;
+       int found = 0;
+       i2c_addr_t addr;
+       uint8_t val;
+
+       for (addr = 0x09; addr < 0x78; addr++) {
+               /*
+                * Skip certain i2c addresses:
+                *      0x00            General Call / START
+                *      0x01            CBUS Address
+                *      0x02            Different Bus format
+                *      0x03 - 0x07     Reserved
+                *      0x08            Host Address
+                *      0x0c            Alert Response Address
+                *      0x28            ACCESS.Bus host
+                *      0x37            ACCESS.Bus default address
+                *      0x48 - 0x4b     Prototypes
+                *      0x61            Device Default Address
+                *      0x78 - 0x7b     10-bit addresses
+                *      0x7c - 0x7f     Reserved
+                *
+                * Some of these are skipped by judicious selection
+                * of the range of the above for (;;) statement.
+                *
+                * if (addr <= 0x08 || addr >= 0x78)
+                *      continue;
+                */
+               if (addr == 0x0c || addr == 0x28 || addr == 0x37 ||
+                   addr == 0x61 || (addr & 0x7c) == 0x48)
+                       continue;
+
+               /*
+                * Use SMBus quick_write command to detect most
+                * addresses;  should avoid hanging the bus on
+                * some write-only devices (like clocks that show
+                * up at address 0x69)
+                *
+                * XXX The quick_write() is allegedly known to
+                * XXX corrupt the Atmel AT24RF08 EEPROM found
+                * XXX on some IBM Thinkpads!
+                */
+               printf("\r%s: scanning 0x%02x", dname, addr);
+               fflush(stdout);
+               if ((addr & 0xf8) == 0x30 ||
+                   (addr & 0xf0) == 0x50 ||
+                   mode == MODE_READ)
+                       error = iic_smbus_receive_byte(fd, addr, &val, 0);
+               else
+                       error = iic_smbus_quick_write(fd, addr, 0);
+               if (error == 0) {
+                       printf("\r%s: found device at 0x%02x\n",
+                           dname, addr);
+                       ++found;
+               }
+       }
+       if (found == 0)
+               printf("\r%s: no devices found\n", dname);
+       else
+               printf("\r%s: %d devices found\n", dname, found);
+}
+
+int
+main(int argc, char *argv[])
+{
+       int fd;
+       int ch, rflag;
+       int mode;
+
+       setprogname(*argv);
+
+       rflag = 0;
+
+       while ((ch = getopt(argc, argv, "r")) != -1)
+               switch (ch) {
+               case 'r':
+                       rflag = 1;
+                       break;
+               default:
+                       break;
+               }
+       argv += optind;
+       argc -= optind;
+
+       if (rflag)
+               mode = MODE_READ;
+       else
+               mode = MODE_DEFAULT;
+
+       if (*argv == NULL)
+               usage();
+
+       fd = open(*argv, O_RDWR);
+       if (fd == -1)
+               err(EXIT_FAILURE, "couldn't open %s", *argv);
+
+       do_i2c_scan(*argv, fd, mode);
+
+       close(fd);
+
+       return EXIT_SUCCESS;
+}