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/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <bus/u4b/usb.h>
47 #include <bus/u4b/usbdi.h>
48 #include <bus/u4b/usbdi_util.h>
49 #include <bus/u4b/usb_ioctl.h>
50 #include <bus/u4b/usbhid.h>
52 #define USB_DEBUG_VAR usb_debug
54 #include <bus/u4b/usb_core.h>
55 #include <bus/u4b/usb_busdma.h>
56 #include <bus/u4b/usb_request.h>
57 #include <bus/u4b/usb_process.h>
58 #include <bus/u4b/usb_transfer.h>
59 #include <bus/u4b/usb_debug.h>
60 #include <bus/u4b/usb_device.h>
61 #include <bus/u4b/usb_util.h>
62 #include <bus/u4b/usb_dynamic.h>
64 #include <bus/u4b/usb_controller.h>
65 #include <bus/u4b/usb_bus.h>
66 #include <sys/ctype.h>
68 static int usb_no_cs_fail;
70 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
71 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
74 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
75 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
77 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
78 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
79 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
80 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
83 /* The following structures are used in connection to fault injection. */
84 struct usb_ctrl_debug {
85 int bus_index; /* target bus */
86 int dev_index; /* target address */
87 int ds_fail; /* fail data stage */
88 int ss_fail; /* fail data stage */
89 int ds_delay; /* data stage delay in ms */
90 int ss_delay; /* status stage delay in ms */
91 int bmRequestType_value;
95 struct usb_ctrl_debug_bits {
103 /* The default is to disable fault injection. */
105 static struct usb_ctrl_debug usb_ctrl_debug = {
108 .bmRequestType_value = -1,
109 .bRequest_value = -1,
112 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
113 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
114 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
115 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
117 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
119 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
120 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
121 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
122 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
123 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
124 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
125 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
126 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
127 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
129 /*------------------------------------------------------------------------*
130 * usbd_get_debug_bits
132 * This function is only useful in USB host mode.
133 *------------------------------------------------------------------------*/
135 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
136 struct usb_ctrl_debug_bits *dbg)
140 memset(dbg, 0, sizeof(*dbg));
142 /* Compute data stage delay */
144 temp = usb_ctrl_debug.ds_delay;
147 else if (temp > (16*1024))
150 dbg->ds_delay = temp;
152 /* Compute status stage delay */
154 temp = usb_ctrl_debug.ss_delay;
157 else if (temp > (16*1024))
160 dbg->ss_delay = temp;
162 /* Check if this control request should be failed */
164 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
167 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
170 temp = usb_ctrl_debug.bmRequestType_value;
172 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
175 temp = usb_ctrl_debug.bRequest_value;
177 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
180 temp = usb_ctrl_debug.ds_fail;
184 temp = usb_ctrl_debug.ss_fail;
190 #endif /* USB_REQ_DEBUG */
191 #endif /* USB_DEBUG */
193 /*------------------------------------------------------------------------*
194 * usbd_do_request_callback
196 * This function is the USB callback for generic USB Host control
198 *------------------------------------------------------------------------*/
200 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
202 ; /* workaround for a bug in "indent" */
204 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
206 switch (USB_GET_STATE(xfer)) {
208 usbd_transfer_submit(xfer);
211 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
216 /*------------------------------------------------------------------------*
217 * usb_do_clear_stall_callback
219 * This function is the USB callback for generic clear stall requests.
220 *------------------------------------------------------------------------*/
222 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
224 struct usb_device_request req;
225 struct usb_device *udev;
226 struct usb_endpoint *ep;
227 struct usb_endpoint *ep_end;
228 struct usb_endpoint *ep_first;
231 udev = xfer->xroot->udev;
233 USB_BUS_LOCK(udev->bus);
235 /* round robin endpoint clear stall */
238 ep_end = udev->endpoints + udev->endpoints_max;
239 ep_first = udev->endpoints;
240 to = udev->endpoints_max;
242 switch (USB_GET_STATE(xfer)) {
243 case USB_ST_TRANSFERRED:
245 /* reset error counter */
246 udev->clear_stall_errors = 0;
249 goto tr_setup; /* device was unconfigured */
254 /* some hardware needs a callback to clear the data toggle */
255 usbd_clear_stall_locked(udev, ep);
256 /* start up the current or next transfer, if any */
257 usb_command_wrapper(&ep->endpoint_q,
258 ep->endpoint_q.curr);
265 break; /* no endpoints - nothing to do */
266 if ((ep < ep_first) || (ep >= ep_end))
267 ep = ep_first; /* endpoint wrapped around */
271 /* setup a clear-stall packet */
273 req.bmRequestType = UT_WRITE_ENDPOINT;
274 req.bRequest = UR_CLEAR_FEATURE;
275 USETW(req.wValue, UF_ENDPOINT_HALT);
276 req.wIndex[0] = ep->edesc->bEndpointAddress;
278 USETW(req.wLength, 0);
280 /* copy in the transfer */
282 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
285 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
287 USB_BUS_UNLOCK(udev->bus);
289 usbd_transfer_submit(xfer);
291 USB_BUS_LOCK(udev->bus);
299 if (error == USB_ERR_CANCELLED)
302 DPRINTF("Clear stall failed.\n");
305 * Some VMs like VirtualBox always return failure on
306 * clear-stall which we sometimes should just ignore.
310 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
313 if (error == USB_ERR_TIMEOUT) {
314 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
315 DPRINTF("Trying to re-enumerate.\n");
316 usbd_start_re_enumerate(udev);
318 udev->clear_stall_errors++;
319 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
320 DPRINTF("Trying to re-enumerate.\n");
321 usbd_start_re_enumerate(udev);
327 /* store current endpoint */
329 USB_BUS_UNLOCK(udev->bus);
332 static usb_handle_req_t *
333 usbd_get_hr_func(struct usb_device *udev)
335 /* figure out if there is a Handle Request function */
336 if (udev->flags.usb_mode == USB_MODE_DEVICE)
337 return (usb_temp_get_desc_p);
338 else if (udev->parent_hub == NULL)
339 return (udev->bus->methods->roothub_exec);
344 /*------------------------------------------------------------------------*
345 * usbd_do_request_flags and usbd_do_request
347 * Description of arguments passed to these functions:
349 * "udev" - this is the "usb_device" structure pointer on which the
350 * request should be performed. It is possible to call this function
351 * in both Host Side mode and Device Side mode.
353 * "mtx" - if this argument is non-NULL the mutex pointed to by it
354 * will get dropped and picked up during the execution of this
355 * function, hence this function sometimes needs to sleep. If this
356 * argument is NULL it has no effect.
358 * "req" - this argument must always be non-NULL and points to an
359 * 8-byte structure holding the USB request to be done. The USB
360 * request structure has a bit telling the direction of the USB
361 * request, if it is a read or a write.
363 * "data" - if the "wLength" part of the structure pointed to by "req"
364 * is non-zero this argument must point to a valid kernel buffer which
365 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
368 * "flags" - here is a list of valid flags:
370 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
373 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
374 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
375 * sysctl. This flag is mostly useful for debugging.
377 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
380 * "actlen" - if non-NULL the actual transfer length will be stored in
381 * the 16-bit unsigned integer pointed to by "actlen". This
382 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
385 * "timeout" - gives the timeout for the control transfer in
386 * milliseconds. A "timeout" value less than 50 milliseconds is
387 * treated like a 50 millisecond timeout. A "timeout" value greater
388 * than 30 seconds is treated like a 30 second timeout. This USB stack
389 * does not allow control requests without a timeout.
391 * NOTE: This function is thread safe. All calls to
392 * "usbd_do_request_flags" will be serialised by the use of an
393 * internal "sx_lock".
398 *------------------------------------------------------------------------*/
400 usbd_do_request_flags(struct usb_device *udev, struct lock *lock,
401 struct usb_device_request *req, void *data, uint16_t flags,
402 uint16_t *actlen, usb_timeout_t timeout)
405 struct usb_ctrl_debug_bits dbg;
407 usb_handle_req_t *hr_func;
408 struct usb_xfer *xfer;
411 usb_ticks_t start_ticks;
412 usb_ticks_t delta_ticks;
413 usb_ticks_t max_ticks;
420 /* timeout is too small */
423 if (timeout > 30000) {
424 /* timeout is too big */
427 length = UGETW(req->wLength);
429 enum_locked = usbd_enum_is_locked(udev);
431 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
432 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
433 udev, req->bmRequestType, req->bRequest,
434 req->wValue[1], req->wValue[0],
435 req->wIndex[1], req->wIndex[0],
436 req->wLength[1], req->wLength[0]);
438 /* Check if the device is still alive */
439 if (udev->state < USB_STATE_POWERED) {
440 DPRINTF("usb device has gone\n");
441 return (USB_ERR_NOT_CONFIGURED);
445 * Set "actlen" to a known value in case the caller does not
446 * check the return value:
451 #if (USB_HAVE_USER_IO == 0)
452 if (flags & USB_USER_DATA_PTR)
453 return (USB_ERR_INVAL);
456 if ((mtx != NULL) && (mtx != &Giant)) {
459 lockmgr(lock, LK_RELEASE);
460 KKASSERT(!lockowned(lock));
464 * We need to allow suspend and resume at this point, else the
465 * control transfer will timeout if the device is suspended!
468 usbd_sr_unlock(udev);
471 * Grab the default sx-lock so that serialisation
472 * is achieved when multiple threads are involved:
474 lockmgr(&udev->ctrl_lock, LK_EXCLUSIVE);
476 hr_func = usbd_get_hr_func(udev);
478 if (hr_func != NULL) {
479 DPRINTF("Handle Request function is set\n");
484 if (!(req->bmRequestType & UT_READ)) {
486 DPRINTFN(1, "The handle request function "
487 "does not support writing data!\n");
493 /* The root HUB code needs the BUS lock locked */
495 USB_BUS_LOCK(udev->bus);
496 err = (hr_func) (udev, req, &desc, &temp);
497 USB_BUS_UNLOCK(udev->bus);
503 if (!(flags & USB_SHORT_XFER_OK)) {
504 err = USB_ERR_SHORT_XFER;
514 if (flags & USB_USER_DATA_PTR) {
515 if (copyout(desc, data, length)) {
521 memcpy(data, desc, length);
523 goto done; /* success */
527 * Setup a new USB transfer or use the existing one, if any:
529 usbd_ctrl_transfer_setup(udev);
531 xfer = udev->ctrl_xfer[0];
533 /* most likely out of memory */
540 usbd_get_debug_bits(udev, req, &dbg);
542 /* Check for fault injection */
544 flags |= USB_DELAY_STATUS_STAGE;
548 if (flags & USB_DELAY_STATUS_STAGE)
549 xfer->flags.manual_status = 1;
551 xfer->flags.manual_status = 0;
553 if (flags & USB_SHORT_XFER_OK)
554 xfer->flags.short_xfer_ok = 1;
556 xfer->flags.short_xfer_ok = 0;
558 xfer->timeout = timeout;
562 max_ticks = USB_MS_TO_TICKS(timeout);
564 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
566 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
570 if (temp > usbd_xfer_max_len(xfer)) {
571 temp = usbd_xfer_max_len(xfer);
574 if (xfer->flags.manual_status) {
575 if (usbd_xfer_frame_len(xfer, 0) != 0) {
576 /* Execute data stage separately */
578 } else if (temp > 0) {
583 if (dbg.ds_delay > 0) {
585 xfer->xroot->xfer_lock,
586 USB_MS_TO_TICKS(dbg.ds_delay));
587 /* make sure we don't time out */
593 usbd_xfer_set_frame_len(xfer, 1, temp);
596 if (!(req->bmRequestType & UT_READ)) {
598 if (flags & USB_USER_DATA_PTR) {
599 USB_XFER_UNLOCK(xfer);
600 err = usbd_copy_in_user(xfer->frbuffers + 1,
609 usbd_copy_in(xfer->frbuffers + 1,
612 usbd_xfer_set_frames(xfer, 2);
614 if (usbd_xfer_frame_len(xfer, 0) == 0) {
615 if (xfer->flags.manual_status) {
621 if (dbg.ss_delay > 0) {
623 xfer->xroot->xfer_lock,
624 USB_MS_TO_TICKS(dbg.ss_delay));
625 /* make sure we don't time out */
629 xfer->flags.manual_status = 0;
634 usbd_xfer_set_frames(xfer, 1);
637 usbd_transfer_start(xfer);
639 while (usbd_transfer_pending(xfer)) {
640 cv_wait(&udev->ctrlreq_cv,
641 xfer->xroot->xfer_lock);
650 /* get actual length of DATA stage */
652 if (xfer->aframes < 2) {
655 acttemp = usbd_xfer_frame_len(xfer, 1);
658 /* check for short packet */
660 if (temp > acttemp) {
665 if (req->bmRequestType & UT_READ) {
667 if (flags & USB_USER_DATA_PTR) {
668 USB_XFER_UNLOCK(xfer);
669 err = usbd_copy_out_user(xfer->frbuffers + 1,
678 usbd_copy_out(xfer->frbuffers + 1,
683 * Clear "frlengths[0]" so that we don't send the setup
686 usbd_xfer_set_frame_len(xfer, 0, 0);
688 /* update length and data pointer */
690 data = USB_ADD_BYTES(data, temp);
695 /* check for timeout */
697 delta_ticks = ticks - start_ticks;
698 if (delta_ticks > max_ticks) {
700 err = USB_ERR_TIMEOUT;
710 * Make sure that the control endpoint is no longer
711 * blocked in case of a non-transfer related error:
713 usbd_transfer_stop(xfer);
715 USB_XFER_UNLOCK(xfer);
718 lockmgr(&udev->ctrl_lock, LK_RELEASE);
724 if ((mtx != NULL) && (mtx != &Giant))
727 lockmgr(lock, LK_EXCLUSIVE);
729 return ((usb_error_t)err);
732 /*------------------------------------------------------------------------*
733 * usbd_do_request_proc - factored out code
735 * This function is factored out code. It does basically the same like
736 * usbd_do_request_flags, except it will check the status of the
737 * passed process argument before doing the USB request. If the
738 * process is draining the USB_ERR_IOERROR code will be returned. It
739 * is assumed that the mutex associated with the process is locked
740 * when calling this function.
741 *------------------------------------------------------------------------*/
743 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
744 struct usb_device_request *req, void *data, uint16_t flags,
745 uint16_t *actlen, usb_timeout_t timeout)
750 /* get request data length */
751 len = UGETW(req->wLength);
753 /* check if the device is being detached */
754 if (usb_proc_is_gone(pproc)) {
755 err = USB_ERR_IOERROR;
759 /* forward the USB request */
760 err = usbd_do_request_flags(udev, pproc->up_lock,
761 req, data, flags, actlen, timeout);
764 /* on failure we zero the data */
765 /* on short packet we zero the unused data */
766 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
768 memset(data, 0, len);
769 else if (actlen && *actlen != len)
770 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
775 /*------------------------------------------------------------------------*
776 * usbd_req_reset_port
778 * This function will instruct a USB HUB to perform a reset sequence
779 * on the specified port number.
782 * 0: Success. The USB device should now be at address zero.
783 * Else: Failure. No USB device is present and the USB port should be
785 *------------------------------------------------------------------------*/
787 usbd_req_reset_port(struct usb_device *udev, struct lock *lock, uint8_t port)
789 struct usb_port_status ps;
796 uint16_t pr_poll_delay;
797 uint16_t pr_recovery_delay;
803 /* clear any leftover port reset changes first */
804 usbd_req_clear_port_feature(
805 udev, lock, port, UHF_C_PORT_RESET);
807 /* assert port reset on the given port */
808 err = usbd_req_set_port_feature(
809 udev, lock, port, UHF_PORT_RESET);
811 /* check for errors */
815 /* range check input parameters */
816 pr_poll_delay = usb_pr_poll_delay;
817 if (pr_poll_delay < 1) {
819 } else if (pr_poll_delay > 1000) {
820 pr_poll_delay = 1000;
822 pr_recovery_delay = usb_pr_recovery_delay;
823 if (pr_recovery_delay > 1000) {
824 pr_recovery_delay = 1000;
830 /* wait for the device to recover from reset */
831 usb_pause_mtx(lock, USB_MS_TO_TICKS(pr_poll_delay));
834 /* wait for the device to recover from reset */
835 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
836 n += USB_PORT_RESET_DELAY;
838 err = usbd_req_get_port_status(udev, lock, &ps, port);
842 status = UGETW(ps.wPortStatus);
843 change = UGETW(ps.wPortChange);
845 /* if the device disappeared, just give up */
846 if (!(status & UPS_CURRENT_CONNECT_STATUS))
849 /* check if reset is complete */
850 if (change & UPS_C_PORT_RESET)
854 * Some Virtual Machines like VirtualBox 4.x fail to
855 * generate a port reset change event. Check if reset
856 * is no longer asserted.
858 if (!(status & UPS_RESET))
861 /* check for timeout */
868 /* clear port reset first */
869 err = usbd_req_clear_port_feature(
870 udev, lock, port, UHF_C_PORT_RESET);
874 /* check for timeout */
876 err = USB_ERR_TIMEOUT;
880 /* wait for the device to recover from reset */
881 usb_pause_mtx(lock, USB_MS_TO_TICKS(pr_recovery_delay));
883 /* wait for the device to recover from reset */
884 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
888 DPRINTFN(2, "port %d reset returning error=%s\n",
889 port, usbd_errstr(err));
893 /*------------------------------------------------------------------------*
894 * usbd_req_warm_reset_port
896 * This function will instruct an USB HUB to perform a warm reset
897 * sequence on the specified port number. This kind of reset is not
898 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
899 * for SUPER-speed USB HUBs.
902 * 0: Success. The USB device should now be available again.
903 * Else: Failure. No USB device is present and the USB port should be
905 *------------------------------------------------------------------------*/
907 usbd_req_warm_reset_port(struct usb_device *udev, struct lock *lock,
910 struct usb_port_status ps;
917 uint16_t pr_poll_delay;
918 uint16_t pr_recovery_delay;
924 err = usbd_req_get_port_status(udev, lock, &ps, port);
928 status = UGETW(ps.wPortStatus);
930 switch (UPS_PORT_LINK_STATE_GET(status)) {
932 case UPS_PORT_LS_COMP_MODE:
933 case UPS_PORT_LS_LOOPBACK:
934 case UPS_PORT_LS_SS_INA:
937 DPRINTF("Wrong state for warm reset\n");
941 /* clear any leftover warm port reset changes first */
942 usbd_req_clear_port_feature(udev, lock,
943 port, UHF_C_BH_PORT_RESET);
945 /* set warm port reset */
946 err = usbd_req_set_port_feature(udev, lock,
947 port, UHF_BH_PORT_RESET);
952 /* range check input parameters */
953 pr_poll_delay = usb_pr_poll_delay;
954 if (pr_poll_delay < 1) {
956 } else if (pr_poll_delay > 1000) {
957 pr_poll_delay = 1000;
959 pr_recovery_delay = usb_pr_recovery_delay;
960 if (pr_recovery_delay > 1000) {
961 pr_recovery_delay = 1000;
967 /* wait for the device to recover from reset */
968 usb_pause_mtx(lock, USB_MS_TO_TICKS(pr_poll_delay));
971 /* wait for the device to recover from reset */
972 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
973 n += USB_PORT_RESET_DELAY;
975 err = usbd_req_get_port_status(udev, lock, &ps, port);
979 status = UGETW(ps.wPortStatus);
980 change = UGETW(ps.wPortChange);
982 /* if the device disappeared, just give up */
983 if (!(status & UPS_CURRENT_CONNECT_STATUS))
986 /* check if reset is complete */
987 if (change & UPS_C_BH_PORT_RESET)
990 /* check for timeout */
997 /* clear port reset first */
998 err = usbd_req_clear_port_feature(
999 udev, lock, port, UHF_C_BH_PORT_RESET);
1003 /* check for timeout */
1005 err = USB_ERR_TIMEOUT;
1009 /* wait for the device to recover from reset */
1010 usb_pause_mtx(lock, USB_MS_TO_TICKS(pr_recovery_delay));
1012 /* wait for the device to recover from reset */
1013 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
1017 DPRINTFN(2, "port %d warm reset returning error=%s\n",
1018 port, usbd_errstr(err));
1022 /*------------------------------------------------------------------------*
1025 * This function can be used to retrieve USB descriptors. It contains
1026 * some additional logic like zeroing of missing descriptor bytes and
1027 * retrying an USB descriptor in case of failure. The "min_len"
1028 * argument specifies the minimum descriptor length. The "max_len"
1029 * argument specifies the maximum descriptor length. If the real
1030 * descriptor length is less than the minimum length the missing
1031 * byte(s) will be zeroed. The type field, the second byte of the USB
1032 * descriptor, will get forced to the correct type. If the "actlen"
1033 * pointer is non-NULL, the actual length of the transfer will get
1034 * stored in the 16-bit unsigned integer which it is pointing to. The
1035 * first byte of the descriptor will not get updated. If the "actlen"
1036 * pointer is NULL the first byte of the descriptor will get updated
1037 * to reflect the actual length instead. If "min_len" is not equal to
1038 * "max_len" then this function will try to retrive the beginning of
1039 * the descriptor and base the maximum length on the first byte of the
1045 *------------------------------------------------------------------------*/
1047 usbd_req_get_desc(struct usb_device *udev,
1048 struct lock *lock, uint16_t *actlen, void *desc,
1049 uint16_t min_len, uint16_t max_len,
1050 uint16_t id, uint8_t type, uint8_t index,
1053 struct usb_device_request req;
1057 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
1058 id, type, index, max_len);
1060 req.bmRequestType = UT_READ_DEVICE;
1061 req.bRequest = UR_GET_DESCRIPTOR;
1062 USETW2(req.wValue, type, index);
1063 USETW(req.wIndex, id);
1067 if ((min_len < 2) || (max_len < 2)) {
1068 err = USB_ERR_INVAL;
1071 USETW(req.wLength, min_len);
1073 err = usbd_do_request_flags(udev, lock, &req,
1074 desc, 0, NULL, 1000);
1082 usb_pause_mtx(lock, hz / 5);
1088 if (min_len == max_len) {
1090 /* enforce correct length */
1091 if ((buf[0] > min_len) && (actlen == NULL))
1094 /* enforce correct type */
1101 if (max_len > buf[0]) {
1104 /* zero minimum data */
1106 while (min_len > max_len) {
1111 /* set new minimum length */
1116 if (actlen != NULL) {
1125 /*------------------------------------------------------------------------*
1126 * usbd_req_get_string_any
1128 * This function will return the string given by "string_index"
1129 * using the first language ID. The maximum length "len" includes
1130 * the terminating zero. The "len" argument should be twice as
1131 * big pluss 2 bytes, compared with the actual maximum string length !
1136 *------------------------------------------------------------------------*/
1138 usbd_req_get_string_any(struct usb_device *udev, struct lock *lock, char *buf,
1139 uint16_t len, uint8_t string_index)
1150 /* should not happen */
1151 return (USB_ERR_NORMAL_COMPLETION);
1153 if (string_index == 0) {
1154 /* this is the language table */
1156 return (USB_ERR_INVAL);
1158 if (udev->flags.no_strings) {
1160 return (USB_ERR_STALLED);
1162 err = usbd_req_get_string_desc
1163 (udev, lock, buf, len, udev->langid, string_index);
1168 temp = (uint8_t *)buf;
1171 /* string length is too short */
1173 return (USB_ERR_INVAL);
1175 /* reserve one byte for terminating zero */
1178 /* find maximum length */
1180 n = (temp[0] / 2) - 1;
1184 /* skip descriptor header */
1187 /* reset swap state */
1190 /* convert and filter */
1191 for (i = 0; (i != n); i++) {
1192 c = UGETW(temp + (2 * i));
1194 /* convert from Unicode, handle buggy strings */
1195 if (((c & 0xff00) == 0) && (swap & 1)) {
1196 /* Little Endian, default */
1199 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1204 /* silently skip bad character */
1209 * Filter by default - We only allow alphanumerical
1210 * and a few more to avoid any problems with scripts
1223 /* silently skip bad character */
1225 *s = 0; /* zero terminate resulting string */
1226 return (USB_ERR_NORMAL_COMPLETION);
1229 /*------------------------------------------------------------------------*
1230 * usbd_req_get_string_desc
1232 * If you don't know the language ID, consider using
1233 * "usbd_req_get_string_any()".
1238 *------------------------------------------------------------------------*/
1240 usbd_req_get_string_desc(struct usb_device *udev, struct lock *lock, void *sdesc,
1241 uint16_t max_len, uint16_t lang_id,
1242 uint8_t string_index)
1244 return (usbd_req_get_desc(udev, lock, NULL, sdesc, 2, max_len, lang_id,
1245 UDESC_STRING, string_index, 0));
1248 /*------------------------------------------------------------------------*
1249 * usbd_req_get_config_desc_ptr
1251 * This function is used in device side mode to retrieve the pointer
1252 * to the generated config descriptor. This saves allocating space for
1253 * an additional config descriptor when setting the configuration.
1258 *------------------------------------------------------------------------*/
1260 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1261 struct usb_config_descriptor **ppcd, uint16_t wValue)
1263 struct usb_device_request req;
1264 usb_handle_req_t *hr_func;
1269 req.bmRequestType = UT_READ_DEVICE;
1270 req.bRequest = UR_GET_DESCRIPTOR;
1271 USETW(req.wValue, wValue);
1272 USETW(req.wIndex, 0);
1273 USETW(req.wLength, 0);
1278 hr_func = usbd_get_hr_func(udev);
1280 if (hr_func == NULL)
1281 err = USB_ERR_INVAL;
1283 USB_BUS_LOCK(udev->bus);
1284 err = (hr_func) (udev, &req, &ptr, &len);
1285 USB_BUS_UNLOCK(udev->bus);
1290 else if (ptr == NULL)
1291 err = USB_ERR_INVAL;
1293 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1298 /*------------------------------------------------------------------------*
1299 * usbd_req_get_config_desc
1304 *------------------------------------------------------------------------*/
1306 usbd_req_get_config_desc(struct usb_device *udev, struct lock *lock,
1307 struct usb_config_descriptor *d, uint8_t conf_index)
1311 DPRINTFN(4, "confidx=%d\n", conf_index);
1313 err = usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1314 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1318 /* Extra sanity checking */
1319 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1320 err = USB_ERR_INVAL;
1326 /*------------------------------------------------------------------------*
1327 * usbd_req_get_config_desc_full
1329 * This function gets the complete USB configuration descriptor and
1330 * ensures that "wTotalLength" is correct.
1335 *------------------------------------------------------------------------*/
1337 usbd_req_get_config_desc_full(struct usb_device *udev, struct lock *lock,
1338 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1341 struct usb_config_descriptor cd;
1342 struct usb_config_descriptor *cdesc;
1346 DPRINTFN(4, "index=%d\n", index);
1350 err = usbd_req_get_config_desc(udev, lock, &cd, index);
1354 /* get full descriptor */
1355 len = UGETW(cd.wTotalLength);
1356 if (len < sizeof(*cdesc)) {
1357 /* corrupt descriptor */
1358 return (USB_ERR_INVAL);
1360 cdesc = kmalloc(len, mtype, M_WAITOK);
1361 if (cdesc == NULL) {
1362 return (USB_ERR_NOMEM);
1364 err = usbd_req_get_desc(udev, lock, NULL, cdesc, len, len, 0,
1365 UDESC_CONFIG, index, 3);
1367 kfree(cdesc, mtype);
1370 /* make sure that the device is not fooling us: */
1371 USETW(cdesc->wTotalLength, len);
1375 return (0); /* success */
1378 /*------------------------------------------------------------------------*
1379 * usbd_req_get_device_desc
1384 *------------------------------------------------------------------------*/
1386 usbd_req_get_device_desc(struct usb_device *udev, struct lock *lock,
1387 struct usb_device_descriptor *d)
1390 return (usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1391 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1394 /*------------------------------------------------------------------------*
1395 * usbd_req_get_alt_interface_no
1400 *------------------------------------------------------------------------*/
1402 usbd_req_get_alt_interface_no(struct usb_device *udev, struct lock *lock,
1403 uint8_t *alt_iface_no, uint8_t iface_index)
1405 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1406 struct usb_device_request req;
1408 if ((iface == NULL) || (iface->idesc == NULL))
1409 return (USB_ERR_INVAL);
1411 req.bmRequestType = UT_READ_INTERFACE;
1412 req.bRequest = UR_GET_INTERFACE;
1413 USETW(req.wValue, 0);
1414 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1416 USETW(req.wLength, 1);
1417 return (usbd_do_request(udev, lock, &req, alt_iface_no));
1420 /*------------------------------------------------------------------------*
1421 * usbd_req_set_alt_interface_no
1426 *------------------------------------------------------------------------*/
1428 usbd_req_set_alt_interface_no(struct usb_device *udev, struct lock *lock,
1429 uint8_t iface_index, uint8_t alt_no)
1431 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1432 struct usb_device_request req;
1434 if ((iface == NULL) || (iface->idesc == NULL))
1435 return (USB_ERR_INVAL);
1437 req.bmRequestType = UT_WRITE_INTERFACE;
1438 req.bRequest = UR_SET_INTERFACE;
1439 req.wValue[0] = alt_no;
1441 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1443 USETW(req.wLength, 0);
1444 return (usbd_do_request(udev, lock, &req, 0));
1447 /*------------------------------------------------------------------------*
1448 * usbd_req_get_device_status
1453 *------------------------------------------------------------------------*/
1455 usbd_req_get_device_status(struct usb_device *udev, struct lock *lock,
1456 struct usb_status *st)
1458 struct usb_device_request req;
1460 req.bmRequestType = UT_READ_DEVICE;
1461 req.bRequest = UR_GET_STATUS;
1462 USETW(req.wValue, 0);
1463 USETW(req.wIndex, 0);
1464 USETW(req.wLength, sizeof(*st));
1465 return (usbd_do_request(udev, lock, &req, st));
1468 /*------------------------------------------------------------------------*
1469 * usbd_req_get_hub_descriptor
1474 *------------------------------------------------------------------------*/
1476 usbd_req_get_hub_descriptor(struct usb_device *udev, struct lock *lock,
1477 struct usb_hub_descriptor *hd, uint8_t nports)
1479 struct usb_device_request req;
1480 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1482 req.bmRequestType = UT_READ_CLASS_DEVICE;
1483 req.bRequest = UR_GET_DESCRIPTOR;
1484 USETW2(req.wValue, UDESC_HUB, 0);
1485 USETW(req.wIndex, 0);
1486 USETW(req.wLength, len);
1487 return (usbd_do_request(udev, lock, &req, hd));
1490 /*------------------------------------------------------------------------*
1491 * usbd_req_get_ss_hub_descriptor
1496 *------------------------------------------------------------------------*/
1498 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct lock *lock,
1499 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1501 struct usb_device_request req;
1502 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1504 req.bmRequestType = UT_READ_CLASS_DEVICE;
1505 req.bRequest = UR_GET_DESCRIPTOR;
1506 USETW2(req.wValue, UDESC_SS_HUB, 0);
1507 USETW(req.wIndex, 0);
1508 USETW(req.wLength, len);
1509 return (usbd_do_request(udev, lock, &req, hd));
1512 /*------------------------------------------------------------------------*
1513 * usbd_req_get_hub_status
1518 *------------------------------------------------------------------------*/
1520 usbd_req_get_hub_status(struct usb_device *udev, struct lock *lock,
1521 struct usb_hub_status *st)
1523 struct usb_device_request req;
1525 req.bmRequestType = UT_READ_CLASS_DEVICE;
1526 req.bRequest = UR_GET_STATUS;
1527 USETW(req.wValue, 0);
1528 USETW(req.wIndex, 0);
1529 USETW(req.wLength, sizeof(struct usb_hub_status));
1530 return (usbd_do_request(udev, lock, &req, st));
1533 /*------------------------------------------------------------------------*
1534 * usbd_req_set_address
1536 * This function is used to set the address for an USB device. After
1537 * port reset the USB device will respond at address zero.
1542 *------------------------------------------------------------------------*/
1544 usbd_req_set_address(struct usb_device *udev, struct lock *lock, uint16_t addr)
1546 struct usb_device_request req;
1549 DPRINTFN(6, "setting device address=%d\n", addr);
1551 req.bmRequestType = UT_WRITE_DEVICE;
1552 req.bRequest = UR_SET_ADDRESS;
1553 USETW(req.wValue, addr);
1554 USETW(req.wIndex, 0);
1555 USETW(req.wLength, 0);
1557 err = USB_ERR_INVAL;
1559 /* check if USB controller handles set address */
1560 if (udev->bus->methods->set_address != NULL)
1561 err = (udev->bus->methods->set_address) (udev, lock, addr);
1563 if (err != USB_ERR_INVAL)
1566 /* Setting the address should not take more than 1 second ! */
1567 err = usbd_do_request_flags(udev, lock, &req, NULL,
1568 USB_DELAY_STATUS_STAGE, NULL, 1000);
1571 /* allow device time to set new address */
1573 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1578 /*------------------------------------------------------------------------*
1579 * usbd_req_get_port_status
1584 *------------------------------------------------------------------------*/
1586 usbd_req_get_port_status(struct usb_device *udev, struct lock *lock,
1587 struct usb_port_status *ps, uint8_t port)
1589 struct usb_device_request req;
1591 req.bmRequestType = UT_READ_CLASS_OTHER;
1592 req.bRequest = UR_GET_STATUS;
1593 USETW(req.wValue, 0);
1594 req.wIndex[0] = port;
1596 USETW(req.wLength, sizeof *ps);
1597 return (usbd_do_request(udev, lock, &req, ps));
1600 /*------------------------------------------------------------------------*
1601 * usbd_req_clear_hub_feature
1606 *------------------------------------------------------------------------*/
1608 usbd_req_clear_hub_feature(struct usb_device *udev, struct lock *lock,
1611 struct usb_device_request req;
1613 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1614 req.bRequest = UR_CLEAR_FEATURE;
1615 USETW(req.wValue, sel);
1616 USETW(req.wIndex, 0);
1617 USETW(req.wLength, 0);
1618 return (usbd_do_request(udev, lock, &req, 0));
1621 /*------------------------------------------------------------------------*
1622 * usbd_req_set_hub_feature
1627 *------------------------------------------------------------------------*/
1629 usbd_req_set_hub_feature(struct usb_device *udev, struct lock *lock,
1632 struct usb_device_request req;
1634 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1635 req.bRequest = UR_SET_FEATURE;
1636 USETW(req.wValue, sel);
1637 USETW(req.wIndex, 0);
1638 USETW(req.wLength, 0);
1639 return (usbd_do_request(udev, lock, &req, 0));
1642 /*------------------------------------------------------------------------*
1643 * usbd_req_set_hub_u1_timeout
1648 *------------------------------------------------------------------------*/
1650 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct lock *lock,
1651 uint8_t port, uint8_t timeout)
1653 struct usb_device_request req;
1655 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1656 req.bRequest = UR_SET_FEATURE;
1657 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1658 req.wIndex[0] = port;
1659 req.wIndex[1] = timeout;
1660 USETW(req.wLength, 0);
1661 return (usbd_do_request(udev, lock, &req, 0));
1664 /*------------------------------------------------------------------------*
1665 * usbd_req_set_hub_u2_timeout
1670 *------------------------------------------------------------------------*/
1672 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct lock *lock,
1673 uint8_t port, uint8_t timeout)
1675 struct usb_device_request req;
1677 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1678 req.bRequest = UR_SET_FEATURE;
1679 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1680 req.wIndex[0] = port;
1681 req.wIndex[1] = timeout;
1682 USETW(req.wLength, 0);
1683 return (usbd_do_request(udev, lock, &req, 0));
1686 /*------------------------------------------------------------------------*
1687 * usbd_req_set_hub_depth
1692 *------------------------------------------------------------------------*/
1694 usbd_req_set_hub_depth(struct usb_device *udev, struct lock *lock,
1697 struct usb_device_request req;
1699 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1700 req.bRequest = UR_SET_HUB_DEPTH;
1701 USETW(req.wValue, depth);
1702 USETW(req.wIndex, 0);
1703 USETW(req.wLength, 0);
1704 return (usbd_do_request(udev, lock, &req, 0));
1707 /*------------------------------------------------------------------------*
1708 * usbd_req_clear_port_feature
1713 *------------------------------------------------------------------------*/
1715 usbd_req_clear_port_feature(struct usb_device *udev, struct lock *lock,
1716 uint8_t port, uint16_t sel)
1718 struct usb_device_request req;
1720 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1721 req.bRequest = UR_CLEAR_FEATURE;
1722 USETW(req.wValue, sel);
1723 req.wIndex[0] = port;
1725 USETW(req.wLength, 0);
1726 return (usbd_do_request(udev, lock, &req, 0));
1729 /*------------------------------------------------------------------------*
1730 * usbd_req_set_port_feature
1735 *------------------------------------------------------------------------*/
1737 usbd_req_set_port_feature(struct usb_device *udev, struct lock *lock,
1738 uint8_t port, uint16_t sel)
1740 struct usb_device_request req;
1742 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1743 req.bRequest = UR_SET_FEATURE;
1744 USETW(req.wValue, sel);
1745 req.wIndex[0] = port;
1747 USETW(req.wLength, 0);
1748 return (usbd_do_request(udev, lock, &req, 0));
1751 /*------------------------------------------------------------------------*
1752 * usbd_req_set_protocol
1757 *------------------------------------------------------------------------*/
1759 usbd_req_set_protocol(struct usb_device *udev, struct lock *lock,
1760 uint8_t iface_index, uint16_t report)
1762 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1763 struct usb_device_request req;
1765 if ((iface == NULL) || (iface->idesc == NULL)) {
1766 return (USB_ERR_INVAL);
1768 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1769 iface, report, iface->idesc->bInterfaceNumber);
1771 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1772 req.bRequest = UR_SET_PROTOCOL;
1773 USETW(req.wValue, report);
1774 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1776 USETW(req.wLength, 0);
1777 return (usbd_do_request(udev, lock, &req, 0));
1780 /*------------------------------------------------------------------------*
1781 * usbd_req_set_report
1786 *------------------------------------------------------------------------*/
1788 usbd_req_set_report(struct usb_device *udev, struct lock *lock, void *data, uint16_t len,
1789 uint8_t iface_index, uint8_t type, uint8_t id)
1791 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1792 struct usb_device_request req;
1794 if ((iface == NULL) || (iface->idesc == NULL)) {
1795 return (USB_ERR_INVAL);
1797 DPRINTFN(5, "len=%d\n", len);
1799 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1800 req.bRequest = UR_SET_REPORT;
1801 USETW2(req.wValue, type, id);
1802 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1804 USETW(req.wLength, len);
1805 return (usbd_do_request(udev, lock, &req, data));
1808 /*------------------------------------------------------------------------*
1809 * usbd_req_get_report
1814 *------------------------------------------------------------------------*/
1816 usbd_req_get_report(struct usb_device *udev, struct lock *lock, void *data,
1817 uint16_t len, 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_READ_CLASS_INTERFACE;
1828 req.bRequest = UR_GET_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, lock, &req, data));
1836 /*------------------------------------------------------------------------*
1842 *------------------------------------------------------------------------*/
1844 usbd_req_set_idle(struct usb_device *udev, struct lock *lock,
1845 uint8_t iface_index, uint8_t duration, 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, "%d %d\n", duration, id);
1855 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1856 req.bRequest = UR_SET_IDLE;
1857 USETW2(req.wValue, duration, id);
1858 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1860 USETW(req.wLength, 0);
1861 return (usbd_do_request(udev, lock, &req, 0));
1864 /*------------------------------------------------------------------------*
1865 * usbd_req_get_report_descriptor
1870 *------------------------------------------------------------------------*/
1872 usbd_req_get_report_descriptor(struct usb_device *udev, struct lock *lock,
1873 void *d, uint16_t size, uint8_t iface_index)
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 req.bmRequestType = UT_READ_INTERFACE;
1882 req.bRequest = UR_GET_DESCRIPTOR;
1883 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1884 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1886 USETW(req.wLength, size);
1887 return (usbd_do_request(udev, lock, &req, d));
1890 /*------------------------------------------------------------------------*
1891 * usbd_req_set_config
1893 * This function is used to select the current configuration number in
1894 * both USB device side mode and USB host side mode. When setting the
1895 * configuration the function of the interfaces can change.
1900 *------------------------------------------------------------------------*/
1902 usbd_req_set_config(struct usb_device *udev, struct lock *lock, uint8_t conf)
1904 struct usb_device_request req;
1906 DPRINTF("setting config %d\n", conf);
1908 /* do "set configuration" request */
1910 req.bmRequestType = UT_WRITE_DEVICE;
1911 req.bRequest = UR_SET_CONFIG;
1912 req.wValue[0] = conf;
1914 USETW(req.wIndex, 0);
1915 USETW(req.wLength, 0);
1916 return (usbd_do_request(udev, lock, &req, 0));
1919 /*------------------------------------------------------------------------*
1920 * usbd_req_get_config
1925 *------------------------------------------------------------------------*/
1927 usbd_req_get_config(struct usb_device *udev, struct lock *lock, uint8_t *pconf)
1929 struct usb_device_request req;
1931 req.bmRequestType = UT_READ_DEVICE;
1932 req.bRequest = UR_GET_CONFIG;
1933 USETW(req.wValue, 0);
1934 USETW(req.wIndex, 0);
1935 USETW(req.wLength, 1);
1936 return (usbd_do_request(udev, lock, &req, pconf));
1939 /*------------------------------------------------------------------------*
1940 * usbd_setup_device_desc
1941 *------------------------------------------------------------------------*/
1943 usbd_setup_device_desc(struct usb_device *udev, struct lock *lock)
1948 * Get the first 8 bytes of the device descriptor !
1950 * NOTE: "usbd_do_request()" will check the device descriptor
1951 * next time we do a request to see if the maximum packet size
1952 * changed! The 8 first bytes of the device descriptor
1953 * contains the maximum packet size to use on control endpoint
1954 * 0. If this value is different from "USB_MAX_IPACKET" a new
1955 * USB control request will be setup!
1957 switch (udev->speed) {
1958 case USB_SPEED_FULL:
1960 err = usbd_req_get_desc(udev, lock, NULL, &udev->ddesc,
1961 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1963 DPRINTFN(0, "getting device descriptor "
1964 "at addr %d failed, %s\n", udev->address,
1970 DPRINTF("Minimum MaxPacketSize is large enough "
1971 "to hold the complete device descriptor\n");
1975 /* get the full device descriptor */
1976 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1978 /* try one more time, if error */
1980 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1983 DPRINTF("addr=%d, getting full desc failed\n",
1988 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1989 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1990 udev->address, UGETW(udev->ddesc.bcdUSB),
1991 udev->ddesc.bDeviceClass,
1992 udev->ddesc.bDeviceSubClass,
1993 udev->ddesc.bDeviceProtocol,
1994 udev->ddesc.bMaxPacketSize,
1995 udev->ddesc.bLength,
2001 /*------------------------------------------------------------------------*
2002 * usbd_req_re_enumerate
2004 * NOTE: After this function returns the hardware is in the
2005 * unconfigured state! The application is responsible for setting a
2006 * new configuration.
2011 *------------------------------------------------------------------------*/
2013 usbd_req_re_enumerate(struct usb_device *udev, struct lock *lock)
2015 struct usb_device *parent_hub;
2018 uint8_t do_retry = 1;
2020 if (udev->flags.usb_mode != USB_MODE_HOST) {
2021 return (USB_ERR_INVAL);
2023 old_addr = udev->address;
2024 parent_hub = udev->parent_hub;
2025 if (parent_hub == NULL) {
2026 return (USB_ERR_INVAL);
2030 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2031 * speed device, if any.
2033 if (udev->parent_hs_hub != NULL &&
2034 udev->speed != USB_SPEED_HIGH) {
2035 DPRINTF("Trying to reset parent High Speed TT.\n");
2036 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
2039 DPRINTF("Resetting parent High "
2040 "Speed TT failed (%s).\n",
2045 /* Try to warm reset first */
2046 if (parent_hub->speed == USB_SPEED_SUPER)
2047 usbd_req_warm_reset_port(parent_hub, lock, udev->port_no);
2049 /* Try to reset the parent HUB port. */
2050 err = usbd_req_reset_port(parent_hub, lock, udev->port_no);
2052 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2053 old_addr, usbd_errstr(err));
2058 * After that the port has been reset our device should be at
2061 udev->address = USB_START_ADDR;
2063 /* reset "bMaxPacketSize" */
2064 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2066 /* reset USB state */
2067 usb_set_device_state(udev, USB_STATE_POWERED);
2070 * Restore device address:
2072 err = usbd_req_set_address(udev, lock, old_addr);
2074 /* XXX ignore any errors! */
2075 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2076 old_addr, usbd_errstr(err));
2079 * Restore device address, if the controller driver did not
2082 if (udev->address == USB_START_ADDR)
2083 udev->address = old_addr;
2085 /* setup the device descriptor and the initial "wMaxPacketSize" */
2086 err = usbd_setup_device_desc(udev, lock);
2089 if (err && do_retry) {
2090 /* give the USB firmware some time to load */
2091 usb_pause_mtx(lock, hz / 2);
2092 /* no more retries after this retry */
2097 /* restore address */
2098 if (udev->address == USB_START_ADDR)
2099 udev->address = old_addr;
2100 /* update state, if successful */
2102 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2106 /*------------------------------------------------------------------------*
2107 * usbd_req_clear_device_feature
2112 *------------------------------------------------------------------------*/
2114 usbd_req_clear_device_feature(struct usb_device *udev, struct lock *lock,
2117 struct usb_device_request req;
2119 req.bmRequestType = UT_WRITE_DEVICE;
2120 req.bRequest = UR_CLEAR_FEATURE;
2121 USETW(req.wValue, sel);
2122 USETW(req.wIndex, 0);
2123 USETW(req.wLength, 0);
2124 return (usbd_do_request(udev, lock, &req, 0));
2127 /*------------------------------------------------------------------------*
2128 * usbd_req_set_device_feature
2133 *------------------------------------------------------------------------*/
2135 usbd_req_set_device_feature(struct usb_device *udev, struct lock *lock,
2138 struct usb_device_request req;
2140 req.bmRequestType = UT_WRITE_DEVICE;
2141 req.bRequest = UR_SET_FEATURE;
2142 USETW(req.wValue, sel);
2143 USETW(req.wIndex, 0);
2144 USETW(req.wLength, 0);
2145 return (usbd_do_request(udev, lock, &req, 0));
2148 /*------------------------------------------------------------------------*
2154 *------------------------------------------------------------------------*/
2156 usbd_req_reset_tt(struct usb_device *udev, struct lock *lock,
2159 struct usb_device_request req;
2161 /* For single TT HUBs the port should be 1 */
2163 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2164 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2167 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2168 req.bRequest = UR_RESET_TT;
2169 USETW(req.wValue, 0);
2170 req.wIndex[0] = port;
2172 USETW(req.wLength, 0);
2173 return (usbd_do_request(udev, lock, &req, 0));
2176 /*------------------------------------------------------------------------*
2177 * usbd_req_clear_tt_buffer
2179 * For single TT HUBs the port should be 1.
2184 *------------------------------------------------------------------------*/
2186 usbd_req_clear_tt_buffer(struct usb_device *udev, struct lock *lock,
2187 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2189 struct usb_device_request req;
2192 /* For single TT HUBs the port should be 1 */
2194 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2195 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2198 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2199 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2201 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2202 req.bRequest = UR_CLEAR_TT_BUFFER;
2203 USETW(req.wValue, wValue);
2204 req.wIndex[0] = port;
2206 USETW(req.wLength, 0);
2207 return (usbd_do_request(udev, lock, &req, 0));
2210 /*------------------------------------------------------------------------*
2211 * usbd_req_set_port_link_state
2213 * USB 3.0 specific request
2218 *------------------------------------------------------------------------*/
2220 usbd_req_set_port_link_state(struct usb_device *udev, struct lock *lock,
2221 uint8_t port, uint8_t link_state)
2223 struct usb_device_request req;
2225 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2226 req.bRequest = UR_SET_FEATURE;
2227 USETW(req.wValue, UHF_PORT_LINK_STATE);
2228 req.wIndex[0] = port;
2229 req.wIndex[1] = link_state;
2230 USETW(req.wLength, 0);
2231 return (usbd_do_request(udev, lock, &req, 0));