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