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 $ */
3 /* $DragonFly: src/sys/bus/usb/usbdi.c,v 1.4 2003/07/19 21:14:30 dillon Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #if defined(__NetBSD__) || defined(__OpenBSD__)
45 #include <sys/device.h>
46 #elif defined(__FreeBSD__)
47 #include <sys/module.h>
50 #if defined(DIAGNOSTIC) && defined(__i386__)
51 #include <machine/cpu.h>
54 #include <sys/malloc.h>
57 #include <machine/bus.h>
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdivar.h>
63 #include <dev/usb/usb_mem.h>
65 #if defined(__FreeBSD__)
67 #include <machine/clock.h>
68 #define delay(d) DELAY(d)
72 #define DPRINTF(x) if (usbdebug) logprintf x
73 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
80 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
81 Static void usbd_do_request_async_cb
82 (usbd_xfer_handle, usbd_private_handle, usbd_status);
83 Static void usbd_start_next(usbd_pipe_handle pipe);
84 Static usbd_status usbd_open_pipe_ival
85 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
87 Static int usbd_nbuses = 0;
101 Static __inline int usbd_xfer_isread(usbd_xfer_handle xfer);
103 usbd_xfer_isread(xfer)
104 usbd_xfer_handle xfer;
106 if (xfer->rqflags & URQ_REQUEST)
107 return (xfer->request.bmRequestType & UT_READ);
109 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
115 usbd_dump_queue(usbd_pipe_handle);
118 usbd_dump_queue(usbd_pipe_handle pipe)
120 usbd_xfer_handle xfer;
122 printf("usbd_dump_queue: pipe=%p\n", pipe);
123 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
125 xfer = SIMPLEQ_NEXT(xfer, next)) {
126 printf(" xfer=%p\n", xfer);
132 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
133 u_int8_t flags, usbd_pipe_handle *pipe)
135 return (usbd_open_pipe_ival(iface, address, flags, pipe,
136 USBD_DEFAULT_INTERVAL));
140 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
141 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
144 struct usbd_endpoint *ep;
148 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
149 iface, address, flags));
151 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
152 ep = &iface->endpoints[i];
153 if (ep->edesc == NULL)
154 return (USBD_IOERROR);
155 if (ep->edesc->bEndpointAddress == address)
158 return (USBD_BAD_ADDRESS);
160 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
161 return (USBD_IN_USE);
162 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
165 LIST_INSERT_HEAD(&iface->pipes, p, next);
167 return (USBD_NORMAL_COMPLETION);
171 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
172 u_int8_t flags, usbd_pipe_handle *pipe,
173 usbd_private_handle priv, void *buffer, u_int32_t len,
174 usbd_callback cb, int ival)
177 usbd_xfer_handle xfer;
178 usbd_pipe_handle ipipe;
180 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
181 address, flags, len));
183 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
187 xfer = usbd_alloc_xfer(iface->device);
192 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
193 USBD_NO_TIMEOUT, cb);
194 ipipe->intrxfer = xfer;
196 err = usbd_transfer(xfer);
198 if (err != USBD_IN_PROGRESS)
200 return (USBD_NORMAL_COMPLETION);
203 ipipe->intrxfer = NULL;
205 usbd_free_xfer(xfer);
207 usbd_close_pipe(ipipe);
212 usbd_close_pipe(usbd_pipe_handle pipe)
216 printf("usbd_close_pipe: pipe==NULL\n");
217 return (USBD_NORMAL_COMPLETION);
221 if (--pipe->refcnt != 0)
222 return (USBD_NORMAL_COMPLETION);
223 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
224 return (USBD_PENDING_REQUESTS);
225 LIST_REMOVE(pipe, next);
226 pipe->endpoint->refcnt--;
227 pipe->methods->close(pipe);
228 if (pipe->intrxfer != NULL)
229 usbd_free_xfer(pipe->intrxfer);
231 return (USBD_NORMAL_COMPLETION);
235 usbd_transfer(usbd_xfer_handle xfer)
237 usbd_pipe_handle pipe = xfer->pipe;
238 usb_dma_t *dmap = &xfer->dmabuf;
243 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
244 xfer, xfer->flags, pipe, pipe->running));
247 usbd_dump_queue(pipe);
252 /* If there is no buffer, allocate one. */
253 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
254 struct usbd_bus *bus = pipe->device->bus;
257 if (xfer->rqflags & URQ_AUTO_DMABUF)
258 printf("usbd_transfer: has old buffer!\n");
260 err = bus->methods->allocm(bus, dmap, size);
263 xfer->rqflags |= URQ_AUTO_DMABUF;
266 /* Copy data if going out. */
267 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
268 !usbd_xfer_isread(xfer))
269 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
271 err = pipe->methods->transfer(xfer);
273 if (err != USBD_IN_PROGRESS && err) {
274 /* The transfer has not been queued, so free buffer. */
275 if (xfer->rqflags & URQ_AUTO_DMABUF) {
276 struct usbd_bus *bus = pipe->device->bus;
278 bus->methods->freem(bus, &xfer->dmabuf);
279 xfer->rqflags &= ~URQ_AUTO_DMABUF;
283 if (!(xfer->flags & USBD_SYNCHRONOUS))
286 /* Sync transfer, wait for completion. */
287 if (err != USBD_IN_PROGRESS)
291 if (pipe->device->bus->use_polling)
292 panic("usbd_transfer: not done\n");
293 /* XXX Temporary hack XXX */
294 if (xfer->flags & USBD_NO_TSLEEP) {
296 usbd_bus_handle bus = pipe->device->bus;
297 int to = xfer->timeout * 1000;
298 for (i = 0; i < to; i += 10) {
300 bus->methods->do_poll(bus);
305 pipe->methods->abort(xfer);
307 /* XXX End hack XXX */
308 tsleep(xfer, 0, "usbsyn", 0);
311 return (xfer->status);
314 /* Like usbd_transfer(), but waits for completion. */
316 usbd_sync_transfer(usbd_xfer_handle xfer)
318 xfer->flags |= USBD_SYNCHRONOUS;
319 return (usbd_transfer(xfer));
323 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
325 struct usbd_bus *bus = xfer->device->bus;
328 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
331 xfer->rqflags |= URQ_DEV_DMABUF;
332 return (KERNADDR(&xfer->dmabuf, 0));
336 usbd_free_buffer(usbd_xfer_handle xfer)
339 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
340 printf("usbd_free_buffer: no buffer\n");
344 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
345 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
349 usbd_get_buffer(usbd_xfer_handle xfer)
351 if (!(xfer->rqflags & URQ_DEV_DMABUF))
353 return (KERNADDR(&xfer->dmabuf, 0));
357 usbd_alloc_xfer(usbd_device_handle dev)
359 usbd_xfer_handle xfer;
361 xfer = dev->bus->methods->allocx(dev->bus);
365 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
370 usbd_free_xfer(usbd_xfer_handle xfer)
372 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
373 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
374 usbd_free_buffer(xfer);
375 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
376 return (USBD_NORMAL_COMPLETION);
380 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
381 usbd_private_handle priv, void *buffer, u_int32_t length,
382 u_int16_t flags, u_int32_t timeout,
383 usbd_callback callback)
387 xfer->buffer = buffer;
388 xfer->length = length;
391 xfer->timeout = timeout;
392 xfer->status = USBD_NOT_STARTED;
393 xfer->callback = callback;
394 xfer->rqflags &= ~URQ_REQUEST;
399 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
400 usbd_private_handle priv, u_int32_t timeout,
401 usb_device_request_t *req, void *buffer,
402 u_int32_t length, u_int16_t flags,
403 usbd_callback callback)
405 xfer->pipe = dev->default_pipe;
407 xfer->buffer = buffer;
408 xfer->length = length;
411 xfer->timeout = timeout;
412 xfer->status = USBD_NOT_STARTED;
413 xfer->callback = callback;
414 xfer->request = *req;
415 xfer->rqflags |= URQ_REQUEST;
420 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
421 usbd_private_handle priv, u_int16_t *frlengths,
422 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
430 xfer->timeout = USBD_NO_TIMEOUT;
431 xfer->status = USBD_NOT_STARTED;
432 xfer->callback = callback;
433 xfer->rqflags &= ~URQ_REQUEST;
434 xfer->frlengths = frlengths;
435 xfer->nframes = nframes;
439 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
440 void **buffer, u_int32_t *count, usbd_status *status)
445 *buffer = xfer->buffer;
447 *count = xfer->actlen;
449 *status = xfer->status;
452 usb_config_descriptor_t *
453 usbd_get_config_descriptor(usbd_device_handle dev)
457 printf("usbd_get_config_descriptor: dev == NULL\n");
464 usb_interface_descriptor_t *
465 usbd_get_interface_descriptor(usbd_interface_handle iface)
469 printf("usbd_get_interface_descriptor: dev == NULL\n");
473 return (iface->idesc);
476 usb_device_descriptor_t *
477 usbd_get_device_descriptor(usbd_device_handle dev)
479 return (&dev->ddesc);
482 usb_endpoint_descriptor_t *
483 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
485 if (index >= iface->idesc->bNumEndpoints)
487 return (iface->endpoints[index].edesc);
491 usbd_abort_pipe(usbd_pipe_handle pipe)
498 printf("usbd_close_pipe: pipe==NULL\n");
499 return (USBD_NORMAL_COMPLETION);
503 err = usbd_ar_pipe(pipe);
509 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
511 usbd_device_handle dev = pipe->device;
512 usb_device_request_t req;
515 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
518 * Clearing en endpoint stall resets the enpoint toggle, so
519 * do the same to the HC toggle.
521 pipe->methods->cleartoggle(pipe);
523 req.bmRequestType = UT_WRITE_ENDPOINT;
524 req.bRequest = UR_CLEAR_FEATURE;
525 USETW(req.wValue, UF_ENDPOINT_HALT);
526 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
527 USETW(req.wLength, 0);
528 err = usbd_do_request(dev, &req, 0);
530 XXX should we do this?
532 pipe->state = USBD_PIPE_ACTIVE;
533 /* XXX activate pipe */
540 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
542 usbd_device_handle dev = pipe->device;
543 usb_device_request_t req;
546 pipe->methods->cleartoggle(pipe);
548 req.bmRequestType = UT_WRITE_ENDPOINT;
549 req.bRequest = UR_CLEAR_FEATURE;
550 USETW(req.wValue, UF_ENDPOINT_HALT);
551 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
552 USETW(req.wLength, 0);
553 err = usbd_do_request_async(dev, &req, 0);
558 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
560 pipe->methods->cleartoggle(pipe);
565 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
567 *count = iface->idesc->bNumEndpoints;
568 return (USBD_NORMAL_COMPLETION);
572 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
574 if (dev->cdesc == NULL)
575 return (USBD_NOT_CONFIGURED);
576 *count = dev->cdesc->bNumInterface;
577 return (USBD_NORMAL_COMPLETION);
581 usbd_interface2device_handle(usbd_interface_handle iface,
582 usbd_device_handle *dev)
584 *dev = iface->device;
585 return (USBD_NORMAL_COMPLETION);
589 usbd_device2interface_handle(usbd_device_handle dev,
590 u_int8_t ifaceno, usbd_interface_handle *iface)
592 if (dev->cdesc == NULL)
593 return (USBD_NOT_CONFIGURED);
594 if (ifaceno >= dev->cdesc->bNumInterface)
596 *iface = &dev->ifaces[ifaceno];
597 return (USBD_NORMAL_COMPLETION);
601 usbd_pipe2device_handle(usbd_pipe_handle pipe)
603 return (pipe->device);
608 usbd_set_interface(usbd_interface_handle iface, int altidx)
610 usb_device_request_t req;
613 if (LIST_FIRST(&iface->pipes) != 0)
614 return (USBD_IN_USE);
616 if (iface->endpoints)
617 free(iface->endpoints, M_USB);
618 iface->endpoints = 0;
621 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
625 req.bmRequestType = UT_WRITE_INTERFACE;
626 req.bRequest = UR_SET_INTERFACE;
627 USETW(req.wValue, iface->idesc->bAlternateSetting);
628 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
629 USETW(req.wLength, 0);
630 return (usbd_do_request(iface->device, &req, 0));
634 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
636 char *p = (char *)cdesc;
637 char *end = p + UGETW(cdesc->wTotalLength);
638 usb_interface_descriptor_t *d;
641 for (n = 0; p < end; p += d->bLength) {
642 d = (usb_interface_descriptor_t *)p;
643 if (p + d->bLength <= end &&
644 d->bDescriptorType == UDESC_INTERFACE &&
645 d->bInterfaceNumber == ifaceno)
652 usbd_get_interface_altindex(usbd_interface_handle iface)
654 return (iface->altindex);
658 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
660 usb_device_request_t req;
662 req.bmRequestType = UT_READ_INTERFACE;
663 req.bRequest = UR_GET_INTERFACE;
664 USETW(req.wValue, 0);
665 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
666 USETW(req.wLength, 1);
667 return (usbd_do_request(iface->device, &req, aiface));
670 /*** Internal routines ***/
672 /* Dequeue all pipe operations, called at splusb(). */
674 usbd_ar_pipe(usbd_pipe_handle pipe)
676 usbd_xfer_handle xfer;
680 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
683 usbd_dump_queue(pipe);
686 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
687 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
688 pipe, xfer, pipe->methods));
689 /* Make the HC abort it (and invoke the callback). */
690 pipe->methods->abort(xfer);
691 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
693 return (USBD_NORMAL_COMPLETION);
696 /* Called at splusb() */
698 usb_transfer_complete(usbd_xfer_handle xfer)
700 usbd_pipe_handle pipe = xfer->pipe;
701 usb_dma_t *dmap = &xfer->dmabuf;
702 int repeat = pipe->repeat;
707 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
708 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
712 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
716 polling = pipe->device->bus->use_polling;
721 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
722 usbd_xfer_isread(xfer)) {
724 if (xfer->actlen > xfer->length) {
725 printf("usb_transfer_complete: actlen > len %d > %d\n",
726 xfer->actlen, xfer->length);
727 xfer->actlen = xfer->length;
730 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
733 /* if we allocated the buffer in usbd_transfer() we free it here. */
734 if (xfer->rqflags & URQ_AUTO_DMABUF) {
736 struct usbd_bus *bus = pipe->device->bus;
737 bus->methods->freem(bus, dmap);
738 xfer->rqflags &= ~URQ_AUTO_DMABUF;
742 if (pipe->methods->done != NULL)
743 pipe->methods->done(xfer);
746 /* Remove request from queue. */
748 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
749 printf("usb_transfer_complete: bad dequeue %p != %p\n",
750 xfer, SIMPLEQ_FIRST(&pipe->queue));
752 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
755 /* Count completed transfers. */
756 ++pipe->device->bus->stats.uds_requests
757 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
760 if (!xfer->status && xfer->actlen < xfer->length &&
761 !(xfer->flags & USBD_SHORT_XFER_OK)) {
762 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
763 xfer->actlen, xfer->length));
764 xfer->status = USBD_SHORT_XFER;
768 xfer->callback(xfer, xfer->priv, xfer->status);
770 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
774 /* XXX should we stop the queue on all errors? */
775 if ((xfer->status == USBD_CANCELLED
776 || xfer->status == USBD_TIMEOUT)
777 && pipe->iface != NULL) /* not control pipe */
780 usbd_start_next(pipe);
785 usb_insert_transfer(usbd_xfer_handle xfer)
787 usbd_pipe_handle pipe = xfer->pipe;
791 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
792 pipe, pipe->running, xfer->timeout));
794 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
796 err = USBD_IN_PROGRESS;
799 err = USBD_NORMAL_COMPLETION;
805 /* Called at splusb() */
807 usbd_start_next(usbd_pipe_handle pipe)
809 usbd_xfer_handle xfer;
816 printf("usbd_start_next: pipe == NULL\n");
819 if (pipe->methods == NULL || pipe->methods->start == NULL) {
820 printf("usbd_start_next: pipe=%p no start method\n", pipe);
825 /* Get next request in queue. */
826 xfer = SIMPLEQ_FIRST(&pipe->queue);
827 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
831 err = pipe->methods->start(xfer);
832 if (err != USBD_IN_PROGRESS) {
833 printf("usbd_start_next: error=%d\n", err);
841 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
843 return (usbd_do_request_flags(dev, req, data, 0, 0));
847 usbd_do_request_flags(usbd_device_handle dev,
848 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
850 usbd_xfer_handle xfer;
854 #if defined(__FreeBSD__)
855 KASSERT(mycpu->gd_intr_nesting_level == 0,
856 ("usbd_do_request: in interrupt context"));
858 if (dev->bus->intr_context) {
859 printf("usbd_do_request: not in process context\n");
864 xfer = usbd_alloc_xfer(dev);
867 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
868 data, UGETW(req->wLength), flags, 0);
869 err = usbd_sync_transfer(xfer);
870 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
871 if (xfer->actlen > xfer->length)
872 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
873 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
874 dev->address, xfer->request.bmRequestType,
875 xfer->request.bRequest, UGETW(xfer->request.wValue),
876 UGETW(xfer->request.wIndex),
877 UGETW(xfer->request.wLength),
878 xfer->length, xfer->actlen));
881 *actlen = xfer->actlen;
882 if (err == USBD_STALLED) {
884 * The control endpoint has stalled. Control endpoints
885 * should not halt, but some may do so anyway so clear
886 * any halt condition.
888 usb_device_request_t treq;
893 treq.bmRequestType = UT_READ_ENDPOINT;
894 treq.bRequest = UR_GET_STATUS;
895 USETW(treq.wValue, 0);
896 USETW(treq.wIndex, 0);
897 USETW(treq.wLength, sizeof(usb_status_t));
898 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
899 &treq, &status,sizeof(usb_status_t),
901 nerr = usbd_sync_transfer(xfer);
904 s = UGETW(status.wStatus);
905 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
908 treq.bmRequestType = UT_WRITE_ENDPOINT;
909 treq.bRequest = UR_CLEAR_FEATURE;
910 USETW(treq.wValue, UF_ENDPOINT_HALT);
911 USETW(treq.wIndex, 0);
912 USETW(treq.wLength, 0);
913 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
914 &treq, &status, 0, 0, 0);
915 nerr = usbd_sync_transfer(xfer);
921 usbd_free_xfer(xfer);
926 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
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 xfer->pipe->device->address,
934 xfer->request.bmRequestType,
935 xfer->request.bRequest, UGETW(xfer->request.wValue),
936 UGETW(xfer->request.wIndex),
937 UGETW(xfer->request.wLength),
938 xfer->length, xfer->actlen));
940 usbd_free_xfer(xfer);
944 * Execute a request without waiting for completion.
945 * Can be used from interrupt context.
948 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
951 usbd_xfer_handle xfer;
954 xfer = usbd_alloc_xfer(dev);
957 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
958 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
959 err = usbd_transfer(xfer);
960 if (err != USBD_IN_PROGRESS) {
961 usbd_free_xfer(xfer);
964 return (USBD_NORMAL_COMPLETION);
968 usbd_get_quirks(usbd_device_handle dev)
970 return (dev->quirks);
973 /* XXX do periodic free() of free list */
976 * Called from keyboard driver when in polling mode.
979 usbd_dopoll(usbd_interface_handle iface)
981 iface->device->bus->methods->do_poll(iface->device->bus);
985 usbd_set_polling(usbd_interface_handle iface, int on)
988 iface->device->bus->use_polling++;
990 iface->device->bus->use_polling--;
994 usb_endpoint_descriptor_t *
995 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
997 struct usbd_endpoint *ep;
1000 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1001 ep = &iface->endpoints[i];
1002 if (ep->edesc->bEndpointAddress == address)
1003 return (iface->endpoints[i].edesc);
1009 * Search for a vendor/product pair in an array. The item size is
1010 * given as an argument.
1012 const struct usb_devno *
1013 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1014 u_int16_t vendor, u_int16_t product)
1016 while (nentries-- > 0) {
1017 if (tbl->ud_vendor == vendor && tbl->ud_product == product)
1019 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1024 #if defined(__FreeBSD__)
1026 usbd_driver_load(module_t mod, int what, void *arg)
1028 /* XXX should implement something like a function that removes all generic devices */