1 /* $NetBSD: usbdi.c,v 1.125 2008/12/12 05:35:11 jmorse Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.28 1999/11/17 22:33:49 n_hibma 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.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.125 2008/12/12 05:35:11 jmorse Exp $");
37 #include "opt_compat_netbsd.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usb_mem.h>
53 #include <dev/usb/usb_quirks.h>
55 /* UTF-8 encoding stuff */
56 #include <fs/unicode.h>
59 #define DPRINTF(x) if (usbdebug) logprintf x
60 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
67 Static usbd_status
usbd_ar_pipe(usbd_pipe_handle pipe
);
68 Static
void usbd_do_request_async_cb
69 (usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
70 Static
void usbd_start_next(usbd_pipe_handle pipe
);
71 Static usbd_status usbd_open_pipe_ival
72 (usbd_interface_handle
, u_int8_t
, u_int8_t
, usbd_pipe_handle
*, int);
75 usbd_xfer_isread(usbd_xfer_handle xfer
)
77 if (xfer
->rqflags
& URQ_REQUEST
)
78 return (xfer
->request
.bmRequestType
& UT_READ
);
80 return (xfer
->pipe
->endpoint
->edesc
->bEndpointAddress
&
86 usbd_dump_iface(struct usbd_interface
*iface
)
88 printf("usbd_dump_iface: iface=%p\n", iface
);
91 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
92 iface
->device
, iface
->idesc
, iface
->index
, iface
->altindex
,
97 usbd_dump_device(struct usbd_device
*dev
)
99 printf("usbd_dump_device: dev=%p\n", dev
);
102 printf(" bus=%p default_pipe=%p\n", dev
->bus
, dev
->default_pipe
);
103 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
104 "power=%d langid=%d\n",
105 dev
->address
, dev
->config
, dev
->depth
, dev
->speed
,
106 dev
->self_powered
, dev
->power
, dev
->langid
);
110 usbd_dump_endpoint(struct usbd_endpoint
*endp
)
112 printf("usbd_dump_endpoint: endp=%p\n", endp
);
115 printf(" edesc=%p refcnt=%d\n", endp
->edesc
, endp
->refcnt
);
117 printf(" bEndpointAddress=0x%02x\n",
118 endp
->edesc
->bEndpointAddress
);
122 usbd_dump_queue(usbd_pipe_handle pipe
)
124 usbd_xfer_handle xfer
;
126 printf("usbd_dump_queue: pipe=%p\n", pipe
);
127 SIMPLEQ_FOREACH(xfer
, &pipe
->queue
, next
) {
128 printf(" xfer=%p\n", xfer
);
133 usbd_dump_pipe(usbd_pipe_handle pipe
)
135 printf("usbd_dump_pipe: pipe=%p\n", pipe
);
138 usbd_dump_iface(pipe
->iface
);
139 usbd_dump_device(pipe
->device
);
140 usbd_dump_endpoint(pipe
->endpoint
);
141 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
142 pipe
->refcnt
, pipe
->running
, pipe
->aborting
);
143 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
144 pipe
->intrxfer
, pipe
->repeat
, pipe
->interval
);
149 usbd_open_pipe(usbd_interface_handle iface
, u_int8_t address
,
150 u_int8_t flags
, usbd_pipe_handle
*pipe
)
152 return (usbd_open_pipe_ival(iface
, address
, flags
, pipe
,
153 USBD_DEFAULT_INTERVAL
));
157 usbd_open_pipe_ival(usbd_interface_handle iface
, u_int8_t address
,
158 u_int8_t flags
, usbd_pipe_handle
*pipe
, int ival
)
161 struct usbd_endpoint
*ep
;
165 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
166 iface
, address
, flags
));
168 for (i
= 0; i
< iface
->idesc
->bNumEndpoints
; i
++) {
169 ep
= &iface
->endpoints
[i
];
170 if (ep
->edesc
== NULL
)
171 return (USBD_IOERROR
);
172 if (ep
->edesc
->bEndpointAddress
== address
)
175 return (USBD_BAD_ADDRESS
);
177 if ((flags
& USBD_EXCLUSIVE_USE
) && ep
->refcnt
!= 0)
178 return (USBD_IN_USE
);
179 err
= usbd_setup_pipe(iface
->device
, iface
, ep
, ival
, &p
);
182 LIST_INSERT_HEAD(&iface
->pipes
, p
, next
);
184 return (USBD_NORMAL_COMPLETION
);
188 usbd_open_pipe_intr(usbd_interface_handle iface
, u_int8_t address
,
189 u_int8_t flags
, usbd_pipe_handle
*pipe
,
190 usbd_private_handle priv
, void *buffer
, u_int32_t len
,
191 usbd_callback cb
, int ival
)
194 usbd_xfer_handle xfer
;
195 usbd_pipe_handle ipipe
;
197 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
198 address
, flags
, len
));
200 err
= usbd_open_pipe_ival(iface
, address
, USBD_EXCLUSIVE_USE
,
204 xfer
= usbd_alloc_xfer(iface
->device
);
209 usbd_setup_xfer(xfer
, ipipe
, priv
, buffer
, len
, flags
,
210 USBD_NO_TIMEOUT
, cb
);
211 ipipe
->intrxfer
= xfer
;
213 err
= usbd_transfer(xfer
);
215 if (err
!= USBD_IN_PROGRESS
)
217 return (USBD_NORMAL_COMPLETION
);
220 ipipe
->intrxfer
= NULL
;
222 usbd_free_xfer(xfer
);
224 usbd_close_pipe(ipipe
);
229 usbd_close_pipe(usbd_pipe_handle pipe
)
233 printf("usbd_close_pipe: pipe==NULL\n");
234 return (USBD_NORMAL_COMPLETION
);
238 if (--pipe
->refcnt
!= 0)
239 return (USBD_NORMAL_COMPLETION
);
240 if (! SIMPLEQ_EMPTY(&pipe
->queue
))
241 return (USBD_PENDING_REQUESTS
);
242 LIST_REMOVE(pipe
, next
);
243 pipe
->endpoint
->refcnt
--;
244 pipe
->methods
->close(pipe
);
245 if (pipe
->intrxfer
!= NULL
)
246 usbd_free_xfer(pipe
->intrxfer
);
248 return (USBD_NORMAL_COMPLETION
);
252 usbd_transfer(usbd_xfer_handle xfer
)
254 usbd_pipe_handle pipe
= xfer
->pipe
;
255 usb_dma_t
*dmap
= &xfer
->dmabuf
;
257 unsigned int size
, flags
;
260 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n",
261 xfer
, xfer
->flags
, pipe
, pipe
->running
));
264 usbd_dump_queue(pipe
);
269 return (USBD_CANCELLED
);
272 /* If there is no buffer, allocate one. */
273 if (!(xfer
->rqflags
& URQ_DEV_DMABUF
) && size
!= 0) {
274 struct usbd_bus
*bus
= pipe
->device
->bus
;
277 if (xfer
->rqflags
& URQ_AUTO_DMABUF
)
278 printf("usbd_transfer: has old buffer!\n");
280 err
= bus
->methods
->allocm(bus
, dmap
, size
);
283 xfer
->rqflags
|= URQ_AUTO_DMABUF
;
288 /* Copy data if going out. */
289 if (!(flags
& USBD_NO_COPY
) && size
!= 0 && !usbd_xfer_isread(xfer
))
290 memcpy(KERNADDR(dmap
, 0), xfer
->buffer
, size
);
292 /* xfer is not valid after the transfer method unless synchronous */
293 err
= pipe
->methods
->transfer(xfer
);
295 if (err
!= USBD_IN_PROGRESS
&& err
) {
296 /* The transfer has not been queued, so free buffer. */
297 if (xfer
->rqflags
& URQ_AUTO_DMABUF
) {
298 struct usbd_bus
*bus
= pipe
->device
->bus
;
300 bus
->methods
->freem(bus
, &xfer
->dmabuf
);
301 xfer
->rqflags
&= ~URQ_AUTO_DMABUF
;
305 if (!(flags
& USBD_SYNCHRONOUS
))
308 /* Sync transfer, wait for completion. */
309 if (err
!= USBD_IN_PROGRESS
)
313 if (pipe
->device
->bus
->use_polling
)
314 panic("usbd_transfer: not done");
315 tsleep(xfer
, PRIBIO
, "usbsyn", 0);
318 return (xfer
->status
);
321 /* Like usbd_transfer(), but waits for completion. */
323 usbd_sync_transfer(usbd_xfer_handle xfer
)
325 xfer
->flags
|= USBD_SYNCHRONOUS
;
326 return (usbd_transfer(xfer
));
330 usbd_alloc_buffer(usbd_xfer_handle xfer
, u_int32_t size
)
332 struct usbd_bus
*bus
= xfer
->device
->bus
;
336 if (xfer
->rqflags
& (URQ_DEV_DMABUF
| URQ_AUTO_DMABUF
))
337 printf("usbd_alloc_buffer: xfer already has a buffer\n");
339 err
= bus
->methods
->allocm(bus
, &xfer
->dmabuf
, size
);
342 xfer
->rqflags
|= URQ_DEV_DMABUF
;
343 return (KERNADDR(&xfer
->dmabuf
, 0));
347 usbd_free_buffer(usbd_xfer_handle xfer
)
350 if (!(xfer
->rqflags
& (URQ_DEV_DMABUF
| URQ_AUTO_DMABUF
))) {
351 printf("usbd_free_buffer: no buffer\n");
355 xfer
->rqflags
&= ~(URQ_DEV_DMABUF
| URQ_AUTO_DMABUF
);
356 xfer
->device
->bus
->methods
->freem(xfer
->device
->bus
, &xfer
->dmabuf
);
360 usbd_get_buffer(usbd_xfer_handle xfer
)
362 if (!(xfer
->rqflags
& URQ_DEV_DMABUF
))
364 return (KERNADDR(&xfer
->dmabuf
, 0));
368 usbd_alloc_xfer(usbd_device_handle dev
)
370 usbd_xfer_handle xfer
;
372 xfer
= dev
->bus
->methods
->allocx(dev
->bus
);
376 usb_callout_init(xfer
->timeout_handle
);
377 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer
));
382 usbd_free_xfer(usbd_xfer_handle xfer
)
384 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer
));
385 if (xfer
->rqflags
& (URQ_DEV_DMABUF
| URQ_AUTO_DMABUF
))
386 usbd_free_buffer(xfer
);
387 #if defined(DIAGNOSTIC)
388 if (callout_pending(&xfer
->timeout_handle
)) {
389 callout_stop(&xfer
->timeout_handle
);
390 printf("usbd_free_xfer: timout_handle pending");
393 xfer
->device
->bus
->methods
->freex(xfer
->device
->bus
, xfer
);
394 return (USBD_NORMAL_COMPLETION
);
398 usbd_setup_xfer(usbd_xfer_handle xfer
, usbd_pipe_handle pipe
,
399 usbd_private_handle priv
, void *buffer
, u_int32_t length
,
400 u_int16_t flags
, u_int32_t timeout
,
401 usbd_callback callback
)
405 xfer
->buffer
= buffer
;
406 xfer
->length
= length
;
409 xfer
->timeout
= timeout
;
410 xfer
->status
= USBD_NOT_STARTED
;
411 xfer
->callback
= callback
;
412 xfer
->rqflags
&= ~URQ_REQUEST
;
417 usbd_setup_default_xfer(usbd_xfer_handle xfer
, usbd_device_handle dev
,
418 usbd_private_handle priv
, u_int32_t timeout
,
419 usb_device_request_t
*req
, void *buffer
,
420 u_int32_t length
, u_int16_t flags
,
421 usbd_callback callback
)
423 xfer
->pipe
= dev
->default_pipe
;
425 xfer
->buffer
= buffer
;
426 xfer
->length
= length
;
429 xfer
->timeout
= timeout
;
430 xfer
->status
= USBD_NOT_STARTED
;
431 xfer
->callback
= callback
;
432 xfer
->request
= *req
;
433 xfer
->rqflags
|= URQ_REQUEST
;
438 usbd_setup_isoc_xfer(usbd_xfer_handle xfer
, usbd_pipe_handle pipe
,
439 usbd_private_handle priv
, u_int16_t
*frlengths
,
440 u_int32_t nframes
, u_int16_t flags
, usbd_callback callback
)
448 xfer
->timeout
= USBD_NO_TIMEOUT
;
449 xfer
->status
= USBD_NOT_STARTED
;
450 xfer
->callback
= callback
;
451 xfer
->rqflags
&= ~URQ_REQUEST
;
452 xfer
->frlengths
= frlengths
;
453 xfer
->nframes
= nframes
;
457 usbd_get_xfer_status(usbd_xfer_handle xfer
, usbd_private_handle
*priv
,
458 void **buffer
, u_int32_t
*count
, usbd_status
*status
)
463 *buffer
= xfer
->buffer
;
465 *count
= xfer
->actlen
;
467 *status
= xfer
->status
;
470 usb_config_descriptor_t
*
471 usbd_get_config_descriptor(usbd_device_handle dev
)
475 printf("usbd_get_config_descriptor: dev == NULL\n");
482 usb_interface_descriptor_t
*
483 usbd_get_interface_descriptor(usbd_interface_handle iface
)
487 printf("usbd_get_interface_descriptor: dev == NULL\n");
491 return (iface
->idesc
);
494 usb_device_descriptor_t
*
495 usbd_get_device_descriptor(usbd_device_handle dev
)
497 return (&dev
->ddesc
);
500 usb_endpoint_descriptor_t
*
501 usbd_interface2endpoint_descriptor(usbd_interface_handle iface
, u_int8_t index
)
503 if (index
>= iface
->idesc
->bNumEndpoints
)
505 return (iface
->endpoints
[index
].edesc
);
508 /* Some drivers may wish to abort requests on the default pipe, *
509 * but there is no mechanism for getting a handle on it. */
511 usbd_abort_default_pipe(struct usbd_device
*device
)
514 return usbd_abort_pipe(device
->default_pipe
);
518 usbd_abort_pipe(usbd_pipe_handle pipe
)
525 printf("usbd_abort_pipe: pipe==NULL\n");
526 return (USBD_NORMAL_COMPLETION
);
530 err
= usbd_ar_pipe(pipe
);
536 usbd_clear_endpoint_stall(usbd_pipe_handle pipe
)
538 usbd_device_handle dev
= pipe
->device
;
539 usb_device_request_t req
;
542 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
545 * Clearing en endpoint stall resets the endpoint toggle, so
546 * do the same to the HC toggle.
548 pipe
->methods
->cleartoggle(pipe
);
550 req
.bmRequestType
= UT_WRITE_ENDPOINT
;
551 req
.bRequest
= UR_CLEAR_FEATURE
;
552 USETW(req
.wValue
, UF_ENDPOINT_HALT
);
553 USETW(req
.wIndex
, pipe
->endpoint
->edesc
->bEndpointAddress
);
554 USETW(req
.wLength
, 0);
555 err
= usbd_do_request(dev
, &req
, 0);
557 XXX should we
do this?
559 pipe
->state
= USBD_PIPE_ACTIVE
;
560 /* XXX activate pipe */
567 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe
)
569 usbd_device_handle dev
= pipe
->device
;
570 usb_device_request_t req
;
573 pipe
->methods
->cleartoggle(pipe
);
575 req
.bmRequestType
= UT_WRITE_ENDPOINT
;
576 req
.bRequest
= UR_CLEAR_FEATURE
;
577 USETW(req
.wValue
, UF_ENDPOINT_HALT
);
578 USETW(req
.wIndex
, pipe
->endpoint
->edesc
->bEndpointAddress
);
579 USETW(req
.wLength
, 0);
580 err
= usbd_do_request_async(dev
, &req
, 0);
585 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe
)
587 pipe
->methods
->cleartoggle(pipe
);
591 usbd_endpoint_count(usbd_interface_handle iface
, u_int8_t
*count
)
594 if (iface
== NULL
|| iface
->idesc
== NULL
) {
595 printf("usbd_endpoint_count: NULL pointer\n");
599 *count
= iface
->idesc
->bNumEndpoints
;
600 return (USBD_NORMAL_COMPLETION
);
604 usbd_interface_count(usbd_device_handle dev
, u_int8_t
*count
)
606 if (dev
->cdesc
== NULL
)
607 return (USBD_NOT_CONFIGURED
);
608 *count
= dev
->cdesc
->bNumInterface
;
609 return (USBD_NORMAL_COMPLETION
);
613 usbd_interface2device_handle(usbd_interface_handle iface
,
614 usbd_device_handle
*dev
)
616 *dev
= iface
->device
;
620 usbd_device2interface_handle(usbd_device_handle dev
,
621 u_int8_t ifaceno
, usbd_interface_handle
*iface
)
623 if (dev
->cdesc
== NULL
)
624 return (USBD_NOT_CONFIGURED
);
625 if (ifaceno
>= dev
->cdesc
->bNumInterface
)
627 *iface
= &dev
->ifaces
[ifaceno
];
628 return (USBD_NORMAL_COMPLETION
);
632 usbd_pipe2device_handle(usbd_pipe_handle pipe
)
634 return (pipe
->device
);
639 usbd_set_interface(usbd_interface_handle iface
, int altidx
)
641 usb_device_request_t req
;
645 if (LIST_FIRST(&iface
->pipes
) != 0)
646 return (USBD_IN_USE
);
648 endpoints
= iface
->endpoints
;
649 err
= usbd_fill_iface_data(iface
->device
, iface
->index
, altidx
);
653 /* new setting works, we can free old endpoints */
654 if (endpoints
!= NULL
)
655 free(endpoints
, M_USB
);
658 if (iface
->idesc
== NULL
) {
659 printf("usbd_set_interface: NULL pointer\n");
664 req
.bmRequestType
= UT_WRITE_INTERFACE
;
665 req
.bRequest
= UR_SET_INTERFACE
;
666 USETW(req
.wValue
, iface
->idesc
->bAlternateSetting
);
667 USETW(req
.wIndex
, iface
->idesc
->bInterfaceNumber
);
668 USETW(req
.wLength
, 0);
669 return (usbd_do_request(iface
->device
, &req
, 0));
673 usbd_get_no_alts(usb_config_descriptor_t
*cdesc
, int ifaceno
)
675 char *p
= (char *)cdesc
;
676 char *end
= p
+ UGETW(cdesc
->wTotalLength
);
677 usb_interface_descriptor_t
*d
;
680 for (n
= 0; p
< end
; p
+= d
->bLength
) {
681 d
= (usb_interface_descriptor_t
*)p
;
682 if (p
+ d
->bLength
<= end
&&
683 d
->bDescriptorType
== UDESC_INTERFACE
&&
684 d
->bInterfaceNumber
== ifaceno
)
691 usbd_get_interface_altindex(usbd_interface_handle iface
)
693 return (iface
->altindex
);
697 usbd_get_interface(usbd_interface_handle iface
, u_int8_t
*aiface
)
699 usb_device_request_t req
;
701 req
.bmRequestType
= UT_READ_INTERFACE
;
702 req
.bRequest
= UR_GET_INTERFACE
;
703 USETW(req
.wValue
, 0);
704 USETW(req
.wIndex
, iface
->idesc
->bInterfaceNumber
);
705 USETW(req
.wLength
, 1);
706 return (usbd_do_request(iface
->device
, &req
, aiface
));
709 /*** Internal routines ***/
711 /* Dequeue all pipe operations, called at splusb(). */
713 usbd_ar_pipe(usbd_pipe_handle pipe
)
715 usbd_xfer_handle xfer
;
719 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe
));
722 usbd_dump_queue(pipe
);
726 while ((xfer
= SIMPLEQ_FIRST(&pipe
->queue
)) != NULL
) {
727 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
728 pipe
, xfer
, pipe
->methods
));
729 /* Make the HC abort it (and invoke the callback). */
730 pipe
->methods
->abort(xfer
);
731 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
734 return (USBD_NORMAL_COMPLETION
);
737 /* Called at splusb() */
739 usb_transfer_complete(usbd_xfer_handle xfer
)
741 usbd_pipe_handle pipe
= xfer
->pipe
;
742 usb_dma_t
*dmap
= &xfer
->dmabuf
;
743 int sync
= xfer
->flags
& USBD_SYNCHRONOUS
;
744 int erred
= xfer
->status
== USBD_CANCELLED
||
745 xfer
->status
== USBD_TIMEOUT
;
750 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
751 "actlen=%d\n", pipe
, xfer
, xfer
->status
, xfer
->actlen
));
753 if (xfer
->busy_free
!= XFER_ONQU
) {
754 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
755 xfer
, xfer
->busy_free
);
761 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer
);
765 repeat
= pipe
->repeat
;
766 polling
= pipe
->device
->bus
->use_polling
;
771 if (!(xfer
->flags
& USBD_NO_COPY
) && xfer
->actlen
!= 0 &&
772 usbd_xfer_isread(xfer
)) {
774 if (xfer
->actlen
> xfer
->length
) {
775 printf("usb_transfer_complete: actlen > len %d > %d\n",
776 xfer
->actlen
, xfer
->length
);
777 xfer
->actlen
= xfer
->length
;
780 memcpy(xfer
->buffer
, KERNADDR(dmap
, 0), xfer
->actlen
);
783 /* if we allocated the buffer in usbd_transfer() we free it here. */
784 if (xfer
->rqflags
& URQ_AUTO_DMABUF
) {
786 struct usbd_bus
*bus
= pipe
->device
->bus
;
787 bus
->methods
->freem(bus
, dmap
);
788 xfer
->rqflags
&= ~URQ_AUTO_DMABUF
;
793 /* Remove request from queue. */
795 if (xfer
!= SIMPLEQ_FIRST(&pipe
->queue
))
796 printf("usb_transfer_complete: bad dequeue %p != %p\n",
797 xfer
, SIMPLEQ_FIRST(&pipe
->queue
));
798 xfer
->busy_free
= XFER_BUSY
;
800 SIMPLEQ_REMOVE_HEAD(&pipe
->queue
, next
);
802 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
803 repeat
, SIMPLEQ_FIRST(&pipe
->queue
)));
805 /* Count completed transfers. */
806 ++pipe
->device
->bus
->stats
.uds_requests
807 [pipe
->endpoint
->edesc
->bmAttributes
& UE_XFERTYPE
];
810 if (!xfer
->status
&& xfer
->actlen
< xfer
->length
&&
811 !(xfer
->flags
& USBD_SHORT_XFER_OK
)) {
812 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
813 xfer
->actlen
, xfer
->length
));
814 xfer
->status
= USBD_SHORT_XFER
;
819 xfer
->callback(xfer
, xfer
->priv
, xfer
->status
);
820 pipe
->methods
->done(xfer
);
822 pipe
->methods
->done(xfer
);
824 xfer
->callback(xfer
, xfer
->priv
, xfer
->status
);
827 if (sync
&& !polling
)
831 /* XXX should we stop the queue on all errors? */
832 if (erred
&& pipe
->iface
!= NULL
) /* not control pipe */
835 usbd_start_next(pipe
);
840 usb_insert_transfer(usbd_xfer_handle xfer
)
842 usbd_pipe_handle pipe
= xfer
->pipe
;
846 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
847 pipe
, pipe
->running
, xfer
->timeout
));
849 if (xfer
->busy_free
!= XFER_BUSY
) {
850 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
851 xfer
, xfer
->busy_free
);
854 xfer
->busy_free
= XFER_ONQU
;
857 SIMPLEQ_INSERT_TAIL(&pipe
->queue
, xfer
, next
);
859 err
= USBD_IN_PROGRESS
;
862 err
= USBD_NORMAL_COMPLETION
;
868 /* Called at splusb() */
870 usbd_start_next(usbd_pipe_handle pipe
)
872 usbd_xfer_handle xfer
;
879 printf("usbd_start_next: pipe == NULL\n");
882 if (pipe
->methods
== NULL
|| pipe
->methods
->start
== NULL
) {
883 printf("usbd_start_next: pipe=%p no start method\n", pipe
);
888 /* Get next request in queue. */
889 xfer
= SIMPLEQ_FIRST(&pipe
->queue
);
890 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe
, xfer
));
894 err
= pipe
->methods
->start(xfer
);
895 if (err
!= USBD_IN_PROGRESS
) {
896 printf("usbd_start_next: error=%d\n", err
);
904 usbd_do_request(usbd_device_handle dev
, usb_device_request_t
*req
, void *data
)
906 return (usbd_do_request_flags(dev
, req
, data
, 0, 0,
907 USBD_DEFAULT_TIMEOUT
));
911 usbd_do_request_flags(usbd_device_handle dev
, usb_device_request_t
*req
,
912 void *data
, u_int16_t flags
, int *actlen
, u_int32_t timo
)
914 return (usbd_do_request_flags_pipe(dev
, dev
->default_pipe
, req
,
915 data
, flags
, actlen
, timo
));
919 usbd_do_request_flags_pipe(usbd_device_handle dev
, usbd_pipe_handle pipe
,
920 usb_device_request_t
*req
, void *data
, u_int16_t flags
, int *actlen
,
923 usbd_xfer_handle xfer
;
927 if (dev
->bus
->intr_context
) {
928 printf("usbd_do_request: not in process context\n");
933 xfer
= usbd_alloc_xfer(dev
);
936 usbd_setup_default_xfer(xfer
, dev
, 0, timeout
, req
,
937 data
, UGETW(req
->wLength
), flags
, 0);
939 err
= usbd_sync_transfer(xfer
);
940 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
941 if (xfer
->actlen
> xfer
->length
) {
942 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
943 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
944 dev
->address
, xfer
->request
.bmRequestType
,
945 xfer
->request
.bRequest
, UGETW(xfer
->request
.wValue
),
946 UGETW(xfer
->request
.wIndex
),
947 UGETW(xfer
->request
.wLength
),
948 xfer
->length
, xfer
->actlen
));
952 *actlen
= xfer
->actlen
;
953 if (err
== USBD_STALLED
) {
955 * The control endpoint has stalled. Control endpoints
956 * should not halt, but some may do so anyway so clear
957 * any halt condition.
959 usb_device_request_t treq
;
964 treq
.bmRequestType
= UT_READ_ENDPOINT
;
965 treq
.bRequest
= UR_GET_STATUS
;
966 USETW(treq
.wValue
, 0);
967 USETW(treq
.wIndex
, 0);
968 USETW(treq
.wLength
, sizeof(usb_status_t
));
969 usbd_setup_default_xfer(xfer
, dev
, 0, USBD_DEFAULT_TIMEOUT
,
970 &treq
, &status
,sizeof(usb_status_t
),
972 nerr
= usbd_sync_transfer(xfer
);
975 s
= UGETW(status
.wStatus
);
976 DPRINTF(("usbd_do_request: status = 0x%04x\n", s
));
979 treq
.bmRequestType
= UT_WRITE_ENDPOINT
;
980 treq
.bRequest
= UR_CLEAR_FEATURE
;
981 USETW(treq
.wValue
, UF_ENDPOINT_HALT
);
982 USETW(treq
.wIndex
, 0);
983 USETW(treq
.wLength
, 0);
984 usbd_setup_default_xfer(xfer
, dev
, 0, USBD_DEFAULT_TIMEOUT
,
985 &treq
, &status
, 0, 0, 0);
986 nerr
= usbd_sync_transfer(xfer
);
992 usbd_free_xfer(xfer
);
997 usbd_do_request_async_cb(usbd_xfer_handle xfer
,
998 usbd_private_handle priv
, usbd_status status
)
1000 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1001 if (xfer
->actlen
> xfer
->length
) {
1002 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1003 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1004 xfer
->pipe
->device
->address
,
1005 xfer
->request
.bmRequestType
,
1006 xfer
->request
.bRequest
, UGETW(xfer
->request
.wValue
),
1007 UGETW(xfer
->request
.wIndex
),
1008 UGETW(xfer
->request
.wLength
),
1009 xfer
->length
, xfer
->actlen
));
1012 usbd_free_xfer(xfer
);
1016 * Execute a request without waiting for completion.
1017 * Can be used from interrupt context.
1020 usbd_do_request_async(usbd_device_handle dev
, usb_device_request_t
*req
,
1023 usbd_xfer_handle xfer
;
1026 xfer
= usbd_alloc_xfer(dev
);
1028 return (USBD_NOMEM
);
1029 usbd_setup_default_xfer(xfer
, dev
, 0, USBD_DEFAULT_TIMEOUT
, req
,
1030 data
, UGETW(req
->wLength
), 0, usbd_do_request_async_cb
);
1031 err
= usbd_transfer(xfer
);
1032 if (err
!= USBD_IN_PROGRESS
) {
1033 usbd_free_xfer(xfer
);
1036 return (USBD_NORMAL_COMPLETION
);
1039 const struct usbd_quirks
*
1040 usbd_get_quirks(usbd_device_handle dev
)
1044 printf("usbd_get_quirks: dev == NULL\n");
1048 return (dev
->quirks
);
1051 /* XXX do periodic free() of free list */
1054 * Called from keyboard driver when in polling mode.
1057 usbd_dopoll(usbd_interface_handle iface
)
1059 iface
->device
->bus
->methods
->do_poll(iface
->device
->bus
);
1063 usbd_set_polling(usbd_device_handle dev
, int on
)
1066 dev
->bus
->use_polling
++;
1068 dev
->bus
->use_polling
--;
1069 /* When polling we need to make sure there is nothing pending to do. */
1070 if (dev
->bus
->use_polling
)
1071 dev
->bus
->methods
->soft_intr(dev
->bus
);
1075 usb_endpoint_descriptor_t
*
1076 usbd_get_endpoint_descriptor(usbd_interface_handle iface
, u_int8_t address
)
1078 struct usbd_endpoint
*ep
;
1081 for (i
= 0; i
< iface
->idesc
->bNumEndpoints
; i
++) {
1082 ep
= &iface
->endpoints
[i
];
1083 if (ep
->edesc
->bEndpointAddress
== address
)
1084 return (iface
->endpoints
[i
].edesc
);
1090 * usbd_ratecheck() can limit the number of error messages that occurs.
1091 * When a device is unplugged it may take up to 0.25s for the hub driver
1092 * to notice it. If the driver continuosly tries to do I/O operations
1093 * this can generate a large number of messages.
1096 usbd_ratecheck(struct timeval
*last
)
1098 static struct timeval errinterval
= { 0, 250000 }; /* 0.25 s*/
1100 return (ratecheck(last
, &errinterval
));
1104 * Search for a vendor/product pair in an array. The item size is
1105 * given as an argument.
1107 const struct usb_devno
*
1108 usb_match_device(const struct usb_devno
*tbl
, u_int nentries
, u_int sz
,
1109 u_int16_t vendor
, u_int16_t product
)
1111 while (nentries
-- > 0) {
1112 u_int16_t tproduct
= tbl
->ud_product
;
1113 if (tbl
->ud_vendor
== vendor
&&
1114 (tproduct
== product
|| tproduct
== USB_PRODUCT_ANY
))
1116 tbl
= (const struct usb_devno
*)((const char *)tbl
+ sz
);
1123 usb_desc_iter_init(usbd_device_handle dev
, usbd_desc_iter_t
*iter
)
1125 const usb_config_descriptor_t
*cd
= usbd_get_config_descriptor(dev
);
1127 iter
->cur
= (const uByte
*)cd
;
1128 iter
->end
= (const uByte
*)cd
+ UGETW(cd
->wTotalLength
);
1131 const usb_descriptor_t
*
1132 usb_desc_iter_next(usbd_desc_iter_t
*iter
)
1134 const usb_descriptor_t
*desc
;
1136 if (iter
->cur
+ sizeof(usb_descriptor_t
) >= iter
->end
) {
1137 if (iter
->cur
!= iter
->end
)
1138 printf("usb_desc_iter_next: bad descriptor\n");
1141 desc
= (const usb_descriptor_t
*)iter
->cur
;
1142 if (desc
->bLength
== 0) {
1143 printf("usb_desc_iter_next: descriptor length = 0\n");
1146 iter
->cur
+= desc
->bLength
;
1147 if (iter
->cur
> iter
->end
) {
1148 printf("usb_desc_iter_next: descriptor length too large\n");
1155 usbd_get_string(usbd_device_handle dev
, int si
, char *buf
)
1157 return usbd_get_string0(dev
, si
, buf
, 1);
1161 usbd_get_string0(usbd_device_handle dev
, int si
, char *buf
, int unicode
)
1163 int swap
= dev
->quirks
->uq_flags
& UQ_SWAP_UNICODE
;
1164 usb_string_descriptor_t us
;
1173 return (USBD_INVAL
);
1174 if (dev
->quirks
->uq_flags
& UQ_NO_STRINGS
)
1175 return (USBD_STALLED
);
1176 if (dev
->langid
== USBD_NOLANG
) {
1177 /* Set up default language */
1178 err
= usbd_get_string_desc(dev
, USB_LANGUAGE_TABLE
, 0, &us
,
1180 if (err
|| size
< 4) {
1181 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1182 dev
->langid
= 0; /* Well, just pick something then */
1184 /* Pick the first language as the default. */
1185 dev
->langid
= UGETW(us
.bString
[0]);
1188 err
= usbd_get_string_desc(dev
, si
, dev
->langid
, &us
, &size
);
1194 for (i
= 0; i
< n
; i
++) {
1195 c
= UGETW(us
.bString
[i
]);
1197 c
= (c
>> 8) | (c
<< 8);
1198 s
+= wput_utf8(s
, 3, c
);
1204 for (i
= 0; i
< n
; i
++) {
1205 c
= UGETW(us
.bString
[i
]);
1207 c
= (c
>> 8) | (c
<< 8);
1208 *s
++ = (c
< 0x80) ? c
: '?';
1213 return (USBD_NORMAL_COMPLETION
);