xref: /netbsd-src/sys/dev/usb/usbdi.c (revision 4391d5e9d4f291db41e3b3ba26a01b5e51364aae)
1 /*	$NetBSD: usbdi.c,v 1.159 2013/10/04 12:47:04 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.159 2013/10/04 12:47:04 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,
403 	    dev->bus->methods->get_lock ? CALLOUT_MPSAFE : 0);
404 	cv_init(&xfer->cv, "usbxfer");
405 	cv_init(&xfer->hccv, "usbhcxfer");
406 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
407 	return (xfer);
408 }
409 
410 usbd_status
411 usbd_free_xfer(usbd_xfer_handle xfer)
412 {
413 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
414 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
415 		usbd_free_buffer(xfer);
416 #if defined(DIAGNOSTIC)
417 	if (callout_pending(&xfer->timeout_handle)) {
418 		callout_stop(&xfer->timeout_handle);
419 		printf("usbd_free_xfer: timeout_handle pending\n");
420 	}
421 #endif
422 	cv_destroy(&xfer->cv);
423 	cv_destroy(&xfer->hccv);
424 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
425 	return (USBD_NORMAL_COMPLETION);
426 }
427 
428 void
429 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
430 		usbd_private_handle priv, void *buffer, u_int32_t length,
431 		u_int16_t flags, u_int32_t timeout,
432 		usbd_callback callback)
433 {
434 	xfer->pipe = pipe;
435 	xfer->priv = priv;
436 	xfer->buffer = buffer;
437 	xfer->length = length;
438 	xfer->actlen = 0;
439 	xfer->flags = flags;
440 	xfer->timeout = timeout;
441 	xfer->status = USBD_NOT_STARTED;
442 	xfer->callback = callback;
443 	xfer->rqflags &= ~URQ_REQUEST;
444 	xfer->nframes = 0;
445 }
446 
447 void
448 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
449 			usbd_private_handle priv, u_int32_t timeout,
450 			usb_device_request_t *req, void *buffer,
451 			u_int32_t length, u_int16_t flags,
452 			usbd_callback callback)
453 {
454 	xfer->pipe = dev->default_pipe;
455 	xfer->priv = priv;
456 	xfer->buffer = buffer;
457 	xfer->length = length;
458 	xfer->actlen = 0;
459 	xfer->flags = flags;
460 	xfer->timeout = timeout;
461 	xfer->status = USBD_NOT_STARTED;
462 	xfer->callback = callback;
463 	xfer->request = *req;
464 	xfer->rqflags |= URQ_REQUEST;
465 	xfer->nframes = 0;
466 }
467 
468 void
469 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
470 		     usbd_private_handle priv, u_int16_t *frlengths,
471 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
472 {
473 	xfer->pipe = pipe;
474 	xfer->priv = priv;
475 	xfer->buffer = 0;
476 	xfer->length = 0;
477 	xfer->actlen = 0;
478 	xfer->flags = flags;
479 	xfer->timeout = USBD_NO_TIMEOUT;
480 	xfer->status = USBD_NOT_STARTED;
481 	xfer->callback = callback;
482 	xfer->rqflags &= ~URQ_REQUEST;
483 	xfer->frlengths = frlengths;
484 	xfer->nframes = nframes;
485 }
486 
487 void
488 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
489 		     void **buffer, u_int32_t *count, usbd_status *status)
490 {
491 	if (priv != NULL)
492 		*priv = xfer->priv;
493 	if (buffer != NULL)
494 		*buffer = xfer->buffer;
495 	if (count != NULL)
496 		*count = xfer->actlen;
497 	if (status != NULL)
498 		*status = xfer->status;
499 }
500 
501 usb_config_descriptor_t *
502 usbd_get_config_descriptor(usbd_device_handle dev)
503 {
504 #ifdef DIAGNOSTIC
505 	if (dev == NULL) {
506 		printf("usbd_get_config_descriptor: dev == NULL\n");
507 		return (NULL);
508 	}
509 #endif
510 	return (dev->cdesc);
511 }
512 
513 usb_interface_descriptor_t *
514 usbd_get_interface_descriptor(usbd_interface_handle iface)
515 {
516 #ifdef DIAGNOSTIC
517 	if (iface == NULL) {
518 		printf("usbd_get_interface_descriptor: dev == NULL\n");
519 		return (NULL);
520 	}
521 #endif
522 	return (iface->idesc);
523 }
524 
525 usb_device_descriptor_t *
526 usbd_get_device_descriptor(usbd_device_handle dev)
527 {
528 	return (&dev->ddesc);
529 }
530 
531 usb_endpoint_descriptor_t *
532 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
533 {
534 	if (index >= iface->idesc->bNumEndpoints)
535 		return (NULL);
536 	return (iface->endpoints[index].edesc);
537 }
538 
539 /* Some drivers may wish to abort requests on the default pipe, *
540  * but there is no mechanism for getting a handle on it.        */
541 usbd_status
542 usbd_abort_default_pipe(struct usbd_device *device)
543 {
544 
545 	return usbd_abort_pipe(device->default_pipe);
546 }
547 
548 usbd_status
549 usbd_abort_pipe(usbd_pipe_handle pipe)
550 {
551 	usbd_status err;
552 	usbd_xfer_handle intrxfer = pipe->intrxfer;
553 
554 #ifdef DIAGNOSTIC
555 	if (pipe == NULL) {
556 		printf("usbd_abort_pipe: pipe==NULL\n");
557 		return (USBD_NORMAL_COMPLETION);
558 	}
559 #endif
560 	usbd_lock_pipe(pipe);
561 	err = usbd_ar_pipe(pipe);
562 	usbd_unlock_pipe(pipe);
563 	if (pipe->intrxfer != intrxfer)
564 		usbd_free_xfer(intrxfer);
565 	return (err);
566 }
567 
568 usbd_status
569 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
570 {
571 	usbd_device_handle dev = pipe->device;
572 	usb_device_request_t req;
573 	usbd_status err;
574 
575 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
576 
577 	/*
578 	 * Clearing en endpoint stall resets the endpoint toggle, so
579 	 * do the same to the HC toggle.
580 	 */
581 	pipe->methods->cleartoggle(pipe);
582 
583 	req.bmRequestType = UT_WRITE_ENDPOINT;
584 	req.bRequest = UR_CLEAR_FEATURE;
585 	USETW(req.wValue, UF_ENDPOINT_HALT);
586 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
587 	USETW(req.wLength, 0);
588 	err = usbd_do_request(dev, &req, 0);
589 #if 0
590 XXX should we do this?
591 	if (!err) {
592 		pipe->state = USBD_PIPE_ACTIVE;
593 		/* XXX activate pipe */
594 	}
595 #endif
596 	return (err);
597 }
598 
599 void
600 usbd_clear_endpoint_stall_task(void *arg)
601 {
602 	usbd_pipe_handle pipe = arg;
603 	usbd_device_handle dev = pipe->device;
604 	usb_device_request_t req;
605 
606 	pipe->methods->cleartoggle(pipe);
607 
608 	req.bmRequestType = UT_WRITE_ENDPOINT;
609 	req.bRequest = UR_CLEAR_FEATURE;
610 	USETW(req.wValue, UF_ENDPOINT_HALT);
611 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
612 	USETW(req.wLength, 0);
613 	(void)usbd_do_request(dev, &req, 0);
614 }
615 
616 void
617 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
618 {
619 
620 	usb_add_task(pipe->device, &pipe->async_task, USB_TASKQ_DRIVER);
621 }
622 
623 void
624 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
625 {
626 	pipe->methods->cleartoggle(pipe);
627 }
628 
629 usbd_status
630 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
631 {
632 #ifdef DIAGNOSTIC
633 	if (iface == NULL || iface->idesc == NULL) {
634 		printf("usbd_endpoint_count: NULL pointer\n");
635 		return (USBD_INVAL);
636 	}
637 #endif
638 	*count = iface->idesc->bNumEndpoints;
639 	return (USBD_NORMAL_COMPLETION);
640 }
641 
642 usbd_status
643 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
644 {
645 	if (dev->cdesc == NULL)
646 		return (USBD_NOT_CONFIGURED);
647 	*count = dev->cdesc->bNumInterface;
648 	return (USBD_NORMAL_COMPLETION);
649 }
650 
651 void
652 usbd_interface2device_handle(usbd_interface_handle iface,
653 			     usbd_device_handle *dev)
654 {
655 	*dev = iface->device;
656 }
657 
658 usbd_status
659 usbd_device2interface_handle(usbd_device_handle dev,
660 			     u_int8_t ifaceno, usbd_interface_handle *iface)
661 {
662 	if (dev->cdesc == NULL)
663 		return (USBD_NOT_CONFIGURED);
664 	if (ifaceno >= dev->cdesc->bNumInterface)
665 		return (USBD_INVAL);
666 	*iface = &dev->ifaces[ifaceno];
667 	return (USBD_NORMAL_COMPLETION);
668 }
669 
670 usbd_device_handle
671 usbd_pipe2device_handle(usbd_pipe_handle pipe)
672 {
673 	return (pipe->device);
674 }
675 
676 /* XXXX use altno */
677 usbd_status
678 usbd_set_interface(usbd_interface_handle iface, int altidx)
679 {
680 	usb_device_request_t req;
681 	usbd_status err;
682 	void *endpoints;
683 
684 	if (LIST_FIRST(&iface->pipes) != 0)
685 		return (USBD_IN_USE);
686 
687 	endpoints = iface->endpoints;
688 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
689 	if (err)
690 		return (err);
691 
692 	/* new setting works, we can free old endpoints */
693 	if (endpoints != NULL)
694 		free(endpoints, M_USB);
695 
696 #ifdef DIAGNOSTIC
697 	if (iface->idesc == NULL) {
698 		printf("usbd_set_interface: NULL pointer\n");
699 		return (USBD_INVAL);
700 	}
701 #endif
702 
703 	req.bmRequestType = UT_WRITE_INTERFACE;
704 	req.bRequest = UR_SET_INTERFACE;
705 	USETW(req.wValue, iface->idesc->bAlternateSetting);
706 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
707 	USETW(req.wLength, 0);
708 	return (usbd_do_request(iface->device, &req, 0));
709 }
710 
711 int
712 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
713 {
714 	char *p = (char *)cdesc;
715 	char *end = p + UGETW(cdesc->wTotalLength);
716 	usb_interface_descriptor_t *d;
717 	int n;
718 
719 	for (n = 0; p < end; p += d->bLength) {
720 		d = (usb_interface_descriptor_t *)p;
721 		if (p + d->bLength <= end &&
722 		    d->bDescriptorType == UDESC_INTERFACE &&
723 		    d->bInterfaceNumber == ifaceno)
724 			n++;
725 	}
726 	return (n);
727 }
728 
729 int
730 usbd_get_interface_altindex(usbd_interface_handle iface)
731 {
732 	return (iface->altindex);
733 }
734 
735 usbd_status
736 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
737 {
738 	usb_device_request_t req;
739 
740 	req.bmRequestType = UT_READ_INTERFACE;
741 	req.bRequest = UR_GET_INTERFACE;
742 	USETW(req.wValue, 0);
743 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
744 	USETW(req.wLength, 1);
745 	return (usbd_do_request(iface->device, &req, aiface));
746 }
747 
748 /*** Internal routines ***/
749 
750 /* Dequeue all pipe operations, called at splusb(). */
751 Static usbd_status
752 usbd_ar_pipe(usbd_pipe_handle pipe)
753 {
754 	usbd_xfer_handle xfer;
755 
756 	KASSERT(mutex_owned(pipe->device->bus->lock));
757 
758 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
759 #ifdef USB_DEBUG
760 	if (usbdebug > 5)
761 		usbd_dump_queue(pipe);
762 #endif
763 	pipe->repeat = 0;
764 	pipe->aborting = 1;
765 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
766 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
767 			    pipe, xfer, pipe->methods));
768 		/* Make the HC abort it (and invoke the callback). */
769 		pipe->methods->abort(xfer);
770 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
771 	}
772 	pipe->aborting = 0;
773 	return (USBD_NORMAL_COMPLETION);
774 }
775 
776 /* Called with USB lock held. */
777 void
778 usb_transfer_complete(usbd_xfer_handle xfer)
779 {
780 	usbd_pipe_handle pipe = xfer->pipe;
781 	usb_dma_t *dmap = &xfer->dmabuf;
782 	int sync = xfer->flags & USBD_SYNCHRONOUS;
783 	int erred = xfer->status == USBD_CANCELLED ||
784 	    xfer->status == USBD_TIMEOUT;
785 	int polling = pipe->device->bus->use_polling;
786 	int repeat;
787 
788 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
789 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
790 
791 	KASSERT(polling || mutex_owned(pipe->device->bus->lock));
792 
793 #ifdef DIAGNOSTIC
794 	if (xfer->busy_free != XFER_ONQU) {
795 		printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n",
796 		       xfer, xfer->busy_free);
797 	}
798 #endif
799 
800 #ifdef DIAGNOSTIC
801 	if (pipe == NULL) {
802 		printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
803 		return;
804 	}
805 #endif
806 	repeat = pipe->repeat;
807 	/* XXXX */
808 	if (polling)
809 		pipe->running = 0;
810 
811 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
812 	    usbd_xfer_isread(xfer)) {
813 #ifdef DIAGNOSTIC
814 		if (xfer->actlen > xfer->length) {
815 			printf("%s: actlen (%d) > len (%d)\n", __func__,
816 			       xfer->actlen, xfer->length);
817 			xfer->actlen = xfer->length;
818 		}
819 #endif
820 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
821 	}
822 
823 	/* if we allocated the buffer in usbd_transfer() we free it here. */
824 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
825 		if (!repeat) {
826 			struct usbd_bus *bus = pipe->device->bus;
827 			bus->methods->freem(bus, dmap);
828 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
829 		}
830 	}
831 
832 	if (!repeat) {
833 		/* Remove request from queue. */
834 
835 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->queue),
836 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
837 		     xfer);
838 #ifdef DIAGNOSTIC
839 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
840 			printf("%s: bad dequeue %p != %p\n", __func__,
841 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
842 		xfer->busy_free = XFER_BUSY;
843 #endif
844 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
845 	}
846 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
847 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
848 
849 	/* Count completed transfers. */
850 	++pipe->device->bus->stats.uds_requests
851 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
852 
853 	xfer->done = 1;
854 	if (!xfer->status && xfer->actlen < xfer->length &&
855 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
856 		DPRINTFN(-1,("usb_transfer_complete: short transfer %d<%d\n",
857 			     xfer->actlen, xfer->length));
858 		xfer->status = USBD_SHORT_XFER;
859 	}
860 
861 	if (repeat) {
862 		if (xfer->callback) {
863 			if (!polling)
864 				mutex_exit(pipe->device->bus->lock);
865 
866 			if (!(pipe->flags & USBD_MPSAFE))
867 				KERNEL_LOCK(1, curlwp);
868 			xfer->callback(xfer, xfer->priv, xfer->status);
869 			if (!(pipe->flags & USBD_MPSAFE))
870 				KERNEL_UNLOCK_ONE(curlwp);
871 
872 			if (!polling)
873 				mutex_enter(pipe->device->bus->lock);
874 		}
875 		pipe->methods->done(xfer);
876 	} else {
877 		pipe->methods->done(xfer);
878 		if (xfer->callback) {
879 			if (!polling)
880 				mutex_exit(pipe->device->bus->lock);
881 
882 			if (!(pipe->flags & USBD_MPSAFE))
883 				KERNEL_LOCK(1, curlwp);
884 			xfer->callback(xfer, xfer->priv, xfer->status);
885 			if (!(pipe->flags & USBD_MPSAFE))
886 				KERNEL_UNLOCK_ONE(curlwp);
887 
888 			if (!polling)
889 				mutex_enter(pipe->device->bus->lock);
890 		}
891 	}
892 
893 	if (sync && !polling) {
894 		cv_broadcast(&xfer->cv);
895 	}
896 
897 	if (!repeat) {
898 		/* XXX should we stop the queue on all errors? */
899 		if (erred && pipe->iface != NULL)	/* not control pipe */
900 			pipe->running = 0;
901 		else
902 			usbd_start_next(pipe);
903 	}
904 }
905 
906 /* Called with USB lock held. */
907 usbd_status
908 usb_insert_transfer(usbd_xfer_handle xfer)
909 {
910 	usbd_pipe_handle pipe = xfer->pipe;
911 	usbd_status err;
912 
913 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
914 		    pipe, pipe->running, xfer->timeout));
915 
916 	KASSERT(mutex_owned(pipe->device->bus->lock));
917 
918 #ifdef DIAGNOSTIC
919 	if (xfer->busy_free != XFER_BUSY) {
920 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
921 		       xfer, xfer->busy_free);
922 		return (USBD_INVAL);
923 	}
924 	xfer->busy_free = XFER_ONQU;
925 #endif
926 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
927 	if (pipe->running)
928 		err = USBD_IN_PROGRESS;
929 	else {
930 		pipe->running = 1;
931 		err = USBD_NORMAL_COMPLETION;
932 	}
933 	return (err);
934 }
935 
936 /* Called with USB lock held. */
937 void
938 usbd_start_next(usbd_pipe_handle pipe)
939 {
940 	usbd_xfer_handle xfer;
941 	usbd_status err;
942 
943 #ifdef DIAGNOSTIC
944 	if (pipe == NULL) {
945 		printf("usbd_start_next: pipe == NULL\n");
946 		return;
947 	}
948 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
949 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
950 		return;
951 	}
952 #endif
953 
954 	KASSERT(mutex_owned(pipe->device->bus->lock));
955 
956 	/* Get next request in queue. */
957 	xfer = SIMPLEQ_FIRST(&pipe->queue);
958 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
959 	if (xfer == NULL) {
960 		pipe->running = 0;
961 	} else {
962 		mutex_exit(pipe->device->bus->lock);
963 		err = pipe->methods->start(xfer);
964 		mutex_enter(pipe->device->bus->lock);
965 
966 		if (err != USBD_IN_PROGRESS) {
967 			printf("usbd_start_next: error=%d\n", err);
968 			pipe->running = 0;
969 			/* XXX do what? */
970 		}
971 	}
972 
973 	KASSERT(mutex_owned(pipe->device->bus->lock));
974 }
975 
976 usbd_status
977 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
978 {
979 	return (usbd_do_request_flags(dev, req, data, 0, 0,
980 				      USBD_DEFAULT_TIMEOUT));
981 }
982 
983 usbd_status
984 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
985 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
986 {
987 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
988 					   data, flags, actlen, timo));
989 }
990 
991 usbd_status
992 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
993 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
994 	u_int32_t timeout)
995 {
996 	usbd_xfer_handle xfer;
997 	usbd_status err;
998 
999 #ifdef DIAGNOSTIC
1000 	if (cpu_intr_p() || cpu_softintr_p()) {
1001 		printf("usbd_do_request: not in process context\n");
1002 		return (USBD_INVAL);
1003 	}
1004 #endif
1005 
1006 	xfer = usbd_alloc_xfer(dev);
1007 	if (xfer == NULL)
1008 		return (USBD_NOMEM);
1009 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
1010 				data, UGETW(req->wLength), flags, 0);
1011 	xfer->pipe = pipe;
1012 	err = usbd_sync_transfer(xfer);
1013 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1014 	if (xfer->actlen > xfer->length) {
1015 		DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
1016 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1017 			 __func__, dev->address, xfer->request.bmRequestType,
1018 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
1019 			 UGETW(xfer->request.wIndex),
1020 			 UGETW(xfer->request.wLength),
1021 			 xfer->length, xfer->actlen));
1022 	}
1023 #endif
1024 	if (actlen != NULL)
1025 		*actlen = xfer->actlen;
1026 	if (err == USBD_STALLED) {
1027 		/*
1028 		 * The control endpoint has stalled.  Control endpoints
1029 		 * should not halt, but some may do so anyway so clear
1030 		 * any halt condition.
1031 		 */
1032 		usb_device_request_t treq;
1033 		usb_status_t status;
1034 		u_int16_t s;
1035 		usbd_status nerr;
1036 
1037 		treq.bmRequestType = UT_READ_ENDPOINT;
1038 		treq.bRequest = UR_GET_STATUS;
1039 		USETW(treq.wValue, 0);
1040 		USETW(treq.wIndex, 0);
1041 		USETW(treq.wLength, sizeof(usb_status_t));
1042 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1043 					   &treq, &status,sizeof(usb_status_t),
1044 					   0, 0);
1045 		nerr = usbd_sync_transfer(xfer);
1046 		if (nerr)
1047 			goto bad;
1048 		s = UGETW(status.wStatus);
1049 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1050 		if (!(s & UES_HALT))
1051 			goto bad;
1052 		treq.bmRequestType = UT_WRITE_ENDPOINT;
1053 		treq.bRequest = UR_CLEAR_FEATURE;
1054 		USETW(treq.wValue, UF_ENDPOINT_HALT);
1055 		USETW(treq.wIndex, 0);
1056 		USETW(treq.wLength, 0);
1057 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1058 					   &treq, &status, 0, 0, 0);
1059 		nerr = usbd_sync_transfer(xfer);
1060 		if (nerr)
1061 			goto bad;
1062 	}
1063 
1064  bad:
1065 	if (err) {
1066 		DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
1067 	}
1068 	usbd_free_xfer(xfer);
1069 	return (err);
1070 }
1071 
1072 const struct usbd_quirks *
1073 usbd_get_quirks(usbd_device_handle dev)
1074 {
1075 #ifdef DIAGNOSTIC
1076 	if (dev == NULL) {
1077 		printf("usbd_get_quirks: dev == NULL\n");
1078 		return 0;
1079 	}
1080 #endif
1081 	return (dev->quirks);
1082 }
1083 
1084 /* XXX do periodic free() of free list */
1085 
1086 /*
1087  * Called from keyboard driver when in polling mode.
1088  */
1089 void
1090 usbd_dopoll(usbd_interface_handle iface)
1091 {
1092 	iface->device->bus->methods->do_poll(iface->device->bus);
1093 }
1094 
1095 /*
1096  * XXX use this more???  use_polling it touched manually all over
1097  */
1098 void
1099 usbd_set_polling(usbd_device_handle dev, int on)
1100 {
1101 	if (on)
1102 		dev->bus->use_polling++;
1103 	else
1104 		dev->bus->use_polling--;
1105 
1106 	/* Kick the host controller when switching modes */
1107 	mutex_enter(dev->bus->lock);
1108 	(*dev->bus->methods->soft_intr)(dev->bus);
1109 	mutex_exit(dev->bus->lock);
1110 }
1111 
1112 
1113 usb_endpoint_descriptor_t *
1114 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1115 {
1116 	struct usbd_endpoint *ep;
1117 	int i;
1118 
1119 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1120 		ep = &iface->endpoints[i];
1121 		if (ep->edesc->bEndpointAddress == address)
1122 			return (iface->endpoints[i].edesc);
1123 	}
1124 	return (NULL);
1125 }
1126 
1127 /*
1128  * usbd_ratecheck() can limit the number of error messages that occurs.
1129  * When a device is unplugged it may take up to 0.25s for the hub driver
1130  * to notice it.  If the driver continuosly tries to do I/O operations
1131  * this can generate a large number of messages.
1132  */
1133 int
1134 usbd_ratecheck(struct timeval *last)
1135 {
1136 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1137 
1138 	return (ratecheck(last, &errinterval));
1139 }
1140 
1141 /*
1142  * Search for a vendor/product pair in an array.  The item size is
1143  * given as an argument.
1144  */
1145 const struct usb_devno *
1146 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1147 		 u_int16_t vendor, u_int16_t product)
1148 {
1149 	while (nentries-- > 0) {
1150 		u_int16_t tproduct = tbl->ud_product;
1151 		if (tbl->ud_vendor == vendor &&
1152 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
1153 			return (tbl);
1154 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
1155 	}
1156 	return (NULL);
1157 }
1158 
1159 
1160 void
1161 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1162 {
1163 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1164 
1165         iter->cur = (const uByte *)cd;
1166         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1167 }
1168 
1169 const usb_descriptor_t *
1170 usb_desc_iter_next(usbd_desc_iter_t *iter)
1171 {
1172 	const usb_descriptor_t *desc;
1173 
1174 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1175 		if (iter->cur != iter->end)
1176 			printf("usb_desc_iter_next: bad descriptor\n");
1177 		return NULL;
1178 	}
1179 	desc = (const usb_descriptor_t *)iter->cur;
1180 	if (desc->bLength == 0) {
1181 		printf("usb_desc_iter_next: descriptor length = 0\n");
1182 		return NULL;
1183 	}
1184 	iter->cur += desc->bLength;
1185 	if (iter->cur > iter->end) {
1186 		printf("usb_desc_iter_next: descriptor length too large\n");
1187 		return NULL;
1188 	}
1189 	return desc;
1190 }
1191 
1192 usbd_status
1193 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1194 {
1195 	return usbd_get_string0(dev, si, buf, 1);
1196 }
1197 
1198 usbd_status
1199 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
1200 {
1201 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1202 	usb_string_descriptor_t us;
1203 	char *s;
1204 	int i, n;
1205 	u_int16_t c;
1206 	usbd_status err;
1207 	int size;
1208 
1209 	buf[0] = '\0';
1210 	if (si == 0)
1211 		return (USBD_INVAL);
1212 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1213 		return (USBD_STALLED);
1214 	if (dev->langid == USBD_NOLANG) {
1215 		/* Set up default language */
1216 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1217 		    &size);
1218 		if (err || size < 4) {
1219 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1220 			dev->langid = 0; /* Well, just pick something then */
1221 		} else {
1222 			/* Pick the first language as the default. */
1223 			dev->langid = UGETW(us.bString[0]);
1224 		}
1225 	}
1226 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1227 	if (err)
1228 		return (err);
1229 	s = buf;
1230 	n = size / 2 - 1;
1231 	if (unicode) {
1232 		for (i = 0; i < n; i++) {
1233 			c = UGETW(us.bString[i]);
1234 			if (swap)
1235 				c = (c >> 8) | (c << 8);
1236 			s += wput_utf8(s, 3, c);
1237 		}
1238 		*s++ = 0;
1239 	}
1240 #ifdef COMPAT_30
1241 	else {
1242 		for (i = 0; i < n; i++) {
1243 			c = UGETW(us.bString[i]);
1244 			if (swap)
1245 				c = (c >> 8) | (c << 8);
1246 			*s++ = (c < 0x80) ? c : '?';
1247 		}
1248 		*s++ = 0;
1249 	}
1250 #endif
1251 	return (USBD_NORMAL_COMPLETION);
1252 }
1253