xref: /netbsd-src/sys/dev/usb/usbdi.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: usbdi.c,v 1.160 2013/11/30 12:16:14 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2012 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 and Matthew R. Green (mrg@eterna.com.au).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.160 2013/11/30 12:16:14 skrll Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_compat_netbsd.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/bus.h>
47 #include <sys/cpu.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbdivar.h>
53 #include <dev/usb/usb_mem.h>
54 #include <dev/usb/usb_quirks.h>
55 
56 /* UTF-8 encoding stuff */
57 #include <fs/unicode.h>
58 
59 #ifdef USB_DEBUG
60 #define DPRINTF(x)	if (usbdebug) printf x
61 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
62 extern int usbdebug;
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n,x)
66 #endif
67 
68 Static usbd_status usbd_ar_pipe(usbd_pipe_handle);
69 Static void usbd_start_next(usbd_pipe_handle);
70 Static usbd_status usbd_open_pipe_ival
71 	(usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
72 
73 static inline int
74 usbd_xfer_isread(usbd_xfer_handle xfer)
75 {
76 	if (xfer->rqflags & URQ_REQUEST)
77 		return (xfer->request.bmRequestType & UT_READ);
78 	else
79 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
80 			UE_DIR_IN);
81 }
82 
83 #if defined(USB_DEBUG) || defined(EHCI_DEBUG) || defined(OHCI_DEBUG)
84 void
85 usbd_dump_iface(struct usbd_interface *iface)
86 {
87 	printf("usbd_dump_iface: iface=%p\n", iface);
88 	if (iface == NULL)
89 		return;
90 	printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
91 	       iface->device, iface->idesc, iface->index, iface->altindex,
92 	       iface->priv);
93 }
94 
95 void
96 usbd_dump_device(struct usbd_device *dev)
97 {
98 	printf("usbd_dump_device: dev=%p\n", dev);
99 	if (dev == NULL)
100 		return;
101 	printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
102 	printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
103 	       "power=%d langid=%d\n",
104 	       dev->address, dev->config, dev->depth, dev->speed,
105 	       dev->self_powered, dev->power, dev->langid);
106 }
107 
108 void
109 usbd_dump_endpoint(struct usbd_endpoint *endp)
110 {
111 	printf("usbd_dump_endpoint: endp=%p\n", endp);
112 	if (endp == NULL)
113 		return;
114 	printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
115 	if (endp->edesc)
116 		printf(" bEndpointAddress=0x%02x\n",
117 		       endp->edesc->bEndpointAddress);
118 }
119 
120 void
121 usbd_dump_queue(usbd_pipe_handle pipe)
122 {
123 	usbd_xfer_handle xfer;
124 
125 	printf("usbd_dump_queue: pipe=%p\n", pipe);
126 	SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
127 		printf("  xfer=%p\n", xfer);
128 	}
129 }
130 
131 void
132 usbd_dump_pipe(usbd_pipe_handle pipe)
133 {
134 	printf("usbd_dump_pipe: pipe=%p\n", pipe);
135 	if (pipe == NULL)
136 		return;
137 	usbd_dump_iface(pipe->iface);
138 	usbd_dump_device(pipe->device);
139 	usbd_dump_endpoint(pipe->endpoint);
140 	printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
141 	       pipe->refcnt, pipe->running, pipe->aborting);
142 	printf(" intrxfer=%p, repeat=%d, interval=%d\n",
143 	       pipe->intrxfer, pipe->repeat, pipe->interval);
144 }
145 #endif
146 
147 usbd_status
148 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
149 	       u_int8_t flags, usbd_pipe_handle *pipe)
150 {
151 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
152 				    USBD_DEFAULT_INTERVAL));
153 }
154 
155 usbd_status
156 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
157 		    u_int8_t flags, usbd_pipe_handle *pipe, int ival)
158 {
159 	usbd_pipe_handle p;
160 	struct usbd_endpoint *ep;
161 	usbd_status err;
162 	int i;
163 
164 	DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
165 		    iface, address, flags));
166 
167 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
168 		ep = &iface->endpoints[i];
169 		if (ep->edesc == NULL)
170 			return (USBD_IOERROR);
171 		if (ep->edesc->bEndpointAddress == address)
172 			goto found;
173 	}
174 	return (USBD_BAD_ADDRESS);
175  found:
176 	if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
177 		return (USBD_IN_USE);
178 	err = usbd_setup_pipe_flags(iface->device, iface, ep, ival, &p, flags);
179 	if (err)
180 		return (err);
181 	LIST_INSERT_HEAD(&iface->pipes, p, next);
182 	*pipe = p;
183 	return (USBD_NORMAL_COMPLETION);
184 }
185 
186 usbd_status
187 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
188 		    u_int8_t flags, usbd_pipe_handle *pipe,
189 		    usbd_private_handle priv, void *buffer, u_int32_t len,
190 		    usbd_callback cb, int ival)
191 {
192 	usbd_status err;
193 	usbd_xfer_handle xfer;
194 	usbd_pipe_handle ipipe;
195 
196 	DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
197 		    address, flags, len));
198 
199 	err = usbd_open_pipe_ival(iface, address,
200 				  USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
201 				  &ipipe, ival);
202 	if (err)
203 		return (err);
204 	xfer = usbd_alloc_xfer(iface->device);
205 	if (xfer == NULL) {
206 		err = USBD_NOMEM;
207 		goto bad1;
208 	}
209 	usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
210 	    USBD_NO_TIMEOUT, cb);
211 	ipipe->intrxfer = xfer;
212 	ipipe->repeat = 1;
213 	err = usbd_transfer(xfer);
214 	*pipe = ipipe;
215 	if (err != USBD_IN_PROGRESS)
216 		goto bad2;
217 	return (USBD_NORMAL_COMPLETION);
218 
219  bad2:
220 	ipipe->intrxfer = NULL;
221 	ipipe->repeat = 0;
222 	usbd_free_xfer(xfer);
223  bad1:
224 	usbd_close_pipe(ipipe);
225 	return (err);
226 }
227 
228 usbd_status
229 usbd_close_pipe(usbd_pipe_handle pipe)
230 {
231 
232 #ifdef DIAGNOSTIC
233 	if (pipe == NULL) {
234 		printf("usbd_close_pipe: pipe==NULL\n");
235 		return (USBD_NORMAL_COMPLETION);
236 	}
237 #endif
238 
239 	usbd_lock_pipe(pipe);
240 	if (--pipe->refcnt != 0) {
241 		usbd_unlock_pipe(pipe);
242 		return (USBD_NORMAL_COMPLETION);
243 	}
244 	if (! SIMPLEQ_EMPTY(&pipe->queue)) {
245 		usbd_unlock_pipe(pipe);
246 		return (USBD_PENDING_REQUESTS);
247 	}
248 	LIST_REMOVE(pipe, next);
249 	pipe->endpoint->refcnt--;
250 	pipe->methods->close(pipe);
251 	usbd_unlock_pipe(pipe);
252 	if (pipe->intrxfer != NULL)
253 		usbd_free_xfer(pipe->intrxfer);
254 	free(pipe, M_USB);
255 	return (USBD_NORMAL_COMPLETION);
256 }
257 
258 usbd_status
259 usbd_transfer(usbd_xfer_handle xfer)
260 {
261 	usbd_pipe_handle pipe = xfer->pipe;
262 	usb_dma_t *dmap = &xfer->dmabuf;
263 	usbd_status err;
264 	unsigned int size, flags;
265 
266 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n",
267 		    xfer, xfer->flags, pipe, pipe->running));
268 
269 #ifdef USB_DEBUG
270 	if (usbdebug > 5)
271 		usbd_dump_queue(pipe);
272 #endif
273 	xfer->done = 0;
274 
275 	if (pipe->aborting)
276 		return (USBD_CANCELLED);
277 
278 	size = xfer->length;
279 	/* If there is no buffer, allocate one. */
280 	if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
281 		struct usbd_bus *bus = pipe->device->bus;
282 
283 #ifdef DIAGNOSTIC
284 		if (xfer->rqflags & URQ_AUTO_DMABUF)
285 			printf("usbd_transfer: has old buffer!\n");
286 #endif
287 		err = bus->methods->allocm(bus, dmap, size);
288 		if (err)
289 			return (err);
290 		xfer->rqflags |= URQ_AUTO_DMABUF;
291 	}
292 
293 	flags = xfer->flags;
294 
295 	/* Copy data if going out. */
296 	if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer))
297 		memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
298 
299 	/* xfer is not valid after the transfer method unless synchronous */
300 	err = pipe->methods->transfer(xfer);
301 
302 	if (err != USBD_IN_PROGRESS && err) {
303 		/* The transfer has not been queued, so free buffer. */
304 		if (xfer->rqflags & URQ_AUTO_DMABUF) {
305 			struct usbd_bus *bus = pipe->device->bus;
306 
307 			bus->methods->freem(bus, &xfer->dmabuf);
308 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
309 		}
310 	}
311 
312 	if (!(flags & USBD_SYNCHRONOUS))
313 		return (err);
314 
315 	/* Sync transfer, wait for completion. */
316 	if (err != USBD_IN_PROGRESS)
317 		return (err);
318 	usbd_lock_pipe(pipe);
319 	while (!xfer->done) {
320 		if (pipe->device->bus->use_polling)
321 			panic("usbd_transfer: not done");
322 
323 		err = 0;
324 		if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
325 			err = cv_wait_sig(&xfer->cv, pipe->device->bus->lock);
326 		} else {
327 			cv_wait(&xfer->cv, pipe->device->bus->lock);
328 		}
329 		if (err) {
330 			if (!xfer->done)
331 				pipe->methods->abort(xfer);
332 			break;
333 		}
334 	}
335 	usbd_unlock_pipe(pipe);
336 	return (xfer->status);
337 }
338 
339 /* Like usbd_transfer(), but waits for completion. */
340 usbd_status
341 usbd_sync_transfer(usbd_xfer_handle xfer)
342 {
343 	xfer->flags |= USBD_SYNCHRONOUS;
344 	return (usbd_transfer(xfer));
345 }
346 
347 /* Like usbd_transfer(), but waits for completion and listens for signals. */
348 usbd_status
349 usbd_sync_transfer_sig(usbd_xfer_handle xfer)
350 {
351 	xfer->flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
352 	return (usbd_transfer(xfer));
353 }
354 
355 void *
356 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
357 {
358 	struct usbd_bus *bus = xfer->device->bus;
359 	usbd_status err;
360 
361 #ifdef DIAGNOSTIC
362 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
363 		printf("usbd_alloc_buffer: xfer already has a buffer\n");
364 #endif
365 	err = bus->methods->allocm(bus, &xfer->dmabuf, size);
366 	if (err)
367 		return (NULL);
368 	xfer->rqflags |= URQ_DEV_DMABUF;
369 	return (KERNADDR(&xfer->dmabuf, 0));
370 }
371 
372 void
373 usbd_free_buffer(usbd_xfer_handle xfer)
374 {
375 #ifdef DIAGNOSTIC
376 	if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
377 		printf("usbd_free_buffer: no buffer\n");
378 		return;
379 	}
380 #endif
381 	xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
382 	xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
383 }
384 
385 void *
386 usbd_get_buffer(usbd_xfer_handle xfer)
387 {
388 	if (!(xfer->rqflags & URQ_DEV_DMABUF))
389 		return (NULL);
390 	return (KERNADDR(&xfer->dmabuf, 0));
391 }
392 
393 usbd_xfer_handle
394 usbd_alloc_xfer(usbd_device_handle dev)
395 {
396 	usbd_xfer_handle xfer;
397 
398 	xfer = dev->bus->methods->allocx(dev->bus);
399 	if (xfer == NULL)
400 		return (NULL);
401 	xfer->device = dev;
402 	callout_init(&xfer->timeout_handle, CALLOUT_MPSAFE);
403 	cv_init(&xfer->cv, "usbxfer");
404 	cv_init(&xfer->hccv, "usbhcxfer");
405 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
406 	return (xfer);
407 }
408 
409 usbd_status
410 usbd_free_xfer(usbd_xfer_handle xfer)
411 {
412 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
413 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
414 		usbd_free_buffer(xfer);
415 #if defined(DIAGNOSTIC)
416 	if (callout_pending(&xfer->timeout_handle)) {
417 		callout_stop(&xfer->timeout_handle);
418 		printf("usbd_free_xfer: timeout_handle pending\n");
419 	}
420 #endif
421 	cv_destroy(&xfer->cv);
422 	cv_destroy(&xfer->hccv);
423 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
424 	return (USBD_NORMAL_COMPLETION);
425 }
426 
427 void
428 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
429 		usbd_private_handle priv, void *buffer, u_int32_t length,
430 		u_int16_t flags, u_int32_t timeout,
431 		usbd_callback callback)
432 {
433 	xfer->pipe = pipe;
434 	xfer->priv = priv;
435 	xfer->buffer = buffer;
436 	xfer->length = length;
437 	xfer->actlen = 0;
438 	xfer->flags = flags;
439 	xfer->timeout = timeout;
440 	xfer->status = USBD_NOT_STARTED;
441 	xfer->callback = callback;
442 	xfer->rqflags &= ~URQ_REQUEST;
443 	xfer->nframes = 0;
444 }
445 
446 void
447 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
448 			usbd_private_handle priv, u_int32_t timeout,
449 			usb_device_request_t *req, void *buffer,
450 			u_int32_t length, u_int16_t flags,
451 			usbd_callback callback)
452 {
453 	xfer->pipe = dev->default_pipe;
454 	xfer->priv = priv;
455 	xfer->buffer = buffer;
456 	xfer->length = length;
457 	xfer->actlen = 0;
458 	xfer->flags = flags;
459 	xfer->timeout = timeout;
460 	xfer->status = USBD_NOT_STARTED;
461 	xfer->callback = callback;
462 	xfer->request = *req;
463 	xfer->rqflags |= URQ_REQUEST;
464 	xfer->nframes = 0;
465 }
466 
467 void
468 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
469 		     usbd_private_handle priv, u_int16_t *frlengths,
470 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
471 {
472 	xfer->pipe = pipe;
473 	xfer->priv = priv;
474 	xfer->buffer = 0;
475 	xfer->length = 0;
476 	xfer->actlen = 0;
477 	xfer->flags = flags;
478 	xfer->timeout = USBD_NO_TIMEOUT;
479 	xfer->status = USBD_NOT_STARTED;
480 	xfer->callback = callback;
481 	xfer->rqflags &= ~URQ_REQUEST;
482 	xfer->frlengths = frlengths;
483 	xfer->nframes = nframes;
484 }
485 
486 void
487 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
488 		     void **buffer, u_int32_t *count, usbd_status *status)
489 {
490 	if (priv != NULL)
491 		*priv = xfer->priv;
492 	if (buffer != NULL)
493 		*buffer = xfer->buffer;
494 	if (count != NULL)
495 		*count = xfer->actlen;
496 	if (status != NULL)
497 		*status = xfer->status;
498 }
499 
500 usb_config_descriptor_t *
501 usbd_get_config_descriptor(usbd_device_handle dev)
502 {
503 #ifdef DIAGNOSTIC
504 	if (dev == NULL) {
505 		printf("usbd_get_config_descriptor: dev == NULL\n");
506 		return (NULL);
507 	}
508 #endif
509 	return (dev->cdesc);
510 }
511 
512 usb_interface_descriptor_t *
513 usbd_get_interface_descriptor(usbd_interface_handle iface)
514 {
515 #ifdef DIAGNOSTIC
516 	if (iface == NULL) {
517 		printf("usbd_get_interface_descriptor: dev == NULL\n");
518 		return (NULL);
519 	}
520 #endif
521 	return (iface->idesc);
522 }
523 
524 usb_device_descriptor_t *
525 usbd_get_device_descriptor(usbd_device_handle dev)
526 {
527 	return (&dev->ddesc);
528 }
529 
530 usb_endpoint_descriptor_t *
531 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
532 {
533 	if (index >= iface->idesc->bNumEndpoints)
534 		return (NULL);
535 	return (iface->endpoints[index].edesc);
536 }
537 
538 /* Some drivers may wish to abort requests on the default pipe, *
539  * but there is no mechanism for getting a handle on it.        */
540 usbd_status
541 usbd_abort_default_pipe(struct usbd_device *device)
542 {
543 
544 	return usbd_abort_pipe(device->default_pipe);
545 }
546 
547 usbd_status
548 usbd_abort_pipe(usbd_pipe_handle pipe)
549 {
550 	usbd_status err;
551 	usbd_xfer_handle intrxfer = pipe->intrxfer;
552 
553 #ifdef DIAGNOSTIC
554 	if (pipe == NULL) {
555 		printf("usbd_abort_pipe: pipe==NULL\n");
556 		return (USBD_NORMAL_COMPLETION);
557 	}
558 #endif
559 	usbd_lock_pipe(pipe);
560 	err = usbd_ar_pipe(pipe);
561 	usbd_unlock_pipe(pipe);
562 	if (pipe->intrxfer != intrxfer)
563 		usbd_free_xfer(intrxfer);
564 	return (err);
565 }
566 
567 usbd_status
568 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
569 {
570 	usbd_device_handle dev = pipe->device;
571 	usb_device_request_t req;
572 	usbd_status err;
573 
574 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
575 
576 	/*
577 	 * Clearing en endpoint stall resets the endpoint toggle, so
578 	 * do the same to the HC toggle.
579 	 */
580 	pipe->methods->cleartoggle(pipe);
581 
582 	req.bmRequestType = UT_WRITE_ENDPOINT;
583 	req.bRequest = UR_CLEAR_FEATURE;
584 	USETW(req.wValue, UF_ENDPOINT_HALT);
585 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
586 	USETW(req.wLength, 0);
587 	err = usbd_do_request(dev, &req, 0);
588 #if 0
589 XXX should we do this?
590 	if (!err) {
591 		pipe->state = USBD_PIPE_ACTIVE;
592 		/* XXX activate pipe */
593 	}
594 #endif
595 	return (err);
596 }
597 
598 void
599 usbd_clear_endpoint_stall_task(void *arg)
600 {
601 	usbd_pipe_handle pipe = arg;
602 	usbd_device_handle dev = pipe->device;
603 	usb_device_request_t req;
604 
605 	pipe->methods->cleartoggle(pipe);
606 
607 	req.bmRequestType = UT_WRITE_ENDPOINT;
608 	req.bRequest = UR_CLEAR_FEATURE;
609 	USETW(req.wValue, UF_ENDPOINT_HALT);
610 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
611 	USETW(req.wLength, 0);
612 	(void)usbd_do_request(dev, &req, 0);
613 }
614 
615 void
616 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
617 {
618 
619 	usb_add_task(pipe->device, &pipe->async_task, USB_TASKQ_DRIVER);
620 }
621 
622 void
623 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
624 {
625 	pipe->methods->cleartoggle(pipe);
626 }
627 
628 usbd_status
629 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
630 {
631 #ifdef DIAGNOSTIC
632 	if (iface == NULL || iface->idesc == NULL) {
633 		printf("usbd_endpoint_count: NULL pointer\n");
634 		return (USBD_INVAL);
635 	}
636 #endif
637 	*count = iface->idesc->bNumEndpoints;
638 	return (USBD_NORMAL_COMPLETION);
639 }
640 
641 usbd_status
642 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
643 {
644 	if (dev->cdesc == NULL)
645 		return (USBD_NOT_CONFIGURED);
646 	*count = dev->cdesc->bNumInterface;
647 	return (USBD_NORMAL_COMPLETION);
648 }
649 
650 void
651 usbd_interface2device_handle(usbd_interface_handle iface,
652 			     usbd_device_handle *dev)
653 {
654 	*dev = iface->device;
655 }
656 
657 usbd_status
658 usbd_device2interface_handle(usbd_device_handle dev,
659 			     u_int8_t ifaceno, usbd_interface_handle *iface)
660 {
661 	if (dev->cdesc == NULL)
662 		return (USBD_NOT_CONFIGURED);
663 	if (ifaceno >= dev->cdesc->bNumInterface)
664 		return (USBD_INVAL);
665 	*iface = &dev->ifaces[ifaceno];
666 	return (USBD_NORMAL_COMPLETION);
667 }
668 
669 usbd_device_handle
670 usbd_pipe2device_handle(usbd_pipe_handle pipe)
671 {
672 	return (pipe->device);
673 }
674 
675 /* XXXX use altno */
676 usbd_status
677 usbd_set_interface(usbd_interface_handle iface, int altidx)
678 {
679 	usb_device_request_t req;
680 	usbd_status err;
681 	void *endpoints;
682 
683 	if (LIST_FIRST(&iface->pipes) != 0)
684 		return (USBD_IN_USE);
685 
686 	endpoints = iface->endpoints;
687 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
688 	if (err)
689 		return (err);
690 
691 	/* new setting works, we can free old endpoints */
692 	if (endpoints != NULL)
693 		free(endpoints, M_USB);
694 
695 #ifdef DIAGNOSTIC
696 	if (iface->idesc == NULL) {
697 		printf("usbd_set_interface: NULL pointer\n");
698 		return (USBD_INVAL);
699 	}
700 #endif
701 
702 	req.bmRequestType = UT_WRITE_INTERFACE;
703 	req.bRequest = UR_SET_INTERFACE;
704 	USETW(req.wValue, iface->idesc->bAlternateSetting);
705 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
706 	USETW(req.wLength, 0);
707 	return (usbd_do_request(iface->device, &req, 0));
708 }
709 
710 int
711 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
712 {
713 	char *p = (char *)cdesc;
714 	char *end = p + UGETW(cdesc->wTotalLength);
715 	usb_interface_descriptor_t *d;
716 	int n;
717 
718 	for (n = 0; p < end; p += d->bLength) {
719 		d = (usb_interface_descriptor_t *)p;
720 		if (p + d->bLength <= end &&
721 		    d->bDescriptorType == UDESC_INTERFACE &&
722 		    d->bInterfaceNumber == ifaceno)
723 			n++;
724 	}
725 	return (n);
726 }
727 
728 int
729 usbd_get_interface_altindex(usbd_interface_handle iface)
730 {
731 	return (iface->altindex);
732 }
733 
734 usbd_status
735 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
736 {
737 	usb_device_request_t req;
738 
739 	req.bmRequestType = UT_READ_INTERFACE;
740 	req.bRequest = UR_GET_INTERFACE;
741 	USETW(req.wValue, 0);
742 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
743 	USETW(req.wLength, 1);
744 	return (usbd_do_request(iface->device, &req, aiface));
745 }
746 
747 /*** Internal routines ***/
748 
749 /* Dequeue all pipe operations, called at splusb(). */
750 Static usbd_status
751 usbd_ar_pipe(usbd_pipe_handle pipe)
752 {
753 	usbd_xfer_handle xfer;
754 
755 	KASSERT(mutex_owned(pipe->device->bus->lock));
756 
757 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
758 #ifdef USB_DEBUG
759 	if (usbdebug > 5)
760 		usbd_dump_queue(pipe);
761 #endif
762 	pipe->repeat = 0;
763 	pipe->aborting = 1;
764 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
765 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
766 			    pipe, xfer, pipe->methods));
767 		/* Make the HC abort it (and invoke the callback). */
768 		pipe->methods->abort(xfer);
769 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
770 	}
771 	pipe->aborting = 0;
772 	return (USBD_NORMAL_COMPLETION);
773 }
774 
775 /* Called with USB lock held. */
776 void
777 usb_transfer_complete(usbd_xfer_handle xfer)
778 {
779 	usbd_pipe_handle pipe = xfer->pipe;
780 	usb_dma_t *dmap = &xfer->dmabuf;
781 	int sync = xfer->flags & USBD_SYNCHRONOUS;
782 	int erred = xfer->status == USBD_CANCELLED ||
783 	    xfer->status == USBD_TIMEOUT;
784 	int polling = pipe->device->bus->use_polling;
785 	int repeat;
786 
787 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
788 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
789 
790 	KASSERT(polling || mutex_owned(pipe->device->bus->lock));
791 
792 #ifdef DIAGNOSTIC
793 	if (xfer->busy_free != XFER_ONQU) {
794 		printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n",
795 		       xfer, xfer->busy_free);
796 	}
797 #endif
798 
799 #ifdef DIAGNOSTIC
800 	if (pipe == NULL) {
801 		printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
802 		return;
803 	}
804 #endif
805 	repeat = pipe->repeat;
806 	/* XXXX */
807 	if (polling)
808 		pipe->running = 0;
809 
810 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
811 	    usbd_xfer_isread(xfer)) {
812 #ifdef DIAGNOSTIC
813 		if (xfer->actlen > xfer->length) {
814 			printf("%s: actlen (%d) > len (%d)\n", __func__,
815 			       xfer->actlen, xfer->length);
816 			xfer->actlen = xfer->length;
817 		}
818 #endif
819 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
820 	}
821 
822 	/* if we allocated the buffer in usbd_transfer() we free it here. */
823 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
824 		if (!repeat) {
825 			struct usbd_bus *bus = pipe->device->bus;
826 			bus->methods->freem(bus, dmap);
827 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
828 		}
829 	}
830 
831 	if (!repeat) {
832 		/* Remove request from queue. */
833 
834 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->queue),
835 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
836 		     xfer);
837 #ifdef DIAGNOSTIC
838 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
839 			printf("%s: bad dequeue %p != %p\n", __func__,
840 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
841 		xfer->busy_free = XFER_BUSY;
842 #endif
843 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
844 	}
845 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
846 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
847 
848 	/* Count completed transfers. */
849 	++pipe->device->bus->stats.uds_requests
850 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
851 
852 	xfer->done = 1;
853 	if (!xfer->status && xfer->actlen < xfer->length &&
854 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
855 		DPRINTFN(-1,("usb_transfer_complete: short transfer %d<%d\n",
856 			     xfer->actlen, xfer->length));
857 		xfer->status = USBD_SHORT_XFER;
858 	}
859 
860 	if (repeat) {
861 		if (xfer->callback) {
862 			if (!polling)
863 				mutex_exit(pipe->device->bus->lock);
864 
865 			if (!(pipe->flags & USBD_MPSAFE))
866 				KERNEL_LOCK(1, curlwp);
867 			xfer->callback(xfer, xfer->priv, xfer->status);
868 			if (!(pipe->flags & USBD_MPSAFE))
869 				KERNEL_UNLOCK_ONE(curlwp);
870 
871 			if (!polling)
872 				mutex_enter(pipe->device->bus->lock);
873 		}
874 		pipe->methods->done(xfer);
875 	} else {
876 		pipe->methods->done(xfer);
877 		if (xfer->callback) {
878 			if (!polling)
879 				mutex_exit(pipe->device->bus->lock);
880 
881 			if (!(pipe->flags & USBD_MPSAFE))
882 				KERNEL_LOCK(1, curlwp);
883 			xfer->callback(xfer, xfer->priv, xfer->status);
884 			if (!(pipe->flags & USBD_MPSAFE))
885 				KERNEL_UNLOCK_ONE(curlwp);
886 
887 			if (!polling)
888 				mutex_enter(pipe->device->bus->lock);
889 		}
890 	}
891 
892 	if (sync && !polling) {
893 		cv_broadcast(&xfer->cv);
894 	}
895 
896 	if (!repeat) {
897 		/* XXX should we stop the queue on all errors? */
898 		if (erred && pipe->iface != NULL)	/* not control pipe */
899 			pipe->running = 0;
900 		else
901 			usbd_start_next(pipe);
902 	}
903 }
904 
905 /* Called with USB lock held. */
906 usbd_status
907 usb_insert_transfer(usbd_xfer_handle xfer)
908 {
909 	usbd_pipe_handle pipe = xfer->pipe;
910 	usbd_status err;
911 
912 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
913 		    pipe, pipe->running, xfer->timeout));
914 
915 	KASSERT(mutex_owned(pipe->device->bus->lock));
916 
917 #ifdef DIAGNOSTIC
918 	if (xfer->busy_free != XFER_BUSY) {
919 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
920 		       xfer, xfer->busy_free);
921 		return (USBD_INVAL);
922 	}
923 	xfer->busy_free = XFER_ONQU;
924 #endif
925 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
926 	if (pipe->running)
927 		err = USBD_IN_PROGRESS;
928 	else {
929 		pipe->running = 1;
930 		err = USBD_NORMAL_COMPLETION;
931 	}
932 	return (err);
933 }
934 
935 /* Called with USB lock held. */
936 void
937 usbd_start_next(usbd_pipe_handle pipe)
938 {
939 	usbd_xfer_handle xfer;
940 	usbd_status err;
941 
942 #ifdef DIAGNOSTIC
943 	if (pipe == NULL) {
944 		printf("usbd_start_next: pipe == NULL\n");
945 		return;
946 	}
947 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
948 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
949 		return;
950 	}
951 #endif
952 
953 	KASSERT(mutex_owned(pipe->device->bus->lock));
954 
955 	/* Get next request in queue. */
956 	xfer = SIMPLEQ_FIRST(&pipe->queue);
957 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
958 	if (xfer == NULL) {
959 		pipe->running = 0;
960 	} else {
961 		mutex_exit(pipe->device->bus->lock);
962 		err = pipe->methods->start(xfer);
963 		mutex_enter(pipe->device->bus->lock);
964 
965 		if (err != USBD_IN_PROGRESS) {
966 			printf("usbd_start_next: error=%d\n", err);
967 			pipe->running = 0;
968 			/* XXX do what? */
969 		}
970 	}
971 
972 	KASSERT(mutex_owned(pipe->device->bus->lock));
973 }
974 
975 usbd_status
976 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
977 {
978 	return (usbd_do_request_flags(dev, req, data, 0, 0,
979 				      USBD_DEFAULT_TIMEOUT));
980 }
981 
982 usbd_status
983 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
984 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
985 {
986 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
987 					   data, flags, actlen, timo));
988 }
989 
990 usbd_status
991 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
992 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
993 	u_int32_t timeout)
994 {
995 	usbd_xfer_handle xfer;
996 	usbd_status err;
997 
998 #ifdef DIAGNOSTIC
999 	if (cpu_intr_p() || cpu_softintr_p()) {
1000 		printf("usbd_do_request: not in process context\n");
1001 		return (USBD_INVAL);
1002 	}
1003 #endif
1004 
1005 	xfer = usbd_alloc_xfer(dev);
1006 	if (xfer == NULL)
1007 		return (USBD_NOMEM);
1008 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
1009 				data, UGETW(req->wLength), flags, 0);
1010 	xfer->pipe = pipe;
1011 	err = usbd_sync_transfer(xfer);
1012 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1013 	if (xfer->actlen > xfer->length) {
1014 		DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
1015 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1016 			 __func__, dev->address, xfer->request.bmRequestType,
1017 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
1018 			 UGETW(xfer->request.wIndex),
1019 			 UGETW(xfer->request.wLength),
1020 			 xfer->length, xfer->actlen));
1021 	}
1022 #endif
1023 	if (actlen != NULL)
1024 		*actlen = xfer->actlen;
1025 	if (err == USBD_STALLED) {
1026 		/*
1027 		 * The control endpoint has stalled.  Control endpoints
1028 		 * should not halt, but some may do so anyway so clear
1029 		 * any halt condition.
1030 		 */
1031 		usb_device_request_t treq;
1032 		usb_status_t status;
1033 		u_int16_t s;
1034 		usbd_status nerr;
1035 
1036 		treq.bmRequestType = UT_READ_ENDPOINT;
1037 		treq.bRequest = UR_GET_STATUS;
1038 		USETW(treq.wValue, 0);
1039 		USETW(treq.wIndex, 0);
1040 		USETW(treq.wLength, sizeof(usb_status_t));
1041 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1042 					   &treq, &status,sizeof(usb_status_t),
1043 					   0, 0);
1044 		nerr = usbd_sync_transfer(xfer);
1045 		if (nerr)
1046 			goto bad;
1047 		s = UGETW(status.wStatus);
1048 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1049 		if (!(s & UES_HALT))
1050 			goto bad;
1051 		treq.bmRequestType = UT_WRITE_ENDPOINT;
1052 		treq.bRequest = UR_CLEAR_FEATURE;
1053 		USETW(treq.wValue, UF_ENDPOINT_HALT);
1054 		USETW(treq.wIndex, 0);
1055 		USETW(treq.wLength, 0);
1056 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1057 					   &treq, &status, 0, 0, 0);
1058 		nerr = usbd_sync_transfer(xfer);
1059 		if (nerr)
1060 			goto bad;
1061 	}
1062 
1063  bad:
1064 	if (err) {
1065 		DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
1066 	}
1067 	usbd_free_xfer(xfer);
1068 	return (err);
1069 }
1070 
1071 const struct usbd_quirks *
1072 usbd_get_quirks(usbd_device_handle dev)
1073 {
1074 #ifdef DIAGNOSTIC
1075 	if (dev == NULL) {
1076 		printf("usbd_get_quirks: dev == NULL\n");
1077 		return 0;
1078 	}
1079 #endif
1080 	return (dev->quirks);
1081 }
1082 
1083 /* XXX do periodic free() of free list */
1084 
1085 /*
1086  * Called from keyboard driver when in polling mode.
1087  */
1088 void
1089 usbd_dopoll(usbd_interface_handle iface)
1090 {
1091 	iface->device->bus->methods->do_poll(iface->device->bus);
1092 }
1093 
1094 /*
1095  * XXX use this more???  use_polling it touched manually all over
1096  */
1097 void
1098 usbd_set_polling(usbd_device_handle dev, int on)
1099 {
1100 	if (on)
1101 		dev->bus->use_polling++;
1102 	else
1103 		dev->bus->use_polling--;
1104 
1105 	/* Kick the host controller when switching modes */
1106 	mutex_enter(dev->bus->lock);
1107 	(*dev->bus->methods->soft_intr)(dev->bus);
1108 	mutex_exit(dev->bus->lock);
1109 }
1110 
1111 
1112 usb_endpoint_descriptor_t *
1113 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1114 {
1115 	struct usbd_endpoint *ep;
1116 	int i;
1117 
1118 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1119 		ep = &iface->endpoints[i];
1120 		if (ep->edesc->bEndpointAddress == address)
1121 			return (iface->endpoints[i].edesc);
1122 	}
1123 	return (NULL);
1124 }
1125 
1126 /*
1127  * usbd_ratecheck() can limit the number of error messages that occurs.
1128  * When a device is unplugged it may take up to 0.25s for the hub driver
1129  * to notice it.  If the driver continuosly tries to do I/O operations
1130  * this can generate a large number of messages.
1131  */
1132 int
1133 usbd_ratecheck(struct timeval *last)
1134 {
1135 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1136 
1137 	return (ratecheck(last, &errinterval));
1138 }
1139 
1140 /*
1141  * Search for a vendor/product pair in an array.  The item size is
1142  * given as an argument.
1143  */
1144 const struct usb_devno *
1145 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1146 		 u_int16_t vendor, u_int16_t product)
1147 {
1148 	while (nentries-- > 0) {
1149 		u_int16_t tproduct = tbl->ud_product;
1150 		if (tbl->ud_vendor == vendor &&
1151 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
1152 			return (tbl);
1153 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
1154 	}
1155 	return (NULL);
1156 }
1157 
1158 
1159 void
1160 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1161 {
1162 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1163 
1164         iter->cur = (const uByte *)cd;
1165         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1166 }
1167 
1168 const usb_descriptor_t *
1169 usb_desc_iter_next(usbd_desc_iter_t *iter)
1170 {
1171 	const usb_descriptor_t *desc;
1172 
1173 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1174 		if (iter->cur != iter->end)
1175 			printf("usb_desc_iter_next: bad descriptor\n");
1176 		return NULL;
1177 	}
1178 	desc = (const usb_descriptor_t *)iter->cur;
1179 	if (desc->bLength == 0) {
1180 		printf("usb_desc_iter_next: descriptor length = 0\n");
1181 		return NULL;
1182 	}
1183 	iter->cur += desc->bLength;
1184 	if (iter->cur > iter->end) {
1185 		printf("usb_desc_iter_next: descriptor length too large\n");
1186 		return NULL;
1187 	}
1188 	return desc;
1189 }
1190 
1191 usbd_status
1192 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1193 {
1194 	return usbd_get_string0(dev, si, buf, 1);
1195 }
1196 
1197 usbd_status
1198 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
1199 {
1200 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1201 	usb_string_descriptor_t us;
1202 	char *s;
1203 	int i, n;
1204 	u_int16_t c;
1205 	usbd_status err;
1206 	int size;
1207 
1208 	buf[0] = '\0';
1209 	if (si == 0)
1210 		return (USBD_INVAL);
1211 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1212 		return (USBD_STALLED);
1213 	if (dev->langid == USBD_NOLANG) {
1214 		/* Set up default language */
1215 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1216 		    &size);
1217 		if (err || size < 4) {
1218 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1219 			dev->langid = 0; /* Well, just pick something then */
1220 		} else {
1221 			/* Pick the first language as the default. */
1222 			dev->langid = UGETW(us.bString[0]);
1223 		}
1224 	}
1225 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1226 	if (err)
1227 		return (err);
1228 	s = buf;
1229 	n = size / 2 - 1;
1230 	if (unicode) {
1231 		for (i = 0; i < n; i++) {
1232 			c = UGETW(us.bString[i]);
1233 			if (swap)
1234 				c = (c >> 8) | (c << 8);
1235 			s += wput_utf8(s, 3, c);
1236 		}
1237 		*s++ = 0;
1238 	}
1239 #ifdef COMPAT_30
1240 	else {
1241 		for (i = 0; i < n; i++) {
1242 			c = UGETW(us.bString[i]);
1243 			if (swap)
1244 				c = (c >> 8) | (c << 8);
1245 			*s++ = (c < 0x80) ? c : '?';
1246 		}
1247 		*s++ = 0;
1248 	}
1249 #endif
1250 	return (USBD_NORMAL_COMPLETION);
1251 }
1252