Subject: Re: Detaching devices
To: Bill Studenmund <wrstuden@nas.nasa.gov>
From: Jason Thorpe <thorpej@nas.nasa.gov>
List: tech-kern
Date: 06/28/1999 12:46:18
On Mon, 28 Jun 1999 12:05:45 -0700 (PDT) 
 Bill Studenmund <wrstuden@nas.nasa.gov> wrote:

 > that's what reference counting is for. :-) What you/we need to do is add
 > foo_addref and foo_delref routines. When all the references have been
 > deleted, the memory goes away.

guys, this is not that hard...

	(1) USB controller gets "status event", and wakes up USB
	    controller's "event thread".

	(2) USB controller's "event thread" determines the type of status
	    event (i.e. "removal" in this case).  Do this in process context
	    (i.e. the "event thread") so you can sleep while this happens.
	    "Event thread" calls a USB stack interface function to the
	    device (i.e. some pointer in a function switch, *not*
	    config_detach()).

	(3) Device driver takes appropriate action, e.g. vdevgone()'s the
	    devices, sets a flag in its softc "dying", or whatever.  Any
	    things asleep in the driver are now awakened.

	(4) As things are awakened, they see the "dying" bit set, and return
	    errors.  When the last of these things is awakened, there are
	    no more references to the device (open/read/write fail, close
	    is stub'd to a "OK!  WORKED!" in deadfs, because of the
	    vdevgone()).  At this point, you can config_detach() (either
	    directly or via a callback into the USB controller code; probably
	    the latter so it can know "ok, it's really gone now").

Anyhow, this mechanism can probably be cleaned up a little, but you get the
idea.

        -- Jason R. Thorpe <thorpej@nas.nasa.gov>