Subject: kern/36744: Add enumeration of i2c bus devices
To: None <kern-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: None <paul@whooppee.com>
List: netbsd-bugs
Date: 08/07/2007 11:45:01
>Number:         36744
>Category:       kern
>Synopsis:       Add enumeration of i2c bus devices
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Aug 07 11:45:00 +0000 2007
>Originator:     Paul Goyette
>Release:        NetBSD 4.99.26 sources as of 2007-08-01 12:42:27 UTC
>Organization:
>Environment:
System: NetBSD quicky.whooppee.com 4.99.26 NetBSD 4.99.26 (QUICKY (ASUS A8N5X) 2007-08-01 12:42:27 UTC) #185: Wed Aug 1 06:09:48 PDT 2007 paul@quicky.whooppee.com:/usr/obj/objdir/amd64/sys/arch/amd64/compile/QUICKY amd64
Architecture: x86_64
Machine: amd64
>Description:
	For SMB busses, add ability to enumerate devices on the bus.  This
	does NOT identify the devices, simply determines their presense.
	I2C devices still need to be attached at wired bus addresses.

	Other SMB bus drivers need to be modified to set the bus type;  I
	only have nfsmb so I haven't modified or tested the others.

	Code originally provided by Nicolas Joly.
>How-To-Repeat:
>Fix:

Index: i2c/i2c.c
===================================================================
RCS file: /cvsroot/src/sys/dev/i2c/i2c.c,v
retrieving revision 1.14
diff -u -p -r1.14 i2c.c
--- i2c/i2c.c	9 Jul 2007 21:00:32 -0000	1.14
+++ i2c/i2c.c	7 Aug 2007 11:41:52 -0000
@@ -109,8 +109,11 @@ iic_attach(struct device *parent, struct
 {
 	struct iic_softc *sc = device_private(self);
 	struct i2cbus_attach_args *iba = aux;
+	i2c_addr_t addr;
 	i2c_tag_t ic;
 	int rv;
+	int found = 0;
+	uint8_t cmd = 0, val;
 
 	aprint_naive(": I2C bus\n");
 	aprint_normal(": I2C bus\n");
@@ -128,6 +131,24 @@ iic_attach(struct device *parent, struct
 	if (rv)
 		printf("%s: unable to create intr thread\n", ic->ic_devname);
 
+	if (sc->sc_type == I2C_TYPE_SMBUS) {
+		for (addr = 0x0; addr < 0x80; addr++) {
+			iic_acquire_bus(ic, 0);
+			if (iic_exec(ic, I2C_OP_READ_WITH_STOP, addr,
+			    &cmd, 1, &val, 1, 0) == 0) {
+				if (found == 0)
+					aprint_normal("%s: devices at",
+							ic->ic_devname);
+				found++;
+				aprint_normal(" 0x%02x", addr);
+			}
+			iic_release_bus(ic, 0);
+		}
+		if (found == 0)
+			aprint_normal("%s: no devices found", ic->ic_devname);
+		aprint_normal("\n");
+	}
+
 	/*
 	 * Attach all i2c devices described in the kernel
 	 * configuration file.
Index: pci/nfsmb.c
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/nfsmb.c,v
retrieving revision 1.3
diff -u -p -r1.3 nfsmb.c
--- pci/nfsmb.c	28 Jul 2007 12:31:50 -0000	1.3
+++ pci/nfsmb.c	7 Aug 2007 11:41:52 -0000
@@ -217,6 +217,7 @@ nfsmb_attach(struct device *parent, stru
 		return;
 	}
 
+	iba.iba_type = I2C_TYPE_SMBUS;
 	iba.iba_tag = &sc->sc_i2c;
 	(void) config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
 }