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