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