svn commit: r188081 - user/thompsa/usb/sys/dev/usb2/ethernet

Andrew Thompson thompsa at FreeBSD.org
Tue Feb 3 10:07:22 PST 2009


Author: thompsa
Date: Tue Feb  3 18:07:21 2009
New Revision: 188081
URL: http://svn.freebsd.org/changeset/base/188081

Log:
  Use a flag and cv when initing the hardware as the device could be detached
  while the driver init is sleeping.
  
  Spotted by:	HPS

Modified:
  user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.c
  user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.h

Modified: user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.c
==============================================================================
--- user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.c	Tue Feb  3 17:58:20 2009	(r188080)
+++ user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.c	Tue Feb  3 18:07:21 2009	(r188081)
@@ -27,6 +27,7 @@
 #include <dev/usb2/core/usb2_core.h>
 #include <dev/usb2/core/usb2_process.h>
 #include <dev/usb2/core/usb2_busdma.h>
+#include <dev/usb2/core/usb2_util.h>
 #include <dev/usb2/ethernet/usb2_ethernet.h>
 
 SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 0, "USB Ethernet parameters");
@@ -102,6 +103,7 @@ usb2_ether_ifattach(struct usb2_ether *u
 	USB_TASK_INIT(&ue->ue_tick_task, ue_tick_task, ue, ue->ue_mtx);
 	usb2_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0);
 	sysctl_ctx_init(&ue->ue_sysctl_ctx);
+	usb2_cv_init(&ue->ue_cv, device_get_nameunit(ue->ue_dev));
 
 	error = usb2_proc_create(&ue->ue_tq, USB_PRI_MED,
 	    device_get_nameunit(ue->ue_dev));
@@ -172,6 +174,12 @@ usb2_ether_ifdetach(struct usb2_ether *u
 	KASSERT((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0,
 	    ("ifnet still running"));
 
+	UE_LOCK(ue);
+	/* Wait if the interface init routine is still running */
+	while (ue->ue_flags & UE_FLAG_INIT)
+		usb2_cv_wait(&ue->ue_cv, ue->ue_mtx);
+	UE_UNLOCK(ue);
+
 	usb2_callout_drain(&ue->ue_watchdog);
 	usb2_proc_free(&ue->ue_tq);
 	if (ue->ue_miibus)
@@ -179,6 +187,7 @@ usb2_ether_ifdetach(struct usb2_ether *u
 	ether_ifdetach(ifp);
 	if_free(ifp);
 	sysctl_ctx_free(&ue->ue_sysctl_ctx);
+	usb2_cv_destroy(&ue->ue_cv);
 	free_unr(ueunit, ue->ue_unit);
 }
 
@@ -199,7 +208,15 @@ ue_init_locked(struct usb2_ether *ue)
 
 	UE_LOCK_ASSERT(ue, MA_OWNED);
 
+	/*
+	 * Init the hardware, this is flagged as the device could be
+	 * detached while the driver init is sleeping.
+	 */
+	ue->ue_flags |= UE_FLAG_INIT;
 	ue->ue_init(ue->ue_sc);
+	ue->ue_flags &= ~UE_FLAG_INIT;
+	usb2_cv_signal(&ue->ue_cv);
+
 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && ue->ue_tick != NULL)
 		usb2_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
 }

Modified: user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.h
==============================================================================
--- user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.h	Tue Feb  3 17:58:20 2009	(r188080)
+++ user/thompsa/usb/sys/dev/usb2/ethernet/usb2_ethernet.h	Tue Feb  3 18:07:21 2009	(r188081)
@@ -63,6 +63,10 @@ struct usb2_ether {
 
 	struct ifqueue		ue_rxq;
 
+	int			ue_flags;
+#define UE_FLAG_INIT		1
+
+	struct cv		ue_cv;
 	struct usb2_callout	ue_watchdog;
 	struct usb2_task	ue_media_task;
 	struct usb2_task	ue_multi_task;


More information about the svn-src-user mailing list