1 /* $NetBSD: usbdi.c,v 1.106 2004/10/24 12:52:40 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.91.2.1 2005/12/15 00:36:00 iedowse 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 (lennart@augustsson.net) 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 #include <sys/kernel.h>
44 #include <sys/module.h>
47 #if defined(DIAGNOSTIC) && defined(__i386__)
48 #include <machine/cpu.h>
50 #include <sys/malloc.h>
52 #include <sys/thread2.h>
54 #include <bus/usb/usb.h>
55 #include <bus/usb/usbdi.h>
56 #include <bus/usb/usbdi_util.h>
57 #include <bus/usb/usbdivar.h>
58 #include <bus/usb/usb_mem.h>
59 #include <bus/usb/usb_quirks.h>
61 #define delay(d) DELAY(d)
64 #define DPRINTF(x) if (usbdebug) kprintf x
65 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
72 static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
73 static void usbd_do_request_async_cb
74 (usbd_xfer_handle, usbd_private_handle, usbd_status);
75 static void usbd_start_next(usbd_pipe_handle pipe);
76 static usbd_status usbd_open_pipe_ival
77 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
78 static int usbd_xfer_isread(usbd_xfer_handle xfer);
80 static int usbd_nbuses = 0;
95 usbd_xfer_isread(usbd_xfer_handle xfer)
97 if (xfer->rqflags & URQ_REQUEST)
98 return (xfer->request.bmRequestType & UT_READ);
100 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
106 usbd_dump_iface(struct usbd_interface *iface)
108 kprintf("usbd_dump_iface: iface=%p\n", iface);
111 kprintf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
112 iface->device, iface->idesc, iface->index, iface->altindex,
117 usbd_dump_device(struct usbd_device *dev)
119 kprintf("usbd_dump_device: dev=%p\n", dev);
122 kprintf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
123 kprintf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
124 "power=%d langid=%d\n",
125 dev->address, dev->config, dev->depth, dev->speed,
126 dev->self_powered, dev->power, dev->langid);
130 usbd_dump_endpoint(struct usbd_endpoint *endp)
132 kprintf("usbd_dump_endpoint: endp=%p\n", endp);
135 kprintf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
137 kprintf(" bEndpointAddress=0x%02x\n",
138 endp->edesc->bEndpointAddress);
142 usbd_dump_queue(usbd_pipe_handle pipe)
144 usbd_xfer_handle xfer;
146 kprintf("usbd_dump_queue: pipe=%p\n", pipe);
147 STAILQ_FOREACH(xfer, &pipe->queue, next) {
148 kprintf(" xfer=%p\n", xfer);
153 usbd_dump_pipe(usbd_pipe_handle pipe)
155 kprintf("usbd_dump_pipe: pipe=%p\n", pipe);
158 usbd_dump_iface(pipe->iface);
159 usbd_dump_device(pipe->device);
160 usbd_dump_endpoint(pipe->endpoint);
161 kprintf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
162 pipe->refcnt, pipe->running, pipe->aborting);
163 kprintf(" intrxfer=%p, repeat=%d, interval=%d\n",
164 pipe->intrxfer, pipe->repeat, pipe->interval);
169 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
170 u_int8_t flags, usbd_pipe_handle *pipe)
172 return (usbd_open_pipe_ival(iface, address, flags, pipe,
173 USBD_DEFAULT_INTERVAL));
177 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
178 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
181 struct usbd_endpoint *ep;
185 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
186 iface, address, flags));
188 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
189 ep = &iface->endpoints[i];
190 if (ep->edesc == NULL)
191 return (USBD_IOERROR);
192 if (ep->edesc->bEndpointAddress == address)
195 return (USBD_BAD_ADDRESS);
197 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
198 return (USBD_IN_USE);
199 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
202 LIST_INSERT_HEAD(&iface->pipes, p, next);
204 return (USBD_NORMAL_COMPLETION);
208 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
209 u_int8_t flags, usbd_pipe_handle *pipe,
210 usbd_private_handle priv, void *buffer, u_int32_t len,
211 usbd_callback cb, int ival)
214 usbd_xfer_handle xfer;
215 usbd_pipe_handle ipipe;
217 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
218 address, flags, len));
220 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
224 xfer = usbd_alloc_xfer(iface->device);
229 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
230 USBD_NO_TIMEOUT, cb);
231 ipipe->intrxfer = xfer;
233 err = usbd_transfer(xfer);
235 if (err != USBD_IN_PROGRESS && err)
237 return (USBD_NORMAL_COMPLETION);
240 ipipe->intrxfer = NULL;
242 usbd_free_xfer(xfer);
244 usbd_close_pipe(ipipe);
249 usbd_close_pipe(usbd_pipe_handle pipe)
253 kprintf("usbd_close_pipe: pipe==NULL\n");
254 return (USBD_NORMAL_COMPLETION);
258 if (--pipe->refcnt != 0)
259 return (USBD_NORMAL_COMPLETION);
260 if (! STAILQ_EMPTY(&pipe->queue))
261 return (USBD_PENDING_REQUESTS);
262 LIST_REMOVE(pipe, next);
263 pipe->endpoint->refcnt--;
264 pipe->methods->close(pipe);
265 if (pipe->intrxfer != NULL)
266 usbd_free_xfer(pipe->intrxfer);
268 return (USBD_NORMAL_COMPLETION);
272 usbd_transfer(usbd_xfer_handle xfer)
274 usbd_pipe_handle pipe = xfer->pipe;
275 usb_dma_t *dmap = &xfer->dmabuf;
280 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
281 xfer, xfer->flags, pipe, pipe->running));
284 usbd_dump_queue(pipe);
289 return (USBD_CANCELLED);
292 /* If there is no buffer, allocate one. */
293 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
294 struct usbd_bus *bus = pipe->device->bus;
297 if (xfer->rqflags & URQ_AUTO_DMABUF)
298 kprintf("usbd_transfer: has old buffer!\n");
300 err = bus->methods->allocm(bus, dmap, size);
303 xfer->rqflags |= URQ_AUTO_DMABUF;
306 /* Copy data if going out. */
307 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
308 !usbd_xfer_isread(xfer))
309 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
311 err = pipe->methods->transfer(xfer);
313 if (err != USBD_IN_PROGRESS && err) {
314 /* The transfer has not been queued, so free buffer. */
315 if (xfer->rqflags & URQ_AUTO_DMABUF) {
316 struct usbd_bus *bus = pipe->device->bus;
318 bus->methods->freem(bus, &xfer->dmabuf);
319 xfer->rqflags &= ~URQ_AUTO_DMABUF;
323 if (!(xfer->flags & USBD_SYNCHRONOUS))
326 /* Sync transfer, wait for completion. */
327 if (err != USBD_IN_PROGRESS)
331 while (!xfer->done) {
332 if (pipe->device->bus->use_polling) {
333 pipe->device->bus->methods->do_poll(pipe->device->bus);
335 kprintf("Warning: usbd_transfer: not done, "
339 usbd_abort_pipe(pipe);
343 tsleep(xfer, 0, "usbsyn", hz);
348 return (xfer->status);
351 /* Like usbd_transfer(), but waits for completion. */
353 usbd_sync_transfer(usbd_xfer_handle xfer)
355 xfer->flags |= USBD_SYNCHRONOUS;
356 return (usbd_transfer(xfer));
360 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
362 struct usbd_bus *bus = xfer->device->bus;
366 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
367 kprintf("usbd_alloc_buffer: xfer already has a buffer\n");
369 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
371 device_printf(bus->bdev, "Unable to allocate %u contig\n",
375 xfer->rqflags |= URQ_DEV_DMABUF;
376 return (KERNADDR(&xfer->dmabuf, 0));
380 usbd_free_buffer(usbd_xfer_handle xfer)
383 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
384 kprintf("usbd_free_buffer: no buffer\n");
388 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
389 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
393 usbd_get_buffer(usbd_xfer_handle xfer)
395 if (!(xfer->rqflags & URQ_DEV_DMABUF))
397 return (KERNADDR(&xfer->dmabuf, 0));
401 usbd_alloc_xfer(usbd_device_handle dev)
403 usbd_xfer_handle xfer;
405 xfer = dev->bus->methods->allocx(dev->bus);
409 callout_init(&xfer->timeout_handle);
410 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
415 usbd_free_xfer(usbd_xfer_handle xfer)
417 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
418 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
419 usbd_free_buffer(xfer);
420 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
421 return (USBD_NORMAL_COMPLETION);
425 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
426 usbd_private_handle priv, void *buffer, u_int32_t length,
427 u_int16_t flags, u_int32_t timeout,
428 usbd_callback callback)
432 xfer->buffer = buffer;
433 xfer->length = length;
436 xfer->timeout = timeout;
437 xfer->status = USBD_NOT_STARTED;
438 xfer->callback = callback;
439 xfer->rqflags &= ~URQ_REQUEST;
444 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
445 usbd_private_handle priv, u_int32_t timeout,
446 usb_device_request_t *req, void *buffer,
447 u_int32_t length, u_int16_t flags,
448 usbd_callback callback)
450 xfer->pipe = dev->default_pipe;
452 xfer->buffer = buffer;
453 xfer->length = length;
456 xfer->timeout = timeout;
457 xfer->status = USBD_NOT_STARTED;
458 xfer->callback = callback;
459 xfer->request = *req;
460 xfer->rqflags |= URQ_REQUEST;
465 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
466 usbd_private_handle priv, u_int16_t *frlengths,
467 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
475 xfer->timeout = USBD_NO_TIMEOUT;
476 xfer->status = USBD_NOT_STARTED;
477 xfer->callback = callback;
478 xfer->rqflags &= ~URQ_REQUEST;
479 xfer->frlengths = frlengths;
480 xfer->nframes = nframes;
484 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
485 void **buffer, u_int32_t *count, usbd_status *status)
490 *buffer = xfer->buffer;
492 *count = xfer->actlen;
494 *status = xfer->status;
498 usbd_get_speed(usbd_device_handle dev)
503 usb_config_descriptor_t *
504 usbd_get_config_descriptor(usbd_device_handle dev)
508 kprintf("usbd_get_config_descriptor: dev == NULL\n");
515 usb_interface_descriptor_t *
516 usbd_get_interface_descriptor(usbd_interface_handle iface)
520 kprintf("usbd_get_interface_descriptor: dev == NULL\n");
524 return (iface->idesc);
527 usb_device_descriptor_t *
528 usbd_get_device_descriptor(usbd_device_handle dev)
530 return (&dev->ddesc);
533 usb_endpoint_descriptor_t *
534 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
536 if (index >= iface->idesc->bNumEndpoints)
538 return (iface->endpoints[index].edesc);
542 usbd_abort_pipe(usbd_pipe_handle pipe)
548 kprintf("usbd_close_pipe: pipe==NULL\n");
549 return (USBD_NORMAL_COMPLETION);
553 err = usbd_ar_pipe(pipe);
559 usbd_abort_default_pipe(usbd_device_handle dev)
561 return (usbd_abort_pipe(dev->default_pipe));
565 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
567 usbd_device_handle dev = pipe->device;
568 usb_device_request_t req;
571 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
574 * Clearing en endpoint stall resets the endpoint toggle, so
575 * do the same to the HC toggle.
577 pipe->methods->cleartoggle(pipe);
579 req.bmRequestType = UT_WRITE_ENDPOINT;
580 req.bRequest = UR_CLEAR_FEATURE;
581 USETW(req.wValue, UF_ENDPOINT_HALT);
582 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
583 USETW(req.wLength, 0);
584 err = usbd_do_request(dev, &req, 0);
586 XXX should we do this?
588 pipe->state = USBD_PIPE_ACTIVE;
589 /* XXX activate pipe */
596 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
598 usbd_device_handle dev = pipe->device;
599 usb_device_request_t req;
602 pipe->methods->cleartoggle(pipe);
604 req.bmRequestType = UT_WRITE_ENDPOINT;
605 req.bRequest = UR_CLEAR_FEATURE;
606 USETW(req.wValue, UF_ENDPOINT_HALT);
607 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
608 USETW(req.wLength, 0);
609 err = usbd_do_request_async(dev, &req, 0);
614 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
616 pipe->methods->cleartoggle(pipe);
620 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
623 if (iface == NULL || iface->idesc == NULL) {
624 kprintf("usbd_endpoint_count: NULL pointer\n");
628 *count = iface->idesc->bNumEndpoints;
629 return (USBD_NORMAL_COMPLETION);
633 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
635 if (dev->cdesc == NULL)
636 return (USBD_NOT_CONFIGURED);
637 *count = dev->cdesc->bNumInterface;
638 return (USBD_NORMAL_COMPLETION);
642 usbd_interface2device_handle(usbd_interface_handle iface,
643 usbd_device_handle *dev)
645 *dev = iface->device;
649 usbd_device2interface_handle(usbd_device_handle dev,
650 u_int8_t ifaceno, usbd_interface_handle *iface)
652 if (dev->cdesc == NULL)
653 return (USBD_NOT_CONFIGURED);
654 if (ifaceno >= dev->cdesc->bNumInterface)
656 *iface = &dev->ifaces[ifaceno];
657 return (USBD_NORMAL_COMPLETION);
661 usbd_pipe2device_handle(usbd_pipe_handle pipe)
663 return (pipe->device);
668 usbd_set_interface(usbd_interface_handle iface, int altidx)
670 usb_device_request_t req;
674 if (LIST_FIRST(&iface->pipes) != 0)
675 return (USBD_IN_USE);
677 endpoints = iface->endpoints;
678 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
682 /* new setting works, we can free old endpoints */
683 if (endpoints != NULL)
684 kfree(endpoints, M_USB);
687 if (iface->idesc == NULL) {
688 kprintf("usbd_set_interface: NULL pointer\n");
693 req.bmRequestType = UT_WRITE_INTERFACE;
694 req.bRequest = UR_SET_INTERFACE;
695 USETW(req.wValue, iface->idesc->bAlternateSetting);
696 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
697 USETW(req.wLength, 0);
698 return (usbd_do_request(iface->device, &req, 0));
702 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
704 char *p = (char *)cdesc;
705 char *end = p + UGETW(cdesc->wTotalLength);
706 usb_interface_descriptor_t *d;
709 for (n = 0; p < end; p += d->bLength) {
710 d = (usb_interface_descriptor_t *)p;
711 if (p + d->bLength <= end &&
712 d->bDescriptorType == UDESC_INTERFACE &&
713 d->bInterfaceNumber == ifaceno)
720 usbd_get_interface_altindex(usbd_interface_handle iface)
722 return (iface->altindex);
726 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
728 usb_device_request_t req;
730 req.bmRequestType = UT_READ_INTERFACE;
731 req.bRequest = UR_GET_INTERFACE;
732 USETW(req.wValue, 0);
733 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
734 USETW(req.wLength, 1);
735 return (usbd_do_request(iface->device, &req, aiface));
738 /*** Internal routines ***/
740 /* Dequeue all pipe operations, called from critical section. */
742 usbd_ar_pipe(usbd_pipe_handle pipe)
744 usbd_xfer_handle xfer;
746 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
749 usbd_dump_queue(pipe);
753 while ((xfer = STAILQ_FIRST(&pipe->queue)) != NULL) {
754 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
755 pipe, xfer, pipe->methods));
756 /* Make the HC abort it (and invoke the callback). */
757 pipe->methods->abort(xfer);
758 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
761 return (USBD_NORMAL_COMPLETION);
764 /* Called from critical section */
766 usb_transfer_complete(usbd_xfer_handle xfer)
768 usbd_pipe_handle pipe = xfer->pipe;
769 usb_dma_t *dmap = &xfer->dmabuf;
770 int sync = xfer->flags & USBD_SYNCHRONOUS;
771 int erred = xfer->status == USBD_CANCELLED ||
772 xfer->status == USBD_TIMEOUT;
776 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
777 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
779 if (xfer->busy_free != XFER_ONQU) {
780 kprintf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
781 xfer, xfer->busy_free);
788 kprintf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
792 repeat = pipe->repeat;
793 polling = pipe->device->bus->use_polling;
798 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
799 usbd_xfer_isread(xfer)) {
801 if (xfer->actlen > xfer->length) {
802 kprintf("usb_transfer_complete: actlen > len %d > %d\n",
803 xfer->actlen, xfer->length);
804 xfer->actlen = xfer->length;
807 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
810 /* if we allocated the buffer in usbd_transfer() we free it here. */
811 if (xfer->rqflags & URQ_AUTO_DMABUF) {
813 struct usbd_bus *bus = pipe->device->bus;
814 bus->methods->freem(bus, dmap);
815 xfer->rqflags &= ~URQ_AUTO_DMABUF;
820 /* Remove request from queue. */
822 if (xfer != STAILQ_FIRST(&pipe->queue))
823 kprintf("usb_transfer_complete: bad dequeue %p != %p\n",
824 xfer, STAILQ_FIRST(&pipe->queue));
825 xfer->busy_free = XFER_BUSY;
827 STAILQ_REMOVE_HEAD(&pipe->queue, next);
829 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
830 repeat, STAILQ_FIRST(&pipe->queue)));
832 /* Count completed transfers. */
833 ++pipe->device->bus->stats.uds_requests
834 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
837 if (!xfer->status && xfer->actlen < xfer->length &&
838 !(xfer->flags & USBD_SHORT_XFER_OK)) {
839 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
840 xfer->actlen, xfer->length));
841 xfer->status = USBD_SHORT_XFER;
845 * For repeat operations, call the callback first, as the xfer
846 * will not go away and the "done" method may modify it. Otherwise
847 * reverse the order in case the callback wants to free or reuse
850 * USBD_CALLBACK_LAST is set by the keyboard driver to ensure
851 * that the xfer is restarted prior to doing the callback.
852 * Otherwise a CTL-ALT-ESC into the debugger will leave the
853 * xfer inactive and the keyboard will stop working.
855 if (repeat && (xfer->flags & USBD_CALLBACK_LAST) == 0) {
857 xfer->callback(xfer, xfer->priv, xfer->status);
858 pipe->methods->done(xfer);
860 int status = xfer->status; /* get this before restart */
861 pipe->methods->done(xfer); /* restart */
863 xfer->callback(xfer, xfer->priv, status);
866 if (sync && !polling)
870 /* XXX should we stop the queue on all errors? */
871 if (erred && pipe->iface != NULL) /* not control pipe */
874 usbd_start_next(pipe);
879 usb_insert_transfer(usbd_xfer_handle xfer)
881 usbd_pipe_handle pipe = xfer->pipe;
884 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
885 pipe, pipe->running, xfer->timeout));
887 if (xfer->busy_free != XFER_BUSY) {
888 kprintf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
889 xfer, xfer->busy_free);
892 xfer->busy_free = XFER_ONQU;
895 STAILQ_INSERT_TAIL(&pipe->queue, xfer, next);
897 err = USBD_IN_PROGRESS;
900 err = USBD_NORMAL_COMPLETION;
906 /* Called from critical section */
908 usbd_start_next(usbd_pipe_handle pipe)
910 usbd_xfer_handle xfer;
915 kprintf("usbd_start_next: pipe == NULL\n");
918 if (pipe->methods == NULL || pipe->methods->start == NULL) {
919 kprintf("usbd_start_next: pipe=%p no start method\n", pipe);
924 /* Get next request in queue. */
925 xfer = STAILQ_FIRST(&pipe->queue);
926 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
930 err = pipe->methods->start(xfer);
931 if (err != USBD_IN_PROGRESS) {
932 kprintf("usbd_start_next: error=%d\n", err);
940 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
942 return (usbd_do_request_flags(dev, req, data, 0, 0,
943 USBD_DEFAULT_TIMEOUT));
947 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
948 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
950 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
951 data, flags, actlen, timo));
955 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
956 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
959 usbd_xfer_handle xfer;
963 KASSERT(mycpu->gd_intr_nesting_level == 0,
964 ("usbd_do_request: in interrupt context"));
965 if (dev->bus->intr_context) {
966 kprintf("usbd_do_request: not in process context\n");
971 xfer = usbd_alloc_xfer(dev);
974 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
975 data, UGETW(req->wLength), flags, 0);
977 err = usbd_sync_transfer(xfer);
978 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
979 if (xfer->actlen > xfer->length)
980 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
981 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
982 dev->address, xfer->request.bmRequestType,
983 xfer->request.bRequest, UGETW(xfer->request.wValue),
984 UGETW(xfer->request.wIndex),
985 UGETW(xfer->request.wLength),
986 xfer->length, xfer->actlen));
989 *actlen = xfer->actlen;
990 if (err == USBD_STALLED) {
992 * The control endpoint has stalled. Control endpoints
993 * should not halt, but some may do so anyway so clear
994 * any halt condition.
996 usb_device_request_t treq;
1001 treq.bmRequestType = UT_READ_ENDPOINT;
1002 treq.bRequest = UR_GET_STATUS;
1003 USETW(treq.wValue, 0);
1004 USETW(treq.wIndex, 0);
1005 USETW(treq.wLength, sizeof(usb_status_t));
1006 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1007 &treq, &status,sizeof(usb_status_t),
1009 nerr = usbd_sync_transfer(xfer);
1012 s = UGETW(status.wStatus);
1013 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1014 if (!(s & UES_HALT))
1016 treq.bmRequestType = UT_WRITE_ENDPOINT;
1017 treq.bRequest = UR_CLEAR_FEATURE;
1018 USETW(treq.wValue, UF_ENDPOINT_HALT);
1019 USETW(treq.wIndex, 0);
1020 USETW(treq.wLength, 0);
1021 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1022 &treq, &status, 0, 0, 0);
1023 nerr = usbd_sync_transfer(xfer);
1029 usbd_free_xfer(xfer);
1034 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1037 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1038 if (xfer->actlen > xfer->length)
1039 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1040 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1041 xfer->pipe->device->address,
1042 xfer->request.bmRequestType,
1043 xfer->request.bRequest, UGETW(xfer->request.wValue),
1044 UGETW(xfer->request.wIndex),
1045 UGETW(xfer->request.wLength),
1046 xfer->length, xfer->actlen));
1048 usbd_free_xfer(xfer);
1052 * Execute a request without waiting for completion.
1053 * Can be used from interrupt context.
1056 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1059 usbd_xfer_handle xfer;
1062 xfer = usbd_alloc_xfer(dev);
1064 return (USBD_NOMEM);
1065 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1066 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1067 err = usbd_transfer(xfer);
1068 if (err != USBD_IN_PROGRESS && err) {
1069 usbd_free_xfer(xfer);
1072 return (USBD_NORMAL_COMPLETION);
1075 const struct usbd_quirks *
1076 usbd_get_quirks(usbd_device_handle dev)
1080 kprintf("usbd_get_quirks: dev == NULL\n");
1084 return (dev->quirks);
1087 /* XXX do periodic free() of free list */
1090 * Called from keyboard driver when in polling mode.
1093 usbd_dopoll(usbd_interface_handle iface)
1095 iface->device->bus->methods->do_poll(iface->device->bus);
1099 usbd_set_polling(usbd_device_handle dev, int on)
1102 dev->bus->use_polling++;
1104 dev->bus->use_polling--;
1105 /* When polling we need to make sure there is nothing pending to do. */
1106 if (dev->bus->use_polling)
1107 dev->bus->methods->soft_intr(dev->bus);
1111 usb_endpoint_descriptor_t *
1112 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1114 struct usbd_endpoint *ep;
1117 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1118 ep = &iface->endpoints[i];
1119 if (ep->edesc->bEndpointAddress == address)
1120 return (iface->endpoints[i].edesc);
1126 * usbd_ratecheck() can limit the number of error messages that occurs.
1127 * When a device is unplugged it may take up to 0.25s for the hub driver
1128 * to notice it. If the driver continuosly tries to do I/O operations
1129 * this can generate a large number of messages.
1132 usbd_ratecheck(struct timeval *last)
1134 if (last->tv_sec == time_uptime)
1136 last->tv_sec = time_uptime;
1141 * Search for a vendor/product pair in an array. The item size is
1142 * given as an argument.
1144 const struct usb_devno *
1145 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1146 u_int16_t vendor, u_int16_t product)
1148 while (nentries-- > 0) {
1149 u_int16_t tproduct = tbl->ud_product;
1150 if (tbl->ud_vendor == vendor &&
1151 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1153 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1160 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1162 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1164 iter->cur = (const uByte *)cd;
1165 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1168 const usb_descriptor_t *
1169 usb_desc_iter_next(usbd_desc_iter_t *iter)
1171 const usb_descriptor_t *desc;
1173 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1174 if (iter->cur != iter->end)
1175 kprintf("usb_desc_iter_next: bad descriptor\n");
1178 desc = (const usb_descriptor_t *)iter->cur;
1179 if (desc->bLength == 0) {
1180 kprintf("usb_desc_iter_next: descriptor length = 0\n");
1183 iter->cur += desc->bLength;
1184 if (iter->cur > iter->end) {
1185 kprintf("usb_desc_iter_next: descriptor length too large\n");
1192 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1194 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1195 usb_string_descriptor_t us;
1204 return (USBD_INVAL);
1205 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1206 return (USBD_STALLED);
1207 if (dev->langid == USBD_NOLANG) {
1208 /* Set up default language */
1209 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1211 if (err || size < 4) {
1212 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1213 dev->langid = 0; /* Well, just pick something then */
1215 /* Pick the first language as the default. */
1216 dev->langid = UGETW(us.bString[0]);
1219 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1224 for (i = 0; i < n; i++) {
1225 c = UGETW(us.bString[i]);
1226 /* Convert from Unicode, handle buggy strings. */
1227 if ((c & 0xff00) == 0)
1229 else if ((c & 0x00ff) == 0 && swap)
1235 return (USBD_NORMAL_COMPLETION);
1239 usbd_driver_load(module_t mod, int what, void *arg)
1241 /* XXX should implement something like a function that removes all generic devices */