Annotation of src/sys/dev/usb/usbdi.c, Revision 1.225
1.225 ! riastrad 1: /* $NetBSD: usbdi.c,v 1.224 2022/03/03 06:05:38 riastradh Exp $ */
1.1 augustss 2:
3: /*
1.169 skrll 4: * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
1.1 augustss 5: * All rights reserved.
6: *
1.11 augustss 7: * This code is derived from software contributed to The NetBSD Foundation
1.72 augustss 8: * by Lennart Augustsson (lennart@augustsson.net) at
1.169 skrll 9: * Carlstedt Research & Technology, Matthew R. Green (mrg@eterna.com.au),
10: * and Nick Hudson.
1.1 augustss 11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
15: * 1. Redistributions of source code must retain the above copyright
16: * notice, this list of conditions and the following disclaimer.
17: * 2. Redistributions in binary form must reproduce the above copyright
18: * notice, this list of conditions and the following disclaimer in the
19: * documentation and/or other materials provided with the distribution.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
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
24: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
31: * POSSIBILITY OF SUCH DAMAGE.
32: */
1.84 lukem 33:
34: #include <sys/cdefs.h>
1.225 ! riastrad 35: __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.224 2022/03/03 06:05:38 riastradh Exp $");
1.115 pavel 36:
1.141 christos 37: #ifdef _KERNEL_OPT
1.162 skrll 38: #include "opt_usb.h"
1.115 pavel 39: #include "opt_compat_netbsd.h"
1.169 skrll 40: #include "usb_dma.h"
1.141 christos 41: #endif
1.1 augustss 42:
43: #include <sys/param.h>
44: #include <sys/systm.h>
45: #include <sys/kernel.h>
1.15 augustss 46: #include <sys/device.h>
1.169 skrll 47: #include <sys/kmem.h>
1.1 augustss 48: #include <sys/proc.h>
1.121 ad 49: #include <sys/bus.h>
1.136 mrg 50: #include <sys/cpu.h>
1.36 augustss 51:
1.1 augustss 52: #include <dev/usb/usb.h>
53: #include <dev/usb/usbdi.h>
54: #include <dev/usb/usbdi_util.h>
55: #include <dev/usb/usbdivar.h>
1.36 augustss 56: #include <dev/usb/usb_mem.h>
1.105 augustss 57: #include <dev/usb/usb_quirks.h>
1.195 riastrad 58: #include <dev/usb/usb_sdt.h>
1.162 skrll 59: #include <dev/usb/usbhist.h>
1.1 augustss 60:
1.119 drochner 61: /* UTF-8 encoding stuff */
62: #include <fs/unicode.h>
63:
1.195 riastrad 64: SDT_PROBE_DEFINE5(usb, device, pipe, open,
65: "struct usbd_interface *"/*iface*/,
66: "uint8_t"/*address*/,
67: "uint8_t"/*flags*/,
68: "int"/*ival*/,
69: "struct usbd_pipe *"/*pipe*/);
70:
71: SDT_PROBE_DEFINE7(usb, device, pipe, open__intr,
72: "struct usbd_interface *"/*iface*/,
73: "uint8_t"/*address*/,
74: "uint8_t"/*flags*/,
75: "int"/*ival*/,
76: "usbd_callback"/*cb*/,
77: "void *"/*cookie*/,
78: "struct usbd_pipe *"/*pipe*/);
79:
80: SDT_PROBE_DEFINE2(usb, device, pipe, transfer__start,
81: "struct usbd_pipe *"/*pipe*/,
82: "struct usbd_xfer *"/*xfer*/);
83: SDT_PROBE_DEFINE3(usb, device, pipe, transfer__done,
84: "struct usbd_pipe *"/*pipe*/,
85: "struct usbd_xfer *"/*xfer*/,
86: "usbd_status"/*err*/);
87: SDT_PROBE_DEFINE2(usb, device, pipe, start,
88: "struct usbd_pipe *"/*pipe*/,
89: "struct usbd_xfer *"/*xfer*/);
90:
91: SDT_PROBE_DEFINE1(usb, device, pipe, close, "struct usbd_pipe *"/*pipe*/);
92: SDT_PROBE_DEFINE1(usb, device, pipe, abort__start,
93: "struct usbd_pipe *"/*pipe*/);
94: SDT_PROBE_DEFINE1(usb, device, pipe, abort__done,
95: "struct usbd_pipe *"/*pipe*/);
96: SDT_PROBE_DEFINE1(usb, device, pipe, clear__endpoint__stall,
97: "struct usbd_pipe *"/*pipe*/);
98: SDT_PROBE_DEFINE1(usb, device, pipe, clear__endpoint__toggle,
99: "struct usbd_pipe *"/*pipe*/);
100:
101: SDT_PROBE_DEFINE5(usb, device, xfer, create,
102: "struct usbd_xfer *"/*xfer*/,
103: "struct usbd_pipe *"/*pipe*/,
104: "size_t"/*len*/,
105: "unsigned int"/*flags*/,
106: "unsigned int"/*nframes*/);
107: SDT_PROBE_DEFINE1(usb, device, xfer, start, "struct usbd_xfer *"/*xfer*/);
108: SDT_PROBE_DEFINE1(usb, device, xfer, preabort, "struct usbd_xfer *"/*xfer*/);
109: SDT_PROBE_DEFINE1(usb, device, xfer, abort, "struct usbd_xfer *"/*xfer*/);
110: SDT_PROBE_DEFINE1(usb, device, xfer, timeout, "struct usbd_xfer *"/*xfer*/);
111: SDT_PROBE_DEFINE2(usb, device, xfer, done,
112: "struct usbd_xfer *"/*xfer*/,
113: "usbd_status"/*status*/);
114: SDT_PROBE_DEFINE1(usb, device, xfer, destroy, "struct usbd_xfer *"/*xfer*/);
115:
1.224 riastrad 116: Static void usbd_ar_pipe(struct usbd_pipe *);
1.223 riastrad 117: static usbd_status usb_insert_transfer(struct usbd_xfer *);
1.169 skrll 118: Static void usbd_start_next(struct usbd_pipe *);
1.69 augustss 119: Static usbd_status usbd_open_pipe_ival
1.169 skrll 120: (struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
121: static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
122: static void usbd_free_buffer(struct usbd_xfer *);
123: static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
124: static usbd_status usbd_free_xfer(struct usbd_xfer *);
1.176 khorben 125: static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
1.192 riastrad 126: static void usbd_xfer_timeout(void *);
127: static void usbd_xfer_timeout_task(void *);
128: static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
129: static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
1.1 augustss 130:
1.162 skrll 131: #if defined(USB_DEBUG)
1.82 augustss 132: void
133: usbd_dump_iface(struct usbd_interface *iface)
134: {
1.183 mrg 135: USBHIST_FUNC();
136: USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
1.162 skrll 137:
1.82 augustss 138: if (iface == NULL)
139: return;
1.196 christos 140: USBHIST_LOG(usbdebug, " device = %#jx idesc = %#jx index = %jd",
1.175 pgoyette 141: (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
142: iface->ui_index, 0);
1.215 riastrad 143: USBHIST_LOG(usbdebug, " altindex=%jd",
144: iface->ui_altindex, 0, 0, 0);
1.82 augustss 145: }
146:
147: void
148: usbd_dump_device(struct usbd_device *dev)
149: {
1.183 mrg 150: USBHIST_FUNC();
151: USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
1.162 skrll 152:
1.82 augustss 153: if (dev == NULL)
154: return;
1.175 pgoyette 155: USBHIST_LOG(usbdebug, " bus = %#jx default_pipe = %#jx",
156: (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
157: USBHIST_LOG(usbdebug, " address = %jd config = %jd depth = %jd ",
1.169 skrll 158: dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
1.175 pgoyette 159: USBHIST_LOG(usbdebug, " speed = %jd self_powered = %jd "
160: "power = %jd langid = %jd",
1.169 skrll 161: dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
1.82 augustss 162: }
163:
164: void
165: usbd_dump_endpoint(struct usbd_endpoint *endp)
166: {
1.183 mrg 167: USBHIST_FUNC();
168: USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
1.162 skrll 169:
1.82 augustss 170: if (endp == NULL)
171: return;
1.175 pgoyette 172: USBHIST_LOG(usbdebug, " edesc = %#jx refcnt = %jd",
173: (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
1.169 skrll 174: if (endp->ue_edesc)
1.198 christos 175: USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02jx",
1.169 skrll 176: endp->ue_edesc->bEndpointAddress, 0, 0, 0);
1.82 augustss 177: }
178:
1.28 augustss 179: void
1.169 skrll 180: usbd_dump_queue(struct usbd_pipe *pipe)
1.28 augustss 181: {
1.169 skrll 182: struct usbd_xfer *xfer;
1.28 augustss 183:
1.183 mrg 184: USBHIST_FUNC();
185: USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
1.162 skrll 186:
1.169 skrll 187: SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
1.175 pgoyette 188: USBHIST_LOG(usbdebug, " xfer = %#jx", (uintptr_t)xfer,
189: 0, 0, 0);
1.28 augustss 190: }
1.82 augustss 191: }
192:
193: void
1.169 skrll 194: usbd_dump_pipe(struct usbd_pipe *pipe)
1.82 augustss 195: {
1.183 mrg 196: USBHIST_FUNC();
197: USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
1.162 skrll 198:
1.82 augustss 199: if (pipe == NULL)
200: return;
1.169 skrll 201: usbd_dump_iface(pipe->up_iface);
202: usbd_dump_device(pipe->up_dev);
203: usbd_dump_endpoint(pipe->up_endpoint);
1.162 skrll 204: USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
1.175 pgoyette 205: USBHIST_LOG(usbdebug, " running = %jd aborting = %jd",
1.169 skrll 206: pipe->up_running, pipe->up_aborting, 0, 0);
1.175 pgoyette 207: USBHIST_LOG(usbdebug, " intrxfer = %#jx, repeat = %jd, "
208: "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
209: pipe->up_interval, 0);
1.28 augustss 210: }
211: #endif
212:
1.102 augustss 213: usbd_status
1.169 skrll 214: usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
215: uint8_t flags, struct usbd_pipe **pipe)
1.102 augustss 216: {
217: return (usbd_open_pipe_ival(iface, address, flags, pipe,
1.60 augustss 218: USBD_DEFAULT_INTERVAL));
219: }
220:
1.102 augustss 221: usbd_status
1.169 skrll 222: usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
223: uint8_t flags, struct usbd_pipe **pipe, int ival)
1.102 augustss 224: {
1.218 riastrad 225: struct usbd_pipe *p = NULL;
1.217 mlelstv 226: struct usbd_endpoint *ep = NULL /* XXXGCC */;
1.216 riastrad 227: bool piperef = false;
1.48 augustss 228: usbd_status err;
1.12 augustss 229: int i;
1.1 augustss 230:
1.183 mrg 231: USBHIST_FUNC();
1.196 christos 232: USBHIST_CALLARGS(usbdebug, "iface = %#jx address = %#jx flags = %#jx",
1.175 pgoyette 233: (uintptr_t)iface, address, flags, 0);
1.53 augustss 234:
1.216 riastrad 235: /*
236: * Block usbd_set_interface so we have a snapshot of the
237: * interface endpoints. They will remain stable until we drop
238: * the reference in usbd_close_pipe (or on failure here).
239: */
240: err = usbd_iface_piperef(iface);
241: if (err)
242: goto out;
243: piperef = true;
244:
245: /* Find the endpoint at this address. */
1.169 skrll 246: for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
247: ep = &iface->ui_endpoints[i];
1.216 riastrad 248: if (ep->ue_edesc == NULL) {
249: err = USBD_IOERROR;
250: goto out;
251: }
1.169 skrll 252: if (ep->ue_edesc->bEndpointAddress == address)
1.216 riastrad 253: break;
1.1 augustss 254: }
1.216 riastrad 255: if (i == iface->ui_idesc->bNumEndpoints) {
256: err = USBD_BAD_ADDRESS;
257: goto out;
258: }
259:
260: /* Set up the pipe with this endpoint. */
1.210 riastrad 261: err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
262: if (err)
1.216 riastrad 263: goto out;
264:
265: /* Success! */
1.1 augustss 266: *pipe = p;
1.216 riastrad 267: p = NULL; /* handed off to caller */
268: piperef = false; /* handed off to pipe */
1.195 riastrad 269: SDT_PROBE5(usb, device, pipe, open,
270: iface, address, flags, ival, p);
1.216 riastrad 271: err = USBD_NORMAL_COMPLETION;
272:
273: out: if (p)
274: usbd_close_pipe(p);
275: if (piperef)
276: usbd_iface_pipeunref(iface);
277: return err;
1.1 augustss 278: }
279:
1.102 augustss 280: usbd_status
1.169 skrll 281: usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
282: uint8_t flags, struct usbd_pipe **pipe,
283: void *priv, void *buffer, uint32_t len,
1.74 augustss 284: usbd_callback cb, int ival)
1.1 augustss 285: {
1.48 augustss 286: usbd_status err;
1.169 skrll 287: struct usbd_xfer *xfer;
288: struct usbd_pipe *ipipe;
1.1 augustss 289:
1.183 mrg 290: USBHIST_FUNC();
1.196 christos 291: USBHIST_CALLARGS(usbdebug, "address = %#jx flags = %#jx len = %jd",
1.162 skrll 292: address, flags, len, 0);
1.53 augustss 293:
1.150 jmcneill 294: err = usbd_open_pipe_ival(iface, address,
295: USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
1.60 augustss 296: &ipipe, ival);
1.48 augustss 297: if (err)
1.169 skrll 298: return err;
299: err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
300: if (err)
1.1 augustss 301: goto bad1;
1.169 skrll 302:
303: usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
304: ipipe->up_intrxfer = xfer;
305: ipipe->up_repeat = 1;
1.48 augustss 306: err = usbd_transfer(xfer);
1.1 augustss 307: *pipe = ipipe;
1.48 augustss 308: if (err != USBD_IN_PROGRESS)
1.169 skrll 309: goto bad3;
1.195 riastrad 310: SDT_PROBE7(usb, device, pipe, open__intr,
311: iface, address, flags, ival, cb, priv, ipipe);
1.169 skrll 312: return USBD_NORMAL_COMPLETION;
313:
314: bad3:
315: ipipe->up_intrxfer = NULL;
316: ipipe->up_repeat = 0;
1.1 augustss 317:
1.169 skrll 318: usbd_destroy_xfer(xfer);
1.36 augustss 319: bad1:
1.1 augustss 320: usbd_close_pipe(ipipe);
1.169 skrll 321: return err;
1.9 augustss 322: }
323:
1.225 ! riastrad 324: void
1.169 skrll 325: usbd_close_pipe(struct usbd_pipe *pipe)
1.1 augustss 326: {
1.162 skrll 327: USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
328:
1.169 skrll 329: KASSERT(pipe != NULL);
1.26 augustss 330:
1.138 mrg 331: usbd_lock_pipe(pipe);
1.195 riastrad 332: SDT_PROBE1(usb, device, pipe, close, pipe);
1.169 skrll 333: if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
334: printf("WARNING: pipe closed with active xfers on addr %d\n",
335: pipe->up_dev->ud_addr);
336: usbd_ar_pipe(pipe);
1.138 mrg 337: }
1.169 skrll 338: KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
1.181 skrll 339: pipe->up_methods->upm_close(pipe);
1.213 riastrad 340: usbd_unlock_pipe(pipe);
1.181 skrll 341:
1.219 riastrad 342: cv_destroy(&pipe->up_callingcv);
1.213 riastrad 343: if (pipe->up_intrxfer)
1.169 skrll 344: usbd_destroy_xfer(pipe->up_intrxfer);
1.211 riastrad 345: usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
346: NULL);
1.207 riastrad 347: usbd_endpoint_release(pipe->up_dev, pipe->up_endpoint);
1.216 riastrad 348: if (pipe->up_iface)
349: usbd_iface_pipeunref(pipe->up_iface);
1.169 skrll 350: kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
1.1 augustss 351: }
352:
1.31 augustss 353: usbd_status
1.169 skrll 354: usbd_transfer(struct usbd_xfer *xfer)
1.1 augustss 355: {
1.169 skrll 356: struct usbd_pipe *pipe = xfer->ux_pipe;
1.48 augustss 357: usbd_status err;
1.120 kiyohara 358: unsigned int size, flags;
1.16 augustss 359:
1.183 mrg 360: USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
1.175 pgoyette 361: "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
362: (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
1.177 mrg 363: KASSERT(xfer->ux_status == USBD_NOT_STARTED);
1.195 riastrad 364: SDT_PROBE1(usb, device, xfer, start, xfer);
1.134 jmcneill 365:
1.28 augustss 366: #ifdef USB_DEBUG
367: if (usbdebug > 5)
368: usbd_dump_queue(pipe);
369: #endif
1.169 skrll 370: xfer->ux_done = 0;
1.31 augustss 371:
1.169 skrll 372: if (pipe->up_aborting) {
1.175 pgoyette 373: USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
374: (uintptr_t)xfer, 0, 0, 0);
1.195 riastrad 375: SDT_PROBE2(usb, device, xfer, done, xfer, USBD_CANCELLED);
1.169 skrll 376: return USBD_CANCELLED;
1.162 skrll 377: }
1.71 augustss 378:
1.169 skrll 379: KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
380:
381: size = xfer->ux_length;
382: flags = xfer->ux_flags;
1.37 augustss 383:
1.169 skrll 384: if (size != 0) {
385: /*
386: * Use the xfer buffer if none specified in transfer setup.
387: * isoc transfers always use the xfer buffer, i.e.
388: * ux_buffer is always NULL for isoc.
389: */
390: if (xfer->ux_buffer == NULL) {
391: xfer->ux_buffer = xfer->ux_buf;
392: }
393:
394: /*
395: * If not using the xfer buffer copy data to the
396: * xfer buffer for OUT transfers of >0 length
397: */
398: if (xfer->ux_buffer != xfer->ux_buf) {
399: KASSERT(xfer->ux_buf);
400: if (!usbd_xfer_isread(xfer)) {
401: memcpy(xfer->ux_buf, xfer->ux_buffer, size);
402: }
1.162 skrll 403: }
1.38 augustss 404: }
1.37 augustss 405:
1.120 kiyohara 406: /* xfer is not valid after the transfer method unless synchronous */
1.195 riastrad 407: SDT_PROBE2(usb, device, pipe, transfer__start, pipe, xfer);
1.223 riastrad 408: do {
409: usbd_lock_pipe(pipe);
410: err = usb_insert_transfer(xfer);
411: usbd_unlock_pipe(pipe);
412: if (err)
413: break;
414: err = pipe->up_methods->upm_transfer(xfer);
415: } while (0);
1.195 riastrad 416: SDT_PROBE3(usb, device, pipe, transfer__done, pipe, xfer, err);
1.37 augustss 417:
1.51 augustss 418: if (err != USBD_IN_PROGRESS && err) {
1.168 skrll 419: /*
420: * The transfer made it onto the pipe queue, but didn't get
421: * accepted by the HCD for some reason. It needs removing
422: * from the pipe queue.
423: */
1.196 christos 424: USBHIST_LOG(usbdebug, "xfer failed: %jd, reinserting",
1.186 mrg 425: err, 0, 0, 0);
1.168 skrll 426: usbd_lock_pipe(pipe);
1.195 riastrad 427: SDT_PROBE1(usb, device, xfer, preabort, xfer);
1.203 mrg 428: #ifdef DIAGNOSTIC
429: xfer->ux_state = XFER_BUSY;
430: #endif
1.169 skrll 431: SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
432: if (pipe->up_serialise)
433: usbd_start_next(pipe);
1.168 skrll 434: usbd_unlock_pipe(pipe);
1.37 augustss 435: }
436:
1.162 skrll 437: if (!(flags & USBD_SYNCHRONOUS)) {
1.175 pgoyette 438: USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
439: (uintptr_t)xfer, err, 0, 0);
1.195 riastrad 440: if (err != USBD_IN_PROGRESS) /* XXX Possible? */
441: SDT_PROBE2(usb, device, xfer, done, xfer, err);
1.169 skrll 442: return err;
1.162 skrll 443: }
1.31 augustss 444:
1.162 skrll 445: if (err != USBD_IN_PROGRESS) {
1.186 mrg 446: USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
447: (uintptr_t)xfer, err, 0, 0);
1.195 riastrad 448: SDT_PROBE2(usb, device, xfer, done, xfer, err);
1.169 skrll 449: return err;
1.162 skrll 450: }
1.167 skrll 451:
452: /* Sync transfer, wait for completion. */
1.138 mrg 453: usbd_lock_pipe(pipe);
1.169 skrll 454: while (!xfer->ux_done) {
455: if (pipe->up_dev->ud_bus->ub_usepolling)
1.103 provos 456: panic("usbd_transfer: not done");
1.175 pgoyette 457: USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
458: (uintptr_t)xfer, 0, 0, 0);
1.138 mrg 459:
1.144 christos 460: err = 0;
1.138 mrg 461: if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
1.169 skrll 462: err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
1.138 mrg 463: } else {
1.169 skrll 464: cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
1.138 mrg 465: }
1.154 aymeric 466: if (err) {
1.195 riastrad 467: if (!xfer->ux_done) {
468: SDT_PROBE1(usb, device, xfer, abort, xfer);
1.169 skrll 469: pipe->up_methods->upm_abort(xfer);
1.195 riastrad 470: }
1.144 christos 471: break;
1.154 aymeric 472: }
1.31 augustss 473: }
1.195 riastrad 474: SDT_PROBE2(usb, device, xfer, done, xfer, xfer->ux_status);
475: /* XXX Race to read xfer->ux_status? */
1.138 mrg 476: usbd_unlock_pipe(pipe);
1.169 skrll 477: return xfer->ux_status;
1.31 augustss 478: }
479:
480: /* Like usbd_transfer(), but waits for completion. */
481: usbd_status
1.169 skrll 482: usbd_sync_transfer(struct usbd_xfer *xfer)
1.31 augustss 483: {
1.169 skrll 484: xfer->ux_flags |= USBD_SYNCHRONOUS;
485: return usbd_transfer(xfer);
1.16 augustss 486: }
487:
1.138 mrg 488: /* Like usbd_transfer(), but waits for completion and listens for signals. */
489: usbd_status
1.169 skrll 490: usbd_sync_transfer_sig(struct usbd_xfer *xfer)
1.138 mrg 491: {
1.169 skrll 492: xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
493: return usbd_transfer(xfer);
1.138 mrg 494: }
495:
1.169 skrll 496: static void *
497: usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
1.36 augustss 498: {
1.169 skrll 499: KASSERT(xfer->ux_buf == NULL);
500: KASSERT(size != 0);
501:
502: xfer->ux_bufsize = 0;
503: #if NUSB_DMA > 0
504: struct usbd_bus *bus = xfer->ux_bus;
505:
506: if (bus->ub_usedma) {
507: usb_dma_t *dmap = &xfer->ux_dmabuf;
508:
1.200 skrll 509: KASSERT((bus->ub_dmaflags & USBMALLOC_COHERENT) == 0);
1.220 skrll 510: int err = usb_allocmem(bus->ub_dmatag, size, 0, bus->ub_dmaflags, dmap);
1.169 skrll 511: if (err) {
512: return NULL;
513: }
514: xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
515: xfer->ux_bufsize = size;
1.37 augustss 516:
1.169 skrll 517: return xfer->ux_buf;
518: }
1.92 augustss 519: #endif
1.169 skrll 520: KASSERT(xfer->ux_bus->ub_usedma == false);
521: xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
1.173 chs 522: xfer->ux_bufsize = size;
1.169 skrll 523: return xfer->ux_buf;
1.36 augustss 524: }
525:
1.169 skrll 526: static void
527: usbd_free_buffer(struct usbd_xfer *xfer)
1.36 augustss 528: {
1.169 skrll 529: KASSERT(xfer->ux_buf != NULL);
530: KASSERT(xfer->ux_bufsize != 0);
531:
532: void *buf = xfer->ux_buf;
533: uint32_t size = xfer->ux_bufsize;
534:
535: xfer->ux_buf = NULL;
536: xfer->ux_bufsize = 0;
537:
538: #if NUSB_DMA > 0
539: struct usbd_bus *bus = xfer->ux_bus;
540:
541: if (bus->ub_usedma) {
542: usb_dma_t *dmap = &xfer->ux_dmabuf;
543:
1.220 skrll 544: usb_freemem(dmap);
1.37 augustss 545: return;
546: }
547: #endif
1.169 skrll 548: KASSERT(xfer->ux_bus->ub_usedma == false);
549:
550: kmem_free(buf, size);
1.36 augustss 551: }
552:
1.38 augustss 553: void *
1.169 skrll 554: usbd_get_buffer(struct usbd_xfer *xfer)
1.38 augustss 555: {
1.169 skrll 556: return xfer->ux_buf;
1.38 augustss 557: }
558:
1.169 skrll 559: struct usbd_pipe *
560: usbd_get_pipe0(struct usbd_device *dev)
1.1 augustss 561: {
1.169 skrll 562:
563: return dev->ud_pipe0;
564: }
565:
566: static struct usbd_xfer *
567: usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
568: {
569: struct usbd_xfer *xfer;
1.1 augustss 570:
1.183 mrg 571: USBHIST_FUNC();
1.162 skrll 572:
1.169 skrll 573: ASSERT_SLEEPABLE();
574:
575: xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
1.48 augustss 576: if (xfer == NULL)
1.183 mrg 577: goto out;
1.169 skrll 578: xfer->ux_bus = dev->ud_bus;
579: callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
1.192 riastrad 580: callout_setfunc(&xfer->ux_callout, usbd_xfer_timeout, xfer);
1.169 skrll 581: cv_init(&xfer->ux_cv, "usbxfer");
1.192 riastrad 582: usb_init_task(&xfer->ux_aborttask, usbd_xfer_timeout_task, xfer,
583: USB_TASKQ_MPSAFE);
1.162 skrll 584:
1.183 mrg 585: out:
586: USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
1.162 skrll 587:
1.169 skrll 588: return xfer;
1.1 augustss 589: }
590:
1.169 skrll 591: static usbd_status
592: usbd_free_xfer(struct usbd_xfer *xfer)
1.1 augustss 593: {
1.183 mrg 594: USBHIST_FUNC();
595: USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
1.162 skrll 596:
1.169 skrll 597: if (xfer->ux_buf) {
1.48 augustss 598: usbd_free_buffer(xfer);
1.169 skrll 599: }
1.190 riastrad 600:
601: /* Wait for any straggling timeout to complete. */
602: mutex_enter(xfer->ux_bus->ub_lock);
1.191 riastrad 603: xfer->ux_timeout_reset = false; /* do not resuscitate */
1.190 riastrad 604: callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
605: usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
606: USB_TASKQ_HC, xfer->ux_bus->ub_lock);
607: mutex_exit(xfer->ux_bus->ub_lock);
608:
1.169 skrll 609: cv_destroy(&xfer->ux_cv);
610: xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
611: return USBD_NORMAL_COMPLETION;
612: }
613:
614: int
615: usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
616: unsigned int nframes, struct usbd_xfer **xp)
617: {
618: KASSERT(xp != NULL);
1.171 pooka 619: void *buf = NULL;
1.169 skrll 620:
621: struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
622: if (xfer == NULL)
623: return ENOMEM;
624:
1.199 skrll 625: xfer->ux_pipe = pipe;
626: xfer->ux_flags = flags;
627: xfer->ux_nframes = nframes;
628: xfer->ux_methods = pipe->up_methods;
629:
1.169 skrll 630: if (len) {
631: buf = usbd_alloc_buffer(xfer, len);
632: if (!buf) {
633: usbd_free_xfer(xfer);
634: return ENOMEM;
635: }
636: }
637:
638: if (xfer->ux_methods->upm_init) {
639: int err = xfer->ux_methods->upm_init(xfer);
640: if (err) {
641: usbd_free_xfer(xfer);
642: return err;
643: }
644: }
645:
646: *xp = xfer;
1.195 riastrad 647: SDT_PROBE5(usb, device, xfer, create,
648: xfer, pipe, len, flags, nframes);
1.169 skrll 649: return 0;
1.1 augustss 650: }
651:
1.36 augustss 652: void
1.169 skrll 653: usbd_destroy_xfer(struct usbd_xfer *xfer)
654: {
655:
1.195 riastrad 656: SDT_PROBE1(usb, device, xfer, destroy, xfer);
657: if (xfer->ux_methods->upm_fini)
1.169 skrll 658: xfer->ux_methods->upm_fini(xfer);
659:
660: usbd_free_xfer(xfer);
1.1 augustss 661: }
662:
1.36 augustss 663: void
1.169 skrll 664: usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
665: uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
666: {
667: KASSERT(xfer->ux_pipe);
668:
669: xfer->ux_priv = priv;
670: xfer->ux_buffer = buffer;
671: xfer->ux_length = length;
672: xfer->ux_actlen = 0;
673: xfer->ux_flags = flags;
674: xfer->ux_timeout = timeout;
675: xfer->ux_status = USBD_NOT_STARTED;
676: xfer->ux_callback = callback;
677: xfer->ux_rqflags &= ~URQ_REQUEST;
678: xfer->ux_nframes = 0;
1.36 augustss 679: }
680:
681: void
1.169 skrll 682: usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
683: void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
684: uint32_t length, uint16_t flags, usbd_callback callback)
685: {
686: KASSERT(xfer->ux_pipe == dev->ud_pipe0);
687:
688: xfer->ux_priv = priv;
689: xfer->ux_buffer = buffer;
690: xfer->ux_length = length;
691: xfer->ux_actlen = 0;
692: xfer->ux_flags = flags;
693: xfer->ux_timeout = timeout;
694: xfer->ux_status = USBD_NOT_STARTED;
695: xfer->ux_callback = callback;
696: xfer->ux_request = *req;
697: xfer->ux_rqflags |= URQ_REQUEST;
698: xfer->ux_nframes = 0;
699: }
700:
701: void
702: usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
703: uint32_t nframes, uint16_t flags, usbd_callback callback)
704: {
705: xfer->ux_priv = priv;
706: xfer->ux_buffer = NULL;
707: xfer->ux_length = 0;
708: xfer->ux_actlen = 0;
709: xfer->ux_flags = flags;
710: xfer->ux_timeout = USBD_NO_TIMEOUT;
711: xfer->ux_status = USBD_NOT_STARTED;
712: xfer->ux_callback = callback;
713: xfer->ux_rqflags &= ~URQ_REQUEST;
714: xfer->ux_frlengths = frlengths;
715: xfer->ux_nframes = nframes;
1.201 skrll 716:
717: for (size_t i = 0; i < xfer->ux_nframes; i++)
718: xfer->ux_length += xfer->ux_frlengths[i];
1.1 augustss 719: }
720:
1.31 augustss 721: void
1.169 skrll 722: usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
723: void **buffer, uint32_t *count, usbd_status *status)
1.1 augustss 724: {
1.48 augustss 725: if (priv != NULL)
1.169 skrll 726: *priv = xfer->ux_priv;
1.48 augustss 727: if (buffer != NULL)
1.169 skrll 728: *buffer = xfer->ux_buffer;
1.48 augustss 729: if (count != NULL)
1.169 skrll 730: *count = xfer->ux_actlen;
1.48 augustss 731: if (status != NULL)
1.169 skrll 732: *status = xfer->ux_status;
1.1 augustss 733: }
734:
735: usb_config_descriptor_t *
1.169 skrll 736: usbd_get_config_descriptor(struct usbd_device *dev)
1.1 augustss 737: {
1.169 skrll 738: KASSERT(dev != NULL);
739:
740: return dev->ud_cdesc;
1.1 augustss 741: }
742:
743: usb_interface_descriptor_t *
1.169 skrll 744: usbd_get_interface_descriptor(struct usbd_interface *iface)
1.1 augustss 745: {
1.169 skrll 746: KASSERT(iface != NULL);
747:
748: return iface->ui_idesc;
1.1 augustss 749: }
750:
751: usb_device_descriptor_t *
1.169 skrll 752: usbd_get_device_descriptor(struct usbd_device *dev)
1.1 augustss 753: {
1.169 skrll 754: KASSERT(dev != NULL);
755:
756: return &dev->ud_ddesc;
1.1 augustss 757: }
758:
759: usb_endpoint_descriptor_t *
1.169 skrll 760: usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
1.1 augustss 761: {
1.169 skrll 762:
763: if (index >= iface->ui_idesc->bNumEndpoints)
764: return NULL;
765: return iface->ui_endpoints[index].ue_edesc;
1.1 augustss 766: }
767:
1.125 jmorse 768: /* Some drivers may wish to abort requests on the default pipe, *
769: * but there is no mechanism for getting a handle on it. */
1.224 riastrad 770: void
1.125 jmorse 771: usbd_abort_default_pipe(struct usbd_device *device)
772: {
1.224 riastrad 773: usbd_abort_pipe(device->ud_pipe0);
1.125 jmorse 774: }
775:
1.224 riastrad 776: void
1.169 skrll 777: usbd_abort_pipe(struct usbd_pipe *pipe)
1.1 augustss 778: {
779:
1.169 skrll 780: KASSERT(pipe != NULL);
781:
1.138 mrg 782: usbd_lock_pipe(pipe);
1.224 riastrad 783: usbd_ar_pipe(pipe);
1.138 mrg 784: usbd_unlock_pipe(pipe);
1.1 augustss 785: }
1.102 augustss 786:
787: usbd_status
1.169 skrll 788: usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
1.1 augustss 789: {
1.169 skrll 790: struct usbd_device *dev = pipe->up_dev;
1.48 augustss 791: usbd_status err;
1.1 augustss 792:
1.162 skrll 793: USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1.195 riastrad 794: SDT_PROBE1(usb, device, pipe, clear__endpoint__stall, pipe);
1.33 augustss 795:
1.102 augustss 796: /*
1.87 augustss 797: * Clearing en endpoint stall resets the endpoint toggle, so
1.33 augustss 798: * do the same to the HC toggle.
799: */
1.195 riastrad 800: SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
1.169 skrll 801: pipe->up_methods->upm_cleartoggle(pipe);
1.33 augustss 802:
1.188 maxv 803: err = usbd_clear_endpoint_feature(dev,
804: pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
1.1 augustss 805: #if 0
806: XXX should we do this?
1.51 augustss 807: if (!err) {
1.1 augustss 808: pipe->state = USBD_PIPE_ACTIVE;
809: /* XXX activate pipe */
810: }
811: #endif
1.169 skrll 812: return err;
1.1 augustss 813: }
814:
1.139 mrg 815: void
1.156 skrll 816: usbd_clear_endpoint_stall_task(void *arg)
1.7 augustss 817: {
1.169 skrll 818: struct usbd_pipe *pipe = arg;
819: struct usbd_device *dev = pipe->up_dev;
1.7 augustss 820:
1.195 riastrad 821: SDT_PROBE1(usb, device, pipe, clear__endpoint__stall, pipe);
822: SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
1.169 skrll 823: pipe->up_methods->upm_cleartoggle(pipe);
1.33 augustss 824:
1.188 maxv 825: (void)usbd_clear_endpoint_feature(dev,
826: pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
1.139 mrg 827: }
828:
829: void
1.169 skrll 830: usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
1.139 mrg 831: {
1.169 skrll 832: usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
1.7 augustss 833: }
834:
1.61 augustss 835: void
1.169 skrll 836: usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
1.61 augustss 837: {
1.169 skrll 838:
1.195 riastrad 839: SDT_PROBE1(usb, device, pipe, clear__endpoint__toggle, pipe);
1.169 skrll 840: pipe->up_methods->upm_cleartoggle(pipe);
1.61 augustss 841: }
842:
1.102 augustss 843: usbd_status
1.169 skrll 844: usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
1.1 augustss 845: {
1.169 skrll 846: KASSERT(iface != NULL);
847: KASSERT(iface->ui_idesc != NULL);
848:
849: *count = iface->ui_idesc->bNumEndpoints;
850: return USBD_NORMAL_COMPLETION;
1.1 augustss 851: }
852:
1.102 augustss 853: usbd_status
1.169 skrll 854: usbd_interface_count(struct usbd_device *dev, uint8_t *count)
1.1 augustss 855: {
1.169 skrll 856:
857: if (dev->ud_cdesc == NULL)
858: return USBD_NOT_CONFIGURED;
859: *count = dev->ud_cdesc->bNumInterface;
860: return USBD_NORMAL_COMPLETION;
1.1 augustss 861: }
862:
1.85 augustss 863: void
1.169 skrll 864: usbd_interface2device_handle(struct usbd_interface *iface,
865: struct usbd_device **dev)
1.1 augustss 866: {
1.169 skrll 867:
868: *dev = iface->ui_dev;
1.1 augustss 869: }
870:
1.102 augustss 871: usbd_status
1.169 skrll 872: usbd_device2interface_handle(struct usbd_device *dev,
873: uint8_t ifaceno, struct usbd_interface **iface)
1.1 augustss 874: {
1.169 skrll 875:
876: if (dev->ud_cdesc == NULL)
877: return USBD_NOT_CONFIGURED;
878: if (ifaceno >= dev->ud_cdesc->bNumInterface)
879: return USBD_INVAL;
880: *iface = &dev->ud_ifaces[ifaceno];
881: return USBD_NORMAL_COMPLETION;
1.1 augustss 882: }
883:
1.169 skrll 884: struct usbd_device *
885: usbd_pipe2device_handle(struct usbd_pipe *pipe)
1.36 augustss 886: {
1.169 skrll 887: KASSERT(pipe != NULL);
888:
889: return pipe->up_dev;
1.36 augustss 890: }
891:
1.13 augustss 892: /* XXXX use altno */
1.3 augustss 893: usbd_status
1.169 skrll 894: usbd_set_interface(struct usbd_interface *iface, int altidx)
1.3 augustss 895: {
1.216 riastrad 896: bool locked = false;
1.3 augustss 897: usb_device_request_t req;
1.48 augustss 898: usbd_status err;
1.13 augustss 899:
1.183 mrg 900: USBHIST_FUNC();
1.214 riastrad 901: USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
1.169 skrll 902:
1.216 riastrad 903: err = usbd_iface_lock(iface);
904: if (err)
1.208 riastrad 905: goto out;
1.216 riastrad 906: locked = true;
1.13 augustss 907:
1.169 skrll 908: err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
1.48 augustss 909: if (err)
1.208 riastrad 910: goto out;
1.73 augustss 911:
1.3 augustss 912: req.bmRequestType = UT_WRITE_INTERFACE;
913: req.bRequest = UR_SET_INTERFACE;
1.169 skrll 914: USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
915: USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
1.3 augustss 916: USETW(req.wLength, 0);
1.208 riastrad 917: err = usbd_do_request(iface->ui_dev, &req, 0);
918:
1.216 riastrad 919: out: /* XXX back out iface data? */
920: if (locked)
921: usbd_iface_unlock(iface);
1.208 riastrad 922: return err;
1.12 augustss 923: }
924:
925: int
1.74 augustss 926: usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
1.12 augustss 927: {
1.13 augustss 928: char *p = (char *)cdesc;
929: char *end = p + UGETW(cdesc->wTotalLength);
1.12 augustss 930: usb_interface_descriptor_t *d;
931: int n;
932:
933: for (n = 0; p < end; p += d->bLength) {
934: d = (usb_interface_descriptor_t *)p;
1.102 augustss 935: if (p + d->bLength <= end &&
1.12 augustss 936: d->bDescriptorType == UDESC_INTERFACE &&
1.13 augustss 937: d->bInterfaceNumber == ifaceno)
1.12 augustss 938: n++;
939: }
1.169 skrll 940: return n;
1.13 augustss 941: }
942:
943: int
1.169 skrll 944: usbd_get_interface_altindex(struct usbd_interface *iface)
1.13 augustss 945: {
1.169 skrll 946: return iface->ui_altindex;
1.12 augustss 947: }
948:
949: usbd_status
1.169 skrll 950: usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
1.12 augustss 951: {
952: usb_device_request_t req;
953:
954: req.bmRequestType = UT_READ_INTERFACE;
955: req.bRequest = UR_GET_INTERFACE;
956: USETW(req.wValue, 0);
1.169 skrll 957: USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
1.12 augustss 958: USETW(req.wLength, 1);
1.169 skrll 959: return usbd_do_request(iface->ui_dev, &req, aiface);
1.3 augustss 960: }
1.1 augustss 961:
962: /*** Internal routines ***/
963:
1.174 skrll 964: /* Dequeue all pipe operations, called with bus lock held. */
1.224 riastrad 965: Static void
1.169 skrll 966: usbd_ar_pipe(struct usbd_pipe *pipe)
1.1 augustss 967: {
1.169 skrll 968: struct usbd_xfer *xfer;
1.1 augustss 969:
1.183 mrg 970: USBHIST_FUNC();
971: USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
1.195 riastrad 972: SDT_PROBE1(usb, device, pipe, abort__start, pipe);
1.162 skrll 973:
1.169 skrll 974: KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1.138 mrg 975:
1.28 augustss 976: #ifdef USB_DEBUG
977: if (usbdebug > 5)
978: usbd_dump_queue(pipe);
979: #endif
1.169 skrll 980: pipe->up_repeat = 0;
1.194 riastrad 981: pipe->up_running = 0;
1.169 skrll 982: pipe->up_aborting = 1;
983: while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
1.175 pgoyette 984: USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
985: "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
986: (uintptr_t)pipe->up_methods, 0);
1.182 rin 987: if (xfer->ux_status == USBD_NOT_STARTED) {
1.195 riastrad 988: SDT_PROBE1(usb, device, xfer, preabort, xfer);
1.203 mrg 989: #ifdef DIAGNOSTIC
990: xfer->ux_state = XFER_BUSY;
991: #endif
1.182 rin 992: SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
993: } else {
994: /* Make the HC abort it (and invoke the callback). */
1.195 riastrad 995: SDT_PROBE1(usb, device, xfer, abort, xfer);
1.182 rin 996: pipe->up_methods->upm_abort(xfer);
1.219 riastrad 997: while (pipe->up_callingxfer == xfer) {
998: USBHIST_LOG(usbdebug, "wait for callback"
999: "pipe = %#jx xfer = %#jx",
1000: (uintptr_t)pipe, (uintptr_t)xfer, 0, 0);
1001: cv_wait(&pipe->up_callingcv,
1002: pipe->up_dev->ud_bus->ub_lock);
1003: }
1.182 rin 1004: /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
1005: }
1.20 augustss 1006: }
1.169 skrll 1007: pipe->up_aborting = 0;
1.195 riastrad 1008: SDT_PROBE1(usb, device, pipe, abort__done, pipe);
1.1 augustss 1009: }
1010:
1.138 mrg 1011: /* Called with USB lock held. */
1.31 augustss 1012: void
1.169 skrll 1013: usb_transfer_complete(struct usbd_xfer *xfer)
1.1 augustss 1014: {
1.169 skrll 1015: struct usbd_pipe *pipe = xfer->ux_pipe;
1.172 skrll 1016: struct usbd_bus *bus = pipe->up_dev->ud_bus;
1.169 skrll 1017: int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
1.179 manu 1018: int erred;
1.169 skrll 1019: int polling = bus->ub_usepolling;
1020: int repeat = pipe->up_repeat;
1.31 augustss 1021:
1.183 mrg 1022: USBHIST_FUNC();
1023: USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
1.175 pgoyette 1024: "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
1025: xfer->ux_actlen);
1.134 jmcneill 1026:
1.169 skrll 1027: KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1.177 mrg 1028: KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
1029: xfer->ux_state);
1.169 skrll 1030: KASSERT(pipe != NULL);
1.5 augustss 1031:
1.179 manu 1032: /*
1033: * If device is known to miss out ack, then pretend that
1034: * output timeout is a success. Userland should handle
1035: * the logic to verify that the operation succeeded.
1036: */
1037: if (pipe->up_dev->ud_quirks &&
1038: pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
1039: xfer->ux_status == USBD_TIMEOUT &&
1040: !usbd_xfer_isread(xfer)) {
1041: USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
1.197 christos 1042: "hiding write timeout to %jd.%jd for %ju bytes written",
1.180 manu 1043: (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
1.179 manu 1044: xfer->ux_length);
1045:
1046: xfer->ux_status = USBD_NORMAL_COMPLETION;
1047: xfer->ux_actlen = xfer->ux_length;
1048: }
1049:
1050: erred = xfer->ux_status == USBD_CANCELLED ||
1051: xfer->ux_status == USBD_TIMEOUT;
1052:
1.47 augustss 1053: if (!repeat) {
1054: /* Remove request from queue. */
1.153 jakllsch 1055:
1.169 skrll 1056: KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
1.145 skrll 1057: "pipe %p is empty, but xfer %p wants to complete", pipe,
1058: xfer);
1.169 skrll 1059: KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
1060: "xfer %p is not start of queue (%p is at start)", xfer,
1061: SIMPLEQ_FIRST(&pipe->up_queue));
1062:
1.47 augustss 1063: #ifdef DIAGNOSTIC
1.169 skrll 1064: xfer->ux_state = XFER_BUSY;
1.45 augustss 1065: #endif
1.169 skrll 1066: SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
1.47 augustss 1067: }
1.175 pgoyette 1068: USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
1069: (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
1070: 0);
1.31 augustss 1071:
1072: /* Count completed transfers. */
1.169 skrll 1073: ++pipe->up_dev->ud_bus->ub_stats.uds_requests
1074: [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
1.1 augustss 1075:
1.169 skrll 1076: xfer->ux_done = 1;
1077: if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
1078: !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
1.175 pgoyette 1079: USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
1.169 skrll 1080: xfer->ux_actlen, xfer->ux_length, 0, 0);
1081: xfer->ux_status = USBD_SHORT_XFER;
1.1 augustss 1082: }
1.31 augustss 1083:
1.175 pgoyette 1084: USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
1085: (uintptr_t)pipe->up_methods->upm_done, 0, 0);
1.195 riastrad 1086: SDT_PROBE2(usb, device, xfer, done, xfer, xfer->ux_status);
1.169 skrll 1087: pipe->up_methods->upm_done(xfer);
1088:
1089: if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
1090: KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
1091: "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
1.148 jmcneill 1092:
1.169 skrll 1093: /* Only if IN transfer */
1094: if (usbd_xfer_isread(xfer)) {
1095: memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
1.138 mrg 1096: }
1.169 skrll 1097: }
1098:
1.175 pgoyette 1099: USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
1100: (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
1.169 skrll 1101:
1102: if (xfer->ux_callback) {
1.178 mrg 1103: if (!polling) {
1.219 riastrad 1104: KASSERT(pipe->up_callingxfer == NULL);
1105: pipe->up_callingxfer = xfer;
1.169 skrll 1106: mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1.178 mrg 1107: if (!(pipe->up_flags & USBD_MPSAFE))
1108: KERNEL_LOCK(1, curlwp);
1109: }
1.169 skrll 1110:
1111: xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
1112:
1.178 mrg 1113: if (!polling) {
1114: if (!(pipe->up_flags & USBD_MPSAFE))
1115: KERNEL_UNLOCK_ONE(curlwp);
1.169 skrll 1116: mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1.219 riastrad 1117: KASSERT(pipe->up_callingxfer == xfer);
1118: pipe->up_callingxfer = NULL;
1119: cv_broadcast(&pipe->up_callingcv);
1.178 mrg 1120: }
1.124 jmcneill 1121: }
1.31 augustss 1122:
1.138 mrg 1123: if (sync && !polling) {
1.175 pgoyette 1124: USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
1125: (uintptr_t)xfer, 0, 0, 0);
1.169 skrll 1126: cv_broadcast(&xfer->ux_cv);
1.138 mrg 1127: }
1.31 augustss 1128:
1.169 skrll 1129: if (repeat) {
1130: xfer->ux_actlen = 0;
1131: xfer->ux_status = USBD_NOT_STARTED;
1132: } else {
1.40 augustss 1133: /* XXX should we stop the queue on all errors? */
1.169 skrll 1134: if (erred && pipe->up_iface != NULL) /* not control pipe */
1135: pipe->up_running = 0;
1.40 augustss 1136: }
1.169 skrll 1137: if (pipe->up_running && pipe->up_serialise)
1138: usbd_start_next(pipe);
1.1 augustss 1139: }
1140:
1.138 mrg 1141: /* Called with USB lock held. */
1.223 riastrad 1142: static usbd_status
1.169 skrll 1143: usb_insert_transfer(struct usbd_xfer *xfer)
1.1 augustss 1144: {
1.169 skrll 1145: struct usbd_pipe *pipe = xfer->ux_pipe;
1.48 augustss 1146: usbd_status err;
1.31 augustss 1147:
1.183 mrg 1148: USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
1149: "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
1150: (uintptr_t)xfer, (uintptr_t)pipe,
1.175 pgoyette 1151: pipe->up_running, xfer->ux_timeout);
1.134 jmcneill 1152:
1.169 skrll 1153: KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1.174 skrll 1154: KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1155: xfer->ux_state);
1.134 jmcneill 1156:
1.93 augustss 1157: #ifdef DIAGNOSTIC
1.169 skrll 1158: xfer->ux_state = XFER_ONQU;
1.93 augustss 1159: #endif
1.169 skrll 1160: SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1161: if (pipe->up_running && pipe->up_serialise)
1.48 augustss 1162: err = USBD_IN_PROGRESS;
1.37 augustss 1163: else {
1.169 skrll 1164: pipe->up_running = 1;
1.48 augustss 1165: err = USBD_NORMAL_COMPLETION;
1.37 augustss 1166: }
1.175 pgoyette 1167: USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1168: err, 0, 0);
1.169 skrll 1169: return err;
1.1 augustss 1170: }
1171:
1.138 mrg 1172: /* Called with USB lock held. */
1.31 augustss 1173: void
1.169 skrll 1174: usbd_start_next(struct usbd_pipe *pipe)
1.31 augustss 1175: {
1.169 skrll 1176: struct usbd_xfer *xfer;
1.48 augustss 1177: usbd_status err;
1.40 augustss 1178:
1.183 mrg 1179: USBHIST_FUNC();
1.162 skrll 1180:
1.169 skrll 1181: KASSERT(pipe != NULL);
1182: KASSERT(pipe->up_methods != NULL);
1183: KASSERT(pipe->up_methods->upm_start != NULL);
1184: KASSERT(pipe->up_serialise == true);
1.31 augustss 1185:
1.169 skrll 1186: int polling = pipe->up_dev->ud_bus->ub_usepolling;
1187: KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1.138 mrg 1188:
1.31 augustss 1189: /* Get next request in queue. */
1.169 skrll 1190: xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1.183 mrg 1191: USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1.175 pgoyette 1192: (uintptr_t)xfer, 0, 0);
1.51 augustss 1193: if (xfer == NULL) {
1.169 skrll 1194: pipe->up_running = 0;
1.51 augustss 1195: } else {
1.165 skrll 1196: if (!polling)
1.169 skrll 1197: mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1.195 riastrad 1198: SDT_PROBE2(usb, device, pipe, start, pipe, xfer);
1.169 skrll 1199: err = pipe->up_methods->upm_start(xfer);
1.165 skrll 1200: if (!polling)
1.169 skrll 1201: mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1.158 skrll 1202:
1.48 augustss 1203: if (err != USBD_IN_PROGRESS) {
1.175 pgoyette 1204: USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1.169 skrll 1205: pipe->up_running = 0;
1.31 augustss 1206: /* XXX do what? */
1207: }
1.1 augustss 1208: }
1.138 mrg 1209:
1.169 skrll 1210: KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1.1 augustss 1211: }
1212:
1213: usbd_status
1.169 skrll 1214: usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1.1 augustss 1215: {
1.17 augustss 1216:
1.169 skrll 1217: return usbd_do_request_flags(dev, req, data, 0, 0,
1218: USBD_DEFAULT_TIMEOUT);
1.77 augustss 1219: }
1220:
1221: usbd_status
1.169 skrll 1222: usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1223: void *data, uint16_t flags, int *actlen, uint32_t timeout)
1.77 augustss 1224: {
1.186 mrg 1225: size_t len = UGETW(req->wLength);
1226:
1227: return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
1228: }
1229:
1230: usbd_status
1231: usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
1232: size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
1233: {
1.169 skrll 1234: struct usbd_xfer *xfer;
1.48 augustss 1235: usbd_status err;
1.186 mrg 1236:
1237: KASSERT(len >= UGETW(req->wLength));
1.1 augustss 1238:
1.183 mrg 1239: USBHIST_FUNC();
1.186 mrg 1240: USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
1.185 mrg 1241: (uintptr_t)dev, (uintptr_t)req, flags, len);
1.183 mrg 1242:
1.169 skrll 1243: ASSERT_SLEEPABLE();
1.7 augustss 1244:
1.169 skrll 1245: int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1246: if (error)
1.221 skrll 1247: return USBD_NOMEM;
1.169 skrll 1248:
1249: usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1250: UGETW(req->wLength), flags, NULL);
1251: KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1.48 augustss 1252: err = usbd_sync_transfer(xfer);
1.1 augustss 1253: #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1.169 skrll 1254: if (xfer->ux_actlen > xfer->ux_length) {
1.198 christos 1255: USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1.169 skrll 1256: dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1.198 christos 1257: USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1.175 pgoyette 1258: "index = %jd",
1.169 skrll 1259: xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1260: UGETW(xfer->ux_request.wIndex), 0);
1.175 pgoyette 1261: USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1262: "actlen = %jd",
1.169 skrll 1263: UGETW(xfer->ux_request.wLength),
1264: xfer->ux_length, xfer->ux_actlen, 0);
1.112 dogcow 1265: }
1.1 augustss 1266: #endif
1.48 augustss 1267: if (actlen != NULL)
1.169 skrll 1268: *actlen = xfer->ux_actlen;
1269:
1270: usbd_destroy_xfer(xfer);
1.15 augustss 1271:
1.132 matt 1272: if (err) {
1.175 pgoyette 1273: USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1.132 matt 1274: }
1.169 skrll 1275: return err;
1.7 augustss 1276: }
1277:
1.176 khorben 1278: static void
1279: usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1280: {
1.202 jakllsch 1281: usbd_destroy_xfer(xfer);
1.176 khorben 1282: }
1283:
1284: /*
1285: * Execute a request without waiting for completion.
1286: * Can be used from interrupt context.
1287: */
1288: usbd_status
1289: usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1290: usb_device_request_t *req, void *priv, usbd_callback callback)
1291: {
1292: usbd_status err;
1293:
1294: if (callback == NULL)
1295: callback = usbd_request_async_cb;
1296:
1297: usbd_setup_default_xfer(xfer, dev, priv,
1298: USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1299: callback);
1300: err = usbd_transfer(xfer);
1301: if (err != USBD_IN_PROGRESS) {
1.202 jakllsch 1302: usbd_destroy_xfer(xfer);
1.176 khorben 1303: return (err);
1304: }
1305: return (USBD_NORMAL_COMPLETION);
1306: }
1307:
1.78 augustss 1308: const struct usbd_quirks *
1.169 skrll 1309: usbd_get_quirks(struct usbd_device *dev)
1.1 augustss 1310: {
1.81 augustss 1311: #ifdef DIAGNOSTIC
1312: if (dev == NULL) {
1313: printf("usbd_get_quirks: dev == NULL\n");
1314: return 0;
1315: }
1316: #endif
1.169 skrll 1317: return dev->ud_quirks;
1.1 augustss 1318: }
1319:
1320: /* XXX do periodic free() of free list */
1321:
1.6 augustss 1322: /*
1323: * Called from keyboard driver when in polling mode.
1324: */
1325: void
1.169 skrll 1326: usbd_dopoll(struct usbd_interface *iface)
1.6 augustss 1327: {
1.169 skrll 1328: iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1.8 augustss 1329: }
1330:
1.138 mrg 1331: /*
1.178 mrg 1332: * This is for keyboard driver as well, which only operates in polling
1333: * mode from the ask root, etc., prompt and from DDB.
1.138 mrg 1334: */
1.8 augustss 1335: void
1.169 skrll 1336: usbd_set_polling(struct usbd_device *dev, int on)
1.8 augustss 1337: {
1.42 augustss 1338: if (on)
1.169 skrll 1339: dev->ud_bus->ub_usepolling++;
1.42 augustss 1340: else
1.169 skrll 1341: dev->ud_bus->ub_usepolling--;
1.133 jmcneill 1342:
1343: /* Kick the host controller when switching modes */
1.169 skrll 1344: mutex_enter(dev->ud_bus->ub_lock);
1345: dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1346: mutex_exit(dev->ud_bus->ub_lock);
1.6 augustss 1347: }
1.12 augustss 1348:
1349:
1350: usb_endpoint_descriptor_t *
1.169 skrll 1351: usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1.12 augustss 1352: {
1353: struct usbd_endpoint *ep;
1354: int i;
1355:
1.169 skrll 1356: for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1357: ep = &iface->ui_endpoints[i];
1358: if (ep->ue_edesc->bEndpointAddress == address)
1359: return iface->ui_endpoints[i].ue_edesc;
1.12 augustss 1360: }
1.169 skrll 1361: return NULL;
1.63 augustss 1362: }
1363:
1364: /*
1365: * usbd_ratecheck() can limit the number of error messages that occurs.
1366: * When a device is unplugged it may take up to 0.25s for the hub driver
1.169 skrll 1367: * to notice it. If the driver continuously tries to do I/O operations
1.63 augustss 1368: * this can generate a large number of messages.
1369: */
1370: int
1.74 augustss 1371: usbd_ratecheck(struct timeval *last)
1.63 augustss 1372: {
1.64 augustss 1373: static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1.63 augustss 1374:
1.169 skrll 1375: return ratecheck(last, &errinterval);
1.89 augustss 1376: }
1377:
1378: /*
1379: * Search for a vendor/product pair in an array. The item size is
1380: * given as an argument.
1381: */
1.90 augustss 1382: const struct usb_devno *
1383: usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1.169 skrll 1384: uint16_t vendor, uint16_t product)
1.89 augustss 1385: {
1386: while (nentries-- > 0) {
1.169 skrll 1387: uint16_t tproduct = tbl->ud_product;
1.93 augustss 1388: if (tbl->ud_vendor == vendor &&
1389: (tproduct == product || tproduct == USB_PRODUCT_ANY))
1.169 skrll 1390: return tbl;
1.95 augustss 1391: tbl = (const struct usb_devno *)((const char *)tbl + sz);
1.89 augustss 1392: }
1.169 skrll 1393: return NULL;
1.12 augustss 1394: }
1395:
1.105 augustss 1396: usbd_status
1.169 skrll 1397: usbd_get_string(struct usbd_device *dev, int si, char *buf)
1.105 augustss 1398: {
1.115 pavel 1399: return usbd_get_string0(dev, si, buf, 1);
1400: }
1401:
1402: usbd_status
1.169 skrll 1403: usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1.115 pavel 1404: {
1.169 skrll 1405: int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1.105 augustss 1406: usb_string_descriptor_t us;
1407: char *s;
1.116 macallan 1408: int i, n;
1.169 skrll 1409: uint16_t c;
1.105 augustss 1410: usbd_status err;
1411: int size;
1412:
1.162 skrll 1413: USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1414:
1.105 augustss 1415: buf[0] = '\0';
1416: if (si == 0)
1.169 skrll 1417: return USBD_INVAL;
1418: if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1419: return USBD_STALLED;
1420: if (dev->ud_langid == USBD_NOLANG) {
1.105 augustss 1421: /* Set up default language */
1422: err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1423: &size);
1424: if (err || size < 4) {
1.162 skrll 1425: USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1426: 0, 0, 0, 0);
1.169 skrll 1427: dev->ud_langid = 0; /* Well, just pick something then */
1.105 augustss 1428: } else {
1429: /* Pick the first language as the default. */
1.169 skrll 1430: dev->ud_langid = UGETW(us.bString[0]);
1.105 augustss 1431: }
1432: }
1.169 skrll 1433: err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1.105 augustss 1434: if (err)
1.169 skrll 1435: return err;
1.105 augustss 1436: s = buf;
1437: n = size / 2 - 1;
1.115 pavel 1438: if (unicode) {
1439: for (i = 0; i < n; i++) {
1440: c = UGETW(us.bString[i]);
1441: if (swap)
1442: c = (c >> 8) | (c << 8);
1.119 drochner 1443: s += wput_utf8(s, 3, c);
1.115 pavel 1444: }
1445: *s++ = 0;
1446: }
1447: #ifdef COMPAT_30
1448: else {
1.119 drochner 1449: for (i = 0; i < n; i++) {
1.115 pavel 1450: c = UGETW(us.bString[i]);
1451: if (swap)
1452: c = (c >> 8) | (c << 8);
1.119 drochner 1453: *s++ = (c < 0x80) ? c : '?';
1.107 augustss 1454: }
1.119 drochner 1455: *s++ = 0;
1.105 augustss 1456: }
1.115 pavel 1457: #endif
1.169 skrll 1458: return USBD_NORMAL_COMPLETION;
1.105 augustss 1459: }
1.192 riastrad 1460:
1461: /*
1462: * usbd_xfer_trycomplete(xfer)
1463: *
1464: * Try to claim xfer for completion. Return true if successful,
1465: * false if the xfer has been synchronously aborted or has timed
1466: * out.
1467: *
1468: * If this returns true, caller is responsible for setting
1469: * xfer->ux_status and calling usb_transfer_complete. To be used
1470: * in a host controller interrupt handler.
1471: *
1472: * Caller must either hold the bus lock or have the bus in polling
1473: * mode.
1474: */
1475: bool
1476: usbd_xfer_trycomplete(struct usbd_xfer *xfer)
1477: {
1478: struct usbd_bus *bus __diagused = xfer->ux_bus;
1479:
1480: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1481:
1482: /*
1483: * If software has completed it, either by synchronous abort or
1484: * by timeout, too late.
1485: */
1486: if (xfer->ux_status != USBD_IN_PROGRESS)
1487: return false;
1488:
1489: /*
1490: * We are completing the xfer. Cancel the timeout if we can,
1491: * but only asynchronously. See usbd_xfer_cancel_timeout_async
1492: * for why we need not wait for the callout or task here.
1493: */
1494: usbd_xfer_cancel_timeout_async(xfer);
1495:
1496: /* Success! Note: Caller must set xfer->ux_status afterwar. */
1497: return true;
1498: }
1499:
1500: /*
1501: * usbd_xfer_abort(xfer)
1502: *
1503: * Try to claim xfer to abort. If successful, mark it completed
1504: * with USBD_CANCELLED and call the bus-specific method to abort
1505: * at the hardware level.
1506: *
1507: * To be called in thread context from struct
1508: * usbd_pipe_methods::upm_abort.
1509: *
1510: * Caller must hold the bus lock.
1511: */
1512: void
1513: usbd_xfer_abort(struct usbd_xfer *xfer)
1514: {
1515: struct usbd_bus *bus = xfer->ux_bus;
1516:
1517: KASSERT(mutex_owned(bus->ub_lock));
1518:
1519: /*
1520: * If host controller interrupt or timer interrupt has
1521: * completed it, too late. But the xfer cannot be
1522: * cancelled already -- only one caller can synchronously
1523: * abort.
1524: */
1525: KASSERT(xfer->ux_status != USBD_CANCELLED);
1526: if (xfer->ux_status != USBD_IN_PROGRESS)
1527: return;
1528:
1529: /*
1530: * Cancel the timeout if we can, but only asynchronously; see
1531: * usbd_xfer_cancel_timeout_async for why we need not wait for
1532: * the callout or task here.
1533: */
1534: usbd_xfer_cancel_timeout_async(xfer);
1535:
1536: /*
1537: * We beat everyone else. Claim the status as cancelled and do
1538: * the bus-specific dance to abort the hardware.
1539: */
1540: xfer->ux_status = USBD_CANCELLED;
1541: bus->ub_methods->ubm_abortx(xfer);
1542: }
1543:
1544: /*
1545: * usbd_xfer_timeout(xfer)
1546: *
1547: * Called at IPL_SOFTCLOCK when too much time has elapsed waiting
1548: * for xfer to complete. Since we can't abort the xfer at
1549: * IPL_SOFTCLOCK, defer to a usb_task to run it in thread context,
1550: * unless the xfer has completed or aborted concurrently -- and if
1551: * the xfer has also been resubmitted, take care of rescheduling
1552: * the callout.
1553: */
1554: static void
1555: usbd_xfer_timeout(void *cookie)
1556: {
1557: struct usbd_xfer *xfer = cookie;
1558: struct usbd_bus *bus = xfer->ux_bus;
1559: struct usbd_device *dev = xfer->ux_pipe->up_dev;
1560:
1561: /* Acquire the lock so we can transition the timeout state. */
1562: mutex_enter(bus->ub_lock);
1563:
1564: /*
1565: * Use usbd_xfer_probe_timeout to check whether the timeout is
1566: * still valid, or to reschedule the callout if necessary. If
1567: * it is still valid, schedule the task.
1568: */
1569: if (usbd_xfer_probe_timeout(xfer))
1570: usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
1571:
1572: /*
1573: * Notify usbd_xfer_cancel_timeout_async that we may have
1574: * scheduled the task. This causes callout_invoking to return
1575: * false in usbd_xfer_cancel_timeout_async so that it can tell
1576: * which stage in the callout->task->abort process we're at.
1577: */
1578: callout_ack(&xfer->ux_callout);
1579:
1580: /* All done -- release the lock. */
1581: mutex_exit(bus->ub_lock);
1582: }
1583:
1584: /*
1585: * usbd_xfer_timeout_task(xfer)
1586: *
1587: * Called in thread context when too much time has elapsed waiting
1588: * for xfer to complete. Abort the xfer with USBD_TIMEOUT, unless
1589: * it has completed or aborted concurrently -- and if the xfer has
1590: * also been resubmitted, take care of rescheduling the callout.
1591: */
1592: static void
1593: usbd_xfer_timeout_task(void *cookie)
1594: {
1595: struct usbd_xfer *xfer = cookie;
1596: struct usbd_bus *bus = xfer->ux_bus;
1597:
1598: /* Acquire the lock so we can transition the timeout state. */
1599: mutex_enter(bus->ub_lock);
1600:
1601: /*
1602: * Use usbd_xfer_probe_timeout to check whether the timeout is
1603: * still valid, or to reschedule the callout if necessary. If
1604: * it is not valid -- the timeout has been asynchronously
1605: * cancelled, or the xfer has already been resubmitted -- then
1606: * we're done here.
1607: */
1608: if (!usbd_xfer_probe_timeout(xfer))
1609: goto out;
1610:
1611: /*
1612: * May have completed or been aborted, but we're the only one
1613: * who can time it out. If it has completed or been aborted,
1614: * no need to timeout.
1615: */
1616: KASSERT(xfer->ux_status != USBD_TIMEOUT);
1617: if (xfer->ux_status != USBD_IN_PROGRESS)
1618: goto out;
1619:
1620: /*
1621: * We beat everyone else. Claim the status as timed out and do
1622: * the bus-specific dance to abort the hardware.
1623: */
1624: xfer->ux_status = USBD_TIMEOUT;
1625: bus->ub_methods->ubm_abortx(xfer);
1626:
1627: out: /* All done -- release the lock. */
1628: mutex_exit(bus->ub_lock);
1629: }
1630:
1631: /*
1632: * usbd_xfer_probe_timeout(xfer)
1633: *
1634: * Probe the status of xfer's timeout. Acknowledge and process a
1635: * request to reschedule. Return true if the timeout is still
1636: * valid and the caller should take further action (queueing a
1637: * task or aborting the xfer), false if it must stop here.
1638: */
1639: static bool
1640: usbd_xfer_probe_timeout(struct usbd_xfer *xfer)
1641: {
1642: struct usbd_bus *bus = xfer->ux_bus;
1643: bool valid;
1644:
1645: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1646:
1647: /* The timeout must be set. */
1648: KASSERT(xfer->ux_timeout_set);
1649:
1650: /*
1651: * Neither callout nor task may be pending; they execute
1652: * alternately in lock step.
1653: */
1654: KASSERT(!callout_pending(&xfer->ux_callout));
1655: KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1656:
1657: /* There are a few cases... */
1658: if (bus->ub_methods->ubm_dying(bus)) {
1659: /* Host controller dying. Drop it all on the floor. */
1660: xfer->ux_timeout_set = false;
1661: xfer->ux_timeout_reset = false;
1662: valid = false;
1663: } else if (xfer->ux_timeout_reset) {
1664: /*
1665: * The xfer completed _and_ got resubmitted while we
1666: * waited for the lock. Acknowledge the request to
1667: * reschedule, and reschedule it if there is a timeout
1668: * and the bus is not polling.
1669: */
1670: xfer->ux_timeout_reset = false;
1671: if (xfer->ux_timeout && !bus->ub_usepolling) {
1672: KASSERT(xfer->ux_timeout_set);
1673: callout_schedule(&xfer->ux_callout,
1674: mstohz(xfer->ux_timeout));
1675: } else {
1676: /* No more callout or task scheduled. */
1677: xfer->ux_timeout_set = false;
1678: }
1679: valid = false;
1680: } else if (xfer->ux_status != USBD_IN_PROGRESS) {
1681: /*
1682: * The xfer has completed by hardware completion or by
1683: * software abort, and has not been resubmitted, so the
1684: * timeout must be unset, and is no longer valid for
1685: * the caller.
1686: */
1687: xfer->ux_timeout_set = false;
1688: valid = false;
1689: } else {
1690: /*
1691: * The xfer has not yet completed, so the timeout is
1692: * valid.
1693: */
1694: valid = true;
1695: }
1696:
1697: /* Any reset must have been processed. */
1698: KASSERT(!xfer->ux_timeout_reset);
1699:
1700: /*
1701: * Either we claim the timeout is set, or the callout is idle.
1702: * If the timeout is still set, we may be handing off to the
1703: * task instead, so this is an if but not an iff.
1704: */
1705: KASSERT(xfer->ux_timeout_set || !callout_pending(&xfer->ux_callout));
1706:
1707: /*
1708: * The task must be idle now.
1709: *
1710: * - If the caller is the callout, _and_ the timeout is still
1711: * valid, the caller will schedule it, but it hasn't been
1712: * scheduled yet. (If the timeout is not valid, the task
1713: * should not be scheduled.)
1714: *
1715: * - If the caller is the task, it cannot be scheduled again
1716: * until the callout runs again, which won't happen until we
1717: * next release the lock.
1718: */
1719: KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1720:
1721: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1722:
1723: return valid;
1724: }
1725:
1726: /*
1727: * usbd_xfer_schedule_timeout(xfer)
1728: *
1729: * Ensure that xfer has a timeout. If the callout is already
1730: * queued or the task is already running, request that they
1731: * reschedule the callout. If not, and if we're not polling,
1732: * schedule the callout anew.
1733: *
1734: * To be called in thread context from struct
1735: * usbd_pipe_methods::upm_start.
1736: */
1737: void
1738: usbd_xfer_schedule_timeout(struct usbd_xfer *xfer)
1739: {
1740: struct usbd_bus *bus = xfer->ux_bus;
1741:
1742: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1743:
1744: if (xfer->ux_timeout_set) {
1745: /*
1746: * Callout or task has fired from a prior completed
1747: * xfer but has not yet noticed that the xfer is done.
1748: * Ask it to reschedule itself to ux_timeout.
1749: */
1750: xfer->ux_timeout_reset = true;
1751: } else if (xfer->ux_timeout && !bus->ub_usepolling) {
1752: /* Callout is not scheduled. Schedule it. */
1753: KASSERT(!callout_pending(&xfer->ux_callout));
1754: callout_schedule(&xfer->ux_callout, mstohz(xfer->ux_timeout));
1755: xfer->ux_timeout_set = true;
1756: }
1757:
1758: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1759: }
1760:
1761: /*
1762: * usbd_xfer_cancel_timeout_async(xfer)
1763: *
1764: * Cancel the callout and the task of xfer, which have not yet run
1765: * to completion, but don't wait for the callout or task to finish
1766: * running.
1767: *
1768: * If they have already fired, at worst they are waiting for the
1769: * bus lock. They will see that the xfer is no longer in progress
1770: * and give up, or they will see that the xfer has been
1771: * resubmitted with a new timeout and reschedule the callout.
1772: *
1773: * If a resubmitted request completed so fast that the callout
1774: * didn't have time to process a timer reset, just cancel the
1775: * timer reset.
1776: */
1777: static void
1778: usbd_xfer_cancel_timeout_async(struct usbd_xfer *xfer)
1779: {
1780: struct usbd_bus *bus __diagused = xfer->ux_bus;
1781:
1782: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1783:
1784: /*
1785: * If the timer wasn't running anyway, forget about it. This
1786: * can happen if we are completing an isochronous transfer
1787: * which doesn't use the same timeout logic.
1788: */
1789: if (!xfer->ux_timeout_set)
1790: return;
1791:
1792: xfer->ux_timeout_reset = false;
1793: if (!callout_stop(&xfer->ux_callout)) {
1794: /*
1795: * We stopped the callout before it ran. The timeout
1796: * is no longer set.
1797: */
1798: xfer->ux_timeout_set = false;
1799: } else if (callout_invoking(&xfer->ux_callout)) {
1800: /*
1801: * The callout has begun to run but it has not yet
1802: * acquired the lock and called callout_ack. The task
1803: * cannot be queued yet, and the callout cannot have
1804: * been rescheduled yet.
1805: *
1806: * By the time the callout acquires the lock, we will
1807: * have transitioned from USBD_IN_PROGRESS to a
1808: * completed status, and possibly also resubmitted the
1809: * xfer and set xfer->ux_timeout_reset = true. In both
1810: * cases, the callout will DTRT, so no further action
1811: * is needed here.
1812: */
1813: } else if (usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask)) {
1814: /*
1815: * The callout had fired and scheduled the task, but we
1816: * stopped the task before it could run. The timeout
1817: * is therefore no longer set -- the next resubmission
1818: * of the xfer must schedule a new timeout.
1819: *
1.204 msaitoh 1820: * The callout should not be pending at this point:
1.192 riastrad 1821: * it is scheduled only under the lock, and only when
1822: * xfer->ux_timeout_set is false, or by the callout or
1823: * task itself when xfer->ux_timeout_reset is true.
1824: */
1825: xfer->ux_timeout_set = false;
1826: }
1827:
1828: /*
1829: * The callout cannot be scheduled and the task cannot be
1830: * queued at this point. Either we cancelled them, or they are
1831: * already running and waiting for the bus lock.
1832: */
1833: KASSERT(!callout_pending(&xfer->ux_callout));
1834: KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
1835:
1836: KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
1837: }
CVSweb <webmaster@jp.NetBSD.org>