xref: /netbsd-src/sys/dev/usb/usbdi.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: usbdi.c,v 1.182 2019/02/17 04:17:31 rin 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@augustsson.net) at
9  * Carlstedt Research & Technology, Matthew R. Green (mrg@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.182 2019/02/17 04:17:31 rin 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/usbhist.h>
59 
60 /* UTF-8 encoding stuff */
61 #include <fs/unicode.h>
62 
63 extern int usbdebug;
64 
65 Static usbd_status usbd_ar_pipe(struct usbd_pipe *);
66 Static void usbd_start_next(struct usbd_pipe *);
67 Static usbd_status usbd_open_pipe_ival
68 	(struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
69 static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
70 static void usbd_free_buffer(struct usbd_xfer *);
71 static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
72 static usbd_status usbd_free_xfer(struct usbd_xfer *);
73 static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
74 
75 #if defined(USB_DEBUG)
76 void
77 usbd_dump_iface(struct usbd_interface *iface)
78 {
79 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
80 
81 	USBHIST_LOG(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
82 	if (iface == NULL)
83 		return;
84 	USBHIST_LOG(usbdebug, "     device = %#jx idesc = %#jx index = %d",
85 	    (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
86 	    iface->ui_index, 0);
87 	USBHIST_LOG(usbdebug, "     altindex=%d priv=%#jx",
88 	    iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
89 }
90 
91 void
92 usbd_dump_device(struct usbd_device *dev)
93 {
94 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
95 
96 	USBHIST_LOG(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
97 	if (dev == NULL)
98 		return;
99 	USBHIST_LOG(usbdebug, "     bus = %#jx default_pipe = %#jx",
100 	    (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
101 	USBHIST_LOG(usbdebug, "     address = %jd config = %jd depth = %jd ",
102 	    dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
103 	USBHIST_LOG(usbdebug, "     speed = %jd self_powered = %jd "
104 	    "power = %jd langid = %jd",
105 	    dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
106 }
107 
108 void
109 usbd_dump_endpoint(struct usbd_endpoint *endp)
110 {
111 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
112 
113 	USBHIST_LOG(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
114 	if (endp == NULL)
115 		return;
116 	USBHIST_LOG(usbdebug, "    edesc = %#jx refcnt = %jd",
117 	    (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
118 	if (endp->ue_edesc)
119 		USBHIST_LOG(usbdebug, "     bEndpointAddress=0x%02x",
120 		    endp->ue_edesc->bEndpointAddress, 0, 0, 0);
121 }
122 
123 void
124 usbd_dump_queue(struct usbd_pipe *pipe)
125 {
126 	struct usbd_xfer *xfer;
127 
128 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
129 
130 	USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
131 	SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
132 		USBHIST_LOG(usbdebug, "     xfer = %#jx", (uintptr_t)xfer,
133 		    0, 0, 0);
134 	}
135 }
136 
137 void
138 usbd_dump_pipe(struct usbd_pipe *pipe)
139 {
140 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
141 
142 	USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
143 	if (pipe == NULL)
144 		return;
145 	usbd_dump_iface(pipe->up_iface);
146 	usbd_dump_device(pipe->up_dev);
147 	usbd_dump_endpoint(pipe->up_endpoint);
148 	USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
149 	USBHIST_LOG(usbdebug, "     running = %jd aborting = %jd",
150 	    pipe->up_running, pipe->up_aborting, 0, 0);
151 	USBHIST_LOG(usbdebug, "     intrxfer = %#jx, repeat = %jd, "
152 	    "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
153 	    pipe->up_interval, 0);
154 }
155 #endif
156 
157 usbd_status
158 usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
159 	       uint8_t flags, struct usbd_pipe **pipe)
160 {
161 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
162 				    USBD_DEFAULT_INTERVAL));
163 }
164 
165 usbd_status
166 usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
167 		    uint8_t flags, struct usbd_pipe **pipe, int ival)
168 {
169 	struct usbd_pipe *p;
170 	struct usbd_endpoint *ep;
171 	usbd_status err;
172 	int i;
173 
174 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
175 
176 	USBHIST_LOG(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
177 	    (uintptr_t)iface, address, flags, 0);
178 
179 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
180 		ep = &iface->ui_endpoints[i];
181 		if (ep->ue_edesc == NULL)
182 			return USBD_IOERROR;
183 		if (ep->ue_edesc->bEndpointAddress == address)
184 			goto found;
185 	}
186 	return USBD_BAD_ADDRESS;
187  found:
188 	if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
189 		return USBD_IN_USE;
190 	err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
191 	if (err)
192 		return err;
193 	LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
194 	*pipe = p;
195 	return USBD_NORMAL_COMPLETION;
196 }
197 
198 usbd_status
199 usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
200 		    uint8_t flags, struct usbd_pipe **pipe,
201 		    void *priv, void *buffer, uint32_t len,
202 		    usbd_callback cb, int ival)
203 {
204 	usbd_status err;
205 	struct usbd_xfer *xfer;
206 	struct usbd_pipe *ipipe;
207 
208 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
209 
210 	USBHIST_LOG(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
211 	    address, flags, len, 0);
212 
213 	err = usbd_open_pipe_ival(iface, address,
214 				  USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
215 				  &ipipe, ival);
216 	if (err)
217 		return err;
218 	err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
219 	if (err)
220 		goto bad1;
221 
222 	usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
223 	ipipe->up_intrxfer = xfer;
224 	ipipe->up_repeat = 1;
225 	err = usbd_transfer(xfer);
226 	*pipe = ipipe;
227 	if (err != USBD_IN_PROGRESS)
228 		goto bad3;
229 	return USBD_NORMAL_COMPLETION;
230 
231  bad3:
232 	ipipe->up_intrxfer = NULL;
233 	ipipe->up_repeat = 0;
234 
235 	usbd_destroy_xfer(xfer);
236  bad1:
237 	usbd_close_pipe(ipipe);
238 	return err;
239 }
240 
241 usbd_status
242 usbd_close_pipe(struct usbd_pipe *pipe)
243 {
244 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
245 
246 	KASSERT(pipe != NULL);
247 
248 	usbd_lock_pipe(pipe);
249 
250 	if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
251 		printf("WARNING: pipe closed with active xfers on addr %d\n",
252 		    pipe->up_dev->ud_addr);
253 		usbd_ar_pipe(pipe);
254 	}
255 
256 	KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
257 
258 	LIST_REMOVE(pipe, up_next);
259 	pipe->up_endpoint->ue_refcnt--;
260 
261 	pipe->up_methods->upm_close(pipe);
262 
263 	if (pipe->up_intrxfer != NULL) {
264 	    	usbd_unlock_pipe(pipe);
265 		usbd_destroy_xfer(pipe->up_intrxfer);
266 		usbd_lock_pipe(pipe);
267 	}
268 
269 	usbd_unlock_pipe(pipe);
270 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
271 
272 	return USBD_NORMAL_COMPLETION;
273 }
274 
275 usbd_status
276 usbd_transfer(struct usbd_xfer *xfer)
277 {
278 	struct usbd_pipe *pipe = xfer->ux_pipe;
279 	usbd_status err;
280 	unsigned int size, flags;
281 
282 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
283 
284 	USBHIST_LOG(usbdebug,
285 	    "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
286 	    (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
287 	KASSERT(xfer->ux_status == USBD_NOT_STARTED);
288 
289 #ifdef USB_DEBUG
290 	if (usbdebug > 5)
291 		usbd_dump_queue(pipe);
292 #endif
293 	xfer->ux_done = 0;
294 
295 	if (pipe->up_aborting) {
296 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
297 		    (uintptr_t)xfer, 0, 0, 0);
298 		return USBD_CANCELLED;
299 	}
300 
301 	KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
302 
303 	size = xfer->ux_length;
304 	flags = xfer->ux_flags;
305 
306 	if (size != 0) {
307 		/*
308 		 * Use the xfer buffer if none specified in transfer setup.
309 		 * isoc transfers always use the xfer buffer, i.e.
310 		 * ux_buffer is always NULL for isoc.
311 		 */
312 		if (xfer->ux_buffer == NULL) {
313 			xfer->ux_buffer = xfer->ux_buf;
314 		}
315 
316 		/*
317 		 * If not using the xfer buffer copy data to the
318 		 * xfer buffer for OUT transfers of >0 length
319 		 */
320 		if (xfer->ux_buffer != xfer->ux_buf) {
321 			KASSERT(xfer->ux_buf);
322 			if (!usbd_xfer_isread(xfer)) {
323 				memcpy(xfer->ux_buf, xfer->ux_buffer, size);
324 			}
325 		}
326 	}
327 
328 	/* xfer is not valid after the transfer method unless synchronous */
329 	err = pipe->up_methods->upm_transfer(xfer);
330 	USBHIST_LOG(usbdebug, "<- done transfer %#jx, err = %jd",
331 	    (uintptr_t)xfer, err, 0, 0);
332 
333 	if (err != USBD_IN_PROGRESS && err) {
334 		/*
335 		 * The transfer made it onto the pipe queue, but didn't get
336 		 * accepted by the HCD for some reason.  It needs removing
337 		 * from the pipe queue.
338 		 */
339 		usbd_lock_pipe(pipe);
340 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
341 		if (pipe->up_serialise)
342 			usbd_start_next(pipe);
343 		usbd_unlock_pipe(pipe);
344 	}
345 
346 	if (!(flags & USBD_SYNCHRONOUS)) {
347 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
348 		    (uintptr_t)xfer, err, 0, 0);
349 		return err;
350 	}
351 
352 	if (err != USBD_IN_PROGRESS) {
353 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)"
354 		    "(complete/error)", (uintptr_t)xfer, err, 0, 0);
355 		return err;
356 	}
357 
358 	/* Sync transfer, wait for completion. */
359 	usbd_lock_pipe(pipe);
360 	while (!xfer->ux_done) {
361 		if (pipe->up_dev->ud_bus->ub_usepolling)
362 			panic("usbd_transfer: not done");
363 		USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
364 		    (uintptr_t)xfer, 0, 0, 0);
365 
366 		err = 0;
367 		if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
368 			err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
369 		} else {
370 			cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
371 		}
372 		if (err) {
373 			if (!xfer->ux_done)
374 				pipe->up_methods->upm_abort(xfer);
375 			break;
376 		}
377 	}
378 	usbd_unlock_pipe(pipe);
379 	return xfer->ux_status;
380 }
381 
382 /* Like usbd_transfer(), but waits for completion. */
383 usbd_status
384 usbd_sync_transfer(struct usbd_xfer *xfer)
385 {
386 	xfer->ux_flags |= USBD_SYNCHRONOUS;
387 	return usbd_transfer(xfer);
388 }
389 
390 /* Like usbd_transfer(), but waits for completion and listens for signals. */
391 usbd_status
392 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
393 {
394 	xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
395 	return usbd_transfer(xfer);
396 }
397 
398 static void *
399 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
400 {
401 	KASSERT(xfer->ux_buf == NULL);
402 	KASSERT(size != 0);
403 
404 	xfer->ux_bufsize = 0;
405 #if NUSB_DMA > 0
406 	struct usbd_bus *bus = xfer->ux_bus;
407 
408 	if (bus->ub_usedma) {
409 		usb_dma_t *dmap = &xfer->ux_dmabuf;
410 
411 		int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
412 		if (err) {
413 			return NULL;
414 		}
415 		xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
416 		xfer->ux_bufsize = size;
417 
418 		return xfer->ux_buf;
419 	}
420 #endif
421 	KASSERT(xfer->ux_bus->ub_usedma == false);
422 	xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
423 	xfer->ux_bufsize = size;
424 	return xfer->ux_buf;
425 }
426 
427 static void
428 usbd_free_buffer(struct usbd_xfer *xfer)
429 {
430 	KASSERT(xfer->ux_buf != NULL);
431 	KASSERT(xfer->ux_bufsize != 0);
432 
433 	void *buf = xfer->ux_buf;
434 	uint32_t size = xfer->ux_bufsize;
435 
436 	xfer->ux_buf = NULL;
437 	xfer->ux_bufsize = 0;
438 
439 #if NUSB_DMA > 0
440 	struct usbd_bus *bus = xfer->ux_bus;
441 
442 	if (bus->ub_usedma) {
443 		usb_dma_t *dmap = &xfer->ux_dmabuf;
444 
445 		usb_freemem(bus, dmap);
446 		return;
447 	}
448 #endif
449 	KASSERT(xfer->ux_bus->ub_usedma == false);
450 
451 	kmem_free(buf, size);
452 }
453 
454 void *
455 usbd_get_buffer(struct usbd_xfer *xfer)
456 {
457 	return xfer->ux_buf;
458 }
459 
460 struct usbd_pipe *
461 usbd_get_pipe0(struct usbd_device *dev)
462 {
463 
464 	return dev->ud_pipe0;
465 }
466 
467 static struct usbd_xfer *
468 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
469 {
470 	struct usbd_xfer *xfer;
471 
472 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
473 
474 	ASSERT_SLEEPABLE();
475 
476 	xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
477 	if (xfer == NULL)
478 		return NULL;
479 	xfer->ux_bus = dev->ud_bus;
480 	callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
481 	cv_init(&xfer->ux_cv, "usbxfer");
482 
483 	USBHIST_LOG(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
484 
485 	return xfer;
486 }
487 
488 static usbd_status
489 usbd_free_xfer(struct usbd_xfer *xfer)
490 {
491 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
492 
493 	USBHIST_LOG(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
494 	if (xfer->ux_buf) {
495 		usbd_free_buffer(xfer);
496 	}
497 #if defined(DIAGNOSTIC)
498 	if (callout_pending(&xfer->ux_callout)) {
499 		callout_stop(&xfer->ux_callout);
500 		printf("usbd_free_xfer: timeout_handle pending\n");
501 	}
502 #endif
503 	cv_destroy(&xfer->ux_cv);
504 	xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
505 	return USBD_NORMAL_COMPLETION;
506 }
507 
508 int
509 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
510     unsigned int nframes, struct usbd_xfer **xp)
511 {
512 	KASSERT(xp != NULL);
513 	void *buf = NULL;
514 
515 	struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
516 	if (xfer == NULL)
517 		return ENOMEM;
518 
519 	if (len) {
520 		buf = usbd_alloc_buffer(xfer, len);
521 		if (!buf) {
522 			usbd_free_xfer(xfer);
523 			return ENOMEM;
524 		}
525 	}
526 	xfer->ux_pipe = pipe;
527 	xfer->ux_flags = flags;
528 	xfer->ux_nframes = nframes;
529 	xfer->ux_methods = pipe->up_methods;
530 
531 	if (xfer->ux_methods->upm_init) {
532 		int err = xfer->ux_methods->upm_init(xfer);
533 		if (err) {
534 			if (buf)
535 				usbd_free_buffer(xfer);
536 			usbd_free_xfer(xfer);
537 			return err;
538 		}
539 	}
540 
541 	*xp = xfer;
542 	return 0;
543 }
544 
545 void
546 usbd_destroy_xfer(struct usbd_xfer *xfer)
547 {
548 
549 	if (xfer->ux_methods->upm_fini) {
550 		xfer->ux_methods->upm_fini(xfer);
551 	}
552 
553 	usbd_free_xfer(xfer);
554 }
555 
556 void
557 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
558     uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
559 {
560 	KASSERT(xfer->ux_pipe);
561 
562 	xfer->ux_priv = priv;
563 	xfer->ux_buffer = buffer;
564 	xfer->ux_length = length;
565 	xfer->ux_actlen = 0;
566 	xfer->ux_flags = flags;
567 	xfer->ux_timeout = timeout;
568 	xfer->ux_status = USBD_NOT_STARTED;
569 	xfer->ux_callback = callback;
570 	xfer->ux_rqflags &= ~URQ_REQUEST;
571 	xfer->ux_nframes = 0;
572 }
573 
574 void
575 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
576     void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
577     uint32_t length, uint16_t flags, usbd_callback callback)
578 {
579 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
580 
581 	xfer->ux_priv = priv;
582 	xfer->ux_buffer = buffer;
583 	xfer->ux_length = length;
584 	xfer->ux_actlen = 0;
585 	xfer->ux_flags = flags;
586 	xfer->ux_timeout = timeout;
587 	xfer->ux_status = USBD_NOT_STARTED;
588 	xfer->ux_callback = callback;
589 	xfer->ux_request = *req;
590 	xfer->ux_rqflags |= URQ_REQUEST;
591 	xfer->ux_nframes = 0;
592 }
593 
594 void
595 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
596     uint32_t nframes, uint16_t flags, usbd_callback callback)
597 {
598 	xfer->ux_priv = priv;
599 	xfer->ux_buffer = NULL;
600 	xfer->ux_length = 0;
601 	xfer->ux_actlen = 0;
602 	xfer->ux_flags = flags;
603 	xfer->ux_timeout = USBD_NO_TIMEOUT;
604 	xfer->ux_status = USBD_NOT_STARTED;
605 	xfer->ux_callback = callback;
606 	xfer->ux_rqflags &= ~URQ_REQUEST;
607 	xfer->ux_frlengths = frlengths;
608 	xfer->ux_nframes = nframes;
609 }
610 
611 void
612 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
613 		     void **buffer, uint32_t *count, usbd_status *status)
614 {
615 	if (priv != NULL)
616 		*priv = xfer->ux_priv;
617 	if (buffer != NULL)
618 		*buffer = xfer->ux_buffer;
619 	if (count != NULL)
620 		*count = xfer->ux_actlen;
621 	if (status != NULL)
622 		*status = xfer->ux_status;
623 }
624 
625 usb_config_descriptor_t *
626 usbd_get_config_descriptor(struct usbd_device *dev)
627 {
628 	KASSERT(dev != NULL);
629 
630 	return dev->ud_cdesc;
631 }
632 
633 usb_interface_descriptor_t *
634 usbd_get_interface_descriptor(struct usbd_interface *iface)
635 {
636 	KASSERT(iface != NULL);
637 
638 	return iface->ui_idesc;
639 }
640 
641 usb_device_descriptor_t *
642 usbd_get_device_descriptor(struct usbd_device *dev)
643 {
644 	KASSERT(dev != NULL);
645 
646 	return &dev->ud_ddesc;
647 }
648 
649 usb_endpoint_descriptor_t *
650 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
651 {
652 
653 	if (index >= iface->ui_idesc->bNumEndpoints)
654 		return NULL;
655 	return iface->ui_endpoints[index].ue_edesc;
656 }
657 
658 /* Some drivers may wish to abort requests on the default pipe, *
659  * but there is no mechanism for getting a handle on it.        */
660 usbd_status
661 usbd_abort_default_pipe(struct usbd_device *device)
662 {
663 	return usbd_abort_pipe(device->ud_pipe0);
664 }
665 
666 usbd_status
667 usbd_abort_pipe(struct usbd_pipe *pipe)
668 {
669 	usbd_status err;
670 
671 	KASSERT(pipe != NULL);
672 
673 	usbd_lock_pipe(pipe);
674 	err = usbd_ar_pipe(pipe);
675 	usbd_unlock_pipe(pipe);
676 	return err;
677 }
678 
679 usbd_status
680 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
681 {
682 	struct usbd_device *dev = pipe->up_dev;
683 	usb_device_request_t req;
684 	usbd_status err;
685 
686 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
687 
688 	/*
689 	 * Clearing en endpoint stall resets the endpoint toggle, so
690 	 * do the same to the HC toggle.
691 	 */
692 	pipe->up_methods->upm_cleartoggle(pipe);
693 
694 	req.bmRequestType = UT_WRITE_ENDPOINT;
695 	req.bRequest = UR_CLEAR_FEATURE;
696 	USETW(req.wValue, UF_ENDPOINT_HALT);
697 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
698 	USETW(req.wLength, 0);
699 	err = usbd_do_request(dev, &req, 0);
700 #if 0
701 XXX should we do this?
702 	if (!err) {
703 		pipe->state = USBD_PIPE_ACTIVE;
704 		/* XXX activate pipe */
705 	}
706 #endif
707 	return err;
708 }
709 
710 void
711 usbd_clear_endpoint_stall_task(void *arg)
712 {
713 	struct usbd_pipe *pipe = arg;
714 	struct usbd_device *dev = pipe->up_dev;
715 	usb_device_request_t req;
716 
717 	pipe->up_methods->upm_cleartoggle(pipe);
718 
719 	req.bmRequestType = UT_WRITE_ENDPOINT;
720 	req.bRequest = UR_CLEAR_FEATURE;
721 	USETW(req.wValue, UF_ENDPOINT_HALT);
722 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
723 	USETW(req.wLength, 0);
724 	(void)usbd_do_request(dev, &req, 0);
725 }
726 
727 void
728 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
729 {
730 	usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
731 }
732 
733 void
734 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
735 {
736 
737 	pipe->up_methods->upm_cleartoggle(pipe);
738 }
739 
740 usbd_status
741 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
742 {
743 	KASSERT(iface != NULL);
744 	KASSERT(iface->ui_idesc != NULL);
745 
746 	*count = iface->ui_idesc->bNumEndpoints;
747 	return USBD_NORMAL_COMPLETION;
748 }
749 
750 usbd_status
751 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
752 {
753 
754 	if (dev->ud_cdesc == NULL)
755 		return USBD_NOT_CONFIGURED;
756 	*count = dev->ud_cdesc->bNumInterface;
757 	return USBD_NORMAL_COMPLETION;
758 }
759 
760 void
761 usbd_interface2device_handle(struct usbd_interface *iface,
762 			     struct usbd_device **dev)
763 {
764 
765 	*dev = iface->ui_dev;
766 }
767 
768 usbd_status
769 usbd_device2interface_handle(struct usbd_device *dev,
770 			     uint8_t ifaceno, struct usbd_interface **iface)
771 {
772 
773 	if (dev->ud_cdesc == NULL)
774 		return USBD_NOT_CONFIGURED;
775 	if (ifaceno >= dev->ud_cdesc->bNumInterface)
776 		return USBD_INVAL;
777 	*iface = &dev->ud_ifaces[ifaceno];
778 	return USBD_NORMAL_COMPLETION;
779 }
780 
781 struct usbd_device *
782 usbd_pipe2device_handle(struct usbd_pipe *pipe)
783 {
784 	KASSERT(pipe != NULL);
785 
786 	return pipe->up_dev;
787 }
788 
789 /* XXXX use altno */
790 usbd_status
791 usbd_set_interface(struct usbd_interface *iface, int altidx)
792 {
793 	usb_device_request_t req;
794 	usbd_status err;
795 	void *endpoints;
796 
797 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
798 
799 	if (LIST_FIRST(&iface->ui_pipes) != NULL)
800 		return USBD_IN_USE;
801 
802 	endpoints = iface->ui_endpoints;
803 	int nendpt = iface->ui_idesc->bNumEndpoints;
804 	USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
805 	    (uintptr_t)iface, (uintptr_t)endpoints,
806 	    iface->ui_idesc->bNumEndpoints, 0);
807 	err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
808 	if (err)
809 		return err;
810 
811 	/* new setting works, we can free old endpoints */
812 	if (endpoints != NULL) {
813 		USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
814 		    (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
815 		kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
816 	}
817 	KASSERT(iface->ui_idesc != NULL);
818 
819 	req.bmRequestType = UT_WRITE_INTERFACE;
820 	req.bRequest = UR_SET_INTERFACE;
821 	USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
822 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
823 	USETW(req.wLength, 0);
824 	return usbd_do_request(iface->ui_dev, &req, 0);
825 }
826 
827 int
828 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
829 {
830 	char *p = (char *)cdesc;
831 	char *end = p + UGETW(cdesc->wTotalLength);
832 	usb_interface_descriptor_t *d;
833 	int n;
834 
835 	for (n = 0; p < end; p += d->bLength) {
836 		d = (usb_interface_descriptor_t *)p;
837 		if (p + d->bLength <= end &&
838 		    d->bDescriptorType == UDESC_INTERFACE &&
839 		    d->bInterfaceNumber == ifaceno)
840 			n++;
841 	}
842 	return n;
843 }
844 
845 int
846 usbd_get_interface_altindex(struct usbd_interface *iface)
847 {
848 	return iface->ui_altindex;
849 }
850 
851 usbd_status
852 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
853 {
854 	usb_device_request_t req;
855 
856 	req.bmRequestType = UT_READ_INTERFACE;
857 	req.bRequest = UR_GET_INTERFACE;
858 	USETW(req.wValue, 0);
859 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
860 	USETW(req.wLength, 1);
861 	return usbd_do_request(iface->ui_dev, &req, aiface);
862 }
863 
864 /*** Internal routines ***/
865 
866 /* Dequeue all pipe operations, called with bus lock held. */
867 Static usbd_status
868 usbd_ar_pipe(struct usbd_pipe *pipe)
869 {
870 	struct usbd_xfer *xfer;
871 
872 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
873 
874 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
875 
876 	USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
877 #ifdef USB_DEBUG
878 	if (usbdebug > 5)
879 		usbd_dump_queue(pipe);
880 #endif
881 	pipe->up_repeat = 0;
882 	pipe->up_aborting = 1;
883 	while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
884 		USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
885 		    "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
886 		    (uintptr_t)pipe->up_methods, 0);
887 		if (xfer->ux_status == USBD_NOT_STARTED) {
888 			SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
889 		} else {
890 			/* Make the HC abort it (and invoke the callback). */
891 			pipe->up_methods->upm_abort(xfer);
892 			/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
893 		}
894 	}
895 	pipe->up_aborting = 0;
896 	return USBD_NORMAL_COMPLETION;
897 }
898 
899 /* Called with USB lock held. */
900 void
901 usb_transfer_complete(struct usbd_xfer *xfer)
902 {
903 	struct usbd_pipe *pipe = xfer->ux_pipe;
904 	struct usbd_bus *bus = pipe->up_dev->ud_bus;
905 	int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
906 	int erred;
907 	int polling = bus->ub_usepolling;
908 	int repeat = pipe->up_repeat;
909 
910 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
911 
912 	USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
913 	    "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
914 	    xfer->ux_actlen);
915 
916 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
917 	KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
918 	    xfer->ux_state);
919 	KASSERT(pipe != NULL);
920 
921 	/*
922 	 * If device is known to miss out ack, then pretend that
923 	 * output timeout is a success. Userland should handle
924 	 * the logic to verify that the operation succeeded.
925 	 */
926 	if (pipe->up_dev->ud_quirks &&
927 	    pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
928 	    xfer->ux_status == USBD_TIMEOUT &&
929 	    !usbd_xfer_isread(xfer)) {
930 		USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
931 		    "hiding write timeout to %d.%s for %d bytes written",
932 		    (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
933 		    xfer->ux_length);
934 
935 		xfer->ux_status = USBD_NORMAL_COMPLETION;
936 		xfer->ux_actlen = xfer->ux_length;
937 	}
938 
939 	erred = xfer->ux_status == USBD_CANCELLED ||
940 	        xfer->ux_status == USBD_TIMEOUT;
941 
942 	if (!repeat) {
943 		/* Remove request from queue. */
944 
945 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
946 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
947 		     xfer);
948 		KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
949 		    "xfer %p is not start of queue (%p is at start)", xfer,
950 		   SIMPLEQ_FIRST(&pipe->up_queue));
951 
952 #ifdef DIAGNOSTIC
953 		xfer->ux_state = XFER_BUSY;
954 #endif
955 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
956 	}
957 	USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
958 	    (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
959 	    0);
960 
961 	/* Count completed transfers. */
962 	++pipe->up_dev->ud_bus->ub_stats.uds_requests
963 		[pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
964 
965 	xfer->ux_done = 1;
966 	if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
967 	    !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
968 		USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
969 		    xfer->ux_actlen, xfer->ux_length, 0, 0);
970 		xfer->ux_status = USBD_SHORT_XFER;
971 	}
972 
973 	USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
974 	    (uintptr_t)pipe->up_methods->upm_done, 0, 0);
975 	pipe->up_methods->upm_done(xfer);
976 
977 	if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
978 		KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
979 		    "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
980 
981 		/* Only if IN transfer */
982 		if (usbd_xfer_isread(xfer)) {
983 			memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
984 		}
985 	}
986 
987 	USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
988 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
989 
990 	if (xfer->ux_callback) {
991 		if (!polling) {
992 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
993 			if (!(pipe->up_flags & USBD_MPSAFE))
994 				KERNEL_LOCK(1, curlwp);
995 		}
996 
997 		xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
998 
999 		if (!polling) {
1000 			if (!(pipe->up_flags & USBD_MPSAFE))
1001 				KERNEL_UNLOCK_ONE(curlwp);
1002 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1003 		}
1004 	}
1005 
1006 	if (sync && !polling) {
1007 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
1008 		    (uintptr_t)xfer, 0, 0, 0);
1009 		cv_broadcast(&xfer->ux_cv);
1010 	}
1011 
1012 	if (repeat) {
1013 		xfer->ux_actlen = 0;
1014 		xfer->ux_status = USBD_NOT_STARTED;
1015 	} else {
1016 		/* XXX should we stop the queue on all errors? */
1017 		if (erred && pipe->up_iface != NULL)	/* not control pipe */
1018 			pipe->up_running = 0;
1019 	}
1020 	if (pipe->up_running && pipe->up_serialise)
1021 		usbd_start_next(pipe);
1022 }
1023 
1024 /* Called with USB lock held. */
1025 usbd_status
1026 usb_insert_transfer(struct usbd_xfer *xfer)
1027 {
1028 	struct usbd_pipe *pipe = xfer->ux_pipe;
1029 	usbd_status err;
1030 
1031 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1032 
1033 	USBHIST_LOG(usbdebug, "xfer = %#jx pipe = %#jx running = %jd "
1034 	    "timeout = %jd", (uintptr_t)xfer, (uintptr_t)pipe,
1035 	    pipe->up_running, xfer->ux_timeout);
1036 
1037 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1038 	KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1039 	    xfer->ux_state);
1040 
1041 #ifdef DIAGNOSTIC
1042 	xfer->ux_state = XFER_ONQU;
1043 #endif
1044 	SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1045 	if (pipe->up_running && pipe->up_serialise)
1046 		err = USBD_IN_PROGRESS;
1047 	else {
1048 		pipe->up_running = 1;
1049 		err = USBD_NORMAL_COMPLETION;
1050 	}
1051 	USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1052 	    err, 0, 0);
1053 	return err;
1054 }
1055 
1056 /* Called with USB lock held. */
1057 void
1058 usbd_start_next(struct usbd_pipe *pipe)
1059 {
1060 	struct usbd_xfer *xfer;
1061 	usbd_status err;
1062 
1063 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1064 
1065 	KASSERT(pipe != NULL);
1066 	KASSERT(pipe->up_methods != NULL);
1067 	KASSERT(pipe->up_methods->upm_start != NULL);
1068 	KASSERT(pipe->up_serialise == true);
1069 
1070 	int polling = pipe->up_dev->ud_bus->ub_usepolling;
1071 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1072 
1073 	/* Get next request in queue. */
1074 	xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1075 	USBHIST_LOG(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1076 	    (uintptr_t)xfer, 0, 0);
1077 	if (xfer == NULL) {
1078 		pipe->up_running = 0;
1079 	} else {
1080 		if (!polling)
1081 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1082 		err = pipe->up_methods->upm_start(xfer);
1083 		if (!polling)
1084 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1085 
1086 		if (err != USBD_IN_PROGRESS) {
1087 			USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1088 			pipe->up_running = 0;
1089 			/* XXX do what? */
1090 		}
1091 	}
1092 
1093 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1094 }
1095 
1096 usbd_status
1097 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1098 {
1099 
1100 	return usbd_do_request_flags(dev, req, data, 0, 0,
1101 	    USBD_DEFAULT_TIMEOUT);
1102 }
1103 
1104 usbd_status
1105 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1106     void *data, uint16_t flags, int *actlen, uint32_t timeout)
1107 {
1108 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1109 	struct usbd_xfer *xfer;
1110 	usbd_status err;
1111 
1112 	ASSERT_SLEEPABLE();
1113 
1114 	size_t len = UGETW(req->wLength);
1115 	int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1116 	if (error)
1117 		return error;
1118 
1119 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1120 	    UGETW(req->wLength), flags, NULL);
1121 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1122 	err = usbd_sync_transfer(xfer);
1123 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1124 	if (xfer->ux_actlen > xfer->ux_length) {
1125 		USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1126 		    dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1127 		USBHIST_LOG(usbdebug, "     req = 0x%02jx val = %jd "
1128 		    "index = %jd",
1129 		    xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1130 		    UGETW(xfer->ux_request.wIndex), 0);
1131 		USBHIST_LOG(usbdebug, "     rlen = %jd length = %jd "
1132 		    "actlen = %jd",
1133 		    UGETW(xfer->ux_request.wLength),
1134 		    xfer->ux_length, xfer->ux_actlen, 0);
1135 	}
1136 #endif
1137 	if (actlen != NULL)
1138 		*actlen = xfer->ux_actlen;
1139 
1140 	usbd_destroy_xfer(xfer);
1141 
1142 	if (err) {
1143 		USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1144 	}
1145 	return err;
1146 }
1147 
1148 static void
1149 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1150 {
1151 	usbd_free_xfer(xfer);
1152 }
1153 
1154 /*
1155  * Execute a request without waiting for completion.
1156  * Can be used from interrupt context.
1157  */
1158 usbd_status
1159 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1160     usb_device_request_t *req, void *priv, usbd_callback callback)
1161 {
1162 	usbd_status err;
1163 
1164 	if (callback == NULL)
1165 		callback = usbd_request_async_cb;
1166 
1167 	usbd_setup_default_xfer(xfer, dev, priv,
1168 	    USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1169 	    callback);
1170 	err = usbd_transfer(xfer);
1171 	if (err != USBD_IN_PROGRESS) {
1172 		usbd_free_xfer(xfer);
1173 		return (err);
1174 	}
1175 	return (USBD_NORMAL_COMPLETION);
1176 }
1177 
1178 const struct usbd_quirks *
1179 usbd_get_quirks(struct usbd_device *dev)
1180 {
1181 #ifdef DIAGNOSTIC
1182 	if (dev == NULL) {
1183 		printf("usbd_get_quirks: dev == NULL\n");
1184 		return 0;
1185 	}
1186 #endif
1187 	return dev->ud_quirks;
1188 }
1189 
1190 /* XXX do periodic free() of free list */
1191 
1192 /*
1193  * Called from keyboard driver when in polling mode.
1194  */
1195 void
1196 usbd_dopoll(struct usbd_interface *iface)
1197 {
1198 	iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1199 }
1200 
1201 /*
1202  * This is for keyboard driver as well, which only operates in polling
1203  * mode from the ask root, etc., prompt and from DDB.
1204  */
1205 void
1206 usbd_set_polling(struct usbd_device *dev, int on)
1207 {
1208 	if (on)
1209 		dev->ud_bus->ub_usepolling++;
1210 	else
1211 		dev->ud_bus->ub_usepolling--;
1212 
1213 	/* Kick the host controller when switching modes */
1214 	mutex_enter(dev->ud_bus->ub_lock);
1215 	dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1216 	mutex_exit(dev->ud_bus->ub_lock);
1217 }
1218 
1219 
1220 usb_endpoint_descriptor_t *
1221 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1222 {
1223 	struct usbd_endpoint *ep;
1224 	int i;
1225 
1226 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1227 		ep = &iface->ui_endpoints[i];
1228 		if (ep->ue_edesc->bEndpointAddress == address)
1229 			return iface->ui_endpoints[i].ue_edesc;
1230 	}
1231 	return NULL;
1232 }
1233 
1234 /*
1235  * usbd_ratecheck() can limit the number of error messages that occurs.
1236  * When a device is unplugged it may take up to 0.25s for the hub driver
1237  * to notice it.  If the driver continuously tries to do I/O operations
1238  * this can generate a large number of messages.
1239  */
1240 int
1241 usbd_ratecheck(struct timeval *last)
1242 {
1243 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1244 
1245 	return ratecheck(last, &errinterval);
1246 }
1247 
1248 /*
1249  * Search for a vendor/product pair in an array.  The item size is
1250  * given as an argument.
1251  */
1252 const struct usb_devno *
1253 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1254 		 uint16_t vendor, uint16_t product)
1255 {
1256 	while (nentries-- > 0) {
1257 		uint16_t tproduct = tbl->ud_product;
1258 		if (tbl->ud_vendor == vendor &&
1259 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
1260 			return tbl;
1261 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
1262 	}
1263 	return NULL;
1264 }
1265 
1266 
1267 void
1268 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
1269 {
1270 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1271 
1272 	iter->cur = (const uByte *)cd;
1273 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1274 }
1275 
1276 const usb_descriptor_t *
1277 usb_desc_iter_next(usbd_desc_iter_t *iter)
1278 {
1279 	const usb_descriptor_t *desc;
1280 
1281 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1282 		if (iter->cur != iter->end)
1283 			printf("usb_desc_iter_next: bad descriptor\n");
1284 		return NULL;
1285 	}
1286 	desc = (const usb_descriptor_t *)iter->cur;
1287 	if (desc->bLength == 0) {
1288 		printf("usb_desc_iter_next: descriptor length = 0\n");
1289 		return NULL;
1290 	}
1291 	iter->cur += desc->bLength;
1292 	if (iter->cur > iter->end) {
1293 		printf("usb_desc_iter_next: descriptor length too large\n");
1294 		return NULL;
1295 	}
1296 	return desc;
1297 }
1298 
1299 usbd_status
1300 usbd_get_string(struct usbd_device *dev, int si, char *buf)
1301 {
1302 	return usbd_get_string0(dev, si, buf, 1);
1303 }
1304 
1305 usbd_status
1306 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1307 {
1308 	int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1309 	usb_string_descriptor_t us;
1310 	char *s;
1311 	int i, n;
1312 	uint16_t c;
1313 	usbd_status err;
1314 	int size;
1315 
1316 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1317 
1318 	buf[0] = '\0';
1319 	if (si == 0)
1320 		return USBD_INVAL;
1321 	if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1322 		return USBD_STALLED;
1323 	if (dev->ud_langid == USBD_NOLANG) {
1324 		/* Set up default language */
1325 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1326 		    &size);
1327 		if (err || size < 4) {
1328 			USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1329 			    0, 0, 0, 0);
1330 			dev->ud_langid = 0; /* Well, just pick something then */
1331 		} else {
1332 			/* Pick the first language as the default. */
1333 			dev->ud_langid = UGETW(us.bString[0]);
1334 		}
1335 	}
1336 	err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1337 	if (err)
1338 		return err;
1339 	s = buf;
1340 	n = size / 2 - 1;
1341 	if (unicode) {
1342 		for (i = 0; i < n; i++) {
1343 			c = UGETW(us.bString[i]);
1344 			if (swap)
1345 				c = (c >> 8) | (c << 8);
1346 			s += wput_utf8(s, 3, c);
1347 		}
1348 		*s++ = 0;
1349 	}
1350 #ifdef COMPAT_30
1351 	else {
1352 		for (i = 0; i < n; i++) {
1353 			c = UGETW(us.bString[i]);
1354 			if (swap)
1355 				c = (c >> 8) | (c << 8);
1356 			*s++ = (c < 0x80) ? c : '?';
1357 		}
1358 		*s++ = 0;
1359 	}
1360 #endif
1361 	return USBD_NORMAL_COMPLETION;
1362 }
1363