3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008-2020 Hans Petter Selasky. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbhid.h>
58 #define USB_DEBUG_VAR usb_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_busdma.h>
62 #include <dev/usb/usb_request.h>
63 #include <dev/usb/usb_process.h>
64 #include <dev/usb/usb_transfer.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_device.h>
67 #include <dev/usb/usb_util.h>
68 #include <dev/usb/usb_dynamic.h>
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
72 #include <sys/ctype.h>
73 #endif /* USB_GLOBAL_INCLUDE_FILE */
75 static int usb_no_cs_fail;
77 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RWTUN,
78 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
80 static int usb_full_ddesc;
82 SYSCTL_INT(_hw_usb, OID_AUTO, full_ddesc, CTLFLAG_RWTUN,
83 &usb_full_ddesc, 0, "USB always read complete device descriptor, if set");
87 /* The following structures are used in connection to fault injection. */
88 struct usb_ctrl_debug {
89 int bus_index; /* target bus */
90 int dev_index; /* target address */
91 int ds_fail; /* fail data stage */
92 int ss_fail; /* fail status stage */
93 int ds_delay; /* data stage delay in ms */
94 int ss_delay; /* status stage delay in ms */
95 int bmRequestType_value;
99 struct usb_ctrl_debug_bits {
107 /* The default is to disable fault injection. */
109 static struct usb_ctrl_debug usb_ctrl_debug = {
112 .bmRequestType_value = -1,
113 .bRequest_value = -1,
116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RWTUN,
117 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RWTUN,
119 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
120 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RWTUN,
121 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
122 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RWTUN,
123 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
124 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RWTUN,
125 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
126 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RWTUN,
127 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
128 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RWTUN,
129 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
130 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RWTUN,
131 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
133 /*------------------------------------------------------------------------*
134 * usbd_get_debug_bits
136 * This function is only useful in USB host mode.
137 *------------------------------------------------------------------------*/
139 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
140 struct usb_ctrl_debug_bits *dbg)
144 memset(dbg, 0, sizeof(*dbg));
146 /* Compute data stage delay */
148 temp = usb_ctrl_debug.ds_delay;
151 else if (temp > (16*1024))
154 dbg->ds_delay = temp;
156 /* Compute status stage delay */
158 temp = usb_ctrl_debug.ss_delay;
161 else if (temp > (16*1024))
164 dbg->ss_delay = temp;
166 /* Check if this control request should be failed */
168 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
171 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
174 temp = usb_ctrl_debug.bmRequestType_value;
176 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
179 temp = usb_ctrl_debug.bRequest_value;
181 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
184 temp = usb_ctrl_debug.ds_fail;
188 temp = usb_ctrl_debug.ss_fail;
194 #endif /* USB_REQ_DEBUG */
195 #endif /* USB_DEBUG */
197 /*------------------------------------------------------------------------*
198 * usbd_do_request_callback
200 * This function is the USB callback for generic USB Host control
202 *------------------------------------------------------------------------*/
204 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
206 ; /* workaround for a bug in "indent" */
208 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
210 switch (USB_GET_STATE(xfer)) {
212 usbd_transfer_submit(xfer);
215 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
220 /*------------------------------------------------------------------------*
221 * usb_do_clear_stall_callback
223 * This function is the USB callback for generic clear stall requests.
224 *------------------------------------------------------------------------*/
226 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
228 struct usb_device_request req;
229 struct usb_device *udev;
230 struct usb_endpoint *ep;
231 struct usb_endpoint *ep_end;
232 struct usb_endpoint *ep_first;
236 udev = xfer->xroot->udev;
238 USB_BUS_LOCK(udev->bus);
240 /* round robin endpoint clear stall */
243 ep_end = udev->endpoints + udev->endpoints_max;
244 ep_first = udev->endpoints;
245 to = udev->endpoints_max;
247 switch (USB_GET_STATE(xfer)) {
248 case USB_ST_TRANSFERRED:
250 /* reset error counter */
251 udev->clear_stall_errors = 0;
254 goto tr_setup; /* device was unconfigured */
259 /* some hardware needs a callback to clear the data toggle */
260 usbd_clear_stall_locked(udev, ep);
261 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
262 /* start the current or next transfer, if any */
263 usb_command_wrapper(&ep->endpoint_q[x],
264 ep->endpoint_q[x].curr);
272 break; /* no endpoints - nothing to do */
273 if ((ep < ep_first) || (ep >= ep_end))
274 ep = ep_first; /* endpoint wrapped around */
277 /* setup a clear-stall packet */
279 req.bmRequestType = UT_WRITE_ENDPOINT;
280 req.bRequest = UR_CLEAR_FEATURE;
281 USETW(req.wValue, UF_ENDPOINT_HALT);
282 req.wIndex[0] = ep->edesc->bEndpointAddress;
284 USETW(req.wLength, 0);
286 /* copy in the transfer */
288 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
291 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
293 USB_BUS_UNLOCK(udev->bus);
295 usbd_transfer_submit(xfer);
297 USB_BUS_LOCK(udev->bus);
305 if (error == USB_ERR_CANCELLED)
308 DPRINTF("Clear stall failed.\n");
311 * Some VMs like VirtualBox always return failure on
312 * clear-stall which we sometimes should just ignore.
316 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
319 if (error == USB_ERR_TIMEOUT) {
320 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
321 DPRINTF("Trying to re-enumerate.\n");
322 usbd_start_re_enumerate(udev);
324 udev->clear_stall_errors++;
325 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
326 DPRINTF("Trying to re-enumerate.\n");
327 usbd_start_re_enumerate(udev);
333 /* store current endpoint */
335 USB_BUS_UNLOCK(udev->bus);
338 static usb_handle_req_t *
339 usbd_get_hr_func(struct usb_device *udev)
341 /* figure out if there is a Handle Request function */
342 if (udev->flags.usb_mode == USB_MODE_DEVICE)
343 return (usb_temp_get_desc_p);
344 else if (udev->parent_hub == NULL)
345 return (udev->bus->methods->roothub_exec);
350 /*------------------------------------------------------------------------*
351 * usbd_do_request_flags and usbd_do_request
353 * Description of arguments passed to these functions:
355 * "udev" - this is the "usb_device" structure pointer on which the
356 * request should be performed. It is possible to call this function
357 * in both Host Side mode and Device Side mode.
359 * "mtx" - if this argument is non-NULL the mutex pointed to by it
360 * will get dropped and picked up during the execution of this
361 * function, hence this function sometimes needs to sleep. If this
362 * argument is NULL it has no effect.
364 * "req" - this argument must always be non-NULL and points to an
365 * 8-byte structure holding the USB request to be done. The USB
366 * request structure has a bit telling the direction of the USB
367 * request, if it is a read or a write.
369 * "data" - if the "wLength" part of the structure pointed to by "req"
370 * is non-zero this argument must point to a valid kernel buffer which
371 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
374 * "flags" - here is a list of valid flags:
376 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
379 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
380 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
381 * sysctl. This flag is mostly useful for debugging.
383 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
386 * "actlen" - if non-NULL the actual transfer length will be stored in
387 * the 16-bit unsigned integer pointed to by "actlen". This
388 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
391 * "timeout" - gives the timeout for the control transfer in
392 * milliseconds. A "timeout" value less than 50 milliseconds is
393 * treated like a 50 millisecond timeout. A "timeout" value greater
394 * than 30 seconds is treated like a 30 second timeout. This USB stack
395 * does not allow control requests without a timeout.
397 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
398 * will be serialized by the use of the USB device enumeration lock.
403 *------------------------------------------------------------------------*/
405 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
406 struct usb_device_request *req, void *data, uint16_t flags,
407 uint16_t *actlen, usb_timeout_t timeout)
410 struct usb_ctrl_debug_bits dbg;
412 usb_handle_req_t *hr_func;
413 struct usb_xfer *xfer;
416 usb_ticks_t start_ticks;
417 usb_ticks_t delta_ticks;
418 usb_ticks_t max_ticks;
425 /* timeout is too small */
428 if (timeout > 30000) {
429 /* timeout is too big */
432 length = UGETW(req->wLength);
434 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
435 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
436 udev, req->bmRequestType, req->bRequest,
437 req->wValue[1], req->wValue[0],
438 req->wIndex[1], req->wIndex[0],
439 req->wLength[1], req->wLength[0]);
441 /* Check if the device is still alive */
442 if (udev->state < USB_STATE_POWERED) {
443 DPRINTF("usb device has gone\n");
444 return (USB_ERR_NOT_CONFIGURED);
448 * Set "actlen" to a known value in case the caller does not
449 * check the return value:
454 #if (USB_HAVE_USER_IO == 0)
455 if (flags & USB_USER_DATA_PTR)
456 return (USB_ERR_INVAL);
458 if ((mtx != NULL) && (mtx != &Giant)) {
460 USB_MTX_ASSERT(mtx, MA_NOTOWNED);
464 * Serialize access to this function:
466 do_unlock = usbd_ctrl_lock(udev);
468 hr_func = usbd_get_hr_func(udev);
470 if (hr_func != NULL) {
471 DPRINTF("Handle Request function is set\n");
476 if (!(req->bmRequestType & UT_READ)) {
478 DPRINTFN(1, "The handle request function "
479 "does not support writing data!\n");
485 /* The root HUB code needs the BUS lock locked */
487 USB_BUS_LOCK(udev->bus);
488 err = (hr_func) (udev, req, &desc, &temp);
489 USB_BUS_UNLOCK(udev->bus);
495 if (!(flags & USB_SHORT_XFER_OK)) {
496 err = USB_ERR_SHORT_XFER;
506 if (flags & USB_USER_DATA_PTR) {
507 if (copyout(desc, data, length)) {
513 memcpy(data, desc, length);
515 goto done; /* success */
519 * Setup a new USB transfer or use the existing one, if any:
521 usbd_ctrl_transfer_setup(udev);
523 xfer = udev->ctrl_xfer[0];
525 /* most likely out of memory */
532 usbd_get_debug_bits(udev, req, &dbg);
534 /* Check for fault injection */
536 flags |= USB_DELAY_STATUS_STAGE;
540 if (flags & USB_DELAY_STATUS_STAGE)
541 xfer->flags.manual_status = 1;
543 xfer->flags.manual_status = 0;
545 if (flags & USB_SHORT_XFER_OK)
546 xfer->flags.short_xfer_ok = 1;
548 xfer->flags.short_xfer_ok = 0;
550 xfer->timeout = timeout;
554 max_ticks = USB_MS_TO_TICKS(timeout);
556 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
558 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
562 if (temp > usbd_xfer_max_len(xfer)) {
563 temp = usbd_xfer_max_len(xfer);
566 if (xfer->flags.manual_status) {
567 if (usbd_xfer_frame_len(xfer, 0) != 0) {
568 /* Execute data stage separately */
570 } else if (temp > 0) {
575 if (dbg.ds_delay > 0) {
577 xfer->xroot->xfer_mtx,
578 USB_MS_TO_TICKS(dbg.ds_delay));
579 /* make sure we don't time out */
585 usbd_xfer_set_frame_len(xfer, 1, temp);
588 if (!(req->bmRequestType & UT_READ)) {
590 if (flags & USB_USER_DATA_PTR) {
591 USB_XFER_UNLOCK(xfer);
592 err = usbd_copy_in_user(xfer->frbuffers + 1,
601 usbd_copy_in(xfer->frbuffers + 1,
604 usbd_xfer_set_frames(xfer, 2);
606 if (usbd_xfer_frame_len(xfer, 0) == 0) {
607 if (xfer->flags.manual_status) {
613 if (dbg.ss_delay > 0) {
615 xfer->xroot->xfer_mtx,
616 USB_MS_TO_TICKS(dbg.ss_delay));
617 /* make sure we don't time out */
621 xfer->flags.manual_status = 0;
626 usbd_xfer_set_frames(xfer, 1);
629 usbd_transfer_start(xfer);
631 while (usbd_transfer_pending(xfer)) {
632 cv_wait(&udev->ctrlreq_cv,
633 xfer->xroot->xfer_mtx);
642 /* get actual length of DATA stage */
644 if (xfer->aframes < 2) {
647 acttemp = usbd_xfer_frame_len(xfer, 1);
650 /* check for short packet */
652 if (temp > acttemp) {
657 if (req->bmRequestType & UT_READ) {
659 if (flags & USB_USER_DATA_PTR) {
660 USB_XFER_UNLOCK(xfer);
661 err = usbd_copy_out_user(xfer->frbuffers + 1,
670 usbd_copy_out(xfer->frbuffers + 1,
675 * Clear "frlengths[0]" so that we don't send the setup
678 usbd_xfer_set_frame_len(xfer, 0, 0);
680 /* update length and data pointer */
682 data = USB_ADD_BYTES(data, temp);
687 /* check for timeout */
689 delta_ticks = ticks - start_ticks;
690 if (delta_ticks > max_ticks) {
692 err = USB_ERR_TIMEOUT;
702 * Make sure that the control endpoint is no longer
703 * blocked in case of a non-transfer related error:
705 usbd_transfer_stop(xfer);
707 USB_XFER_UNLOCK(xfer);
711 usbd_ctrl_unlock(udev);
713 if ((mtx != NULL) && (mtx != &Giant))
717 case USB_ERR_NORMAL_COMPLETION:
718 case USB_ERR_SHORT_XFER:
719 case USB_ERR_STALLED:
720 case USB_ERR_CANCELLED:
723 DPRINTF("error=%s - waiting a bit for TT cleanup\n",
725 usb_pause_mtx(mtx, hz / 16);
728 return ((usb_error_t)err);
731 /*------------------------------------------------------------------------*
732 * usbd_do_request_proc - factored out code
734 * This function is factored out code. It does basically the same like
735 * usbd_do_request_flags, except it will check the status of the
736 * passed process argument before doing the USB request. If the
737 * process is draining the USB_ERR_IOERROR code will be returned. It
738 * is assumed that the mutex associated with the process is locked
739 * when calling this function.
740 *------------------------------------------------------------------------*/
742 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
743 struct usb_device_request *req, void *data, uint16_t flags,
744 uint16_t *actlen, usb_timeout_t timeout)
749 /* get request data length */
750 len = UGETW(req->wLength);
752 /* check if the device is being detached */
753 if (usb_proc_is_gone(pproc)) {
754 err = USB_ERR_IOERROR;
758 /* forward the USB request */
759 err = usbd_do_request_flags(udev, pproc->up_mtx,
760 req, data, flags, actlen, timeout);
763 /* on failure we zero the data */
764 /* on short packet we zero the unused data */
765 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
767 memset(data, 0, len);
768 else if (actlen && *actlen != len)
769 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
774 /*------------------------------------------------------------------------*
775 * usbd_req_reset_port
777 * This function will instruct a USB HUB to perform a reset sequence
778 * on the specified port number.
781 * 0: Success. The USB device should now be at address zero.
782 * Else: Failure. No USB device is present and the USB port should be
784 *------------------------------------------------------------------------*/
786 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
788 struct usb_port_status ps;
796 /* clear any leftover port reset changes first */
797 usbd_req_clear_port_feature(
798 udev, mtx, port, UHF_C_PORT_RESET);
800 /* assert port reset on the given port */
801 err = usbd_req_set_port_feature(
802 udev, mtx, port, UHF_PORT_RESET);
804 /* check for errors */
809 /* wait for the device to recover from reset */
810 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
811 n += usb_port_reset_delay;
812 err = usbd_req_get_port_status(udev, mtx, &ps, port);
816 status = UGETW(ps.wPortStatus);
817 change = UGETW(ps.wPortChange);
819 /* if the device disappeared, just give up */
820 if (!(status & UPS_CURRENT_CONNECT_STATUS))
823 /* check if reset is complete */
824 if (change & UPS_C_PORT_RESET)
828 * Some Virtual Machines like VirtualBox 4.x fail to
829 * generate a port reset change event. Check if reset
830 * is no longer asserted.
832 if (!(status & UPS_RESET))
835 /* check for timeout */
842 /* clear port reset first */
843 err = usbd_req_clear_port_feature(
844 udev, mtx, port, UHF_C_PORT_RESET);
848 /* check for timeout */
850 err = USB_ERR_TIMEOUT;
853 /* wait for the device to recover from reset */
854 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
857 DPRINTFN(2, "port %d reset returning error=%s\n",
858 port, usbd_errstr(err));
862 /*------------------------------------------------------------------------*
863 * usbd_req_warm_reset_port
865 * This function will instruct an USB HUB to perform a warm reset
866 * sequence on the specified port number. This kind of reset is not
867 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
868 * for SUPER-speed USB HUBs.
871 * 0: Success. The USB device should now be available again.
872 * Else: Failure. No USB device is present and the USB port should be
874 *------------------------------------------------------------------------*/
876 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
879 struct usb_port_status ps;
887 err = usbd_req_get_port_status(udev, mtx, &ps, port);
891 status = UGETW(ps.wPortStatus);
893 switch (UPS_PORT_LINK_STATE_GET(status)) {
895 case UPS_PORT_LS_COMP_MODE:
896 case UPS_PORT_LS_LOOPBACK:
897 case UPS_PORT_LS_SS_INA:
900 DPRINTF("Wrong state for warm reset\n");
904 /* clear any leftover warm port reset changes first */
905 usbd_req_clear_port_feature(udev, mtx,
906 port, UHF_C_BH_PORT_RESET);
908 /* set warm port reset */
909 err = usbd_req_set_port_feature(udev, mtx,
910 port, UHF_BH_PORT_RESET);
916 /* wait for the device to recover from reset */
917 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
918 n += usb_port_reset_delay;
919 err = usbd_req_get_port_status(udev, mtx, &ps, port);
923 status = UGETW(ps.wPortStatus);
924 change = UGETW(ps.wPortChange);
926 /* if the device disappeared, just give up */
927 if (!(status & UPS_CURRENT_CONNECT_STATUS))
930 /* check if reset is complete */
931 if (change & UPS_C_BH_PORT_RESET)
934 /* check for timeout */
941 /* clear port reset first */
942 err = usbd_req_clear_port_feature(
943 udev, mtx, port, UHF_C_BH_PORT_RESET);
947 /* check for timeout */
949 err = USB_ERR_TIMEOUT;
952 /* wait for the device to recover from reset */
953 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
956 DPRINTFN(2, "port %d warm reset returning error=%s\n",
957 port, usbd_errstr(err));
961 /*------------------------------------------------------------------------*
964 * This function can be used to retrieve USB descriptors. It contains
965 * some additional logic like zeroing of missing descriptor bytes and
966 * retrying an USB descriptor in case of failure. The "min_len"
967 * argument specifies the minimum descriptor length. The "max_len"
968 * argument specifies the maximum descriptor length. If the real
969 * descriptor length is less than the minimum length the missing
970 * byte(s) will be zeroed. The type field, the second byte of the USB
971 * descriptor, will get forced to the correct type. If the "actlen"
972 * pointer is non-NULL, the actual length of the transfer will get
973 * stored in the 16-bit unsigned integer which it is pointing to. The
974 * first byte of the descriptor will not get updated. If the "actlen"
975 * pointer is NULL the first byte of the descriptor will get updated
976 * to reflect the actual length instead. If "min_len" is not equal to
977 * "max_len" then this function will try to retrive the beginning of
978 * the descriptor and base the maximum length on the first byte of the
984 *------------------------------------------------------------------------*/
986 usbd_req_get_desc(struct usb_device *udev,
987 struct mtx *mtx, uint16_t *actlen, void *desc,
988 uint16_t min_len, uint16_t max_len,
989 uint16_t id, uint8_t type, uint8_t index,
992 struct usb_device_request req;
996 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
997 id, type, index, max_len);
999 req.bmRequestType = UT_READ_DEVICE;
1000 req.bRequest = UR_GET_DESCRIPTOR;
1001 USETW2(req.wValue, type, index);
1002 USETW(req.wIndex, id);
1005 if ((min_len < 2) || (max_len < 2)) {
1006 err = USB_ERR_INVAL;
1009 USETW(req.wLength, min_len);
1011 err = usbd_do_request_flags(udev, mtx, &req,
1012 desc, 0, NULL, 1000 /* ms */);
1014 if (err != 0 && err != USB_ERR_TIMEOUT &&
1015 min_len != max_len) {
1016 /* clear descriptor data */
1017 memset(desc, 0, max_len);
1019 /* try to read full descriptor length */
1020 USETW(req.wLength, max_len);
1022 err = usbd_do_request_flags(udev, mtx, &req,
1023 desc, USB_SHORT_XFER_OK, NULL, 1000 /* ms */);
1027 if (buf[0] > max_len)
1029 else if (buf[0] < 2)
1030 err = USB_ERR_INVAL;
1034 /* enforce descriptor type */
1046 usb_pause_mtx(mtx, hz / 5);
1051 if (min_len == max_len) {
1052 /* enforce correct length */
1053 if ((buf[0] > min_len) && (actlen == NULL))
1056 /* enforce correct type */
1063 if (max_len > buf[0]) {
1066 /* zero minimum data */
1068 while (min_len > max_len) {
1073 /* set new minimum length */
1078 if (actlen != NULL) {
1087 /*------------------------------------------------------------------------*
1088 * usbd_req_get_string_any
1090 * This function will return the string given by "string_index"
1091 * using the first language ID. The maximum length "len" includes
1092 * the terminating zero. The "len" argument should be twice as
1093 * big pluss 2 bytes, compared with the actual maximum string length !
1098 *------------------------------------------------------------------------*/
1100 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1101 uint16_t len, uint8_t string_index)
1112 /* should not happen */
1113 return (USB_ERR_NORMAL_COMPLETION);
1115 if (string_index == 0) {
1116 /* this is the language table */
1118 return (USB_ERR_INVAL);
1120 if (udev->flags.no_strings) {
1122 return (USB_ERR_STALLED);
1124 err = usbd_req_get_string_desc
1125 (udev, mtx, buf, len, udev->langid, string_index);
1130 temp = (uint8_t *)buf;
1133 /* string length is too short */
1135 return (USB_ERR_INVAL);
1137 /* reserve one byte for terminating zero */
1140 /* find maximum length */
1142 n = (temp[0] / 2) - 1;
1146 /* skip descriptor header */
1149 /* reset swap state */
1152 /* convert and filter */
1153 for (i = 0; (i != n); i++) {
1154 c = UGETW(temp + (2 * i));
1156 /* convert from Unicode, handle buggy strings */
1157 if (((c & 0xff00) == 0) && (swap & 1)) {
1158 /* Little Endian, default */
1161 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1166 /* silently skip bad character */
1171 * Filter by default - We only allow alphanumerical
1172 * and a few more to avoid any problems with scripts
1189 /* silently skip bad character */
1191 *s = 0; /* zero terminate resulting string */
1192 return (USB_ERR_NORMAL_COMPLETION);
1195 /*------------------------------------------------------------------------*
1196 * usbd_req_get_string_desc
1198 * If you don't know the language ID, consider using
1199 * "usbd_req_get_string_any()".
1204 *------------------------------------------------------------------------*/
1206 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1207 uint16_t max_len, uint16_t lang_id,
1208 uint8_t string_index)
1210 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1211 UDESC_STRING, string_index, 0));
1214 /*------------------------------------------------------------------------*
1215 * usbd_req_get_config_desc_ptr
1217 * This function is used in device side mode to retrieve the pointer
1218 * to the generated config descriptor. This saves allocating space for
1219 * an additional config descriptor when setting the configuration.
1224 *------------------------------------------------------------------------*/
1226 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1227 struct usb_config_descriptor **ppcd, uint16_t wValue)
1229 struct usb_device_request req;
1230 usb_handle_req_t *hr_func;
1235 req.bmRequestType = UT_READ_DEVICE;
1236 req.bRequest = UR_GET_DESCRIPTOR;
1237 USETW(req.wValue, wValue);
1238 USETW(req.wIndex, 0);
1239 USETW(req.wLength, 0);
1244 hr_func = usbd_get_hr_func(udev);
1246 if (hr_func == NULL)
1247 err = USB_ERR_INVAL;
1249 USB_BUS_LOCK(udev->bus);
1250 err = (hr_func) (udev, &req, &ptr, &len);
1251 USB_BUS_UNLOCK(udev->bus);
1256 else if (ptr == NULL)
1257 err = USB_ERR_INVAL;
1259 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1264 /*------------------------------------------------------------------------*
1265 * usbd_req_get_config_desc
1270 *------------------------------------------------------------------------*/
1272 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1273 struct usb_config_descriptor *d, uint8_t conf_index)
1277 DPRINTFN(4, "confidx=%d\n", conf_index);
1279 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1280 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1284 /* Extra sanity checking */
1285 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1286 err = USB_ERR_INVAL;
1292 /*------------------------------------------------------------------------*
1293 * usbd_alloc_config_desc
1295 * This function is used to allocate a zeroed configuration
1301 *------------------------------------------------------------------------*/
1303 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1305 if (size > USB_CONFIG_MAX) {
1306 DPRINTF("Configuration descriptor too big\n");
1309 #if (USB_HAVE_FIXED_CONFIG == 0)
1310 return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1312 memset(udev->config_data, 0, sizeof(udev->config_data));
1313 return (udev->config_data);
1317 /*------------------------------------------------------------------------*
1318 * usbd_alloc_config_desc
1320 * This function is used to free a configuration descriptor.
1321 *------------------------------------------------------------------------*/
1323 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1325 #if (USB_HAVE_FIXED_CONFIG == 0)
1326 free(ptr, M_USBDEV);
1330 /*------------------------------------------------------------------------*
1331 * usbd_req_get_config_desc_full
1333 * This function gets the complete USB configuration descriptor and
1334 * ensures that "wTotalLength" is correct. The returned configuration
1335 * descriptor is freed by calling "usbd_free_config_desc()".
1340 *------------------------------------------------------------------------*/
1342 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1343 struct usb_config_descriptor **ppcd, uint8_t index)
1345 struct usb_config_descriptor cd;
1346 struct usb_config_descriptor *cdesc;
1350 DPRINTFN(4, "index=%d\n", index);
1354 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1358 /* get full descriptor */
1359 len = UGETW(cd.wTotalLength);
1360 if (len < (uint32_t)sizeof(*cdesc)) {
1361 /* corrupt descriptor */
1362 return (USB_ERR_INVAL);
1363 } else if (len > USB_CONFIG_MAX) {
1364 DPRINTF("Configuration descriptor was truncated\n");
1365 len = USB_CONFIG_MAX;
1367 cdesc = usbd_alloc_config_desc(udev, len);
1369 return (USB_ERR_NOMEM);
1370 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1371 UDESC_CONFIG, index, 3);
1373 usbd_free_config_desc(udev, cdesc);
1376 /* make sure that the device is not fooling us: */
1377 USETW(cdesc->wTotalLength, len);
1381 return (0); /* success */
1384 /*------------------------------------------------------------------------*
1385 * usbd_req_get_device_desc
1390 *------------------------------------------------------------------------*/
1392 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1393 struct usb_device_descriptor *d)
1396 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1397 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1400 /*------------------------------------------------------------------------*
1401 * usbd_req_get_alt_interface_no
1406 *------------------------------------------------------------------------*/
1408 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1409 uint8_t *alt_iface_no, uint8_t iface_index)
1411 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1412 struct usb_device_request req;
1414 if ((iface == NULL) || (iface->idesc == NULL))
1415 return (USB_ERR_INVAL);
1417 req.bmRequestType = UT_READ_INTERFACE;
1418 req.bRequest = UR_GET_INTERFACE;
1419 USETW(req.wValue, 0);
1420 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1422 USETW(req.wLength, 1);
1423 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1426 /*------------------------------------------------------------------------*
1427 * usbd_req_set_alt_interface_no
1432 *------------------------------------------------------------------------*/
1434 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1435 uint8_t iface_index, uint8_t alt_no)
1437 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1438 struct usb_device_request req;
1441 if ((iface == NULL) || (iface->idesc == NULL))
1442 return (USB_ERR_INVAL);
1444 req.bmRequestType = UT_WRITE_INTERFACE;
1445 req.bRequest = UR_SET_INTERFACE;
1446 req.wValue[0] = alt_no;
1448 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1450 USETW(req.wLength, 0);
1451 err = usbd_do_request(udev, mtx, &req, 0);
1452 if (err == USB_ERR_STALLED && iface->num_altsetting == 1) {
1454 * The USB specification chapter 9.4.10 says that USB
1455 * devices having only one alternate setting are
1456 * allowed to STALL this request. Ignore this failure.
1459 DPRINTF("Setting default alternate number failed. (ignored)\n");
1464 /*------------------------------------------------------------------------*
1465 * usbd_req_get_device_status
1470 *------------------------------------------------------------------------*/
1472 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1473 struct usb_status *st)
1475 struct usb_device_request req;
1477 req.bmRequestType = UT_READ_DEVICE;
1478 req.bRequest = UR_GET_STATUS;
1479 USETW(req.wValue, 0);
1480 USETW(req.wIndex, 0);
1481 USETW(req.wLength, sizeof(*st));
1482 return (usbd_do_request(udev, mtx, &req, st));
1485 /*------------------------------------------------------------------------*
1486 * usbd_req_get_hub_descriptor
1491 *------------------------------------------------------------------------*/
1493 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1494 struct usb_hub_descriptor *hd, uint8_t nports)
1496 struct usb_device_request req;
1497 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1499 req.bmRequestType = UT_READ_CLASS_DEVICE;
1500 req.bRequest = UR_GET_DESCRIPTOR;
1501 USETW2(req.wValue, UDESC_HUB, 0);
1502 USETW(req.wIndex, 0);
1503 USETW(req.wLength, len);
1504 return (usbd_do_request(udev, mtx, &req, hd));
1507 /*------------------------------------------------------------------------*
1508 * usbd_req_get_ss_hub_descriptor
1513 *------------------------------------------------------------------------*/
1515 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1516 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1518 struct usb_device_request req;
1519 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1521 req.bmRequestType = UT_READ_CLASS_DEVICE;
1522 req.bRequest = UR_GET_DESCRIPTOR;
1523 USETW2(req.wValue, UDESC_SS_HUB, 0);
1524 USETW(req.wIndex, 0);
1525 USETW(req.wLength, len);
1526 return (usbd_do_request(udev, mtx, &req, hd));
1529 /*------------------------------------------------------------------------*
1530 * usbd_req_get_hub_status
1535 *------------------------------------------------------------------------*/
1537 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1538 struct usb_hub_status *st)
1540 struct usb_device_request req;
1542 req.bmRequestType = UT_READ_CLASS_DEVICE;
1543 req.bRequest = UR_GET_STATUS;
1544 USETW(req.wValue, 0);
1545 USETW(req.wIndex, 0);
1546 USETW(req.wLength, sizeof(struct usb_hub_status));
1547 return (usbd_do_request(udev, mtx, &req, st));
1550 /*------------------------------------------------------------------------*
1551 * usbd_req_set_address
1553 * This function is used to set the address for an USB device. After
1554 * port reset the USB device will respond at address zero.
1559 *------------------------------------------------------------------------*/
1561 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1563 struct usb_device_request req;
1566 DPRINTFN(6, "setting device address=%d\n", addr);
1568 req.bmRequestType = UT_WRITE_DEVICE;
1569 req.bRequest = UR_SET_ADDRESS;
1570 USETW(req.wValue, addr);
1571 USETW(req.wIndex, 0);
1572 USETW(req.wLength, 0);
1574 err = USB_ERR_INVAL;
1576 /* check if USB controller handles set address */
1577 if (udev->bus->methods->set_address != NULL)
1578 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1580 if (err != USB_ERR_INVAL)
1583 /* Setting the address should not take more than 1 second ! */
1584 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1585 USB_DELAY_STATUS_STAGE, NULL, 1000);
1588 /* allow device time to set new address */
1590 USB_MS_TO_TICKS(usb_set_address_settle));
1595 /*------------------------------------------------------------------------*
1596 * usbd_req_get_port_status
1601 *------------------------------------------------------------------------*/
1603 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1604 struct usb_port_status *ps, uint8_t port)
1606 struct usb_device_request req;
1608 req.bmRequestType = UT_READ_CLASS_OTHER;
1609 req.bRequest = UR_GET_STATUS;
1610 USETW(req.wValue, 0);
1611 req.wIndex[0] = port;
1613 USETW(req.wLength, sizeof(*ps));
1615 return (usbd_do_request_flags(udev, mtx, &req, ps, 0, NULL, 1000));
1618 /*------------------------------------------------------------------------*
1619 * usbd_req_clear_hub_feature
1624 *------------------------------------------------------------------------*/
1626 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1629 struct usb_device_request req;
1631 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1632 req.bRequest = UR_CLEAR_FEATURE;
1633 USETW(req.wValue, sel);
1634 USETW(req.wIndex, 0);
1635 USETW(req.wLength, 0);
1636 return (usbd_do_request(udev, mtx, &req, 0));
1639 /*------------------------------------------------------------------------*
1640 * usbd_req_set_hub_feature
1645 *------------------------------------------------------------------------*/
1647 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1650 struct usb_device_request req;
1652 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1653 req.bRequest = UR_SET_FEATURE;
1654 USETW(req.wValue, sel);
1655 USETW(req.wIndex, 0);
1656 USETW(req.wLength, 0);
1657 return (usbd_do_request(udev, mtx, &req, 0));
1660 /*------------------------------------------------------------------------*
1661 * usbd_req_set_hub_u1_timeout
1666 *------------------------------------------------------------------------*/
1668 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1669 uint8_t port, uint8_t timeout)
1671 struct usb_device_request req;
1673 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1674 req.bRequest = UR_SET_FEATURE;
1675 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1676 req.wIndex[0] = port;
1677 req.wIndex[1] = timeout;
1678 USETW(req.wLength, 0);
1679 return (usbd_do_request(udev, mtx, &req, 0));
1682 /*------------------------------------------------------------------------*
1683 * usbd_req_set_hub_u2_timeout
1688 *------------------------------------------------------------------------*/
1690 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1691 uint8_t port, uint8_t timeout)
1693 struct usb_device_request req;
1695 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1696 req.bRequest = UR_SET_FEATURE;
1697 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1698 req.wIndex[0] = port;
1699 req.wIndex[1] = timeout;
1700 USETW(req.wLength, 0);
1701 return (usbd_do_request(udev, mtx, &req, 0));
1704 /*------------------------------------------------------------------------*
1705 * usbd_req_set_hub_depth
1710 *------------------------------------------------------------------------*/
1712 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1715 struct usb_device_request req;
1717 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1718 req.bRequest = UR_SET_HUB_DEPTH;
1719 USETW(req.wValue, depth);
1720 USETW(req.wIndex, 0);
1721 USETW(req.wLength, 0);
1722 return (usbd_do_request(udev, mtx, &req, 0));
1725 /*------------------------------------------------------------------------*
1726 * usbd_req_clear_port_feature
1731 *------------------------------------------------------------------------*/
1733 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1734 uint8_t port, uint16_t sel)
1736 struct usb_device_request req;
1738 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1739 req.bRequest = UR_CLEAR_FEATURE;
1740 USETW(req.wValue, sel);
1741 req.wIndex[0] = port;
1743 USETW(req.wLength, 0);
1744 return (usbd_do_request(udev, mtx, &req, 0));
1747 /*------------------------------------------------------------------------*
1748 * usbd_req_set_port_feature
1753 *------------------------------------------------------------------------*/
1755 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1756 uint8_t port, uint16_t sel)
1758 struct usb_device_request req;
1760 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1761 req.bRequest = UR_SET_FEATURE;
1762 USETW(req.wValue, sel);
1763 req.wIndex[0] = port;
1765 USETW(req.wLength, 0);
1766 return (usbd_do_request(udev, mtx, &req, 0));
1769 /*------------------------------------------------------------------------*
1770 * usbd_req_set_protocol
1775 *------------------------------------------------------------------------*/
1777 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1778 uint8_t iface_index, uint16_t report)
1780 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1781 struct usb_device_request req;
1783 if ((iface == NULL) || (iface->idesc == NULL)) {
1784 return (USB_ERR_INVAL);
1786 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1787 iface, report, iface->idesc->bInterfaceNumber);
1789 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1790 req.bRequest = UR_SET_PROTOCOL;
1791 USETW(req.wValue, report);
1792 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1794 USETW(req.wLength, 0);
1795 return (usbd_do_request(udev, mtx, &req, 0));
1798 /*------------------------------------------------------------------------*
1799 * usbd_req_set_report
1804 *------------------------------------------------------------------------*/
1806 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1807 uint8_t iface_index, uint8_t type, uint8_t id)
1809 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1810 struct usb_device_request req;
1812 if ((iface == NULL) || (iface->idesc == NULL)) {
1813 return (USB_ERR_INVAL);
1815 DPRINTFN(5, "len=%d\n", len);
1817 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1818 req.bRequest = UR_SET_REPORT;
1819 USETW2(req.wValue, type, id);
1820 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1822 USETW(req.wLength, len);
1823 return (usbd_do_request(udev, mtx, &req, data));
1826 /*------------------------------------------------------------------------*
1827 * usbd_req_get_report
1832 *------------------------------------------------------------------------*/
1834 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1835 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1837 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1838 struct usb_device_request req;
1840 if ((iface == NULL) || (iface->idesc == NULL)) {
1841 return (USB_ERR_INVAL);
1843 DPRINTFN(5, "len=%d\n", len);
1845 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1846 req.bRequest = UR_GET_REPORT;
1847 USETW2(req.wValue, type, id);
1848 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1850 USETW(req.wLength, len);
1851 return (usbd_do_request(udev, mtx, &req, data));
1854 /*------------------------------------------------------------------------*
1860 *------------------------------------------------------------------------*/
1862 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1863 uint8_t iface_index, uint8_t duration, uint8_t id)
1865 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1866 struct usb_device_request req;
1868 if ((iface == NULL) || (iface->idesc == NULL)) {
1869 return (USB_ERR_INVAL);
1871 DPRINTFN(5, "%d %d\n", duration, id);
1873 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1874 req.bRequest = UR_SET_IDLE;
1875 USETW2(req.wValue, duration, id);
1876 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1878 USETW(req.wLength, 0);
1879 return (usbd_do_request(udev, mtx, &req, 0));
1882 /*------------------------------------------------------------------------*
1883 * usbd_req_get_report_descriptor
1888 *------------------------------------------------------------------------*/
1890 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1891 void *d, uint16_t size, uint8_t iface_index)
1893 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1894 struct usb_device_request req;
1896 if ((iface == NULL) || (iface->idesc == NULL)) {
1897 return (USB_ERR_INVAL);
1899 req.bmRequestType = UT_READ_INTERFACE;
1900 req.bRequest = UR_GET_DESCRIPTOR;
1901 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1902 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1904 USETW(req.wLength, size);
1905 return (usbd_do_request(udev, mtx, &req, d));
1908 /*------------------------------------------------------------------------*
1909 * usbd_req_set_config
1911 * This function is used to select the current configuration number in
1912 * both USB device side mode and USB host side mode. When setting the
1913 * configuration the function of the interfaces can change.
1918 *------------------------------------------------------------------------*/
1920 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1922 struct usb_device_request req;
1924 DPRINTF("setting config %d\n", conf);
1926 /* do "set configuration" request */
1928 req.bmRequestType = UT_WRITE_DEVICE;
1929 req.bRequest = UR_SET_CONFIG;
1930 req.wValue[0] = conf;
1932 USETW(req.wIndex, 0);
1933 USETW(req.wLength, 0);
1934 return (usbd_do_request(udev, mtx, &req, 0));
1937 /*------------------------------------------------------------------------*
1938 * usbd_req_get_config
1943 *------------------------------------------------------------------------*/
1945 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1947 struct usb_device_request req;
1949 req.bmRequestType = UT_READ_DEVICE;
1950 req.bRequest = UR_GET_CONFIG;
1951 USETW(req.wValue, 0);
1952 USETW(req.wIndex, 0);
1953 USETW(req.wLength, 1);
1954 return (usbd_do_request(udev, mtx, &req, pconf));
1957 /*------------------------------------------------------------------------*
1958 * usbd_setup_device_desc
1959 *------------------------------------------------------------------------*/
1961 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1966 * Get the first 8 bytes of the device descriptor !
1968 * NOTE: "usbd_do_request()" will check the device descriptor
1969 * next time we do a request to see if the maximum packet size
1970 * changed! The 8 first bytes of the device descriptor
1971 * contains the maximum packet size to use on control endpoint
1972 * 0. If this value is different from "USB_MAX_IPACKET" a new
1973 * USB control request will be setup!
1975 switch (udev->speed) {
1976 case USB_SPEED_FULL:
1977 if (usb_full_ddesc != 0) {
1978 /* get full device descriptor */
1979 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1984 /* get partial device descriptor, some devices crash on this */
1985 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1986 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1988 DPRINTF("Trying fallback for getting the USB device descriptor\n");
1989 /* try 8 bytes bMaxPacketSize */
1990 udev->ddesc.bMaxPacketSize = 8;
1991 /* get full device descriptor */
1992 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1995 /* try 16 bytes bMaxPacketSize */
1996 udev->ddesc.bMaxPacketSize = 16;
1997 /* get full device descriptor */
1998 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2001 /* try 32/64 bytes bMaxPacketSize */
2002 udev->ddesc.bMaxPacketSize = 32;
2004 /* get the full device descriptor */
2005 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2009 DPRINTF("Minimum bMaxPacketSize is large enough "
2010 "to hold the complete device descriptor or "
2011 "only one bMaxPacketSize choice\n");
2013 /* get the full device descriptor */
2014 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2016 /* try one more time, if error */
2018 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2023 DPRINTFN(0, "getting device descriptor "
2024 "at addr %d failed, %s\n", udev->address,
2029 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
2030 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
2031 udev->address, UGETW(udev->ddesc.bcdUSB),
2032 udev->ddesc.bDeviceClass,
2033 udev->ddesc.bDeviceSubClass,
2034 udev->ddesc.bDeviceProtocol,
2035 udev->ddesc.bMaxPacketSize,
2036 udev->ddesc.bLength,
2042 /*------------------------------------------------------------------------*
2043 * usbd_req_re_enumerate
2045 * NOTE: After this function returns the hardware is in the
2046 * unconfigured state! The application is responsible for setting a
2047 * new configuration.
2052 *------------------------------------------------------------------------*/
2054 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2056 struct usb_device *parent_hub;
2059 uint8_t do_retry = 1;
2061 if (udev->flags.usb_mode != USB_MODE_HOST) {
2062 return (USB_ERR_INVAL);
2064 old_addr = udev->address;
2065 parent_hub = udev->parent_hub;
2066 if (parent_hub == NULL) {
2067 return (USB_ERR_INVAL);
2070 #if USB_HAVE_TT_SUPPORT
2072 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2073 * speed device, if any.
2075 if (udev->parent_hs_hub != NULL &&
2076 udev->speed != USB_SPEED_HIGH) {
2077 DPRINTF("Trying to reset parent High Speed TT.\n");
2078 if (udev->parent_hs_hub == parent_hub &&
2079 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2080 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2081 /* we can reset the whole TT */
2082 err = usbd_req_reset_tt(parent_hub, NULL,
2085 /* only reset a particular device and endpoint */
2086 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2087 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2090 DPRINTF("Resetting parent High "
2091 "Speed TT failed (%s).\n",
2096 /* Try to warm reset first */
2097 if (parent_hub->speed == USB_SPEED_SUPER)
2098 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2100 /* Try to reset the parent HUB port. */
2101 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2103 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2104 old_addr, usbd_errstr(err));
2109 * After that the port has been reset our device should be at
2112 udev->address = USB_START_ADDR;
2114 /* reset "bMaxPacketSize" */
2115 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2117 /* reset USB state */
2118 usb_set_device_state(udev, USB_STATE_POWERED);
2121 * Restore device address:
2123 err = usbd_req_set_address(udev, mtx, old_addr);
2125 /* XXX ignore any errors! */
2126 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2127 old_addr, usbd_errstr(err));
2130 * Restore device address, if the controller driver did not
2133 if (udev->address == USB_START_ADDR)
2134 udev->address = old_addr;
2136 /* setup the device descriptor and the initial "wMaxPacketSize" */
2137 err = usbd_setup_device_desc(udev, mtx);
2140 if (err && do_retry) {
2141 /* give the USB firmware some time to load */
2142 usb_pause_mtx(mtx, hz / 2);
2143 /* no more retries after this retry */
2148 /* restore address */
2149 if (udev->address == USB_START_ADDR)
2150 udev->address = old_addr;
2151 /* update state, if successful */
2153 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2157 /*------------------------------------------------------------------------*
2158 * usbd_req_clear_device_feature
2163 *------------------------------------------------------------------------*/
2165 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2168 struct usb_device_request req;
2170 req.bmRequestType = UT_WRITE_DEVICE;
2171 req.bRequest = UR_CLEAR_FEATURE;
2172 USETW(req.wValue, sel);
2173 USETW(req.wIndex, 0);
2174 USETW(req.wLength, 0);
2175 return (usbd_do_request(udev, mtx, &req, 0));
2178 /*------------------------------------------------------------------------*
2179 * usbd_req_set_device_feature
2184 *------------------------------------------------------------------------*/
2186 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2189 struct usb_device_request req;
2191 req.bmRequestType = UT_WRITE_DEVICE;
2192 req.bRequest = UR_SET_FEATURE;
2193 USETW(req.wValue, sel);
2194 USETW(req.wIndex, 0);
2195 USETW(req.wLength, 0);
2196 return (usbd_do_request(udev, mtx, &req, 0));
2199 /*------------------------------------------------------------------------*
2205 *------------------------------------------------------------------------*/
2207 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2210 struct usb_device_request req;
2212 /* For single TT HUBs the port should be 1 */
2214 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2215 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2218 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2219 req.bRequest = UR_RESET_TT;
2220 USETW(req.wValue, 0);
2221 req.wIndex[0] = port;
2223 USETW(req.wLength, 0);
2224 return (usbd_do_request(udev, mtx, &req, 0));
2227 /*------------------------------------------------------------------------*
2228 * usbd_req_clear_tt_buffer
2230 * For single TT HUBs the port should be 1.
2235 *------------------------------------------------------------------------*/
2237 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2238 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2240 struct usb_device_request req;
2243 /* For single TT HUBs the port should be 1 */
2245 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2246 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2249 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2250 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2252 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2253 req.bRequest = UR_CLEAR_TT_BUFFER;
2254 USETW(req.wValue, wValue);
2255 req.wIndex[0] = port;
2257 USETW(req.wLength, 0);
2258 return (usbd_do_request(udev, mtx, &req, 0));
2261 /*------------------------------------------------------------------------*
2262 * usbd_req_set_port_link_state
2264 * USB 3.0 specific request
2269 *------------------------------------------------------------------------*/
2271 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2272 uint8_t port, uint8_t link_state)
2274 struct usb_device_request req;
2276 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2277 req.bRequest = UR_SET_FEATURE;
2278 USETW(req.wValue, UHF_PORT_LINK_STATE);
2279 req.wIndex[0] = port;
2280 req.wIndex[1] = link_state;
2281 USETW(req.wLength, 0);
2282 return (usbd_do_request(udev, mtx, &req, 0));
2285 /*------------------------------------------------------------------------*
2286 * usbd_req_set_lpm_info
2288 * USB 2.0 specific request for Link Power Management.
2292 * USB_ERR_PENDING_REQUESTS: NYET
2293 * USB_ERR_TIMEOUT: TIMEOUT
2294 * USB_ERR_STALL: STALL
2296 *------------------------------------------------------------------------*/
2298 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2299 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2301 struct usb_device_request req;
2305 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2306 req.bRequest = UR_SET_AND_TEST;
2307 USETW(req.wValue, UHF_PORT_L1);
2308 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2309 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2310 USETW(req.wLength, sizeof(buf));
2312 /* set default value in case of short transfer */
2315 err = usbd_do_request(udev, mtx, &req, buf);
2320 case 0x00: /* SUCCESS */
2322 case 0x10: /* NYET */
2323 err = USB_ERR_PENDING_REQUESTS;
2325 case 0x11: /* TIMEOUT */
2326 err = USB_ERR_TIMEOUT;
2328 case 0x30: /* STALL */
2329 err = USB_ERR_STALLED;
2331 default: /* reserved */
2332 err = USB_ERR_IOERROR;