1 /* $NetBSD: usbdi.c,v 1.60 2000/01/19 00:23:58 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.34.2.7 2002/11/06 14:03:37 joe Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #if defined(__NetBSD__) || defined(__OpenBSD__)
44 #include <sys/device.h>
45 #elif defined(__FreeBSD__)
46 #include <sys/module.h>
49 #if defined(DIAGNOSTIC) && defined(__i386__)
50 #include <machine/cpu.h>
53 #include <sys/malloc.h>
56 #include <machine/bus.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdivar.h>
62 #include <dev/usb/usb_mem.h>
64 #if defined(__FreeBSD__)
66 #include <machine/clock.h>
67 #define delay(d) DELAY(d)
71 #define DPRINTF(x) if (usbdebug) logprintf x
72 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
79 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
80 Static void usbd_do_request_async_cb
81 (usbd_xfer_handle, usbd_private_handle, usbd_status);
82 Static void usbd_start_next(usbd_pipe_handle pipe);
83 Static usbd_status usbd_open_pipe_ival
84 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
86 Static int usbd_nbuses = 0;
100 Static __inline int usbd_xfer_isread(usbd_xfer_handle xfer);
102 usbd_xfer_isread(xfer)
103 usbd_xfer_handle xfer;
105 if (xfer->rqflags & URQ_REQUEST)
106 return (xfer->request.bmRequestType & UT_READ);
108 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
114 usbd_dump_queue(usbd_pipe_handle);
117 usbd_dump_queue(usbd_pipe_handle pipe)
119 usbd_xfer_handle xfer;
121 printf("usbd_dump_queue: pipe=%p\n", pipe);
122 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
124 xfer = SIMPLEQ_NEXT(xfer, next)) {
125 printf(" xfer=%p\n", xfer);
131 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
132 u_int8_t flags, usbd_pipe_handle *pipe)
134 return (usbd_open_pipe_ival(iface, address, flags, pipe,
135 USBD_DEFAULT_INTERVAL));
139 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
140 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
143 struct usbd_endpoint *ep;
147 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
148 iface, address, flags));
150 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
151 ep = &iface->endpoints[i];
152 if (ep->edesc == NULL)
153 return (USBD_IOERROR);
154 if (ep->edesc->bEndpointAddress == address)
157 return (USBD_BAD_ADDRESS);
159 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
160 return (USBD_IN_USE);
161 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
164 LIST_INSERT_HEAD(&iface->pipes, p, next);
166 return (USBD_NORMAL_COMPLETION);
170 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
171 u_int8_t flags, usbd_pipe_handle *pipe,
172 usbd_private_handle priv, void *buffer, u_int32_t len,
173 usbd_callback cb, int ival)
176 usbd_xfer_handle xfer;
177 usbd_pipe_handle ipipe;
179 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
180 address, flags, len));
182 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
186 xfer = usbd_alloc_xfer(iface->device);
191 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
192 USBD_NO_TIMEOUT, cb);
193 ipipe->intrxfer = xfer;
195 err = usbd_transfer(xfer);
197 if (err != USBD_IN_PROGRESS)
199 return (USBD_NORMAL_COMPLETION);
202 ipipe->intrxfer = NULL;
204 usbd_free_xfer(xfer);
206 usbd_close_pipe(ipipe);
211 usbd_close_pipe(usbd_pipe_handle pipe)
215 printf("usbd_close_pipe: pipe==NULL\n");
216 return (USBD_NORMAL_COMPLETION);
220 if (--pipe->refcnt != 0)
221 return (USBD_NORMAL_COMPLETION);
222 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
223 return (USBD_PENDING_REQUESTS);
224 LIST_REMOVE(pipe, next);
225 pipe->endpoint->refcnt--;
226 pipe->methods->close(pipe);
227 if (pipe->intrxfer != NULL)
228 usbd_free_xfer(pipe->intrxfer);
230 return (USBD_NORMAL_COMPLETION);
234 usbd_transfer(usbd_xfer_handle xfer)
236 usbd_pipe_handle pipe = xfer->pipe;
237 usb_dma_t *dmap = &xfer->dmabuf;
242 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
243 xfer, xfer->flags, pipe, pipe->running));
246 usbd_dump_queue(pipe);
251 /* If there is no buffer, allocate one. */
252 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
253 struct usbd_bus *bus = pipe->device->bus;
256 if (xfer->rqflags & URQ_AUTO_DMABUF)
257 printf("usbd_transfer: has old buffer!\n");
259 err = bus->methods->allocm(bus, dmap, size);
262 xfer->rqflags |= URQ_AUTO_DMABUF;
265 /* Copy data if going out. */
266 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
267 !usbd_xfer_isread(xfer))
268 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
270 err = pipe->methods->transfer(xfer);
272 if (err != USBD_IN_PROGRESS && err) {
273 /* The transfer has not been queued, so free buffer. */
274 if (xfer->rqflags & URQ_AUTO_DMABUF) {
275 struct usbd_bus *bus = pipe->device->bus;
277 bus->methods->freem(bus, &xfer->dmabuf);
278 xfer->rqflags &= ~URQ_AUTO_DMABUF;
282 if (!(xfer->flags & USBD_SYNCHRONOUS))
285 /* Sync transfer, wait for completion. */
286 if (err != USBD_IN_PROGRESS)
290 if (pipe->device->bus->use_polling)
291 panic("usbd_transfer: not done\n");
292 /* XXX Temporary hack XXX */
293 if (xfer->flags & USBD_NO_TSLEEP) {
295 usbd_bus_handle bus = pipe->device->bus;
296 int to = xfer->timeout * 1000;
297 for (i = 0; i < to; i += 10) {
299 bus->methods->do_poll(bus);
304 pipe->methods->abort(xfer);
306 /* XXX End hack XXX */
307 tsleep(xfer, PRIBIO, "usbsyn", 0);
310 return (xfer->status);
313 /* Like usbd_transfer(), but waits for completion. */
315 usbd_sync_transfer(usbd_xfer_handle xfer)
317 xfer->flags |= USBD_SYNCHRONOUS;
318 return (usbd_transfer(xfer));
322 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
324 struct usbd_bus *bus = xfer->device->bus;
327 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
330 xfer->rqflags |= URQ_DEV_DMABUF;
331 return (KERNADDR(&xfer->dmabuf, 0));
335 usbd_free_buffer(usbd_xfer_handle xfer)
338 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
339 printf("usbd_free_buffer: no buffer\n");
343 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
344 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
348 usbd_get_buffer(usbd_xfer_handle xfer)
350 if (!(xfer->rqflags & URQ_DEV_DMABUF))
352 return (KERNADDR(&xfer->dmabuf, 0));
356 usbd_alloc_xfer(usbd_device_handle dev)
358 usbd_xfer_handle xfer;
360 xfer = dev->bus->methods->allocx(dev->bus);
364 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
369 usbd_free_xfer(usbd_xfer_handle xfer)
371 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
372 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
373 usbd_free_buffer(xfer);
374 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
375 return (USBD_NORMAL_COMPLETION);
379 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
380 usbd_private_handle priv, void *buffer, u_int32_t length,
381 u_int16_t flags, u_int32_t timeout,
382 usbd_callback callback)
386 xfer->buffer = buffer;
387 xfer->length = length;
390 xfer->timeout = timeout;
391 xfer->status = USBD_NOT_STARTED;
392 xfer->callback = callback;
393 xfer->rqflags &= ~URQ_REQUEST;
398 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
399 usbd_private_handle priv, u_int32_t timeout,
400 usb_device_request_t *req, void *buffer,
401 u_int32_t length, u_int16_t flags,
402 usbd_callback callback)
404 xfer->pipe = dev->default_pipe;
406 xfer->buffer = buffer;
407 xfer->length = length;
410 xfer->timeout = timeout;
411 xfer->status = USBD_NOT_STARTED;
412 xfer->callback = callback;
413 xfer->request = *req;
414 xfer->rqflags |= URQ_REQUEST;
419 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
420 usbd_private_handle priv, u_int16_t *frlengths,
421 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
429 xfer->timeout = USBD_NO_TIMEOUT;
430 xfer->status = USBD_NOT_STARTED;
431 xfer->callback = callback;
432 xfer->rqflags &= ~URQ_REQUEST;
433 xfer->frlengths = frlengths;
434 xfer->nframes = nframes;
438 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
439 void **buffer, u_int32_t *count, usbd_status *status)
444 *buffer = xfer->buffer;
446 *count = xfer->actlen;
448 *status = xfer->status;
451 usb_config_descriptor_t *
452 usbd_get_config_descriptor(usbd_device_handle dev)
456 printf("usbd_get_config_descriptor: dev == NULL\n");
463 usb_interface_descriptor_t *
464 usbd_get_interface_descriptor(usbd_interface_handle iface)
468 printf("usbd_get_interface_descriptor: dev == NULL\n");
472 return (iface->idesc);
475 usb_device_descriptor_t *
476 usbd_get_device_descriptor(usbd_device_handle dev)
478 return (&dev->ddesc);
481 usb_endpoint_descriptor_t *
482 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
484 if (index >= iface->idesc->bNumEndpoints)
486 return (iface->endpoints[index].edesc);
490 usbd_abort_pipe(usbd_pipe_handle pipe)
497 printf("usbd_close_pipe: pipe==NULL\n");
498 return (USBD_NORMAL_COMPLETION);
502 err = usbd_ar_pipe(pipe);
508 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
510 usbd_device_handle dev = pipe->device;
511 usb_device_request_t req;
514 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
517 * Clearing en endpoint stall resets the enpoint toggle, so
518 * do the same to the HC toggle.
520 pipe->methods->cleartoggle(pipe);
522 req.bmRequestType = UT_WRITE_ENDPOINT;
523 req.bRequest = UR_CLEAR_FEATURE;
524 USETW(req.wValue, UF_ENDPOINT_HALT);
525 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
526 USETW(req.wLength, 0);
527 err = usbd_do_request(dev, &req, 0);
529 XXX should we do this?
531 pipe->state = USBD_PIPE_ACTIVE;
532 /* XXX activate pipe */
539 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
541 usbd_device_handle dev = pipe->device;
542 usb_device_request_t req;
545 pipe->methods->cleartoggle(pipe);
547 req.bmRequestType = UT_WRITE_ENDPOINT;
548 req.bRequest = UR_CLEAR_FEATURE;
549 USETW(req.wValue, UF_ENDPOINT_HALT);
550 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
551 USETW(req.wLength, 0);
552 err = usbd_do_request_async(dev, &req, 0);
557 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
559 pipe->methods->cleartoggle(pipe);
564 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
566 *count = iface->idesc->bNumEndpoints;
567 return (USBD_NORMAL_COMPLETION);
571 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
573 if (dev->cdesc == NULL)
574 return (USBD_NOT_CONFIGURED);
575 *count = dev->cdesc->bNumInterface;
576 return (USBD_NORMAL_COMPLETION);
580 usbd_interface2device_handle(usbd_interface_handle iface,
581 usbd_device_handle *dev)
583 *dev = iface->device;
584 return (USBD_NORMAL_COMPLETION);
588 usbd_device2interface_handle(usbd_device_handle dev,
589 u_int8_t ifaceno, usbd_interface_handle *iface)
591 if (dev->cdesc == NULL)
592 return (USBD_NOT_CONFIGURED);
593 if (ifaceno >= dev->cdesc->bNumInterface)
595 *iface = &dev->ifaces[ifaceno];
596 return (USBD_NORMAL_COMPLETION);
600 usbd_pipe2device_handle(usbd_pipe_handle pipe)
602 return (pipe->device);
607 usbd_set_interface(usbd_interface_handle iface, int altidx)
609 usb_device_request_t req;
612 if (LIST_FIRST(&iface->pipes) != 0)
613 return (USBD_IN_USE);
615 if (iface->endpoints)
616 free(iface->endpoints, M_USB);
617 iface->endpoints = 0;
620 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
624 req.bmRequestType = UT_WRITE_INTERFACE;
625 req.bRequest = UR_SET_INTERFACE;
626 USETW(req.wValue, iface->idesc->bAlternateSetting);
627 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
628 USETW(req.wLength, 0);
629 return (usbd_do_request(iface->device, &req, 0));
633 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
635 char *p = (char *)cdesc;
636 char *end = p + UGETW(cdesc->wTotalLength);
637 usb_interface_descriptor_t *d;
640 for (n = 0; p < end; p += d->bLength) {
641 d = (usb_interface_descriptor_t *)p;
642 if (p + d->bLength <= end &&
643 d->bDescriptorType == UDESC_INTERFACE &&
644 d->bInterfaceNumber == ifaceno)
651 usbd_get_interface_altindex(usbd_interface_handle iface)
653 return (iface->altindex);
657 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
659 usb_device_request_t req;
661 req.bmRequestType = UT_READ_INTERFACE;
662 req.bRequest = UR_GET_INTERFACE;
663 USETW(req.wValue, 0);
664 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
665 USETW(req.wLength, 1);
666 return (usbd_do_request(iface->device, &req, aiface));
669 /*** Internal routines ***/
671 /* Dequeue all pipe operations, called at splusb(). */
673 usbd_ar_pipe(usbd_pipe_handle pipe)
675 usbd_xfer_handle xfer;
679 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
682 usbd_dump_queue(pipe);
685 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
686 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
687 pipe, xfer, pipe->methods));
688 /* Make the HC abort it (and invoke the callback). */
689 pipe->methods->abort(xfer);
690 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
692 return (USBD_NORMAL_COMPLETION);
695 /* Called at splusb() */
697 usb_transfer_complete(usbd_xfer_handle xfer)
699 usbd_pipe_handle pipe = xfer->pipe;
700 usb_dma_t *dmap = &xfer->dmabuf;
701 int repeat = pipe->repeat;
706 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
707 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
711 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
715 polling = pipe->device->bus->use_polling;
720 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
721 usbd_xfer_isread(xfer)) {
723 if (xfer->actlen > xfer->length) {
724 printf("usb_transfer_complete: actlen > len %d > %d\n",
725 xfer->actlen, xfer->length);
726 xfer->actlen = xfer->length;
729 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
732 /* if we allocated the buffer in usbd_transfer() we free it here. */
733 if (xfer->rqflags & URQ_AUTO_DMABUF) {
735 struct usbd_bus *bus = pipe->device->bus;
736 bus->methods->freem(bus, dmap);
737 xfer->rqflags &= ~URQ_AUTO_DMABUF;
741 if (pipe->methods->done != NULL)
742 pipe->methods->done(xfer);
745 /* Remove request from queue. */
747 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
748 printf("usb_transfer_complete: bad dequeue %p != %p\n",
749 xfer, SIMPLEQ_FIRST(&pipe->queue));
751 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
754 /* Count completed transfers. */
755 ++pipe->device->bus->stats.uds_requests
756 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
759 if (!xfer->status && xfer->actlen < xfer->length &&
760 !(xfer->flags & USBD_SHORT_XFER_OK)) {
761 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
762 xfer->actlen, xfer->length));
763 xfer->status = USBD_SHORT_XFER;
767 xfer->callback(xfer, xfer->priv, xfer->status);
769 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
773 /* XXX should we stop the queue on all errors? */
774 if ((xfer->status == USBD_CANCELLED
775 || xfer->status == USBD_TIMEOUT)
776 && pipe->iface != NULL) /* not control pipe */
779 usbd_start_next(pipe);
784 usb_insert_transfer(usbd_xfer_handle xfer)
786 usbd_pipe_handle pipe = xfer->pipe;
790 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
791 pipe, pipe->running, xfer->timeout));
793 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
795 err = USBD_IN_PROGRESS;
798 err = USBD_NORMAL_COMPLETION;
804 /* Called at splusb() */
806 usbd_start_next(usbd_pipe_handle pipe)
808 usbd_xfer_handle xfer;
815 printf("usbd_start_next: pipe == NULL\n");
818 if (pipe->methods == NULL || pipe->methods->start == NULL) {
819 printf("usbd_start_next: pipe=%p no start method\n", pipe);
824 /* Get next request in queue. */
825 xfer = SIMPLEQ_FIRST(&pipe->queue);
826 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
830 err = pipe->methods->start(xfer);
831 if (err != USBD_IN_PROGRESS) {
832 printf("usbd_start_next: error=%d\n", err);
840 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
842 return (usbd_do_request_flags(dev, req, data, 0, 0));
846 usbd_do_request_flags(usbd_device_handle dev,
847 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
849 usbd_xfer_handle xfer;
853 #if defined(__i386__) && defined(__FreeBSD__)
854 KASSERT(intr_nesting_level == 0,
855 ("usbd_do_request: in interrupt context"));
857 if (dev->bus->intr_context) {
858 printf("usbd_do_request: not in process context\n");
863 xfer = usbd_alloc_xfer(dev);
866 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
867 data, UGETW(req->wLength), flags, 0);
868 err = usbd_sync_transfer(xfer);
869 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
870 if (xfer->actlen > xfer->length)
871 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
872 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
873 dev->address, xfer->request.bmRequestType,
874 xfer->request.bRequest, UGETW(xfer->request.wValue),
875 UGETW(xfer->request.wIndex),
876 UGETW(xfer->request.wLength),
877 xfer->length, xfer->actlen));
880 *actlen = xfer->actlen;
881 if (err == USBD_STALLED) {
883 * The control endpoint has stalled. Control endpoints
884 * should not halt, but some may do so anyway so clear
885 * any halt condition.
887 usb_device_request_t treq;
892 treq.bmRequestType = UT_READ_ENDPOINT;
893 treq.bRequest = UR_GET_STATUS;
894 USETW(treq.wValue, 0);
895 USETW(treq.wIndex, 0);
896 USETW(treq.wLength, sizeof(usb_status_t));
897 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
898 &treq, &status,sizeof(usb_status_t),
900 nerr = usbd_sync_transfer(xfer);
903 s = UGETW(status.wStatus);
904 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
907 treq.bmRequestType = UT_WRITE_ENDPOINT;
908 treq.bRequest = UR_CLEAR_FEATURE;
909 USETW(treq.wValue, UF_ENDPOINT_HALT);
910 USETW(treq.wIndex, 0);
911 USETW(treq.wLength, 0);
912 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
913 &treq, &status, 0, 0, 0);
914 nerr = usbd_sync_transfer(xfer);
920 usbd_free_xfer(xfer);
925 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
928 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
929 if (xfer->actlen > xfer->length)
930 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
931 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
932 xfer->pipe->device->address,
933 xfer->request.bmRequestType,
934 xfer->request.bRequest, UGETW(xfer->request.wValue),
935 UGETW(xfer->request.wIndex),
936 UGETW(xfer->request.wLength),
937 xfer->length, xfer->actlen));
939 usbd_free_xfer(xfer);
943 * Execute a request without waiting for completion.
944 * Can be used from interrupt context.
947 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
950 usbd_xfer_handle xfer;
953 xfer = usbd_alloc_xfer(dev);
956 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
957 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
958 err = usbd_transfer(xfer);
959 if (err != USBD_IN_PROGRESS) {
960 usbd_free_xfer(xfer);
963 return (USBD_NORMAL_COMPLETION);
967 usbd_get_quirks(usbd_device_handle dev)
969 return (dev->quirks);
972 /* XXX do periodic free() of free list */
975 * Called from keyboard driver when in polling mode.
978 usbd_dopoll(usbd_interface_handle iface)
980 iface->device->bus->methods->do_poll(iface->device->bus);
984 usbd_set_polling(usbd_interface_handle iface, int on)
987 iface->device->bus->use_polling++;
989 iface->device->bus->use_polling--;
993 usb_endpoint_descriptor_t *
994 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
996 struct usbd_endpoint *ep;
999 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1000 ep = &iface->endpoints[i];
1001 if (ep->edesc->bEndpointAddress == address)
1002 return (iface->endpoints[i].edesc);
1008 * Search for a vendor/product pair in an array. The item size is
1009 * given as an argument.
1011 const struct usb_devno *
1012 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1013 u_int16_t vendor, u_int16_t product)
1015 while (nentries-- > 0) {
1016 if (tbl->ud_vendor == vendor && tbl->ud_product == product)
1018 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1023 #if defined(__FreeBSD__)
1025 usbd_driver_load(module_t mod, int what, void *arg)
1027 /* XXX should implement something like a function that removes all generic devices */