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