summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2008-06-06 18:45:32 +0000
committerjhb <jhb@FreeBSD.org>2008-06-06 18:45:32 +0000
commit89ebac28714b8dce44ae9bf04610ec915e0bd633 (patch)
treeb4366ef731ab7b1f24493611ae302dab66ee53e8 /sys
parenta7ed9169932600f570091db7f67266f2b20a938a (diff)
downloadFreeBSD-src-89ebac28714b8dce44ae9bf04610ec915e0bd633.zip
FreeBSD-src-89ebac28714b8dce44ae9bf04610ec915e0bd633.tar.gz
- Store the device_t of the smbX device in the softc.
- Store the softc of the device in the 'si_drv1' of the cdev. - Lookup the softc via 'si_drv1' in cdev methods rather than using the minor number as a unit for devclass_get_softc(). - Lookup the device_t via the softc field in cdev methods rather than using the minor number as a unit for devclass_get_device(). - Add a mutex to the softc to protect 'sc_opened'. - Remove D_NEEDGIANT as all the smbus drivers are now MPSAFE and this driver is now MPSAFE. - Remove some checks for NULL softc pointers that can't happen and don't bzero the softc during attach.
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/smbus/smb.c59
1 files changed, 22 insertions, 37 deletions
diff --git a/sys/dev/smbus/smb.c b/sys/dev/smbus/smb.c
index 6fce9b2..df3ebe9 100644
--- a/sys/dev/smbus/smb.c
+++ b/sys/dev/smbus/smb.c
@@ -48,17 +48,12 @@
#define BUFSIZE 1024
struct smb_softc {
-
+ device_t sc_dev;
int sc_count; /* >0 if device opened */
struct cdev *sc_devnode;
+ struct mtx sc_lock;
};
-#define IIC_SOFTC(unit) \
- ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
-
-#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);
@@ -91,7 +86,7 @@ static d_ioctl_t smbioctl;
static struct cdevsw smb_cdevsw = {
.d_version = D_VERSION,
- .d_flags = D_NEEDGIANT,
+ .d_flags = D_TRACKCLOSE,
.d_open = smbopen,
.d_close = smbclose,
.d_ioctl = smbioctl,
@@ -117,16 +112,13 @@ smb_probe(device_t dev)
static int
smb_attach(device_t dev)
{
- struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
-
- if (!sc)
- return (ENOMEM);
-
- bzero(sc, sizeof(struct smb_softc *));
+ struct smb_softc *sc = device_get_softc(dev);
+ sc->sc_dev = dev;
sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev),
- UID_ROOT, GID_WHEEL,
- 0600, "smb%d", device_get_unit(dev));
+ UID_ROOT, GID_WHEEL, 0600, "smb%d", device_get_unit(dev));
+ sc->sc_devnode->si_drv1 = sc;
+ mtx_init(&sc->sc_lock, device_get_nameunit(dev), NULL, MTX_DEF);
return (0);
}
@@ -138,22 +130,24 @@ smb_detach(device_t dev)
if (sc->sc_devnode)
destroy_dev(sc->sc_devnode);
+ mtx_destroy(&sc->sc_lock);
return (0);
}
static int
-smbopen (struct cdev *dev, int flags, int fmt, struct thread *td)
+smbopen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
- struct smb_softc *sc = IIC_SOFTC(minor(dev));
+ struct smb_softc *sc = dev->si_drv1;
- if (sc == NULL)
- return (ENXIO);
-
- if (sc->sc_count != 0)
+ mtx_lock(&sc->sc_lock);
+ if (sc->sc_count != 0) {
+ mtx_unlock(&sc->sc_lock);
return (EBUSY);
+ }
sc->sc_count++;
+ mtx_unlock(&sc->sc_lock);
return (0);
}
@@ -161,16 +155,12 @@ smbopen (struct cdev *dev, int flags, int fmt, struct thread *td)
static int
smbclose(struct cdev *dev, int flags, int fmt, struct thread *td)
{
- struct smb_softc *sc = IIC_SOFTC(minor(dev));
-
- if (sc == NULL)
- return (ENXIO);
-
- if (sc->sc_count == 0)
- /* This is not supposed to happen. */
- return (0);
+ struct smb_softc *sc = dev->si_drv1;
+ mtx_lock(&sc->sc_lock);
+ KASSERT(sc->sc_count == 1, ("device not busy"));
sc->sc_count--;
+ mtx_unlock(&sc->sc_lock);
return (0);
}
@@ -181,18 +171,13 @@ smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *t
char buf[SMB_MAXBLOCKSIZE];
device_t parent;
struct smbcmd *s = (struct smbcmd *)data;
- struct smb_softc *sc = IIC_SOFTC(minor(dev));
- device_t smbdev = IIC_DEVICE(minor(dev));
+ struct smb_softc *sc = dev->si_drv1;
+ device_t smbdev = sc->sc_dev;
int error;
short w;
u_char count;
char c;
- if (sc == NULL)
- return (ENXIO);
- if (s == NULL)
- return (EINVAL);
-
parent = device_get_parent(smbdev);
/* Allocate the bus. */
OpenPOWER on IntegriCloud