Author: ian
Date: Sun Aug 11 21:21:32 2019
New Revision: 350870
URL: https://svnweb.freebsd.org/changeset/base/350870

Log:
  MFC r349839, r349850
  
  r349839:
  Call device_unbusy() on the error exit path, because if iicbus_request_bus()
  returns an error, iicbus_release_bus() is not going to be called.
  
  r349850:
  Restore the ability for i2c slave devices to do IO from their probe method.
  
  r348164 added code to iicbus_request_bus/iicbus_release_bus to automatically
  call device_busy()/device_unbusy() as part of aquiring exclusive use of the
  bus (so modules can't be unloaded while the bus is exclusively owned and/or
  IO is in progress).  That broke the ability to do i2c IO from a slave device
  probe method, because the slave isn't attached yet, so calling device_busy()
  triggers a sanity-check panic for trying to busy a non-attached device.
  
  Now we check whether the device status is < DS_ATTACHING, and if so we busy
  the iicbus rather than the slave device.  I think this leaves a small window
  where a module could be unloaded while probing is in progress.  But I think
  that's true of all devices, and probably should be fixed by introducing a
  DS_PROBING state for devices, and handling that at various points in the
  newbus code.

Modified:
  stable/12/sys/dev/iicbus/iicbus.h
  stable/12/sys/dev/iicbus/iiconf.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/dev/iicbus/iicbus.h
==============================================================================
--- stable/12/sys/dev/iicbus/iicbus.h   Sun Aug 11 21:15:30 2019        
(r350869)
+++ stable/12/sys/dev/iicbus/iicbus.h   Sun Aug 11 21:21:32 2019        
(r350870)
@@ -41,6 +41,7 @@ struct iicbus_softc
 {
        device_t dev;           /* Myself */
        device_t owner;         /* iicbus owner device structure */
+       device_t busydev;       /* iicbus_release_bus calls unbusy on this */
        u_int owncount;         /* iicbus ownership nesting count */
        u_char started;         /* address of the 'started' slave
                                 * 0 if no start condition succeeded */

Modified: stable/12/sys/dev/iicbus/iiconf.c
==============================================================================
--- stable/12/sys/dev/iicbus/iiconf.c   Sun Aug 11 21:15:30 2019        
(r350869)
+++ stable/12/sys/dev/iicbus/iiconf.c   Sun Aug 11 21:21:32 2019        
(r350870)
@@ -131,9 +131,15 @@ iicbus_request_bus(device_t bus, device_t dev, int how
                        /*
                         * Mark the device busy while it owns the bus, to
                         * prevent detaching the device, bus, or hardware
-                        * controller, until ownership is relinquished.
+                        * controller, until ownership is relinquished.  If the
+                        * device is doing IO from its probe method before
+                        * attaching, it cannot be busied; mark the bus busy.
                         */
-                       device_busy(dev);
+                       if (device_get_state(dev) < DS_ATTACHING)
+                               sc->busydev = bus;
+                       else
+                               sc->busydev = dev;
+                       device_busy(sc->busydev);
                        /* 
                         * Drop the lock around the call to the bus driver, it
                         * should be allowed to sleep in the IIC_WAIT case.
@@ -150,6 +156,7 @@ iicbus_request_bus(device_t bus, device_t dev, int how
                                sc->owner = NULL;
                                sc->owncount = 0;
                                wakeup_one(sc);
+                               device_unbusy(sc->busydev);
                        }
                }
        }
@@ -183,7 +190,7 @@ iicbus_release_bus(device_t bus, device_t dev)
                IICBUS_LOCK(sc);
                sc->owner = NULL;
                wakeup_one(sc);
-               device_unbusy(dev);
+               device_unbusy(sc->busydev);
        }
        IICBUS_UNLOCK(sc);
        return (0);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to