1 /* $NetBSD: usbdi.c,v 1.100 2002/05/19 06:24:33 augustss Exp $ */
3 /* Also already have from NetBSD:
4 * $NetBSD: usbdi.c,v 1.102 2002/07/11 21:14:35 augustss Exp $
5 * $NetBSD: usbdi.c,v 1.103 2002/09/27 15:37:38 provos Exp $
12 * Copyright (c) 1998 The NetBSD Foundation, Inc.
13 * All rights reserved.
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by Lennart Augustsson (lennart@augustsson.net) at
17 * Carlstedt Research & Technology.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the NetBSD
30 * Foundation, Inc. and its contributors.
31 * 4. Neither the name of The NetBSD Foundation nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #if defined(__NetBSD__) || defined(__OpenBSD__)
51 #include <sys/device.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
56 #if defined(DIAGNOSTIC) && defined(__i386__)
57 #include <machine/cpu.h>
60 #include <sys/malloc.h>
63 #include <machine/bus.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdivar.h>
69 #include <dev/usb/usb_mem.h>
71 #if defined(__FreeBSD__)
73 #include <machine/clock.h>
74 #define delay(d) DELAY(d)
78 #define DPRINTF(x) if (usbdebug) logprintf x
79 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
86 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
87 Static void usbd_do_request_async_cb
88 (usbd_xfer_handle, usbd_private_handle, usbd_status);
89 Static void usbd_start_next(usbd_pipe_handle pipe);
90 Static usbd_status usbd_open_pipe_ival
91 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
92 Static int usbd_xfer_isread(usbd_xfer_handle xfer);
94 Static int usbd_nbuses = 0;
109 usbd_xfer_isread(usbd_xfer_handle xfer)
111 if (xfer->rqflags & URQ_REQUEST)
112 return (xfer->request.bmRequestType & UT_READ);
114 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
120 usbd_dump_iface(struct usbd_interface *iface)
122 printf("usbd_dump_iface: iface=%p\n", iface);
125 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
126 iface->device, iface->idesc, iface->index, iface->altindex,
131 usbd_dump_device(struct usbd_device *dev)
133 printf("usbd_dump_device: dev=%p\n", dev);
136 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
137 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
138 "power=%d langid=%d\n",
139 dev->address, dev->config, dev->depth, dev->speed,
140 dev->self_powered, dev->power, dev->langid);
144 usbd_dump_endpoint(struct usbd_endpoint *endp)
146 printf("usbd_dump_endpoint: endp=%p\n", endp);
149 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
151 printf(" bEndpointAddress=0x%02x\n",
152 endp->edesc->bEndpointAddress);
156 usbd_dump_queue(usbd_pipe_handle pipe)
158 usbd_xfer_handle xfer;
160 printf("usbd_dump_queue: pipe=%p\n", pipe);
161 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
162 printf(" xfer=%p\n", xfer);
167 usbd_dump_pipe(usbd_pipe_handle pipe)
169 printf("usbd_dump_pipe: pipe=%p\n", pipe);
172 usbd_dump_iface(pipe->iface);
173 usbd_dump_device(pipe->device);
174 usbd_dump_endpoint(pipe->endpoint);
175 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
176 pipe->refcnt, pipe->running, pipe->aborting);
177 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
178 pipe->intrxfer, pipe->repeat, pipe->interval);
183 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
184 u_int8_t flags, usbd_pipe_handle *pipe)
186 return (usbd_open_pipe_ival(iface, address, flags, pipe,
187 USBD_DEFAULT_INTERVAL));
191 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
192 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
195 struct usbd_endpoint *ep;
199 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
200 iface, address, flags));
202 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
203 ep = &iface->endpoints[i];
204 if (ep->edesc == NULL)
205 return (USBD_IOERROR);
206 if (ep->edesc->bEndpointAddress == address)
209 return (USBD_BAD_ADDRESS);
211 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
212 return (USBD_IN_USE);
213 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
216 LIST_INSERT_HEAD(&iface->pipes, p, next);
218 return (USBD_NORMAL_COMPLETION);
222 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
223 u_int8_t flags, usbd_pipe_handle *pipe,
224 usbd_private_handle priv, void *buffer, u_int32_t len,
225 usbd_callback cb, int ival)
228 usbd_xfer_handle xfer;
229 usbd_pipe_handle ipipe;
231 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
232 address, flags, len));
234 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
238 xfer = usbd_alloc_xfer(iface->device);
243 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
244 USBD_NO_TIMEOUT, cb);
245 ipipe->intrxfer = xfer;
247 err = usbd_transfer(xfer);
249 if (err != USBD_IN_PROGRESS)
251 return (USBD_NORMAL_COMPLETION);
254 ipipe->intrxfer = NULL;
256 usbd_free_xfer(xfer);
258 usbd_close_pipe(ipipe);
263 usbd_close_pipe(usbd_pipe_handle pipe)
267 printf("usbd_close_pipe: pipe==NULL\n");
268 return (USBD_NORMAL_COMPLETION);
272 if (--pipe->refcnt != 0)
273 return (USBD_NORMAL_COMPLETION);
274 if (! SIMPLEQ_EMPTY(&pipe->queue))
275 return (USBD_PENDING_REQUESTS);
276 LIST_REMOVE(pipe, next);
277 pipe->endpoint->refcnt--;
278 pipe->methods->close(pipe);
279 if (pipe->intrxfer != NULL)
280 usbd_free_xfer(pipe->intrxfer);
282 return (USBD_NORMAL_COMPLETION);
286 usbd_transfer(usbd_xfer_handle xfer)
288 usbd_pipe_handle pipe = xfer->pipe;
289 usb_dma_t *dmap = &xfer->dmabuf;
294 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
295 xfer, xfer->flags, pipe, pipe->running));
298 usbd_dump_queue(pipe);
303 return (USBD_CANCELLED);
306 /* If there is no buffer, allocate one. */
307 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
308 struct usbd_bus *bus = pipe->device->bus;
311 if (xfer->rqflags & URQ_AUTO_DMABUF)
312 printf("usbd_transfer: has old buffer!\n");
314 err = bus->methods->allocm(bus, dmap, size);
317 xfer->rqflags |= URQ_AUTO_DMABUF;
320 /* Copy data if going out. */
321 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
322 !usbd_xfer_isread(xfer))
323 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
325 err = pipe->methods->transfer(xfer);
327 if (err != USBD_IN_PROGRESS && err) {
328 /* The transfer has not been queued, so free buffer. */
329 if (xfer->rqflags & URQ_AUTO_DMABUF) {
330 struct usbd_bus *bus = pipe->device->bus;
332 bus->methods->freem(bus, &xfer->dmabuf);
333 xfer->rqflags &= ~URQ_AUTO_DMABUF;
337 if (!(xfer->flags & USBD_SYNCHRONOUS))
340 /* Sync transfer, wait for completion. */
341 if (err != USBD_IN_PROGRESS)
345 if (pipe->device->bus->use_polling)
346 panic("usbd_transfer: not done");
347 tsleep(xfer, PRIBIO, "usbsyn", 0);
350 return (xfer->status);
353 /* Like usbd_transfer(), but waits for completion. */
355 usbd_sync_transfer(usbd_xfer_handle xfer)
357 xfer->flags |= USBD_SYNCHRONOUS;
358 return (usbd_transfer(xfer));
362 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
364 struct usbd_bus *bus = xfer->device->bus;
368 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
369 printf("usbd_alloc_buffer: xfer already has a buffer\n");
371 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
374 xfer->rqflags |= URQ_DEV_DMABUF;
375 return (KERNADDR(&xfer->dmabuf, 0));
379 usbd_free_buffer(usbd_xfer_handle xfer)
382 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
383 printf("usbd_free_buffer: no buffer\n");
387 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
388 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
392 usbd_get_buffer(usbd_xfer_handle xfer)
394 if (!(xfer->rqflags & URQ_DEV_DMABUF))
396 return (KERNADDR(&xfer->dmabuf, 0));
400 usbd_alloc_xfer(usbd_device_handle dev)
402 usbd_xfer_handle xfer;
404 xfer = dev->bus->methods->allocx(dev->bus);
408 usb_callout_init(xfer->timeout_handle);
409 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
414 usbd_free_xfer(usbd_xfer_handle xfer)
416 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
417 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
418 usbd_free_buffer(xfer);
419 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
420 if (callout_pending(&xfer->timeout_handle)) {
421 callout_stop(&xfer->timeout_handle);
422 printf("usbd_free_xfer: timout_handle pending");
425 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
426 return (USBD_NORMAL_COMPLETION);
430 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
431 usbd_private_handle priv, void *buffer, u_int32_t length,
432 u_int16_t flags, u_int32_t timeout,
433 usbd_callback callback)
437 xfer->buffer = buffer;
438 xfer->length = length;
441 xfer->timeout = timeout;
442 xfer->status = USBD_NOT_STARTED;
443 xfer->callback = callback;
444 xfer->rqflags &= ~URQ_REQUEST;
449 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
450 usbd_private_handle priv, u_int32_t timeout,
451 usb_device_request_t *req, void *buffer,
452 u_int32_t length, u_int16_t flags,
453 usbd_callback callback)
455 xfer->pipe = dev->default_pipe;
457 xfer->buffer = buffer;
458 xfer->length = length;
461 xfer->timeout = timeout;
462 xfer->status = USBD_NOT_STARTED;
463 xfer->callback = callback;
464 xfer->request = *req;
465 xfer->rqflags |= URQ_REQUEST;
470 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
471 usbd_private_handle priv, u_int16_t *frlengths,
472 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
480 xfer->timeout = USBD_NO_TIMEOUT;
481 xfer->status = USBD_NOT_STARTED;
482 xfer->callback = callback;
483 xfer->rqflags &= ~URQ_REQUEST;
484 xfer->frlengths = frlengths;
485 xfer->nframes = nframes;
489 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
490 void **buffer, u_int32_t *count, usbd_status *status)
495 *buffer = xfer->buffer;
497 *count = xfer->actlen;
499 *status = xfer->status;
502 usb_config_descriptor_t *
503 usbd_get_config_descriptor(usbd_device_handle dev)
507 printf("usbd_get_config_descriptor: dev == NULL\n");
514 usb_interface_descriptor_t *
515 usbd_get_interface_descriptor(usbd_interface_handle iface)
519 printf("usbd_get_interface_descriptor: dev == NULL\n");
523 return (iface->idesc);
526 usb_device_descriptor_t *
527 usbd_get_device_descriptor(usbd_device_handle dev)
529 return (&dev->ddesc);
532 usb_endpoint_descriptor_t *
533 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
535 if (index >= iface->idesc->bNumEndpoints)
537 return (iface->endpoints[index].edesc);
541 usbd_abort_pipe(usbd_pipe_handle pipe)
548 printf("usbd_close_pipe: pipe==NULL\n");
549 return (USBD_NORMAL_COMPLETION);
553 err = usbd_ar_pipe(pipe);
559 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
561 usbd_device_handle dev = pipe->device;
562 usb_device_request_t req;
565 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
568 * Clearing en endpoint stall resets the endpoint toggle, so
569 * do the same to the HC toggle.
571 pipe->methods->cleartoggle(pipe);
573 req.bmRequestType = UT_WRITE_ENDPOINT;
574 req.bRequest = UR_CLEAR_FEATURE;
575 USETW(req.wValue, UF_ENDPOINT_HALT);
576 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
577 USETW(req.wLength, 0);
578 err = usbd_do_request(dev, &req, 0);
580 XXX should we do this?
582 pipe->state = USBD_PIPE_ACTIVE;
583 /* XXX activate pipe */
590 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
592 usbd_device_handle dev = pipe->device;
593 usb_device_request_t req;
596 pipe->methods->cleartoggle(pipe);
598 req.bmRequestType = UT_WRITE_ENDPOINT;
599 req.bRequest = UR_CLEAR_FEATURE;
600 USETW(req.wValue, UF_ENDPOINT_HALT);
601 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
602 USETW(req.wLength, 0);
603 err = usbd_do_request_async(dev, &req, 0);
608 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
610 pipe->methods->cleartoggle(pipe);
614 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
617 if (iface == NULL || iface->idesc == NULL) {
618 printf("usbd_endpoint_count: NULL pointer\n");
622 *count = iface->idesc->bNumEndpoints;
623 return (USBD_NORMAL_COMPLETION);
627 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
629 if (dev->cdesc == NULL)
630 return (USBD_NOT_CONFIGURED);
631 *count = dev->cdesc->bNumInterface;
632 return (USBD_NORMAL_COMPLETION);
636 usbd_interface2device_handle(usbd_interface_handle iface,
637 usbd_device_handle *dev)
639 *dev = iface->device;
643 usbd_device2interface_handle(usbd_device_handle dev,
644 u_int8_t ifaceno, usbd_interface_handle *iface)
646 if (dev->cdesc == NULL)
647 return (USBD_NOT_CONFIGURED);
648 if (ifaceno >= dev->cdesc->bNumInterface)
650 *iface = &dev->ifaces[ifaceno];
651 return (USBD_NORMAL_COMPLETION);
655 usbd_pipe2device_handle(usbd_pipe_handle pipe)
657 return (pipe->device);
662 usbd_set_interface(usbd_interface_handle iface, int altidx)
664 usb_device_request_t req;
668 if (LIST_FIRST(&iface->pipes) != 0)
669 return (USBD_IN_USE);
671 endpoints = iface->endpoints;
672 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
676 /* new setting works, we can free old endpoints */
677 if (endpoints != NULL)
678 free(endpoints, M_USB);
681 if (iface->idesc == NULL) {
682 printf("usbd_set_interface: NULL pointer\n");
687 req.bmRequestType = UT_WRITE_INTERFACE;
688 req.bRequest = UR_SET_INTERFACE;
689 USETW(req.wValue, iface->idesc->bAlternateSetting);
690 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
691 USETW(req.wLength, 0);
692 return (usbd_do_request(iface->device, &req, 0));
696 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
698 char *p = (char *)cdesc;
699 char *end = p + UGETW(cdesc->wTotalLength);
700 usb_interface_descriptor_t *d;
703 for (n = 0; p < end; p += d->bLength) {
704 d = (usb_interface_descriptor_t *)p;
705 if (p + d->bLength <= end &&
706 d->bDescriptorType == UDESC_INTERFACE &&
707 d->bInterfaceNumber == ifaceno)
714 usbd_get_interface_altindex(usbd_interface_handle iface)
716 return (iface->altindex);
720 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
722 usb_device_request_t req;
724 req.bmRequestType = UT_READ_INTERFACE;
725 req.bRequest = UR_GET_INTERFACE;
726 USETW(req.wValue, 0);
727 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
728 USETW(req.wLength, 1);
729 return (usbd_do_request(iface->device, &req, aiface));
732 /*** Internal routines ***/
734 /* Dequeue all pipe operations, called at splusb(). */
736 usbd_ar_pipe(usbd_pipe_handle pipe)
738 usbd_xfer_handle xfer;
742 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
745 usbd_dump_queue(pipe);
749 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
750 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
751 pipe, xfer, pipe->methods));
752 /* Make the HC abort it (and invoke the callback). */
753 pipe->methods->abort(xfer);
754 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
757 return (USBD_NORMAL_COMPLETION);
760 /* Called at splusb() */
762 usb_transfer_complete(usbd_xfer_handle xfer)
764 usbd_pipe_handle pipe = xfer->pipe;
765 usb_dma_t *dmap = &xfer->dmabuf;
766 int repeat = pipe->repeat;
771 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
772 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
774 if (xfer->busy_free != XFER_ONQU) {
775 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
776 xfer, xfer->busy_free);
783 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
787 polling = pipe->device->bus->use_polling;
792 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
793 usbd_xfer_isread(xfer)) {
795 if (xfer->actlen > xfer->length) {
796 printf("usb_transfer_complete: actlen > len %d > %d\n",
797 xfer->actlen, xfer->length);
798 xfer->actlen = xfer->length;
801 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
804 /* if we allocated the buffer in usbd_transfer() we free it here. */
805 if (xfer->rqflags & URQ_AUTO_DMABUF) {
807 struct usbd_bus *bus = pipe->device->bus;
808 bus->methods->freem(bus, dmap);
809 xfer->rqflags &= ~URQ_AUTO_DMABUF;
814 /* Remove request from queue. */
816 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
817 printf("usb_transfer_complete: bad dequeue %p != %p\n",
818 xfer, SIMPLEQ_FIRST(&pipe->queue));
819 xfer->busy_free = XFER_BUSY;
821 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
823 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
824 repeat, SIMPLEQ_FIRST(&pipe->queue)));
826 /* Count completed transfers. */
827 ++pipe->device->bus->stats.uds_requests
828 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
831 if (!xfer->status && xfer->actlen < xfer->length &&
832 !(xfer->flags & USBD_SHORT_XFER_OK)) {
833 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
834 xfer->actlen, xfer->length));
835 xfer->status = USBD_SHORT_XFER;
839 xfer->callback(xfer, xfer->priv, xfer->status);
842 if (pipe->methods->done != NULL)
843 pipe->methods->done(xfer);
845 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
847 pipe->methods->done(xfer);
850 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
854 /* XXX should we stop the queue on all errors? */
855 if ((xfer->status == USBD_CANCELLED ||
856 xfer->status == USBD_TIMEOUT) &&
857 pipe->iface != NULL) /* not control pipe */
860 usbd_start_next(pipe);
865 usb_insert_transfer(usbd_xfer_handle xfer)
867 usbd_pipe_handle pipe = xfer->pipe;
871 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
872 pipe, pipe->running, xfer->timeout));
874 if (xfer->busy_free != XFER_BUSY) {
875 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
876 xfer, xfer->busy_free);
879 xfer->busy_free = XFER_ONQU;
882 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
884 err = USBD_IN_PROGRESS;
887 err = USBD_NORMAL_COMPLETION;
893 /* Called at splusb() */
895 usbd_start_next(usbd_pipe_handle pipe)
897 usbd_xfer_handle xfer;
904 printf("usbd_start_next: pipe == NULL\n");
907 if (pipe->methods == NULL || pipe->methods->start == NULL) {
908 printf("usbd_start_next: pipe=%p no start method\n", pipe);
913 /* Get next request in queue. */
914 xfer = SIMPLEQ_FIRST(&pipe->queue);
915 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
919 err = pipe->methods->start(xfer);
920 if (err != USBD_IN_PROGRESS) {
921 printf("usbd_start_next: error=%d\n", err);
929 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
931 return (usbd_do_request_flags(dev, req, data, 0, 0,
932 USBD_DEFAULT_TIMEOUT));
936 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
937 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
939 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
940 data, flags, actlen, timo));
944 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
945 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
948 usbd_xfer_handle xfer;
952 #if defined(__i386__) && defined(__FreeBSD__)
953 KASSERT(curthread->td_intr_nesting_level == 0,
954 ("usbd_do_request: in interrupt context"));
956 if (dev->bus->intr_context) {
957 printf("usbd_do_request: not in process context\n");
962 xfer = usbd_alloc_xfer(dev);
965 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
966 data, UGETW(req->wLength), flags, 0);
968 err = usbd_sync_transfer(xfer);
969 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
970 if (xfer->actlen > xfer->length)
971 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
972 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
973 dev->address, xfer->request.bmRequestType,
974 xfer->request.bRequest, UGETW(xfer->request.wValue),
975 UGETW(xfer->request.wIndex),
976 UGETW(xfer->request.wLength),
977 xfer->length, xfer->actlen));
980 *actlen = xfer->actlen;
981 if (err == USBD_STALLED) {
983 * The control endpoint has stalled. Control endpoints
984 * should not halt, but some may do so anyway so clear
985 * any halt condition.
987 usb_device_request_t treq;
992 treq.bmRequestType = UT_READ_ENDPOINT;
993 treq.bRequest = UR_GET_STATUS;
994 USETW(treq.wValue, 0);
995 USETW(treq.wIndex, 0);
996 USETW(treq.wLength, sizeof(usb_status_t));
997 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
998 &treq, &status,sizeof(usb_status_t),
1000 nerr = usbd_sync_transfer(xfer);
1003 s = UGETW(status.wStatus);
1004 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1005 if (!(s & UES_HALT))
1007 treq.bmRequestType = UT_WRITE_ENDPOINT;
1008 treq.bRequest = UR_CLEAR_FEATURE;
1009 USETW(treq.wValue, UF_ENDPOINT_HALT);
1010 USETW(treq.wIndex, 0);
1011 USETW(treq.wLength, 0);
1012 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1013 &treq, &status, 0, 0, 0);
1014 nerr = usbd_sync_transfer(xfer);
1020 usbd_free_xfer(xfer);
1025 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1028 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1029 if (xfer->actlen > xfer->length)
1030 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1031 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1032 xfer->pipe->device->address,
1033 xfer->request.bmRequestType,
1034 xfer->request.bRequest, UGETW(xfer->request.wValue),
1035 UGETW(xfer->request.wIndex),
1036 UGETW(xfer->request.wLength),
1037 xfer->length, xfer->actlen));
1039 usbd_free_xfer(xfer);
1043 * Execute a request without waiting for completion.
1044 * Can be used from interrupt context.
1047 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1050 usbd_xfer_handle xfer;
1053 xfer = usbd_alloc_xfer(dev);
1055 return (USBD_NOMEM);
1056 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1057 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1058 err = usbd_transfer(xfer);
1059 if (err != USBD_IN_PROGRESS) {
1060 usbd_free_xfer(xfer);
1063 return (USBD_NORMAL_COMPLETION);
1066 const struct usbd_quirks *
1067 usbd_get_quirks(usbd_device_handle dev)
1071 printf("usbd_get_quirks: dev == NULL\n");
1075 return (dev->quirks);
1078 /* XXX do periodic free() of free list */
1081 * Called from keyboard driver when in polling mode.
1084 usbd_dopoll(usbd_interface_handle iface)
1086 iface->device->bus->methods->do_poll(iface->device->bus);
1090 usbd_set_polling(usbd_device_handle dev, int on)
1093 dev->bus->use_polling++;
1095 dev->bus->use_polling--;
1096 /* When polling we need to make sure there is nothing pending to do. */
1097 if (dev->bus->use_polling)
1098 dev->bus->methods->soft_intr(dev->bus);
1102 usb_endpoint_descriptor_t *
1103 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1105 struct usbd_endpoint *ep;
1108 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1109 ep = &iface->endpoints[i];
1110 if (ep->edesc->bEndpointAddress == address)
1111 return (iface->endpoints[i].edesc);
1117 * usbd_ratecheck() can limit the number of error messages that occurs.
1118 * When a device is unplugged it may take up to 0.25s for the hub driver
1119 * to notice it. If the driver continuosly tries to do I/O operations
1120 * this can generate a large number of messages.
1123 usbd_ratecheck(struct timeval *last)
1126 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1128 return (ratecheck(last, &errinterval));
1134 * Search for a vendor/product pair in an array. The item size is
1135 * given as an argument.
1137 const struct usb_devno *
1138 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1139 u_int16_t vendor, u_int16_t product)
1141 while (nentries-- > 0) {
1142 u_int16_t tproduct = tbl->ud_product;
1143 if (tbl->ud_vendor == vendor &&
1144 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1146 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1151 #if defined(__FreeBSD__)
1153 usbd_driver_load(module_t mod, int what, void *arg)
1155 /* XXX should implement something like a function that removes all generic devices */