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 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 */
278 /* setup a clear-stall packet */
280 req.bmRequestType = UT_WRITE_ENDPOINT;
281 req.bRequest = UR_CLEAR_FEATURE;
282 USETW(req.wValue, UF_ENDPOINT_HALT);
283 req.wIndex[0] = ep->edesc->bEndpointAddress;
285 USETW(req.wLength, 0);
287 /* copy in the transfer */
289 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
292 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
294 USB_BUS_UNLOCK(udev->bus);
296 usbd_transfer_submit(xfer);
298 USB_BUS_LOCK(udev->bus);
306 if (error == USB_ERR_CANCELLED)
309 DPRINTF("Clear stall failed.\n");
312 * Some VMs like VirtualBox always return failure on
313 * clear-stall which we sometimes should just ignore.
317 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
320 if (error == USB_ERR_TIMEOUT) {
321 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
322 DPRINTF("Trying to re-enumerate.\n");
323 usbd_start_re_enumerate(udev);
325 udev->clear_stall_errors++;
326 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
327 DPRINTF("Trying to re-enumerate.\n");
328 usbd_start_re_enumerate(udev);
334 /* store current endpoint */
336 USB_BUS_UNLOCK(udev->bus);
339 static usb_handle_req_t *
340 usbd_get_hr_func(struct usb_device *udev)
342 /* figure out if there is a Handle Request function */
343 if (udev->flags.usb_mode == USB_MODE_DEVICE)
344 return (usb_temp_get_desc_p);
345 else if (udev->parent_hub == NULL)
346 return (udev->bus->methods->roothub_exec);
351 /*------------------------------------------------------------------------*
352 * usbd_do_request_flags and usbd_do_request
354 * Description of arguments passed to these functions:
356 * "udev" - this is the "usb_device" structure pointer on which the
357 * request should be performed. It is possible to call this function
358 * in both Host Side mode and Device Side mode.
360 * "mtx" - if this argument is non-NULL the mutex pointed to by it
361 * will get dropped and picked up during the execution of this
362 * function, hence this function sometimes needs to sleep. If this
363 * argument is NULL it has no effect.
365 * "req" - this argument must always be non-NULL and points to an
366 * 8-byte structure holding the USB request to be done. The USB
367 * request structure has a bit telling the direction of the USB
368 * request, if it is a read or a write.
370 * "data" - if the "wLength" part of the structure pointed to by "req"
371 * is non-zero this argument must point to a valid kernel buffer which
372 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
375 * "flags" - here is a list of valid flags:
377 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
380 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
381 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
382 * sysctl. This flag is mostly useful for debugging.
384 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
387 * "actlen" - if non-NULL the actual transfer length will be stored in
388 * the 16-bit unsigned integer pointed to by "actlen". This
389 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
392 * "timeout" - gives the timeout for the control transfer in
393 * milliseconds. A "timeout" value less than 50 milliseconds is
394 * treated like a 50 millisecond timeout. A "timeout" value greater
395 * than 30 seconds is treated like a 30 second timeout. This USB stack
396 * does not allow control requests without a timeout.
398 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
399 * will be serialized by the use of the USB device enumeration lock.
404 *------------------------------------------------------------------------*/
406 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
407 struct usb_device_request *req, void *data, uint16_t flags,
408 uint16_t *actlen, usb_timeout_t timeout)
411 struct usb_ctrl_debug_bits dbg;
413 usb_handle_req_t *hr_func;
414 struct usb_xfer *xfer;
417 usb_ticks_t start_ticks;
418 usb_ticks_t delta_ticks;
419 usb_ticks_t max_ticks;
426 /* timeout is too small */
429 if (timeout > 30000) {
430 /* timeout is too big */
433 length = UGETW(req->wLength);
435 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
436 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
437 udev, req->bmRequestType, req->bRequest,
438 req->wValue[1], req->wValue[0],
439 req->wIndex[1], req->wIndex[0],
440 req->wLength[1], req->wLength[0]);
442 /* Check if the device is still alive */
443 if (udev->state < USB_STATE_POWERED) {
444 DPRINTF("usb device has gone\n");
445 return (USB_ERR_NOT_CONFIGURED);
449 * Set "actlen" to a known value in case the caller does not
450 * check the return value:
455 #if (USB_HAVE_USER_IO == 0)
456 if (flags & USB_USER_DATA_PTR)
457 return (USB_ERR_INVAL);
459 if ((mtx != NULL) && (mtx != &Giant)) {
461 USB_MTX_ASSERT(mtx, MA_NOTOWNED);
465 * Serialize access to this function:
467 do_unlock = usbd_ctrl_lock(udev);
469 hr_func = usbd_get_hr_func(udev);
471 if (hr_func != NULL) {
472 DPRINTF("Handle Request function is set\n");
477 if (!(req->bmRequestType & UT_READ)) {
479 DPRINTFN(1, "The handle request function "
480 "does not support writing data!\n");
486 /* The root HUB code needs the BUS lock locked */
488 USB_BUS_LOCK(udev->bus);
489 err = (hr_func) (udev, req, &desc, &temp);
490 USB_BUS_UNLOCK(udev->bus);
496 if (!(flags & USB_SHORT_XFER_OK)) {
497 err = USB_ERR_SHORT_XFER;
507 if (flags & USB_USER_DATA_PTR) {
508 if (copyout(desc, data, length)) {
514 memcpy(data, desc, length);
516 goto done; /* success */
520 * Setup a new USB transfer or use the existing one, if any:
522 usbd_ctrl_transfer_setup(udev);
524 xfer = udev->ctrl_xfer[0];
526 /* most likely out of memory */
533 usbd_get_debug_bits(udev, req, &dbg);
535 /* Check for fault injection */
537 flags |= USB_DELAY_STATUS_STAGE;
541 if (flags & USB_DELAY_STATUS_STAGE)
542 xfer->flags.manual_status = 1;
544 xfer->flags.manual_status = 0;
546 if (flags & USB_SHORT_XFER_OK)
547 xfer->flags.short_xfer_ok = 1;
549 xfer->flags.short_xfer_ok = 0;
551 xfer->timeout = timeout;
555 max_ticks = USB_MS_TO_TICKS(timeout);
557 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
559 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
563 if (temp > usbd_xfer_max_len(xfer)) {
564 temp = usbd_xfer_max_len(xfer);
567 if (xfer->flags.manual_status) {
568 if (usbd_xfer_frame_len(xfer, 0) != 0) {
569 /* Execute data stage separately */
571 } else if (temp > 0) {
576 if (dbg.ds_delay > 0) {
578 xfer->xroot->xfer_mtx,
579 USB_MS_TO_TICKS(dbg.ds_delay));
580 /* make sure we don't time out */
586 usbd_xfer_set_frame_len(xfer, 1, temp);
589 if (!(req->bmRequestType & UT_READ)) {
591 if (flags & USB_USER_DATA_PTR) {
592 USB_XFER_UNLOCK(xfer);
593 err = usbd_copy_in_user(xfer->frbuffers + 1,
602 usbd_copy_in(xfer->frbuffers + 1,
605 usbd_xfer_set_frames(xfer, 2);
607 if (usbd_xfer_frame_len(xfer, 0) == 0) {
608 if (xfer->flags.manual_status) {
614 if (dbg.ss_delay > 0) {
616 xfer->xroot->xfer_mtx,
617 USB_MS_TO_TICKS(dbg.ss_delay));
618 /* make sure we don't time out */
622 xfer->flags.manual_status = 0;
627 usbd_xfer_set_frames(xfer, 1);
630 usbd_transfer_start(xfer);
632 while (usbd_transfer_pending(xfer)) {
633 cv_wait(&udev->ctrlreq_cv,
634 xfer->xroot->xfer_mtx);
643 /* get actual length of DATA stage */
645 if (xfer->aframes < 2) {
648 acttemp = usbd_xfer_frame_len(xfer, 1);
651 /* check for short packet */
653 if (temp > acttemp) {
658 if (req->bmRequestType & UT_READ) {
660 if (flags & USB_USER_DATA_PTR) {
661 USB_XFER_UNLOCK(xfer);
662 err = usbd_copy_out_user(xfer->frbuffers + 1,
671 usbd_copy_out(xfer->frbuffers + 1,
676 * Clear "frlengths[0]" so that we don't send the setup
679 usbd_xfer_set_frame_len(xfer, 0, 0);
681 /* update length and data pointer */
683 data = USB_ADD_BYTES(data, temp);
688 /* check for timeout */
690 delta_ticks = ticks - start_ticks;
691 if (delta_ticks > max_ticks) {
693 err = USB_ERR_TIMEOUT;
703 * Make sure that the control endpoint is no longer
704 * blocked in case of a non-transfer related error:
706 usbd_transfer_stop(xfer);
708 USB_XFER_UNLOCK(xfer);
712 usbd_ctrl_unlock(udev);
714 if ((mtx != NULL) && (mtx != &Giant))
718 case USB_ERR_NORMAL_COMPLETION:
719 case USB_ERR_SHORT_XFER:
720 case USB_ERR_STALLED:
721 case USB_ERR_CANCELLED:
724 DPRINTF("I/O error - 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);
1006 if ((min_len < 2) || (max_len < 2)) {
1007 err = USB_ERR_INVAL;
1010 USETW(req.wLength, min_len);
1012 err = usbd_do_request_flags(udev, mtx, &req,
1013 desc, 0, NULL, 500 /* ms */);
1021 usb_pause_mtx(mtx, hz / 5);
1027 if (min_len == max_len) {
1029 /* enforce correct length */
1030 if ((buf[0] > min_len) && (actlen == NULL))
1033 /* enforce correct type */
1040 if (max_len > buf[0]) {
1043 /* zero minimum data */
1045 while (min_len > max_len) {
1050 /* set new minimum length */
1055 if (actlen != NULL) {
1064 /*------------------------------------------------------------------------*
1065 * usbd_req_get_string_any
1067 * This function will return the string given by "string_index"
1068 * using the first language ID. The maximum length "len" includes
1069 * the terminating zero. The "len" argument should be twice as
1070 * big pluss 2 bytes, compared with the actual maximum string length !
1075 *------------------------------------------------------------------------*/
1077 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1078 uint16_t len, uint8_t string_index)
1089 /* should not happen */
1090 return (USB_ERR_NORMAL_COMPLETION);
1092 if (string_index == 0) {
1093 /* this is the language table */
1095 return (USB_ERR_INVAL);
1097 if (udev->flags.no_strings) {
1099 return (USB_ERR_STALLED);
1101 err = usbd_req_get_string_desc
1102 (udev, mtx, buf, len, udev->langid, string_index);
1107 temp = (uint8_t *)buf;
1110 /* string length is too short */
1112 return (USB_ERR_INVAL);
1114 /* reserve one byte for terminating zero */
1117 /* find maximum length */
1119 n = (temp[0] / 2) - 1;
1123 /* skip descriptor header */
1126 /* reset swap state */
1129 /* convert and filter */
1130 for (i = 0; (i != n); i++) {
1131 c = UGETW(temp + (2 * i));
1133 /* convert from Unicode, handle buggy strings */
1134 if (((c & 0xff00) == 0) && (swap & 1)) {
1135 /* Little Endian, default */
1138 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1143 /* silently skip bad character */
1148 * Filter by default - We only allow alphanumerical
1149 * and a few more to avoid any problems with scripts
1166 /* silently skip bad character */
1168 *s = 0; /* zero terminate resulting string */
1169 return (USB_ERR_NORMAL_COMPLETION);
1172 /*------------------------------------------------------------------------*
1173 * usbd_req_get_string_desc
1175 * If you don't know the language ID, consider using
1176 * "usbd_req_get_string_any()".
1181 *------------------------------------------------------------------------*/
1183 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1184 uint16_t max_len, uint16_t lang_id,
1185 uint8_t string_index)
1187 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1188 UDESC_STRING, string_index, 0));
1191 /*------------------------------------------------------------------------*
1192 * usbd_req_get_config_desc_ptr
1194 * This function is used in device side mode to retrieve the pointer
1195 * to the generated config descriptor. This saves allocating space for
1196 * an additional config descriptor when setting the configuration.
1201 *------------------------------------------------------------------------*/
1203 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1204 struct usb_config_descriptor **ppcd, uint16_t wValue)
1206 struct usb_device_request req;
1207 usb_handle_req_t *hr_func;
1212 req.bmRequestType = UT_READ_DEVICE;
1213 req.bRequest = UR_GET_DESCRIPTOR;
1214 USETW(req.wValue, wValue);
1215 USETW(req.wIndex, 0);
1216 USETW(req.wLength, 0);
1221 hr_func = usbd_get_hr_func(udev);
1223 if (hr_func == NULL)
1224 err = USB_ERR_INVAL;
1226 USB_BUS_LOCK(udev->bus);
1227 err = (hr_func) (udev, &req, &ptr, &len);
1228 USB_BUS_UNLOCK(udev->bus);
1233 else if (ptr == NULL)
1234 err = USB_ERR_INVAL;
1236 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1241 /*------------------------------------------------------------------------*
1242 * usbd_req_get_config_desc
1247 *------------------------------------------------------------------------*/
1249 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1250 struct usb_config_descriptor *d, uint8_t conf_index)
1254 DPRINTFN(4, "confidx=%d\n", conf_index);
1256 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1257 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1261 /* Extra sanity checking */
1262 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1263 err = USB_ERR_INVAL;
1269 /*------------------------------------------------------------------------*
1270 * usbd_alloc_config_desc
1272 * This function is used to allocate a zeroed configuration
1278 *------------------------------------------------------------------------*/
1280 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1282 if (size > USB_CONFIG_MAX) {
1283 DPRINTF("Configuration descriptor too big\n");
1286 #if (USB_HAVE_FIXED_CONFIG == 0)
1287 return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1289 memset(udev->config_data, 0, sizeof(udev->config_data));
1290 return (udev->config_data);
1294 /*------------------------------------------------------------------------*
1295 * usbd_alloc_config_desc
1297 * This function is used to free a configuration descriptor.
1298 *------------------------------------------------------------------------*/
1300 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1302 #if (USB_HAVE_FIXED_CONFIG == 0)
1303 free(ptr, M_USBDEV);
1307 /*------------------------------------------------------------------------*
1308 * usbd_req_get_config_desc_full
1310 * This function gets the complete USB configuration descriptor and
1311 * ensures that "wTotalLength" is correct. The returned configuration
1312 * descriptor is freed by calling "usbd_free_config_desc()".
1317 *------------------------------------------------------------------------*/
1319 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1320 struct usb_config_descriptor **ppcd, uint8_t index)
1322 struct usb_config_descriptor cd;
1323 struct usb_config_descriptor *cdesc;
1327 DPRINTFN(4, "index=%d\n", index);
1331 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1335 /* get full descriptor */
1336 len = UGETW(cd.wTotalLength);
1337 if (len < (uint32_t)sizeof(*cdesc)) {
1338 /* corrupt descriptor */
1339 return (USB_ERR_INVAL);
1340 } else if (len > USB_CONFIG_MAX) {
1341 DPRINTF("Configuration descriptor was truncated\n");
1342 len = USB_CONFIG_MAX;
1344 cdesc = usbd_alloc_config_desc(udev, len);
1346 return (USB_ERR_NOMEM);
1347 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1348 UDESC_CONFIG, index, 3);
1350 usbd_free_config_desc(udev, cdesc);
1353 /* make sure that the device is not fooling us: */
1354 USETW(cdesc->wTotalLength, len);
1358 return (0); /* success */
1361 /*------------------------------------------------------------------------*
1362 * usbd_req_get_device_desc
1367 *------------------------------------------------------------------------*/
1369 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1370 struct usb_device_descriptor *d)
1373 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1374 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1377 /*------------------------------------------------------------------------*
1378 * usbd_req_get_alt_interface_no
1383 *------------------------------------------------------------------------*/
1385 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1386 uint8_t *alt_iface_no, uint8_t iface_index)
1388 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1389 struct usb_device_request req;
1391 if ((iface == NULL) || (iface->idesc == NULL))
1392 return (USB_ERR_INVAL);
1394 req.bmRequestType = UT_READ_INTERFACE;
1395 req.bRequest = UR_GET_INTERFACE;
1396 USETW(req.wValue, 0);
1397 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1399 USETW(req.wLength, 1);
1400 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1403 /*------------------------------------------------------------------------*
1404 * usbd_req_set_alt_interface_no
1409 *------------------------------------------------------------------------*/
1411 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1412 uint8_t iface_index, uint8_t alt_no)
1414 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1415 struct usb_device_request req;
1417 if ((iface == NULL) || (iface->idesc == NULL))
1418 return (USB_ERR_INVAL);
1420 req.bmRequestType = UT_WRITE_INTERFACE;
1421 req.bRequest = UR_SET_INTERFACE;
1422 req.wValue[0] = alt_no;
1424 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1426 USETW(req.wLength, 0);
1427 return (usbd_do_request(udev, mtx, &req, 0));
1430 /*------------------------------------------------------------------------*
1431 * usbd_req_get_device_status
1436 *------------------------------------------------------------------------*/
1438 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1439 struct usb_status *st)
1441 struct usb_device_request req;
1443 req.bmRequestType = UT_READ_DEVICE;
1444 req.bRequest = UR_GET_STATUS;
1445 USETW(req.wValue, 0);
1446 USETW(req.wIndex, 0);
1447 USETW(req.wLength, sizeof(*st));
1448 return (usbd_do_request(udev, mtx, &req, st));
1451 /*------------------------------------------------------------------------*
1452 * usbd_req_get_hub_descriptor
1457 *------------------------------------------------------------------------*/
1459 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1460 struct usb_hub_descriptor *hd, uint8_t nports)
1462 struct usb_device_request req;
1463 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1465 req.bmRequestType = UT_READ_CLASS_DEVICE;
1466 req.bRequest = UR_GET_DESCRIPTOR;
1467 USETW2(req.wValue, UDESC_HUB, 0);
1468 USETW(req.wIndex, 0);
1469 USETW(req.wLength, len);
1470 return (usbd_do_request(udev, mtx, &req, hd));
1473 /*------------------------------------------------------------------------*
1474 * usbd_req_get_ss_hub_descriptor
1479 *------------------------------------------------------------------------*/
1481 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1482 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1484 struct usb_device_request req;
1485 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1487 req.bmRequestType = UT_READ_CLASS_DEVICE;
1488 req.bRequest = UR_GET_DESCRIPTOR;
1489 USETW2(req.wValue, UDESC_SS_HUB, 0);
1490 USETW(req.wIndex, 0);
1491 USETW(req.wLength, len);
1492 return (usbd_do_request(udev, mtx, &req, hd));
1495 /*------------------------------------------------------------------------*
1496 * usbd_req_get_hub_status
1501 *------------------------------------------------------------------------*/
1503 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1504 struct usb_hub_status *st)
1506 struct usb_device_request req;
1508 req.bmRequestType = UT_READ_CLASS_DEVICE;
1509 req.bRequest = UR_GET_STATUS;
1510 USETW(req.wValue, 0);
1511 USETW(req.wIndex, 0);
1512 USETW(req.wLength, sizeof(struct usb_hub_status));
1513 return (usbd_do_request(udev, mtx, &req, st));
1516 /*------------------------------------------------------------------------*
1517 * usbd_req_set_address
1519 * This function is used to set the address for an USB device. After
1520 * port reset the USB device will respond at address zero.
1525 *------------------------------------------------------------------------*/
1527 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1529 struct usb_device_request req;
1532 DPRINTFN(6, "setting device address=%d\n", addr);
1534 req.bmRequestType = UT_WRITE_DEVICE;
1535 req.bRequest = UR_SET_ADDRESS;
1536 USETW(req.wValue, addr);
1537 USETW(req.wIndex, 0);
1538 USETW(req.wLength, 0);
1540 err = USB_ERR_INVAL;
1542 /* check if USB controller handles set address */
1543 if (udev->bus->methods->set_address != NULL)
1544 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1546 if (err != USB_ERR_INVAL)
1549 /* Setting the address should not take more than 1 second ! */
1550 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1551 USB_DELAY_STATUS_STAGE, NULL, 1000);
1554 /* allow device time to set new address */
1556 USB_MS_TO_TICKS(usb_set_address_settle));
1561 /*------------------------------------------------------------------------*
1562 * usbd_req_get_port_status
1567 *------------------------------------------------------------------------*/
1569 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1570 struct usb_port_status *ps, uint8_t port)
1572 struct usb_device_request req;
1574 req.bmRequestType = UT_READ_CLASS_OTHER;
1575 req.bRequest = UR_GET_STATUS;
1576 USETW(req.wValue, 0);
1577 req.wIndex[0] = port;
1579 USETW(req.wLength, sizeof *ps);
1580 return (usbd_do_request(udev, mtx, &req, ps));
1583 /*------------------------------------------------------------------------*
1584 * usbd_req_clear_hub_feature
1589 *------------------------------------------------------------------------*/
1591 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1594 struct usb_device_request req;
1596 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1597 req.bRequest = UR_CLEAR_FEATURE;
1598 USETW(req.wValue, sel);
1599 USETW(req.wIndex, 0);
1600 USETW(req.wLength, 0);
1601 return (usbd_do_request(udev, mtx, &req, 0));
1604 /*------------------------------------------------------------------------*
1605 * usbd_req_set_hub_feature
1610 *------------------------------------------------------------------------*/
1612 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1615 struct usb_device_request req;
1617 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1618 req.bRequest = UR_SET_FEATURE;
1619 USETW(req.wValue, sel);
1620 USETW(req.wIndex, 0);
1621 USETW(req.wLength, 0);
1622 return (usbd_do_request(udev, mtx, &req, 0));
1625 /*------------------------------------------------------------------------*
1626 * usbd_req_set_hub_u1_timeout
1631 *------------------------------------------------------------------------*/
1633 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1634 uint8_t port, uint8_t timeout)
1636 struct usb_device_request req;
1638 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1639 req.bRequest = UR_SET_FEATURE;
1640 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1641 req.wIndex[0] = port;
1642 req.wIndex[1] = timeout;
1643 USETW(req.wLength, 0);
1644 return (usbd_do_request(udev, mtx, &req, 0));
1647 /*------------------------------------------------------------------------*
1648 * usbd_req_set_hub_u2_timeout
1653 *------------------------------------------------------------------------*/
1655 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1656 uint8_t port, uint8_t timeout)
1658 struct usb_device_request req;
1660 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1661 req.bRequest = UR_SET_FEATURE;
1662 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1663 req.wIndex[0] = port;
1664 req.wIndex[1] = timeout;
1665 USETW(req.wLength, 0);
1666 return (usbd_do_request(udev, mtx, &req, 0));
1669 /*------------------------------------------------------------------------*
1670 * usbd_req_set_hub_depth
1675 *------------------------------------------------------------------------*/
1677 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1680 struct usb_device_request req;
1682 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1683 req.bRequest = UR_SET_HUB_DEPTH;
1684 USETW(req.wValue, depth);
1685 USETW(req.wIndex, 0);
1686 USETW(req.wLength, 0);
1687 return (usbd_do_request(udev, mtx, &req, 0));
1690 /*------------------------------------------------------------------------*
1691 * usbd_req_clear_port_feature
1696 *------------------------------------------------------------------------*/
1698 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1699 uint8_t port, uint16_t sel)
1701 struct usb_device_request req;
1703 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1704 req.bRequest = UR_CLEAR_FEATURE;
1705 USETW(req.wValue, sel);
1706 req.wIndex[0] = port;
1708 USETW(req.wLength, 0);
1709 return (usbd_do_request(udev, mtx, &req, 0));
1712 /*------------------------------------------------------------------------*
1713 * usbd_req_set_port_feature
1718 *------------------------------------------------------------------------*/
1720 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1721 uint8_t port, uint16_t sel)
1723 struct usb_device_request req;
1725 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1726 req.bRequest = UR_SET_FEATURE;
1727 USETW(req.wValue, sel);
1728 req.wIndex[0] = port;
1730 USETW(req.wLength, 0);
1731 return (usbd_do_request(udev, mtx, &req, 0));
1734 /*------------------------------------------------------------------------*
1735 * usbd_req_set_protocol
1740 *------------------------------------------------------------------------*/
1742 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1743 uint8_t iface_index, uint16_t report)
1745 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1746 struct usb_device_request req;
1748 if ((iface == NULL) || (iface->idesc == NULL)) {
1749 return (USB_ERR_INVAL);
1751 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1752 iface, report, iface->idesc->bInterfaceNumber);
1754 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1755 req.bRequest = UR_SET_PROTOCOL;
1756 USETW(req.wValue, report);
1757 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1759 USETW(req.wLength, 0);
1760 return (usbd_do_request(udev, mtx, &req, 0));
1763 /*------------------------------------------------------------------------*
1764 * usbd_req_set_report
1769 *------------------------------------------------------------------------*/
1771 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1772 uint8_t iface_index, uint8_t type, uint8_t id)
1774 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1775 struct usb_device_request req;
1777 if ((iface == NULL) || (iface->idesc == NULL)) {
1778 return (USB_ERR_INVAL);
1780 DPRINTFN(5, "len=%d\n", len);
1782 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1783 req.bRequest = UR_SET_REPORT;
1784 USETW2(req.wValue, type, id);
1785 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1787 USETW(req.wLength, len);
1788 return (usbd_do_request(udev, mtx, &req, data));
1791 /*------------------------------------------------------------------------*
1792 * usbd_req_get_report
1797 *------------------------------------------------------------------------*/
1799 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1800 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1802 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1803 struct usb_device_request req;
1805 if ((iface == NULL) || (iface->idesc == NULL)) {
1806 return (USB_ERR_INVAL);
1808 DPRINTFN(5, "len=%d\n", len);
1810 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1811 req.bRequest = UR_GET_REPORT;
1812 USETW2(req.wValue, type, id);
1813 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1815 USETW(req.wLength, len);
1816 return (usbd_do_request(udev, mtx, &req, data));
1819 /*------------------------------------------------------------------------*
1825 *------------------------------------------------------------------------*/
1827 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1828 uint8_t iface_index, uint8_t duration, uint8_t id)
1830 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1831 struct usb_device_request req;
1833 if ((iface == NULL) || (iface->idesc == NULL)) {
1834 return (USB_ERR_INVAL);
1836 DPRINTFN(5, "%d %d\n", duration, id);
1838 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1839 req.bRequest = UR_SET_IDLE;
1840 USETW2(req.wValue, duration, id);
1841 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1843 USETW(req.wLength, 0);
1844 return (usbd_do_request(udev, mtx, &req, 0));
1847 /*------------------------------------------------------------------------*
1848 * usbd_req_get_report_descriptor
1853 *------------------------------------------------------------------------*/
1855 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1856 void *d, uint16_t size, uint8_t iface_index)
1858 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1859 struct usb_device_request req;
1861 if ((iface == NULL) || (iface->idesc == NULL)) {
1862 return (USB_ERR_INVAL);
1864 req.bmRequestType = UT_READ_INTERFACE;
1865 req.bRequest = UR_GET_DESCRIPTOR;
1866 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1867 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1869 USETW(req.wLength, size);
1870 return (usbd_do_request(udev, mtx, &req, d));
1873 /*------------------------------------------------------------------------*
1874 * usbd_req_set_config
1876 * This function is used to select the current configuration number in
1877 * both USB device side mode and USB host side mode. When setting the
1878 * configuration the function of the interfaces can change.
1883 *------------------------------------------------------------------------*/
1885 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1887 struct usb_device_request req;
1889 DPRINTF("setting config %d\n", conf);
1891 /* do "set configuration" request */
1893 req.bmRequestType = UT_WRITE_DEVICE;
1894 req.bRequest = UR_SET_CONFIG;
1895 req.wValue[0] = conf;
1897 USETW(req.wIndex, 0);
1898 USETW(req.wLength, 0);
1899 return (usbd_do_request(udev, mtx, &req, 0));
1902 /*------------------------------------------------------------------------*
1903 * usbd_req_get_config
1908 *------------------------------------------------------------------------*/
1910 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1912 struct usb_device_request req;
1914 req.bmRequestType = UT_READ_DEVICE;
1915 req.bRequest = UR_GET_CONFIG;
1916 USETW(req.wValue, 0);
1917 USETW(req.wIndex, 0);
1918 USETW(req.wLength, 1);
1919 return (usbd_do_request(udev, mtx, &req, pconf));
1922 /*------------------------------------------------------------------------*
1923 * usbd_setup_device_desc
1924 *------------------------------------------------------------------------*/
1926 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1931 * Get the first 8 bytes of the device descriptor !
1933 * NOTE: "usbd_do_request()" will check the device descriptor
1934 * next time we do a request to see if the maximum packet size
1935 * changed! The 8 first bytes of the device descriptor
1936 * contains the maximum packet size to use on control endpoint
1937 * 0. If this value is different from "USB_MAX_IPACKET" a new
1938 * USB control request will be setup!
1940 switch (udev->speed) {
1941 case USB_SPEED_FULL:
1942 if (usb_full_ddesc != 0) {
1943 /* get full device descriptor */
1944 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1949 /* get partial device descriptor, some devices crash on this */
1950 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1951 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1955 /* get the full device descriptor */
1956 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1960 DPRINTF("Minimum bMaxPacketSize is large enough "
1961 "to hold the complete device descriptor or "
1962 "only one bMaxPacketSize choice\n");
1964 /* get the full device descriptor */
1965 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1967 /* try one more time, if error */
1969 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1974 DPRINTFN(0, "getting device descriptor "
1975 "at addr %d failed, %s\n", udev->address,
1980 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1981 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1982 udev->address, UGETW(udev->ddesc.bcdUSB),
1983 udev->ddesc.bDeviceClass,
1984 udev->ddesc.bDeviceSubClass,
1985 udev->ddesc.bDeviceProtocol,
1986 udev->ddesc.bMaxPacketSize,
1987 udev->ddesc.bLength,
1993 /*------------------------------------------------------------------------*
1994 * usbd_req_re_enumerate
1996 * NOTE: After this function returns the hardware is in the
1997 * unconfigured state! The application is responsible for setting a
1998 * new configuration.
2003 *------------------------------------------------------------------------*/
2005 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2007 struct usb_device *parent_hub;
2010 uint8_t do_retry = 1;
2012 if (udev->flags.usb_mode != USB_MODE_HOST) {
2013 return (USB_ERR_INVAL);
2015 old_addr = udev->address;
2016 parent_hub = udev->parent_hub;
2017 if (parent_hub == NULL) {
2018 return (USB_ERR_INVAL);
2021 #if USB_HAVE_TT_SUPPORT
2023 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2024 * speed device, if any.
2026 if (udev->parent_hs_hub != NULL &&
2027 udev->speed != USB_SPEED_HIGH) {
2028 DPRINTF("Trying to reset parent High Speed TT.\n");
2029 if (udev->parent_hs_hub == parent_hub &&
2030 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2031 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2032 /* we can reset the whole TT */
2033 err = usbd_req_reset_tt(parent_hub, NULL,
2036 /* only reset a particular device and endpoint */
2037 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2038 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2041 DPRINTF("Resetting parent High "
2042 "Speed TT failed (%s).\n",
2047 /* Try to warm reset first */
2048 if (parent_hub->speed == USB_SPEED_SUPER)
2049 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2051 /* Try to reset the parent HUB port. */
2052 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2054 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2055 old_addr, usbd_errstr(err));
2060 * After that the port has been reset our device should be at
2063 udev->address = USB_START_ADDR;
2065 /* reset "bMaxPacketSize" */
2066 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2068 /* reset USB state */
2069 usb_set_device_state(udev, USB_STATE_POWERED);
2072 * Restore device address:
2074 err = usbd_req_set_address(udev, mtx, old_addr);
2076 /* XXX ignore any errors! */
2077 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2078 old_addr, usbd_errstr(err));
2081 * Restore device address, if the controller driver did not
2084 if (udev->address == USB_START_ADDR)
2085 udev->address = old_addr;
2087 /* setup the device descriptor and the initial "wMaxPacketSize" */
2088 err = usbd_setup_device_desc(udev, mtx);
2091 if (err && do_retry) {
2092 /* give the USB firmware some time to load */
2093 usb_pause_mtx(mtx, hz / 2);
2094 /* no more retries after this retry */
2099 /* restore address */
2100 if (udev->address == USB_START_ADDR)
2101 udev->address = old_addr;
2102 /* update state, if successful */
2104 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2108 /*------------------------------------------------------------------------*
2109 * usbd_req_clear_device_feature
2114 *------------------------------------------------------------------------*/
2116 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2119 struct usb_device_request req;
2121 req.bmRequestType = UT_WRITE_DEVICE;
2122 req.bRequest = UR_CLEAR_FEATURE;
2123 USETW(req.wValue, sel);
2124 USETW(req.wIndex, 0);
2125 USETW(req.wLength, 0);
2126 return (usbd_do_request(udev, mtx, &req, 0));
2129 /*------------------------------------------------------------------------*
2130 * usbd_req_set_device_feature
2135 *------------------------------------------------------------------------*/
2137 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2140 struct usb_device_request req;
2142 req.bmRequestType = UT_WRITE_DEVICE;
2143 req.bRequest = UR_SET_FEATURE;
2144 USETW(req.wValue, sel);
2145 USETW(req.wIndex, 0);
2146 USETW(req.wLength, 0);
2147 return (usbd_do_request(udev, mtx, &req, 0));
2150 /*------------------------------------------------------------------------*
2156 *------------------------------------------------------------------------*/
2158 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2161 struct usb_device_request req;
2163 /* For single TT HUBs the port should be 1 */
2165 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2166 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2169 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2170 req.bRequest = UR_RESET_TT;
2171 USETW(req.wValue, 0);
2172 req.wIndex[0] = port;
2174 USETW(req.wLength, 0);
2175 return (usbd_do_request(udev, mtx, &req, 0));
2178 /*------------------------------------------------------------------------*
2179 * usbd_req_clear_tt_buffer
2181 * For single TT HUBs the port should be 1.
2186 *------------------------------------------------------------------------*/
2188 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2189 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2191 struct usb_device_request req;
2194 /* For single TT HUBs the port should be 1 */
2196 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2197 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2200 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2201 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2203 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2204 req.bRequest = UR_CLEAR_TT_BUFFER;
2205 USETW(req.wValue, wValue);
2206 req.wIndex[0] = port;
2208 USETW(req.wLength, 0);
2209 return (usbd_do_request(udev, mtx, &req, 0));
2212 /*------------------------------------------------------------------------*
2213 * usbd_req_set_port_link_state
2215 * USB 3.0 specific request
2220 *------------------------------------------------------------------------*/
2222 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2223 uint8_t port, uint8_t link_state)
2225 struct usb_device_request req;
2227 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2228 req.bRequest = UR_SET_FEATURE;
2229 USETW(req.wValue, UHF_PORT_LINK_STATE);
2230 req.wIndex[0] = port;
2231 req.wIndex[1] = link_state;
2232 USETW(req.wLength, 0);
2233 return (usbd_do_request(udev, mtx, &req, 0));
2236 /*------------------------------------------------------------------------*
2237 * usbd_req_set_lpm_info
2239 * USB 2.0 specific request for Link Power Management.
2243 * USB_ERR_PENDING_REQUESTS: NYET
2244 * USB_ERR_TIMEOUT: TIMEOUT
2245 * USB_ERR_STALL: STALL
2247 *------------------------------------------------------------------------*/
2249 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2250 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2252 struct usb_device_request req;
2256 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2257 req.bRequest = UR_SET_AND_TEST;
2258 USETW(req.wValue, UHF_PORT_L1);
2259 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2260 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2261 USETW(req.wLength, sizeof(buf));
2263 /* set default value in case of short transfer */
2266 err = usbd_do_request(udev, mtx, &req, buf);
2271 case 0x00: /* SUCCESS */
2273 case 0x10: /* NYET */
2274 err = USB_ERR_PENDING_REQUESTS;
2276 case 0x11: /* TIMEOUT */
2277 err = USB_ERR_TIMEOUT;
2279 case 0x30: /* STALL */
2280 err = USB_ERR_STALLED;
2282 default: /* reserved */
2283 err = USB_ERR_IOERROR;