Module Name:    src
Committed By:   riastradh
Date:           Sat Jun 12 15:39:46 UTC 2021

Modified Files:
        src/sys/dev/usb: usb_subr.c usbdi.c

Log Message:
usb(4): Fix fix for interface change pipe fix.

If there is an interface:
- Always put the pipe on the list in usbd_setup_pipe (if successful).
- Always have the pipe on the list from _before_ upm_open.
- Always keep the pipe on the list to _after_ upm_close, and after
  the async task has completed.

This brings the logic in usbd_close_pipe and usbd_kill_pipe closer.


To generate a diff of this commit:
cvs rdiff -u -r1.256 -r1.257 src/sys/dev/usb/usb_subr.c
cvs rdiff -u -r1.210 -r1.211 src/sys/dev/usb/usbdi.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/usb_subr.c
diff -u src/sys/dev/usb/usb_subr.c:1.256 src/sys/dev/usb/usb_subr.c:1.257
--- src/sys/dev/usb/usb_subr.c:1.256	Sat Jun 12 14:43:27 2021
+++ src/sys/dev/usb/usb_subr.c	Sat Jun 12 15:39:46 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: usb_subr.c,v 1.256 2021/06/12 14:43:27 riastradh Exp $	*/
+/*	$NetBSD: usb_subr.c,v 1.257 2021/06/12 15:39:46 riastradh Exp $	*/
 /*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.256 2021/06/12 14:43:27 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.257 2021/06/12 15:39:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -840,10 +840,22 @@ usbd_setup_pipe_flags(struct usbd_device
 	p->up_interval = ival;
 	p->up_flags = flags;
 	SIMPLEQ_INIT(&p->up_queue);
+
+	if (iface) {
+		mutex_enter(&iface->ui_pipelock);
+		LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
+		mutex_exit(&iface->ui_pipelock);
+	}
+
 	err = dev->ud_bus->ub_methods->ubm_open(p);
 	if (err) {
 		DPRINTF("endpoint=%#jx failed, error=%jd",
 		    (uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0);
+		if (iface) {
+			mutex_enter(&iface->ui_pipelock);
+			LIST_REMOVE(p, up_next);
+			mutex_exit(&iface->ui_pipelock);
+		}
 		kmem_free(p, dev->ud_bus->ub_pipesize);
 		usbd_endpoint_release(dev, ep);
 		return err;
@@ -898,6 +910,7 @@ usbd_kill_pipe(struct usbd_pipe *pipe)
 	usbd_unlock_pipe(pipe);
 	usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
 	    NULL);
+	KASSERT(pipe->up_iface == NULL);
 	usbd_endpoint_release(pipe->up_dev, pipe->up_endpoint);
 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
 }

Index: src/sys/dev/usb/usbdi.c
diff -u src/sys/dev/usb/usbdi.c:1.210 src/sys/dev/usb/usbdi.c:1.211
--- src/sys/dev/usb/usbdi.c:1.210	Sat Jun 12 15:09:18 2021
+++ src/sys/dev/usb/usbdi.c	Sat Jun 12 15:39:46 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: usbdi.c,v 1.210 2021/06/12 15:09:18 riastradh Exp $	*/
+/*	$NetBSD: usbdi.c,v 1.211 2021/06/12 15:39:46 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.210 2021/06/12 15:09:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.211 2021/06/12 15:39:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -244,9 +244,6 @@ usbd_open_pipe_ival(struct usbd_interfac
 	err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
 	if (err)
 		return err;
-	mutex_enter(&iface->ui_pipelock);
-	LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
-	mutex_exit(&iface->ui_pipelock);
 	*pipe = p;
 	SDT_PROBE5(usb, device, pipe, open,
 	    iface, address, flags, ival, p);
@@ -320,11 +317,11 @@ usbd_close_pipe(struct usbd_pipe *pipe)
 	usbd_unlock_pipe(pipe);
 	if (pipe->up_intrxfer != NULL)
 		usbd_destroy_xfer(pipe->up_intrxfer);
+	usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
+	    NULL);
 	mutex_enter(&pipe->up_iface->ui_pipelock);
 	LIST_REMOVE(pipe, up_next);
 	mutex_exit(&pipe->up_iface->ui_pipelock);
-	usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
-	    NULL);
 	usbd_endpoint_release(pipe->up_dev, pipe->up_endpoint);
 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
 

Reply via email to