Thu Mar 3 06:09:44 2022 UTC ()
usb: Inline usb_insert_transfer.

This makes it clearer which part happens irrespective of error
(putting it on the queue -- unconditional, not rolled back by
usb_insert_transfer) and what the possible `errors' mean (neither of
which is an error, per se).


(riastradh)
diff -r1.228 -r1.229 src/sys/dev/usb/usbdi.c

cvs diff -r1.228 -r1.229 src/sys/dev/usb/usbdi.c (expand / switch to unified diff)

--- src/sys/dev/usb/usbdi.c 2022/03/03 06:09:33 1.228
+++ src/sys/dev/usb/usbdi.c 2022/03/03 06:09:44 1.229
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: usbdi.c,v 1.228 2022/03/03 06:09:33 riastradh Exp $ */ 1/* $NetBSD: usbdi.c,v 1.229 2022/03/03 06:09:44 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at 8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology, Matthew R. Green (mrg@eterna.com.au), 9 * Carlstedt Research & Technology, Matthew R. Green (mrg@eterna.com.au),
10 * and Nick Hudson. 10 * and Nick Hudson.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE. 31 * POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.228 2022/03/03 06:09:33 riastradh Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.229 2022/03/03 06:09:44 riastradh Exp $");
36 36
37#ifdef _KERNEL_OPT 37#ifdef _KERNEL_OPT
38#include "opt_usb.h" 38#include "opt_usb.h"
39#include "opt_compat_netbsd.h" 39#include "opt_compat_netbsd.h"
40#include "usb_dma.h" 40#include "usb_dma.h"
41#endif 41#endif
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/systm.h> 44#include <sys/systm.h>
45#include <sys/kernel.h> 45#include <sys/kernel.h>
46#include <sys/device.h> 46#include <sys/device.h>
47#include <sys/kmem.h> 47#include <sys/kmem.h>
48#include <sys/proc.h> 48#include <sys/proc.h>
@@ -104,27 +104,26 @@ SDT_PROBE_DEFINE5(usb, device, xfer, cre @@ -104,27 +104,26 @@ SDT_PROBE_DEFINE5(usb, device, xfer, cre
104 "size_t"/*len*/, 104 "size_t"/*len*/,
105 "unsigned int"/*flags*/, 105 "unsigned int"/*flags*/,
106 "unsigned int"/*nframes*/); 106 "unsigned int"/*nframes*/);
107SDT_PROBE_DEFINE1(usb, device, xfer, start, "struct usbd_xfer *"/*xfer*/); 107SDT_PROBE_DEFINE1(usb, device, xfer, start, "struct usbd_xfer *"/*xfer*/);
108SDT_PROBE_DEFINE1(usb, device, xfer, preabort, "struct usbd_xfer *"/*xfer*/); 108SDT_PROBE_DEFINE1(usb, device, xfer, preabort, "struct usbd_xfer *"/*xfer*/);
109SDT_PROBE_DEFINE1(usb, device, xfer, abort, "struct usbd_xfer *"/*xfer*/); 109SDT_PROBE_DEFINE1(usb, device, xfer, abort, "struct usbd_xfer *"/*xfer*/);
110SDT_PROBE_DEFINE1(usb, device, xfer, timeout, "struct usbd_xfer *"/*xfer*/); 110SDT_PROBE_DEFINE1(usb, device, xfer, timeout, "struct usbd_xfer *"/*xfer*/);
111SDT_PROBE_DEFINE2(usb, device, xfer, done, 111SDT_PROBE_DEFINE2(usb, device, xfer, done,
112 "struct usbd_xfer *"/*xfer*/, 112 "struct usbd_xfer *"/*xfer*/,
113 "usbd_status"/*status*/); 113 "usbd_status"/*status*/);
114SDT_PROBE_DEFINE1(usb, device, xfer, destroy, "struct usbd_xfer *"/*xfer*/); 114SDT_PROBE_DEFINE1(usb, device, xfer, destroy, "struct usbd_xfer *"/*xfer*/);
115 115
116Static void usbd_ar_pipe(struct usbd_pipe *); 116Static void usbd_ar_pipe(struct usbd_pipe *);
117static usbd_status usb_insert_transfer(struct usbd_xfer *); 
118Static void usbd_start_next(struct usbd_pipe *); 117Static void usbd_start_next(struct usbd_pipe *);
119Static usbd_status usbd_open_pipe_ival 118Static usbd_status usbd_open_pipe_ival
120 (struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int); 119 (struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
121static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t); 120static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
122static void usbd_free_buffer(struct usbd_xfer *); 121static void usbd_free_buffer(struct usbd_xfer *);
123static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int); 122static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
124static void usbd_free_xfer(struct usbd_xfer *); 123static void usbd_free_xfer(struct usbd_xfer *);
125static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status); 124static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
126static void usbd_xfer_timeout(void *); 125static void usbd_xfer_timeout(void *);
127static void usbd_xfer_timeout_task(void *); 126static void usbd_xfer_timeout_task(void *);
128static bool usbd_xfer_probe_timeout(struct usbd_xfer *); 127static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
129static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *); 128static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
130 129
@@ -397,27 +396,36 @@ usbd_transfer(struct usbd_xfer *xfer) @@ -397,27 +396,36 @@ usbd_transfer(struct usbd_xfer *xfer)
397 */ 396 */
398 if (xfer->ux_buffer != xfer->ux_buf) { 397 if (xfer->ux_buffer != xfer->ux_buf) {
399 KASSERT(xfer->ux_buf); 398 KASSERT(xfer->ux_buf);
400 if (!usbd_xfer_isread(xfer)) { 399 if (!usbd_xfer_isread(xfer)) {
401 memcpy(xfer->ux_buf, xfer->ux_buffer, size); 400 memcpy(xfer->ux_buf, xfer->ux_buffer, size);
402 } 401 }
403 } 402 }
404 } 403 }
405 404
406 /* xfer is not valid after the transfer method unless synchronous */ 405 /* xfer is not valid after the transfer method unless synchronous */
407 SDT_PROBE2(usb, device, pipe, transfer__start, pipe, xfer); 406 SDT_PROBE2(usb, device, pipe, transfer__start, pipe, xfer);
408 do { 407 do {
409 usbd_lock_pipe(pipe); 408 usbd_lock_pipe(pipe);
410 err = usb_insert_transfer(xfer); 409#ifdef DIAGNOSTIC
 410 xfer->ux_state = XFER_ONQU;
 411#endif
 412 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
 413 if (pipe->up_running && pipe->up_serialise) {
 414 err = USBD_IN_PROGRESS;
 415 } else {
 416 pipe->up_running = 1;
 417 err = USBD_NORMAL_COMPLETION;
 418 }
411 usbd_unlock_pipe(pipe); 419 usbd_unlock_pipe(pipe);
412 if (err) 420 if (err)
413 break; 421 break;
414 err = pipe->up_methods->upm_transfer(xfer); 422 err = pipe->up_methods->upm_transfer(xfer);
415 } while (0); 423 } while (0);
416 SDT_PROBE3(usb, device, pipe, transfer__done, pipe, xfer, err); 424 SDT_PROBE3(usb, device, pipe, transfer__done, pipe, xfer, err);
417 425
418 if (err != USBD_IN_PROGRESS && err) { 426 if (err != USBD_IN_PROGRESS && err) {
419 /* 427 /*
420 * The transfer made it onto the pipe queue, but didn't get 428 * The transfer made it onto the pipe queue, but didn't get
421 * accepted by the HCD for some reason. It needs removing 429 * accepted by the HCD for some reason. It needs removing
422 * from the pipe queue. 430 * from the pipe queue.
423 */ 431 */
@@ -1143,57 +1151,26 @@ usb_transfer_complete(struct usbd_xfer * @@ -1143,57 +1151,26 @@ usb_transfer_complete(struct usbd_xfer *
1143 if (repeat) { 1151 if (repeat) {
1144 xfer->ux_actlen = 0; 1152 xfer->ux_actlen = 0;
1145 xfer->ux_status = USBD_NOT_STARTED; 1153 xfer->ux_status = USBD_NOT_STARTED;
1146 } else { 1154 } else {
1147 /* XXX should we stop the queue on all errors? */ 1155 /* XXX should we stop the queue on all errors? */
1148 if (erred && pipe->up_iface != NULL) /* not control pipe */ 1156 if (erred && pipe->up_iface != NULL) /* not control pipe */
1149 pipe->up_running = 0; 1157 pipe->up_running = 0;
1150 } 1158 }
1151 if (pipe->up_running && pipe->up_serialise) 1159 if (pipe->up_running && pipe->up_serialise)
1152 usbd_start_next(pipe); 1160 usbd_start_next(pipe);
1153} 1161}
1154 1162
1155/* Called with USB lock held. */ 1163/* Called with USB lock held. */
1156static usbd_status 
1157usb_insert_transfer(struct usbd_xfer *xfer) 
1158{ 
1159 struct usbd_pipe *pipe = xfer->ux_pipe; 
1160 usbd_status err; 
1161 
1162 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, 
1163 "xfer = %#jx pipe = %#jx running = %jd timeout = %jd", 
1164 (uintptr_t)xfer, (uintptr_t)pipe, 
1165 pipe->up_running, xfer->ux_timeout); 
1166 
1167 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock)); 
1168 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer, 
1169 xfer->ux_state); 
1170 
1171#ifdef DIAGNOSTIC 
1172 xfer->ux_state = XFER_ONQU; 
1173#endif 
1174 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next); 
1175 if (pipe->up_running && pipe->up_serialise) 
1176 err = USBD_IN_PROGRESS; 
1177 else { 
1178 pipe->up_running = 1; 
1179 err = USBD_NORMAL_COMPLETION; 
1180 } 
1181 USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer, 
1182 err, 0, 0); 
1183 return err; 
1184} 
1185 
1186/* Called with USB lock held. */ 
1187void 1164void
1188usbd_start_next(struct usbd_pipe *pipe) 1165usbd_start_next(struct usbd_pipe *pipe)
1189{ 1166{
1190 struct usbd_xfer *xfer; 1167 struct usbd_xfer *xfer;
1191 usbd_status err; 1168 usbd_status err;
1192 1169
1193 USBHIST_FUNC(); 1170 USBHIST_FUNC();
1194 1171
1195 KASSERT(pipe != NULL); 1172 KASSERT(pipe != NULL);
1196 KASSERT(pipe->up_methods != NULL); 1173 KASSERT(pipe->up_methods != NULL);
1197 KASSERT(pipe->up_methods->upm_start != NULL); 1174 KASSERT(pipe->up_methods->upm_start != NULL);
1198 KASSERT(pipe->up_serialise == true); 1175 KASSERT(pipe->up_serialise == true);
1199 1176