1998-09-03 20:51:50 +00:00
|
|
|
/*-
|
2002-03-23 15:49:15 +00:00
|
|
|
* Copyright (c) 1998, 2001 Nicolas Souchu
|
1998-09-03 20:51:50 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 AUTHOR 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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-22 21:28:20 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-09-03 20:51:50 +00:00
|
|
|
/*
|
|
|
|
* I2C to SMB bridge
|
1998-10-31 11:31:07 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* smb bttv
|
|
|
|
* \ /
|
|
|
|
* smbus
|
|
|
|
* / \
|
|
|
|
* iicsmb bti2c
|
|
|
|
* |
|
|
|
|
* iicbus
|
|
|
|
* / | \
|
|
|
|
* iicbb pcf ...
|
|
|
|
* |
|
|
|
|
* lpbb
|
1998-09-03 20:51:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2008-06-06 18:40:38 +00:00
|
|
|
#include <sys/bus.h>
|
1998-09-03 20:51:50 +00:00
|
|
|
#include <sys/kernel.h>
|
2008-06-06 18:40:38 +00:00
|
|
|
#include <sys/lock.h>
|
1998-09-03 20:51:50 +00:00
|
|
|
#include <sys/module.h>
|
2008-06-06 18:40:38 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/systm.h>
|
1998-09-03 20:51:50 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <dev/iicbus/iiconf.h>
|
|
|
|
#include <dev/iicbus/iicbus.h>
|
|
|
|
|
|
|
|
#include <dev/smbus/smbconf.h>
|
|
|
|
|
|
|
|
#include "iicbus_if.h"
|
|
|
|
#include "smbus_if.h"
|
|
|
|
|
|
|
|
struct iicsmb_softc {
|
|
|
|
|
|
|
|
#define SMB_WAITING_ADDR 0x0
|
|
|
|
#define SMB_WAITING_LOW 0x1
|
|
|
|
#define SMB_WAITING_HIGH 0x2
|
|
|
|
#define SMB_DONE 0x3
|
|
|
|
int state;
|
|
|
|
|
|
|
|
u_char devaddr; /* slave device address */
|
|
|
|
|
|
|
|
char low; /* low byte received first */
|
|
|
|
char high; /* high byte */
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
struct mtx lock;
|
1998-09-03 20:51:50 +00:00
|
|
|
device_t smbus;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int iicsmb_probe(device_t);
|
|
|
|
static int iicsmb_attach(device_t);
|
2002-03-23 15:49:15 +00:00
|
|
|
static int iicsmb_detach(device_t);
|
|
|
|
static void iicsmb_identify(driver_t *driver, device_t parent);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2009-02-10 22:50:23 +00:00
|
|
|
static int iicsmb_intr(device_t dev, int event, char *buf);
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
static int iicsmb_callback(device_t dev, int index, void *data);
|
1998-09-03 20:51:50 +00:00
|
|
|
static int iicsmb_quick(device_t dev, u_char slave, int how);
|
|
|
|
static int iicsmb_sendb(device_t dev, u_char slave, char byte);
|
|
|
|
static int iicsmb_recvb(device_t dev, u_char slave, char *byte);
|
|
|
|
static int iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte);
|
|
|
|
static int iicsmb_writew(device_t dev, u_char slave, char cmd, short word);
|
|
|
|
static int iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte);
|
|
|
|
static int iicsmb_readw(device_t dev, u_char slave, char cmd, short *word);
|
|
|
|
static int iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata);
|
|
|
|
static int iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf);
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
static int iicsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
static devclass_t iicsmb_devclass;
|
|
|
|
|
|
|
|
static device_method_t iicsmb_methods[] = {
|
|
|
|
/* device interface */
|
2002-03-23 15:49:15 +00:00
|
|
|
DEVMETHOD(device_identify, iicsmb_identify),
|
1998-09-03 20:51:50 +00:00
|
|
|
DEVMETHOD(device_probe, iicsmb_probe),
|
|
|
|
DEVMETHOD(device_attach, iicsmb_attach),
|
2002-03-23 15:49:15 +00:00
|
|
|
DEVMETHOD(device_detach, iicsmb_detach),
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
/* iicbus interface */
|
|
|
|
DEVMETHOD(iicbus_intr, iicsmb_intr),
|
|
|
|
|
|
|
|
/* smbus interface */
|
1998-10-31 11:31:07 +00:00
|
|
|
DEVMETHOD(smbus_callback, iicsmb_callback),
|
1998-09-03 20:51:50 +00:00
|
|
|
DEVMETHOD(smbus_quick, iicsmb_quick),
|
|
|
|
DEVMETHOD(smbus_sendb, iicsmb_sendb),
|
|
|
|
DEVMETHOD(smbus_recvb, iicsmb_recvb),
|
|
|
|
DEVMETHOD(smbus_writeb, iicsmb_writeb),
|
|
|
|
DEVMETHOD(smbus_writew, iicsmb_writew),
|
|
|
|
DEVMETHOD(smbus_readb, iicsmb_readb),
|
|
|
|
DEVMETHOD(smbus_readw, iicsmb_readw),
|
|
|
|
DEVMETHOD(smbus_pcall, iicsmb_pcall),
|
|
|
|
DEVMETHOD(smbus_bwrite, iicsmb_bwrite),
|
|
|
|
DEVMETHOD(smbus_bread, iicsmb_bread),
|
2011-11-22 21:28:20 +00:00
|
|
|
|
|
|
|
DEVMETHOD_END
|
1998-09-03 20:51:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t iicsmb_driver = {
|
|
|
|
"iicsmb",
|
|
|
|
iicsmb_methods,
|
|
|
|
sizeof(struct iicsmb_softc),
|
|
|
|
};
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
#define IICBUS_TIMEOUT 100 /* us */
|
|
|
|
|
|
|
|
static void
|
|
|
|
iicsmb_identify(driver_t *driver, device_t parent)
|
|
|
|
{
|
2008-08-04 21:03:06 +00:00
|
|
|
|
|
|
|
if (device_find_child(parent, "iicsmb", -1) == NULL)
|
|
|
|
BUS_ADD_CHILD(parent, 0, "iicsmb", -1);
|
2002-03-23 15:49:15 +00:00
|
|
|
}
|
|
|
|
|
1998-09-03 20:51:50 +00:00
|
|
|
static int
|
|
|
|
iicsmb_probe(device_t dev)
|
|
|
|
{
|
2002-03-23 15:49:15 +00:00
|
|
|
device_set_desc(dev, "SMBus over I2C bridge");
|
2009-01-06 17:23:37 +00:00
|
|
|
return (BUS_PROBE_NOWILDCARD);
|
1998-09-03 20:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_attach(device_t dev)
|
|
|
|
{
|
|
|
|
struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
mtx_init(&sc->lock, "iicsmb", NULL, MTX_DEF);
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
sc->smbus = device_add_child(dev, "smbus", -1);
|
|
|
|
|
1998-09-03 20:51:50 +00:00
|
|
|
/* probe and attach the smbus */
|
2002-03-23 15:49:15 +00:00
|
|
|
bus_generic_attach(dev);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_detach(device_t dev)
|
|
|
|
{
|
|
|
|
struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
|
2015-10-20 19:52:59 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
bus_generic_detach(dev);
|
2015-10-20 19:52:59 +00:00
|
|
|
device_delete_children(dev);
|
2008-08-04 21:03:06 +00:00
|
|
|
mtx_destroy(&sc->lock);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iicsmb_intr()
|
|
|
|
*
|
|
|
|
* iicbus interrupt handler
|
|
|
|
*/
|
2009-02-10 22:50:23 +00:00
|
|
|
static int
|
1998-09-03 20:51:50 +00:00
|
|
|
iicsmb_intr(device_t dev, int event, char *buf)
|
|
|
|
{
|
|
|
|
struct iicsmb_softc *sc = (struct iicsmb_softc *)device_get_softc(dev);
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
mtx_lock(&sc->lock);
|
1998-09-03 20:51:50 +00:00
|
|
|
switch (event) {
|
|
|
|
case INTR_GENERAL:
|
|
|
|
case INTR_START:
|
|
|
|
sc->state = SMB_WAITING_ADDR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INTR_STOP:
|
|
|
|
/* call smbus intr handler */
|
|
|
|
smbus_intr(sc->smbus, sc->devaddr,
|
|
|
|
sc->low, sc->high, SMB_ENOERR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INTR_RECEIVE:
|
|
|
|
switch (sc->state) {
|
|
|
|
case SMB_DONE:
|
|
|
|
/* XXX too much data, discard */
|
2001-12-10 08:09:49 +00:00
|
|
|
printf("%s: too much data from 0x%x\n", __func__,
|
1998-09-03 20:51:50 +00:00
|
|
|
sc->devaddr & 0xff);
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
case SMB_WAITING_ADDR:
|
|
|
|
sc->devaddr = (u_char)*buf;
|
|
|
|
sc->state = SMB_WAITING_LOW;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_WAITING_LOW:
|
|
|
|
sc->low = *buf;
|
|
|
|
sc->state = SMB_WAITING_HIGH;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_WAITING_HIGH:
|
|
|
|
sc->high = *buf;
|
|
|
|
sc->state = SMB_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INTR_TRANSMIT:
|
|
|
|
case INTR_NOACK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INTR_ERROR:
|
|
|
|
switch (*buf) {
|
|
|
|
case IIC_EBUSERR:
|
|
|
|
smbus_intr(sc->smbus, sc->devaddr, 0, 0, SMB_EBUSERR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-12-10 08:09:49 +00:00
|
|
|
printf("%s unknown error 0x%x!\n", __func__,
|
1998-09-03 20:51:50 +00:00
|
|
|
(int)*buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-12-10 08:09:49 +00:00
|
|
|
panic("%s: unknown event (%d)!", __func__, event);
|
1998-09-03 20:51:50 +00:00
|
|
|
}
|
2008-08-04 21:03:06 +00:00
|
|
|
mtx_unlock(&sc->lock);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2009-02-10 22:50:23 +00:00
|
|
|
return (0);
|
1998-09-03 20:51:50 +00:00
|
|
|
}
|
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
static int
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
iicsmb_callback(device_t dev, int index, void *data)
|
1998-10-31 11:31:07 +00:00
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error = 0;
|
|
|
|
int how;
|
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case SMB_REQUEST_BUS:
|
|
|
|
/* request underlying iicbus */
|
|
|
|
how = *(int *)data;
|
|
|
|
error = iicbus_request_bus(parent, dev, how);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_RELEASE_BUS:
|
|
|
|
/* release underlying iicbus */
|
|
|
|
error = iicbus_release_bus(parent, dev);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1998-09-03 20:51:50 +00:00
|
|
|
static int
|
|
|
|
iicsmb_quick(device_t dev, u_char slave, int how)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch (how) {
|
|
|
|
case SMB_QWRITE:
|
2002-03-23 15:49:15 +00:00
|
|
|
error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SMB_QREAD:
|
2002-03-23 15:49:15 +00:00
|
|
|
error = iicbus_start(parent, slave | LSB, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
error = iicbus_stop(parent);
|
2008-06-06 18:40:38 +00:00
|
|
|
|
1998-09-03 20:51:50 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_sendb(device_t dev, u_char slave, char byte)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
if (!error) {
|
2002-03-23 15:49:15 +00:00
|
|
|
error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
iicbus_stop(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_recvb(device_t dev, u_char slave, char *byte)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, read;
|
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
error = iicbus_start(parent, slave | LSB, 0);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
if (!error) {
|
2002-03-23 15:49:15 +00:00
|
|
|
error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT);
|
1998-10-31 11:31:07 +00:00
|
|
|
|
|
|
|
iicbus_stop(parent);
|
|
|
|
}
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent;
|
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
error = iicbus_start(parent, slave & ~LSB, 0);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
if (!error) {
|
2002-03-23 15:49:15 +00:00
|
|
|
if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
|
|
|
error = iicbus_write(parent, &byte, 1, &sent, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
iicbus_stop(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_writew(device_t dev, u_char slave, char cmd, short word)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent;
|
|
|
|
|
|
|
|
char low = (char)(word & 0xff);
|
|
|
|
char high = (char)((word & 0xff00) >> 8);
|
|
|
|
|
1998-10-31 11:31:07 +00:00
|
|
|
error = iicbus_start(parent, slave & ~LSB, 0);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
if (!error) {
|
2002-03-23 15:49:15 +00:00
|
|
|
if (!(error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
|
|
|
if (!(error = iicbus_write(parent, &low, 1, &sent, IICBUS_TIMEOUT)))
|
|
|
|
error = iicbus_write(parent, &high, 1, &sent, IICBUS_TIMEOUT);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
iicbus_stop(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent, read;
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
|
1998-10-31 11:31:07 +00:00
|
|
|
return (error);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_read(parent, byte, 1, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
error:
|
1998-10-31 11:31:07 +00:00
|
|
|
iicbus_stop(parent);
|
1998-09-03 20:51:50 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUF2SHORT(low,high) \
|
|
|
|
((short)(((high) & 0xff) << 8) | (short)((low) & 0xff))
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_readw(device_t dev, u_char slave, char cmd, short *word)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent, read;
|
|
|
|
char buf[2];
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
|
1998-10-31 11:31:07 +00:00
|
|
|
return (error);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* first, receive low, then high byte */
|
|
|
|
*word = BUF2SHORT(buf[0], buf[1]);
|
|
|
|
|
|
|
|
error:
|
1998-10-31 11:31:07 +00:00
|
|
|
iicbus_stop(parent);
|
1998-09-03 20:51:50 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_pcall(device_t dev, u_char slave, char cmd, short sdata, short *rdata)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent, read;
|
|
|
|
char buf[2];
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
|
1998-10-31 11:31:07 +00:00
|
|
|
return (error);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* first, send low, then high byte */
|
|
|
|
buf[0] = (char)(sdata & 0xff);
|
|
|
|
buf[1] = (char)((sdata & 0xff00) >> 8);
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, buf, 2, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_read(parent, buf, 2, &read, IIC_LAST_READ, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* first, receive low, then high byte */
|
|
|
|
*rdata = BUF2SHORT(buf[0], buf[1]);
|
|
|
|
|
|
|
|
error:
|
1998-10-31 11:31:07 +00:00
|
|
|
iicbus_stop(parent);
|
1998-09-03 20:51:50 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iicsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
|
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, buf, (int)count, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((error = iicbus_stop(parent)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
error:
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
iicsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
|
1998-09-03 20:51:50 +00:00
|
|
|
{
|
|
|
|
device_t parent = device_get_parent(dev);
|
|
|
|
int error, sent, read;
|
|
|
|
|
2008-08-04 21:03:06 +00:00
|
|
|
if ((error = iicbus_start(parent, slave & ~LSB, IICBUS_TIMEOUT)))
|
1998-10-31 11:31:07 +00:00
|
|
|
return (error);
|
1998-09-03 20:51:50 +00:00
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_write(parent, &cmd, 1, &sent, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
2002-03-23 15:49:15 +00:00
|
|
|
if ((error = iicbus_repeated_start(parent, slave | LSB, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
|
|
|
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
if ((error = iicbus_read(parent, buf, (int)*count, &read,
|
2002-03-23 15:49:15 +00:00
|
|
|
IIC_LAST_READ, IICBUS_TIMEOUT)))
|
1998-09-03 20:51:50 +00:00
|
|
|
goto error;
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
*count = read;
|
1998-09-03 20:51:50 +00:00
|
|
|
|
|
|
|
error:
|
1998-10-31 11:31:07 +00:00
|
|
|
iicbus_stop(parent);
|
1998-09-03 20:51:50 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
DRIVER_MODULE(iicsmb, iicbus, iicsmb_driver, iicsmb_devclass, 0, 0);
|
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)
2006-09-11 20:52:41 +00:00
|
|
|
DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0);
|
2002-03-23 15:49:15 +00:00
|
|
|
MODULE_DEPEND(iicsmb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
|
|
|
|
MODULE_DEPEND(iicsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
|
|
|
|
MODULE_VERSION(iicsmb, 1);
|