Module Name:    src
Committed By:   skrll
Date:           Wed Jan 15 08:20:13 UTC 2020

Modified Files:
        src/sys/dev/usb: if_otus.c if_otusvar.h if_upgt.c if_upgtvar.h
            if_urtwn.c if_urtwnvar.h if_zyd.c if_zydreg.h

Log Message:
Use modern interfaces and kill tsleep/wakeup.  From nick-nhusb branch.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/dev/usb/if_otus.c
cvs rdiff -u -r1.10 -r1.11 src/sys/dev/usb/if_otusvar.h \
    src/sys/dev/usb/if_zydreg.h
cvs rdiff -u -r1.26 -r1.27 src/sys/dev/usb/if_upgt.c
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/usb/if_upgtvar.h
cvs rdiff -u -r1.79 -r1.80 src/sys/dev/usb/if_urtwn.c
cvs rdiff -u -r1.14 -r1.15 src/sys/dev/usb/if_urtwnvar.h
cvs rdiff -u -r1.53 -r1.54 src/sys/dev/usb/if_zyd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/usb/if_otus.c
diff -u src/sys/dev/usb/if_otus.c:1.39 src/sys/dev/usb/if_otus.c:1.40
--- src/sys/dev/usb/if_otus.c:1.39	Sat Sep 14 12:37:34 2019
+++ src/sys/dev/usb/if_otus.c	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_otus.c,v 1.39 2019/09/14 12:37:34 maxv Exp $	*/
+/*	$NetBSD: if_otus.c,v 1.40 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_otus.c,v 1.18 2010/08/27 17:08:00 jsg Exp $	*/
 
 /*-
@@ -23,7 +23,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_otus.c,v 1.39 2019/09/14 12:37:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_otus.c,v 1.40 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -630,6 +630,8 @@ otus_attach(device_t parent, device_t se
 	aprint_normal_dev(sc->sc_dev, "%s\n", devinfop);
 	usbd_devinfo_free(devinfop);
 
+	cv_init(&sc->sc_task_cv, "otustsk");
+	cv_init(&sc->sc_cmd_cv, "otuscmd");
 	mutex_init(&sc->sc_cmd_mtx,   MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(&sc->sc_task_mtx,  MUTEX_DEFAULT, IPL_NET);
 	mutex_init(&sc->sc_tx_mtx,    MUTEX_DEFAULT, IPL_NONE);
@@ -678,8 +680,10 @@ otus_wait_async(struct otus_softc *sc)
 
 	DPRINTFN(DBG_FN, sc, "\n");
 
+	mutex_spin_enter(&sc->sc_task_mtx);
 	while (sc->sc_cmdq.queued > 0)
-		tsleep(&sc->sc_cmdq, 0, "sc_cmdq", 0);
+		cv_wait(&sc->sc_task_cv, &sc->sc_task_mtx);
+	mutex_spin_exit(&sc->sc_task_mtx);
 }
 
 Static int
@@ -720,6 +724,9 @@ otus_detach(device_t self, int flags)
 	mutex_destroy(&sc->sc_tx_mtx);
 	mutex_destroy(&sc->sc_task_mtx);
 	mutex_destroy(&sc->sc_cmd_mtx);
+	cv_destroy(&sc->sc_task_cv);
+	cv_destroy(&sc->sc_cmd_cv);
+
 	return 0;
 }
 
@@ -1242,34 +1249,29 @@ otus_task(void *arg)
 	struct otus_softc *sc;
 	struct otus_host_cmd_ring *ring;
 	struct otus_host_cmd *cmd;
-	int s;
 
 	sc = arg;
 
 	DPRINTFN(DBG_FN, sc, "\n");
 
 	/* Process host commands. */
-	s = splusb();
 	mutex_spin_enter(&sc->sc_task_mtx);
 	ring = &sc->sc_cmdq;
 	while (ring->next != ring->cur) {
 		cmd = &ring->cmd[ring->next];
 		mutex_spin_exit(&sc->sc_task_mtx);
-		splx(s);
 
 		/* Callback. */
 		DPRINTFN(DBG_CMD, sc, "cb=%p queued=%d\n", cmd->cb,
 		    ring->queued);
 		cmd->cb(sc, cmd->data);
 
-		s = splusb();
 		mutex_spin_enter(&sc->sc_task_mtx);
 		ring->queued--;
 		ring->next = (ring->next + 1) % OTUS_HOST_CMD_RING_COUNT;
 	}
+	cv_signal(&sc->sc_task_cv);
 	mutex_spin_exit(&sc->sc_task_mtx);
-	wakeup(ring);
-	splx(s);
 }
 
 Static void
@@ -1278,12 +1280,10 @@ otus_do_async(struct otus_softc *sc, voi
 {
 	struct otus_host_cmd_ring *ring;
 	struct otus_host_cmd *cmd;
-	int s;
+	bool sched = false;
 
 	DPRINTFN(DBG_FN, sc, "cb=%p\n", cb);
 
-
-	s = splusb();
 	mutex_spin_enter(&sc->sc_task_mtx);
 	ring = &sc->sc_cmdq;
 	cmd = &ring->cmd[ring->cur];
@@ -1294,13 +1294,12 @@ otus_do_async(struct otus_softc *sc, voi
 
 	/* If there is no pending command already, schedule a task. */
 	if (++ring->queued == 1) {
-		mutex_spin_exit(&sc->sc_task_mtx);
-		usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
+		sched = true;
 	}
-	else
-		mutex_spin_exit(&sc->sc_task_mtx);
-	wakeup(ring);
-	splx(s);
+	cv_signal(&sc->sc_task_cv);
+	mutex_spin_exit(&sc->sc_task_mtx);
+	if (sched)
+		usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
 }
 
 Static int
@@ -1401,7 +1400,7 @@ otus_cmd(struct otus_softc *sc, uint8_t 
 {
 	struct otus_tx_cmd *cmd;
 	struct ar_cmd_hdr *hdr;
-	int s, xferlen, error;
+	int xferlen, error;
 
 	DPRINTFN(DBG_FN, sc, "\n");
 
@@ -1427,14 +1426,12 @@ otus_cmd(struct otus_softc *sc, uint8_t 
 	DPRINTFN(DBG_CMD, sc, "sending command code=0x%02x len=%d token=%d\n",
 	    code, ilen, hdr->token);
 
-	s = splusb();
 	cmd->odata = odata;
 	cmd->done = 0;
 	usbd_setup_xfer(cmd->xfer, cmd, cmd->buf, xferlen,
 	    USBD_FORCE_SHORT_XFER, OTUS_CMD_TIMEOUT, NULL);
 	error = usbd_sync_transfer(cmd->xfer);
 	if (error != 0) {
-		splx(s);
 		mutex_exit(&sc->sc_cmd_mtx);
 #if defined(DIAGNOSTIC) || defined(OTUS_DEBUG)	/* XXX: kill some noise */
 		aprint_error_dev(sc->sc_dev,
@@ -1444,9 +1441,8 @@ otus_cmd(struct otus_softc *sc, uint8_t 
 		return EIO;
 	}
 	if (!cmd->done)
-		error = tsleep(cmd, PCATCH, "otuscmd", hz);
+		error = cv_timedwait_sig(&sc->sc_cmd_cv, &sc->sc_cmd_mtx, hz);
 	cmd->odata = NULL;	/* In case answer is received too late. */
-	splx(s);
 	mutex_exit(&sc->sc_cmd_mtx);
 	if (error != 0) {
 		aprint_error_dev(sc->sc_dev,
@@ -1501,7 +1497,7 @@ otus_node_alloc(struct ieee80211_node_ta
 	DPRINTFN(DBG_FN, DBG_NO_SC, "\n");
 
 	on = malloc(sizeof(*on), M_DEVBUF, M_NOWAIT | M_ZERO);
-	return &on->ni;
+	return on ? &on->ni : NULL;
 }
 
 Static int
@@ -1647,14 +1643,18 @@ otus_cmd_rxeof(struct otus_softc *sc, ui
 	if ((hdr->code & 0xc0) != 0xc0) {
 		DPRINTFN(DBG_RX, sc, "received reply code=0x%02x len=%d token=%d\n",
 		    hdr->code, hdr->len, hdr->token);
+		mutex_enter(&sc->sc_cmd_mtx);
 		cmd = &sc->sc_tx_cmd;
-		if (__predict_false(hdr->token != cmd->token))
+		if (__predict_false(hdr->token != cmd->token)) {
+			mutex_exit(&sc->sc_cmd_mtx);
 			return;
+		}
 		/* Copy answer into caller's supplied buffer. */
 		if (cmd->odata != NULL)
 			memcpy(cmd->odata, &hdr[1], hdr->len);
 		cmd->done = 1;
-		wakeup(cmd);
+		cv_signal(&sc->sc_cmd_cv);
+		mutex_exit(&sc->sc_cmd_mtx);
 		return;
 	}
 

Index: src/sys/dev/usb/if_otusvar.h
diff -u src/sys/dev/usb/if_otusvar.h:1.10 src/sys/dev/usb/if_otusvar.h:1.11
--- src/sys/dev/usb/if_otusvar.h:1.10	Sat Oct  5 23:27:20 2019
+++ src/sys/dev/usb/if_otusvar.h	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_otusvar.h,v 1.10 2019/10/05 23:27:20 mrg Exp $	*/
+/*	$NetBSD: if_otusvar.h,v 1.11 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_otusreg.h,v 1.6 2009/04/06 18:17:01 damien Exp $	*/
 
 /*-
@@ -199,6 +199,8 @@ struct otus_softc {
 	unsigned int			sc_write_idx;
 	uint32_t			sc_led_state;
 
+	kcondvar_t			sc_task_cv;
+	kcondvar_t			sc_cmd_cv;
 	kmutex_t			sc_cmd_mtx;
 	kmutex_t			sc_task_mtx;
 	kmutex_t			sc_write_mtx;
Index: src/sys/dev/usb/if_zydreg.h
diff -u src/sys/dev/usb/if_zydreg.h:1.10 src/sys/dev/usb/if_zydreg.h:1.11
--- src/sys/dev/usb/if_zydreg.h:1.10	Sat Oct  5 23:27:20 2019
+++ src/sys/dev/usb/if_zydreg.h	Wed Jan 15 08:20:13 2020
@@ -1,5 +1,5 @@
 /*	$OpenBSD: if_zydreg.h,v 1.19 2006/11/30 19:28:07 damien Exp $	*/
-/*	$NetBSD: if_zydreg.h,v 1.10 2019/10/05 23:27:20 mrg Exp $	*/
+/*	$NetBSD: if_zydreg.h,v 1.11 2020/01/15 08:20:13 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2006 by Damien Bergamini <damien.bergam...@free.fr>
@@ -1195,6 +1195,9 @@ struct zyd_softc {
 
 	struct ieee80211_amrr		amrr;
 
+	kmutex_t			sc_lock;
+	kcondvar_t			sc_cmdcv;
+
 	SIMPLEQ_HEAD(rqh, rq) sc_rqh;
 
 	uint16_t			fwbase;

Index: src/sys/dev/usb/if_upgt.c
diff -u src/sys/dev/usb/if_upgt.c:1.26 src/sys/dev/usb/if_upgt.c:1.27
--- src/sys/dev/usb/if_upgt.c:1.26	Sat Sep 14 12:53:24 2019
+++ src/sys/dev/usb/if_upgt.c	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_upgt.c,v 1.26 2019/09/14 12:53:24 maxv Exp $	*/
+/*	$NetBSD: if_upgt.c,v 1.27 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_upgt.c,v 1.49 2010/04/20 22:05:43 tedu Exp $ */
 
 /*
@@ -18,7 +18,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_upgt.c,v 1.26 2019/09/14 12:53:24 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_upgt.c,v 1.27 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1017,7 +1017,11 @@ upgt_eeprom_read(struct upgt_softc *sc)
 			    "could not transmit EEPROM data URB\n");
 			return EIO;
 		}
-		if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) {
+
+		mutex_enter(&sc->sc_mtx);
+		int res = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, UPGT_USB_TIMEOUT);
+		mutex_exit(&sc->sc_mtx);
+		if (res) {
 			aprint_error_dev(sc->sc_dev,
 			    "timeout while waiting for EEPROM data\n");
 			return EIO;
@@ -1792,12 +1796,15 @@ upgt_rx_cb(struct usbd_xfer *xfer, void 
 		DPRINTF(2, "%s: received EEPROM block (offset=%d, len=%d)\n",
 			device_xname(sc->sc_dev), eeprom_offset, eeprom_len);
 
+		mutex_enter(&sc->sc_mtx);
 		memcpy(sc->sc_eeprom + eeprom_offset,
 		    data_rx->buf + sizeof(struct upgt_lmac_eeprom) + 4,
 		    eeprom_len);
 
-		/* EEPROM data has arrived in time, wakeup tsleep() */
-		wakeup(sc);
+		/* EEPROM data has arrived in time, wakeup upgt_eeprom_read */
+		/* Note eeprom data arrived */
+		cv_broadcast(&sc->sc_cv);
+		mutex_exit(&sc->sc_mtx);
 	} else
 	if (h1_type == UPGT_H1_TYPE_CTRL &&
 	    h2_type == UPGT_H2_TYPE_TX_DONE) {
@@ -2322,6 +2329,7 @@ upgt_alloc_cmd(struct upgt_softc *sc)
 
 	data_cmd->buf = usbd_get_buffer(data_cmd->xfer);
 
+	cv_init(&sc->sc_cv, "upgteeprom");
 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
 
 	return 0;
@@ -2368,6 +2376,7 @@ upgt_free_cmd(struct upgt_softc *sc)
 	}
 
 	mutex_destroy(&sc->sc_mtx);
+	cv_destroy(&sc->sc_cv);
 }
 
 static int

Index: src/sys/dev/usb/if_upgtvar.h
diff -u src/sys/dev/usb/if_upgtvar.h:1.4 src/sys/dev/usb/if_upgtvar.h:1.5
--- src/sys/dev/usb/if_upgtvar.h:1.4	Sat Oct  5 23:27:20 2019
+++ src/sys/dev/usb/if_upgtvar.h	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_upgtvar.h,v 1.4 2019/10/05 23:27:20 mrg Exp $	*/
+/*	$NetBSD: if_upgtvar.h,v 1.5 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_upgtvar.h,v 1.15 2009/08/10 20:02:19 deraadt Exp $ */
 
 /*
@@ -416,6 +416,7 @@ struct upgt_softc {
 	struct upgt_data	 cmd_data;
 	int			 tx_queued;
 	kmutex_t		 sc_mtx;
+	kcondvar_t		 sc_cv;
 
 	uint8_t			 sc_device_type;
 	struct ieee80211com	 sc_ic;

Index: src/sys/dev/usb/if_urtwn.c
diff -u src/sys/dev/usb/if_urtwn.c:1.79 src/sys/dev/usb/if_urtwn.c:1.80
--- src/sys/dev/usb/if_urtwn.c:1.79	Wed Jan 15 08:13:20 2020
+++ src/sys/dev/usb/if_urtwn.c	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_urtwn.c,v 1.79 2020/01/15 08:13:20 skrll Exp $	*/
+/*	$NetBSD: if_urtwn.c,v 1.80 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_urtwn.c,v 1.42 2015/02/10 23:25:46 mpi Exp $	*/
 
 /*-
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_urtwn.c,v 1.79 2020/01/15 08:13:20 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_urtwn.c,v 1.80 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -395,6 +395,7 @@ urtwn_attach(device_t parent, device_t s
 
 	(void) usbd_do_request(sc->sc_udev, &req, 0);
 
+	cv_init(&sc->sc_task_cv, "urtwntsk");
 	mutex_init(&sc->sc_task_mtx, MUTEX_DEFAULT, IPL_NET);
 	mutex_init(&sc->sc_tx_mtx, MUTEX_DEFAULT, IPL_NONE);
 	mutex_init(&sc->sc_rx_mtx, MUTEX_DEFAULT, IPL_NONE);
@@ -591,6 +592,7 @@ urtwn_detach(device_t self, int flags)
 	callout_destroy(&sc->sc_scan_to);
 	callout_destroy(&sc->sc_calib_to);
 
+	cv_destroy(&sc->sc_task_cv);
 	mutex_destroy(&sc->sc_write_mtx);
 	mutex_destroy(&sc->sc_fwcmd_mtx);
 	mutex_destroy(&sc->sc_tx_mtx);
@@ -889,8 +891,8 @@ urtwn_task(void *arg)
 		ring->queued--;
 		ring->next = (ring->next + 1) % URTWN_HOST_CMD_RING_COUNT;
 	}
+	cv_broadcast(&sc->sc_task_cv);
 	mutex_spin_exit(&sc->sc_task_mtx);
-	wakeup(&sc->cmdq);
 	splx(s);
 }
 
@@ -930,8 +932,10 @@ urtwn_wait_async(struct urtwn_softc *sc)
 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
 
 	/* Wait for all queued asynchronous commands to complete. */
+	mutex_spin_enter(&sc->sc_task_mtx);
 	while (sc->cmdq.queued > 0)
-		tsleep(&sc->cmdq, 0, "endtask", 0);
+		cv_wait(&sc->sc_task_cv, &sc->sc_task_mtx);
+	mutex_spin_exit(&sc->sc_task_mtx);
 }
 
 static int

Index: src/sys/dev/usb/if_urtwnvar.h
diff -u src/sys/dev/usb/if_urtwnvar.h:1.14 src/sys/dev/usb/if_urtwnvar.h:1.15
--- src/sys/dev/usb/if_urtwnvar.h:1.14	Sat Oct  5 23:27:20 2019
+++ src/sys/dev/usb/if_urtwnvar.h	Wed Jan 15 08:20:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_urtwnvar.h,v 1.14 2019/10/05 23:27:20 mrg Exp $	*/
+/*	$NetBSD: if_urtwnvar.h,v 1.15 2020/01/15 08:20:13 skrll Exp $	*/
 /*	$OpenBSD: if_urtwnreg.h,v 1.3 2010/11/16 18:02:59 damien Exp $	*/
 
 /*-
@@ -128,6 +128,7 @@ struct urtwn_softc {
 	callout_t			sc_scan_to;
 	callout_t			sc_calib_to;
 
+	kcondvar_t			sc_task_cv;
 	kmutex_t			sc_task_mtx;
 	kmutex_t			sc_fwcmd_mtx;
 	kmutex_t			sc_tx_mtx;

Index: src/sys/dev/usb/if_zyd.c
diff -u src/sys/dev/usb/if_zyd.c:1.53 src/sys/dev/usb/if_zyd.c:1.54
--- src/sys/dev/usb/if_zyd.c:1.53	Thu Nov 28 17:09:10 2019
+++ src/sys/dev/usb/if_zyd.c	Wed Jan 15 08:20:13 2020
@@ -1,5 +1,5 @@
 /*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
-/*	$NetBSD: if_zyd.c,v 1.53 2019/11/28 17:09:10 maxv Exp $	*/
+/*	$NetBSD: if_zyd.c,v 1.54 2020/01/15 08:20:13 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2006 by Damien Bergamini <damien.bergam...@free.fr>
@@ -23,7 +23,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.53 2019/11/28 17:09:10 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.54 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -335,6 +335,8 @@ zyd_attach(device_t parent, device_t sel
 	IFQ_SET_READY(&ifp->if_snd);
 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
 
+	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
+	cv_init(&sc->sc_cmdcv, "zydcmd");
 	SIMPLEQ_INIT(&sc->sc_rqh);
 
 	/* defer configrations after file system is ready to load firmware */
@@ -458,12 +460,11 @@ zyd_detach(device_t self, int flags)
 	struct zyd_softc *sc = device_private(self);
 	struct ieee80211com *ic = &sc->sc_ic;
 	struct ifnet *ifp = &sc->sc_if;
-	int s;
 
 	if (!sc->attached)
 		return 0;
 
-	s = splusb();
+	mutex_enter(&sc->sc_lock);
 
 	zyd_stop(ifp, 1);
 	callout_halt(&sc->sc_scan_ch, NULL);
@@ -479,7 +480,10 @@ zyd_detach(device_t self, int flags)
 	ieee80211_ifdetach(ic);
 	if_detach(ifp);
 
-	splx(s);
+	mutex_exit(&sc->sc_lock);
+
+	mutex_destroy(&sc->sc_lock);
+	cv_destroy(&sc->sc_cmdcv);
 
 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
 
@@ -787,7 +791,6 @@ zyd_cmd(struct zyd_softc *sc, uint16_t c
 	uint16_t xferflags;
 	int error;
 	usbd_status uerror;
-	int s = 0;
 
 	error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_IOUT],
 	    sizeof(uint16_t) + ilen, USBD_FORCE_SHORT_XFER, 0, &xfer);
@@ -801,19 +804,18 @@ zyd_cmd(struct zyd_softc *sc, uint16_t c
 	if (!(flags & ZYD_CMD_FLAG_READ))
 		xferflags |= USBD_SYNCHRONOUS;
 	else {
-		s = splusb();
 		rq.idata = idata;
 		rq.odata = odata;
 		rq.len = olen / sizeof(struct zyd_pair);
+		mutex_enter(&sc->sc_lock);
 		SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
+		mutex_exit(&sc->sc_lock);
 	}
 
 	usbd_setup_xfer(xfer, 0, &cmd, sizeof(uint16_t) + ilen, xferflags,
 	    ZYD_INTR_TIMEOUT, NULL);
 	uerror = usbd_transfer(xfer);
 	if (uerror != USBD_IN_PROGRESS && uerror != 0) {
-		if (flags & ZYD_CMD_FLAG_READ)
-			splx(s);
 		printf("%s: could not send command (error=%s)\n",
 		    device_xname(sc->sc_dev), usbd_errstr(uerror));
 		(void)usbd_destroy_xfer(xfer);
@@ -824,11 +826,12 @@ zyd_cmd(struct zyd_softc *sc, uint16_t c
 		return 0;	/* write: don't wait for reply */
 	}
 	/* wait at most one second for command reply */
-	error = tsleep(odata, PCATCH, "zydcmd", hz);
+	mutex_enter(&sc->sc_lock);
+	error = cv_timedwait_sig(&sc->sc_cmdcv, &sc->sc_lock, hz);
 	if (error == EWOULDBLOCK)
 		printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev));
 	SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
-	splx(s);
+	mutex_exit(&sc->sc_lock);
 
 	(void)usbd_destroy_xfer(xfer);
 	return error;
@@ -1867,6 +1870,7 @@ zyd_intr(struct usbd_xfer *xfer, void * 
 		datalen -= sizeof(cmd->code);
 		datalen -= 2;	/* XXX: padding? */
 
+		mutex_enter(&sc->sc_lock);
 		SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
 			int i;
 
@@ -1883,10 +1887,11 @@ zyd_intr(struct usbd_xfer *xfer, void * 
 			/* copy answer into caller-supplied buffer */
 			memcpy(rqp->odata, cmd->data,
 			    sizeof(struct zyd_pair) * rqp->len);
-			wakeup(rqp->odata);	/* wakeup caller */
-
+			cv_signal(&sc->sc_cmdcv);
+			mutex_exit(&sc->sc_lock);
 			return;
 		}
+		mutex_exit(&sc->sc_lock);
 		return;	/* unexpected IORD notification */
 	} else {
 		printf("%s: unknown notification %x\n", device_xname(sc->sc_dev),

Reply via email to