diff --git a/sys/dev/usb2/core/usb2_core.h b/sys/dev/usb2/core/usb2_core.h index 33b1a2707853..06f77cb77d1d 100644 --- a/sys/dev/usb2/core/usb2_core.h +++ b/sys/dev/usb2/core/usb2_core.h @@ -449,6 +449,7 @@ void usb2_start_hardware(struct usb2_xfer *xfer); void usb2_transfer_clear_stall(struct usb2_xfer *xfer); void usb2_transfer_drain(struct usb2_xfer *xfer); void usb2_transfer_set_stall(struct usb2_xfer *xfer); +uint8_t usb2_transfer_pending(struct usb2_xfer *xfer); void usb2_transfer_start(struct usb2_xfer *xfer); void usb2_transfer_stop(struct usb2_xfer *xfer); void usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup); diff --git a/sys/dev/usb2/core/usb2_process.c b/sys/dev/usb2/core/usb2_process.c index 890d53e2367a..4c5069591298 100644 --- a/sys/dev/usb2/core/usb2_process.c +++ b/sys/dev/usb2/core/usb2_process.c @@ -84,9 +84,9 @@ usb2_process(void *arg) while (1) { - if (up->up_gone) { + if (up->up_gone) break; - } + /* * NOTE to reimplementors: dequeueing a command from the * "used" queue and executing it must be atomic, with regard @@ -213,10 +213,10 @@ usb2_proc_create(struct usb2_process *up, struct mtx *p_mtx, void usb2_proc_free(struct usb2_process *up) { - if (!(up->up_mtx)) { - /* not initialised */ + /* check if not initialised */ + if (up->up_mtx == NULL) return; - } + usb2_proc_drain(up); usb2_cv_destroy(&up->up_cv); @@ -246,6 +246,10 @@ usb2_proc_msignal(struct usb2_process *up, void *_pm0, void *_pm1) uint32_t d; uint8_t t; + /* check if gone, return dummy value */ + if (up->up_gone) + return (_pm0); + mtx_assert(up->up_mtx, MA_OWNED); t = 0; @@ -319,9 +323,11 @@ usb2_proc_msignal(struct usb2_process *up, void *_pm0, void *_pm1) uint8_t usb2_proc_is_gone(struct usb2_process *up) { - mtx_assert(up->up_mtx, MA_OWNED); + if (up->up_gone) + return (1); - return (up->up_gone ? 1 : 0); + mtx_assert(up->up_mtx, MA_OWNED); + return (0); } /*------------------------------------------------------------------------* @@ -337,6 +343,10 @@ usb2_proc_mwait(struct usb2_process *up, void *_pm0, void *_pm1) struct usb2_proc_msg *pm0 = _pm0; struct usb2_proc_msg *pm1 = _pm1; + /* check if gone */ + if (up->up_gone) + return; + mtx_assert(up->up_mtx, MA_OWNED); if (up->up_curtd == curthread) { @@ -372,13 +382,13 @@ usb2_proc_mwait(struct usb2_process *up, void *_pm0, void *_pm1) void usb2_proc_drain(struct usb2_process *up) { - if (!(up->up_mtx)) { - /* not initialised */ + /* check if not initialised */ + if (up->up_mtx == NULL) return; - } - if (up->up_mtx != &Giant) { + /* handle special case with Giant */ + if (up->up_mtx != &Giant) mtx_assert(up->up_mtx, MA_NOTOWNED); - } + mtx_lock(up->up_mtx); /* Set the gone flag */ @@ -398,7 +408,8 @@ usb2_proc_drain(struct usb2_process *up) if (cold) { USB_THREAD_SUSPEND(up->up_ptr); - printf("WARNING: A USB process has been left suspended!\n"); + printf("WARNING: A USB process has " + "been left suspended!\n"); break; } usb2_cv_wait(&up->up_cv, up->up_mtx); @@ -413,64 +424,3 @@ usb2_proc_drain(struct usb2_process *up) } mtx_unlock(up->up_mtx); } - -/*------------------------------------------------------------------------* - * usb2_proc_cwait - * - * This function will suspend the current process until - * "usb2_proc_signal()" or "usb2_proc_drain()" is called. The - * "timeout" parameter defines the maximum wait time in system - * ticks. If "timeout" is zero that means no timeout. - * - * NOTE: This function can only be called from within an USB process. - * - * Return values: - * USB_PROC_WAIT_TIMEOUT: Timeout - * USB_PROC_WAIT_NORMAL: Success - * Else: USB process is tearing down - *------------------------------------------------------------------------*/ -uint8_t -usb2_proc_cwait(struct usb2_process *up, int timeout) -{ - int error; - - mtx_assert(up->up_mtx, MA_OWNED); - - if (up->up_gone) { - return (USB_PROC_WAIT_DRAIN); - } - up->up_csleep = 1; - - if (timeout == 0) { - usb2_cv_wait(&up->up_cv, up->up_mtx); - error = 0; - } else { - error = usb2_cv_timedwait(&up->up_cv, up->up_mtx, timeout); - } - - up->up_csleep = 0; - - if (up->up_gone) { - return (USB_PROC_WAIT_DRAIN); - } - if (error == EWOULDBLOCK) { - return (USB_PROC_WAIT_TIMEOUT); - } - return (0); -} - -/*------------------------------------------------------------------------* - * usb2_proc_csignal - * - * This function will wakeup the given USB process. - *------------------------------------------------------------------------*/ -void -usb2_proc_csignal(struct usb2_process *up) -{ - mtx_assert(up->up_mtx, MA_OWNED); - - if (up->up_csleep) { - up->up_csleep = 0; - usb2_cv_signal(&up->up_cv); - } -} diff --git a/sys/dev/usb2/core/usb2_process.h b/sys/dev/usb2/core/usb2_process.h index 78fdb3aa5646..756b92913adb 100644 --- a/sys/dev/usb2/core/usb2_process.h +++ b/sys/dev/usb2/core/usb2_process.h @@ -77,11 +77,9 @@ struct usb2_process { /* prototypes */ -uint8_t usb2_proc_cwait(struct usb2_process *up, int timeout); uint8_t usb2_proc_is_gone(struct usb2_process *up); int usb2_proc_create(struct usb2_process *up, struct mtx *p_mtx, const char *pmesg, uint8_t prio); -void usb2_proc_csignal(struct usb2_process *up); void usb2_proc_drain(struct usb2_process *up); void usb2_proc_mwait(struct usb2_process *up, void *pm0, void *pm1); void usb2_proc_free(struct usb2_process *up); diff --git a/sys/dev/usb2/core/usb2_transfer.c b/sys/dev/usb2/core/usb2_transfer.c index ef512d37a91c..2572b25dc3d1 100644 --- a/sys/dev/usb2/core/usb2_transfer.c +++ b/sys/dev/usb2/core/usb2_transfer.c @@ -1690,6 +1690,10 @@ usb2_transfer_pending(struct usb2_xfer *xfer) struct usb2_xfer_root *info; struct usb2_xfer_queue *pq; + if (xfer == NULL) { + /* transfer is gone */ + return (0); + } USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); if (xfer->flags_int.transferring) { diff --git a/sys/dev/usb2/core/usb2_transfer.h b/sys/dev/usb2/core/usb2_transfer.h index 41494993c503..34124c5b5d18 100644 --- a/sys/dev/usb2/core/usb2_transfer.h +++ b/sys/dev/usb2/core/usb2_transfer.h @@ -102,7 +102,6 @@ struct usb2_setup_params { /* function prototypes */ -uint8_t usb2_transfer_pending(struct usb2_xfer *xfer); uint8_t usb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm, struct usb2_page_cache **ppc, uint32_t size, uint32_t align, uint32_t count);