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.14 2006/12/22 23:12:17 swildner 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 #if defined(__NetBSD__) || defined(__OpenBSD__)
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #elif defined(__FreeBSD__) || defined(__DragonFly__)
48 #include <sys/module.h>
51 #if defined(DIAGNOSTIC) && defined(__i386__)
52 #include <machine/cpu.h>
55 #include <sys/malloc.h>
58 #include <sys/thread2.h>
61 #include <bus/usb/usb.h>
62 #include <bus/usb/usbdi.h>
63 #include <bus/usb/usbdi_util.h>
64 #include <bus/usb/usbdivar.h>
65 #include <bus/usb/usb_mem.h>
66 #include <bus/usb/usb_quirks.h>
68 #if defined(__FreeBSD__) || defined(__DragonFly__)
69 #define delay(d) DELAY(d)
73 #define DPRINTF(x) if (usbdebug) logprintf x
74 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
81 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
82 Static void usbd_do_request_async_cb
83 (usbd_xfer_handle, usbd_private_handle, usbd_status);
84 Static void usbd_start_next(usbd_pipe_handle pipe);
85 Static usbd_status usbd_open_pipe_ival
86 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
87 Static int usbd_xfer_isread(usbd_xfer_handle xfer);
89 Static int usbd_nbuses = 0;
104 usbd_xfer_isread(usbd_xfer_handle xfer)
106 if (xfer->rqflags & URQ_REQUEST)
107 return (xfer->request.bmRequestType & UT_READ);
109 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
115 usbd_dump_iface(struct usbd_interface *iface)
117 kprintf("usbd_dump_iface: iface=%p\n", iface);
120 kprintf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
121 iface->device, iface->idesc, iface->index, iface->altindex,
126 usbd_dump_device(struct usbd_device *dev)
128 kprintf("usbd_dump_device: dev=%p\n", dev);
131 kprintf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
132 kprintf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
133 "power=%d langid=%d\n",
134 dev->address, dev->config, dev->depth, dev->speed,
135 dev->self_powered, dev->power, dev->langid);
139 usbd_dump_endpoint(struct usbd_endpoint *endp)
141 kprintf("usbd_dump_endpoint: endp=%p\n", endp);
144 kprintf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
146 kprintf(" bEndpointAddress=0x%02x\n",
147 endp->edesc->bEndpointAddress);
151 usbd_dump_queue(usbd_pipe_handle pipe)
153 usbd_xfer_handle xfer;
155 kprintf("usbd_dump_queue: pipe=%p\n", pipe);
156 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
157 kprintf(" xfer=%p\n", xfer);
162 usbd_dump_pipe(usbd_pipe_handle pipe)
164 kprintf("usbd_dump_pipe: pipe=%p\n", pipe);
167 usbd_dump_iface(pipe->iface);
168 usbd_dump_device(pipe->device);
169 usbd_dump_endpoint(pipe->endpoint);
170 kprintf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
171 pipe->refcnt, pipe->running, pipe->aborting);
172 kprintf(" intrxfer=%p, repeat=%d, interval=%d\n",
173 pipe->intrxfer, pipe->repeat, pipe->interval);
178 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
179 u_int8_t flags, usbd_pipe_handle *pipe)
181 return (usbd_open_pipe_ival(iface, address, flags, pipe,
182 USBD_DEFAULT_INTERVAL));
186 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
187 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
190 struct usbd_endpoint *ep;
194 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
195 iface, address, flags));
197 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
198 ep = &iface->endpoints[i];
199 if (ep->edesc == NULL)
200 return (USBD_IOERROR);
201 if (ep->edesc->bEndpointAddress == address)
204 return (USBD_BAD_ADDRESS);
206 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
207 return (USBD_IN_USE);
208 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
211 LIST_INSERT_HEAD(&iface->pipes, p, next);
213 return (USBD_NORMAL_COMPLETION);
217 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
218 u_int8_t flags, usbd_pipe_handle *pipe,
219 usbd_private_handle priv, void *buffer, u_int32_t len,
220 usbd_callback cb, int ival)
223 usbd_xfer_handle xfer;
224 usbd_pipe_handle ipipe;
226 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
227 address, flags, len));
229 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
233 xfer = usbd_alloc_xfer(iface->device);
238 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
239 USBD_NO_TIMEOUT, cb);
240 ipipe->intrxfer = xfer;
242 err = usbd_transfer(xfer);
244 if (err != USBD_IN_PROGRESS && err)
246 return (USBD_NORMAL_COMPLETION);
249 ipipe->intrxfer = NULL;
251 usbd_free_xfer(xfer);
253 usbd_close_pipe(ipipe);
258 usbd_close_pipe(usbd_pipe_handle pipe)
262 kprintf("usbd_close_pipe: pipe==NULL\n");
263 return (USBD_NORMAL_COMPLETION);
267 if (--pipe->refcnt != 0)
268 return (USBD_NORMAL_COMPLETION);
269 if (! SIMPLEQ_EMPTY(&pipe->queue))
270 return (USBD_PENDING_REQUESTS);
271 LIST_REMOVE(pipe, next);
272 pipe->endpoint->refcnt--;
273 pipe->methods->close(pipe);
274 if (pipe->intrxfer != NULL)
275 usbd_free_xfer(pipe->intrxfer);
277 return (USBD_NORMAL_COMPLETION);
281 usbd_transfer(usbd_xfer_handle xfer)
283 usbd_pipe_handle pipe = xfer->pipe;
284 usb_dma_t *dmap = &xfer->dmabuf;
288 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
289 xfer, xfer->flags, pipe, pipe->running));
292 usbd_dump_queue(pipe);
297 return (USBD_CANCELLED);
300 /* If there is no buffer, allocate one. */
301 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
302 struct usbd_bus *bus = pipe->device->bus;
305 if (xfer->rqflags & URQ_AUTO_DMABUF)
306 kprintf("usbd_transfer: has old buffer!\n");
308 err = bus->methods->allocm(bus, dmap, size);
311 xfer->rqflags |= URQ_AUTO_DMABUF;
314 /* Copy data if going out. */
315 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
316 !usbd_xfer_isread(xfer))
317 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
319 err = pipe->methods->transfer(xfer);
321 if (err != USBD_IN_PROGRESS && err) {
322 /* The transfer has not been queued, so free buffer. */
323 if (xfer->rqflags & URQ_AUTO_DMABUF) {
324 struct usbd_bus *bus = pipe->device->bus;
326 bus->methods->freem(bus, &xfer->dmabuf);
327 xfer->rqflags &= ~URQ_AUTO_DMABUF;
331 if (!(xfer->flags & USBD_SYNCHRONOUS))
334 /* Sync transfer, wait for completion. */
335 if (err != USBD_IN_PROGRESS)
339 if (pipe->device->bus->use_polling)
340 panic("usbd_transfer: not done");
341 tsleep(xfer, 0, "usbsyn", 0);
344 return (xfer->status);
347 /* Like usbd_transfer(), but waits for completion. */
349 usbd_sync_transfer(usbd_xfer_handle xfer)
351 xfer->flags |= USBD_SYNCHRONOUS;
352 return (usbd_transfer(xfer));
356 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
358 struct usbd_bus *bus = xfer->device->bus;
362 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
363 kprintf("usbd_alloc_buffer: xfer already has a buffer\n");
365 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
368 xfer->rqflags |= URQ_DEV_DMABUF;
369 return (KERNADDR(&xfer->dmabuf, 0));
373 usbd_free_buffer(usbd_xfer_handle xfer)
376 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
377 kprintf("usbd_free_buffer: no buffer\n");
381 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
382 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
386 usbd_get_buffer(usbd_xfer_handle xfer)
388 if (!(xfer->rqflags & URQ_DEV_DMABUF))
390 return (KERNADDR(&xfer->dmabuf, 0));
394 usbd_alloc_xfer(usbd_device_handle dev)
396 usbd_xfer_handle xfer;
398 xfer = dev->bus->methods->allocx(dev->bus);
402 usb_callout_init(xfer->timeout_handle);
403 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
408 usbd_free_xfer(usbd_xfer_handle xfer)
410 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
411 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
412 usbd_free_buffer(xfer);
413 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
414 if (callout_pending(&xfer->timeout_handle)) {
415 callout_stop(&xfer->timeout_handle);
416 kprintf("usbd_free_xfer: timout_handle pending");
419 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
420 return (USBD_NORMAL_COMPLETION);
424 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
425 usbd_private_handle priv, void *buffer, u_int32_t length,
426 u_int16_t flags, u_int32_t timeout,
427 usbd_callback callback)
431 xfer->buffer = buffer;
432 xfer->length = length;
435 xfer->timeout = timeout;
436 xfer->status = USBD_NOT_STARTED;
437 xfer->callback = callback;
438 xfer->rqflags &= ~URQ_REQUEST;
443 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
444 usbd_private_handle priv, u_int32_t timeout,
445 usb_device_request_t *req, void *buffer,
446 u_int32_t length, u_int16_t flags,
447 usbd_callback callback)
449 xfer->pipe = dev->default_pipe;
451 xfer->buffer = buffer;
452 xfer->length = length;
455 xfer->timeout = timeout;
456 xfer->status = USBD_NOT_STARTED;
457 xfer->callback = callback;
458 xfer->request = *req;
459 xfer->rqflags |= URQ_REQUEST;
464 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
465 usbd_private_handle priv, u_int16_t *frlengths,
466 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
474 xfer->timeout = USBD_NO_TIMEOUT;
475 xfer->status = USBD_NOT_STARTED;
476 xfer->callback = callback;
477 xfer->rqflags &= ~URQ_REQUEST;
478 xfer->frlengths = frlengths;
479 xfer->nframes = nframes;
483 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
484 void **buffer, u_int32_t *count, usbd_status *status)
489 *buffer = xfer->buffer;
491 *count = xfer->actlen;
493 *status = xfer->status;
497 usbd_get_speed(usbd_device_handle dev)
502 usb_config_descriptor_t *
503 usbd_get_config_descriptor(usbd_device_handle dev)
507 kprintf("usbd_get_config_descriptor: dev == NULL\n");
514 usb_interface_descriptor_t *
515 usbd_get_interface_descriptor(usbd_interface_handle iface)
519 kprintf("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)
547 kprintf("usbd_close_pipe: pipe==NULL\n");
548 return (USBD_NORMAL_COMPLETION);
552 err = usbd_ar_pipe(pipe);
558 usbd_abort_default_pipe(usbd_device_handle dev)
560 return (usbd_abort_pipe(dev->default_pipe));
564 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
566 usbd_device_handle dev = pipe->device;
567 usb_device_request_t req;
570 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
573 * Clearing en endpoint stall resets the endpoint toggle, so
574 * do the same to the HC toggle.
576 pipe->methods->cleartoggle(pipe);
578 req.bmRequestType = UT_WRITE_ENDPOINT;
579 req.bRequest = UR_CLEAR_FEATURE;
580 USETW(req.wValue, UF_ENDPOINT_HALT);
581 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
582 USETW(req.wLength, 0);
583 err = usbd_do_request(dev, &req, 0);
585 XXX should we do this?
587 pipe->state = USBD_PIPE_ACTIVE;
588 /* XXX activate pipe */
595 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
597 usbd_device_handle dev = pipe->device;
598 usb_device_request_t req;
601 pipe->methods->cleartoggle(pipe);
603 req.bmRequestType = UT_WRITE_ENDPOINT;
604 req.bRequest = UR_CLEAR_FEATURE;
605 USETW(req.wValue, UF_ENDPOINT_HALT);
606 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
607 USETW(req.wLength, 0);
608 err = usbd_do_request_async(dev, &req, 0);
613 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
615 pipe->methods->cleartoggle(pipe);
619 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
622 if (iface == NULL || iface->idesc == NULL) {
623 kprintf("usbd_endpoint_count: NULL pointer\n");
627 *count = iface->idesc->bNumEndpoints;
628 return (USBD_NORMAL_COMPLETION);
632 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
634 if (dev->cdesc == NULL)
635 return (USBD_NOT_CONFIGURED);
636 *count = dev->cdesc->bNumInterface;
637 return (USBD_NORMAL_COMPLETION);
641 usbd_interface2device_handle(usbd_interface_handle iface,
642 usbd_device_handle *dev)
644 *dev = iface->device;
648 usbd_device2interface_handle(usbd_device_handle dev,
649 u_int8_t ifaceno, usbd_interface_handle *iface)
651 if (dev->cdesc == NULL)
652 return (USBD_NOT_CONFIGURED);
653 if (ifaceno >= dev->cdesc->bNumInterface)
655 *iface = &dev->ifaces[ifaceno];
656 return (USBD_NORMAL_COMPLETION);
660 usbd_pipe2device_handle(usbd_pipe_handle pipe)
662 return (pipe->device);
667 usbd_set_interface(usbd_interface_handle iface, int altidx)
669 usb_device_request_t req;
673 if (LIST_FIRST(&iface->pipes) != 0)
674 return (USBD_IN_USE);
676 endpoints = iface->endpoints;
677 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
681 /* new setting works, we can free old endpoints */
682 if (endpoints != NULL)
683 kfree(endpoints, M_USB);
686 if (iface->idesc == NULL) {
687 kprintf("usbd_set_interface: NULL pointer\n");
692 req.bmRequestType = UT_WRITE_INTERFACE;
693 req.bRequest = UR_SET_INTERFACE;
694 USETW(req.wValue, iface->idesc->bAlternateSetting);
695 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
696 USETW(req.wLength, 0);
697 return (usbd_do_request(iface->device, &req, 0));
701 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
703 char *p = (char *)cdesc;
704 char *end = p + UGETW(cdesc->wTotalLength);
705 usb_interface_descriptor_t *d;
708 for (n = 0; p < end; p += d->bLength) {
709 d = (usb_interface_descriptor_t *)p;
710 if (p + d->bLength <= end &&
711 d->bDescriptorType == UDESC_INTERFACE &&
712 d->bInterfaceNumber == ifaceno)
719 usbd_get_interface_altindex(usbd_interface_handle iface)
721 return (iface->altindex);
725 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
727 usb_device_request_t req;
729 req.bmRequestType = UT_READ_INTERFACE;
730 req.bRequest = UR_GET_INTERFACE;
731 USETW(req.wValue, 0);
732 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
733 USETW(req.wLength, 1);
734 return (usbd_do_request(iface->device, &req, aiface));
737 /*** Internal routines ***/
739 /* Dequeue all pipe operations, called from critical section. */
741 usbd_ar_pipe(usbd_pipe_handle pipe)
743 usbd_xfer_handle xfer;
745 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
748 usbd_dump_queue(pipe);
752 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
753 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
754 pipe, xfer, pipe->methods));
755 /* Make the HC abort it (and invoke the callback). */
756 pipe->methods->abort(xfer);
757 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
760 return (USBD_NORMAL_COMPLETION);
763 /* Called from critical section */
765 usb_transfer_complete(usbd_xfer_handle xfer)
767 usbd_pipe_handle pipe = xfer->pipe;
768 usb_dma_t *dmap = &xfer->dmabuf;
769 int sync = xfer->flags & USBD_SYNCHRONOUS;
770 int erred = xfer->status == USBD_CANCELLED ||
771 xfer->status == USBD_TIMEOUT;
772 int repeat = pipe->repeat;
775 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
776 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
778 if (xfer->busy_free != XFER_ONQU) {
779 kprintf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
780 xfer, xfer->busy_free);
787 kprintf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
791 polling = pipe->device->bus->use_polling;
796 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
797 usbd_xfer_isread(xfer)) {
799 if (xfer->actlen > xfer->length) {
800 kprintf("usb_transfer_complete: actlen > len %d > %d\n",
801 xfer->actlen, xfer->length);
802 xfer->actlen = xfer->length;
805 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
808 /* if we allocated the buffer in usbd_transfer() we free it here. */
809 if (xfer->rqflags & URQ_AUTO_DMABUF) {
811 struct usbd_bus *bus = pipe->device->bus;
812 bus->methods->freem(bus, dmap);
813 xfer->rqflags &= ~URQ_AUTO_DMABUF;
818 /* Remove request from queue. */
820 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
821 kprintf("usb_transfer_complete: bad dequeue %p != %p\n",
822 xfer, SIMPLEQ_FIRST(&pipe->queue));
823 xfer->busy_free = XFER_BUSY;
825 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
827 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
828 repeat, SIMPLEQ_FIRST(&pipe->queue)));
830 /* Count completed transfers. */
831 ++pipe->device->bus->stats.uds_requests
832 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
835 if (!xfer->status && xfer->actlen < xfer->length &&
836 !(xfer->flags & USBD_SHORT_XFER_OK)) {
837 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
838 xfer->actlen, xfer->length));
839 xfer->status = USBD_SHORT_XFER;
843 * For repeat operations, call the callback first, as the xfer
844 * will not go away and the "done" method may modify it. Otherwise
845 * reverse the order in case the callback wants to free or reuse
850 xfer->callback(xfer, xfer->priv, xfer->status);
851 pipe->methods->done(xfer);
853 pipe->methods->done(xfer);
855 xfer->callback(xfer, xfer->priv, xfer->status);
858 if (sync && !polling)
862 /* XXX should we stop the queue on all errors? */
863 if (erred && pipe->iface != NULL) /* not control pipe */
866 usbd_start_next(pipe);
871 usb_insert_transfer(usbd_xfer_handle xfer)
873 usbd_pipe_handle pipe = xfer->pipe;
876 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
877 pipe, pipe->running, xfer->timeout));
879 if (xfer->busy_free != XFER_BUSY) {
880 kprintf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
881 xfer, xfer->busy_free);
884 xfer->busy_free = XFER_ONQU;
887 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
889 err = USBD_IN_PROGRESS;
892 err = USBD_NORMAL_COMPLETION;
898 /* Called from critical section */
900 usbd_start_next(usbd_pipe_handle pipe)
902 usbd_xfer_handle xfer;
907 kprintf("usbd_start_next: pipe == NULL\n");
910 if (pipe->methods == NULL || pipe->methods->start == NULL) {
911 kprintf("usbd_start_next: pipe=%p no start method\n", pipe);
916 /* Get next request in queue. */
917 xfer = SIMPLEQ_FIRST(&pipe->queue);
918 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
922 err = pipe->methods->start(xfer);
923 if (err != USBD_IN_PROGRESS) {
924 kprintf("usbd_start_next: error=%d\n", err);
932 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
934 return (usbd_do_request_flags(dev, req, data, 0, 0,
935 USBD_DEFAULT_TIMEOUT));
939 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
940 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
942 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
943 data, flags, actlen, timo));
947 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
948 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
951 usbd_xfer_handle xfer;
955 #if defined(__i386__) && (defined(__FreeBSD__) || defined(__DragonFly__))
956 #if defined(__FreeBSD__)
957 KASSERT(curthread->td_intr_nesting_level == 0,
958 ("usbd_do_request: in interrupt context"));
959 #elif defined(__DragonFly__)
960 KASSERT(mycpu->gd_intr_nesting_level == 0,
961 ("usbd_do_request: in interrupt context"));
964 if (dev->bus->intr_context) {
965 kprintf("usbd_do_request: not in process context\n");
970 xfer = usbd_alloc_xfer(dev);
973 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
974 data, UGETW(req->wLength), flags, 0);
976 err = usbd_sync_transfer(xfer);
977 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
978 if (xfer->actlen > xfer->length)
979 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
980 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
981 dev->address, xfer->request.bmRequestType,
982 xfer->request.bRequest, UGETW(xfer->request.wValue),
983 UGETW(xfer->request.wIndex),
984 UGETW(xfer->request.wLength),
985 xfer->length, xfer->actlen));
988 *actlen = xfer->actlen;
989 if (err == USBD_STALLED) {
991 * The control endpoint has stalled. Control endpoints
992 * should not halt, but some may do so anyway so clear
993 * any halt condition.
995 usb_device_request_t treq;
1000 treq.bmRequestType = UT_READ_ENDPOINT;
1001 treq.bRequest = UR_GET_STATUS;
1002 USETW(treq.wValue, 0);
1003 USETW(treq.wIndex, 0);
1004 USETW(treq.wLength, sizeof(usb_status_t));
1005 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1006 &treq, &status,sizeof(usb_status_t),
1008 nerr = usbd_sync_transfer(xfer);
1011 s = UGETW(status.wStatus);
1012 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
1013 if (!(s & UES_HALT))
1015 treq.bmRequestType = UT_WRITE_ENDPOINT;
1016 treq.bRequest = UR_CLEAR_FEATURE;
1017 USETW(treq.wValue, UF_ENDPOINT_HALT);
1018 USETW(treq.wIndex, 0);
1019 USETW(treq.wLength, 0);
1020 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1021 &treq, &status, 0, 0, 0);
1022 nerr = usbd_sync_transfer(xfer);
1028 usbd_free_xfer(xfer);
1033 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1036 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1037 if (xfer->actlen > xfer->length)
1038 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1039 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1040 xfer->pipe->device->address,
1041 xfer->request.bmRequestType,
1042 xfer->request.bRequest, UGETW(xfer->request.wValue),
1043 UGETW(xfer->request.wIndex),
1044 UGETW(xfer->request.wLength),
1045 xfer->length, xfer->actlen));
1047 usbd_free_xfer(xfer);
1051 * Execute a request without waiting for completion.
1052 * Can be used from interrupt context.
1055 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1058 usbd_xfer_handle xfer;
1061 xfer = usbd_alloc_xfer(dev);
1063 return (USBD_NOMEM);
1064 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1065 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1066 err = usbd_transfer(xfer);
1067 if (err != USBD_IN_PROGRESS && err) {
1068 usbd_free_xfer(xfer);
1071 return (USBD_NORMAL_COMPLETION);
1074 const struct usbd_quirks *
1075 usbd_get_quirks(usbd_device_handle dev)
1079 kprintf("usbd_get_quirks: dev == NULL\n");
1083 return (dev->quirks);
1086 /* XXX do periodic free() of free list */
1089 * Called from keyboard driver when in polling mode.
1092 usbd_dopoll(usbd_interface_handle iface)
1094 iface->device->bus->methods->do_poll(iface->device->bus);
1098 usbd_set_polling(usbd_device_handle dev, int on)
1101 dev->bus->use_polling++;
1103 dev->bus->use_polling--;
1104 /* When polling we need to make sure there is nothing pending to do. */
1105 if (dev->bus->use_polling)
1106 dev->bus->methods->soft_intr(dev->bus);
1110 usb_endpoint_descriptor_t *
1111 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1113 struct usbd_endpoint *ep;
1116 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1117 ep = &iface->endpoints[i];
1118 if (ep->edesc->bEndpointAddress == address)
1119 return (iface->endpoints[i].edesc);
1125 * usbd_ratecheck() can limit the number of error messages that occurs.
1126 * When a device is unplugged it may take up to 0.25s for the hub driver
1127 * to notice it. If the driver continuosly tries to do I/O operations
1128 * this can generate a large number of messages.
1131 usbd_ratecheck(struct timeval *last)
1133 if (last->tv_sec == time_second)
1135 last->tv_sec = time_second;
1140 * Search for a vendor/product pair in an array. The item size is
1141 * given as an argument.
1143 const struct usb_devno *
1144 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1145 u_int16_t vendor, u_int16_t product)
1147 while (nentries-- > 0) {
1148 u_int16_t tproduct = tbl->ud_product;
1149 if (tbl->ud_vendor == vendor &&
1150 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1152 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1159 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1161 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1163 iter->cur = (const uByte *)cd;
1164 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1167 const usb_descriptor_t *
1168 usb_desc_iter_next(usbd_desc_iter_t *iter)
1170 const usb_descriptor_t *desc;
1172 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1173 if (iter->cur != iter->end)
1174 kprintf("usb_desc_iter_next: bad descriptor\n");
1177 desc = (const usb_descriptor_t *)iter->cur;
1178 if (desc->bLength == 0) {
1179 kprintf("usb_desc_iter_next: descriptor length = 0\n");
1182 iter->cur += desc->bLength;
1183 if (iter->cur > iter->end) {
1184 kprintf("usb_desc_iter_next: descriptor length too large\n");
1191 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1193 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1194 usb_string_descriptor_t us;
1203 return (USBD_INVAL);
1204 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1205 return (USBD_STALLED);
1206 if (dev->langid == USBD_NOLANG) {
1207 /* Set up default language */
1208 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1210 if (err || size < 4) {
1211 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1212 dev->langid = 0; /* Well, just pick something then */
1214 /* Pick the first language as the default. */
1215 dev->langid = UGETW(us.bString[0]);
1218 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1223 for (i = 0; i < n; i++) {
1224 c = UGETW(us.bString[i]);
1225 /* Convert from Unicode, handle buggy strings. */
1226 if ((c & 0xff00) == 0)
1228 else if ((c & 0x00ff) == 0 && swap)
1234 return (USBD_NORMAL_COMPLETION);
1237 #if defined(__FreeBSD__) || defined(__DragonFly__)
1239 usbd_driver_load(module_t mod, int what, void *arg)
1241 /* XXX should implement something like a function that removes all generic devices */