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 $ */
3 /* $DragonFly: src/sys/bus/usb/usbdi.c,v 1.18 2007/06/29 22:56:31 hasso Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
48 #if defined(DIAGNOSTIC) && defined(__i386__)
49 #include <machine/cpu.h>
51 #include <sys/malloc.h>
53 #include <sys/thread2.h>
55 #include <bus/usb/usb.h>
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
58 #include <bus/usb/usbdivar.h>
59 #include <bus/usb/usb_mem.h>
60 #include <bus/usb/usb_quirks.h>
62 #define delay(d) DELAY(d)
65 #define DPRINTF(x) if (usbdebug) kprintf x
66 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
73 static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
74 static void usbd_do_request_async_cb
75 (usbd_xfer_handle, usbd_private_handle, usbd_status);
76 static void usbd_start_next(usbd_pipe_handle pipe);
77 static usbd_status usbd_open_pipe_ival
78 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
79 static int usbd_xfer_isread(usbd_xfer_handle xfer);
81 static int usbd_nbuses = 0;
96 usbd_xfer_isread(usbd_xfer_handle xfer)
98 if (xfer->rqflags & URQ_REQUEST)
99 return (xfer->request.bmRequestType & UT_READ);
101 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
107 usbd_dump_iface(struct usbd_interface *iface)
109 kprintf("usbd_dump_iface: iface=%p\n", iface);
112 kprintf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
113 iface->device, iface->idesc, iface->index, iface->altindex,
118 usbd_dump_device(struct usbd_device *dev)
120 kprintf("usbd_dump_device: dev=%p\n", dev);
123 kprintf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
124 kprintf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
125 "power=%d langid=%d\n",
126 dev->address, dev->config, dev->depth, dev->speed,
127 dev->self_powered, dev->power, dev->langid);
131 usbd_dump_endpoint(struct usbd_endpoint *endp)
133 kprintf("usbd_dump_endpoint: endp=%p\n", endp);
136 kprintf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
138 kprintf(" bEndpointAddress=0x%02x\n",
139 endp->edesc->bEndpointAddress);
143 usbd_dump_queue(usbd_pipe_handle pipe)
145 usbd_xfer_handle xfer;
147 kprintf("usbd_dump_queue: pipe=%p\n", pipe);
148 STAILQ_FOREACH(xfer, &pipe->queue, next) {
149 kprintf(" xfer=%p\n", xfer);
154 usbd_dump_pipe(usbd_pipe_handle pipe)
156 kprintf("usbd_dump_pipe: pipe=%p\n", pipe);
159 usbd_dump_iface(pipe->iface);
160 usbd_dump_device(pipe->device);
161 usbd_dump_endpoint(pipe->endpoint);
162 kprintf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
163 pipe->refcnt, pipe->running, pipe->aborting);
164 kprintf(" intrxfer=%p, repeat=%d, interval=%d\n",
165 pipe->intrxfer, pipe->repeat, pipe->interval);
170 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
171 u_int8_t flags, usbd_pipe_handle *pipe)
173 return (usbd_open_pipe_ival(iface, address, flags, pipe,
174 USBD_DEFAULT_INTERVAL));
178 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
179 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
182 struct usbd_endpoint *ep;
186 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
187 iface, address, flags));
189 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
190 ep = &iface->endpoints[i];
191 if (ep->edesc == NULL)
192 return (USBD_IOERROR);
193 if (ep->edesc->bEndpointAddress == address)
196 return (USBD_BAD_ADDRESS);
198 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
199 return (USBD_IN_USE);
200 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
203 LIST_INSERT_HEAD(&iface->pipes, p, next);
205 return (USBD_NORMAL_COMPLETION);
209 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
210 u_int8_t flags, usbd_pipe_handle *pipe,
211 usbd_private_handle priv, void *buffer, u_int32_t len,
212 usbd_callback cb, int ival)
215 usbd_xfer_handle xfer;
216 usbd_pipe_handle ipipe;
218 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
219 address, flags, len));
221 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
225 xfer = usbd_alloc_xfer(iface->device);
230 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
231 USBD_NO_TIMEOUT, cb);
232 ipipe->intrxfer = xfer;
234 err = usbd_transfer(xfer);
236 if (err != USBD_IN_PROGRESS && err)
238 return (USBD_NORMAL_COMPLETION);
241 ipipe->intrxfer = NULL;
243 usbd_free_xfer(xfer);
245 usbd_close_pipe(ipipe);
250 usbd_close_pipe(usbd_pipe_handle pipe)
254 kprintf("usbd_close_pipe: pipe==NULL\n");
255 return (USBD_NORMAL_COMPLETION);
259 if (--pipe->refcnt != 0)
260 return (USBD_NORMAL_COMPLETION);
261 if (! STAILQ_EMPTY(&pipe->queue))
262 return (USBD_PENDING_REQUESTS);
263 LIST_REMOVE(pipe, next);
264 pipe->endpoint->refcnt--;
265 pipe->methods->close(pipe);
266 if (pipe->intrxfer != NULL)
267 usbd_free_xfer(pipe->intrxfer);
269 return (USBD_NORMAL_COMPLETION);
273 usbd_transfer(usbd_xfer_handle xfer)
275 usbd_pipe_handle pipe = xfer->pipe;
276 usb_dma_t *dmap = &xfer->dmabuf;
281 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
282 xfer, xfer->flags, pipe, pipe->running));
285 usbd_dump_queue(pipe);
290 return (USBD_CANCELLED);
293 /* If there is no buffer, allocate one. */
294 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
295 struct usbd_bus *bus = pipe->device->bus;
298 if (xfer->rqflags & URQ_AUTO_DMABUF)
299 kprintf("usbd_transfer: has old buffer!\n");
301 err = bus->methods->allocm(bus, dmap, size);
304 xfer->rqflags |= URQ_AUTO_DMABUF;
307 /* Copy data if going out. */
308 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
309 !usbd_xfer_isread(xfer))
310 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
312 err = pipe->methods->transfer(xfer);
314 if (err != USBD_IN_PROGRESS && err) {
315 /* The transfer has not been queued, so free buffer. */
316 if (xfer->rqflags & URQ_AUTO_DMABUF) {
317 struct usbd_bus *bus = pipe->device->bus;
319 bus->methods->freem(bus, &xfer->dmabuf);
320 xfer->rqflags &= ~URQ_AUTO_DMABUF;
324 if (!(xfer->flags & USBD_SYNCHRONOUS))
327 /* Sync transfer, wait for completion. */
328 if (err != USBD_IN_PROGRESS)
332 while (!xfer->done) {
333 if (pipe->device->bus->use_polling) {
334 pipe->device->bus->methods->do_poll(pipe->device->bus);
336 kprintf("Warning: usbd_transfer: not done, "
340 usbd_abort_pipe(pipe);
344 tsleep(xfer, 0, "usbsyn", hz);
349 return (xfer->status);
352 /* Like usbd_transfer(), but waits for completion. */
354 usbd_sync_transfer(usbd_xfer_handle xfer)
356 xfer->flags |= USBD_SYNCHRONOUS;
357 return (usbd_transfer(xfer));
361 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
363 struct usbd_bus *bus = xfer->device->bus;
367 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
368 kprintf("usbd_alloc_buffer: xfer already has a buffer\n");
370 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
372 device_printf(bus->bdev, "Unable to allocate %u contig\n",
376 xfer->rqflags |= URQ_DEV_DMABUF;
377 return (KERNADDR(&xfer->dmabuf, 0));
381 usbd_free_buffer(usbd_xfer_handle xfer)
384 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
385 kprintf("usbd_free_buffer: no buffer\n");
389 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
390 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
394 usbd_get_buffer(usbd_xfer_handle xfer)
396 if (!(xfer->rqflags & URQ_DEV_DMABUF))
398 return (KERNADDR(&xfer->dmabuf, 0));
402 usbd_alloc_xfer(usbd_device_handle dev)
404 usbd_xfer_handle xfer;
406 xfer = dev->bus->methods->allocx(dev->bus);
410 callout_init(&xfer->timeout_handle);
411 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
416 usbd_free_xfer(usbd_xfer_handle xfer)
418 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
419 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
420 usbd_free_buffer(xfer);
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;
499 usbd_get_speed(usbd_device_handle dev)
504 usb_config_descriptor_t *
505 usbd_get_config_descriptor(usbd_device_handle dev)
509 kprintf("usbd_get_config_descriptor: dev == NULL\n");
516 usb_interface_descriptor_t *
517 usbd_get_interface_descriptor(usbd_interface_handle iface)
521 kprintf("usbd_get_interface_descriptor: dev == NULL\n");
525 return (iface->idesc);
528 usb_device_descriptor_t *
529 usbd_get_device_descriptor(usbd_device_handle dev)
531 return (&dev->ddesc);
534 usb_endpoint_descriptor_t *
535 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
537 if (index >= iface->idesc->bNumEndpoints)
539 return (iface->endpoints[index].edesc);
543 usbd_abort_pipe(usbd_pipe_handle pipe)
549 kprintf("usbd_close_pipe: pipe==NULL\n");
550 return (USBD_NORMAL_COMPLETION);
554 err = usbd_ar_pipe(pipe);
560 usbd_abort_default_pipe(usbd_device_handle dev)
562 return (usbd_abort_pipe(dev->default_pipe));
566 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
568 usbd_device_handle dev = pipe->device;
569 usb_device_request_t req;
572 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
575 * Clearing en endpoint stall resets the endpoint toggle, so
576 * do the same to the HC toggle.
578 pipe->methods->cleartoggle(pipe);
580 req.bmRequestType = UT_WRITE_ENDPOINT;
581 req.bRequest = UR_CLEAR_FEATURE;
582 USETW(req.wValue, UF_ENDPOINT_HALT);
583 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
584 USETW(req.wLength, 0);
585 err = usbd_do_request(dev, &req, 0);
587 XXX should we do this?
589 pipe->state = USBD_PIPE_ACTIVE;
590 /* XXX activate pipe */
597 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
599 usbd_device_handle dev = pipe->device;
600 usb_device_request_t req;
603 pipe->methods->cleartoggle(pipe);
605 req.bmRequestType = UT_WRITE_ENDPOINT;
606 req.bRequest = UR_CLEAR_FEATURE;
607 USETW(req.wValue, UF_ENDPOINT_HALT);
608 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
609 USETW(req.wLength, 0);
610 err = usbd_do_request_async(dev, &req, 0);
615 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
617 pipe->methods->cleartoggle(pipe);
621 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
624 if (iface == NULL || iface->idesc == NULL) {
625 kprintf("usbd_endpoint_count: NULL pointer\n");
629 *count = iface->idesc->bNumEndpoints;
630 return (USBD_NORMAL_COMPLETION);
634 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
636 if (dev->cdesc == NULL)
637 return (USBD_NOT_CONFIGURED);
638 *count = dev->cdesc->bNumInterface;
639 return (USBD_NORMAL_COMPLETION);
643 usbd_interface2device_handle(usbd_interface_handle iface,
644 usbd_device_handle *dev)
646 *dev = iface->device;
650 usbd_device2interface_handle(usbd_device_handle dev,
651 u_int8_t ifaceno, usbd_interface_handle *iface)
653 if (dev->cdesc == NULL)
654 return (USBD_NOT_CONFIGURED);
655 if (ifaceno >= dev->cdesc->bNumInterface)
657 *iface = &dev->ifaces[ifaceno];
658 return (USBD_NORMAL_COMPLETION);
662 usbd_pipe2device_handle(usbd_pipe_handle pipe)
664 return (pipe->device);
669 usbd_set_interface(usbd_interface_handle iface, int altidx)
671 usb_device_request_t req;
675 if (LIST_FIRST(&iface->pipes) != 0)
676 return (USBD_IN_USE);
678 endpoints = iface->endpoints;
679 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
683 /* new setting works, we can free old endpoints */
684 if (endpoints != NULL)
685 kfree(endpoints, M_USB);
688 if (iface->idesc == NULL) {
689 kprintf("usbd_set_interface: NULL pointer\n");
694 req.bmRequestType = UT_WRITE_INTERFACE;
695 req.bRequest = UR_SET_INTERFACE;
696 USETW(req.wValue, iface->idesc->bAlternateSetting);
697 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
698 USETW(req.wLength, 0);
699 return (usbd_do_request(iface->device, &req, 0));
703 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
705 char *p = (char *)cdesc;
706 char *end = p + UGETW(cdesc->wTotalLength);
707 usb_interface_descriptor_t *d;
710 for (n = 0; p < end; p += d->bLength) {
711 d = (usb_interface_descriptor_t *)p;
712 if (p + d->bLength <= end &&
713 d->bDescriptorType == UDESC_INTERFACE &&
714 d->bInterfaceNumber == ifaceno)
721 usbd_get_interface_altindex(usbd_interface_handle iface)
723 return (iface->altindex);
727 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
729 usb_device_request_t req;
731 req.bmRequestType = UT_READ_INTERFACE;
732 req.bRequest = UR_GET_INTERFACE;
733 USETW(req.wValue, 0);
734 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
735 USETW(req.wLength, 1);
736 return (usbd_do_request(iface->device, &req, aiface));
739 /*** Internal routines ***/
741 /* Dequeue all pipe operations, called from critical section. */
743 usbd_ar_pipe(usbd_pipe_handle pipe)
745 usbd_xfer_handle xfer;
747 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
750 usbd_dump_queue(pipe);
754 while ((xfer = STAILQ_FIRST(&pipe->queue)) != NULL) {
755 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
756 pipe, xfer, pipe->methods));
757 /* Make the HC abort it (and invoke the callback). */
758 pipe->methods->abort(xfer);
759 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
762 return (USBD_NORMAL_COMPLETION);
765 /* Called from critical section */
767 usb_transfer_complete(usbd_xfer_handle xfer)
769 usbd_pipe_handle pipe = xfer->pipe;
770 usb_dma_t *dmap = &xfer->dmabuf;
771 int sync = xfer->flags & USBD_SYNCHRONOUS;
772 int erred = xfer->status == USBD_CANCELLED ||
773 xfer->status == USBD_TIMEOUT;
774 int repeat = pipe->repeat;
777 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
778 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
780 if (xfer->busy_free != XFER_ONQU) {
781 kprintf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
782 xfer, xfer->busy_free);
789 kprintf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
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_second)
1136 last->tv_sec = time_second;
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 */