3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usb_ioctl.h>
52 #include <dev/usb/usbhid.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_request.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_transfer.h>
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_util.h>
64 #include <dev/usb/usb_dynamic.h>
66 #include <dev/usb/usb_controller.h>
67 #include <dev/usb/usb_bus.h>
68 #include <sys/ctype.h>
70 static int usb_no_cs_fail;
72 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
73 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
76 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
77 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
79 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
80 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
81 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
82 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
85 /* The following structures are used in connection to fault injection. */
86 struct usb_ctrl_debug {
87 int bus_index; /* target bus */
88 int dev_index; /* target address */
89 int ds_fail; /* fail data stage */
90 int ss_fail; /* fail data stage */
91 int ds_delay; /* data stage delay in ms */
92 int ss_delay; /* status stage delay in ms */
93 int bmRequestType_value;
97 struct usb_ctrl_debug_bits {
105 /* The default is to disable fault injection. */
107 static struct usb_ctrl_debug usb_ctrl_debug = {
110 .bmRequestType_value = -1,
111 .bRequest_value = -1,
114 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
115 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
117 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
119 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
120 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
121 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
122 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
123 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
124 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
125 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
126 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
127 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
128 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
129 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
131 /*------------------------------------------------------------------------*
132 * usbd_get_debug_bits
134 * This function is only useful in USB host mode.
135 *------------------------------------------------------------------------*/
137 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
138 struct usb_ctrl_debug_bits *dbg)
142 memset(dbg, 0, sizeof(*dbg));
144 /* Compute data stage delay */
146 temp = usb_ctrl_debug.ds_delay;
149 else if (temp > (16*1024))
152 dbg->ds_delay = temp;
154 /* Compute status stage delay */
156 temp = usb_ctrl_debug.ss_delay;
159 else if (temp > (16*1024))
162 dbg->ss_delay = temp;
164 /* Check if this control request should be failed */
166 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
169 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
172 temp = usb_ctrl_debug.bmRequestType_value;
174 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
177 temp = usb_ctrl_debug.bRequest_value;
179 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
182 temp = usb_ctrl_debug.ds_fail;
186 temp = usb_ctrl_debug.ss_fail;
192 #endif /* USB_REQ_DEBUG */
193 #endif /* USB_DEBUG */
195 /*------------------------------------------------------------------------*
196 * usbd_do_request_callback
198 * This function is the USB callback for generic USB Host control
200 *------------------------------------------------------------------------*/
202 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
204 ; /* workaround for a bug in "indent" */
206 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
208 switch (USB_GET_STATE(xfer)) {
210 usbd_transfer_submit(xfer);
213 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
218 /*------------------------------------------------------------------------*
219 * usb_do_clear_stall_callback
221 * This function is the USB callback for generic clear stall requests.
222 *------------------------------------------------------------------------*/
224 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
226 struct usb_device_request req;
227 struct usb_device *udev;
228 struct usb_endpoint *ep;
229 struct usb_endpoint *ep_end;
230 struct usb_endpoint *ep_first;
233 udev = xfer->xroot->udev;
235 USB_BUS_LOCK(udev->bus);
237 /* round robin endpoint clear stall */
240 ep_end = udev->endpoints + udev->endpoints_max;
241 ep_first = udev->endpoints;
242 to = udev->endpoints_max;
244 switch (USB_GET_STATE(xfer)) {
245 case USB_ST_TRANSFERRED:
247 /* reset error counter */
248 udev->clear_stall_errors = 0;
251 goto tr_setup; /* device was unconfigured */
256 /* some hardware needs a callback to clear the data toggle */
257 usbd_clear_stall_locked(udev, ep);
258 /* start up the current or next transfer, if any */
259 usb_command_wrapper(&ep->endpoint_q,
260 ep->endpoint_q.curr);
267 break; /* no endpoints - nothing to do */
268 if ((ep < ep_first) || (ep >= ep_end))
269 ep = ep_first; /* endpoint wrapped around */
273 /* setup a clear-stall packet */
275 req.bmRequestType = UT_WRITE_ENDPOINT;
276 req.bRequest = UR_CLEAR_FEATURE;
277 USETW(req.wValue, UF_ENDPOINT_HALT);
278 req.wIndex[0] = ep->edesc->bEndpointAddress;
280 USETW(req.wLength, 0);
282 /* copy in the transfer */
284 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
287 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
289 USB_BUS_UNLOCK(udev->bus);
291 usbd_transfer_submit(xfer);
293 USB_BUS_LOCK(udev->bus);
301 if (error == USB_ERR_CANCELLED)
304 DPRINTF("Clear stall failed.\n");
307 * Some VMs like VirtualBox always return failure on
308 * clear-stall which we sometimes should just ignore.
312 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
315 if (error == USB_ERR_TIMEOUT) {
316 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
317 DPRINTF("Trying to re-enumerate.\n");
318 usbd_start_re_enumerate(udev);
320 udev->clear_stall_errors++;
321 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
322 DPRINTF("Trying to re-enumerate.\n");
323 usbd_start_re_enumerate(udev);
329 /* store current endpoint */
331 USB_BUS_UNLOCK(udev->bus);
334 static usb_handle_req_t *
335 usbd_get_hr_func(struct usb_device *udev)
337 /* figure out if there is a Handle Request function */
338 if (udev->flags.usb_mode == USB_MODE_DEVICE)
339 return (usb_temp_get_desc_p);
340 else if (udev->parent_hub == NULL)
341 return (udev->bus->methods->roothub_exec);
346 /*------------------------------------------------------------------------*
347 * usbd_do_request_flags and usbd_do_request
349 * Description of arguments passed to these functions:
351 * "udev" - this is the "usb_device" structure pointer on which the
352 * request should be performed. It is possible to call this function
353 * in both Host Side mode and Device Side mode.
355 * "mtx" - if this argument is non-NULL the mutex pointed to by it
356 * will get dropped and picked up during the execution of this
357 * function, hence this function sometimes needs to sleep. If this
358 * argument is NULL it has no effect.
360 * "req" - this argument must always be non-NULL and points to an
361 * 8-byte structure holding the USB request to be done. The USB
362 * request structure has a bit telling the direction of the USB
363 * request, if it is a read or a write.
365 * "data" - if the "wLength" part of the structure pointed to by "req"
366 * is non-zero this argument must point to a valid kernel buffer which
367 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
370 * "flags" - here is a list of valid flags:
372 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
375 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
376 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
377 * sysctl. This flag is mostly useful for debugging.
379 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
382 * "actlen" - if non-NULL the actual transfer length will be stored in
383 * the 16-bit unsigned integer pointed to by "actlen". This
384 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
387 * "timeout" - gives the timeout for the control transfer in
388 * milliseconds. A "timeout" value less than 50 milliseconds is
389 * treated like a 50 millisecond timeout. A "timeout" value greater
390 * than 30 seconds is treated like a 30 second timeout. This USB stack
391 * does not allow control requests without a timeout.
393 * NOTE: This function is thread safe. All calls to
394 * "usbd_do_request_flags" will be serialised by the use of an
395 * internal "sx_lock".
400 *------------------------------------------------------------------------*/
402 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
403 struct usb_device_request *req, void *data, uint16_t flags,
404 uint16_t *actlen, usb_timeout_t timeout)
407 struct usb_ctrl_debug_bits dbg;
409 usb_handle_req_t *hr_func;
410 struct usb_xfer *xfer;
413 usb_ticks_t start_ticks;
414 usb_ticks_t delta_ticks;
415 usb_ticks_t max_ticks;
422 /* timeout is too small */
425 if (timeout > 30000) {
426 /* timeout is too big */
429 length = UGETW(req->wLength);
431 enum_locked = usbd_enum_is_locked(udev);
433 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
434 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
435 udev, req->bmRequestType, req->bRequest,
436 req->wValue[1], req->wValue[0],
437 req->wIndex[1], req->wIndex[0],
438 req->wLength[1], req->wLength[0]);
440 /* Check if the device is still alive */
441 if (udev->state < USB_STATE_POWERED) {
442 DPRINTF("usb device has gone\n");
443 return (USB_ERR_NOT_CONFIGURED);
447 * Set "actlen" to a known value in case the caller does not
448 * check the return value:
453 #if (USB_HAVE_USER_IO == 0)
454 if (flags & USB_USER_DATA_PTR)
455 return (USB_ERR_INVAL);
457 if ((mtx != NULL) && (mtx != &Giant)) {
459 mtx_assert(mtx, MA_NOTOWNED);
463 * We need to allow suspend and resume at this point, else the
464 * control transfer will timeout if the device is suspended!
467 usbd_sr_unlock(udev);
470 * Grab the default sx-lock so that serialisation
471 * is achieved when multiple threads are involved:
473 sx_xlock(&udev->ctrl_sx);
475 hr_func = usbd_get_hr_func(udev);
477 if (hr_func != NULL) {
478 DPRINTF("Handle Request function is set\n");
483 if (!(req->bmRequestType & UT_READ)) {
485 DPRINTFN(1, "The handle request function "
486 "does not support writing data!\n");
492 /* The root HUB code needs the BUS lock locked */
494 USB_BUS_LOCK(udev->bus);
495 err = (hr_func) (udev, req, &desc, &temp);
496 USB_BUS_UNLOCK(udev->bus);
502 if (!(flags & USB_SHORT_XFER_OK)) {
503 err = USB_ERR_SHORT_XFER;
513 if (flags & USB_USER_DATA_PTR) {
514 if (copyout(desc, data, length)) {
520 memcpy(data, desc, length);
522 goto done; /* success */
526 * Setup a new USB transfer or use the existing one, if any:
528 usbd_ctrl_transfer_setup(udev);
530 xfer = udev->ctrl_xfer[0];
532 /* most likely out of memory */
539 usbd_get_debug_bits(udev, req, &dbg);
541 /* Check for fault injection */
543 flags |= USB_DELAY_STATUS_STAGE;
547 if (flags & USB_DELAY_STATUS_STAGE)
548 xfer->flags.manual_status = 1;
550 xfer->flags.manual_status = 0;
552 if (flags & USB_SHORT_XFER_OK)
553 xfer->flags.short_xfer_ok = 1;
555 xfer->flags.short_xfer_ok = 0;
557 xfer->timeout = timeout;
561 max_ticks = USB_MS_TO_TICKS(timeout);
563 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
565 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
569 if (temp > usbd_xfer_max_len(xfer)) {
570 temp = usbd_xfer_max_len(xfer);
573 if (xfer->flags.manual_status) {
574 if (usbd_xfer_frame_len(xfer, 0) != 0) {
575 /* Execute data stage separately */
577 } else if (temp > 0) {
582 if (dbg.ds_delay > 0) {
584 xfer->xroot->xfer_mtx,
585 USB_MS_TO_TICKS(dbg.ds_delay));
586 /* make sure we don't time out */
592 usbd_xfer_set_frame_len(xfer, 1, temp);
595 if (!(req->bmRequestType & UT_READ)) {
597 if (flags & USB_USER_DATA_PTR) {
598 USB_XFER_UNLOCK(xfer);
599 err = usbd_copy_in_user(xfer->frbuffers + 1,
608 usbd_copy_in(xfer->frbuffers + 1,
611 usbd_xfer_set_frames(xfer, 2);
613 if (usbd_xfer_frame_len(xfer, 0) == 0) {
614 if (xfer->flags.manual_status) {
620 if (dbg.ss_delay > 0) {
622 xfer->xroot->xfer_mtx,
623 USB_MS_TO_TICKS(dbg.ss_delay));
624 /* make sure we don't time out */
628 xfer->flags.manual_status = 0;
633 usbd_xfer_set_frames(xfer, 1);
636 usbd_transfer_start(xfer);
638 while (usbd_transfer_pending(xfer)) {
639 cv_wait(&udev->ctrlreq_cv,
640 xfer->xroot->xfer_mtx);
649 /* get actual length of DATA stage */
651 if (xfer->aframes < 2) {
654 acttemp = usbd_xfer_frame_len(xfer, 1);
657 /* check for short packet */
659 if (temp > acttemp) {
664 if (req->bmRequestType & UT_READ) {
666 if (flags & USB_USER_DATA_PTR) {
667 USB_XFER_UNLOCK(xfer);
668 err = usbd_copy_out_user(xfer->frbuffers + 1,
677 usbd_copy_out(xfer->frbuffers + 1,
682 * Clear "frlengths[0]" so that we don't send the setup
685 usbd_xfer_set_frame_len(xfer, 0, 0);
687 /* update length and data pointer */
689 data = USB_ADD_BYTES(data, temp);
694 /* check for timeout */
696 delta_ticks = ticks - start_ticks;
697 if (delta_ticks > max_ticks) {
699 err = USB_ERR_TIMEOUT;
709 * Make sure that the control endpoint is no longer
710 * blocked in case of a non-transfer related error:
712 usbd_transfer_stop(xfer);
714 USB_XFER_UNLOCK(xfer);
717 sx_xunlock(&udev->ctrl_sx);
722 if ((mtx != NULL) && (mtx != &Giant))
725 return ((usb_error_t)err);
728 /*------------------------------------------------------------------------*
729 * usbd_do_request_proc - factored out code
731 * This function is factored out code. It does basically the same like
732 * usbd_do_request_flags, except it will check the status of the
733 * passed process argument before doing the USB request. If the
734 * process is draining the USB_ERR_IOERROR code will be returned. It
735 * is assumed that the mutex associated with the process is locked
736 * when calling this function.
737 *------------------------------------------------------------------------*/
739 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
740 struct usb_device_request *req, void *data, uint16_t flags,
741 uint16_t *actlen, usb_timeout_t timeout)
746 /* get request data length */
747 len = UGETW(req->wLength);
749 /* check if the device is being detached */
750 if (usb_proc_is_gone(pproc)) {
751 err = USB_ERR_IOERROR;
755 /* forward the USB request */
756 err = usbd_do_request_flags(udev, pproc->up_mtx,
757 req, data, flags, actlen, timeout);
760 /* on failure we zero the data */
761 /* on short packet we zero the unused data */
762 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
764 memset(data, 0, len);
765 else if (actlen && *actlen != len)
766 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
771 /*------------------------------------------------------------------------*
772 * usbd_req_reset_port
774 * This function will instruct a USB HUB to perform a reset sequence
775 * on the specified port number.
778 * 0: Success. The USB device should now be at address zero.
779 * Else: Failure. No USB device is present and the USB port should be
781 *------------------------------------------------------------------------*/
783 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
785 struct usb_port_status ps;
792 uint16_t pr_poll_delay;
793 uint16_t pr_recovery_delay;
799 /* clear any leftover port reset changes first */
800 usbd_req_clear_port_feature(
801 udev, mtx, port, UHF_C_PORT_RESET);
803 /* assert port reset on the given port */
804 err = usbd_req_set_port_feature(
805 udev, mtx, port, UHF_PORT_RESET);
807 /* check for errors */
811 /* range check input parameters */
812 pr_poll_delay = usb_pr_poll_delay;
813 if (pr_poll_delay < 1) {
815 } else if (pr_poll_delay > 1000) {
816 pr_poll_delay = 1000;
818 pr_recovery_delay = usb_pr_recovery_delay;
819 if (pr_recovery_delay > 1000) {
820 pr_recovery_delay = 1000;
826 /* wait for the device to recover from reset */
827 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
830 /* wait for the device to recover from reset */
831 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
832 n += USB_PORT_RESET_DELAY;
834 err = usbd_req_get_port_status(udev, mtx, &ps, port);
838 status = UGETW(ps.wPortStatus);
839 change = UGETW(ps.wPortChange);
841 /* if the device disappeared, just give up */
842 if (!(status & UPS_CURRENT_CONNECT_STATUS))
845 /* check if reset is complete */
846 if (change & UPS_C_PORT_RESET)
850 * Some Virtual Machines like VirtualBox 4.x fail to
851 * generate a port reset change event. Check if reset
852 * is no longer asserted.
854 if (!(status & UPS_RESET))
857 /* check for timeout */
864 /* clear port reset first */
865 err = usbd_req_clear_port_feature(
866 udev, mtx, port, UHF_C_PORT_RESET);
870 /* check for timeout */
872 err = USB_ERR_TIMEOUT;
876 /* wait for the device to recover from reset */
877 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
879 /* wait for the device to recover from reset */
880 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
884 DPRINTFN(2, "port %d reset returning error=%s\n",
885 port, usbd_errstr(err));
889 /*------------------------------------------------------------------------*
890 * usbd_req_warm_reset_port
892 * This function will instruct an USB HUB to perform a warm reset
893 * sequence on the specified port number. This kind of reset is not
894 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
895 * for SUPER-speed USB HUBs.
898 * 0: Success. The USB device should now be available again.
899 * Else: Failure. No USB device is present and the USB port should be
901 *------------------------------------------------------------------------*/
903 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
906 struct usb_port_status ps;
913 uint16_t pr_poll_delay;
914 uint16_t pr_recovery_delay;
920 err = usbd_req_get_port_status(udev, mtx, &ps, port);
924 status = UGETW(ps.wPortStatus);
926 switch (UPS_PORT_LINK_STATE_GET(status)) {
928 case UPS_PORT_LS_COMP_MODE:
929 case UPS_PORT_LS_LOOPBACK:
930 case UPS_PORT_LS_SS_INA:
933 DPRINTF("Wrong state for warm reset\n");
937 /* clear any leftover warm port reset changes first */
938 usbd_req_clear_port_feature(udev, mtx,
939 port, UHF_C_BH_PORT_RESET);
941 /* set warm port reset */
942 err = usbd_req_set_port_feature(udev, mtx,
943 port, UHF_BH_PORT_RESET);
948 /* range check input parameters */
949 pr_poll_delay = usb_pr_poll_delay;
950 if (pr_poll_delay < 1) {
952 } else if (pr_poll_delay > 1000) {
953 pr_poll_delay = 1000;
955 pr_recovery_delay = usb_pr_recovery_delay;
956 if (pr_recovery_delay > 1000) {
957 pr_recovery_delay = 1000;
963 /* wait for the device to recover from reset */
964 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
967 /* wait for the device to recover from reset */
968 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
969 n += USB_PORT_RESET_DELAY;
971 err = usbd_req_get_port_status(udev, mtx, &ps, port);
975 status = UGETW(ps.wPortStatus);
976 change = UGETW(ps.wPortChange);
978 /* if the device disappeared, just give up */
979 if (!(status & UPS_CURRENT_CONNECT_STATUS))
982 /* check if reset is complete */
983 if (change & UPS_C_BH_PORT_RESET)
986 /* check for timeout */
993 /* clear port reset first */
994 err = usbd_req_clear_port_feature(
995 udev, mtx, port, UHF_C_BH_PORT_RESET);
999 /* check for timeout */
1001 err = USB_ERR_TIMEOUT;
1005 /* wait for the device to recover from reset */
1006 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
1008 /* wait for the device to recover from reset */
1009 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
1013 DPRINTFN(2, "port %d warm reset returning error=%s\n",
1014 port, usbd_errstr(err));
1018 /*------------------------------------------------------------------------*
1021 * This function can be used to retrieve USB descriptors. It contains
1022 * some additional logic like zeroing of missing descriptor bytes and
1023 * retrying an USB descriptor in case of failure. The "min_len"
1024 * argument specifies the minimum descriptor length. The "max_len"
1025 * argument specifies the maximum descriptor length. If the real
1026 * descriptor length is less than the minimum length the missing
1027 * byte(s) will be zeroed. The type field, the second byte of the USB
1028 * descriptor, will get forced to the correct type. If the "actlen"
1029 * pointer is non-NULL, the actual length of the transfer will get
1030 * stored in the 16-bit unsigned integer which it is pointing to. The
1031 * first byte of the descriptor will not get updated. If the "actlen"
1032 * pointer is NULL the first byte of the descriptor will get updated
1033 * to reflect the actual length instead. If "min_len" is not equal to
1034 * "max_len" then this function will try to retrive the beginning of
1035 * the descriptor and base the maximum length on the first byte of the
1041 *------------------------------------------------------------------------*/
1043 usbd_req_get_desc(struct usb_device *udev,
1044 struct mtx *mtx, uint16_t *actlen, void *desc,
1045 uint16_t min_len, uint16_t max_len,
1046 uint16_t id, uint8_t type, uint8_t index,
1049 struct usb_device_request req;
1053 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
1054 id, type, index, max_len);
1056 req.bmRequestType = UT_READ_DEVICE;
1057 req.bRequest = UR_GET_DESCRIPTOR;
1058 USETW2(req.wValue, type, index);
1059 USETW(req.wIndex, id);
1063 if ((min_len < 2) || (max_len < 2)) {
1064 err = USB_ERR_INVAL;
1067 USETW(req.wLength, min_len);
1069 err = usbd_do_request_flags(udev, mtx, &req,
1070 desc, 0, NULL, 1000);
1078 usb_pause_mtx(mtx, hz / 5);
1084 if (min_len == max_len) {
1086 /* enforce correct length */
1087 if ((buf[0] > min_len) && (actlen == NULL))
1090 /* enforce correct type */
1097 if (max_len > buf[0]) {
1100 /* zero minimum data */
1102 while (min_len > max_len) {
1107 /* set new minimum length */
1112 if (actlen != NULL) {
1121 /*------------------------------------------------------------------------*
1122 * usbd_req_get_string_any
1124 * This function will return the string given by "string_index"
1125 * using the first language ID. The maximum length "len" includes
1126 * the terminating zero. The "len" argument should be twice as
1127 * big pluss 2 bytes, compared with the actual maximum string length !
1132 *------------------------------------------------------------------------*/
1134 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1135 uint16_t len, uint8_t string_index)
1146 /* should not happen */
1147 return (USB_ERR_NORMAL_COMPLETION);
1149 if (string_index == 0) {
1150 /* this is the language table */
1152 return (USB_ERR_INVAL);
1154 if (udev->flags.no_strings) {
1156 return (USB_ERR_STALLED);
1158 err = usbd_req_get_string_desc
1159 (udev, mtx, buf, len, udev->langid, string_index);
1164 temp = (uint8_t *)buf;
1167 /* string length is too short */
1169 return (USB_ERR_INVAL);
1171 /* reserve one byte for terminating zero */
1174 /* find maximum length */
1176 n = (temp[0] / 2) - 1;
1180 /* skip descriptor header */
1183 /* reset swap state */
1186 /* convert and filter */
1187 for (i = 0; (i != n); i++) {
1188 c = UGETW(temp + (2 * i));
1190 /* convert from Unicode, handle buggy strings */
1191 if (((c & 0xff00) == 0) && (swap & 1)) {
1192 /* Little Endian, default */
1195 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1200 /* silently skip bad character */
1205 * Filter by default - We only allow alphanumerical
1206 * and a few more to avoid any problems with scripts
1219 /* silently skip bad character */
1221 *s = 0; /* zero terminate resulting string */
1222 return (USB_ERR_NORMAL_COMPLETION);
1225 /*------------------------------------------------------------------------*
1226 * usbd_req_get_string_desc
1228 * If you don't know the language ID, consider using
1229 * "usbd_req_get_string_any()".
1234 *------------------------------------------------------------------------*/
1236 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1237 uint16_t max_len, uint16_t lang_id,
1238 uint8_t string_index)
1240 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1241 UDESC_STRING, string_index, 0));
1244 /*------------------------------------------------------------------------*
1245 * usbd_req_get_config_desc_ptr
1247 * This function is used in device side mode to retrieve the pointer
1248 * to the generated config descriptor. This saves allocating space for
1249 * an additional config descriptor when setting the configuration.
1254 *------------------------------------------------------------------------*/
1256 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1257 struct usb_config_descriptor **ppcd, uint16_t wValue)
1259 struct usb_device_request req;
1260 usb_handle_req_t *hr_func;
1265 req.bmRequestType = UT_READ_DEVICE;
1266 req.bRequest = UR_GET_DESCRIPTOR;
1267 USETW(req.wValue, wValue);
1268 USETW(req.wIndex, 0);
1269 USETW(req.wLength, 0);
1274 hr_func = usbd_get_hr_func(udev);
1276 if (hr_func == NULL)
1277 err = USB_ERR_INVAL;
1279 USB_BUS_LOCK(udev->bus);
1280 err = (hr_func) (udev, &req, &ptr, &len);
1281 USB_BUS_UNLOCK(udev->bus);
1286 else if (ptr == NULL)
1287 err = USB_ERR_INVAL;
1289 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1294 /*------------------------------------------------------------------------*
1295 * usbd_req_get_config_desc
1300 *------------------------------------------------------------------------*/
1302 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1303 struct usb_config_descriptor *d, uint8_t conf_index)
1307 DPRINTFN(4, "confidx=%d\n", conf_index);
1309 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1310 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1314 /* Extra sanity checking */
1315 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1316 err = USB_ERR_INVAL;
1322 /*------------------------------------------------------------------------*
1323 * usbd_req_get_config_desc_full
1325 * This function gets the complete USB configuration descriptor and
1326 * ensures that "wTotalLength" is correct.
1331 *------------------------------------------------------------------------*/
1333 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1334 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1337 struct usb_config_descriptor cd;
1338 struct usb_config_descriptor *cdesc;
1342 DPRINTFN(4, "index=%d\n", index);
1346 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1350 /* get full descriptor */
1351 len = UGETW(cd.wTotalLength);
1352 if (len < sizeof(*cdesc)) {
1353 /* corrupt descriptor */
1354 return (USB_ERR_INVAL);
1356 cdesc = malloc(len, mtype, M_WAITOK);
1357 if (cdesc == NULL) {
1358 return (USB_ERR_NOMEM);
1360 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1361 UDESC_CONFIG, index, 3);
1366 /* make sure that the device is not fooling us: */
1367 USETW(cdesc->wTotalLength, len);
1371 return (0); /* success */
1374 /*------------------------------------------------------------------------*
1375 * usbd_req_get_device_desc
1380 *------------------------------------------------------------------------*/
1382 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1383 struct usb_device_descriptor *d)
1386 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1387 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1390 /*------------------------------------------------------------------------*
1391 * usbd_req_get_alt_interface_no
1396 *------------------------------------------------------------------------*/
1398 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1399 uint8_t *alt_iface_no, uint8_t iface_index)
1401 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1402 struct usb_device_request req;
1404 if ((iface == NULL) || (iface->idesc == NULL))
1405 return (USB_ERR_INVAL);
1407 req.bmRequestType = UT_READ_INTERFACE;
1408 req.bRequest = UR_GET_INTERFACE;
1409 USETW(req.wValue, 0);
1410 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1412 USETW(req.wLength, 1);
1413 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1416 /*------------------------------------------------------------------------*
1417 * usbd_req_set_alt_interface_no
1422 *------------------------------------------------------------------------*/
1424 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1425 uint8_t iface_index, uint8_t alt_no)
1427 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1428 struct usb_device_request req;
1430 if ((iface == NULL) || (iface->idesc == NULL))
1431 return (USB_ERR_INVAL);
1433 req.bmRequestType = UT_WRITE_INTERFACE;
1434 req.bRequest = UR_SET_INTERFACE;
1435 req.wValue[0] = alt_no;
1437 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1439 USETW(req.wLength, 0);
1440 return (usbd_do_request(udev, mtx, &req, 0));
1443 /*------------------------------------------------------------------------*
1444 * usbd_req_get_device_status
1449 *------------------------------------------------------------------------*/
1451 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1452 struct usb_status *st)
1454 struct usb_device_request req;
1456 req.bmRequestType = UT_READ_DEVICE;
1457 req.bRequest = UR_GET_STATUS;
1458 USETW(req.wValue, 0);
1459 USETW(req.wIndex, 0);
1460 USETW(req.wLength, sizeof(*st));
1461 return (usbd_do_request(udev, mtx, &req, st));
1464 /*------------------------------------------------------------------------*
1465 * usbd_req_get_hub_descriptor
1470 *------------------------------------------------------------------------*/
1472 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1473 struct usb_hub_descriptor *hd, uint8_t nports)
1475 struct usb_device_request req;
1476 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1478 req.bmRequestType = UT_READ_CLASS_DEVICE;
1479 req.bRequest = UR_GET_DESCRIPTOR;
1480 USETW2(req.wValue, UDESC_HUB, 0);
1481 USETW(req.wIndex, 0);
1482 USETW(req.wLength, len);
1483 return (usbd_do_request(udev, mtx, &req, hd));
1486 /*------------------------------------------------------------------------*
1487 * usbd_req_get_ss_hub_descriptor
1492 *------------------------------------------------------------------------*/
1494 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1495 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1497 struct usb_device_request req;
1498 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1500 req.bmRequestType = UT_READ_CLASS_DEVICE;
1501 req.bRequest = UR_GET_DESCRIPTOR;
1502 USETW2(req.wValue, UDESC_SS_HUB, 0);
1503 USETW(req.wIndex, 0);
1504 USETW(req.wLength, len);
1505 return (usbd_do_request(udev, mtx, &req, hd));
1508 /*------------------------------------------------------------------------*
1509 * usbd_req_get_hub_status
1514 *------------------------------------------------------------------------*/
1516 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1517 struct usb_hub_status *st)
1519 struct usb_device_request req;
1521 req.bmRequestType = UT_READ_CLASS_DEVICE;
1522 req.bRequest = UR_GET_STATUS;
1523 USETW(req.wValue, 0);
1524 USETW(req.wIndex, 0);
1525 USETW(req.wLength, sizeof(struct usb_hub_status));
1526 return (usbd_do_request(udev, mtx, &req, st));
1529 /*------------------------------------------------------------------------*
1530 * usbd_req_set_address
1532 * This function is used to set the address for an USB device. After
1533 * port reset the USB device will respond at address zero.
1538 *------------------------------------------------------------------------*/
1540 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1542 struct usb_device_request req;
1545 DPRINTFN(6, "setting device address=%d\n", addr);
1547 req.bmRequestType = UT_WRITE_DEVICE;
1548 req.bRequest = UR_SET_ADDRESS;
1549 USETW(req.wValue, addr);
1550 USETW(req.wIndex, 0);
1551 USETW(req.wLength, 0);
1553 err = USB_ERR_INVAL;
1555 /* check if USB controller handles set address */
1556 if (udev->bus->methods->set_address != NULL)
1557 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1559 if (err != USB_ERR_INVAL)
1562 /* Setting the address should not take more than 1 second ! */
1563 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1564 USB_DELAY_STATUS_STAGE, NULL, 1000);
1567 /* allow device time to set new address */
1569 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1574 /*------------------------------------------------------------------------*
1575 * usbd_req_get_port_status
1580 *------------------------------------------------------------------------*/
1582 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1583 struct usb_port_status *ps, uint8_t port)
1585 struct usb_device_request req;
1587 req.bmRequestType = UT_READ_CLASS_OTHER;
1588 req.bRequest = UR_GET_STATUS;
1589 USETW(req.wValue, 0);
1590 req.wIndex[0] = port;
1592 USETW(req.wLength, sizeof *ps);
1593 return (usbd_do_request(udev, mtx, &req, ps));
1596 /*------------------------------------------------------------------------*
1597 * usbd_req_clear_hub_feature
1602 *------------------------------------------------------------------------*/
1604 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1607 struct usb_device_request req;
1609 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1610 req.bRequest = UR_CLEAR_FEATURE;
1611 USETW(req.wValue, sel);
1612 USETW(req.wIndex, 0);
1613 USETW(req.wLength, 0);
1614 return (usbd_do_request(udev, mtx, &req, 0));
1617 /*------------------------------------------------------------------------*
1618 * usbd_req_set_hub_feature
1623 *------------------------------------------------------------------------*/
1625 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1628 struct usb_device_request req;
1630 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1631 req.bRequest = UR_SET_FEATURE;
1632 USETW(req.wValue, sel);
1633 USETW(req.wIndex, 0);
1634 USETW(req.wLength, 0);
1635 return (usbd_do_request(udev, mtx, &req, 0));
1638 /*------------------------------------------------------------------------*
1639 * usbd_req_set_hub_u1_timeout
1644 *------------------------------------------------------------------------*/
1646 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1647 uint8_t port, uint8_t timeout)
1649 struct usb_device_request req;
1651 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1652 req.bRequest = UR_SET_FEATURE;
1653 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1654 req.wIndex[0] = port;
1655 req.wIndex[1] = timeout;
1656 USETW(req.wLength, 0);
1657 return (usbd_do_request(udev, mtx, &req, 0));
1660 /*------------------------------------------------------------------------*
1661 * usbd_req_set_hub_u2_timeout
1666 *------------------------------------------------------------------------*/
1668 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1669 uint8_t port, uint8_t timeout)
1671 struct usb_device_request req;
1673 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1674 req.bRequest = UR_SET_FEATURE;
1675 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1676 req.wIndex[0] = port;
1677 req.wIndex[1] = timeout;
1678 USETW(req.wLength, 0);
1679 return (usbd_do_request(udev, mtx, &req, 0));
1682 /*------------------------------------------------------------------------*
1683 * usbd_req_set_hub_depth
1688 *------------------------------------------------------------------------*/
1690 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1693 struct usb_device_request req;
1695 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1696 req.bRequest = UR_SET_HUB_DEPTH;
1697 USETW(req.wValue, depth);
1698 USETW(req.wIndex, 0);
1699 USETW(req.wLength, 0);
1700 return (usbd_do_request(udev, mtx, &req, 0));
1703 /*------------------------------------------------------------------------*
1704 * usbd_req_clear_port_feature
1709 *------------------------------------------------------------------------*/
1711 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1712 uint8_t port, uint16_t sel)
1714 struct usb_device_request req;
1716 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1717 req.bRequest = UR_CLEAR_FEATURE;
1718 USETW(req.wValue, sel);
1719 req.wIndex[0] = port;
1721 USETW(req.wLength, 0);
1722 return (usbd_do_request(udev, mtx, &req, 0));
1725 /*------------------------------------------------------------------------*
1726 * usbd_req_set_port_feature
1731 *------------------------------------------------------------------------*/
1733 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1734 uint8_t port, uint16_t sel)
1736 struct usb_device_request req;
1738 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1739 req.bRequest = UR_SET_FEATURE;
1740 USETW(req.wValue, sel);
1741 req.wIndex[0] = port;
1743 USETW(req.wLength, 0);
1744 return (usbd_do_request(udev, mtx, &req, 0));
1747 /*------------------------------------------------------------------------*
1748 * usbd_req_set_protocol
1753 *------------------------------------------------------------------------*/
1755 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1756 uint8_t iface_index, uint16_t report)
1758 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1759 struct usb_device_request req;
1761 if ((iface == NULL) || (iface->idesc == NULL)) {
1762 return (USB_ERR_INVAL);
1764 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1765 iface, report, iface->idesc->bInterfaceNumber);
1767 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1768 req.bRequest = UR_SET_PROTOCOL;
1769 USETW(req.wValue, report);
1770 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1772 USETW(req.wLength, 0);
1773 return (usbd_do_request(udev, mtx, &req, 0));
1776 /*------------------------------------------------------------------------*
1777 * usbd_req_set_report
1782 *------------------------------------------------------------------------*/
1784 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1785 uint8_t iface_index, uint8_t type, uint8_t id)
1787 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1788 struct usb_device_request req;
1790 if ((iface == NULL) || (iface->idesc == NULL)) {
1791 return (USB_ERR_INVAL);
1793 DPRINTFN(5, "len=%d\n", len);
1795 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1796 req.bRequest = UR_SET_REPORT;
1797 USETW2(req.wValue, type, id);
1798 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1800 USETW(req.wLength, len);
1801 return (usbd_do_request(udev, mtx, &req, data));
1804 /*------------------------------------------------------------------------*
1805 * usbd_req_get_report
1810 *------------------------------------------------------------------------*/
1812 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1813 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1815 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1816 struct usb_device_request req;
1818 if ((iface == NULL) || (iface->idesc == NULL)) {
1819 return (USB_ERR_INVAL);
1821 DPRINTFN(5, "len=%d\n", len);
1823 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1824 req.bRequest = UR_GET_REPORT;
1825 USETW2(req.wValue, type, id);
1826 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1828 USETW(req.wLength, len);
1829 return (usbd_do_request(udev, mtx, &req, data));
1832 /*------------------------------------------------------------------------*
1838 *------------------------------------------------------------------------*/
1840 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1841 uint8_t iface_index, uint8_t duration, uint8_t id)
1843 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1844 struct usb_device_request req;
1846 if ((iface == NULL) || (iface->idesc == NULL)) {
1847 return (USB_ERR_INVAL);
1849 DPRINTFN(5, "%d %d\n", duration, id);
1851 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1852 req.bRequest = UR_SET_IDLE;
1853 USETW2(req.wValue, duration, id);
1854 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1856 USETW(req.wLength, 0);
1857 return (usbd_do_request(udev, mtx, &req, 0));
1860 /*------------------------------------------------------------------------*
1861 * usbd_req_get_report_descriptor
1866 *------------------------------------------------------------------------*/
1868 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1869 void *d, uint16_t size, uint8_t iface_index)
1871 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1872 struct usb_device_request req;
1874 if ((iface == NULL) || (iface->idesc == NULL)) {
1875 return (USB_ERR_INVAL);
1877 req.bmRequestType = UT_READ_INTERFACE;
1878 req.bRequest = UR_GET_DESCRIPTOR;
1879 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1880 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1882 USETW(req.wLength, size);
1883 return (usbd_do_request(udev, mtx, &req, d));
1886 /*------------------------------------------------------------------------*
1887 * usbd_req_set_config
1889 * This function is used to select the current configuration number in
1890 * both USB device side mode and USB host side mode. When setting the
1891 * configuration the function of the interfaces can change.
1896 *------------------------------------------------------------------------*/
1898 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1900 struct usb_device_request req;
1902 DPRINTF("setting config %d\n", conf);
1904 /* do "set configuration" request */
1906 req.bmRequestType = UT_WRITE_DEVICE;
1907 req.bRequest = UR_SET_CONFIG;
1908 req.wValue[0] = conf;
1910 USETW(req.wIndex, 0);
1911 USETW(req.wLength, 0);
1912 return (usbd_do_request(udev, mtx, &req, 0));
1915 /*------------------------------------------------------------------------*
1916 * usbd_req_get_config
1921 *------------------------------------------------------------------------*/
1923 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1925 struct usb_device_request req;
1927 req.bmRequestType = UT_READ_DEVICE;
1928 req.bRequest = UR_GET_CONFIG;
1929 USETW(req.wValue, 0);
1930 USETW(req.wIndex, 0);
1931 USETW(req.wLength, 1);
1932 return (usbd_do_request(udev, mtx, &req, pconf));
1935 /*------------------------------------------------------------------------*
1936 * usbd_setup_device_desc
1937 *------------------------------------------------------------------------*/
1939 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1944 * Get the first 8 bytes of the device descriptor !
1946 * NOTE: "usbd_do_request()" will check the device descriptor
1947 * next time we do a request to see if the maximum packet size
1948 * changed! The 8 first bytes of the device descriptor
1949 * contains the maximum packet size to use on control endpoint
1950 * 0. If this value is different from "USB_MAX_IPACKET" a new
1951 * USB control request will be setup!
1953 switch (udev->speed) {
1954 case USB_SPEED_FULL:
1956 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1957 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1959 DPRINTFN(0, "getting device descriptor "
1960 "at addr %d failed, %s\n", udev->address,
1966 DPRINTF("Minimum MaxPacketSize is large enough "
1967 "to hold the complete device descriptor\n");
1971 /* get the full device descriptor */
1972 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1974 /* try one more time, if error */
1976 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1979 DPRINTF("addr=%d, getting full desc failed\n",
1984 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1985 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1986 udev->address, UGETW(udev->ddesc.bcdUSB),
1987 udev->ddesc.bDeviceClass,
1988 udev->ddesc.bDeviceSubClass,
1989 udev->ddesc.bDeviceProtocol,
1990 udev->ddesc.bMaxPacketSize,
1991 udev->ddesc.bLength,
1997 /*------------------------------------------------------------------------*
1998 * usbd_req_re_enumerate
2000 * NOTE: After this function returns the hardware is in the
2001 * unconfigured state! The application is responsible for setting a
2002 * new configuration.
2007 *------------------------------------------------------------------------*/
2009 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2011 struct usb_device *parent_hub;
2014 uint8_t do_retry = 1;
2016 if (udev->flags.usb_mode != USB_MODE_HOST) {
2017 return (USB_ERR_INVAL);
2019 old_addr = udev->address;
2020 parent_hub = udev->parent_hub;
2021 if (parent_hub == NULL) {
2022 return (USB_ERR_INVAL);
2026 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2027 * speed device, if any.
2029 if (udev->parent_hs_hub != NULL &&
2030 udev->speed != USB_SPEED_HIGH) {
2031 DPRINTF("Trying to reset parent High Speed TT.\n");
2032 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
2035 DPRINTF("Resetting parent High "
2036 "Speed TT failed (%s).\n",
2041 /* Try to warm reset first */
2042 if (parent_hub->speed == USB_SPEED_SUPER)
2043 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2045 /* Try to reset the parent HUB port. */
2046 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2048 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2049 old_addr, usbd_errstr(err));
2054 * After that the port has been reset our device should be at
2057 udev->address = USB_START_ADDR;
2059 /* reset "bMaxPacketSize" */
2060 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2062 /* reset USB state */
2063 usb_set_device_state(udev, USB_STATE_POWERED);
2066 * Restore device address:
2068 err = usbd_req_set_address(udev, mtx, old_addr);
2070 /* XXX ignore any errors! */
2071 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2072 old_addr, usbd_errstr(err));
2075 * Restore device address, if the controller driver did not
2078 if (udev->address == USB_START_ADDR)
2079 udev->address = old_addr;
2081 /* setup the device descriptor and the initial "wMaxPacketSize" */
2082 err = usbd_setup_device_desc(udev, mtx);
2085 if (err && do_retry) {
2086 /* give the USB firmware some time to load */
2087 usb_pause_mtx(mtx, hz / 2);
2088 /* no more retries after this retry */
2093 /* restore address */
2094 if (udev->address == USB_START_ADDR)
2095 udev->address = old_addr;
2096 /* update state, if successful */
2098 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2102 /*------------------------------------------------------------------------*
2103 * usbd_req_clear_device_feature
2108 *------------------------------------------------------------------------*/
2110 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2113 struct usb_device_request req;
2115 req.bmRequestType = UT_WRITE_DEVICE;
2116 req.bRequest = UR_CLEAR_FEATURE;
2117 USETW(req.wValue, sel);
2118 USETW(req.wIndex, 0);
2119 USETW(req.wLength, 0);
2120 return (usbd_do_request(udev, mtx, &req, 0));
2123 /*------------------------------------------------------------------------*
2124 * usbd_req_set_device_feature
2129 *------------------------------------------------------------------------*/
2131 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2134 struct usb_device_request req;
2136 req.bmRequestType = UT_WRITE_DEVICE;
2137 req.bRequest = UR_SET_FEATURE;
2138 USETW(req.wValue, sel);
2139 USETW(req.wIndex, 0);
2140 USETW(req.wLength, 0);
2141 return (usbd_do_request(udev, mtx, &req, 0));
2144 /*------------------------------------------------------------------------*
2150 *------------------------------------------------------------------------*/
2152 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2155 struct usb_device_request req;
2157 /* For single TT HUBs the port should be 1 */
2159 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2160 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2163 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2164 req.bRequest = UR_RESET_TT;
2165 USETW(req.wValue, 0);
2166 req.wIndex[0] = port;
2168 USETW(req.wLength, 0);
2169 return (usbd_do_request(udev, mtx, &req, 0));
2172 /*------------------------------------------------------------------------*
2173 * usbd_req_clear_tt_buffer
2175 * For single TT HUBs the port should be 1.
2180 *------------------------------------------------------------------------*/
2182 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2183 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2185 struct usb_device_request req;
2188 /* For single TT HUBs the port should be 1 */
2190 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2191 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2194 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2195 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2197 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2198 req.bRequest = UR_CLEAR_TT_BUFFER;
2199 USETW(req.wValue, wValue);
2200 req.wIndex[0] = port;
2202 USETW(req.wLength, 0);
2203 return (usbd_do_request(udev, mtx, &req, 0));
2206 /*------------------------------------------------------------------------*
2207 * usbd_req_set_port_link_state
2209 * USB 3.0 specific request
2214 *------------------------------------------------------------------------*/
2216 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2217 uint8_t port, uint8_t link_state)
2219 struct usb_device_request req;
2221 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2222 req.bRequest = UR_SET_FEATURE;
2223 USETW(req.wValue, UHF_PORT_LINK_STATE);
2224 req.wIndex[0] = port;
2225 req.wIndex[1] = link_state;
2226 USETW(req.wLength, 0);
2227 return (usbd_do_request(udev, mtx, &req, 0));