summaryrefslogtreecommitdiffstats
path: root/sys/dev/smbus
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2006-09-11 20:52:41 +0000
committerjhb <jhb@FreeBSD.org>2006-09-11 20:52:41 +0000
commit375fa0aa41b95aca6b081230c5f17725b9dcc893 (patch)
tree724ede4fc106e1f96720180f84383d44d174e12d /sys/dev/smbus
parent35312a59c3883c9be486aa033b2e5d025e9ab07c (diff)
downloadFreeBSD-src-375fa0aa41b95aca6b081230c5f17725b9dcc893.zip
FreeBSD-src-375fa0aa41b95aca6b081230c5f17725b9dcc893.tar.gz
Minor overhaul of SMBus support:
- Change smbus_callback() to pass a void * rather than caddr_t. - Change smbus_bread() to pass a pointer to the count and have it be an in/out parameter. The input is the size of the buffer (same as before), but on return it will contain the actual amount of data read back from the bus. Note that this value may be larger than the input value. It is up to the caller to treat this as an error if desired. - Change the SMB_BREAD ioctl to write out the updated struct smbcmd which will contain the actual number of bytes read in the 'count' field. To preserve the previous ABI, the old ioctl value is mapped to SMB_OLD_BREAD which doesn't copy the updated smbcmd back out to userland. I doubt anyone actually used the old BREAD anyway as it was rediculous to do a bulk-read but not tell the using program how much data was actually read. - Make the smbus driver and devclass public in the smbus module and push all the DRIVER_MODULE()'s for attaching the smbus driver to various foosmb drivers out into the foosmb modules. This makes all the foosmb logic centralized and allows new foosmb modules to be self-contained w/o having to hack smbus.c everytime a new smbus driver is added. - Add a new SMB_EINVAL error bit and use it in place of EINVAL to return an error for bad arguments (such as invalid counts for bread and bwrite). - Map SMB bus error bits to EIO in smbus_error(). - Make the smbus driver call bus_generic_probe() and require child drivers such as smb(4) to create device_t's via identify routines. Previously, smbus just created one anonymous device during attach, and if you had multiple drivers that could attach it was just random chance as to which driver got to probe for the sole device_t first. - Add a mutex to the smbus(4) softc and use it in place of dummy splhigh() to protect the 'owner' field and perform necessary synchronization for smbus_request_bus() and smbus_release_bus(). - Change the bread() and bwrite() methods of alpm(4), amdpm(4), and viapm(4) to only perform a single transaction and not try to use a loop of multiple transactions for a large request. The framing and commands to use for a large transaction depend on the upper-layer protocol (such as SSIF for IPMI over SMBus) from what I can tell, and the smb(4) driver never allowed bulk read/writes of more than 32-bytes anyway. The other smb drivers only performed single transactions. - Fix buffer overflows in the bread() methods of ichsmb(4), alpm(4), amdpm(4), amdsmb(4), intpm(4), and nfsmb(4). - Use SMB_xxx errors in viapm(4). - Destroy ichsmb(4)'s mutex after bus_generic_detach() to avoid problems from child devices making smb upcalls that would use the mutex during their detach methods. MFC after: 1 week Reviewed by: jmg (mostly)
Diffstat (limited to 'sys/dev/smbus')
-rw-r--r--sys/dev/smbus/smb.c21
-rw-r--r--sys/dev/smbus/smb.h3
-rw-r--r--sys/dev/smbus/smbconf.c90
-rw-r--r--sys/dev/smbus/smbconf.h18
-rw-r--r--sys/dev/smbus/smbus.c46
-rw-r--r--sys/dev/smbus/smbus.h4
-rw-r--r--sys/dev/smbus/smbus_if.m4
7 files changed, 108 insertions, 78 deletions
diff --git a/sys/dev/smbus/smb.c b/sys/dev/smbus/smb.c
index ca4fbe6..e5944bd 100644
--- a/sys/dev/smbus/smb.c
+++ b/sys/dev/smbus/smb.c
@@ -55,6 +55,7 @@ struct smb_softc {
#define IIC_DEVICE(unit) \
(devclass_get_device(smb_devclass, (unit)))
+static void smb_identify(driver_t *driver, device_t parent);
static int smb_probe(device_t);
static int smb_attach(device_t);
static int smb_detach(device_t);
@@ -63,6 +64,7 @@ static devclass_t smb_devclass;
static device_method_t smb_methods[] = {
/* device interface */
+ DEVMETHOD(device_identify, smb_identify),
DEVMETHOD(device_probe, smb_probe),
DEVMETHOD(device_attach, smb_attach),
DEVMETHOD(device_detach, smb_detach),
@@ -92,6 +94,14 @@ static struct cdevsw smb_cdevsw = {
.d_name = "smb",
};
+static void
+smb_identify(driver_t *driver, device_t parent)
+{
+
+ if (device_find_child(parent, "smb", -1) == NULL)
+ BUS_ADD_CHILD(parent, 0, "smb", -1);
+}
+
static int
smb_probe(device_t dev)
{
@@ -171,6 +181,7 @@ smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *t
device_t smbdev = IIC_DEVICE(minor(dev));
int error;
short w;
+ u_char count;
char c;
if (sc == NULL)
@@ -259,15 +270,17 @@ smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *t
}
break;
+ case SMB_OLD_BREAD:
case SMB_BREAD:
if (s->count && s->data.byte_ptr) {
- if (s->count > SMB_MAXBLOCKSIZE)
- s->count = SMB_MAXBLOCKSIZE;
+ count = min(s->count, SMB_MAXBLOCKSIZE);
error = smbus_error(smbus_bread(parent, s->slave,
- s->cmd, s->count, buf));
+ s->cmd, &count, buf));
if (error)
break;
- error = copyout(buf, s->data.byte_ptr, s->count);
+ error = copyout(buf, s->data.byte_ptr,
+ min(count, s->count));
+ s->count = count;
}
break;
diff --git a/sys/dev/smbus/smb.h b/sys/dev/smbus/smb.h
index 387515f..32347f8 100644
--- a/sys/dev/smbus/smb.h
+++ b/sys/dev/smbus/smb.h
@@ -64,6 +64,7 @@ struct smbcmd {
#define SMB_READW _IOW('i', 8, struct smbcmd)
#define SMB_PCALL _IOW('i', 9, struct smbcmd)
#define SMB_BWRITE _IOW('i', 10, struct smbcmd)
-#define SMB_BREAD _IOW('i', 11, struct smbcmd)
+#define SMB_OLD_BREAD _IOW('i', 11, struct smbcmd)
+#define SMB_BREAD _IOWR('i', 11, struct smbcmd)
#endif
diff --git a/sys/dev/smbus/smbconf.c b/sys/dev/smbus/smbconf.c
index 8a85009..06d225a 100644
--- a/sys/dev/smbus/smbconf.c
+++ b/sys/dev/smbus/smbconf.c
@@ -30,7 +30,9 @@
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/lock.h>
#include <sys/module.h>
+#include <sys/mutex.h>
#include <sys/bus.h>
#include <dev/smbus/smbconf.h>
@@ -43,13 +45,13 @@ __FBSDID("$FreeBSD$");
void
smbus_intr(device_t bus, u_char devaddr, char low, char high, int error)
{
- struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
+ struct smbus_softc *sc = device_get_softc(bus);
/* call owner's intr routine */
+ mtx_lock(&sc->lock);
if (sc->owner)
SMBUS_INTR(sc->owner, devaddr, low, high, error);
-
- return;
+ mtx_unlock(&sc->lock);
}
/*
@@ -65,17 +67,18 @@ smbus_error(int smb_error)
if (smb_error == SMB_ENOERR)
return (0);
- if (smb_error & (SMB_ENOTSUPP)) {
+ if (smb_error & (SMB_ENOTSUPP))
error = ENODEV;
- } else if (smb_error & (SMB_ENOACK)) {
+ else if (smb_error & (SMB_ENOACK))
error = ENXIO;
- } else if (smb_error & (SMB_ETIMEOUT)) {
+ else if (smb_error & (SMB_ETIMEOUT))
error = EWOULDBLOCK;
- } else if (smb_error & (SMB_EBUSY)) {
+ else if (smb_error & (SMB_EBUSY))
error = EBUSY;
- } else {
+ else if (smb_error & (SMB_EABORT | SMB_EBUSERR | SMB_ECOLLI))
+ error = EIO;
+ else
error = EINVAL;
- }
return (error);
}
@@ -86,16 +89,16 @@ smbus_poll(struct smbus_softc *sc, int how)
int error;
switch (how) {
- case (SMB_WAIT | SMB_INTR):
- error = tsleep(sc, SMBPRI|PCATCH, "smbreq", 0);
+ case SMB_WAIT | SMB_INTR:
+ error = msleep(sc, &sc->lock, SMBPRI|PCATCH, "smbreq", 0);
break;
- case (SMB_WAIT | SMB_NOINTR):
- error = tsleep(sc, SMBPRI, "smbreq", 0);
+ case SMB_WAIT | SMB_NOINTR:
+ error = msleep(sc, &sc->lock, SMBPRI, "smbreq", 0);
break;
default:
- return (EWOULDBLOCK);
+ error = EWOULDBLOCK;
break;
}
@@ -112,35 +115,38 @@ smbus_poll(struct smbus_softc *sc, int how)
int
smbus_request_bus(device_t bus, device_t dev, int how)
{
- struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
- int s, error = 0;
+ struct smbus_softc *sc = device_get_softc(bus);
+ device_t parent;
+ int error;
/* first, ask the underlying layers if the request is ok */
+ parent = device_get_parent(bus);
+ mtx_lock(&sc->lock);
do {
- error = SMBUS_CALLBACK(device_get_parent(bus),
- SMB_REQUEST_BUS, (caddr_t)&how);
+ mtx_unlock(&sc->lock);
+ error = SMBUS_CALLBACK(parent, SMB_REQUEST_BUS, &how);
+ mtx_lock(&sc->lock);
+
if (error)
error = smbus_poll(sc, how);
} while (error == EWOULDBLOCK);
- while (!error) {
- s = splhigh();
- if (sc->owner && sc->owner != dev) {
- splx(s);
-
+ while (error == 0) {
+ if (sc->owner && sc->owner != dev)
error = smbus_poll(sc, how);
- } else {
+ else {
sc->owner = dev;
-
- splx(s);
- return (0);
+ break;
}
/* free any allocated resource */
- if (error)
- SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS,
- (caddr_t)&how);
+ if (error) {
+ mtx_unlock(&sc->lock);
+ SMBUS_CALLBACK(parent, SMB_RELEASE_BUS, &how);
+ return (error);
+ }
}
+ mtx_unlock(&sc->lock);
return (error);
}
@@ -153,8 +159,8 @@ smbus_request_bus(device_t bus, device_t dev, int how)
int
smbus_release_bus(device_t bus, device_t dev)
{
- struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
- int s, error;
+ struct smbus_softc *sc = device_get_softc(bus);
+ int error;
/* first, ask the underlying layers if the release is ok */
error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
@@ -162,17 +168,15 @@ smbus_release_bus(device_t bus, device_t dev)
if (error)
return (error);
- s = splhigh();
- if (sc->owner != dev) {
- splx(s);
- return (EACCES);
- }
-
- sc->owner = 0;
- splx(s);
+ mtx_lock(&sc->lock);
+ if (sc->owner == dev) {
+ sc->owner = NULL;
- /* wakeup waiting processes */
- wakeup(sc);
+ /* wakeup waiting processes */
+ wakeup(sc);
+ } else
+ error = EACCES;
+ mtx_unlock(&sc->lock);
- return (0);
+ return (error);
}
diff --git a/sys/dev/smbus/smbconf.h b/sys/dev/smbus/smbconf.h
index 64b0a33..a3d403d 100644
--- a/sys/dev/smbus/smbconf.h
+++ b/sys/dev/smbus/smbconf.h
@@ -59,6 +59,7 @@
#define SMB_EABORT 0x10
#define SMB_ETIMEOUT 0x20
#define SMB_EBUSY 0x40
+#define SMB_EINVAL 0x100
/*
* How Quick command is executed
@@ -69,16 +70,19 @@
/*
* ivars codes
*/
-#define SMBUS_IVAR_ADDR 0x1 /* I2C address of the device */
+#define SMBUS_IVAR_ADDR 0x1 /* slave address of the device */
-extern int smbus_request_bus(device_t, device_t, int);
-extern int smbus_release_bus(device_t, device_t);
-extern device_t smbus_alloc_bus(device_t);
-extern int smbus_error(int error);
+int smbus_request_bus(device_t, device_t, int);
+int smbus_release_bus(device_t, device_t);
+device_t smbus_alloc_bus(device_t);
+int smbus_error(int error);
-extern void smbus_intr(device_t, u_char, char low, char high, int error);
+void smbus_intr(device_t, u_char, char low, char high, int error);
-extern u_char smbus_get_addr(device_t);
+u_char smbus_get_addr(device_t);
+
+extern driver_t smbus_driver;
+extern devclass_t smbus_devclass;
#define smbus_quick(bus,slave,how) \
(SMBUS_QUICK(device_get_parent(bus), slave, how))
diff --git a/sys/dev/smbus/smbus.c b/sys/dev/smbus/smbus.c
index 6a67eb6..ce7bdc6 100644
--- a/sys/dev/smbus/smbus.c
+++ b/sys/dev/smbus/smbus.c
@@ -30,32 +30,30 @@
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
-#include <sys/kernel.h>
+#include <sys/lock.h>
#include <sys/module.h>
+#include <sys/mutex.h>
#include <sys/bus.h>
#include <dev/smbus/smbconf.h>
#include <dev/smbus/smbus.h>
/*
- * Autoconfiguration and support routines for the Philips serial I2C bus
+ * Autoconfiguration and support routines for System Management bus
*/
-#define DEVTOSMBUS(dev) ((struct smbus_device*)device_get_ivars(dev))
-
-static devclass_t smbus_devclass;
-
/*
* Device methods
*/
static int smbus_probe(device_t);
static int smbus_attach(device_t);
+static int smbus_detach(device_t);
static device_method_t smbus_methods[] = {
/* device interface */
DEVMETHOD(device_probe, smbus_probe),
DEVMETHOD(device_attach, smbus_attach),
- DEVMETHOD(device_detach, bus_generic_detach),
+ DEVMETHOD(device_detach, smbus_detach),
/* bus interface */
DEVMETHOD(bus_print_child, bus_generic_print_child),
@@ -63,12 +61,14 @@ static device_method_t smbus_methods[] = {
{ 0, 0 }
};
-static driver_t smbus_driver = {
+driver_t smbus_driver = {
"smbus",
smbus_methods,
sizeof(struct smbus_softc),
};
+devclass_t smbus_devclass;
+
/*
* At 'probe' time, we add all the devices which we know about to the
* bus. The generic attach routine will probe and attach them if they
@@ -77,6 +77,7 @@ static driver_t smbus_driver = {
static int
smbus_probe(device_t dev)
{
+
device_set_desc(dev, "System Management Bus");
return (0);
@@ -85,25 +86,32 @@ smbus_probe(device_t dev)
static int
smbus_attach(device_t dev)
{
- device_add_child(dev, NULL, -1);
+ struct smbus_softc *sc = device_get_softc(dev);
+
+ mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
+ bus_generic_probe(dev);
bus_generic_attach(dev);
return (0);
}
+static int
+smbus_detach(device_t dev)
+{
+ struct smbus_softc *sc = device_get_softc(dev);
+ int error;
+
+ error = bus_generic_detach(dev);
+ if (error)
+ return (error);
+ mtx_destroy(&sc->lock);
+
+ return (0);
+}
+
void
smbus_generic_intr(device_t dev, u_char devaddr, char low, char high)
{
- return;
}
-DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, bktr, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, intsmb, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, alpm, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, ichsmb, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, amdpm, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, amdsmb, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, nfsmb, smbus_driver, smbus_devclass, 0, 0);
-DRIVER_MODULE(smbus, viapropm, smbus_driver, smbus_devclass, 0, 0);
MODULE_VERSION(smbus, SMBUS_MODVER);
diff --git a/sys/dev/smbus/smbus.h b/sys/dev/smbus/smbus.h
index cd84cc8..f8335e8 100644
--- a/sys/dev/smbus/smbus.h
+++ b/sys/dev/smbus/smbus.h
@@ -30,10 +30,10 @@
#define __SMBUS_H
struct smbus_softc {
-
device_t owner; /* smbus owner device structure */
+ struct mtx lock;
};
-extern void smbus_generic_intr(device_t dev, u_char devaddr, char low, char high);
+void smbus_generic_intr(device_t dev, u_char devaddr, char low, char high);
#endif
diff --git a/sys/dev/smbus/smbus_if.m b/sys/dev/smbus/smbus_if.m
index 6b213e1..d969e25 100644
--- a/sys/dev/smbus/smbus_if.m
+++ b/sys/dev/smbus/smbus_if.m
@@ -47,7 +47,7 @@ METHOD void intr {
METHOD int callback {
device_t dev;
int index;
- caddr_t data;
+ void *data;
};
#
@@ -146,6 +146,6 @@ METHOD int bread {
device_t dev;
u_char slave;
char cmd;
- u_char count;
+ u_char *count;
char *buf;
};
OpenPOWER on IntegriCloud