3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/linker_set.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usb_ioctl.h>
53 #include <dev/usb/usbhid.h>
55 #define USB_DEBUG_VAR usb_debug
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_request.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_transfer.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_dynamic.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <sys/ctype.h>
72 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
73 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
74 static int usb_ss_delay = 0;
76 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
77 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
78 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
79 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
80 SYSCTL_INT(_hw_usb, OID_AUTO, ss_delay, CTLFLAG_RW,
81 &usb_ss_delay, 0, "USB status stage delay in ms");
84 /*------------------------------------------------------------------------*
85 * usbd_do_request_callback
87 * This function is the USB callback for generic USB Host control
89 *------------------------------------------------------------------------*/
91 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
93 ; /* workaround for a bug in "indent" */
95 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
97 switch (USB_GET_STATE(xfer)) {
99 usbd_transfer_submit(xfer);
102 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
107 /*------------------------------------------------------------------------*
108 * usb_do_clear_stall_callback
110 * This function is the USB callback for generic clear stall requests.
111 *------------------------------------------------------------------------*/
113 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
115 struct usb_device_request req;
116 struct usb_device *udev;
117 struct usb_endpoint *ep;
118 struct usb_endpoint *ep_end;
119 struct usb_endpoint *ep_first;
122 udev = xfer->xroot->udev;
124 USB_BUS_LOCK(udev->bus);
126 /* round robin endpoint clear stall */
129 ep_end = udev->endpoints + udev->endpoints_max;
130 ep_first = udev->endpoints;
131 to = udev->endpoints_max;
133 switch (USB_GET_STATE(xfer)) {
134 case USB_ST_TRANSFERRED:
136 goto tr_setup; /* device was unconfigured */
141 /* start up the current or next transfer, if any */
142 usb_command_wrapper(&ep->endpoint_q,
143 ep->endpoint_q.curr);
150 break; /* no endpoints - nothing to do */
151 if ((ep < ep_first) || (ep >= ep_end))
152 ep = ep_first; /* endpoint wrapped around */
156 /* setup a clear-stall packet */
158 req.bmRequestType = UT_WRITE_ENDPOINT;
159 req.bRequest = UR_CLEAR_FEATURE;
160 USETW(req.wValue, UF_ENDPOINT_HALT);
161 req.wIndex[0] = ep->edesc->bEndpointAddress;
163 USETW(req.wLength, 0);
165 /* copy in the transfer */
167 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
170 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
172 USB_BUS_UNLOCK(udev->bus);
174 usbd_transfer_submit(xfer);
176 USB_BUS_LOCK(udev->bus);
184 if (xfer->error == USB_ERR_CANCELLED) {
190 /* store current endpoint */
192 USB_BUS_UNLOCK(udev->bus);
195 static usb_handle_req_t *
196 usbd_get_hr_func(struct usb_device *udev)
198 /* figure out if there is a Handle Request function */
199 if (udev->flags.usb_mode == USB_MODE_DEVICE)
200 return (usb_temp_get_desc_p);
201 else if (udev->parent_hub == NULL)
202 return (udev->bus->methods->roothub_exec);
207 /*------------------------------------------------------------------------*
208 * usbd_do_request_flags and usbd_do_request
210 * Description of arguments passed to these functions:
212 * "udev" - this is the "usb_device" structure pointer on which the
213 * request should be performed. It is possible to call this function
214 * in both Host Side mode and Device Side mode.
216 * "mtx" - if this argument is non-NULL the mutex pointed to by it
217 * will get dropped and picked up during the execution of this
218 * function, hence this function sometimes needs to sleep. If this
219 * argument is NULL it has no effect.
221 * "req" - this argument must always be non-NULL and points to an
222 * 8-byte structure holding the USB request to be done. The USB
223 * request structure has a bit telling the direction of the USB
224 * request, if it is a read or a write.
226 * "data" - if the "wLength" part of the structure pointed to by "req"
227 * is non-zero this argument must point to a valid kernel buffer which
228 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
231 * "flags" - here is a list of valid flags:
233 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
236 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
237 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
238 * sysctl. This flag is mostly useful for debugging.
240 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
243 * "actlen" - if non-NULL the actual transfer length will be stored in
244 * the 16-bit unsigned integer pointed to by "actlen". This
245 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
248 * "timeout" - gives the timeout for the control transfer in
249 * milliseconds. A "timeout" value less than 50 milliseconds is
250 * treated like a 50 millisecond timeout. A "timeout" value greater
251 * than 30 seconds is treated like a 30 second timeout. This USB stack
252 * does not allow control requests without a timeout.
254 * NOTE: This function is thread safe. All calls to
255 * "usbd_do_request_flags" will be serialised by the use of an
256 * internal "sx_lock".
261 *------------------------------------------------------------------------*/
263 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
264 struct usb_device_request *req, void *data, uint16_t flags,
265 uint16_t *actlen, usb_timeout_t timeout)
267 usb_handle_req_t *hr_func;
268 struct usb_xfer *xfer;
271 usb_ticks_t start_ticks;
272 usb_ticks_t delta_ticks;
273 usb_ticks_t max_ticks;
279 /* timeout is too small */
282 if (timeout > 30000) {
283 /* timeout is too big */
286 length = UGETW(req->wLength);
288 enum_locked = usbd_enum_is_locked(udev);
290 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
291 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
292 udev, req->bmRequestType, req->bRequest,
293 req->wValue[1], req->wValue[0],
294 req->wIndex[1], req->wIndex[0],
295 req->wLength[1], req->wLength[0]);
297 /* Check if the device is still alive */
298 if (udev->state < USB_STATE_POWERED) {
299 DPRINTF("usb device has gone\n");
300 return (USB_ERR_NOT_CONFIGURED);
304 * Set "actlen" to a known value in case the caller does not
305 * check the return value:
310 #if (USB_HAVE_USER_IO == 0)
311 if (flags & USB_USER_DATA_PTR)
312 return (USB_ERR_INVAL);
314 if ((mtx != NULL) && (mtx != &Giant)) {
316 mtx_assert(mtx, MA_NOTOWNED);
320 * We need to allow suspend and resume at this point, else the
321 * control transfer will timeout if the device is suspended!
324 usbd_sr_unlock(udev);
327 * Grab the default sx-lock so that serialisation
328 * is achieved when multiple threads are involved:
331 sx_xlock(&udev->ctrl_sx);
333 hr_func = usbd_get_hr_func(udev);
335 if (hr_func != NULL) {
336 DPRINTF("Handle Request function is set\n");
341 if (!(req->bmRequestType & UT_READ)) {
343 DPRINTFN(1, "The handle request function "
344 "does not support writing data!\n");
350 /* The root HUB code needs the BUS lock locked */
352 USB_BUS_LOCK(udev->bus);
353 err = (hr_func) (udev, req, &desc, &temp);
354 USB_BUS_UNLOCK(udev->bus);
360 if (!(flags & USB_SHORT_XFER_OK)) {
361 err = USB_ERR_SHORT_XFER;
371 if (flags & USB_USER_DATA_PTR) {
372 if (copyout(desc, data, length)) {
378 bcopy(desc, data, length);
380 goto done; /* success */
384 * Setup a new USB transfer or use the existing one, if any:
386 usbd_ctrl_transfer_setup(udev);
388 xfer = udev->ctrl_xfer[0];
390 /* most likely out of memory */
396 if (flags & USB_DELAY_STATUS_STAGE)
397 xfer->flags.manual_status = 1;
399 xfer->flags.manual_status = 0;
401 if (flags & USB_SHORT_XFER_OK)
402 xfer->flags.short_xfer_ok = 1;
404 xfer->flags.short_xfer_ok = 0;
406 xfer->timeout = timeout;
410 max_ticks = USB_MS_TO_TICKS(timeout);
412 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
414 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
419 if (temp > xfer->max_data_length) {
420 temp = usbd_xfer_max_len(xfer);
422 usbd_xfer_set_frame_len(xfer, 1, temp);
425 if (!(req->bmRequestType & UT_READ)) {
427 if (flags & USB_USER_DATA_PTR) {
428 USB_XFER_UNLOCK(xfer);
429 err = usbd_copy_in_user(xfer->frbuffers + 1,
438 usbd_copy_in(xfer->frbuffers + 1,
443 if (xfer->frlengths[0] == 0) {
444 if (xfer->flags.manual_status) {
454 xfer->xroot->xfer_mtx,
455 USB_MS_TO_TICKS(temp));
458 xfer->flags.manual_status = 0;
466 usbd_transfer_start(xfer);
468 while (usbd_transfer_pending(xfer)) {
469 cv_wait(&udev->ctrlreq_cv,
470 xfer->xroot->xfer_mtx);
478 /* subtract length of SETUP packet, if any */
480 if (xfer->aframes > 0) {
481 xfer->actlen -= xfer->frlengths[0];
486 /* check for short packet */
488 if (temp > xfer->actlen) {
493 if (req->bmRequestType & UT_READ) {
495 if (flags & USB_USER_DATA_PTR) {
496 USB_XFER_UNLOCK(xfer);
497 err = usbd_copy_out_user(xfer->frbuffers + 1,
506 usbd_copy_out(xfer->frbuffers + 1,
511 * Clear "frlengths[0]" so that we don't send the setup
514 usbd_xfer_set_frame_len(xfer, 0, 0);
516 /* update length and data pointer */
518 data = USB_ADD_BYTES(data, temp);
523 /* check for timeout */
525 delta_ticks = ticks - start_ticks;
526 if (delta_ticks > max_ticks) {
528 err = USB_ERR_TIMEOUT;
538 * Make sure that the control endpoint is no longer
539 * blocked in case of a non-transfer related error:
541 usbd_transfer_stop(xfer);
543 USB_XFER_UNLOCK(xfer);
546 sx_xunlock(&udev->ctrl_sx);
551 if ((mtx != NULL) && (mtx != &Giant))
554 return ((usb_error_t)err);
557 /*------------------------------------------------------------------------*
558 * usbd_do_request_proc - factored out code
560 * This function is factored out code. It does basically the same like
561 * usbd_do_request_flags, except it will check the status of the
562 * passed process argument before doing the USB request. If the
563 * process is draining the USB_ERR_IOERROR code will be returned. It
564 * is assumed that the mutex associated with the process is locked
565 * when calling this function.
566 *------------------------------------------------------------------------*/
568 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
569 struct usb_device_request *req, void *data, uint16_t flags,
570 uint16_t *actlen, usb_timeout_t timeout)
575 /* get request data length */
576 len = UGETW(req->wLength);
578 /* check if the device is being detached */
579 if (usb_proc_is_gone(pproc)) {
580 err = USB_ERR_IOERROR;
584 /* forward the USB request */
585 err = usbd_do_request_flags(udev, pproc->up_mtx,
586 req, data, flags, actlen, timeout);
589 /* on failure we zero the data */
590 /* on short packet we zero the unused data */
591 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
593 memset(data, 0, len);
594 else if (actlen && *actlen != len)
595 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
600 /*------------------------------------------------------------------------*
601 * usbd_req_reset_port
603 * This function will instruct an USB HUB to perform a reset sequence
604 * on the specified port number.
607 * 0: Success. The USB device should now be at address zero.
608 * Else: Failure. No USB device is present and the USB port should be
610 *------------------------------------------------------------------------*/
612 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
614 struct usb_port_status ps;
619 uint16_t pr_poll_delay;
620 uint16_t pr_recovery_delay;
623 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
628 /* range check input parameters */
629 pr_poll_delay = usb_pr_poll_delay;
630 if (pr_poll_delay < 1) {
632 } else if (pr_poll_delay > 1000) {
633 pr_poll_delay = 1000;
635 pr_recovery_delay = usb_pr_recovery_delay;
636 if (pr_recovery_delay > 1000) {
637 pr_recovery_delay = 1000;
643 /* wait for the device to recover from reset */
644 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
647 /* wait for the device to recover from reset */
648 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
649 n += USB_PORT_RESET_DELAY;
651 err = usbd_req_get_port_status(udev, mtx, &ps, port);
655 /* if the device disappeared, just give up */
656 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
659 /* check if reset is complete */
660 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
663 /* check for timeout */
670 /* clear port reset first */
671 err = usbd_req_clear_port_feature(
672 udev, mtx, port, UHF_C_PORT_RESET);
676 /* check for timeout */
678 err = USB_ERR_TIMEOUT;
682 /* wait for the device to recover from reset */
683 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
685 /* wait for the device to recover from reset */
686 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
690 DPRINTFN(2, "port %d reset returning error=%s\n",
691 port, usbd_errstr(err));
695 /*------------------------------------------------------------------------*
698 * This function can be used to retrieve USB descriptors. It contains
699 * some additional logic like zeroing of missing descriptor bytes and
700 * retrying an USB descriptor in case of failure. The "min_len"
701 * argument specifies the minimum descriptor length. The "max_len"
702 * argument specifies the maximum descriptor length. If the real
703 * descriptor length is less than the minimum length the missing
704 * byte(s) will be zeroed. The type field, the second byte of the USB
705 * descriptor, will get forced to the correct type. If the "actlen"
706 * pointer is non-NULL, the actual length of the transfer will get
707 * stored in the 16-bit unsigned integer which it is pointing to. The
708 * first byte of the descriptor will not get updated. If the "actlen"
709 * pointer is NULL the first byte of the descriptor will get updated
710 * to reflect the actual length instead. If "min_len" is not equal to
711 * "max_len" then this function will try to retrive the beginning of
712 * the descriptor and base the maximum length on the first byte of the
718 *------------------------------------------------------------------------*/
720 usbd_req_get_desc(struct usb_device *udev,
721 struct mtx *mtx, uint16_t *actlen, void *desc,
722 uint16_t min_len, uint16_t max_len,
723 uint16_t id, uint8_t type, uint8_t index,
726 struct usb_device_request req;
730 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
731 id, type, index, max_len);
733 req.bmRequestType = UT_READ_DEVICE;
734 req.bRequest = UR_GET_DESCRIPTOR;
735 USETW2(req.wValue, type, index);
736 USETW(req.wIndex, id);
740 if ((min_len < 2) || (max_len < 2)) {
744 USETW(req.wLength, min_len);
746 err = usbd_do_request_flags(udev, mtx, &req,
747 desc, 0, NULL, 1000);
755 usb_pause_mtx(mtx, hz / 5);
761 if (min_len == max_len) {
763 /* enforce correct length */
764 if ((buf[0] > min_len) && (actlen == NULL))
767 /* enforce correct type */
774 if (max_len > buf[0]) {
777 /* zero minimum data */
779 while (min_len > max_len) {
784 /* set new minimum length */
789 if (actlen != NULL) {
798 /*------------------------------------------------------------------------*
799 * usbd_req_get_string_any
801 * This function will return the string given by "string_index"
802 * using the first language ID. The maximum length "len" includes
803 * the terminating zero. The "len" argument should be twice as
804 * big pluss 2 bytes, compared with the actual maximum string length !
809 *------------------------------------------------------------------------*/
811 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
812 uint16_t len, uint8_t string_index)
823 /* should not happen */
824 return (USB_ERR_NORMAL_COMPLETION);
826 if (string_index == 0) {
827 /* this is the language table */
829 return (USB_ERR_INVAL);
831 if (udev->flags.no_strings) {
833 return (USB_ERR_STALLED);
835 err = usbd_req_get_string_desc
836 (udev, mtx, buf, len, udev->langid, string_index);
841 temp = (uint8_t *)buf;
844 /* string length is too short */
846 return (USB_ERR_INVAL);
848 /* reserve one byte for terminating zero */
851 /* find maximum length */
853 n = (temp[0] / 2) - 1;
857 /* skip descriptor header */
860 /* reset swap state */
863 /* convert and filter */
864 for (i = 0; (i != n); i++) {
865 c = UGETW(temp + (2 * i));
867 /* convert from Unicode, handle buggy strings */
868 if (((c & 0xff00) == 0) && (swap & 1)) {
869 /* Little Endian, default */
872 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
877 /* silently skip bad character */
882 * Filter by default - we don't allow greater and less than
883 * signs because they might confuse the dmesg printouts!
885 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
886 /* silently skip bad character */
891 *s = 0; /* zero terminate resulting string */
892 return (USB_ERR_NORMAL_COMPLETION);
895 /*------------------------------------------------------------------------*
896 * usbd_req_get_string_desc
898 * If you don't know the language ID, consider using
899 * "usbd_req_get_string_any()".
904 *------------------------------------------------------------------------*/
906 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
907 uint16_t max_len, uint16_t lang_id,
908 uint8_t string_index)
910 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
911 UDESC_STRING, string_index, 0));
914 /*------------------------------------------------------------------------*
915 * usbd_req_get_config_desc_ptr
917 * This function is used in device side mode to retrieve the pointer
918 * to the generated config descriptor. This saves allocating space for
919 * an additional config descriptor when setting the configuration.
924 *------------------------------------------------------------------------*/
926 usbd_req_get_descriptor_ptr(struct usb_device *udev,
927 struct usb_config_descriptor **ppcd, uint16_t wValue)
929 struct usb_device_request req;
930 usb_handle_req_t *hr_func;
935 req.bmRequestType = UT_READ_DEVICE;
936 req.bRequest = UR_GET_DESCRIPTOR;
937 USETW(req.wValue, wValue);
938 USETW(req.wIndex, 0);
939 USETW(req.wLength, 0);
944 hr_func = usbd_get_hr_func(udev);
949 USB_BUS_LOCK(udev->bus);
950 err = (hr_func) (udev, &req, &ptr, &len);
951 USB_BUS_UNLOCK(udev->bus);
956 else if (ptr == NULL)
959 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
964 /*------------------------------------------------------------------------*
965 * usbd_req_get_config_desc
970 *------------------------------------------------------------------------*/
972 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
973 struct usb_config_descriptor *d, uint8_t conf_index)
977 DPRINTFN(4, "confidx=%d\n", conf_index);
979 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
980 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
984 /* Extra sanity checking */
985 if (UGETW(d->wTotalLength) < sizeof(*d)) {
992 /*------------------------------------------------------------------------*
993 * usbd_req_get_config_desc_full
995 * This function gets the complete USB configuration descriptor and
996 * ensures that "wTotalLength" is correct.
1001 *------------------------------------------------------------------------*/
1003 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1004 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1007 struct usb_config_descriptor cd;
1008 struct usb_config_descriptor *cdesc;
1012 DPRINTFN(4, "index=%d\n", index);
1016 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1020 /* get full descriptor */
1021 len = UGETW(cd.wTotalLength);
1022 if (len < sizeof(*cdesc)) {
1023 /* corrupt descriptor */
1024 return (USB_ERR_INVAL);
1026 cdesc = malloc(len, mtype, M_WAITOK);
1027 if (cdesc == NULL) {
1028 return (USB_ERR_NOMEM);
1030 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1031 UDESC_CONFIG, index, 3);
1036 /* make sure that the device is not fooling us: */
1037 USETW(cdesc->wTotalLength, len);
1041 return (0); /* success */
1044 /*------------------------------------------------------------------------*
1045 * usbd_req_get_device_desc
1050 *------------------------------------------------------------------------*/
1052 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1053 struct usb_device_descriptor *d)
1056 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1057 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1060 /*------------------------------------------------------------------------*
1061 * usbd_req_get_alt_interface_no
1066 *------------------------------------------------------------------------*/
1068 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1069 uint8_t *alt_iface_no, uint8_t iface_index)
1071 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1072 struct usb_device_request req;
1074 if ((iface == NULL) || (iface->idesc == NULL))
1075 return (USB_ERR_INVAL);
1077 req.bmRequestType = UT_READ_INTERFACE;
1078 req.bRequest = UR_GET_INTERFACE;
1079 USETW(req.wValue, 0);
1080 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1082 USETW(req.wLength, 1);
1083 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1086 /*------------------------------------------------------------------------*
1087 * usbd_req_set_alt_interface_no
1092 *------------------------------------------------------------------------*/
1094 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1095 uint8_t iface_index, uint8_t alt_no)
1097 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1098 struct usb_device_request req;
1100 if ((iface == NULL) || (iface->idesc == NULL))
1101 return (USB_ERR_INVAL);
1103 req.bmRequestType = UT_WRITE_INTERFACE;
1104 req.bRequest = UR_SET_INTERFACE;
1105 req.wValue[0] = alt_no;
1107 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1109 USETW(req.wLength, 0);
1110 return (usbd_do_request(udev, mtx, &req, 0));
1113 /*------------------------------------------------------------------------*
1114 * usbd_req_get_device_status
1119 *------------------------------------------------------------------------*/
1121 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1122 struct usb_status *st)
1124 struct usb_device_request req;
1126 req.bmRequestType = UT_READ_DEVICE;
1127 req.bRequest = UR_GET_STATUS;
1128 USETW(req.wValue, 0);
1129 USETW(req.wIndex, 0);
1130 USETW(req.wLength, sizeof(*st));
1131 return (usbd_do_request(udev, mtx, &req, st));
1134 /*------------------------------------------------------------------------*
1135 * usbd_req_get_hub_descriptor
1140 *------------------------------------------------------------------------*/
1142 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1143 struct usb_hub_descriptor *hd, uint8_t nports)
1145 struct usb_device_request req;
1146 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1148 req.bmRequestType = UT_READ_CLASS_DEVICE;
1149 req.bRequest = UR_GET_DESCRIPTOR;
1150 USETW2(req.wValue, UDESC_HUB, 0);
1151 USETW(req.wIndex, 0);
1152 USETW(req.wLength, len);
1153 return (usbd_do_request(udev, mtx, &req, hd));
1156 /*------------------------------------------------------------------------*
1157 * usbd_req_get_hub_status
1162 *------------------------------------------------------------------------*/
1164 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1165 struct usb_hub_status *st)
1167 struct usb_device_request req;
1169 req.bmRequestType = UT_READ_CLASS_DEVICE;
1170 req.bRequest = UR_GET_STATUS;
1171 USETW(req.wValue, 0);
1172 USETW(req.wIndex, 0);
1173 USETW(req.wLength, sizeof(struct usb_hub_status));
1174 return (usbd_do_request(udev, mtx, &req, st));
1177 /*------------------------------------------------------------------------*
1178 * usbd_req_set_address
1180 * This function is used to set the address for an USB device. After
1181 * port reset the USB device will respond at address zero.
1186 *------------------------------------------------------------------------*/
1188 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1190 struct usb_device_request req;
1192 DPRINTFN(6, "setting device address=%d\n", addr);
1194 req.bmRequestType = UT_WRITE_DEVICE;
1195 req.bRequest = UR_SET_ADDRESS;
1196 USETW(req.wValue, addr);
1197 USETW(req.wIndex, 0);
1198 USETW(req.wLength, 0);
1200 /* Setting the address should not take more than 1 second ! */
1201 return (usbd_do_request_flags(udev, mtx, &req, NULL,
1202 USB_DELAY_STATUS_STAGE, NULL, 1000));
1205 /*------------------------------------------------------------------------*
1206 * usbd_req_get_port_status
1211 *------------------------------------------------------------------------*/
1213 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1214 struct usb_port_status *ps, uint8_t port)
1216 struct usb_device_request req;
1218 req.bmRequestType = UT_READ_CLASS_OTHER;
1219 req.bRequest = UR_GET_STATUS;
1220 USETW(req.wValue, 0);
1221 req.wIndex[0] = port;
1223 USETW(req.wLength, sizeof *ps);
1224 return (usbd_do_request(udev, mtx, &req, ps));
1227 /*------------------------------------------------------------------------*
1228 * usbd_req_clear_hub_feature
1233 *------------------------------------------------------------------------*/
1235 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1238 struct usb_device_request req;
1240 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1241 req.bRequest = UR_CLEAR_FEATURE;
1242 USETW(req.wValue, sel);
1243 USETW(req.wIndex, 0);
1244 USETW(req.wLength, 0);
1245 return (usbd_do_request(udev, mtx, &req, 0));
1248 /*------------------------------------------------------------------------*
1249 * usbd_req_set_hub_feature
1254 *------------------------------------------------------------------------*/
1256 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1259 struct usb_device_request req;
1261 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1262 req.bRequest = UR_SET_FEATURE;
1263 USETW(req.wValue, sel);
1264 USETW(req.wIndex, 0);
1265 USETW(req.wLength, 0);
1266 return (usbd_do_request(udev, mtx, &req, 0));
1269 /*------------------------------------------------------------------------*
1270 * usbd_req_clear_port_feature
1275 *------------------------------------------------------------------------*/
1277 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1278 uint8_t port, uint16_t sel)
1280 struct usb_device_request req;
1282 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1283 req.bRequest = UR_CLEAR_FEATURE;
1284 USETW(req.wValue, sel);
1285 req.wIndex[0] = port;
1287 USETW(req.wLength, 0);
1288 return (usbd_do_request(udev, mtx, &req, 0));
1291 /*------------------------------------------------------------------------*
1292 * usbd_req_set_port_feature
1297 *------------------------------------------------------------------------*/
1299 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1300 uint8_t port, uint16_t sel)
1302 struct usb_device_request req;
1304 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1305 req.bRequest = UR_SET_FEATURE;
1306 USETW(req.wValue, sel);
1307 req.wIndex[0] = port;
1309 USETW(req.wLength, 0);
1310 return (usbd_do_request(udev, mtx, &req, 0));
1313 /*------------------------------------------------------------------------*
1314 * usbd_req_set_protocol
1319 *------------------------------------------------------------------------*/
1321 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1322 uint8_t iface_index, uint16_t report)
1324 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1325 struct usb_device_request req;
1327 if ((iface == NULL) || (iface->idesc == NULL)) {
1328 return (USB_ERR_INVAL);
1330 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1331 iface, report, iface->idesc->bInterfaceNumber);
1333 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1334 req.bRequest = UR_SET_PROTOCOL;
1335 USETW(req.wValue, report);
1336 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1338 USETW(req.wLength, 0);
1339 return (usbd_do_request(udev, mtx, &req, 0));
1342 /*------------------------------------------------------------------------*
1343 * usbd_req_set_report
1348 *------------------------------------------------------------------------*/
1350 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1351 uint8_t iface_index, uint8_t type, uint8_t id)
1353 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1354 struct usb_device_request req;
1356 if ((iface == NULL) || (iface->idesc == NULL)) {
1357 return (USB_ERR_INVAL);
1359 DPRINTFN(5, "len=%d\n", len);
1361 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1362 req.bRequest = UR_SET_REPORT;
1363 USETW2(req.wValue, type, id);
1364 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1366 USETW(req.wLength, len);
1367 return (usbd_do_request(udev, mtx, &req, data));
1370 /*------------------------------------------------------------------------*
1371 * usbd_req_get_report
1376 *------------------------------------------------------------------------*/
1378 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1379 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1381 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1382 struct usb_device_request req;
1384 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1385 return (USB_ERR_INVAL);
1387 DPRINTFN(5, "len=%d\n", len);
1389 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1390 req.bRequest = UR_GET_REPORT;
1391 USETW2(req.wValue, type, id);
1392 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1394 USETW(req.wLength, len);
1395 return (usbd_do_request(udev, mtx, &req, data));
1398 /*------------------------------------------------------------------------*
1404 *------------------------------------------------------------------------*/
1406 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1407 uint8_t iface_index, uint8_t duration, uint8_t id)
1409 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1410 struct usb_device_request req;
1412 if ((iface == NULL) || (iface->idesc == NULL)) {
1413 return (USB_ERR_INVAL);
1415 DPRINTFN(5, "%d %d\n", duration, id);
1417 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1418 req.bRequest = UR_SET_IDLE;
1419 USETW2(req.wValue, duration, id);
1420 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1422 USETW(req.wLength, 0);
1423 return (usbd_do_request(udev, mtx, &req, 0));
1426 /*------------------------------------------------------------------------*
1427 * usbd_req_get_report_descriptor
1432 *------------------------------------------------------------------------*/
1434 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1435 void *d, uint16_t size, uint8_t iface_index)
1437 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1438 struct usb_device_request req;
1440 if ((iface == NULL) || (iface->idesc == NULL)) {
1441 return (USB_ERR_INVAL);
1443 req.bmRequestType = UT_READ_INTERFACE;
1444 req.bRequest = UR_GET_DESCRIPTOR;
1445 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1446 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1448 USETW(req.wLength, size);
1449 return (usbd_do_request(udev, mtx, &req, d));
1452 /*------------------------------------------------------------------------*
1453 * usbd_req_set_config
1455 * This function is used to select the current configuration number in
1456 * both USB device side mode and USB host side mode. When setting the
1457 * configuration the function of the interfaces can change.
1462 *------------------------------------------------------------------------*/
1464 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1466 struct usb_device_request req;
1468 DPRINTF("setting config %d\n", conf);
1470 /* do "set configuration" request */
1472 req.bmRequestType = UT_WRITE_DEVICE;
1473 req.bRequest = UR_SET_CONFIG;
1474 req.wValue[0] = conf;
1476 USETW(req.wIndex, 0);
1477 USETW(req.wLength, 0);
1478 return (usbd_do_request(udev, mtx, &req, 0));
1481 /*------------------------------------------------------------------------*
1482 * usbd_req_get_config
1487 *------------------------------------------------------------------------*/
1489 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1491 struct usb_device_request req;
1493 req.bmRequestType = UT_READ_DEVICE;
1494 req.bRequest = UR_GET_CONFIG;
1495 USETW(req.wValue, 0);
1496 USETW(req.wIndex, 0);
1497 USETW(req.wLength, 1);
1498 return (usbd_do_request(udev, mtx, &req, pconf));
1501 /*------------------------------------------------------------------------*
1502 * usbd_req_re_enumerate
1504 * NOTE: After this function returns the hardware is in the
1505 * unconfigured state! The application is responsible for setting a
1506 * new configuration.
1511 *------------------------------------------------------------------------*/
1513 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1515 struct usb_device *parent_hub;
1518 uint8_t do_retry = 1;
1520 if (udev->flags.usb_mode != USB_MODE_HOST) {
1521 return (USB_ERR_INVAL);
1523 old_addr = udev->address;
1524 parent_hub = udev->parent_hub;
1525 if (parent_hub == NULL) {
1526 return (USB_ERR_INVAL);
1529 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1531 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1532 old_addr, usbd_errstr(err));
1536 * After that the port has been reset our device should be at
1539 udev->address = USB_START_ADDR;
1541 /* reset "bMaxPacketSize" */
1542 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1545 * Restore device address:
1547 err = usbd_req_set_address(udev, mtx, old_addr);
1549 /* XXX ignore any errors! */
1550 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1551 old_addr, usbd_errstr(err));
1553 /* restore device address */
1554 udev->address = old_addr;
1556 /* allow device time to set new address */
1557 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1559 /* get the device descriptor */
1560 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1561 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1563 DPRINTFN(0, "getting device descriptor "
1564 "at addr %d failed, %s\n", udev->address,
1568 /* get the full device descriptor */
1569 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1571 DPRINTFN(0, "addr=%d, getting device "
1572 "descriptor failed, %s\n", old_addr,
1577 if (err && do_retry) {
1578 /* give the USB firmware some time to load */
1579 usb_pause_mtx(mtx, hz / 2);
1580 /* no more retries after this retry */
1585 /* restore address */
1586 udev->address = old_addr;
1590 /*------------------------------------------------------------------------*
1591 * usbd_req_clear_device_feature
1596 *------------------------------------------------------------------------*/
1598 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
1601 struct usb_device_request req;
1603 req.bmRequestType = UT_WRITE_DEVICE;
1604 req.bRequest = UR_CLEAR_FEATURE;
1605 USETW(req.wValue, sel);
1606 USETW(req.wIndex, 0);
1607 USETW(req.wLength, 0);
1608 return (usbd_do_request(udev, mtx, &req, 0));
1611 /*------------------------------------------------------------------------*
1612 * usbd_req_set_device_feature
1617 *------------------------------------------------------------------------*/
1619 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
1622 struct usb_device_request req;
1624 req.bmRequestType = UT_WRITE_DEVICE;
1625 req.bRequest = UR_SET_FEATURE;
1626 USETW(req.wValue, sel);
1627 USETW(req.wIndex, 0);
1628 USETW(req.wLength, 0);
1629 return (usbd_do_request(udev, mtx, &req, 0));