USB device detach events [PATCH - please test]

From: Johny Mattsson <lonewolf-freebsd_at_earthmagic.org>
Date: Tue, 21 Oct 2003 20:10:25 +1000
Hello,

As an answer to my previous email, I now have patches fixing the missing 
USB device detach events. This works extremely well on my system, but 
it'd be great if I could get some wider exposure before I send-pr them. 
 From my perusal/grepping of all the usb drivers, these patches 
shouldn't break anything, only allow usbd to receive DEVICE_DETACH 
events for all devices.


When I continued chasing my original problem, I discovered that the 
usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev) call in usb_subr.c was 
commented out. Bringing it back into play brought me a nice solid system 
freeze, followed by a reboot shortly thereafter. I figure that must be 
why it was commented out in the first place.

As it turns out, the placement of that call is really bad. The 'dev' it 
hands out is in an inconsistent state if any subdevs existed (if there 
were no subdevs, everything works fine). I say existed, because at the 
original location of the "add event" call, the subdevs have already been 
detached. The usbd_add_dev_event function however calls the 
usbd_fill_deviceinfo, which wants to fiddle with the subdevs as well, 
resulting in Bad Things happening. My patch to usb_subr.c simply moves 
the usbd_add_dev_event call to just before the subdevs are disconnected.

Also, the patch to usb.c guarantees (I believe) that if a DRIVER_DETACH 
event is added to the queue before the DEVICE_DETACH event has been 
processed, the DEVICE_DETACH is kept on the queue, resulting in expected 
behaviour by usbd.

Comments are welcome. If I don't hear anything from anyone, I'll send-pr 
in a couple of days.

Cheers,
/Johny
-- 
Johny Mattsson - System Designer ,-.   ,-.   ,-.  There is no truth.
http://www.earthmagic.org     _.'  `-'   `-'  There is only perception.

--- usb_subr.c.org	Tue Oct 21 17:44:48 2003
+++ usb_subr.c	Tue Oct 21 19:40:44 2003
_at__at_ -1353,6 +1353,8 _at__at_
 	}
 #endif
 
+	usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
+
 	if (dev->subdevs != NULL) {
 		DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
 		for (i = 0; dev->subdevs[i]; i++) {
_at__at_ -1365,7 +1367,6 _at__at_
 		}
 	}
 
-	/*usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);*/
 	dev->bus->devices[dev->address] = NULL;
 	up->device = NULL;
 	usb_free_device(dev);

--- usb.c.org	Tue Oct 21 16:51:25 2003
+++ usb.c	Tue Oct 21 19:42:00 2003
_at__at_ -781,7 +781,8 _at__at_
 		for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
 			ueqi_next = TAILQ_NEXT(ueqi, next);
 			if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
-			    uep->u.ue_device.udi_cookie.cookie) {
+			    uep->u.ue_device.udi_cookie.cookie &&
+			    !USB_EVENT_IS_DETACH(ueqi->ue.ue_type)) {
 				TAILQ_REMOVE(&usb_events, ueqi, next);
 				free(ueqi, M_USBDEV);
 				usb_nevents--;
Received on Tue Oct 21 2003 - 01:10:30 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:26 UTC