3 * SPDX-License-Identifier: BSD-2-Clause
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.
318 * Some non-compliant USB devices do not implement the
319 * clear endpoint halt feature. Silently ignore such
320 * devices, when they at least respond correctly
321 * passing up a valid STALL PID packet.
323 if (error == USB_ERR_STALLED)
326 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
329 if (error == USB_ERR_TIMEOUT) {
330 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
331 DPRINTF("Trying to re-enumerate.\n");
332 usbd_start_re_enumerate(udev);
334 udev->clear_stall_errors++;
335 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
336 DPRINTF("Trying to re-enumerate.\n");
337 usbd_start_re_enumerate(udev);
343 /* store current endpoint */
345 USB_BUS_UNLOCK(udev->bus);
348 static usb_handle_req_t *
349 usbd_get_hr_func(struct usb_device *udev)
351 /* figure out if there is a Handle Request function */
352 if (udev->flags.usb_mode == USB_MODE_DEVICE)
353 return (usb_temp_get_desc_p);
354 else if (udev->parent_hub == NULL)
355 return (udev->bus->methods->roothub_exec);
360 /*------------------------------------------------------------------------*
361 * usbd_do_request_flags and usbd_do_request
363 * Description of arguments passed to these functions:
365 * "udev" - this is the "usb_device" structure pointer on which the
366 * request should be performed. It is possible to call this function
367 * in both Host Side mode and Device Side mode.
369 * "mtx" - if this argument is non-NULL the mutex pointed to by it
370 * will get dropped and picked up during the execution of this
371 * function, hence this function sometimes needs to sleep. If this
372 * argument is NULL it has no effect.
374 * "req" - this argument must always be non-NULL and points to an
375 * 8-byte structure holding the USB request to be done. The USB
376 * request structure has a bit telling the direction of the USB
377 * request, if it is a read or a write.
379 * "data" - if the "wLength" part of the structure pointed to by "req"
380 * is non-zero this argument must point to a valid kernel buffer which
381 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
384 * "flags" - here is a list of valid flags:
386 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
389 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
390 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
391 * sysctl. This flag is mostly useful for debugging.
393 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
396 * "actlen" - if non-NULL the actual transfer length will be stored in
397 * the 16-bit unsigned integer pointed to by "actlen". This
398 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
401 * "timeout" - gives the timeout for the control transfer in
402 * milliseconds. A "timeout" value less than 50 milliseconds is
403 * treated like a 50 millisecond timeout. A "timeout" value greater
404 * than 30 seconds is treated like a 30 second timeout. This USB stack
405 * does not allow control requests without a timeout.
407 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
408 * will be serialized by the use of the USB device enumeration lock.
413 *------------------------------------------------------------------------*/
415 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
416 struct usb_device_request *req, void *data, uint16_t flags,
417 uint16_t *actlen, usb_timeout_t timeout)
420 struct usb_ctrl_debug_bits dbg;
422 usb_handle_req_t *hr_func;
423 struct usb_xfer *xfer;
426 usb_ticks_t start_ticks;
427 usb_ticks_t delta_ticks;
428 usb_ticks_t max_ticks;
435 /* timeout is too small */
438 if (timeout > 30000) {
439 /* timeout is too big */
442 length = UGETW(req->wLength);
444 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
445 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
446 udev, req->bmRequestType, req->bRequest,
447 req->wValue[1], req->wValue[0],
448 req->wIndex[1], req->wIndex[0],
449 req->wLength[1], req->wLength[0]);
451 /* Check if the device is still alive */
452 if (udev->state < USB_STATE_POWERED) {
453 DPRINTF("usb device has gone\n");
454 return (USB_ERR_NOT_CONFIGURED);
458 * Set "actlen" to a known value in case the caller does not
459 * check the return value:
464 #if (USB_HAVE_USER_IO == 0)
465 if (flags & USB_USER_DATA_PTR)
466 return (USB_ERR_INVAL);
468 if ((mtx != NULL) && (mtx != &Giant)) {
470 USB_MTX_ASSERT(mtx, MA_NOTOWNED);
474 * Serialize access to this function:
476 do_unlock = usbd_ctrl_lock(udev);
478 hr_func = usbd_get_hr_func(udev);
480 if (hr_func != NULL) {
481 DPRINTF("Handle Request function is set\n");
486 if (!(req->bmRequestType & UT_READ)) {
488 DPRINTFN(1, "The handle request function "
489 "does not support writing data!\n");
495 /* The root HUB code needs the BUS lock locked */
497 USB_BUS_LOCK(udev->bus);
498 err = (hr_func) (udev, req, &desc, &temp);
499 USB_BUS_UNLOCK(udev->bus);
505 if (!(flags & USB_SHORT_XFER_OK)) {
506 err = USB_ERR_SHORT_XFER;
516 if (flags & USB_USER_DATA_PTR) {
517 if (copyout(desc, data, length)) {
523 memcpy(data, desc, length);
525 goto done; /* success */
529 * Setup a new USB transfer or use the existing one, if any:
531 usbd_ctrl_transfer_setup(udev);
533 xfer = udev->ctrl_xfer[0];
535 /* most likely out of memory */
542 usbd_get_debug_bits(udev, req, &dbg);
544 /* Check for fault injection */
546 flags |= USB_DELAY_STATUS_STAGE;
550 if (flags & USB_DELAY_STATUS_STAGE)
551 xfer->flags.manual_status = 1;
553 xfer->flags.manual_status = 0;
555 if (flags & USB_SHORT_XFER_OK)
556 xfer->flags.short_xfer_ok = 1;
558 xfer->flags.short_xfer_ok = 0;
560 xfer->timeout = timeout;
564 max_ticks = USB_MS_TO_TICKS(timeout);
566 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
568 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
572 if (temp > usbd_xfer_max_len(xfer)) {
573 temp = usbd_xfer_max_len(xfer);
576 if (xfer->flags.manual_status) {
577 if (usbd_xfer_frame_len(xfer, 0) != 0) {
578 /* Execute data stage separately */
580 } else if (temp > 0) {
585 if (dbg.ds_delay > 0) {
587 xfer->xroot->xfer_mtx,
588 USB_MS_TO_TICKS(dbg.ds_delay));
589 /* make sure we don't time out */
595 usbd_xfer_set_frame_len(xfer, 1, temp);
598 if (!(req->bmRequestType & UT_READ)) {
600 if (flags & USB_USER_DATA_PTR) {
601 USB_XFER_UNLOCK(xfer);
602 err = usbd_copy_in_user(xfer->frbuffers + 1,
611 usbd_copy_in(xfer->frbuffers + 1,
614 usbd_xfer_set_frames(xfer, 2);
616 if (usbd_xfer_frame_len(xfer, 0) == 0) {
617 if (xfer->flags.manual_status) {
623 if (dbg.ss_delay > 0) {
625 xfer->xroot->xfer_mtx,
626 USB_MS_TO_TICKS(dbg.ss_delay));
627 /* make sure we don't time out */
631 xfer->flags.manual_status = 0;
636 usbd_xfer_set_frames(xfer, 1);
639 usbd_transfer_start(xfer);
641 while (usbd_transfer_pending(xfer)) {
642 cv_wait(&udev->ctrlreq_cv,
643 xfer->xroot->xfer_mtx);
652 /* get actual length of DATA stage */
654 if (xfer->aframes < 2) {
657 acttemp = usbd_xfer_frame_len(xfer, 1);
660 /* check for short packet */
662 if (temp > acttemp) {
667 if (req->bmRequestType & UT_READ) {
669 if (flags & USB_USER_DATA_PTR) {
670 USB_XFER_UNLOCK(xfer);
671 err = usbd_copy_out_user(xfer->frbuffers + 1,
680 usbd_copy_out(xfer->frbuffers + 1,
685 * Clear "frlengths[0]" so that we don't send the setup
688 usbd_xfer_set_frame_len(xfer, 0, 0);
690 /* update length and data pointer */
692 data = USB_ADD_BYTES(data, temp);
697 /* check for timeout */
699 delta_ticks = ticks - start_ticks;
700 if (delta_ticks > max_ticks) {
702 err = USB_ERR_TIMEOUT;
712 * Make sure that the control endpoint is no longer
713 * blocked in case of a non-transfer related error:
715 usbd_transfer_stop(xfer);
717 USB_XFER_UNLOCK(xfer);
721 usbd_ctrl_unlock(udev);
723 if ((mtx != NULL) && (mtx != &Giant))
727 case USB_ERR_NORMAL_COMPLETION:
728 case USB_ERR_SHORT_XFER:
729 case USB_ERR_STALLED:
730 case USB_ERR_CANCELLED:
733 DPRINTF("error=%s - waiting a bit for TT cleanup\n",
735 usb_pause_mtx(mtx, hz / 16);
738 return ((usb_error_t)err);
741 /*------------------------------------------------------------------------*
742 * usbd_do_request_proc - factored out code
744 * This function is factored out code. It does basically the same like
745 * usbd_do_request_flags, except it will check the status of the
746 * passed process argument before doing the USB request. If the
747 * process is draining the USB_ERR_IOERROR code will be returned. It
748 * is assumed that the mutex associated with the process is locked
749 * when calling this function.
750 *------------------------------------------------------------------------*/
752 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
753 struct usb_device_request *req, void *data, uint16_t flags,
754 uint16_t *actlen, usb_timeout_t timeout)
759 /* get request data length */
760 len = UGETW(req->wLength);
762 /* check if the device is being detached */
763 if (usb_proc_is_gone(pproc)) {
764 err = USB_ERR_IOERROR;
768 /* forward the USB request */
769 err = usbd_do_request_flags(udev, pproc->up_mtx,
770 req, data, flags, actlen, timeout);
773 /* on failure we zero the data */
774 /* on short packet we zero the unused data */
775 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
777 memset(data, 0, len);
778 else if (actlen && *actlen != len)
779 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
784 /*------------------------------------------------------------------------*
785 * usbd_req_reset_port
787 * This function will instruct a USB HUB to perform a reset sequence
788 * on the specified port number.
791 * 0: Success. The USB device should now be at address zero.
792 * Else: Failure. No USB device is present and the USB port should be
794 *------------------------------------------------------------------------*/
796 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
798 struct usb_port_status ps;
806 /* clear any leftover port reset changes first */
807 usbd_req_clear_port_feature(
808 udev, mtx, port, UHF_C_PORT_RESET);
810 /* assert port reset on the given port */
811 err = usbd_req_set_port_feature(
812 udev, mtx, port, UHF_PORT_RESET);
814 /* check for errors */
819 /* wait for the device to recover from reset */
820 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
821 n += usb_port_reset_delay;
822 err = usbd_req_get_port_status(udev, mtx, &ps, port);
826 status = UGETW(ps.wPortStatus);
827 change = UGETW(ps.wPortChange);
829 /* if the device disappeared, just give up */
830 if (!(status & UPS_CURRENT_CONNECT_STATUS))
833 /* check if reset is complete */
834 if (change & UPS_C_PORT_RESET)
838 * Some Virtual Machines like VirtualBox 4.x fail to
839 * generate a port reset change event. Check if reset
840 * is no longer asserted.
842 if (!(status & UPS_RESET))
845 /* check for timeout */
852 /* clear port reset first */
853 err = usbd_req_clear_port_feature(
854 udev, mtx, port, UHF_C_PORT_RESET);
858 /* check for timeout */
860 err = USB_ERR_TIMEOUT;
863 /* wait for the device to recover from reset */
864 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
867 DPRINTFN(2, "port %d reset returning error=%s\n",
868 port, usbd_errstr(err));
872 /*------------------------------------------------------------------------*
873 * usbd_req_warm_reset_port
875 * This function will instruct an USB HUB to perform a warm reset
876 * sequence on the specified port number. This kind of reset is not
877 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
878 * for SUPER-speed USB HUBs.
881 * 0: Success. The USB device should now be available again.
882 * Else: Failure. No USB device is present and the USB port should be
884 *------------------------------------------------------------------------*/
886 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
889 struct usb_port_status ps;
897 err = usbd_req_get_port_status(udev, mtx, &ps, port);
901 status = UGETW(ps.wPortStatus);
903 switch (UPS_PORT_LINK_STATE_GET(status)) {
905 case UPS_PORT_LS_COMP_MODE:
906 case UPS_PORT_LS_LOOPBACK:
907 case UPS_PORT_LS_SS_INA:
910 DPRINTF("Wrong state for warm reset\n");
914 /* clear any leftover warm port reset changes first */
915 usbd_req_clear_port_feature(udev, mtx,
916 port, UHF_C_BH_PORT_RESET);
918 /* set warm port reset */
919 err = usbd_req_set_port_feature(udev, mtx,
920 port, UHF_BH_PORT_RESET);
926 /* wait for the device to recover from reset */
927 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
928 n += usb_port_reset_delay;
929 err = usbd_req_get_port_status(udev, mtx, &ps, port);
933 status = UGETW(ps.wPortStatus);
934 change = UGETW(ps.wPortChange);
936 /* if the device disappeared, just give up */
937 if (!(status & UPS_CURRENT_CONNECT_STATUS))
940 /* check if reset is complete */
941 if (change & UPS_C_BH_PORT_RESET)
944 /* check for timeout */
951 /* clear port reset first */
952 err = usbd_req_clear_port_feature(
953 udev, mtx, port, UHF_C_BH_PORT_RESET);
957 /* check for timeout */
959 err = USB_ERR_TIMEOUT;
962 /* wait for the device to recover from reset */
963 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
966 DPRINTFN(2, "port %d warm reset returning error=%s\n",
967 port, usbd_errstr(err));
971 /*------------------------------------------------------------------------*
974 * This function can be used to retrieve USB descriptors. It contains
975 * some additional logic like zeroing of missing descriptor bytes and
976 * retrying an USB descriptor in case of failure. The "min_len"
977 * argument specifies the minimum descriptor length. The "max_len"
978 * argument specifies the maximum descriptor length. If the real
979 * descriptor length is less than the minimum length the missing
980 * byte(s) will be zeroed. The type field, the second byte of the USB
981 * descriptor, will get forced to the correct type. If the "actlen"
982 * pointer is non-NULL, the actual length of the transfer will get
983 * stored in the 16-bit unsigned integer which it is pointing to. The
984 * first byte of the descriptor will not get updated. If the "actlen"
985 * pointer is NULL the first byte of the descriptor will get updated
986 * to reflect the actual length instead. If "min_len" is not equal to
987 * "max_len" then this function will try to retrive the beginning of
988 * the descriptor and base the maximum length on the first byte of the
994 *------------------------------------------------------------------------*/
996 usbd_req_get_desc(struct usb_device *udev,
997 struct mtx *mtx, uint16_t *actlen, void *desc,
998 uint16_t min_len, uint16_t max_len,
999 uint16_t id, uint8_t type, uint8_t index,
1002 struct usb_device_request req;
1003 uint8_t *buf = desc;
1006 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
1007 id, type, index, max_len);
1009 req.bmRequestType = UT_READ_DEVICE;
1010 req.bRequest = UR_GET_DESCRIPTOR;
1011 USETW2(req.wValue, type, index);
1012 USETW(req.wIndex, id);
1015 if ((min_len < 2) || (max_len < 2)) {
1016 err = USB_ERR_INVAL;
1019 USETW(req.wLength, min_len);
1021 err = usbd_do_request_flags(udev, mtx, &req,
1022 desc, 0, NULL, 1000 /* ms */);
1024 if (err != 0 && err != USB_ERR_TIMEOUT &&
1025 min_len != max_len) {
1026 /* clear descriptor data */
1027 memset(desc, 0, max_len);
1029 /* try to read full descriptor length */
1030 USETW(req.wLength, max_len);
1032 err = usbd_do_request_flags(udev, mtx, &req,
1033 desc, USB_SHORT_XFER_OK, NULL, 1000 /* ms */);
1037 if (buf[0] > max_len)
1039 else if (buf[0] < 2)
1040 err = USB_ERR_INVAL;
1044 /* enforce descriptor type */
1056 usb_pause_mtx(mtx, hz / 5);
1061 if (min_len == max_len) {
1062 /* enforce correct length */
1063 if ((buf[0] > min_len) && (actlen == NULL))
1066 /* enforce correct type */
1073 if (max_len > buf[0]) {
1076 /* zero minimum data */
1078 while (min_len > max_len) {
1083 /* set new minimum length */
1088 if (actlen != NULL) {
1097 /*------------------------------------------------------------------------*
1098 * usbd_req_get_string_any
1100 * This function will return the string given by "string_index"
1101 * using the first language ID. The maximum length "len" includes
1102 * the terminating zero. The "len" argument should be twice as
1103 * big pluss 2 bytes, compared with the actual maximum string length !
1108 *------------------------------------------------------------------------*/
1110 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1111 uint16_t len, uint8_t string_index)
1122 /* should not happen */
1123 return (USB_ERR_NORMAL_COMPLETION);
1125 if (string_index == 0) {
1126 /* this is the language table */
1128 return (USB_ERR_INVAL);
1130 if (udev->flags.no_strings) {
1132 return (USB_ERR_STALLED);
1134 err = usbd_req_get_string_desc
1135 (udev, mtx, buf, len, udev->langid, string_index);
1140 temp = (uint8_t *)buf;
1143 /* string length is too short */
1145 return (USB_ERR_INVAL);
1147 /* reserve one byte for terminating zero */
1150 /* find maximum length */
1152 n = (temp[0] / 2) - 1;
1156 /* skip descriptor header */
1159 /* reset swap state */
1162 /* convert and filter */
1163 for (i = 0; (i != n); i++) {
1164 c = UGETW(temp + (2 * i));
1166 /* convert from Unicode, handle buggy strings */
1167 if (((c & 0xff00) == 0) && (swap & 1)) {
1168 /* Little Endian, default */
1171 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1176 /* silently skip bad character */
1181 * Filter by default - We only allow alphanumerical
1182 * and a few more to avoid any problems with scripts
1199 /* silently skip bad character */
1201 *s = 0; /* zero terminate resulting string */
1202 return (USB_ERR_NORMAL_COMPLETION);
1205 /*------------------------------------------------------------------------*
1206 * usbd_req_get_string_desc
1208 * If you don't know the language ID, consider using
1209 * "usbd_req_get_string_any()".
1214 *------------------------------------------------------------------------*/
1216 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1217 uint16_t max_len, uint16_t lang_id,
1218 uint8_t string_index)
1220 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1221 UDESC_STRING, string_index, 0));
1224 /*------------------------------------------------------------------------*
1225 * usbd_req_get_config_desc_ptr
1227 * This function is used in device side mode to retrieve the pointer
1228 * to the generated config descriptor. This saves allocating space for
1229 * an additional config descriptor when setting the configuration.
1234 *------------------------------------------------------------------------*/
1236 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1237 struct usb_config_descriptor **ppcd, uint16_t wValue)
1239 struct usb_device_request req;
1240 usb_handle_req_t *hr_func;
1245 req.bmRequestType = UT_READ_DEVICE;
1246 req.bRequest = UR_GET_DESCRIPTOR;
1247 USETW(req.wValue, wValue);
1248 USETW(req.wIndex, 0);
1249 USETW(req.wLength, 0);
1254 hr_func = usbd_get_hr_func(udev);
1256 if (hr_func == NULL)
1257 err = USB_ERR_INVAL;
1259 USB_BUS_LOCK(udev->bus);
1260 err = (hr_func) (udev, &req, &ptr, &len);
1261 USB_BUS_UNLOCK(udev->bus);
1266 else if (ptr == NULL)
1267 err = USB_ERR_INVAL;
1269 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1274 /*------------------------------------------------------------------------*
1275 * usbd_req_get_config_desc
1280 *------------------------------------------------------------------------*/
1282 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1283 struct usb_config_descriptor *d, uint8_t conf_index)
1287 DPRINTFN(4, "confidx=%d\n", conf_index);
1289 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1290 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1294 /* Extra sanity checking */
1295 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1296 err = USB_ERR_INVAL;
1302 /*------------------------------------------------------------------------*
1303 * usbd_alloc_config_desc
1305 * This function is used to allocate a zeroed configuration
1311 *------------------------------------------------------------------------*/
1313 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1315 if (size > USB_CONFIG_MAX) {
1316 DPRINTF("Configuration descriptor too big\n");
1319 #if (USB_HAVE_FIXED_CONFIG == 0)
1320 return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1322 memset(udev->config_data, 0, sizeof(udev->config_data));
1323 return (udev->config_data);
1327 /*------------------------------------------------------------------------*
1328 * usbd_alloc_config_desc
1330 * This function is used to free a configuration descriptor.
1331 *------------------------------------------------------------------------*/
1333 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1335 #if (USB_HAVE_FIXED_CONFIG == 0)
1336 free(ptr, M_USBDEV);
1340 /*------------------------------------------------------------------------*
1341 * usbd_req_get_config_desc_full
1343 * This function gets the complete USB configuration descriptor and
1344 * ensures that "wTotalLength" is correct. The returned configuration
1345 * descriptor is freed by calling "usbd_free_config_desc()".
1350 *------------------------------------------------------------------------*/
1352 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1353 struct usb_config_descriptor **ppcd, uint8_t index)
1355 struct usb_config_descriptor cd;
1356 struct usb_config_descriptor *cdesc;
1360 DPRINTFN(4, "index=%d\n", index);
1364 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1368 /* get full descriptor */
1369 len = UGETW(cd.wTotalLength);
1370 if (len < (uint32_t)sizeof(*cdesc)) {
1371 /* corrupt descriptor */
1372 return (USB_ERR_INVAL);
1373 } else if (len > USB_CONFIG_MAX) {
1374 DPRINTF("Configuration descriptor was truncated\n");
1375 len = USB_CONFIG_MAX;
1377 cdesc = usbd_alloc_config_desc(udev, len);
1379 return (USB_ERR_NOMEM);
1380 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1381 UDESC_CONFIG, index, 3);
1383 usbd_free_config_desc(udev, cdesc);
1386 /* make sure that the device is not fooling us: */
1387 USETW(cdesc->wTotalLength, len);
1391 return (0); /* success */
1394 /*------------------------------------------------------------------------*
1395 * usbd_req_get_device_desc
1400 *------------------------------------------------------------------------*/
1402 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1403 struct usb_device_descriptor *d)
1406 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1407 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1410 /*------------------------------------------------------------------------*
1411 * usbd_req_get_alt_interface_no
1416 *------------------------------------------------------------------------*/
1418 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1419 uint8_t *alt_iface_no, uint8_t iface_index)
1421 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1422 struct usb_device_request req;
1424 if ((iface == NULL) || (iface->idesc == NULL))
1425 return (USB_ERR_INVAL);
1427 req.bmRequestType = UT_READ_INTERFACE;
1428 req.bRequest = UR_GET_INTERFACE;
1429 USETW(req.wValue, 0);
1430 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1432 USETW(req.wLength, 1);
1433 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1436 /*------------------------------------------------------------------------*
1437 * usbd_req_set_alt_interface_no
1442 *------------------------------------------------------------------------*/
1444 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1445 uint8_t iface_index, uint8_t alt_no)
1447 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1448 struct usb_device_request req;
1451 if ((iface == NULL) || (iface->idesc == NULL))
1452 return (USB_ERR_INVAL);
1454 req.bmRequestType = UT_WRITE_INTERFACE;
1455 req.bRequest = UR_SET_INTERFACE;
1456 req.wValue[0] = alt_no;
1458 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1460 USETW(req.wLength, 0);
1461 err = usbd_do_request(udev, mtx, &req, 0);
1462 if (err == USB_ERR_STALLED && iface->num_altsetting == 1) {
1464 * The USB specification chapter 9.4.10 says that USB
1465 * devices having only one alternate setting are
1466 * allowed to STALL this request. Ignore this failure.
1469 DPRINTF("Setting default alternate number failed. (ignored)\n");
1474 /*------------------------------------------------------------------------*
1475 * usbd_req_get_device_status
1480 *------------------------------------------------------------------------*/
1482 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1483 struct usb_status *st)
1485 struct usb_device_request req;
1487 req.bmRequestType = UT_READ_DEVICE;
1488 req.bRequest = UR_GET_STATUS;
1489 USETW(req.wValue, 0);
1490 USETW(req.wIndex, 0);
1491 USETW(req.wLength, sizeof(*st));
1492 return (usbd_do_request(udev, mtx, &req, st));
1495 /*------------------------------------------------------------------------*
1496 * usbd_req_get_hub_descriptor
1501 *------------------------------------------------------------------------*/
1503 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1504 struct usb_hub_descriptor *hd, uint8_t nports)
1506 struct usb_device_request req;
1507 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1509 req.bmRequestType = UT_READ_CLASS_DEVICE;
1510 req.bRequest = UR_GET_DESCRIPTOR;
1511 USETW2(req.wValue, UDESC_HUB, 0);
1512 USETW(req.wIndex, 0);
1513 USETW(req.wLength, len);
1514 return (usbd_do_request(udev, mtx, &req, hd));
1517 /*------------------------------------------------------------------------*
1518 * usbd_req_get_ss_hub_descriptor
1523 *------------------------------------------------------------------------*/
1525 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1526 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1528 struct usb_device_request req;
1529 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1531 req.bmRequestType = UT_READ_CLASS_DEVICE;
1532 req.bRequest = UR_GET_DESCRIPTOR;
1533 USETW2(req.wValue, UDESC_SS_HUB, 0);
1534 USETW(req.wIndex, 0);
1535 USETW(req.wLength, len);
1536 return (usbd_do_request(udev, mtx, &req, hd));
1539 /*------------------------------------------------------------------------*
1540 * usbd_req_get_hub_status
1545 *------------------------------------------------------------------------*/
1547 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1548 struct usb_hub_status *st)
1550 struct usb_device_request req;
1552 req.bmRequestType = UT_READ_CLASS_DEVICE;
1553 req.bRequest = UR_GET_STATUS;
1554 USETW(req.wValue, 0);
1555 USETW(req.wIndex, 0);
1556 USETW(req.wLength, sizeof(struct usb_hub_status));
1557 return (usbd_do_request(udev, mtx, &req, st));
1560 /*------------------------------------------------------------------------*
1561 * usbd_req_set_address
1563 * This function is used to set the address for an USB device. After
1564 * port reset the USB device will respond at address zero.
1569 *------------------------------------------------------------------------*/
1571 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1573 struct usb_device_request req;
1576 DPRINTFN(6, "setting device address=%d\n", addr);
1578 req.bmRequestType = UT_WRITE_DEVICE;
1579 req.bRequest = UR_SET_ADDRESS;
1580 USETW(req.wValue, addr);
1581 USETW(req.wIndex, 0);
1582 USETW(req.wLength, 0);
1584 err = USB_ERR_INVAL;
1586 /* check if USB controller handles set address */
1587 if (udev->bus->methods->set_address != NULL)
1588 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1590 if (err != USB_ERR_INVAL)
1593 /* Setting the address should not take more than 1 second ! */
1594 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1595 USB_DELAY_STATUS_STAGE, NULL, 1000);
1598 /* allow device time to set new address */
1600 USB_MS_TO_TICKS(usb_set_address_settle));
1605 /*------------------------------------------------------------------------*
1606 * usbd_req_get_port_status
1611 *------------------------------------------------------------------------*/
1613 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1614 struct usb_port_status *ps, uint8_t port)
1616 struct usb_device_request req;
1618 req.bmRequestType = UT_READ_CLASS_OTHER;
1619 req.bRequest = UR_GET_STATUS;
1620 USETW(req.wValue, 0);
1621 req.wIndex[0] = port;
1623 USETW(req.wLength, sizeof(*ps));
1625 return (usbd_do_request_flags(udev, mtx, &req, ps, 0, NULL, 1000));
1628 /*------------------------------------------------------------------------*
1629 * usbd_req_clear_hub_feature
1634 *------------------------------------------------------------------------*/
1636 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1639 struct usb_device_request req;
1641 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1642 req.bRequest = UR_CLEAR_FEATURE;
1643 USETW(req.wValue, sel);
1644 USETW(req.wIndex, 0);
1645 USETW(req.wLength, 0);
1646 return (usbd_do_request(udev, mtx, &req, 0));
1649 /*------------------------------------------------------------------------*
1650 * usbd_req_set_hub_feature
1655 *------------------------------------------------------------------------*/
1657 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1660 struct usb_device_request req;
1662 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1663 req.bRequest = UR_SET_FEATURE;
1664 USETW(req.wValue, sel);
1665 USETW(req.wIndex, 0);
1666 USETW(req.wLength, 0);
1667 return (usbd_do_request(udev, mtx, &req, 0));
1670 /*------------------------------------------------------------------------*
1671 * usbd_req_set_hub_u1_timeout
1676 *------------------------------------------------------------------------*/
1678 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1679 uint8_t port, uint8_t timeout)
1681 struct usb_device_request req;
1683 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1684 req.bRequest = UR_SET_FEATURE;
1685 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1686 req.wIndex[0] = port;
1687 req.wIndex[1] = timeout;
1688 USETW(req.wLength, 0);
1689 return (usbd_do_request(udev, mtx, &req, 0));
1692 /*------------------------------------------------------------------------*
1693 * usbd_req_set_hub_u2_timeout
1698 *------------------------------------------------------------------------*/
1700 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1701 uint8_t port, uint8_t timeout)
1703 struct usb_device_request req;
1705 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1706 req.bRequest = UR_SET_FEATURE;
1707 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1708 req.wIndex[0] = port;
1709 req.wIndex[1] = timeout;
1710 USETW(req.wLength, 0);
1711 return (usbd_do_request(udev, mtx, &req, 0));
1714 /*------------------------------------------------------------------------*
1715 * usbd_req_set_hub_depth
1720 *------------------------------------------------------------------------*/
1722 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1725 struct usb_device_request req;
1727 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1728 req.bRequest = UR_SET_HUB_DEPTH;
1729 USETW(req.wValue, depth);
1730 USETW(req.wIndex, 0);
1731 USETW(req.wLength, 0);
1732 return (usbd_do_request(udev, mtx, &req, 0));
1735 /*------------------------------------------------------------------------*
1736 * usbd_req_clear_port_feature
1741 *------------------------------------------------------------------------*/
1743 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1744 uint8_t port, uint16_t sel)
1746 struct usb_device_request req;
1748 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1749 req.bRequest = UR_CLEAR_FEATURE;
1750 USETW(req.wValue, sel);
1751 req.wIndex[0] = port;
1753 USETW(req.wLength, 0);
1754 return (usbd_do_request(udev, mtx, &req, 0));
1757 /*------------------------------------------------------------------------*
1758 * usbd_req_set_port_feature
1763 *------------------------------------------------------------------------*/
1765 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1766 uint8_t port, uint16_t sel)
1768 struct usb_device_request req;
1770 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1771 req.bRequest = UR_SET_FEATURE;
1772 USETW(req.wValue, sel);
1773 req.wIndex[0] = port;
1775 USETW(req.wLength, 0);
1776 return (usbd_do_request(udev, mtx, &req, 0));
1779 /*------------------------------------------------------------------------*
1780 * usbd_req_set_protocol
1785 *------------------------------------------------------------------------*/
1787 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1788 uint8_t iface_index, uint16_t report)
1790 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1791 struct usb_device_request req;
1793 if ((iface == NULL) || (iface->idesc == NULL)) {
1794 return (USB_ERR_INVAL);
1796 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1797 iface, report, iface->idesc->bInterfaceNumber);
1799 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1800 req.bRequest = UR_SET_PROTOCOL;
1801 USETW(req.wValue, report);
1802 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1804 USETW(req.wLength, 0);
1805 return (usbd_do_request(udev, mtx, &req, 0));
1808 /*------------------------------------------------------------------------*
1809 * usbd_req_set_report
1814 *------------------------------------------------------------------------*/
1816 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1817 uint8_t iface_index, uint8_t type, uint8_t id)
1819 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1820 struct usb_device_request req;
1822 if ((iface == NULL) || (iface->idesc == NULL)) {
1823 return (USB_ERR_INVAL);
1825 DPRINTFN(5, "len=%d\n", len);
1827 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1828 req.bRequest = UR_SET_REPORT;
1829 USETW2(req.wValue, type, id);
1830 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1832 USETW(req.wLength, len);
1833 return (usbd_do_request(udev, mtx, &req, data));
1836 /*------------------------------------------------------------------------*
1837 * usbd_req_get_report
1842 *------------------------------------------------------------------------*/
1844 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1845 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1847 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1848 struct usb_device_request req;
1850 if ((iface == NULL) || (iface->idesc == NULL)) {
1851 return (USB_ERR_INVAL);
1853 DPRINTFN(5, "len=%d\n", len);
1855 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1856 req.bRequest = UR_GET_REPORT;
1857 USETW2(req.wValue, type, id);
1858 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1860 USETW(req.wLength, len);
1861 return (usbd_do_request(udev, mtx, &req, data));
1864 /*------------------------------------------------------------------------*
1870 *------------------------------------------------------------------------*/
1872 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1873 uint8_t iface_index, uint8_t duration, uint8_t id)
1875 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1876 struct usb_device_request req;
1878 if ((iface == NULL) || (iface->idesc == NULL)) {
1879 return (USB_ERR_INVAL);
1881 DPRINTFN(5, "%d %d\n", duration, id);
1883 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1884 req.bRequest = UR_SET_IDLE;
1885 USETW2(req.wValue, duration, id);
1886 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1888 USETW(req.wLength, 0);
1889 return (usbd_do_request(udev, mtx, &req, 0));
1892 /*------------------------------------------------------------------------*
1893 * usbd_req_get_report_descriptor
1898 *------------------------------------------------------------------------*/
1900 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1901 void *d, uint16_t size, uint8_t iface_index)
1903 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1904 struct usb_device_request req;
1906 if ((iface == NULL) || (iface->idesc == NULL)) {
1907 return (USB_ERR_INVAL);
1909 req.bmRequestType = UT_READ_INTERFACE;
1910 req.bRequest = UR_GET_DESCRIPTOR;
1911 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1912 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1914 USETW(req.wLength, size);
1915 return (usbd_do_request(udev, mtx, &req, d));
1918 /*------------------------------------------------------------------------*
1919 * usbd_req_set_config
1921 * This function is used to select the current configuration number in
1922 * both USB device side mode and USB host side mode. When setting the
1923 * configuration the function of the interfaces can change.
1928 *------------------------------------------------------------------------*/
1930 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1932 struct usb_device_request req;
1934 DPRINTF("setting config %d\n", conf);
1936 /* do "set configuration" request */
1938 req.bmRequestType = UT_WRITE_DEVICE;
1939 req.bRequest = UR_SET_CONFIG;
1940 req.wValue[0] = conf;
1942 USETW(req.wIndex, 0);
1943 USETW(req.wLength, 0);
1944 return (usbd_do_request(udev, mtx, &req, 0));
1947 /*------------------------------------------------------------------------*
1948 * usbd_req_get_config
1953 *------------------------------------------------------------------------*/
1955 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1957 struct usb_device_request req;
1959 req.bmRequestType = UT_READ_DEVICE;
1960 req.bRequest = UR_GET_CONFIG;
1961 USETW(req.wValue, 0);
1962 USETW(req.wIndex, 0);
1963 USETW(req.wLength, 1);
1964 return (usbd_do_request(udev, mtx, &req, pconf));
1967 /*------------------------------------------------------------------------*
1968 * usbd_setup_device_desc
1969 *------------------------------------------------------------------------*/
1971 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1976 * Get the first 8 bytes of the device descriptor !
1978 * NOTE: "usbd_do_request()" will check the device descriptor
1979 * next time we do a request to see if the maximum packet size
1980 * changed! The 8 first bytes of the device descriptor
1981 * contains the maximum packet size to use on control endpoint
1982 * 0. If this value is different from "USB_MAX_IPACKET" a new
1983 * USB control request will be setup!
1985 switch (udev->speed) {
1986 case USB_SPEED_FULL:
1987 if (usb_full_ddesc != 0) {
1988 /* get full device descriptor */
1989 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1994 /* get partial device descriptor, some devices crash on this */
1995 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1996 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1998 DPRINTF("Trying fallback for getting the USB device descriptor\n");
1999 /* try 8 bytes bMaxPacketSize */
2000 udev->ddesc.bMaxPacketSize = 8;
2001 /* get full device descriptor */
2002 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2005 /* try 16 bytes bMaxPacketSize */
2006 udev->ddesc.bMaxPacketSize = 16;
2007 /* get full device descriptor */
2008 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2011 /* try 32/64 bytes bMaxPacketSize */
2012 udev->ddesc.bMaxPacketSize = 32;
2014 /* get the full device descriptor */
2015 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2019 DPRINTF("Minimum bMaxPacketSize is large enough "
2020 "to hold the complete device descriptor or "
2021 "only one bMaxPacketSize choice\n");
2023 /* get the full device descriptor */
2024 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2026 /* try one more time, if error */
2028 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
2033 DPRINTFN(0, "getting device descriptor "
2034 "at addr %d failed, %s\n", udev->address,
2039 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
2040 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
2041 udev->address, UGETW(udev->ddesc.bcdUSB),
2042 udev->ddesc.bDeviceClass,
2043 udev->ddesc.bDeviceSubClass,
2044 udev->ddesc.bDeviceProtocol,
2045 udev->ddesc.bMaxPacketSize,
2046 udev->ddesc.bLength,
2052 /*------------------------------------------------------------------------*
2053 * usbd_req_re_enumerate
2055 * NOTE: After this function returns the hardware is in the
2056 * unconfigured state! The application is responsible for setting a
2057 * new configuration.
2062 *------------------------------------------------------------------------*/
2064 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2066 struct usb_device *parent_hub;
2069 uint8_t do_retry = 1;
2071 if (udev->flags.usb_mode != USB_MODE_HOST) {
2072 return (USB_ERR_INVAL);
2074 old_addr = udev->address;
2075 parent_hub = udev->parent_hub;
2076 if (parent_hub == NULL) {
2077 return (USB_ERR_INVAL);
2080 #if USB_HAVE_TT_SUPPORT
2082 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2083 * speed device, if any.
2085 if (udev->parent_hs_hub != NULL &&
2086 udev->speed != USB_SPEED_HIGH) {
2087 DPRINTF("Trying to reset parent High Speed TT.\n");
2088 if (udev->parent_hs_hub == parent_hub &&
2089 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2090 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2091 /* we can reset the whole TT */
2092 err = usbd_req_reset_tt(parent_hub, NULL,
2095 /* only reset a particular device and endpoint */
2096 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2097 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2100 DPRINTF("Resetting parent High "
2101 "Speed TT failed (%s).\n",
2106 /* Try to warm reset first */
2107 if (parent_hub->speed == USB_SPEED_SUPER)
2108 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2110 /* Try to reset the parent HUB port. */
2111 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2113 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2114 old_addr, usbd_errstr(err));
2119 * After that the port has been reset our device should be at
2122 udev->address = USB_START_ADDR;
2124 /* reset "bMaxPacketSize" */
2125 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2127 /* reset USB state */
2128 usb_set_device_state(udev, USB_STATE_POWERED);
2131 * Restore device address:
2133 err = usbd_req_set_address(udev, mtx, old_addr);
2135 /* XXX ignore any errors! */
2136 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2137 old_addr, usbd_errstr(err));
2140 * Restore device address, if the controller driver did not
2143 if (udev->address == USB_START_ADDR)
2144 udev->address = old_addr;
2146 /* setup the device descriptor and the initial "wMaxPacketSize" */
2147 err = usbd_setup_device_desc(udev, mtx);
2150 if (err && do_retry) {
2151 /* give the USB firmware some time to load */
2152 usb_pause_mtx(mtx, hz / 2);
2153 /* no more retries after this retry */
2158 /* restore address */
2159 if (udev->address == USB_START_ADDR)
2160 udev->address = old_addr;
2161 /* update state, if successful */
2163 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2167 /*------------------------------------------------------------------------*
2168 * usbd_req_clear_device_feature
2173 *------------------------------------------------------------------------*/
2175 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2178 struct usb_device_request req;
2180 req.bmRequestType = UT_WRITE_DEVICE;
2181 req.bRequest = UR_CLEAR_FEATURE;
2182 USETW(req.wValue, sel);
2183 USETW(req.wIndex, 0);
2184 USETW(req.wLength, 0);
2185 return (usbd_do_request(udev, mtx, &req, 0));
2188 /*------------------------------------------------------------------------*
2189 * usbd_req_set_device_feature
2194 *------------------------------------------------------------------------*/
2196 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2199 struct usb_device_request req;
2201 req.bmRequestType = UT_WRITE_DEVICE;
2202 req.bRequest = UR_SET_FEATURE;
2203 USETW(req.wValue, sel);
2204 USETW(req.wIndex, 0);
2205 USETW(req.wLength, 0);
2206 return (usbd_do_request(udev, mtx, &req, 0));
2209 /*------------------------------------------------------------------------*
2215 *------------------------------------------------------------------------*/
2217 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2220 struct usb_device_request req;
2222 /* For single TT HUBs the port should be 1 */
2224 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2225 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2228 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2229 req.bRequest = UR_RESET_TT;
2230 USETW(req.wValue, 0);
2231 req.wIndex[0] = port;
2233 USETW(req.wLength, 0);
2234 return (usbd_do_request(udev, mtx, &req, 0));
2237 /*------------------------------------------------------------------------*
2238 * usbd_req_clear_tt_buffer
2240 * For single TT HUBs the port should be 1.
2245 *------------------------------------------------------------------------*/
2247 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2248 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2250 struct usb_device_request req;
2253 /* For single TT HUBs the port should be 1 */
2255 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2256 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2259 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2260 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2262 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2263 req.bRequest = UR_CLEAR_TT_BUFFER;
2264 USETW(req.wValue, wValue);
2265 req.wIndex[0] = port;
2267 USETW(req.wLength, 0);
2268 return (usbd_do_request(udev, mtx, &req, 0));
2271 /*------------------------------------------------------------------------*
2272 * usbd_req_set_port_link_state
2274 * USB 3.0 specific request
2279 *------------------------------------------------------------------------*/
2281 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2282 uint8_t port, uint8_t link_state)
2284 struct usb_device_request req;
2286 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2287 req.bRequest = UR_SET_FEATURE;
2288 USETW(req.wValue, UHF_PORT_LINK_STATE);
2289 req.wIndex[0] = port;
2290 req.wIndex[1] = link_state;
2291 USETW(req.wLength, 0);
2292 return (usbd_do_request(udev, mtx, &req, 0));
2295 /*------------------------------------------------------------------------*
2296 * usbd_req_set_lpm_info
2298 * USB 2.0 specific request for Link Power Management.
2302 * USB_ERR_PENDING_REQUESTS: NYET
2303 * USB_ERR_TIMEOUT: TIMEOUT
2304 * USB_ERR_STALL: STALL
2306 *------------------------------------------------------------------------*/
2308 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2309 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2311 struct usb_device_request req;
2315 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2316 req.bRequest = UR_SET_AND_TEST;
2317 USETW(req.wValue, UHF_PORT_L1);
2318 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2319 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2320 USETW(req.wLength, sizeof(buf));
2322 /* set default value in case of short transfer */
2325 err = usbd_do_request(udev, mtx, &req, buf);
2330 case 0x00: /* SUCCESS */
2332 case 0x10: /* NYET */
2333 err = USB_ERR_PENDING_REQUESTS;
2335 case 0x11: /* TIMEOUT */
2336 err = USB_ERR_TIMEOUT;
2338 case 0x30: /* STALL */
2339 err = USB_ERR_STALLED;
2341 default: /* reserved */
2342 err = USB_ERR_IOERROR;