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