2 * $NetBSD: usbdi.c,v 1.103 2002/09/27 15:37:38 provos Exp $
3 * $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.84 2003/11/09 23:56:19 joe Exp $
4 * $DragonFly: src/sys/bus/usb/usbdi.c,v 1.11 2006/10/25 20:55:52 dillon Exp $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/module.h>
49 #if defined(DIAGNOSTIC) && defined(__i386__)
50 #include <machine/cpu.h>
52 #include <sys/malloc.h>
54 #include <sys/thread2.h>
58 #include "usbdi_util.h"
62 #if defined(__FreeBSD__) || defined(__DragonFly__)
64 #include <machine/clock.h>
65 #define delay(d) DELAY(d)
69 #define DPRINTF(x) if (usbdebug) logprintf x
70 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
77 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
78 Static void usbd_do_request_async_cb
79 (usbd_xfer_handle, usbd_private_handle, usbd_status);
80 Static void usbd_start_next(usbd_pipe_handle pipe);
81 Static usbd_status usbd_open_pipe_ival
82 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
83 Static int usbd_xfer_isread(usbd_xfer_handle xfer);
85 Static int usbd_nbuses = 0;
100 usbd_xfer_isread(usbd_xfer_handle xfer)
102 if (xfer->rqflags & URQ_REQUEST)
103 return (xfer->request.bmRequestType & UT_READ);
105 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
111 usbd_dump_iface(struct usbd_interface *iface)
113 printf("usbd_dump_iface: iface=%p\n", iface);
116 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
117 iface->device, iface->idesc, iface->index, iface->altindex,
122 usbd_dump_device(struct usbd_device *dev)
124 printf("usbd_dump_device: dev=%p\n", dev);
127 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
128 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
129 "power=%d langid=%d\n",
130 dev->address, dev->config, dev->depth, dev->speed,
131 dev->self_powered, dev->power, dev->langid);
135 usbd_dump_endpoint(struct usbd_endpoint *endp)
137 printf("usbd_dump_endpoint: endp=%p\n", endp);
140 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
142 printf(" bEndpointAddress=0x%02x\n",
143 endp->edesc->bEndpointAddress);
147 usbd_dump_queue(usbd_pipe_handle pipe)
149 usbd_xfer_handle xfer;
151 printf("usbd_dump_queue: pipe=%p\n", pipe);
152 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
153 printf(" xfer=%p\n", xfer);
158 usbd_dump_pipe(usbd_pipe_handle pipe)
160 printf("usbd_dump_pipe: pipe=%p\n", pipe);
163 usbd_dump_iface(pipe->iface);
164 usbd_dump_device(pipe->device);
165 usbd_dump_endpoint(pipe->endpoint);
166 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
167 pipe->refcnt, pipe->running, pipe->aborting);
168 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
169 pipe->intrxfer, pipe->repeat, pipe->interval);
174 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
175 u_int8_t flags, usbd_pipe_handle *pipe)
177 return (usbd_open_pipe_ival(iface, address, flags, pipe,
178 USBD_DEFAULT_INTERVAL));
182 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
183 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
186 struct usbd_endpoint *ep;
190 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
191 iface, address, flags));
193 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
194 ep = &iface->endpoints[i];
195 if (ep->edesc == NULL)
196 return (USBD_IOERROR);
197 if (ep->edesc->bEndpointAddress == address)
200 return (USBD_BAD_ADDRESS);
202 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
203 return (USBD_IN_USE);
204 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
207 LIST_INSERT_HEAD(&iface->pipes, p, next);
209 return (USBD_NORMAL_COMPLETION);
213 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
214 u_int8_t flags, usbd_pipe_handle *pipe,
215 usbd_private_handle priv, void *buffer, u_int32_t len,
216 usbd_callback cb, int ival)
219 usbd_xfer_handle xfer;
220 usbd_pipe_handle ipipe;
222 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
223 address, flags, len));
225 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
229 xfer = usbd_alloc_xfer(iface->device);
234 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
235 USBD_NO_TIMEOUT, cb);
236 ipipe->intrxfer = xfer;
238 err = usbd_transfer(xfer);
240 if (err != USBD_IN_PROGRESS)
242 return (USBD_NORMAL_COMPLETION);
245 ipipe->intrxfer = NULL;
247 usbd_free_xfer(xfer);
249 usbd_close_pipe(ipipe);
254 usbd_close_pipe(usbd_pipe_handle pipe)
258 printf("usbd_close_pipe: pipe==NULL\n");
259 return (USBD_NORMAL_COMPLETION);
263 if (--pipe->refcnt != 0)
264 return (USBD_NORMAL_COMPLETION);
265 if (! SIMPLEQ_EMPTY(&pipe->queue))
266 return (USBD_PENDING_REQUESTS);
267 LIST_REMOVE(pipe, next);
268 pipe->endpoint->refcnt--;
269 pipe->methods->close(pipe);
270 if (pipe->intrxfer != NULL)
271 usbd_free_xfer(pipe->intrxfer);
273 return (USBD_NORMAL_COMPLETION);
277 usbd_transfer(usbd_xfer_handle xfer)
279 usbd_pipe_handle pipe = xfer->pipe;
280 usb_dma_t *dmap = &xfer->dmabuf;
284 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
285 xfer, xfer->flags, pipe, pipe->running));
288 usbd_dump_queue(pipe);
293 return (USBD_CANCELLED);
296 /* If there is no buffer, allocate one. */
297 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
298 struct usbd_bus *bus = pipe->device->bus;
301 if (xfer->rqflags & URQ_AUTO_DMABUF)
302 printf("usbd_transfer: has old buffer!\n");
304 err = bus->methods->allocm(bus, dmap, size);
307 xfer->rqflags |= URQ_AUTO_DMABUF;
310 /* Copy data if going out. */
311 if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
312 !usbd_xfer_isread(xfer))
313 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
315 err = pipe->methods->transfer(xfer);
317 if (err != USBD_IN_PROGRESS && err) {
318 /* The transfer has not been queued, so free buffer. */
319 if (xfer->rqflags & URQ_AUTO_DMABUF) {
320 struct usbd_bus *bus = pipe->device->bus;
322 bus->methods->freem(bus, &xfer->dmabuf);
323 xfer->rqflags &= ~URQ_AUTO_DMABUF;
327 if (!(xfer->flags & USBD_SYNCHRONOUS))
330 /* Sync transfer, wait for completion. */
331 if (err != USBD_IN_PROGRESS)
335 if (pipe->device->bus->use_polling)
336 panic("usbd_transfer: not done\n");
337 tsleep(xfer, 0, "usbsyn", 0);
340 return (xfer->status);
343 /* Like usbd_transfer(), but waits for completion. */
345 usbd_sync_transfer(usbd_xfer_handle xfer)
347 xfer->flags |= USBD_SYNCHRONOUS;
348 return (usbd_transfer(xfer));
352 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
354 struct usbd_bus *bus = xfer->device->bus;
358 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
359 printf("usbd_alloc_buffer: xfer already has a buffer\n");
361 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
364 xfer->rqflags |= URQ_DEV_DMABUF;
365 return (KERNADDR(&xfer->dmabuf, 0));
369 usbd_free_buffer(usbd_xfer_handle xfer)
372 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
373 printf("usbd_free_buffer: no buffer\n");
377 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
378 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
382 usbd_get_buffer(usbd_xfer_handle xfer)
384 if (!(xfer->rqflags & URQ_DEV_DMABUF))
386 return (KERNADDR(&xfer->dmabuf, 0));
390 usbd_alloc_xfer(usbd_device_handle dev)
392 usbd_xfer_handle xfer;
394 xfer = dev->bus->methods->allocx(dev->bus);
398 usb_callout_init(xfer->timeout_handle);
399 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
404 usbd_free_xfer(usbd_xfer_handle xfer)
406 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
407 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
408 usbd_free_buffer(xfer);
409 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
410 if (callout_pending(&xfer->timeout_handle)) {
411 callout_stop(&xfer->timeout_handle);
412 printf("usbd_free_xfer: timout_handle pending");
415 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
416 return (USBD_NORMAL_COMPLETION);
420 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
421 usbd_private_handle priv, void *buffer, u_int32_t length,
422 u_int16_t flags, u_int32_t timeout,
423 usbd_callback callback)
427 xfer->buffer = buffer;
428 xfer->length = length;
431 xfer->timeout = timeout;
432 xfer->status = USBD_NOT_STARTED;
433 xfer->callback = callback;
434 xfer->rqflags &= ~URQ_REQUEST;
439 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
440 usbd_private_handle priv, u_int32_t timeout,
441 usb_device_request_t *req, void *buffer,
442 u_int32_t length, u_int16_t flags,
443 usbd_callback callback)
445 xfer->pipe = dev->default_pipe;
447 xfer->buffer = buffer;
448 xfer->length = length;
451 xfer->timeout = timeout;
452 xfer->status = USBD_NOT_STARTED;
453 xfer->callback = callback;
454 xfer->request = *req;
455 xfer->rqflags |= URQ_REQUEST;
460 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
461 usbd_private_handle priv, u_int16_t *frlengths,
462 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
470 xfer->timeout = USBD_NO_TIMEOUT;
471 xfer->status = USBD_NOT_STARTED;
472 xfer->callback = callback;
473 xfer->rqflags &= ~URQ_REQUEST;
474 xfer->frlengths = frlengths;
475 xfer->nframes = nframes;
479 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
480 void **buffer, u_int32_t *count, usbd_status *status)
485 *buffer = xfer->buffer;
487 *count = xfer->actlen;
489 *status = xfer->status;
492 usb_config_descriptor_t *
493 usbd_get_config_descriptor(usbd_device_handle dev)
497 printf("usbd_get_config_descriptor: dev == NULL\n");
504 usb_interface_descriptor_t *
505 usbd_get_interface_descriptor(usbd_interface_handle iface)
509 printf("usbd_get_interface_descriptor: dev == NULL\n");
513 return (iface->idesc);
516 usb_device_descriptor_t *
517 usbd_get_device_descriptor(usbd_device_handle dev)
519 return (&dev->ddesc);
522 usb_endpoint_descriptor_t *
523 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
525 if (index >= iface->idesc->bNumEndpoints)
527 return (iface->endpoints[index].edesc);
531 usbd_abort_pipe(usbd_pipe_handle pipe)
537 printf("usbd_close_pipe: pipe==NULL\n");
538 return (USBD_NORMAL_COMPLETION);
542 err = usbd_ar_pipe(pipe);
548 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
550 usbd_device_handle dev = pipe->device;
551 usb_device_request_t req;
554 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
557 * Clearing en endpoint stall resets the endpoint toggle, so
558 * do the same to the HC toggle.
560 pipe->methods->cleartoggle(pipe);
562 req.bmRequestType = UT_WRITE_ENDPOINT;
563 req.bRequest = UR_CLEAR_FEATURE;
564 USETW(req.wValue, UF_ENDPOINT_HALT);
565 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
566 USETW(req.wLength, 0);
567 err = usbd_do_request(dev, &req, 0);
569 XXX should we do this?
571 pipe->state = USBD_PIPE_ACTIVE;
572 /* XXX activate pipe */
579 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
581 usbd_device_handle dev = pipe->device;
582 usb_device_request_t req;
585 pipe->methods->cleartoggle(pipe);
587 req.bmRequestType = UT_WRITE_ENDPOINT;
588 req.bRequest = UR_CLEAR_FEATURE;
589 USETW(req.wValue, UF_ENDPOINT_HALT);
590 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
591 USETW(req.wLength, 0);
592 err = usbd_do_request_async(dev, &req, 0);
597 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
599 pipe->methods->cleartoggle(pipe);
603 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
606 if (iface == NULL || iface->idesc == NULL) {
607 printf("usbd_endpoint_count: NULL pointer\n");
611 *count = iface->idesc->bNumEndpoints;
612 return (USBD_NORMAL_COMPLETION);
616 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
618 if (dev->cdesc == NULL)
619 return (USBD_NOT_CONFIGURED);
620 *count = dev->cdesc->bNumInterface;
621 return (USBD_NORMAL_COMPLETION);
625 usbd_interface2device_handle(usbd_interface_handle iface,
626 usbd_device_handle *dev)
628 *dev = iface->device;
632 usbd_device2interface_handle(usbd_device_handle dev,
633 u_int8_t ifaceno, usbd_interface_handle *iface)
635 if (dev->cdesc == NULL)
636 return (USBD_NOT_CONFIGURED);
637 if (ifaceno >= dev->cdesc->bNumInterface)
639 *iface = &dev->ifaces[ifaceno];
640 return (USBD_NORMAL_COMPLETION);
644 usbd_pipe2device_handle(usbd_pipe_handle pipe)
646 return (pipe->device);
651 usbd_set_interface(usbd_interface_handle iface, int altidx)
653 usb_device_request_t req;
657 if (LIST_FIRST(&iface->pipes) != 0)
658 return (USBD_IN_USE);
660 endpoints = iface->endpoints;
661 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
665 /* new setting works, we can free old endpoints */
666 if (endpoints != NULL)
667 kfree(endpoints, M_USB);
670 if (iface->idesc == NULL) {
671 printf("usbd_set_interface: NULL pointer\n");
676 req.bmRequestType = UT_WRITE_INTERFACE;
677 req.bRequest = UR_SET_INTERFACE;
678 USETW(req.wValue, iface->idesc->bAlternateSetting);
679 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
680 USETW(req.wLength, 0);
681 return (usbd_do_request(iface->device, &req, 0));
685 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
687 char *p = (char *)cdesc;
688 char *end = p + UGETW(cdesc->wTotalLength);
689 usb_interface_descriptor_t *d;
692 for (n = 0; p < end; p += d->bLength) {
693 d = (usb_interface_descriptor_t *)p;
694 if (p + d->bLength <= end &&
695 d->bDescriptorType == UDESC_INTERFACE &&
696 d->bInterfaceNumber == ifaceno)
703 usbd_get_interface_altindex(usbd_interface_handle iface)
705 return (iface->altindex);
709 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
711 usb_device_request_t req;
713 req.bmRequestType = UT_READ_INTERFACE;
714 req.bRequest = UR_GET_INTERFACE;
715 USETW(req.wValue, 0);
716 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
717 USETW(req.wLength, 1);
718 return (usbd_do_request(iface->device, &req, aiface));
721 /*** Internal routines ***/
723 /* Dequeue all pipe operations, called from a critical section. */
725 usbd_ar_pipe(usbd_pipe_handle pipe)
727 usbd_xfer_handle xfer;
729 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
732 usbd_dump_queue(pipe);
736 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
737 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
738 pipe, xfer, pipe->methods));
739 /* Make the HC abort it (and invoke the callback). */
740 pipe->methods->abort(xfer);
741 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
744 return (USBD_NORMAL_COMPLETION);
747 /* Called from a critical section. */
749 usb_transfer_complete(usbd_xfer_handle xfer)
751 usbd_pipe_handle pipe = xfer->pipe;
752 usb_dma_t *dmap = &xfer->dmabuf;
753 int repeat = pipe->repeat;
756 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
757 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
759 if (xfer->busy_free != XFER_ONQU) {
760 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
761 xfer, xfer->busy_free);
768 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
772 polling = pipe->device->bus->use_polling;
777 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
778 usbd_xfer_isread(xfer)) {
780 if (xfer->actlen > xfer->length) {
781 printf("usb_transfer_complete: actlen > len %d > %d\n",
782 xfer->actlen, xfer->length);
783 xfer->actlen = xfer->length;
786 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
789 /* if we allocated the buffer in usbd_transfer() we free it here. */
790 if (xfer->rqflags & URQ_AUTO_DMABUF) {
792 struct usbd_bus *bus = pipe->device->bus;
793 bus->methods->freem(bus, dmap);
794 xfer->rqflags &= ~URQ_AUTO_DMABUF;
799 /* Remove request from queue. */
801 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
802 printf("usb_transfer_complete: bad dequeue %p != %p\n",
803 xfer, SIMPLEQ_FIRST(&pipe->queue));
804 xfer->busy_free = XFER_BUSY;
806 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
808 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
809 repeat, SIMPLEQ_FIRST(&pipe->queue)));
811 /* Count completed transfers. */
812 ++pipe->device->bus->stats.uds_requests
813 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
816 if (!xfer->status && xfer->actlen < xfer->length &&
817 !(xfer->flags & USBD_SHORT_XFER_OK)) {
818 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
819 xfer->actlen, xfer->length));
820 xfer->status = USBD_SHORT_XFER;
824 xfer->callback(xfer, xfer->priv, xfer->status);
827 if (pipe->methods->done != NULL)
828 pipe->methods->done(xfer);
830 printf("usb_transfer_complete: pipe->methods->done == NULL\n");
832 pipe->methods->done(xfer);
835 if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
839 /* XXX should we stop the queue on all errors? */
840 if ((xfer->status == USBD_CANCELLED ||
841 xfer->status == USBD_TIMEOUT) &&
842 pipe->iface != NULL) /* not control pipe */
845 usbd_start_next(pipe);
850 usb_insert_transfer(usbd_xfer_handle xfer)
852 usbd_pipe_handle pipe = xfer->pipe;
855 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
856 pipe, pipe->running, xfer->timeout));
858 if (xfer->busy_free != XFER_BUSY) {
859 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
860 xfer, xfer->busy_free);
863 xfer->busy_free = XFER_ONQU;
866 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
868 err = USBD_IN_PROGRESS;
871 err = USBD_NORMAL_COMPLETION;
877 /* Called from a critical section. */
879 usbd_start_next(usbd_pipe_handle pipe)
881 usbd_xfer_handle xfer;
886 printf("usbd_start_next: pipe == NULL\n");
889 if (pipe->methods == NULL || pipe->methods->start == NULL) {
890 printf("usbd_start_next: pipe=%p no start method\n", pipe);
895 /* Get next request in queue. */
896 xfer = SIMPLEQ_FIRST(&pipe->queue);
897 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
901 err = pipe->methods->start(xfer);
902 if (err != USBD_IN_PROGRESS) {
903 printf("usbd_start_next: error=%d\n", err);
912 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
914 return (usbd_do_request_flags(dev, req, data, 0, 0,
915 USBD_DEFAULT_TIMEOUT));
919 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
920 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
922 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
923 data, flags, actlen, timo));
927 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
928 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
931 usbd_xfer_handle xfer;
935 #if defined(__i386__) && (defined(__FreeBSD__) || defined(__DragonFly__))
936 KASSERT(mycpu->gd_intr_nesting_level == 0,
937 ("usbd_do_request: in interrupt context"));
939 if (dev->bus->intr_context) {
940 printf("usbd_do_request: not in process context\n");
945 xfer = usbd_alloc_xfer(dev);
948 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
949 data, UGETW(req->wLength), flags, 0);
951 err = usbd_sync_transfer(xfer);
952 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
953 if (xfer->actlen > xfer->length)
954 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
955 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
956 dev->address, xfer->request.bmRequestType,
957 xfer->request.bRequest, UGETW(xfer->request.wValue),
958 UGETW(xfer->request.wIndex),
959 UGETW(xfer->request.wLength),
960 xfer->length, xfer->actlen));
963 *actlen = xfer->actlen;
964 if (err == USBD_STALLED) {
966 * The control endpoint has stalled. Control endpoints
967 * should not halt, but some may do so anyway so clear
968 * any halt condition.
970 usb_device_request_t treq;
975 treq.bmRequestType = UT_READ_ENDPOINT;
976 treq.bRequest = UR_GET_STATUS;
977 USETW(treq.wValue, 0);
978 USETW(treq.wIndex, 0);
979 USETW(treq.wLength, sizeof(usb_status_t));
980 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
981 &treq, &status,sizeof(usb_status_t),
983 nerr = usbd_sync_transfer(xfer);
986 s = UGETW(status.wStatus);
987 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
990 treq.bmRequestType = UT_WRITE_ENDPOINT;
991 treq.bRequest = UR_CLEAR_FEATURE;
992 USETW(treq.wValue, UF_ENDPOINT_HALT);
993 USETW(treq.wIndex, 0);
994 USETW(treq.wLength, 0);
995 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
996 &treq, &status, 0, 0, 0);
997 nerr = usbd_sync_transfer(xfer);
1003 usbd_free_xfer(xfer);
1008 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
1011 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1012 if (xfer->actlen > xfer->length)
1013 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1014 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1015 xfer->pipe->device->address,
1016 xfer->request.bmRequestType,
1017 xfer->request.bRequest, UGETW(xfer->request.wValue),
1018 UGETW(xfer->request.wIndex),
1019 UGETW(xfer->request.wLength),
1020 xfer->length, xfer->actlen));
1022 usbd_free_xfer(xfer);
1026 * Execute a request without waiting for completion.
1027 * Can be used from interrupt context.
1030 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1033 usbd_xfer_handle xfer;
1036 xfer = usbd_alloc_xfer(dev);
1038 return (USBD_NOMEM);
1039 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1040 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1041 err = usbd_transfer(xfer);
1042 if (err != USBD_IN_PROGRESS) {
1043 usbd_free_xfer(xfer);
1046 return (USBD_NORMAL_COMPLETION);
1049 const struct usbd_quirks *
1050 usbd_get_quirks(usbd_device_handle dev)
1054 printf("usbd_get_quirks: dev == NULL\n");
1058 return (dev->quirks);
1061 /* XXX do periodic free() of free list */
1064 * Called from keyboard driver when in polling mode.
1067 usbd_dopoll(usbd_interface_handle iface)
1069 iface->device->bus->methods->do_poll(iface->device->bus);
1073 usbd_set_polling(usbd_device_handle dev, int on)
1076 dev->bus->use_polling++;
1078 dev->bus->use_polling--;
1079 /* When polling we need to make sure there is nothing pending to do. */
1080 if (dev->bus->use_polling)
1081 dev->bus->methods->soft_intr(dev->bus);
1085 usb_endpoint_descriptor_t *
1086 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1088 struct usbd_endpoint *ep;
1091 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1092 ep = &iface->endpoints[i];
1093 if (ep->edesc->bEndpointAddress == address)
1094 return (iface->endpoints[i].edesc);
1100 * usbd_ratecheck() can limit the number of error messages that occurs.
1101 * When a device is unplugged it may take up to 0.25s for the hub driver
1102 * to notice it. If the driver continuosly tries to do I/O operations
1103 * this can generate a large number of messages.
1106 usbd_ratecheck(struct timeval *last)
1109 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1111 return (ratecheck(last, &errinterval));
1117 * Search for a vendor/product pair in an array. The item size is
1118 * given as an argument.
1120 const struct usb_devno *
1121 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1122 u_int16_t vendor, u_int16_t product)
1124 while (nentries-- > 0) {
1125 u_int16_t tproduct = tbl->ud_product;
1126 if (tbl->ud_vendor == vendor &&
1127 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1129 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1134 #if defined(__FreeBSD__) || defined(__DragonFly__)
1136 usbd_driver_load(module_t mod, int what, void *arg)
1138 /* XXX should implement something like a function that removes all generic devices */