]> xenbits.xensource.com Git - people/julieng/freebsd.git/commitdiff
Replace a local sx lock that allowed only one client at a time to access
authorian <ian@FreeBSD.org>
Sat, 10 Oct 2015 19:51:00 +0000 (19:51 +0000)
committerian <ian@FreeBSD.org>
Sat, 10 Oct 2015 19:51:00 +0000 (19:51 +0000)
an eeprom device with iicbus_request/release_bus(), which achieves the
same effect and also keeps other i2c slave drivers from clashing on the bus.

sys/dev/iicbus/icee.c

index 2f6e923c72e362935545df5e9fb55ae93b4f9aa3..800ec4c6dff720299c1e2e66dbb62a589770dce3 100644 (file)
@@ -48,7 +48,7 @@ __FBSDID("$FreeBSD$");
 
 struct icee_softc {
        device_t        sc_dev;         /* Myself */
-       struct sx       sc_lock;        /* basically a perimeter lock */
+       device_t        sc_busdev;      /* Parent bus */
        struct cdev     *cdev;          /* user interface */
        int             addr;
        int             size;           /* How big am I? */
@@ -57,12 +57,6 @@ struct icee_softc {
        int             wr_sz;          /* What's the write page size */
 };
 
-#define ICEE_LOCK(_sc)         sx_xlock(&(_sc)->sc_lock)
-#define        ICEE_UNLOCK(_sc)        sx_xunlock(&(_sc)->sc_lock)
-#define ICEE_LOCK_INIT(_sc)    sx_init(&_sc->sc_lock, "icee")
-#define ICEE_LOCK_DESTROY(_sc) sx_destroy(&_sc->sc_lock);
-#define ICEE_ASSERT_LOCKED(_sc)        sx_assert(&_sc->sc_lock, SA_XLOCKED);
-#define ICEE_ASSERT_UNLOCKED(_sc) sx_assert(&_sc->sc_lock, SA_UNLOCKED);
 #define CDEV2SOFTC(dev)                ((dev)->si_drv1)
 
 /* cdev routines */
@@ -97,6 +91,7 @@ icee_attach(device_t dev)
        int dunit, err;
 
        sc->sc_dev = dev;
+       sc->sc_busdev = device_get_parent(sc->sc_dev);
        sc->addr = iicbus_get_addr(dev);
        err = 0;
        dname = device_get_name(dev);
@@ -117,7 +112,6 @@ icee_attach(device_t dev)
                goto out;
        }
        sc->cdev->si_drv1 = sc;
-       ICEE_LOCK_INIT(sc);
 out:
        return (err);
 }
@@ -155,7 +149,9 @@ icee_read(struct cdev *dev, struct uio *uio, int ioflag)
                return (EIO);
        if (sc->type != 8 && sc->type != 16)
                return (EINVAL);
-       ICEE_LOCK(sc);
+       error = iicbus_request_bus(sc->sc_busdev, sc->sc_dev, IIC_INTRWAIT);
+       if (error!= 0)
+               return (iic2errno(error));
        slave = error = 0;
        while (uio->uio_resid > 0) {
                if (uio->uio_offset >= sc->size)
@@ -188,7 +184,7 @@ icee_read(struct cdev *dev, struct uio *uio, int ioflag)
                if (error)
                        break;
        }
-       ICEE_UNLOCK(sc);
+       iicbus_release_bus(sc->sc_busdev, sc->sc_dev);
        return (error);
 }
 
@@ -216,7 +212,10 @@ icee_write(struct cdev *dev, struct uio *uio, int ioflag)
                return (EIO);
        if (sc->type != 8 && sc->type != 16)
                return (EINVAL);
-       ICEE_LOCK(sc);
+
+       error = iicbus_request_bus(sc->sc_busdev, sc->sc_dev, IIC_INTRWAIT);
+       if (error!= 0)
+               return (iic2errno(error));
        slave = error = 0;
        while (uio->uio_resid > 0) {
                if (uio->uio_offset >= sc->size)
@@ -256,7 +255,7 @@ icee_write(struct cdev *dev, struct uio *uio, int ioflag)
                        break;
                }
        }
-       ICEE_UNLOCK(sc);
+       iicbus_release_bus(sc->sc_busdev, sc->sc_dev);
        return error;
 }