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.84 2003/11/09 23:56:19 joe Exp $
4 * $DragonFly: src/sys/bus/usb/usbdi.c,v 1.8 2004/02/11 15:17:26 joerg Exp $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 #include <sys/device.h>
48 #elif defined(__FreeBSD__) || defined(__DragonFly__)
49 #include <sys/module.h>
52 #if defined(DIAGNOSTIC) && defined(__i386__)
53 #include <machine/cpu.h>
56 #include <sys/malloc.h>
59 #include <machine/bus.h>
63 #include "usbdi_util.h"
67 #if defined(__FreeBSD__) || defined(__DragonFly__)
69 #include <machine/clock.h>
70 #define delay(d) DELAY(d)
74 #define DPRINTF(x) if (usbdebug) logprintf x
75 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
82 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
83 Static void usbd_do_request_async_cb
84 (usbd_xfer_handle, usbd_private_handle, usbd_status);
85 Static void usbd_start_next(usbd_pipe_handle pipe);
86 Static usbd_status usbd_open_pipe_ival
87 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
88 Static int usbd_xfer_isread(usbd_xfer_handle xfer);
90 Static int usbd_nbuses = 0;
105 usbd_xfer_isread(usbd_xfer_handle xfer)
107 if (xfer->rqflags & URQ_REQUEST)
108 return (xfer->request.bmRequestType & UT_READ);
110 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
116 usbd_dump_iface(struct usbd_interface *iface)
118 printf("usbd_dump_iface: iface=%p\n", iface);
121 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
122 iface->device, iface->idesc, iface->index, iface->altindex,
127 usbd_dump_device(struct usbd_device *dev)
129 printf("usbd_dump_device: dev=%p\n", dev);
132 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
133 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
134 "power=%d langid=%d\n",
135 dev->address, dev->config, dev->depth, dev->speed,
136 dev->self_powered, dev->power, dev->langid);
140 usbd_dump_endpoint(struct usbd_endpoint *endp)
142 printf("usbd_dump_endpoint: endp=%p\n", endp);
145 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
147 printf(" bEndpointAddress=0x%02x\n",
148 endp->edesc->bEndpointAddress);
152 usbd_dump_queue(usbd_pipe_handle pipe)
154 usbd_xfer_handle xfer;
156 printf("usbd_dump_queue: pipe=%p\n", pipe);
157 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
158 printf(" xfer=%p\n", xfer);
163 usbd_dump_pipe(usbd_pipe_handle pipe)
165 printf("usbd_dump_pipe: pipe=%p\n", pipe);
168 usbd_dump_iface(pipe->iface);
169 usbd_dump_device(pipe->device);
170 usbd_dump_endpoint(pipe->endpoint);
171 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
172 pipe->refcnt, pipe->running, pipe->aborting);
173 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
174 pipe->intrxfer, pipe->repeat, pipe->interval);
179 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
180 u_int8_t flags, usbd_pipe_handle *pipe)
182 return (usbd_open_pipe_ival(iface, address, flags, pipe,
183 USBD_DEFAULT_INTERVAL));
187 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
188 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
191 struct usbd_endpoint *ep;
195 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
196 iface, address, flags));
198 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
199 ep = &iface->endpoints[i];
200 if (ep->edesc == NULL)
201 return (USBD_IOERROR);
202 if (ep->edesc->bEndpointAddress == address)
205 return (USBD_BAD_ADDRESS);
207 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
208 return (USBD_IN_USE);
209 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
212 LIST_INSERT_HEAD(&iface->pipes, p, next);
214 return (USBD_NORMAL_COMPLETION);
218 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
219 u_int8_t flags, usbd_pipe_handle *pipe,
220 usbd_private_handle priv, void *buffer, u_int32_t len,
221 usbd_callback cb, int ival)
224 usbd_xfer_handle xfer;
225 usbd_pipe_handle ipipe;
227 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
228 address, flags, len));
230 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
234 xfer = usbd_alloc_xfer(iface->device);
239 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
240 USBD_NO_TIMEOUT, cb);
241 ipipe->intrxfer = xfer;
243 err = usbd_transfer(xfer);
245 if (err != USBD_IN_PROGRESS)
247 return (USBD_NORMAL_COMPLETION);
250 ipipe->intrxfer = NULL;
252 usbd_free_xfer(xfer);
254 usbd_close_pipe(ipipe);
259 usbd_close_pipe(usbd_pipe_handle pipe)
263 printf("usbd_close_pipe: pipe==NULL\n");
264 return (USBD_NORMAL_COMPLETION);
268 if (--pipe->refcnt != 0)
269 return (USBD_NORMAL_COMPLETION);
270 if (! SIMPLEQ_EMPTY(&pipe->queue))
271 return (USBD_PENDING_REQUESTS);
272 LIST_REMOVE(pipe, next);
273 pipe->endpoint->refcnt--;
274 pipe->methods->close(pipe);
275 if (pipe->intrxfer != NULL)
276 usbd_free_xfer(pipe->intrxfer);
278 return (USBD_NORMAL_COMPLETION);
282 usbd_transfer(usbd_xfer_handle xfer)
284 usbd_pipe_handle pipe = xfer->pipe;
285 usb_dma_t *dmap = &xfer->dmabuf;
290 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
291 xfer, xfer->flags, pipe, pipe->running));
294 usbd_dump_queue(pipe);
299 return (USBD_CANCELLED);
302 /* If there is no buffer, allocate one. */
303 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
304 struct usbd_bus *bus = pipe->device->bus;
307 if (xfer->rqflags & URQ_AUTO_DMABUF)
308 printf("usbd_transfer: has old buffer!\n");
310 err = bus->methods->allocm(bus, dmap, size);
313 xfer->rqflags |= URQ_AUTO_DMABUF;
316 /* Copy data if going out. */
317 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
318 !usbd_xfer_isread(xfer))
319 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
321 err = pipe->methods->transfer(xfer);
323 if (err != USBD_IN_PROGRESS && err) {
324 /* The transfer has not been queued, so free buffer. */
325 if (xfer->rqflags & URQ_AUTO_DMABUF) {
326 struct usbd_bus *bus = pipe->device->bus;
328 bus->methods->freem(bus, &xfer->dmabuf);
329 xfer->rqflags &= ~URQ_AUTO_DMABUF;
333 if (!(xfer->flags & USBD_SYNCHRONOUS))
336 /* Sync transfer, wait for completion. */
337 if (err != USBD_IN_PROGRESS)
341 if (pipe->device->bus->use_polling)
342 panic("usbd_transfer: not done\n");
343 tsleep(xfer, 0, "usbsyn", 0);
346 return (xfer->status);
349 /* Like usbd_transfer(), but waits for completion. */
351 usbd_sync_transfer(usbd_xfer_handle xfer)
353 xfer->flags |= USBD_SYNCHRONOUS;
354 return (usbd_transfer(xfer));
358 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
360 struct usbd_bus *bus = xfer->device->bus;
364 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
365 printf("usbd_alloc_buffer: xfer already has a buffer\n");
367 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
370 xfer->rqflags |= URQ_DEV_DMABUF;
371 return (KERNADDR(&xfer->dmabuf, 0));
375 usbd_free_buffer(usbd_xfer_handle xfer)
378 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
379 printf("usbd_free_buffer: no buffer\n");
383 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
384 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
388 usbd_get_buffer(usbd_xfer_handle xfer)
390 if (!(xfer->rqflags & URQ_DEV_DMABUF))
392 return (KERNADDR(&xfer->dmabuf, 0));
396 usbd_alloc_xfer(usbd_device_handle dev)
398 usbd_xfer_handle xfer;
400 xfer = dev->bus->methods->allocx(dev->bus);
404 usb_callout_init(xfer->timeout_handle);
405 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
410 usbd_free_xfer(usbd_xfer_handle xfer)
412 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
413 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
414 usbd_free_buffer(xfer);
415 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
416 if (callout_pending(&xfer->timeout_handle)) {
417 callout_stop(&xfer->timeout_handle);
418 printf("usbd_free_xfer: timout_handle pending");
421 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
422 return (USBD_NORMAL_COMPLETION);
426 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
427 usbd_private_handle priv, void *buffer, u_int32_t length,
428 u_int16_t flags, u_int32_t timeout,
429 usbd_callback callback)
433 xfer->buffer = buffer;
434 xfer->length = length;
437 xfer->timeout = timeout;
438 xfer->status = USBD_NOT_STARTED;
439 xfer->callback = callback;
440 xfer->rqflags &= ~URQ_REQUEST;
445 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
446 usbd_private_handle priv, u_int32_t timeout,
447 usb_device_request_t *req, void *buffer,
448 u_int32_t length, u_int16_t flags,
449 usbd_callback callback)
451 xfer->pipe = dev->default_pipe;
453 xfer->buffer = buffer;
454 xfer->length = length;
457 xfer->timeout = timeout;
458 xfer->status = USBD_NOT_STARTED;
459 xfer->callback = callback;
460 xfer->request = *req;
461 xfer->rqflags |= URQ_REQUEST;
466 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
467 usbd_private_handle priv, u_int16_t *frlengths,
468 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
476 xfer->timeout = USBD_NO_TIMEOUT;
477 xfer->status = USBD_NOT_STARTED;
478 xfer->callback = callback;
479 xfer->rqflags &= ~URQ_REQUEST;
480 xfer->frlengths = frlengths;
481 xfer->nframes = nframes;
485 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
486 void **buffer, u_int32_t *count, usbd_status *status)
491 *buffer = xfer->buffer;
493 *count = xfer->actlen;
495 *status = xfer->status;
498 usb_config_descriptor_t *
499 usbd_get_config_descriptor(usbd_device_handle dev)
503 printf("usbd_get_config_descriptor: dev == NULL\n");
510 usb_interface_descriptor_t *
511 usbd_get_interface_descriptor(usbd_interface_handle iface)
515 printf("usbd_get_interface_descriptor: dev == NULL\n");
519 return (iface->idesc);
522 usb_device_descriptor_t *
523 usbd_get_device_descriptor(usbd_device_handle dev)
525 return (&dev->ddesc);
528 usb_endpoint_descriptor_t *
529 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
531 if (index >= iface->idesc->bNumEndpoints)
533 return (iface->endpoints[index].edesc);
537 usbd_abort_pipe(usbd_pipe_handle pipe)
544 printf("usbd_close_pipe: pipe==NULL\n");
545 return (USBD_NORMAL_COMPLETION);
549 err = usbd_ar_pipe(pipe);
555 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
557 usbd_device_handle dev = pipe->device;
558 usb_device_request_t req;
561 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
564 * Clearing en endpoint stall resets the endpoint toggle, so
565 * do the same to the HC toggle.
567 pipe->methods->cleartoggle(pipe);
569 req.bmRequestType = UT_WRITE_ENDPOINT;
570 req.bRequest = UR_CLEAR_FEATURE;
571 USETW(req.wValue, UF_ENDPOINT_HALT);
572 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
573 USETW(req.wLength, 0);
574 err = usbd_do_request(dev, &req, 0);
576 XXX should we do this?
578 pipe->state = USBD_PIPE_ACTIVE;
579 /* XXX activate pipe */
586 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
588 usbd_device_handle dev = pipe->device;
589 usb_device_request_t req;
592 pipe->methods->cleartoggle(pipe);
594 req.bmRequestType = UT_WRITE_ENDPOINT;
595 req.bRequest = UR_CLEAR_FEATURE;
596 USETW(req.wValue, UF_ENDPOINT_HALT);
597 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
598 USETW(req.wLength, 0);
599 err = usbd_do_request_async(dev, &req, 0);
604 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
606 pipe->methods->cleartoggle(pipe);
610 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
613 if (iface == NULL || iface->idesc == NULL) {
614 printf("usbd_endpoint_count: NULL pointer\n");
618 *count = iface->idesc->bNumEndpoints;
619 return (USBD_NORMAL_COMPLETION);
623 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
625 if (dev->cdesc == NULL)
626 return (USBD_NOT_CONFIGURED);
627 *count = dev->cdesc->bNumInterface;
628 return (USBD_NORMAL_COMPLETION);
632 usbd_interface2device_handle(usbd_interface_handle iface,
633 usbd_device_handle *dev)
635 *dev = iface->device;
639 usbd_device2interface_handle(usbd_device_handle dev,
640 u_int8_t ifaceno, usbd_interface_handle *iface)
642 if (dev->cdesc == NULL)
643 return (USBD_NOT_CONFIGURED);
644 if (ifaceno >= dev->cdesc->bNumInterface)
646 *iface = &dev->ifaces[ifaceno];
647 return (USBD_NORMAL_COMPLETION);
651 usbd_pipe2device_handle(usbd_pipe_handle pipe)
653 return (pipe->device);
658 usbd_set_interface(usbd_interface_handle iface, int altidx)
660 usb_device_request_t req;
664 if (LIST_FIRST(&iface->pipes) != 0)
665 return (USBD_IN_USE);
667 endpoints = iface->endpoints;
668 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
672 /* new setting works, we can free old endpoints */
673 if (endpoints != NULL)
674 free(endpoints, M_USB);
677 if (iface->idesc == NULL) {
678 printf("usbd_set_interface: NULL pointer\n");
683 req.bmRequestType = UT_WRITE_INTERFACE;
684 req.bRequest = UR_SET_INTERFACE;
685 USETW(req.wValue, iface->idesc->bAlternateSetting);
686 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
687 USETW(req.wLength, 0);
688 return (usbd_do_request(iface->device, &req, 0));
692 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
694 char *p = (char *)cdesc;
695 char *end = p + UGETW(cdesc->wTotalLength);
696 usb_interface_descriptor_t *d;
699 for (n = 0; p < end; p += d->bLength) {
700 d = (usb_interface_descriptor_t *)p;
701 if (p + d->bLength <= end &&
702 d->bDescriptorType == UDESC_INTERFACE &&
703 d->bInterfaceNumber == ifaceno)
710 usbd_get_interface_altindex(usbd_interface_handle iface)
712 return (iface->altindex);
716 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
718 usb_device_request_t req;
720 req.bmRequestType = UT_READ_INTERFACE;
721 req.bRequest = UR_GET_INTERFACE;
722 USETW(req.wValue, 0);
723 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
724 USETW(req.wLength, 1);
725 return (usbd_do_request(iface->device, &req, aiface));
728 /*** Internal routines ***/
730 /* Dequeue all pipe operations, called at splusb(). */
732 usbd_ar_pipe(usbd_pipe_handle pipe)
734 usbd_xfer_handle xfer;
738 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
741 usbd_dump_queue(pipe);
745 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
746 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
747 pipe, xfer, pipe->methods));
748 /* Make the HC abort it (and invoke the callback). */
749 pipe->methods->abort(xfer);
750 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
753 return (USBD_NORMAL_COMPLETION);
756 /* Called at splusb() */
758 usb_transfer_complete(usbd_xfer_handle xfer)
760 usbd_pipe_handle pipe = xfer->pipe;
761 usb_dma_t *dmap = &xfer->dmabuf;
762 int repeat = pipe->repeat;
767 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
768 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
770 if (xfer->busy_free != XFER_ONQU) {
771 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
772 xfer, xfer->busy_free);
779 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
783 polling = pipe->device->bus->use_polling;
788 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
789 usbd_xfer_isread(xfer)) {
791 if (xfer->actlen > xfer->length) {
792 printf("usb_transfer_complete: actlen > len %d > %d\n",
793 xfer->actlen, xfer->length);
794 xfer->actlen = xfer->length;
797 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
800 /* if we allocated the buffer in usbd_transfer() we free it here. */
801 if (xfer->rqflags & URQ_AUTO_DMABUF) {
803 struct usbd_bus *bus = pipe->device->bus;
804 bus->methods->freem(bus, dmap);
805 xfer->rqflags &= ~URQ_AUTO_DMABUF;
810 /* Remove request from queue. */
812 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
813 printf("usb_transfer_complete: bad dequeue %p != %p\n",
814 xfer, SIMPLEQ_FIRST(&pipe->queue));
815 xfer->busy_free = XFER_BUSY;
817 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
819 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
820 repeat, SIMPLEQ_FIRST(&pipe->queue)));
822 /* Count completed transfers. */
823 ++pipe->device->bus->stats.uds_requests
824 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
827 if (!xfer->status && xfer->actlen < xfer->length &&
828 !(xfer->flags & USBD_SHORT_XFER_OK)) {
829 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
830 xfer->actlen, xfer->length));
831 xfer->status = USBD_SHORT_XFER;
835 xfer->callback(xfer, xfer->priv, xfer->status);
838 if (pipe->methods->done != NULL)
839 pipe->methods->done(xfer);
841 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
843 pipe->methods->done(xfer);
846 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
850 /* XXX should we stop the queue on all errors? */
851 if ((xfer->status == USBD_CANCELLED ||
852 xfer->status == USBD_TIMEOUT) &&
853 pipe->iface != NULL) /* not control pipe */
856 usbd_start_next(pipe);
861 usb_insert_transfer(usbd_xfer_handle xfer)
863 usbd_pipe_handle pipe = xfer->pipe;
867 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
868 pipe, pipe->running, xfer->timeout));
870 if (xfer->busy_free != XFER_BUSY) {
871 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
872 xfer, xfer->busy_free);
875 xfer->busy_free = XFER_ONQU;
878 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
880 err = USBD_IN_PROGRESS;
883 err = USBD_NORMAL_COMPLETION;
889 /* Called at splusb() */
891 usbd_start_next(usbd_pipe_handle pipe)
893 usbd_xfer_handle xfer;
900 printf("usbd_start_next: pipe == NULL\n");
903 if (pipe->methods == NULL || pipe->methods->start == NULL) {
904 printf("usbd_start_next: pipe=%p no start method\n", pipe);
909 /* Get next request in queue. */
910 xfer = SIMPLEQ_FIRST(&pipe->queue);
911 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
915 err = pipe->methods->start(xfer);
916 if (err != USBD_IN_PROGRESS) {
917 printf("usbd_start_next: error=%d\n", err);
926 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
928 return (usbd_do_request_flags(dev, req, data, 0, 0,
929 USBD_DEFAULT_TIMEOUT));
933 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
934 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
936 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
937 data, flags, actlen, timo));
941 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
942 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
945 usbd_xfer_handle xfer;
949 #if defined(__i386__) && (defined(__FreeBSD__) || defined(__DragonFly__))
950 KASSERT(mycpu->gd_intr_nesting_level == 0,
951 ("usbd_do_request: in interrupt context"));
953 if (dev->bus->intr_context) {
954 printf("usbd_do_request: not in process context\n");
959 xfer = usbd_alloc_xfer(dev);
962 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
963 data, UGETW(req->wLength), flags, 0);
965 err = usbd_sync_transfer(xfer);
966 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
967 if (xfer->actlen > xfer->length)
968 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
969 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
970 dev->address, xfer->request.bmRequestType,
971 xfer->request.bRequest, UGETW(xfer->request.wValue),
972 UGETW(xfer->request.wIndex),
973 UGETW(xfer->request.wLength),
974 xfer->length, xfer->actlen));
977 *actlen = xfer->actlen;
978 if (err == USBD_STALLED) {
980 * The control endpoint has stalled. Control endpoints
981 * should not halt, but some may do so anyway so clear
982 * any halt condition.
984 usb_device_request_t treq;
989 treq.bmRequestType = UT_READ_ENDPOINT;
990 treq.bRequest = UR_GET_STATUS;
991 USETW(treq.wValue, 0);
992 USETW(treq.wIndex, 0);
993 USETW(treq.wLength, sizeof(usb_status_t));
994 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
995 &treq, &status,sizeof(usb_status_t),
997 nerr = usbd_sync_transfer(xfer);
1000 s = UGETW(status.wStatus);
1001 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1002 if (!(s & UES_HALT))
1004 treq.bmRequestType = UT_WRITE_ENDPOINT;
1005 treq.bRequest = UR_CLEAR_FEATURE;
1006 USETW(treq.wValue, UF_ENDPOINT_HALT);
1007 USETW(treq.wIndex, 0);
1008 USETW(treq.wLength, 0);
1009 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1010 &treq, &status, 0, 0, 0);
1011 nerr = usbd_sync_transfer(xfer);
1017 usbd_free_xfer(xfer);
1022 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1025 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1026 if (xfer->actlen > xfer->length)
1027 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1028 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1029 xfer->pipe->device->address,
1030 xfer->request.bmRequestType,
1031 xfer->request.bRequest, UGETW(xfer->request.wValue),
1032 UGETW(xfer->request.wIndex),
1033 UGETW(xfer->request.wLength),
1034 xfer->length, xfer->actlen));
1036 usbd_free_xfer(xfer);
1040 * Execute a request without waiting for completion.
1041 * Can be used from interrupt context.
1044 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1047 usbd_xfer_handle xfer;
1050 xfer = usbd_alloc_xfer(dev);
1052 return (USBD_NOMEM);
1053 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1054 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1055 err = usbd_transfer(xfer);
1056 if (err != USBD_IN_PROGRESS) {
1057 usbd_free_xfer(xfer);
1060 return (USBD_NORMAL_COMPLETION);
1063 const struct usbd_quirks *
1064 usbd_get_quirks(usbd_device_handle dev)
1068 printf("usbd_get_quirks: dev == NULL\n");
1072 return (dev->quirks);
1075 /* XXX do periodic free() of free list */
1078 * Called from keyboard driver when in polling mode.
1081 usbd_dopoll(usbd_interface_handle iface)
1083 iface->device->bus->methods->do_poll(iface->device->bus);
1087 usbd_set_polling(usbd_device_handle dev, int on)
1090 dev->bus->use_polling++;
1092 dev->bus->use_polling--;
1093 /* When polling we need to make sure there is nothing pending to do. */
1094 if (dev->bus->use_polling)
1095 dev->bus->methods->soft_intr(dev->bus);
1099 usb_endpoint_descriptor_t *
1100 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1102 struct usbd_endpoint *ep;
1105 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1106 ep = &iface->endpoints[i];
1107 if (ep->edesc->bEndpointAddress == address)
1108 return (iface->endpoints[i].edesc);
1114 * usbd_ratecheck() can limit the number of error messages that occurs.
1115 * When a device is unplugged it may take up to 0.25s for the hub driver
1116 * to notice it. If the driver continuosly tries to do I/O operations
1117 * this can generate a large number of messages.
1120 usbd_ratecheck(struct timeval *last)
1123 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1125 return (ratecheck(last, &errinterval));
1131 * Search for a vendor/product pair in an array. The item size is
1132 * given as an argument.
1134 const struct usb_devno *
1135 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1136 u_int16_t vendor, u_int16_t product)
1138 while (nentries-- > 0) {
1139 u_int16_t tproduct = tbl->ud_product;
1140 if (tbl->ud_vendor == vendor &&
1141 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1143 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1148 #if defined(__FreeBSD__) || defined(__DragonFly__)
1150 usbd_driver_load(module_t mod, int what, void *arg)
1152 /* XXX should implement something like a function that removes all generic devices */