1 /* $NetBSD: usbdi.c,v 1.47 1999/10/13 23:46:10 augustss 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 (augustss@carlstedt.se) 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/kernel.h>
45 #include <sys/device.h>
46 #elif defined(__FreeBSD__)
47 #include <sys/module.h>
51 #if defined(DIAGNOSTIC) && defined(__i386__)
52 #include <machine/cpu.h>
55 #include <sys/malloc.h>
58 #include <machine/bus.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
64 #include <dev/usb/usb_mem.h>
66 #if defined(__FreeBSD__)
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 __P((usbd_pipe_handle pipe));
80 static void usbd_do_request_async_cb
81 __P((usbd_xfer_handle, usbd_private_handle, usbd_status));
82 static void usbd_start_next __P((usbd_pipe_handle pipe));
84 static SIMPLEQ_HEAD(, usbd_xfer) usbd_free_requests =
85 SIMPLEQ_HEAD_INITIALIZER(usbd_free_requests);
87 static int usbd_nbuses = 0;
98 usbd_xfer_handle xfer;
100 if (--usbd_nbuses == 0) {
101 /* Last controller is gone, free all requests. */
103 xfer = SIMPLEQ_FIRST(&usbd_free_requests);
106 SIMPLEQ_REMOVE_HEAD(&usbd_free_requests, xfer, next);
112 static __inline int usbd_xfer_isread __P((usbd_xfer_handle xfer));
114 usbd_xfer_isread(xfer)
115 usbd_xfer_handle xfer;
117 if (xfer->rqflags & URQ_REQUEST)
118 return (xfer->request.bmRequestType & UT_READ);
120 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
125 void usbd_dump_queue __P((usbd_pipe_handle));
128 usbd_dump_queue(pipe)
129 usbd_pipe_handle pipe;
131 usbd_xfer_handle xfer;
133 printf("usbd_dump_queue: pipe=%p\n", pipe);
134 for (xfer = SIMPLEQ_FIRST(&pipe->queue);
136 xfer = SIMPLEQ_NEXT(xfer, next)) {
137 printf(" xfer=%p\n", xfer);
143 usbd_open_pipe(iface, address, flags, pipe)
144 usbd_interface_handle iface;
147 usbd_pipe_handle *pipe;
150 struct usbd_endpoint *ep;
154 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
155 ep = &iface->endpoints[i];
156 if (ep->edesc->bEndpointAddress == address)
159 return (USBD_BAD_ADDRESS);
161 if ((flags & USBD_EXCLUSIVE_USE) &&
163 return (USBD_IN_USE);
164 err = usbd_setup_pipe(iface->device, iface, ep, &p);
167 LIST_INSERT_HEAD(&iface->pipes, p, next);
169 return (USBD_NORMAL_COMPLETION);
173 usbd_open_pipe_intr(iface, address, flags, pipe, priv, buffer, length, cb)
174 usbd_interface_handle iface;
177 usbd_pipe_handle *pipe;
178 usbd_private_handle priv;
184 usbd_xfer_handle xfer;
185 usbd_pipe_handle ipipe;
187 err = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &ipipe);
190 xfer = usbd_alloc_request(iface->device);
195 usbd_setup_request(xfer, ipipe, priv, buffer, length, flags,
196 USBD_NO_TIMEOUT, cb);
197 ipipe->intrxfer = xfer;
199 err = usbd_transfer(xfer);
201 if (err != USBD_IN_PROGRESS)
203 return (USBD_NORMAL_COMPLETION);
206 ipipe->intrxfer = NULL;
208 usbd_free_request(xfer);
210 usbd_close_pipe(ipipe);
215 usbd_close_pipe(pipe)
216 usbd_pipe_handle pipe;
220 printf("usbd_close_pipe: pipe==NULL\n");
221 return (USBD_NORMAL_COMPLETION);
225 if (--pipe->refcnt != 0)
226 return (USBD_NORMAL_COMPLETION);
227 if (SIMPLEQ_FIRST(&pipe->queue) != 0)
228 return (USBD_PENDING_REQUESTS);
229 LIST_REMOVE(pipe, next);
230 pipe->endpoint->refcnt--;
231 pipe->methods->close(pipe);
232 if (pipe->intrxfer != NULL)
233 usbd_free_request(pipe->intrxfer);
235 return (USBD_NORMAL_COMPLETION);
240 usbd_xfer_handle xfer;
242 usbd_pipe_handle pipe = xfer->pipe;
243 usb_dma_t *dmap = &xfer->dmabuf;
248 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
249 xfer, xfer->flags, pipe, pipe->running));
252 usbd_dump_queue(pipe);
257 /* If there is no buffer, allocate one. */
258 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
259 struct usbd_bus *bus = pipe->device->bus;
262 if (xfer->rqflags & URQ_AUTO_DMABUF)
263 printf("usbd_transfer: has old buffer!\n");
265 err = bus->methods->allocm(bus, dmap, size);
268 xfer->rqflags |= URQ_AUTO_DMABUF;
271 /* Copy data if going out. */
272 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
273 !usbd_xfer_isread(xfer))
274 memcpy(KERNADDR(dmap), xfer->buffer, size);
276 err = pipe->methods->transfer(xfer);
278 if (err != USBD_IN_PROGRESS && err) {
279 /* The transfer has not been queued, so free buffer. */
280 if (xfer->rqflags & URQ_AUTO_DMABUF) {
281 struct usbd_bus *bus = pipe->device->bus;
283 bus->methods->freem(bus, &xfer->dmabuf);
284 xfer->rqflags &= ~URQ_AUTO_DMABUF;
288 if (!(xfer->flags & USBD_SYNCHRONOUS))
291 /* Sync transfer, wait for completion. */
292 if (err != USBD_IN_PROGRESS)
296 if (pipe->device->bus->use_polling)
297 panic("usbd_transfer: not done\n");
298 tsleep(xfer, PRIBIO, "usbsyn", 0);
301 return (xfer->status);
304 /* Like usbd_transfer(), but waits for completion. */
306 usbd_sync_transfer(xfer)
307 usbd_xfer_handle xfer;
309 xfer->flags |= USBD_SYNCHRONOUS;
310 return (usbd_transfer(xfer));
314 usbd_alloc_buffer(xfer, size)
315 usbd_xfer_handle xfer;
318 struct usbd_bus *bus = xfer->device->bus;
321 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
324 xfer->rqflags |= URQ_DEV_DMABUF;
325 return (KERNADDR(&xfer->dmabuf));
329 usbd_free_buffer(xfer)
330 usbd_xfer_handle xfer;
333 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
334 printf("usbd_free_buffer: no buffer\n");
338 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
339 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
343 usbd_get_buffer(xfer)
344 usbd_xfer_handle xfer;
346 if (!(xfer->rqflags & URQ_DEV_DMABUF))
348 return (KERNADDR(&xfer->dmabuf));
352 usbd_alloc_request(dev)
353 usbd_device_handle dev;
355 usbd_xfer_handle xfer;
357 xfer = SIMPLEQ_FIRST(&usbd_free_requests);
359 SIMPLEQ_REMOVE_HEAD(&usbd_free_requests, xfer, next);
361 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
364 memset(xfer, 0, sizeof *xfer);
366 DPRINTFN(5,("usbd_alloc_request() = %p\n", xfer));
371 usbd_free_request(xfer)
372 usbd_xfer_handle xfer;
374 DPRINTFN(5,("usbd_free_request: %p\n", xfer));
375 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
376 usbd_free_buffer(xfer);
377 SIMPLEQ_INSERT_HEAD(&usbd_free_requests, xfer, next);
378 return (USBD_NORMAL_COMPLETION);
382 usbd_setup_request(xfer, pipe, priv, buffer, length, flags, timeout, callback)
383 usbd_xfer_handle xfer;
384 usbd_pipe_handle pipe;
385 usbd_private_handle priv;
390 void (*callback) __P((usbd_xfer_handle,
396 xfer->buffer = buffer;
397 xfer->length = length;
400 xfer->timeout = timeout;
401 xfer->status = USBD_NOT_STARTED;
402 xfer->callback = callback;
403 xfer->rqflags &= ~URQ_REQUEST;
408 usbd_setup_default_request(xfer, dev, priv, timeout, req, buffer,
409 length, flags, callback)
410 usbd_xfer_handle xfer;
411 usbd_device_handle dev;
412 usbd_private_handle priv;
414 usb_device_request_t *req;
418 void (*callback) __P((usbd_xfer_handle,
422 xfer->pipe = dev->default_pipe;
424 xfer->buffer = buffer;
425 xfer->length = length;
428 xfer->timeout = timeout;
429 xfer->status = USBD_NOT_STARTED;
430 xfer->callback = callback;
431 xfer->request = *req;
432 xfer->rqflags |= URQ_REQUEST;
437 usbd_setup_isoc_request(xfer, pipe, priv, frlengths, nframes, flags, callback)
438 usbd_xfer_handle xfer;
439 usbd_pipe_handle pipe;
440 usbd_private_handle priv;
441 u_int16_t *frlengths;
444 usbd_callback callback;
452 xfer->timeout = USBD_NO_TIMEOUT;
453 xfer->status = USBD_NOT_STARTED;
454 xfer->callback = callback;
455 xfer->rqflags &= ~URQ_REQUEST;
456 xfer->frlengths = frlengths;
457 xfer->nframes = nframes;
461 usbd_get_request_status(xfer, priv, buffer, count, status)
462 usbd_xfer_handle xfer;
463 usbd_private_handle *priv;
471 *buffer = xfer->buffer;
473 *count = xfer->actlen;
475 *status = xfer->status;
478 usb_config_descriptor_t *
479 usbd_get_config_descriptor(dev)
480 usbd_device_handle dev;
485 usb_interface_descriptor_t *
486 usbd_get_interface_descriptor(iface)
487 usbd_interface_handle iface;
489 return (iface->idesc);
492 usb_device_descriptor_t *
493 usbd_get_device_descriptor(dev)
494 usbd_device_handle dev;
496 return (&dev->ddesc);
499 usb_endpoint_descriptor_t *
500 usbd_interface2endpoint_descriptor(iface, index)
501 usbd_interface_handle iface;
504 if (index >= iface->idesc->bNumEndpoints)
506 return (iface->endpoints[index].edesc);
510 usbd_abort_pipe(pipe)
511 usbd_pipe_handle pipe;
518 printf("usbd_close_pipe: pipe==NULL\n");
519 return (USBD_NORMAL_COMPLETION);
523 err = usbd_ar_pipe(pipe);
529 usbd_clear_endpoint_stall(pipe)
530 usbd_pipe_handle pipe;
532 usbd_device_handle dev = pipe->device;
533 usb_device_request_t req;
536 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
539 * Clearing en endpoint stall resets the enpoint toggle, so
540 * do the same to the HC toggle.
542 pipe->methods->cleartoggle(pipe);
544 req.bmRequestType = UT_WRITE_ENDPOINT;
545 req.bRequest = UR_CLEAR_FEATURE;
546 USETW(req.wValue, UF_ENDPOINT_HALT);
547 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
548 USETW(req.wLength, 0);
549 err = usbd_do_request(dev, &req, 0);
551 XXX should we do this?
553 pipe->state = USBD_PIPE_ACTIVE;
554 /* XXX activate pipe */
561 usbd_clear_endpoint_stall_async(pipe)
562 usbd_pipe_handle pipe;
564 usbd_device_handle dev = pipe->device;
565 usb_device_request_t req;
568 pipe->methods->cleartoggle(pipe);
570 req.bmRequestType = UT_WRITE_ENDPOINT;
571 req.bRequest = UR_CLEAR_FEATURE;
572 USETW(req.wValue, UF_ENDPOINT_HALT);
573 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
574 USETW(req.wLength, 0);
575 err = usbd_do_request_async(dev, &req, 0);
580 usbd_endpoint_count(iface, count)
581 usbd_interface_handle iface;
584 *count = iface->idesc->bNumEndpoints;
585 return (USBD_NORMAL_COMPLETION);
589 usbd_interface_count(dev, count)
590 usbd_device_handle dev;
593 if (dev->cdesc == NULL)
594 return (USBD_NOT_CONFIGURED);
595 *count = dev->cdesc->bNumInterface;
596 return (USBD_NORMAL_COMPLETION);
600 usbd_interface2device_handle(iface, dev)
601 usbd_interface_handle iface;
602 usbd_device_handle *dev;
604 *dev = iface->device;
605 return (USBD_NORMAL_COMPLETION);
609 usbd_device2interface_handle(dev, ifaceno, iface)
610 usbd_device_handle dev;
612 usbd_interface_handle *iface;
614 if (dev->cdesc == NULL)
615 return (USBD_NOT_CONFIGURED);
616 if (ifaceno >= dev->cdesc->bNumInterface)
618 *iface = &dev->ifaces[ifaceno];
619 return (USBD_NORMAL_COMPLETION);
623 usbd_pipe2device_handle(pipe)
624 usbd_pipe_handle pipe;
626 return (pipe->device);
631 usbd_set_interface(iface, altidx)
632 usbd_interface_handle iface;
635 usb_device_request_t req;
638 if (LIST_FIRST(&iface->pipes) != 0)
639 return (USBD_IN_USE);
641 if (iface->endpoints)
642 free(iface->endpoints, M_USB);
643 iface->endpoints = 0;
646 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
650 req.bmRequestType = UT_WRITE_INTERFACE;
651 req.bRequest = UR_SET_INTERFACE;
652 USETW(req.wValue, iface->idesc->bAlternateSetting);
653 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
654 USETW(req.wLength, 0);
655 return (usbd_do_request(iface->device, &req, 0));
659 usbd_get_no_alts(cdesc, ifaceno)
660 usb_config_descriptor_t *cdesc;
663 char *p = (char *)cdesc;
664 char *end = p + UGETW(cdesc->wTotalLength);
665 usb_interface_descriptor_t *d;
668 for (n = 0; p < end; p += d->bLength) {
669 d = (usb_interface_descriptor_t *)p;
670 if (p + d->bLength <= end &&
671 d->bDescriptorType == UDESC_INTERFACE &&
672 d->bInterfaceNumber == ifaceno)
679 usbd_get_interface_altindex(iface)
680 usbd_interface_handle iface;
682 return (iface->altindex);
686 usbd_get_interface(iface, aiface)
687 usbd_interface_handle iface;
690 usb_device_request_t req;
692 req.bmRequestType = UT_READ_INTERFACE;
693 req.bRequest = UR_GET_INTERFACE;
694 USETW(req.wValue, 0);
695 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
696 USETW(req.wLength, 1);
697 return (usbd_do_request(iface->device, &req, aiface));
700 /*** Internal routines ***/
702 /* Dequeue all pipe operations, called at splusb(). */
705 usbd_pipe_handle pipe;
707 usbd_xfer_handle xfer;
711 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
714 usbd_dump_queue(pipe);
717 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
718 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
719 pipe, xfer, pipe->methods));
720 /* Make the HC abort it (and invoke the callback). */
721 pipe->methods->abort(xfer);
722 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
724 return (USBD_NORMAL_COMPLETION);
727 /* Called at splusb() */
729 usb_transfer_complete(xfer)
730 usbd_xfer_handle xfer;
732 usbd_pipe_handle pipe = xfer->pipe;
733 usb_dma_t *dmap = &xfer->dmabuf;
734 int repeat = pipe->repeat;
739 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
740 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
744 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
748 polling = pipe->device->bus->use_polling;
753 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
754 usbd_xfer_isread(xfer)) {
756 if (xfer->actlen > xfer->length) {
757 printf("usb_transfer_complete: actlen > len %d > %d\n",
758 xfer->actlen, xfer->length);
759 xfer->actlen = xfer->length;
762 memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
765 /* if we allocated the buffer in usbd_transfer() we free it here. */
766 if (xfer->rqflags & URQ_AUTO_DMABUF) {
768 struct usbd_bus *bus = pipe->device->bus;
769 bus->methods->freem(bus, dmap);
770 xfer->rqflags &= ~URQ_AUTO_DMABUF;
774 if (pipe->methods->done != NULL)
775 pipe->methods->done(xfer);
778 /* Remove request from queue. */
780 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
781 printf("usb_transfer_complete: bad dequeue %p != %p\n",
782 xfer, SIMPLEQ_FIRST(&pipe->queue));
784 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
787 /* Count completed transfers. */
788 ++pipe->device->bus->stats.requests
789 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
792 if (xfer->status == USBD_NORMAL_COMPLETION &&
793 xfer->actlen < xfer->length &&
794 !(xfer->flags & USBD_SHORT_XFER_OK)) {
795 DPRINTFN(-1, ("usbd_transfer_cb: short transfer %d<%d\n",
796 xfer->actlen, xfer->length));
797 xfer->status = USBD_SHORT_XFER;
801 xfer->callback(xfer, xfer->priv, xfer->status);
803 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
807 /* XXX should we stop the queue on all errors? */
808 if (xfer->status == USBD_CANCELLED ||
809 xfer->status == USBD_TIMEOUT)
812 usbd_start_next(pipe);
817 usb_insert_transfer(xfer)
818 usbd_xfer_handle xfer;
820 usbd_pipe_handle pipe = xfer->pipe;
824 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
825 pipe, pipe->running, xfer->timeout));
827 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
829 err = USBD_IN_PROGRESS;
832 err = USBD_NORMAL_COMPLETION;
838 /* Called at splusb() */
840 usbd_start_next(pipe)
841 usbd_pipe_handle pipe;
843 usbd_xfer_handle xfer;
850 printf("usbd_start_next: pipe == NULL\n");
853 if (pipe->methods == NULL || pipe->methods->start == NULL) {
854 printf("usbd_start_next: pipe=%p no start method\n", pipe);
859 /* Get next request in queue. */
860 xfer = SIMPLEQ_FIRST(&pipe->queue);
861 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
865 err = pipe->methods->start(xfer);
866 if (err != USBD_IN_PROGRESS) {
867 printf("usbd_start_next: error=%d\n", err);
875 usbd_do_request(dev, req, data)
876 usbd_device_handle dev;
877 usb_device_request_t *req;
880 return (usbd_do_request_flags(dev, req, data, 0, 0));
884 usbd_do_request_flags(dev, req, data, flags, actlen)
885 usbd_device_handle dev;
886 usb_device_request_t *req;
891 usbd_xfer_handle xfer;
895 #if defined(__i386__)
896 KASSERT(intr_nesting_level == 0,
897 ("ohci_abort_req in interrupt context"));
899 if (dev->bus->intr_context) {
900 printf("usbd_do_request: not in process context\n");
905 xfer = usbd_alloc_request(dev);
908 usbd_setup_default_request(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
909 data, UGETW(req->wLength), flags, 0);
910 err = usbd_sync_transfer(xfer);
911 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
912 if (xfer->actlen > xfer->length)
913 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
914 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
915 dev->address, xfer->request.bmRequestType,
916 xfer->request.bRequest, UGETW(xfer->request.wValue),
917 UGETW(xfer->request.wIndex),
918 UGETW(xfer->request.wLength),
919 xfer->length, xfer->actlen));
922 *actlen = xfer->actlen;
923 if (err == USBD_STALLED) {
925 * The control endpoint has stalled. Control endpoints
926 * should not halt, but some may do so anyway so clear
927 * any halt condition.
929 usb_device_request_t treq;
934 treq.bmRequestType = UT_READ_ENDPOINT;
935 treq.bRequest = UR_GET_STATUS;
936 USETW(treq.wValue, 0);
937 USETW(treq.wIndex, 0);
938 USETW(treq.wLength, sizeof(usb_status_t));
939 usbd_setup_default_request(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
940 &treq, &status,sizeof(usb_status_t),
942 nerr = usbd_sync_transfer(xfer);
945 s = UGETW(status.wStatus);
946 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
949 treq.bmRequestType = UT_WRITE_ENDPOINT;
950 treq.bRequest = UR_CLEAR_FEATURE;
951 USETW(treq.wValue, UF_ENDPOINT_HALT);
952 USETW(treq.wIndex, 0);
953 USETW(treq.wLength, 0);
954 usbd_setup_default_request(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
955 &treq, &status, 0, 0, 0);
956 nerr = usbd_sync_transfer(xfer);
962 usbd_free_request(xfer);
967 usbd_do_request_async_cb(xfer, priv, status)
968 usbd_xfer_handle xfer;
969 usbd_private_handle priv;
972 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
973 if (xfer->actlen > xfer->length)
974 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
975 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
976 xfer->pipe->device->address,
977 xfer->request.bmRequestType,
978 xfer->request.bRequest, UGETW(xfer->request.wValue),
979 UGETW(xfer->request.wIndex),
980 UGETW(xfer->request.wLength),
981 xfer->length, xfer->actlen));
983 usbd_free_request(xfer);
987 * Execute a request without waiting for completion.
988 * Can be used from interrupt context.
991 usbd_do_request_async(dev, req, data)
992 usbd_device_handle dev;
993 usb_device_request_t *req;
996 usbd_xfer_handle xfer;
999 xfer = usbd_alloc_request(dev);
1001 return (USBD_NOMEM);
1002 usbd_setup_default_request(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1003 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1004 err = usbd_transfer(xfer);
1005 if (err != USBD_IN_PROGRESS) {
1006 usbd_free_request(xfer);
1009 return (USBD_NORMAL_COMPLETION);
1012 struct usbd_quirks *
1013 usbd_get_quirks(dev)
1014 usbd_device_handle dev;
1016 return (dev->quirks);
1019 /* XXX do periodic free() of free list */
1022 * Called from keyboard driver when in polling mode.
1026 usbd_interface_handle iface;
1028 iface->device->bus->methods->do_poll(iface->device->bus);
1032 usbd_set_polling(iface, on)
1033 usbd_interface_handle iface;
1037 iface->device->bus->use_polling++;
1039 iface->device->bus->use_polling--;
1043 usb_endpoint_descriptor_t *
1044 usbd_get_endpoint_descriptor(iface, address)
1045 usbd_interface_handle iface;
1048 struct usbd_endpoint *ep;
1051 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1052 ep = &iface->endpoints[i];
1053 if (ep->edesc->bEndpointAddress == address)
1054 return (iface->endpoints[i].edesc);
1059 #if defined(__FreeBSD__)
1061 usbd_driver_load(module_t mod, int what, void *arg)
1063 /* XXX should implement something like a function that removes all generic devices */