Subject: Re: Device insertion / removal notification
To: None <explorer@flame.org>
From: M. Warner Losh <imp@bsdimp.com>
List: tech-kern
Date: 08/08/2004 08:06:46
In message: <200408051051.39356@gryphon.flame.org>
            Michael Graff <explorer@flame.org> writes:
: -----BEGIN PGP SIGNED MESSAGE-----
: Hash: SHA1
: 
: Has anyone thought about a way to do this, probably with a userland daemon 
: (perhaps multiple, one per user or something) to act upon device insertion?
: 
: For instance, I have a "car GPS / mp3 player" and that means two devices:
: 
: 	Garmin StreetPilot 2610
: 	External keypad to control xmms :)
: 
: However, the procedure to hot-plug these devices is currently:
: 
: 	int fd = -1;
: 	while (fd < 0) {
: 		fd = open();
: 			if (fd < 0)
: 				sleep(1);
: 	}
: 
: or for usbhidaction, a shell script to restart it when it dies.
: 
: This really isn't optimal.  :)
: 
: I was thinking of using kqueue, but the kevent structure doesn't have any way 
: to extend the structure to include additional fields beyond the int64_t data 
: field.  I suppose I could stuff a pointer in there, but that seems like a 
: hack.  I could add an external data pointer and length, but now the caller 
: would need to clean up the event's additional data.
: 
: Do I write yet another method to do this, or should I hack on kqueue to stuff 
: this in?

I did exactly this on FreeBSD with a simple device driver that
collects the events and reports them over a socket.  A daemon then
reads this data stream and dispatches programs based on a scripting
language.  It also reports the data on a pipe so that other programs
can listen to it.  I didn't bother with kqueue because I wanted more
information than kqueue's data structures allows.  The driver was
dependent on FreeBSD's newbus which has well defined events that were
converted.

I also did it on driver attach, driver detach and unknown device (eg
no driver known) events, rather than on a strict hardware inserted.

Warner