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