Change /dev/smb and /dev/iic interface to allow user programs to interact with

devices dynamically. That means,

        + only one /dev/iic or /dev/smb device for each smb/iic bus to access
        + I2C/SMB device address must be given to any ioctl
        + new devices may be plugged and accessed after boot, which was
          impossible previously (device addresses were hardcoded into
          the kernel)
This commit is contained in:
Nicolas Souchu 1999-01-09 18:08:24 +00:00
parent 26af4d7d6a
commit bf896bd0e2
12 changed files with 196 additions and 84 deletions

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iic.c,v 1.4 1998/10/31 11:31:07 nsouch Exp $
* $Id: iic.c,v 1.5 1998/12/07 21:58:16 archie Exp $
*
*/
#include <sys/param.h>
@ -40,6 +40,7 @@
#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>
#include <machine/iic.h>
#include "iicbus_if.h"
@ -209,15 +210,16 @@ iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
{
device_t iicdev = IIC_DEVICE(minor(dev));
struct iic_softc *sc = IIC_SOFTC(minor(dev));
int error;
device_t parent = device_get_parent(iicdev);
struct iiccmd *s = (struct iiccmd *)data;
int error, count;
if (!sc)
return (EINVAL);
switch (cmd) {
case I2CSTART:
error = iicbus_start(parent, sc->sc_addr, 0);
error = iicbus_start(parent, s->slave, 0);
break;
case I2CSTOP:
@ -228,6 +230,14 @@ iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
error = iicbus_reset(parent, 0, 0, NULL);
break;
case I2CWRITE:
error = iicbus_write(parent, s->buf, s->count, &count, 0);
break;
case I2CREAD:
error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0);
break;
default:
error = ENODEV;
}

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iic.h,v 1.1.2.1 1998/07/17 00:54:01 son Exp $
* $Id: iic.h,v 1.1 1998/09/03 21:00:08 nsouch Exp $
*
*/
#ifndef __IIC_H
@ -31,10 +31,17 @@
#include <sys/ioccom.h>
#define I2CSTART _IO('i', 1) /* start condition */
#define I2CSTOP _IO('i', 2) /* stop condition */
#define I2CADDRESS _IOW('i', 3, long) /* address bus */
#define I2CRSTCARD _IOW('i', 4, long) /* reset the card */
struct iiccmd {
u_char slave;
int count;
int last;
char *buf;
};
#define I2CSTART _IOW('i', 1, struct iiccmd) /* start condition */
#define I2CSTOP _IO('i', 2) /* stop condition */
#define I2CRSTCARD _IOW('i', 3, struct iiccmd) /* reset the card */
#define I2CWRITE _IOW('i', 4, struct iiccmd) /* send data */
#define I2CREAD _IOW('i', 5, struct iiccmd) /* receive data */
#endif

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iicbus.c,v 1.5 1998/11/22 22:01:42 nsouch Exp $
* $Id: iicbus.c,v 1.6 1998/12/07 21:58:16 archie Exp $
*
*/
@ -73,14 +73,14 @@ struct iicbus_device {
/*
* list of known devices
*
* XXX only one smb driver should exist for each I2C interface
*/
struct iicbus_device iicbus_children[] = {
{ "iicsmb", IICBUS_DRIVER_CLASS, "I2C to SMB bridge" },
{ "iic", IICBUS_DEVICE_CLASS, "PCF8574 I2C to 8 bits parallel i/o", 64},
{ "iic", IICBUS_DEVICE_CLASS, "PCF8584 as slave", PCF_MASTER_ADDRESS },
{ "ic", IICBUS_DEVICE_CLASS, "network interface", PCF_MASTER_ADDRESS },
{ "iic", IICBUS_DRIVER_CLASS, "I2C general purpose I/O" },
#if 0
{ "iic", IICBUS_DRIVER_CLASS, "General Call", I2C_GENERAL_CALL },
{ "ic", IICBUS_DEVICE_CLASS, "network interface", PCF_MASTER_ADDRESS },
#endif
{ NULL, 0 }
};
@ -121,10 +121,12 @@ static driver_t iicbus_driver = {
static int
iicbus_probe(device_t dev)
{
/* always present if probed */
device_set_desc(dev, "Philips I2C bus");
return (0);
}
#if 0
static int
iic_probe_device(device_t dev, u_char addr)
{
@ -145,6 +147,7 @@ iic_probe_device(device_t dev, u_char addr)
return (0);
}
#endif
/*
* We add all the devices which we know about.
@ -155,10 +158,15 @@ iicbus_attach(device_t dev)
{
struct iicbus_device *iicdev;
device_t child;
int addr;
iicbus_reset(dev, IIC_FASTEST, 0, NULL);
/* device probing is meaningless since the bus is supposed to be
* hot-plug. Moreover, some I2C chips do not appreciate random
* accesses like stop after start to fast, reads for less than
* x bytes...
*/
#if 0
printf("Probing for devices on iicbus%d:", device_get_unit(dev));
/* probe any devices */
@ -168,21 +176,27 @@ iicbus_attach(device_t dev)
}
}
printf("\n");
#endif
/* attach known devices */
for (iicdev = iicbus_children; iicdev->iicd_name; iicdev++) {
switch (iicdev->iicd_class) {
case IICBUS_DEVICE_CLASS:
/* check if the devclass exists */
if (devclass_find(iicdev->iicd_name) &&
iic_probe_device(dev, iicdev->iicd_addr))
if (devclass_find(iicdev->iicd_name))
iicdev->iicd_alive = 1;
else if (bootverbose)
printf("iicbus: %s devclass not found\n",
iicdev->iicd_name);
break;
case IICBUS_DRIVER_CLASS:
/* check if the devclass exists */
if (!devclass_find(iicdev->iicd_name))
if (devclass_find(iicdev->iicd_name))
iicdev->iicd_alive = 1;
else if (bootverbose)
printf("iicbus: %s devclass not found\n",
iicdev->iicd_name);
break;
default:
@ -272,4 +286,3 @@ iicbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
DRIVER_MODULE(iicbus, pcf, iicbus_driver, iicbus_devclass, 0, 0);
DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
DRIVER_MODULE(iicbus, bti2c, iicbus_driver, iicbus_devclass, 0, 0);
DRIVER_MODULE(iicbus, smbtx, iicbus_driver, iicbus_devclass, 0, 0);

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iicbus.h,v 1.1.1.1 1998/09/03 20:51:50 nsouch Exp $
* $Id: iicbus.h,v 1.2 1998/10/31 11:31:07 nsouch Exp $
*
*/
#ifndef __IICBUS_H
@ -32,6 +32,8 @@
struct iicbus_softc {
device_t owner; /* iicbus owner device structure */
u_char started; /* address of the 'started' slave
* 0 if no start condition succeeded */
};
extern devclass_t iicbus_devclass;

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iiconf.c,v 1.2 1998/10/31 11:31:07 nsouch Exp $
* $Id: iiconf.c,v 1.3 1998/11/22 22:01:42 nsouch Exp $
*
*/
#include <sys/param.h>
@ -65,9 +65,6 @@ iicbus_alloc_bus(device_t parent)
/* add the bus to the parent */
child = device_add_child(parent, "iicbus", -1, NULL);
if (child)
device_set_desc(child, "Philips I2C bus");
return (child);
}
@ -163,6 +160,99 @@ iicbus_release_bus(device_t bus, device_t dev)
return (0);
}
/*
* iicbus_started()
*
* Test if the iicbus is started by the controller
*/
int
iicbus_started(device_t bus)
{
struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
return (sc->started);
}
/*
* iicbus_start()
*
* Send start condition to the slave addressed by 'slave'
*/
int
iicbus_start(device_t bus, u_char slave, int timeout)
{
struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
int error = 0;
if (sc->started)
return (EINVAL); /* bus already started */
if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
sc->started = slave;
else
sc->started = 0;
return (error);
}
/*
* iicbus_stop()
*
* Send stop condition to the bus
*/
int
iicbus_stop(device_t bus)
{
struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
int error = 0;
if (!sc->started)
return (EINVAL); /* bus not started */
error = IICBUS_STOP(device_get_parent(bus));
/* refuse any further access */
sc->started = 0;
return (error);
}
/*
* iicbus_write()
*
* Write a block of data to the slave previously started by
* iicbus_start() call
*/
int
iicbus_write(device_t bus, char *buf, int len, int *sent, int timeout)
{
struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
/* a slave must have been started with the appropriate address */
if (!sc->started || (sc->started & LSB))
return (EINVAL);
return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
}
/*
* iicbus_read()
*
* Read a block of data from the slave previously started by
* iicbus_read() call
*/
int
iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
{
struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
/* a slave must have been started with the appropriate address */
if (!sc->started || !(sc->started & LSB))
return (EINVAL);
return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
}
/*
* iicbus_block_write()
*
@ -220,3 +310,4 @@ iicbus_get_addr(device_t dev)
return ((u_char)addr);
}

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iiconf.h,v 1.1.1.1 1998/09/03 20:51:50 nsouch Exp $
* $Id: iiconf.h,v 1.2 1998/10/31 11:31:07 nsouch Exp $
*/
#ifndef __IICONF_H
#define __IICONF_H
@ -108,17 +108,17 @@ extern int iicbus_null_callback(device_t, int, caddr_t);
#define iicbus_repeated_start(bus,slave,timeout) \
(IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout))
#define iicbus_start(bus,slave,timeout) \
(IICBUS_START(device_get_parent(bus), slave, timeout))
#define iicbus_stop(bus) \
(IICBUS_STOP(device_get_parent(bus)))
#define iicbus_reset(bus,speed,addr,oldaddr) \
(IICBUS_RESET(device_get_parent(bus), speed, addr, oldaddr))
#define iicbus_write(bus,buf,len,sent,timeout) \
(IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout))
#define iicbus_read(bus,buf,len,sent,last,delay) \
(IICBUS_READ(device_get_parent(bus), buf, len, sent, last, delay))
/* basic I2C operations */
extern int iicbus_started(device_t);
extern int iicbus_start(device_t, u_char, int);
extern int iicbus_stop(device_t);
extern int iicbus_write(device_t, char *, int, int *, int);
extern int iicbus_read(device_t, char *, int, int *, int, int);
/* Read/write operations with start/stop conditions managed */
extern int iicbus_block_write(device_t, u_char, char *, int, int *);
extern int iicbus_block_read(device_t, u_char, char *, int, int *);

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smb.c,v 1.3 1998/09/09 18:57:38 nsouch Exp $
* $Id: smb.c,v 1.4 1998/12/07 21:58:17 archie Exp $
*
*/
#include <sys/param.h>
@ -157,24 +157,9 @@ smbclose(dev_t dev, int flags, int fmt, struct proc *p)
static int
smbwrite(dev_t dev, struct uio * uio, int ioflag)
{
device_t smbdev = IIC_DEVICE(minor(dev));
struct smb_softc *sc = IIC_SOFTC(minor(dev));
int count;
/* not supported */
if (!sc || !smbdev)
return (EINVAL);
if (sc->sc_count == 0)
return (EINVAL);
count = min(uio->uio_resid, BUFSIZE);
uiomove(sc->sc_buffer, count, uio);
/* we consider the command char as the first character to send */
smbus_bwrite(device_get_parent(smbdev), sc->sc_addr,
sc->sc_buffer[0], count-1, sc->sc_buffer+1);
return (0);
return (EINVAL);
}
static int
@ -195,65 +180,62 @@ smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
int error = 0;
struct smbcmd *s = (struct smbcmd *)data;
if (!sc)
if (!sc || !s)
return (EINVAL);
switch (cmd) {
case SMB_QUICK_WRITE:
smbus_quick(parent, sc->sc_addr, SMB_QWRITE);
smbus_quick(parent, s->slave, SMB_QWRITE);
goto end;
case SMB_QUICK_READ:
smbus_quick(parent, sc->sc_addr, SMB_QREAD);
smbus_quick(parent, s->slave, SMB_QREAD);
goto end;
};
if (!s)
return (EINVAL);
switch (cmd) {
case SMB_SENDB:
smbus_sendb(parent, sc->sc_addr, s->cmd);
smbus_sendb(parent, s->slave, s->cmd);
break;
case SMB_RECVB:
smbus_recvb(parent, sc->sc_addr, &s->cmd);
smbus_recvb(parent, s->slave, &s->cmd);
break;
case SMB_WRITEB:
smbus_writeb(parent, sc->sc_addr, s->cmd, s->data.byte);
smbus_writeb(parent, s->slave, s->cmd, s->data.byte);
break;
case SMB_WRITEW:
smbus_writew(parent, sc->sc_addr, s->cmd, s->data.word);
smbus_writew(parent, s->slave, s->cmd, s->data.word);
break;
case SMB_READB:
if (s->data.byte_ptr)
smbus_readb(parent, sc->sc_addr, s->cmd,
smbus_readb(parent, s->slave, s->cmd,
s->data.byte_ptr);
break;
case SMB_READW:
if (s->data.word_ptr)
smbus_readw(parent, sc->sc_addr, s->cmd, s->data.word_ptr);
smbus_readw(parent, s->slave, s->cmd, s->data.word_ptr);
break;
case SMB_PCALL:
if (s->data.process.rdata)
smbus_pcall(parent, sc->sc_addr, s->cmd,
smbus_pcall(parent, s->slave, s->cmd,
s->data.process.sdata, s->data.process.rdata);
break;
case SMB_BWRITE:
if (s->count && s->data.byte_ptr)
smbus_bwrite(parent, sc->sc_addr, s->cmd, s->count,
smbus_bwrite(parent, s->slave, s->cmd, s->count,
s->data.byte_ptr);
break;
case SMB_BREAD:
if (s->count && s->data.byte_ptr)
smbus_bread(parent, sc->sc_addr, s->cmd, s->count,
smbus_bread(parent, s->slave, s->cmd, s->count,
s->data.byte_ptr);
break;

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smb.h,v 1.1 1998/08/13 17:13:20 son Exp $
* $Id: smb.h,v 1.1 1998/09/03 21:00:08 nsouch Exp $
*
*/
#ifndef __SMB_H
@ -34,6 +34,7 @@
struct smbcmd {
char cmd;
int count;
u_char slave;
union {
char byte;
short word;
@ -48,8 +49,8 @@ struct smbcmd {
} data;
};
#define SMB_QUICK_WRITE _IO('i', 1)
#define SMB_QUICK_READ _IO('i', 2)
#define SMB_QUICK_WRITE _IOW('i', 1, struct smbcmd)
#define SMB_QUICK_READ _IOW('i', 2, struct smbcmd)
#define SMB_SENDB _IOW('i', 3, struct smbcmd)
#define SMB_RECVB _IOW('i', 4, struct smbcmd)
#define SMB_WRITEB _IOW('i', 5, struct smbcmd)

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smbconf.c,v 1.2 1998/10/31 11:39:54 nsouch Exp $
* $Id: smbconf.c,v 1.3 1998/11/22 22:01:42 nsouch Exp $
*
*/
#include <sys/param.h>
@ -65,9 +65,6 @@ smbus_alloc_bus(device_t parent)
/* add the bus to the parent */
child = device_add_child(parent, "smbus", -1, NULL);
if (child)
device_set_desc(child, "System Management Bus");
return (child);
}

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smbus.c,v 1.5 1998/12/10 02:31:08 archie Exp $
* $Id: smbus.c,v 1.6 1998/12/28 19:07:51 nsouch Exp $
*
*/
#include <sys/param.h>
@ -159,3 +159,4 @@ smbus_read_ivar(device_t bus, device_t dev, int index, u_long* result)
DRIVER_MODULE(smbus, iicsmb, smbus_driver, smbus_devclass, 0, 0);
DRIVER_MODULE(smbus, bti2c, smbus_driver, smbus_devclass, 0, 0);
DRIVER_MODULE(smbus, intsmb, smbus_driver, smbus_devclass, 0, 0);
DRIVER_MODULE(smbus, smbv, smbus_driver, smbus_devclass, 0, 0);

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: iic.h,v 1.1.2.1 1998/07/17 00:54:01 son Exp $
* $Id: iic.h,v 1.1 1998/09/03 21:00:08 nsouch Exp $
*
*/
#ifndef __IIC_H
@ -31,10 +31,17 @@
#include <sys/ioccom.h>
#define I2CSTART _IO('i', 1) /* start condition */
#define I2CSTOP _IO('i', 2) /* stop condition */
#define I2CADDRESS _IOW('i', 3, long) /* address bus */
#define I2CRSTCARD _IOW('i', 4, long) /* reset the card */
struct iiccmd {
u_char slave;
int count;
int last;
char *buf;
};
#define I2CSTART _IOW('i', 1, struct iiccmd) /* start condition */
#define I2CSTOP _IO('i', 2) /* stop condition */
#define I2CRSTCARD _IOW('i', 3, struct iiccmd) /* reset the card */
#define I2CWRITE _IOW('i', 4, struct iiccmd) /* send data */
#define I2CREAD _IOW('i', 5, struct iiccmd) /* receive data */
#endif

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smb.h,v 1.1 1998/08/13 17:13:20 son Exp $
* $Id: smb.h,v 1.1 1998/09/03 21:00:08 nsouch Exp $
*
*/
#ifndef __SMB_H
@ -34,6 +34,7 @@
struct smbcmd {
char cmd;
int count;
u_char slave;
union {
char byte;
short word;
@ -48,8 +49,8 @@ struct smbcmd {
} data;
};
#define SMB_QUICK_WRITE _IO('i', 1)
#define SMB_QUICK_READ _IO('i', 2)
#define SMB_QUICK_WRITE _IOW('i', 1, struct smbcmd)
#define SMB_QUICK_READ _IOW('i', 2, struct smbcmd)
#define SMB_SENDB _IOW('i', 3, struct smbcmd)
#define SMB_RECVB _IOW('i', 4, struct smbcmd)
#define SMB_WRITEB _IOW('i', 5, struct smbcmd)