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 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/usbhid.h>
56 #define USB_DEBUG_VAR usb_debug
58 #include <dev/usb/usb_core.h>
59 #include <dev/usb/usb_busdma.h>
60 #include <dev/usb/usb_request.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_transfer.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_device.h>
65 #include <dev/usb/usb_util.h>
66 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_controller.h>
69 #include <dev/usb/usb_bus.h>
70 #include <sys/ctype.h>
71 #endif /* USB_GLOBAL_INCLUDE_FILE */
73 static int usb_no_cs_fail;
75 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
76 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
78 static int usb_full_ddesc;
80 SYSCTL_INT(_hw_usb, OID_AUTO, full_ddesc, CTLFLAG_RW,
81 &usb_full_ddesc, 0, "USB always read complete device descriptor, if set");
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 status 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;
234 udev = xfer->xroot->udev;
236 USB_BUS_LOCK(udev->bus);
238 /* round robin endpoint clear stall */
241 ep_end = udev->endpoints + udev->endpoints_max;
242 ep_first = udev->endpoints;
243 to = udev->endpoints_max;
245 switch (USB_GET_STATE(xfer)) {
246 case USB_ST_TRANSFERRED:
248 /* reset error counter */
249 udev->clear_stall_errors = 0;
252 goto tr_setup; /* device was unconfigured */
257 /* some hardware needs a callback to clear the data toggle */
258 usbd_clear_stall_locked(udev, ep);
259 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
260 /* start the current or next transfer, if any */
261 usb_command_wrapper(&ep->endpoint_q[x],
262 ep->endpoint_q[x].curr);
270 break; /* no endpoints - nothing to do */
271 if ((ep < ep_first) || (ep >= ep_end))
272 ep = ep_first; /* endpoint wrapped around */
276 /* setup a clear-stall packet */
278 req.bmRequestType = UT_WRITE_ENDPOINT;
279 req.bRequest = UR_CLEAR_FEATURE;
280 USETW(req.wValue, UF_ENDPOINT_HALT);
281 req.wIndex[0] = ep->edesc->bEndpointAddress;
283 USETW(req.wLength, 0);
285 /* copy in the transfer */
287 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
290 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
292 USB_BUS_UNLOCK(udev->bus);
294 usbd_transfer_submit(xfer);
296 USB_BUS_LOCK(udev->bus);
304 if (error == USB_ERR_CANCELLED)
307 DPRINTF("Clear stall failed.\n");
310 * Some VMs like VirtualBox always return failure on
311 * clear-stall which we sometimes should just ignore.
315 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
318 if (error == USB_ERR_TIMEOUT) {
319 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
320 DPRINTF("Trying to re-enumerate.\n");
321 usbd_start_re_enumerate(udev);
323 udev->clear_stall_errors++;
324 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
325 DPRINTF("Trying to re-enumerate.\n");
326 usbd_start_re_enumerate(udev);
332 /* store current endpoint */
334 USB_BUS_UNLOCK(udev->bus);
337 static usb_handle_req_t *
338 usbd_get_hr_func(struct usb_device *udev)
340 /* figure out if there is a Handle Request function */
341 if (udev->flags.usb_mode == USB_MODE_DEVICE)
342 return (usb_temp_get_desc_p);
343 else if (udev->parent_hub == NULL)
344 return (udev->bus->methods->roothub_exec);
349 /*------------------------------------------------------------------------*
350 * usbd_do_request_flags and usbd_do_request
352 * Description of arguments passed to these functions:
354 * "udev" - this is the "usb_device" structure pointer on which the
355 * request should be performed. It is possible to call this function
356 * in both Host Side mode and Device Side mode.
358 * "mtx" - if this argument is non-NULL the mutex pointed to by it
359 * will get dropped and picked up during the execution of this
360 * function, hence this function sometimes needs to sleep. If this
361 * argument is NULL it has no effect.
363 * "req" - this argument must always be non-NULL and points to an
364 * 8-byte structure holding the USB request to be done. The USB
365 * request structure has a bit telling the direction of the USB
366 * request, if it is a read or a write.
368 * "data" - if the "wLength" part of the structure pointed to by "req"
369 * is non-zero this argument must point to a valid kernel buffer which
370 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
373 * "flags" - here is a list of valid flags:
375 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
378 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
379 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
380 * sysctl. This flag is mostly useful for debugging.
382 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
385 * "actlen" - if non-NULL the actual transfer length will be stored in
386 * the 16-bit unsigned integer pointed to by "actlen". This
387 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
390 * "timeout" - gives the timeout for the control transfer in
391 * milliseconds. A "timeout" value less than 50 milliseconds is
392 * treated like a 50 millisecond timeout. A "timeout" value greater
393 * than 30 seconds is treated like a 30 second timeout. This USB stack
394 * does not allow control requests without a timeout.
396 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
397 * will be serialized by the use of the USB device enumeration lock.
402 *------------------------------------------------------------------------*/
404 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
405 struct usb_device_request *req, void *data, uint16_t flags,
406 uint16_t *actlen, usb_timeout_t timeout)
409 struct usb_ctrl_debug_bits dbg;
411 usb_handle_req_t *hr_func;
412 struct usb_xfer *xfer;
415 usb_ticks_t start_ticks;
416 usb_ticks_t delta_ticks;
417 usb_ticks_t max_ticks;
424 /* timeout is too small */
427 if (timeout > 30000) {
428 /* timeout is too big */
431 length = UGETW(req->wLength);
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 * Serialize access to this function:
465 do_unlock = usbd_ctrl_lock(udev);
467 hr_func = usbd_get_hr_func(udev);
469 if (hr_func != NULL) {
470 DPRINTF("Handle Request function is set\n");
475 if (!(req->bmRequestType & UT_READ)) {
477 DPRINTFN(1, "The handle request function "
478 "does not support writing data!\n");
484 /* The root HUB code needs the BUS lock locked */
486 USB_BUS_LOCK(udev->bus);
487 err = (hr_func) (udev, req, &desc, &temp);
488 USB_BUS_UNLOCK(udev->bus);
494 if (!(flags & USB_SHORT_XFER_OK)) {
495 err = USB_ERR_SHORT_XFER;
505 if (flags & USB_USER_DATA_PTR) {
506 if (copyout(desc, data, length)) {
512 memcpy(data, desc, length);
514 goto done; /* success */
518 * Setup a new USB transfer or use the existing one, if any:
520 usbd_ctrl_transfer_setup(udev);
522 xfer = udev->ctrl_xfer[0];
524 /* most likely out of memory */
531 usbd_get_debug_bits(udev, req, &dbg);
533 /* Check for fault injection */
535 flags |= USB_DELAY_STATUS_STAGE;
539 if (flags & USB_DELAY_STATUS_STAGE)
540 xfer->flags.manual_status = 1;
542 xfer->flags.manual_status = 0;
544 if (flags & USB_SHORT_XFER_OK)
545 xfer->flags.short_xfer_ok = 1;
547 xfer->flags.short_xfer_ok = 0;
549 xfer->timeout = timeout;
553 max_ticks = USB_MS_TO_TICKS(timeout);
555 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
557 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
561 if (temp > usbd_xfer_max_len(xfer)) {
562 temp = usbd_xfer_max_len(xfer);
565 if (xfer->flags.manual_status) {
566 if (usbd_xfer_frame_len(xfer, 0) != 0) {
567 /* Execute data stage separately */
569 } else if (temp > 0) {
574 if (dbg.ds_delay > 0) {
576 xfer->xroot->xfer_mtx,
577 USB_MS_TO_TICKS(dbg.ds_delay));
578 /* make sure we don't time out */
584 usbd_xfer_set_frame_len(xfer, 1, temp);
587 if (!(req->bmRequestType & UT_READ)) {
589 if (flags & USB_USER_DATA_PTR) {
590 USB_XFER_UNLOCK(xfer);
591 err = usbd_copy_in_user(xfer->frbuffers + 1,
600 usbd_copy_in(xfer->frbuffers + 1,
603 usbd_xfer_set_frames(xfer, 2);
605 if (usbd_xfer_frame_len(xfer, 0) == 0) {
606 if (xfer->flags.manual_status) {
612 if (dbg.ss_delay > 0) {
614 xfer->xroot->xfer_mtx,
615 USB_MS_TO_TICKS(dbg.ss_delay));
616 /* make sure we don't time out */
620 xfer->flags.manual_status = 0;
625 usbd_xfer_set_frames(xfer, 1);
628 usbd_transfer_start(xfer);
630 while (usbd_transfer_pending(xfer)) {
631 cv_wait(&udev->ctrlreq_cv,
632 xfer->xroot->xfer_mtx);
641 /* get actual length of DATA stage */
643 if (xfer->aframes < 2) {
646 acttemp = usbd_xfer_frame_len(xfer, 1);
649 /* check for short packet */
651 if (temp > acttemp) {
656 if (req->bmRequestType & UT_READ) {
658 if (flags & USB_USER_DATA_PTR) {
659 USB_XFER_UNLOCK(xfer);
660 err = usbd_copy_out_user(xfer->frbuffers + 1,
669 usbd_copy_out(xfer->frbuffers + 1,
674 * Clear "frlengths[0]" so that we don't send the setup
677 usbd_xfer_set_frame_len(xfer, 0, 0);
679 /* update length and data pointer */
681 data = USB_ADD_BYTES(data, temp);
686 /* check for timeout */
688 delta_ticks = ticks - start_ticks;
689 if (delta_ticks > max_ticks) {
691 err = USB_ERR_TIMEOUT;
701 * Make sure that the control endpoint is no longer
702 * blocked in case of a non-transfer related error:
704 usbd_transfer_stop(xfer);
706 USB_XFER_UNLOCK(xfer);
710 usbd_ctrl_unlock(udev);
712 if ((mtx != NULL) && (mtx != &Giant))
716 case USB_ERR_NORMAL_COMPLETION:
717 case USB_ERR_SHORT_XFER:
718 case USB_ERR_STALLED:
719 case USB_ERR_CANCELLED:
722 DPRINTF("I/O error - waiting a bit for TT cleanup\n");
723 usb_pause_mtx(mtx, hz / 16);
726 return ((usb_error_t)err);
729 /*------------------------------------------------------------------------*
730 * usbd_do_request_proc - factored out code
732 * This function is factored out code. It does basically the same like
733 * usbd_do_request_flags, except it will check the status of the
734 * passed process argument before doing the USB request. If the
735 * process is draining the USB_ERR_IOERROR code will be returned. It
736 * is assumed that the mutex associated with the process is locked
737 * when calling this function.
738 *------------------------------------------------------------------------*/
740 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
741 struct usb_device_request *req, void *data, uint16_t flags,
742 uint16_t *actlen, usb_timeout_t timeout)
747 /* get request data length */
748 len = UGETW(req->wLength);
750 /* check if the device is being detached */
751 if (usb_proc_is_gone(pproc)) {
752 err = USB_ERR_IOERROR;
756 /* forward the USB request */
757 err = usbd_do_request_flags(udev, pproc->up_mtx,
758 req, data, flags, actlen, timeout);
761 /* on failure we zero the data */
762 /* on short packet we zero the unused data */
763 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
765 memset(data, 0, len);
766 else if (actlen && *actlen != len)
767 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
772 /*------------------------------------------------------------------------*
773 * usbd_req_reset_port
775 * This function will instruct a USB HUB to perform a reset sequence
776 * on the specified port number.
779 * 0: Success. The USB device should now be at address zero.
780 * Else: Failure. No USB device is present and the USB port should be
782 *------------------------------------------------------------------------*/
784 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
786 struct usb_port_status ps;
794 /* clear any leftover port reset changes first */
795 usbd_req_clear_port_feature(
796 udev, mtx, port, UHF_C_PORT_RESET);
798 /* assert port reset on the given port */
799 err = usbd_req_set_port_feature(
800 udev, mtx, port, UHF_PORT_RESET);
802 /* check for errors */
807 /* wait for the device to recover from reset */
808 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
809 n += usb_port_reset_delay;
810 err = usbd_req_get_port_status(udev, mtx, &ps, port);
814 status = UGETW(ps.wPortStatus);
815 change = UGETW(ps.wPortChange);
817 /* if the device disappeared, just give up */
818 if (!(status & UPS_CURRENT_CONNECT_STATUS))
821 /* check if reset is complete */
822 if (change & UPS_C_PORT_RESET)
826 * Some Virtual Machines like VirtualBox 4.x fail to
827 * generate a port reset change event. Check if reset
828 * is no longer asserted.
830 if (!(status & UPS_RESET))
833 /* check for timeout */
840 /* clear port reset first */
841 err = usbd_req_clear_port_feature(
842 udev, mtx, port, UHF_C_PORT_RESET);
846 /* check for timeout */
848 err = USB_ERR_TIMEOUT;
851 /* wait for the device to recover from reset */
852 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
855 DPRINTFN(2, "port %d reset returning error=%s\n",
856 port, usbd_errstr(err));
860 /*------------------------------------------------------------------------*
861 * usbd_req_warm_reset_port
863 * This function will instruct an USB HUB to perform a warm reset
864 * sequence on the specified port number. This kind of reset is not
865 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
866 * for SUPER-speed USB HUBs.
869 * 0: Success. The USB device should now be available again.
870 * Else: Failure. No USB device is present and the USB port should be
872 *------------------------------------------------------------------------*/
874 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
877 struct usb_port_status ps;
885 err = usbd_req_get_port_status(udev, mtx, &ps, port);
889 status = UGETW(ps.wPortStatus);
891 switch (UPS_PORT_LINK_STATE_GET(status)) {
893 case UPS_PORT_LS_COMP_MODE:
894 case UPS_PORT_LS_LOOPBACK:
895 case UPS_PORT_LS_SS_INA:
898 DPRINTF("Wrong state for warm reset\n");
902 /* clear any leftover warm port reset changes first */
903 usbd_req_clear_port_feature(udev, mtx,
904 port, UHF_C_BH_PORT_RESET);
906 /* set warm port reset */
907 err = usbd_req_set_port_feature(udev, mtx,
908 port, UHF_BH_PORT_RESET);
914 /* wait for the device to recover from reset */
915 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
916 n += usb_port_reset_delay;
917 err = usbd_req_get_port_status(udev, mtx, &ps, port);
921 status = UGETW(ps.wPortStatus);
922 change = UGETW(ps.wPortChange);
924 /* if the device disappeared, just give up */
925 if (!(status & UPS_CURRENT_CONNECT_STATUS))
928 /* check if reset is complete */
929 if (change & UPS_C_BH_PORT_RESET)
932 /* check for timeout */
939 /* clear port reset first */
940 err = usbd_req_clear_port_feature(
941 udev, mtx, port, UHF_C_BH_PORT_RESET);
945 /* check for timeout */
947 err = USB_ERR_TIMEOUT;
950 /* wait for the device to recover from reset */
951 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
954 DPRINTFN(2, "port %d warm reset returning error=%s\n",
955 port, usbd_errstr(err));
959 /*------------------------------------------------------------------------*
962 * This function can be used to retrieve USB descriptors. It contains
963 * some additional logic like zeroing of missing descriptor bytes and
964 * retrying an USB descriptor in case of failure. The "min_len"
965 * argument specifies the minimum descriptor length. The "max_len"
966 * argument specifies the maximum descriptor length. If the real
967 * descriptor length is less than the minimum length the missing
968 * byte(s) will be zeroed. The type field, the second byte of the USB
969 * descriptor, will get forced to the correct type. If the "actlen"
970 * pointer is non-NULL, the actual length of the transfer will get
971 * stored in the 16-bit unsigned integer which it is pointing to. The
972 * first byte of the descriptor will not get updated. If the "actlen"
973 * pointer is NULL the first byte of the descriptor will get updated
974 * to reflect the actual length instead. If "min_len" is not equal to
975 * "max_len" then this function will try to retrive the beginning of
976 * the descriptor and base the maximum length on the first byte of the
982 *------------------------------------------------------------------------*/
984 usbd_req_get_desc(struct usb_device *udev,
985 struct mtx *mtx, uint16_t *actlen, void *desc,
986 uint16_t min_len, uint16_t max_len,
987 uint16_t id, uint8_t type, uint8_t index,
990 struct usb_device_request req;
994 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
995 id, type, index, max_len);
997 req.bmRequestType = UT_READ_DEVICE;
998 req.bRequest = UR_GET_DESCRIPTOR;
999 USETW2(req.wValue, type, index);
1000 USETW(req.wIndex, id);
1004 if ((min_len < 2) || (max_len < 2)) {
1005 err = USB_ERR_INVAL;
1008 USETW(req.wLength, min_len);
1010 err = usbd_do_request_flags(udev, mtx, &req,
1011 desc, 0, NULL, 500 /* ms */);
1013 if (err != 0 && err != USB_ERR_TIMEOUT &&
1014 min_len != max_len) {
1015 /* clear descriptor data */
1016 memset(desc, 0, max_len);
1018 /* try to read full descriptor length */
1019 USETW(req.wLength, max_len);
1021 err = usbd_do_request_flags(udev, mtx, &req,
1022 desc, USB_SHORT_XFER_OK, NULL, 500 /* ms */);
1026 if (buf[0] > max_len)
1028 else if (buf[0] < 2)
1029 err = USB_ERR_INVAL;
1033 /* enforce descriptor type */
1045 usb_pause_mtx(mtx, hz / 5);
1050 if (min_len == max_len) {
1052 /* enforce correct length */
1053 if ((buf[0] > min_len) && (actlen == NULL))
1056 /* enforce correct type */
1063 if (max_len > buf[0]) {
1066 /* zero minimum data */
1068 while (min_len > max_len) {
1073 /* set new minimum length */
1078 if (actlen != NULL) {
1087 /*------------------------------------------------------------------------*
1088 * usbd_req_get_string_any
1090 * This function will return the string given by "string_index"
1091 * using the first language ID. The maximum length "len" includes
1092 * the terminating zero. The "len" argument should be twice as
1093 * big pluss 2 bytes, compared with the actual maximum string length !
1098 *------------------------------------------------------------------------*/
1100 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1101 uint16_t len, uint8_t string_index)
1112 /* should not happen */
1113 return (USB_ERR_NORMAL_COMPLETION);
1115 if (string_index == 0) {
1116 /* this is the language table */
1118 return (USB_ERR_INVAL);
1120 if (udev->flags.no_strings) {
1122 return (USB_ERR_STALLED);
1124 err = usbd_req_get_string_desc
1125 (udev, mtx, buf, len, udev->langid, string_index);
1130 temp = (uint8_t *)buf;
1133 /* string length is too short */
1135 return (USB_ERR_INVAL);
1137 /* reserve one byte for terminating zero */
1140 /* find maximum length */
1142 n = (temp[0] / 2) - 1;
1146 /* skip descriptor header */
1149 /* reset swap state */
1152 /* convert and filter */
1153 for (i = 0; (i != n); i++) {
1154 c = UGETW(temp + (2 * i));
1156 /* convert from Unicode, handle buggy strings */
1157 if (((c & 0xff00) == 0) && (swap & 1)) {
1158 /* Little Endian, default */
1161 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1166 /* silently skip bad character */
1171 * Filter by default - We only allow alphanumerical
1172 * and a few more to avoid any problems with scripts
1185 /* silently skip bad character */
1187 *s = 0; /* zero terminate resulting string */
1188 return (USB_ERR_NORMAL_COMPLETION);
1191 /*------------------------------------------------------------------------*
1192 * usbd_req_get_string_desc
1194 * If you don't know the language ID, consider using
1195 * "usbd_req_get_string_any()".
1200 *------------------------------------------------------------------------*/
1202 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1203 uint16_t max_len, uint16_t lang_id,
1204 uint8_t string_index)
1206 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1207 UDESC_STRING, string_index, 0));
1210 /*------------------------------------------------------------------------*
1211 * usbd_req_get_config_desc_ptr
1213 * This function is used in device side mode to retrieve the pointer
1214 * to the generated config descriptor. This saves allocating space for
1215 * an additional config descriptor when setting the configuration.
1220 *------------------------------------------------------------------------*/
1222 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1223 struct usb_config_descriptor **ppcd, uint16_t wValue)
1225 struct usb_device_request req;
1226 usb_handle_req_t *hr_func;
1231 req.bmRequestType = UT_READ_DEVICE;
1232 req.bRequest = UR_GET_DESCRIPTOR;
1233 USETW(req.wValue, wValue);
1234 USETW(req.wIndex, 0);
1235 USETW(req.wLength, 0);
1240 hr_func = usbd_get_hr_func(udev);
1242 if (hr_func == NULL)
1243 err = USB_ERR_INVAL;
1245 USB_BUS_LOCK(udev->bus);
1246 err = (hr_func) (udev, &req, &ptr, &len);
1247 USB_BUS_UNLOCK(udev->bus);
1252 else if (ptr == NULL)
1253 err = USB_ERR_INVAL;
1255 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1260 /*------------------------------------------------------------------------*
1261 * usbd_req_get_config_desc
1266 *------------------------------------------------------------------------*/
1268 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1269 struct usb_config_descriptor *d, uint8_t conf_index)
1273 DPRINTFN(4, "confidx=%d\n", conf_index);
1275 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1276 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1280 /* Extra sanity checking */
1281 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1282 err = USB_ERR_INVAL;
1288 /*------------------------------------------------------------------------*
1289 * usbd_alloc_config_desc
1291 * This function is used to allocate a zeroed configuration
1297 *------------------------------------------------------------------------*/
1299 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1301 if (size > USB_CONFIG_MAX) {
1302 DPRINTF("Configuration descriptor too big\n");
1305 #if (USB_HAVE_FIXED_CONFIG == 0)
1306 return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1308 memset(udev->config_data, 0, sizeof(udev->config_data));
1309 return (udev->config_data);
1313 /*------------------------------------------------------------------------*
1314 * usbd_alloc_config_desc
1316 * This function is used to free a configuration descriptor.
1317 *------------------------------------------------------------------------*/
1319 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1321 #if (USB_HAVE_FIXED_CONFIG == 0)
1322 free(ptr, M_USBDEV);
1326 /*------------------------------------------------------------------------*
1327 * usbd_req_get_config_desc_full
1329 * This function gets the complete USB configuration descriptor and
1330 * ensures that "wTotalLength" is correct. The returned configuration
1331 * descriptor is freed by calling "usbd_free_config_desc()".
1336 *------------------------------------------------------------------------*/
1338 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1339 struct usb_config_descriptor **ppcd, uint8_t index)
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, mtx, &cd, index);
1354 /* get full descriptor */
1355 len = UGETW(cd.wTotalLength);
1356 if (len < (uint32_t)sizeof(*cdesc)) {
1357 /* corrupt descriptor */
1358 return (USB_ERR_INVAL);
1359 } else if (len > USB_CONFIG_MAX) {
1360 DPRINTF("Configuration descriptor was truncated\n");
1361 len = USB_CONFIG_MAX;
1363 cdesc = usbd_alloc_config_desc(udev, len);
1365 return (USB_ERR_NOMEM);
1366 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1367 UDESC_CONFIG, index, 3);
1369 usbd_free_config_desc(udev, cdesc);
1372 /* make sure that the device is not fooling us: */
1373 USETW(cdesc->wTotalLength, len);
1377 return (0); /* success */
1380 /*------------------------------------------------------------------------*
1381 * usbd_req_get_device_desc
1386 *------------------------------------------------------------------------*/
1388 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1389 struct usb_device_descriptor *d)
1392 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1393 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1396 /*------------------------------------------------------------------------*
1397 * usbd_req_get_alt_interface_no
1402 *------------------------------------------------------------------------*/
1404 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1405 uint8_t *alt_iface_no, uint8_t iface_index)
1407 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1408 struct usb_device_request req;
1410 if ((iface == NULL) || (iface->idesc == NULL))
1411 return (USB_ERR_INVAL);
1413 req.bmRequestType = UT_READ_INTERFACE;
1414 req.bRequest = UR_GET_INTERFACE;
1415 USETW(req.wValue, 0);
1416 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1418 USETW(req.wLength, 1);
1419 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1422 /*------------------------------------------------------------------------*
1423 * usbd_req_set_alt_interface_no
1428 *------------------------------------------------------------------------*/
1430 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1431 uint8_t iface_index, uint8_t alt_no)
1433 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1434 struct usb_device_request req;
1436 if ((iface == NULL) || (iface->idesc == NULL))
1437 return (USB_ERR_INVAL);
1439 req.bmRequestType = UT_WRITE_INTERFACE;
1440 req.bRequest = UR_SET_INTERFACE;
1441 req.wValue[0] = alt_no;
1443 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1445 USETW(req.wLength, 0);
1446 return (usbd_do_request(udev, mtx, &req, 0));
1449 /*------------------------------------------------------------------------*
1450 * usbd_req_get_device_status
1455 *------------------------------------------------------------------------*/
1457 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1458 struct usb_status *st)
1460 struct usb_device_request req;
1462 req.bmRequestType = UT_READ_DEVICE;
1463 req.bRequest = UR_GET_STATUS;
1464 USETW(req.wValue, 0);
1465 USETW(req.wIndex, 0);
1466 USETW(req.wLength, sizeof(*st));
1467 return (usbd_do_request(udev, mtx, &req, st));
1470 /*------------------------------------------------------------------------*
1471 * usbd_req_get_hub_descriptor
1476 *------------------------------------------------------------------------*/
1478 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1479 struct usb_hub_descriptor *hd, uint8_t nports)
1481 struct usb_device_request req;
1482 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1484 req.bmRequestType = UT_READ_CLASS_DEVICE;
1485 req.bRequest = UR_GET_DESCRIPTOR;
1486 USETW2(req.wValue, UDESC_HUB, 0);
1487 USETW(req.wIndex, 0);
1488 USETW(req.wLength, len);
1489 return (usbd_do_request(udev, mtx, &req, hd));
1492 /*------------------------------------------------------------------------*
1493 * usbd_req_get_ss_hub_descriptor
1498 *------------------------------------------------------------------------*/
1500 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1501 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1503 struct usb_device_request req;
1504 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1506 req.bmRequestType = UT_READ_CLASS_DEVICE;
1507 req.bRequest = UR_GET_DESCRIPTOR;
1508 USETW2(req.wValue, UDESC_SS_HUB, 0);
1509 USETW(req.wIndex, 0);
1510 USETW(req.wLength, len);
1511 return (usbd_do_request(udev, mtx, &req, hd));
1514 /*------------------------------------------------------------------------*
1515 * usbd_req_get_hub_status
1520 *------------------------------------------------------------------------*/
1522 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1523 struct usb_hub_status *st)
1525 struct usb_device_request req;
1527 req.bmRequestType = UT_READ_CLASS_DEVICE;
1528 req.bRequest = UR_GET_STATUS;
1529 USETW(req.wValue, 0);
1530 USETW(req.wIndex, 0);
1531 USETW(req.wLength, sizeof(struct usb_hub_status));
1532 return (usbd_do_request(udev, mtx, &req, st));
1535 /*------------------------------------------------------------------------*
1536 * usbd_req_set_address
1538 * This function is used to set the address for an USB device. After
1539 * port reset the USB device will respond at address zero.
1544 *------------------------------------------------------------------------*/
1546 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1548 struct usb_device_request req;
1551 DPRINTFN(6, "setting device address=%d\n", addr);
1553 req.bmRequestType = UT_WRITE_DEVICE;
1554 req.bRequest = UR_SET_ADDRESS;
1555 USETW(req.wValue, addr);
1556 USETW(req.wIndex, 0);
1557 USETW(req.wLength, 0);
1559 err = USB_ERR_INVAL;
1561 /* check if USB controller handles set address */
1562 if (udev->bus->methods->set_address != NULL)
1563 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1565 if (err != USB_ERR_INVAL)
1568 /* Setting the address should not take more than 1 second ! */
1569 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1570 USB_DELAY_STATUS_STAGE, NULL, 1000);
1573 /* allow device time to set new address */
1575 USB_MS_TO_TICKS(usb_set_address_settle));
1580 /*------------------------------------------------------------------------*
1581 * usbd_req_get_port_status
1586 *------------------------------------------------------------------------*/
1588 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1589 struct usb_port_status *ps, uint8_t port)
1591 struct usb_device_request req;
1593 req.bmRequestType = UT_READ_CLASS_OTHER;
1594 req.bRequest = UR_GET_STATUS;
1595 USETW(req.wValue, 0);
1596 req.wIndex[0] = port;
1598 USETW(req.wLength, sizeof(*ps));
1600 return (usbd_do_request_flags(udev, mtx, &req, ps, 0, NULL, 1000));
1603 /*------------------------------------------------------------------------*
1604 * usbd_req_clear_hub_feature
1609 *------------------------------------------------------------------------*/
1611 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1614 struct usb_device_request req;
1616 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1617 req.bRequest = UR_CLEAR_FEATURE;
1618 USETW(req.wValue, sel);
1619 USETW(req.wIndex, 0);
1620 USETW(req.wLength, 0);
1621 return (usbd_do_request(udev, mtx, &req, 0));
1624 /*------------------------------------------------------------------------*
1625 * usbd_req_set_hub_feature
1630 *------------------------------------------------------------------------*/
1632 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1635 struct usb_device_request req;
1637 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1638 req.bRequest = UR_SET_FEATURE;
1639 USETW(req.wValue, sel);
1640 USETW(req.wIndex, 0);
1641 USETW(req.wLength, 0);
1642 return (usbd_do_request(udev, mtx, &req, 0));
1645 /*------------------------------------------------------------------------*
1646 * usbd_req_set_hub_u1_timeout
1651 *------------------------------------------------------------------------*/
1653 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1654 uint8_t port, uint8_t timeout)
1656 struct usb_device_request req;
1658 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1659 req.bRequest = UR_SET_FEATURE;
1660 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1661 req.wIndex[0] = port;
1662 req.wIndex[1] = timeout;
1663 USETW(req.wLength, 0);
1664 return (usbd_do_request(udev, mtx, &req, 0));
1667 /*------------------------------------------------------------------------*
1668 * usbd_req_set_hub_u2_timeout
1673 *------------------------------------------------------------------------*/
1675 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1676 uint8_t port, uint8_t timeout)
1678 struct usb_device_request req;
1680 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1681 req.bRequest = UR_SET_FEATURE;
1682 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1683 req.wIndex[0] = port;
1684 req.wIndex[1] = timeout;
1685 USETW(req.wLength, 0);
1686 return (usbd_do_request(udev, mtx, &req, 0));
1689 /*------------------------------------------------------------------------*
1690 * usbd_req_set_hub_depth
1695 *------------------------------------------------------------------------*/
1697 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1700 struct usb_device_request req;
1702 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1703 req.bRequest = UR_SET_HUB_DEPTH;
1704 USETW(req.wValue, depth);
1705 USETW(req.wIndex, 0);
1706 USETW(req.wLength, 0);
1707 return (usbd_do_request(udev, mtx, &req, 0));
1710 /*------------------------------------------------------------------------*
1711 * usbd_req_clear_port_feature
1716 *------------------------------------------------------------------------*/
1718 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1719 uint8_t port, uint16_t sel)
1721 struct usb_device_request req;
1723 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1724 req.bRequest = UR_CLEAR_FEATURE;
1725 USETW(req.wValue, sel);
1726 req.wIndex[0] = port;
1728 USETW(req.wLength, 0);
1729 return (usbd_do_request(udev, mtx, &req, 0));
1732 /*------------------------------------------------------------------------*
1733 * usbd_req_set_port_feature
1738 *------------------------------------------------------------------------*/
1740 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1741 uint8_t port, uint16_t sel)
1743 struct usb_device_request req;
1745 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1746 req.bRequest = UR_SET_FEATURE;
1747 USETW(req.wValue, sel);
1748 req.wIndex[0] = port;
1750 USETW(req.wLength, 0);
1751 return (usbd_do_request(udev, mtx, &req, 0));
1754 /*------------------------------------------------------------------------*
1755 * usbd_req_set_protocol
1760 *------------------------------------------------------------------------*/
1762 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1763 uint8_t iface_index, uint16_t report)
1765 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1766 struct usb_device_request req;
1768 if ((iface == NULL) || (iface->idesc == NULL)) {
1769 return (USB_ERR_INVAL);
1771 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1772 iface, report, iface->idesc->bInterfaceNumber);
1774 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1775 req.bRequest = UR_SET_PROTOCOL;
1776 USETW(req.wValue, report);
1777 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1779 USETW(req.wLength, 0);
1780 return (usbd_do_request(udev, mtx, &req, 0));
1783 /*------------------------------------------------------------------------*
1784 * usbd_req_set_report
1789 *------------------------------------------------------------------------*/
1791 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1792 uint8_t iface_index, uint8_t type, uint8_t id)
1794 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1795 struct usb_device_request req;
1797 if ((iface == NULL) || (iface->idesc == NULL)) {
1798 return (USB_ERR_INVAL);
1800 DPRINTFN(5, "len=%d\n", len);
1802 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1803 req.bRequest = UR_SET_REPORT;
1804 USETW2(req.wValue, type, id);
1805 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1807 USETW(req.wLength, len);
1808 return (usbd_do_request(udev, mtx, &req, data));
1811 /*------------------------------------------------------------------------*
1812 * usbd_req_get_report
1817 *------------------------------------------------------------------------*/
1819 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1820 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1822 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1823 struct usb_device_request req;
1825 if ((iface == NULL) || (iface->idesc == NULL)) {
1826 return (USB_ERR_INVAL);
1828 DPRINTFN(5, "len=%d\n", len);
1830 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1831 req.bRequest = UR_GET_REPORT;
1832 USETW2(req.wValue, type, id);
1833 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1835 USETW(req.wLength, len);
1836 return (usbd_do_request(udev, mtx, &req, data));
1839 /*------------------------------------------------------------------------*
1845 *------------------------------------------------------------------------*/
1847 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1848 uint8_t iface_index, uint8_t duration, uint8_t id)
1850 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1851 struct usb_device_request req;
1853 if ((iface == NULL) || (iface->idesc == NULL)) {
1854 return (USB_ERR_INVAL);
1856 DPRINTFN(5, "%d %d\n", duration, id);
1858 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1859 req.bRequest = UR_SET_IDLE;
1860 USETW2(req.wValue, duration, id);
1861 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1863 USETW(req.wLength, 0);
1864 return (usbd_do_request(udev, mtx, &req, 0));
1867 /*------------------------------------------------------------------------*
1868 * usbd_req_get_report_descriptor
1873 *------------------------------------------------------------------------*/
1875 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1876 void *d, uint16_t size, uint8_t iface_index)
1878 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1879 struct usb_device_request req;
1881 if ((iface == NULL) || (iface->idesc == NULL)) {
1882 return (USB_ERR_INVAL);
1884 req.bmRequestType = UT_READ_INTERFACE;
1885 req.bRequest = UR_GET_DESCRIPTOR;
1886 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1887 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1889 USETW(req.wLength, size);
1890 return (usbd_do_request(udev, mtx, &req, d));
1893 /*------------------------------------------------------------------------*
1894 * usbd_req_set_config
1896 * This function is used to select the current configuration number in
1897 * both USB device side mode and USB host side mode. When setting the
1898 * configuration the function of the interfaces can change.
1903 *------------------------------------------------------------------------*/
1905 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1907 struct usb_device_request req;
1909 DPRINTF("setting config %d\n", conf);
1911 /* do "set configuration" request */
1913 req.bmRequestType = UT_WRITE_DEVICE;
1914 req.bRequest = UR_SET_CONFIG;
1915 req.wValue[0] = conf;
1917 USETW(req.wIndex, 0);
1918 USETW(req.wLength, 0);
1919 return (usbd_do_request(udev, mtx, &req, 0));
1922 /*------------------------------------------------------------------------*
1923 * usbd_req_get_config
1928 *------------------------------------------------------------------------*/
1930 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1932 struct usb_device_request req;
1934 req.bmRequestType = UT_READ_DEVICE;
1935 req.bRequest = UR_GET_CONFIG;
1936 USETW(req.wValue, 0);
1937 USETW(req.wIndex, 0);
1938 USETW(req.wLength, 1);
1939 return (usbd_do_request(udev, mtx, &req, pconf));
1942 /*------------------------------------------------------------------------*
1943 * usbd_setup_device_desc
1944 *------------------------------------------------------------------------*/
1946 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1951 * Get the first 8 bytes of the device descriptor !
1953 * NOTE: "usbd_do_request()" will check the device descriptor
1954 * next time we do a request to see if the maximum packet size
1955 * changed! The 8 first bytes of the device descriptor
1956 * contains the maximum packet size to use on control endpoint
1957 * 0. If this value is different from "USB_MAX_IPACKET" a new
1958 * USB control request will be setup!
1960 switch (udev->speed) {
1961 case USB_SPEED_FULL:
1962 if (usb_full_ddesc != 0) {
1963 /* get full device descriptor */
1964 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1969 /* get partial device descriptor, some devices crash on this */
1970 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1971 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1975 /* get the full device descriptor */
1976 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1980 DPRINTF("Minimum bMaxPacketSize is large enough "
1981 "to hold the complete device descriptor or "
1982 "only one bMaxPacketSize choice\n");
1984 /* get the full device descriptor */
1985 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1987 /* try one more time, if error */
1989 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1994 DPRINTFN(0, "getting device descriptor "
1995 "at addr %d failed, %s\n", udev->address,
2000 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
2001 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
2002 udev->address, UGETW(udev->ddesc.bcdUSB),
2003 udev->ddesc.bDeviceClass,
2004 udev->ddesc.bDeviceSubClass,
2005 udev->ddesc.bDeviceProtocol,
2006 udev->ddesc.bMaxPacketSize,
2007 udev->ddesc.bLength,
2013 /*------------------------------------------------------------------------*
2014 * usbd_req_re_enumerate
2016 * NOTE: After this function returns the hardware is in the
2017 * unconfigured state! The application is responsible for setting a
2018 * new configuration.
2023 *------------------------------------------------------------------------*/
2025 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2027 struct usb_device *parent_hub;
2030 uint8_t do_retry = 1;
2032 if (udev->flags.usb_mode != USB_MODE_HOST) {
2033 return (USB_ERR_INVAL);
2035 old_addr = udev->address;
2036 parent_hub = udev->parent_hub;
2037 if (parent_hub == NULL) {
2038 return (USB_ERR_INVAL);
2041 #if USB_HAVE_TT_SUPPORT
2043 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2044 * speed device, if any.
2046 if (udev->parent_hs_hub != NULL &&
2047 udev->speed != USB_SPEED_HIGH) {
2048 DPRINTF("Trying to reset parent High Speed TT.\n");
2049 if (udev->parent_hs_hub == parent_hub &&
2050 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2051 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2052 /* we can reset the whole TT */
2053 err = usbd_req_reset_tt(parent_hub, NULL,
2056 /* only reset a particular device and endpoint */
2057 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2058 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2061 DPRINTF("Resetting parent High "
2062 "Speed TT failed (%s).\n",
2067 /* Try to warm reset first */
2068 if (parent_hub->speed == USB_SPEED_SUPER)
2069 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2071 /* Try to reset the parent HUB port. */
2072 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2074 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2075 old_addr, usbd_errstr(err));
2080 * After that the port has been reset our device should be at
2083 udev->address = USB_START_ADDR;
2085 /* reset "bMaxPacketSize" */
2086 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2088 /* reset USB state */
2089 usb_set_device_state(udev, USB_STATE_POWERED);
2092 * Restore device address:
2094 err = usbd_req_set_address(udev, mtx, old_addr);
2096 /* XXX ignore any errors! */
2097 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2098 old_addr, usbd_errstr(err));
2101 * Restore device address, if the controller driver did not
2104 if (udev->address == USB_START_ADDR)
2105 udev->address = old_addr;
2107 /* setup the device descriptor and the initial "wMaxPacketSize" */
2108 err = usbd_setup_device_desc(udev, mtx);
2111 if (err && do_retry) {
2112 /* give the USB firmware some time to load */
2113 usb_pause_mtx(mtx, hz / 2);
2114 /* no more retries after this retry */
2119 /* restore address */
2120 if (udev->address == USB_START_ADDR)
2121 udev->address = old_addr;
2122 /* update state, if successful */
2124 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2128 /*------------------------------------------------------------------------*
2129 * usbd_req_clear_device_feature
2134 *------------------------------------------------------------------------*/
2136 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2139 struct usb_device_request req;
2141 req.bmRequestType = UT_WRITE_DEVICE;
2142 req.bRequest = UR_CLEAR_FEATURE;
2143 USETW(req.wValue, sel);
2144 USETW(req.wIndex, 0);
2145 USETW(req.wLength, 0);
2146 return (usbd_do_request(udev, mtx, &req, 0));
2149 /*------------------------------------------------------------------------*
2150 * usbd_req_set_device_feature
2155 *------------------------------------------------------------------------*/
2157 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2160 struct usb_device_request req;
2162 req.bmRequestType = UT_WRITE_DEVICE;
2163 req.bRequest = UR_SET_FEATURE;
2164 USETW(req.wValue, sel);
2165 USETW(req.wIndex, 0);
2166 USETW(req.wLength, 0);
2167 return (usbd_do_request(udev, mtx, &req, 0));
2170 /*------------------------------------------------------------------------*
2176 *------------------------------------------------------------------------*/
2178 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2181 struct usb_device_request req;
2183 /* For single TT HUBs the port should be 1 */
2185 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2186 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2189 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2190 req.bRequest = UR_RESET_TT;
2191 USETW(req.wValue, 0);
2192 req.wIndex[0] = port;
2194 USETW(req.wLength, 0);
2195 return (usbd_do_request(udev, mtx, &req, 0));
2198 /*------------------------------------------------------------------------*
2199 * usbd_req_clear_tt_buffer
2201 * For single TT HUBs the port should be 1.
2206 *------------------------------------------------------------------------*/
2208 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2209 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2211 struct usb_device_request req;
2214 /* For single TT HUBs the port should be 1 */
2216 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2217 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2220 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2221 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2223 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2224 req.bRequest = UR_CLEAR_TT_BUFFER;
2225 USETW(req.wValue, wValue);
2226 req.wIndex[0] = port;
2228 USETW(req.wLength, 0);
2229 return (usbd_do_request(udev, mtx, &req, 0));
2232 /*------------------------------------------------------------------------*
2233 * usbd_req_set_port_link_state
2235 * USB 3.0 specific request
2240 *------------------------------------------------------------------------*/
2242 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2243 uint8_t port, uint8_t link_state)
2245 struct usb_device_request req;
2247 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2248 req.bRequest = UR_SET_FEATURE;
2249 USETW(req.wValue, UHF_PORT_LINK_STATE);
2250 req.wIndex[0] = port;
2251 req.wIndex[1] = link_state;
2252 USETW(req.wLength, 0);
2253 return (usbd_do_request(udev, mtx, &req, 0));
2256 /*------------------------------------------------------------------------*
2257 * usbd_req_set_lpm_info
2259 * USB 2.0 specific request for Link Power Management.
2263 * USB_ERR_PENDING_REQUESTS: NYET
2264 * USB_ERR_TIMEOUT: TIMEOUT
2265 * USB_ERR_STALL: STALL
2267 *------------------------------------------------------------------------*/
2269 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2270 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2272 struct usb_device_request req;
2276 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2277 req.bRequest = UR_SET_AND_TEST;
2278 USETW(req.wValue, UHF_PORT_L1);
2279 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2280 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2281 USETW(req.wLength, sizeof(buf));
2283 /* set default value in case of short transfer */
2286 err = usbd_do_request(udev, mtx, &req, buf);
2291 case 0x00: /* SUCCESS */
2293 case 0x10: /* NYET */
2294 err = USB_ERR_PENDING_REQUESTS;
2296 case 0x11: /* TIMEOUT */
2297 err = USB_ERR_TIMEOUT;
2299 case 0x30: /* STALL */
2300 err = USB_ERR_STALLED;
2302 default: /* reserved */
2303 err = USB_ERR_IOERROR;