1 /* $NetBSD: usbdi.c,v 1.106 2004/10/24 12:52:40 augustss Exp $ */
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #elif defined(__FreeBSD__)
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>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdivar.h>
65 #include <dev/usb/usb_mem.h>
66 #include <dev/usb/usb_quirks.h>
68 #if defined(__FreeBSD__)
70 #include <machine/clock.h>
71 #define delay(d) DELAY(d)
75 #define DPRINTF(x) if (usbdebug) logprintf x
76 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
83 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
84 Static void usbd_do_request_async_cb
85 (usbd_xfer_handle, usbd_private_handle, usbd_status);
86 Static void usbd_start_next(usbd_pipe_handle pipe);
87 Static usbd_status usbd_open_pipe_ival
88 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
89 Static int usbd_xfer_isread(usbd_xfer_handle xfer);
91 Static int usbd_nbuses = 0;
106 usbd_xfer_isread(usbd_xfer_handle xfer)
108 if (xfer->rqflags & URQ_REQUEST)
109 return (xfer->request.bmRequestType & UT_READ);
111 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
117 usbd_dump_iface(struct usbd_interface *iface)
119 printf("usbd_dump_iface: iface=%p\n", iface);
122 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
123 iface->device, iface->idesc, iface->index, iface->altindex,
128 usbd_dump_device(struct usbd_device *dev)
130 printf("usbd_dump_device: dev=%p\n", dev);
133 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
134 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
135 "power=%d langid=%d\n",
136 dev->address, dev->config, dev->depth, dev->speed,
137 dev->self_powered, dev->power, dev->langid);
141 usbd_dump_endpoint(struct usbd_endpoint *endp)
143 printf("usbd_dump_endpoint: endp=%p\n", endp);
146 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
148 printf(" bEndpointAddress=0x%02x\n",
149 endp->edesc->bEndpointAddress);
153 usbd_dump_queue(usbd_pipe_handle pipe)
155 usbd_xfer_handle xfer;
157 printf("usbd_dump_queue: pipe=%p\n", pipe);
158 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
159 printf(" xfer=%p\n", xfer);
164 usbd_dump_pipe(usbd_pipe_handle pipe)
166 printf("usbd_dump_pipe: pipe=%p\n", pipe);
169 usbd_dump_iface(pipe->iface);
170 usbd_dump_device(pipe->device);
171 usbd_dump_endpoint(pipe->endpoint);
172 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
173 pipe->refcnt, pipe->running, pipe->aborting);
174 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
175 pipe->intrxfer, pipe->repeat, pipe->interval);
180 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
181 u_int8_t flags, usbd_pipe_handle *pipe)
183 return (usbd_open_pipe_ival(iface, address, flags, pipe,
184 USBD_DEFAULT_INTERVAL));
188 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
189 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
192 struct usbd_endpoint *ep;
196 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
197 iface, address, flags));
199 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
200 ep = &iface->endpoints[i];
201 if (ep->edesc == NULL)
202 return (USBD_IOERROR);
203 if (ep->edesc->bEndpointAddress == address)
206 return (USBD_BAD_ADDRESS);
208 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
209 return (USBD_IN_USE);
210 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
213 LIST_INSERT_HEAD(&iface->pipes, p, next);
215 return (USBD_NORMAL_COMPLETION);
219 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
220 u_int8_t flags, usbd_pipe_handle *pipe,
221 usbd_private_handle priv, void *buffer, u_int32_t len,
222 usbd_callback cb, int ival)
225 usbd_xfer_handle xfer;
226 usbd_pipe_handle ipipe;
228 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
229 address, flags, len));
231 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
235 xfer = usbd_alloc_xfer(iface->device);
240 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
241 USBD_NO_TIMEOUT, cb);
242 ipipe->intrxfer = xfer;
244 err = usbd_transfer(xfer);
246 if (err != USBD_IN_PROGRESS && err)
248 return (USBD_NORMAL_COMPLETION);
251 ipipe->intrxfer = NULL;
253 usbd_free_xfer(xfer);
255 usbd_close_pipe(ipipe);
260 usbd_close_pipe(usbd_pipe_handle pipe)
264 printf("usbd_close_pipe: pipe==NULL\n");
265 return (USBD_NORMAL_COMPLETION);
269 if (--pipe->refcnt != 0)
270 return (USBD_NORMAL_COMPLETION);
271 if (! SIMPLEQ_EMPTY(&pipe->queue))
272 return (USBD_PENDING_REQUESTS);
273 LIST_REMOVE(pipe, next);
274 pipe->endpoint->refcnt--;
275 pipe->methods->close(pipe);
276 if (pipe->intrxfer != NULL)
277 usbd_free_xfer(pipe->intrxfer);
279 return (USBD_NORMAL_COMPLETION);
283 usbd_transfer(usbd_xfer_handle xfer)
285 usbd_pipe_handle pipe = xfer->pipe;
286 usb_dma_t *dmap = &xfer->dmabuf;
291 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
292 xfer, xfer->flags, pipe, pipe->running));
295 usbd_dump_queue(pipe);
300 return (USBD_CANCELLED);
303 /* If there is no buffer, allocate one. */
304 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
305 struct usbd_bus *bus = pipe->device->bus;
308 if (xfer->rqflags & URQ_AUTO_DMABUF)
309 printf("usbd_transfer: has old buffer!\n");
311 err = bus->methods->allocm(bus, dmap, size);
314 xfer->rqflags |= URQ_AUTO_DMABUF;
317 /* Copy data if going out. */
318 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
319 !usbd_xfer_isread(xfer))
320 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
322 err = pipe->methods->transfer(xfer);
324 if (err != USBD_IN_PROGRESS && err) {
325 /* The transfer has not been queued, so free buffer. */
326 if (xfer->rqflags & URQ_AUTO_DMABUF) {
327 struct usbd_bus *bus = pipe->device->bus;
329 bus->methods->freem(bus, &xfer->dmabuf);
330 xfer->rqflags &= ~URQ_AUTO_DMABUF;
334 if (!(xfer->flags & USBD_SYNCHRONOUS))
337 /* Sync transfer, wait for completion. */
338 if (err != USBD_IN_PROGRESS)
342 if (pipe->device->bus->use_polling)
343 panic("usbd_transfer: not done");
344 tsleep(xfer, PRIBIO, "usbsyn", 0);
347 return (xfer->status);
350 /* Like usbd_transfer(), but waits for completion. */
352 usbd_sync_transfer(usbd_xfer_handle xfer)
354 xfer->flags |= USBD_SYNCHRONOUS;
355 return (usbd_transfer(xfer));
359 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
361 struct usbd_bus *bus = xfer->device->bus;
365 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
366 printf("usbd_alloc_buffer: xfer already has a buffer\n");
368 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
371 xfer->rqflags |= URQ_DEV_DMABUF;
372 return (KERNADDR(&xfer->dmabuf, 0));
376 usbd_free_buffer(usbd_xfer_handle xfer)
379 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
380 printf("usbd_free_buffer: no buffer\n");
384 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
385 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
389 usbd_get_buffer(usbd_xfer_handle xfer)
391 if (!(xfer->rqflags & URQ_DEV_DMABUF))
393 return (KERNADDR(&xfer->dmabuf, 0));
397 usbd_alloc_xfer(usbd_device_handle dev)
399 usbd_xfer_handle xfer;
401 xfer = dev->bus->methods->allocx(dev->bus);
405 usb_callout_init(xfer->timeout_handle);
406 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
411 usbd_free_xfer(usbd_xfer_handle xfer)
413 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
414 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
415 usbd_free_buffer(xfer);
416 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
417 if (callout_pending(&xfer->timeout_handle)) {
418 callout_stop(&xfer->timeout_handle);
419 printf("usbd_free_xfer: timout_handle pending");
422 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
423 return (USBD_NORMAL_COMPLETION);
427 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
428 usbd_private_handle priv, void *buffer, u_int32_t length,
429 u_int16_t flags, u_int32_t timeout,
430 usbd_callback callback)
434 xfer->buffer = buffer;
435 xfer->length = length;
438 xfer->timeout = timeout;
439 xfer->status = USBD_NOT_STARTED;
440 xfer->callback = callback;
441 xfer->rqflags &= ~URQ_REQUEST;
446 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
447 usbd_private_handle priv, u_int32_t timeout,
448 usb_device_request_t *req, void *buffer,
449 u_int32_t length, u_int16_t flags,
450 usbd_callback callback)
452 xfer->pipe = dev->default_pipe;
454 xfer->buffer = buffer;
455 xfer->length = length;
458 xfer->timeout = timeout;
459 xfer->status = USBD_NOT_STARTED;
460 xfer->callback = callback;
461 xfer->request = *req;
462 xfer->rqflags |= URQ_REQUEST;
467 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
468 usbd_private_handle priv, u_int16_t *frlengths,
469 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
477 xfer->timeout = USBD_NO_TIMEOUT;
478 xfer->status = USBD_NOT_STARTED;
479 xfer->callback = callback;
480 xfer->rqflags &= ~URQ_REQUEST;
481 xfer->frlengths = frlengths;
482 xfer->nframes = nframes;
486 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
487 void **buffer, u_int32_t *count, usbd_status *status)
492 *buffer = xfer->buffer;
494 *count = xfer->actlen;
496 *status = xfer->status;
499 usb_config_descriptor_t *
500 usbd_get_config_descriptor(usbd_device_handle dev)
504 printf("usbd_get_config_descriptor: dev == NULL\n");
512 usbd_get_speed(usbd_device_handle dev)
517 usb_interface_descriptor_t *
518 usbd_get_interface_descriptor(usbd_interface_handle iface)
522 printf("usbd_get_interface_descriptor: dev == NULL\n");
526 return (iface->idesc);
529 usb_device_descriptor_t *
530 usbd_get_device_descriptor(usbd_device_handle dev)
532 return (&dev->ddesc);
535 usb_endpoint_descriptor_t *
536 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
538 if (index >= iface->idesc->bNumEndpoints)
540 return (iface->endpoints[index].edesc);
544 usbd_abort_pipe(usbd_pipe_handle pipe)
551 printf("usbd_close_pipe: pipe==NULL\n");
552 return (USBD_NORMAL_COMPLETION);
556 err = usbd_ar_pipe(pipe);
562 usbd_abort_default_pipe(usbd_device_handle dev)
564 return (usbd_abort_pipe(dev->default_pipe));
568 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
570 usbd_device_handle dev = pipe->device;
571 usb_device_request_t req;
574 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
577 * Clearing en endpoint stall resets the endpoint toggle, so
578 * do the same to the HC toggle.
580 pipe->methods->cleartoggle(pipe);
582 req.bmRequestType = UT_WRITE_ENDPOINT;
583 req.bRequest = UR_CLEAR_FEATURE;
584 USETW(req.wValue, UF_ENDPOINT_HALT);
585 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
586 USETW(req.wLength, 0);
587 err = usbd_do_request(dev, &req, 0);
589 XXX should we do this?
591 pipe->state = USBD_PIPE_ACTIVE;
592 /* XXX activate pipe */
599 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
601 usbd_device_handle dev = pipe->device;
602 usb_device_request_t req;
605 pipe->methods->cleartoggle(pipe);
607 req.bmRequestType = UT_WRITE_ENDPOINT;
608 req.bRequest = UR_CLEAR_FEATURE;
609 USETW(req.wValue, UF_ENDPOINT_HALT);
610 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
611 USETW(req.wLength, 0);
612 err = usbd_do_request_async(dev, &req, 0);
617 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
619 pipe->methods->cleartoggle(pipe);
623 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
626 if (iface == NULL || iface->idesc == NULL) {
627 printf("usbd_endpoint_count: NULL pointer\n");
631 *count = iface->idesc->bNumEndpoints;
632 return (USBD_NORMAL_COMPLETION);
636 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
638 if (dev->cdesc == NULL)
639 return (USBD_NOT_CONFIGURED);
640 *count = dev->cdesc->bNumInterface;
641 return (USBD_NORMAL_COMPLETION);
645 usbd_interface2device_handle(usbd_interface_handle iface,
646 usbd_device_handle *dev)
648 *dev = iface->device;
652 usbd_device2interface_handle(usbd_device_handle dev,
653 u_int8_t ifaceno, usbd_interface_handle *iface)
655 if (dev->cdesc == NULL)
656 return (USBD_NOT_CONFIGURED);
657 if (ifaceno >= dev->cdesc->bNumInterface)
659 *iface = &dev->ifaces[ifaceno];
660 return (USBD_NORMAL_COMPLETION);
664 usbd_pipe2device_handle(usbd_pipe_handle pipe)
666 return (pipe->device);
671 usbd_set_interface(usbd_interface_handle iface, int altidx)
673 usb_device_request_t req;
677 if (LIST_FIRST(&iface->pipes) != 0)
678 return (USBD_IN_USE);
680 endpoints = iface->endpoints;
681 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
685 /* new setting works, we can free old endpoints */
686 if (endpoints != NULL)
687 free(endpoints, M_USB);
690 if (iface->idesc == NULL) {
691 printf("usbd_set_interface: NULL pointer\n");
696 req.bmRequestType = UT_WRITE_INTERFACE;
697 req.bRequest = UR_SET_INTERFACE;
698 USETW(req.wValue, iface->idesc->bAlternateSetting);
699 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
700 USETW(req.wLength, 0);
701 return (usbd_do_request(iface->device, &req, 0));
705 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
707 char *p = (char *)cdesc;
708 char *end = p + UGETW(cdesc->wTotalLength);
709 usb_interface_descriptor_t *d;
712 for (n = 0; p < end; p += d->bLength) {
713 d = (usb_interface_descriptor_t *)p;
714 if (p + d->bLength <= end &&
715 d->bDescriptorType == UDESC_INTERFACE &&
716 d->bInterfaceNumber == ifaceno)
723 usbd_get_interface_altindex(usbd_interface_handle iface)
725 return (iface->altindex);
729 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
731 usb_device_request_t req;
733 req.bmRequestType = UT_READ_INTERFACE;
734 req.bRequest = UR_GET_INTERFACE;
735 USETW(req.wValue, 0);
736 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
737 USETW(req.wLength, 1);
738 return (usbd_do_request(iface->device, &req, aiface));
741 /*** Internal routines ***/
743 /* Dequeue all pipe operations, called at splusb(). */
745 usbd_ar_pipe(usbd_pipe_handle pipe)
747 usbd_xfer_handle xfer;
751 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
754 usbd_dump_queue(pipe);
758 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
759 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
760 pipe, xfer, pipe->methods));
761 /* Make the HC abort it (and invoke the callback). */
762 pipe->methods->abort(xfer);
763 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
766 return (USBD_NORMAL_COMPLETION);
769 /* Called at splusb() */
771 usb_transfer_complete(usbd_xfer_handle xfer)
773 usbd_pipe_handle pipe = xfer->pipe;
774 usb_dma_t *dmap = &xfer->dmabuf;
775 int sync = xfer->flags & USBD_SYNCHRONOUS;
776 int erred = xfer->status == USBD_CANCELLED ||
777 xfer->status == USBD_TIMEOUT;
778 int repeat = pipe->repeat;
783 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
784 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
786 if (xfer->busy_free != XFER_ONQU) {
787 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
788 xfer, xfer->busy_free);
795 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
799 polling = pipe->device->bus->use_polling;
804 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
805 usbd_xfer_isread(xfer)) {
807 if (xfer->actlen > xfer->length) {
808 printf("usb_transfer_complete: actlen > len %d > %d\n",
809 xfer->actlen, xfer->length);
810 xfer->actlen = xfer->length;
813 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
816 /* if we allocated the buffer in usbd_transfer() we free it here. */
817 if (xfer->rqflags & URQ_AUTO_DMABUF) {
819 struct usbd_bus *bus = pipe->device->bus;
820 bus->methods->freem(bus, dmap);
821 xfer->rqflags &= ~URQ_AUTO_DMABUF;
826 /* Remove request from queue. */
828 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
829 printf("usb_transfer_complete: bad dequeue %p != %p\n",
830 xfer, SIMPLEQ_FIRST(&pipe->queue));
831 xfer->busy_free = XFER_BUSY;
833 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
835 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
836 repeat, SIMPLEQ_FIRST(&pipe->queue)));
838 /* Count completed transfers. */
839 ++pipe->device->bus->stats.uds_requests
840 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
843 if (!xfer->status && xfer->actlen < xfer->length &&
844 !(xfer->flags & USBD_SHORT_XFER_OK)) {
845 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
846 xfer->actlen, xfer->length));
847 xfer->status = USBD_SHORT_XFER;
851 xfer->callback(xfer, xfer->priv, xfer->status);
854 if (pipe->methods->done != NULL)
855 pipe->methods->done(xfer);
857 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
859 pipe->methods->done(xfer);
862 if (sync && !polling)
866 /* XXX should we stop the queue on all errors? */
867 if (erred && pipe->iface != NULL) /* not control pipe */
870 usbd_start_next(pipe);
875 usb_insert_transfer(usbd_xfer_handle xfer)
877 usbd_pipe_handle pipe = xfer->pipe;
881 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
882 pipe, pipe->running, xfer->timeout));
884 if (xfer->busy_free != XFER_BUSY) {
885 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
886 xfer, xfer->busy_free);
889 xfer->busy_free = XFER_ONQU;
892 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
894 err = USBD_IN_PROGRESS;
897 err = USBD_NORMAL_COMPLETION;
903 /* Called at splusb() */
905 usbd_start_next(usbd_pipe_handle pipe)
907 usbd_xfer_handle xfer;
914 printf("usbd_start_next: pipe == NULL\n");
917 if (pipe->methods == NULL || pipe->methods->start == NULL) {
918 printf("usbd_start_next: pipe=%p no start method\n", pipe);
923 /* Get next request in queue. */
924 xfer = SIMPLEQ_FIRST(&pipe->queue);
925 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
929 err = pipe->methods->start(xfer);
930 if (err != USBD_IN_PROGRESS) {
931 printf("usbd_start_next: error=%d\n", err);
939 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
941 return (usbd_do_request_flags(dev, req, data, 0, 0,
942 USBD_DEFAULT_TIMEOUT));
946 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
947 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
949 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
950 data, flags, actlen, timo));
954 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
955 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
958 usbd_xfer_handle xfer;
962 #if defined(__i386__) && defined(__FreeBSD__)
963 KASSERT(curthread->td_intr_nesting_level == 0,
964 ("usbd_do_request: in interrupt context"));
966 if (dev->bus->intr_context) {
967 printf("usbd_do_request: not in process context\n");
972 xfer = usbd_alloc_xfer(dev);
975 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
976 data, UGETW(req->wLength), flags, 0);
978 err = usbd_sync_transfer(xfer);
979 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
980 if (xfer->actlen > xfer->length)
981 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
982 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
983 dev->address, xfer->request.bmRequestType,
984 xfer->request.bRequest, UGETW(xfer->request.wValue),
985 UGETW(xfer->request.wIndex),
986 UGETW(xfer->request.wLength),
987 xfer->length, xfer->actlen));
990 *actlen = xfer->actlen;
991 if (err == USBD_STALLED) {
993 * The control endpoint has stalled. Control endpoints
994 * should not halt, but some may do so anyway so clear
995 * any halt condition.
997 usb_device_request_t treq;
1002 treq.bmRequestType = UT_READ_ENDPOINT;
1003 treq.bRequest = UR_GET_STATUS;
1004 USETW(treq.wValue, 0);
1005 USETW(treq.wIndex, 0);
1006 USETW(treq.wLength, sizeof(usb_status_t));
1007 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1008 &treq, &status,sizeof(usb_status_t),
1010 nerr = usbd_sync_transfer(xfer);
1013 s = UGETW(status.wStatus);
1014 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1015 if (!(s & UES_HALT))
1017 treq.bmRequestType = UT_WRITE_ENDPOINT;
1018 treq.bRequest = UR_CLEAR_FEATURE;
1019 USETW(treq.wValue, UF_ENDPOINT_HALT);
1020 USETW(treq.wIndex, 0);
1021 USETW(treq.wLength, 0);
1022 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1023 &treq, &status, 0, 0, 0);
1024 nerr = usbd_sync_transfer(xfer);
1030 usbd_free_xfer(xfer);
1035 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1038 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1039 if (xfer->actlen > xfer->length)
1040 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1041 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1042 xfer->pipe->device->address,
1043 xfer->request.bmRequestType,
1044 xfer->request.bRequest, UGETW(xfer->request.wValue),
1045 UGETW(xfer->request.wIndex),
1046 UGETW(xfer->request.wLength),
1047 xfer->length, xfer->actlen));
1049 usbd_free_xfer(xfer);
1053 * Execute a request without waiting for completion.
1054 * Can be used from interrupt context.
1057 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1060 usbd_xfer_handle xfer;
1063 xfer = usbd_alloc_xfer(dev);
1065 return (USBD_NOMEM);
1066 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1067 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1068 err = usbd_transfer(xfer);
1069 if (err != USBD_IN_PROGRESS && err) {
1070 usbd_free_xfer(xfer);
1073 return (USBD_NORMAL_COMPLETION);
1076 const struct usbd_quirks *
1077 usbd_get_quirks(usbd_device_handle dev)
1081 printf("usbd_get_quirks: dev == NULL\n");
1085 return (dev->quirks);
1088 /* XXX do periodic free() of free list */
1091 * Called from keyboard driver when in polling mode.
1094 usbd_dopoll(usbd_interface_handle iface)
1096 iface->device->bus->methods->do_poll(iface->device->bus);
1100 usbd_set_polling(usbd_device_handle dev, int on)
1103 dev->bus->use_polling++;
1105 dev->bus->use_polling--;
1106 /* When polling we need to make sure there is nothing pending to do. */
1107 if (dev->bus->use_polling)
1108 dev->bus->methods->soft_intr(dev->bus);
1112 usb_endpoint_descriptor_t *
1113 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1115 struct usbd_endpoint *ep;
1118 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1119 ep = &iface->endpoints[i];
1120 if (ep->edesc->bEndpointAddress == address)
1121 return (iface->endpoints[i].edesc);
1127 * usbd_ratecheck() can limit the number of error messages that occurs.
1128 * When a device is unplugged it may take up to 0.25s for the hub driver
1129 * to notice it. If the driver continuosly tries to do I/O operations
1130 * this can generate a large number of messages.
1133 usbd_ratecheck(struct timeval *last)
1135 if (last->tv_sec == time_second)
1137 last->tv_sec = time_second;
1142 * Search for a vendor/product pair in an array. The item size is
1143 * given as an argument.
1145 const struct usb_devno *
1146 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1147 u_int16_t vendor, u_int16_t product)
1149 while (nentries-- > 0) {
1150 u_int16_t tproduct = tbl->ud_product;
1151 if (tbl->ud_vendor == vendor &&
1152 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1154 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1161 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1163 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1165 iter->cur = (const uByte *)cd;
1166 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1169 const usb_descriptor_t *
1170 usb_desc_iter_next(usbd_desc_iter_t *iter)
1172 const usb_descriptor_t *desc;
1174 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1175 if (iter->cur != iter->end)
1176 printf("usb_desc_iter_next: bad descriptor\n");
1179 desc = (const usb_descriptor_t *)iter->cur;
1180 if (desc->bLength == 0) {
1181 printf("usb_desc_iter_next: descriptor length = 0\n");
1184 iter->cur += desc->bLength;
1185 if (iter->cur > iter->end) {
1186 printf("usb_desc_iter_next: descriptor length too large\n");
1193 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1195 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1196 usb_string_descriptor_t us;
1205 return (USBD_INVAL);
1206 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1207 return (USBD_STALLED);
1208 if (dev->langid == USBD_NOLANG) {
1209 /* Set up default language */
1210 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1212 if (err || size < 4) {
1213 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1214 dev->langid = 0; /* Well, just pick something then */
1216 /* Pick the first language as the default. */
1217 dev->langid = UGETW(us.bString[0]);
1220 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1225 for (i = 0; i < n; i++) {
1226 c = UGETW(us.bString[i]);
1227 /* Convert from Unicode, handle buggy strings. */
1228 if ((c & 0xff00) == 0)
1230 else if ((c & 0x00ff) == 0 && swap)
1236 return (USBD_NORMAL_COMPLETION);
1239 #if defined(__FreeBSD__)
1241 usbd_driver_load(module_t mod, int what, void *arg)
1243 /* XXX should implement something like a function that removes all generic devices */