3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usb_ioctl.h>
52 #include <dev/usb/usbhid.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_request.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_transfer.h>
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_util.h>
64 #include <dev/usb/usb_dynamic.h>
66 #include <dev/usb/usb_controller.h>
67 #include <dev/usb/usb_bus.h>
68 #include <sys/ctype.h>
71 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
72 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
74 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
75 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
76 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
77 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
80 /* The following structures are used in connection to fault injection. */
81 struct usb_ctrl_debug {
82 int bus_index; /* target bus */
83 int dev_index; /* target address */
84 int ds_fail; /* fail data stage */
85 int ss_fail; /* fail data stage */
86 int ds_delay; /* data stage delay in ms */
87 int ss_delay; /* status stage delay in ms */
88 int bmRequestType_value;
92 struct usb_ctrl_debug_bits {
100 /* The default is to disable fault injection. */
102 static struct usb_ctrl_debug usb_ctrl_debug = {
105 .bmRequestType_value = -1,
106 .bRequest_value = -1,
109 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
110 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
111 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
112 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
113 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
114 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
115 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
116 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
117 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
118 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
119 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
120 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
121 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
122 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
123 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
124 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
126 /*------------------------------------------------------------------------*
127 * usbd_get_debug_bits
129 * This function is only useful in USB host mode.
130 *------------------------------------------------------------------------*/
132 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
133 struct usb_ctrl_debug_bits *dbg)
137 memset(dbg, 0, sizeof(*dbg));
139 /* Compute data stage delay */
141 temp = usb_ctrl_debug.ds_delay;
144 else if (temp > (16*1024))
147 dbg->ds_delay = temp;
149 /* Compute status stage delay */
151 temp = usb_ctrl_debug.ss_delay;
154 else if (temp > (16*1024))
157 dbg->ss_delay = temp;
159 /* Check if this control request should be failed */
161 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
164 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
167 temp = usb_ctrl_debug.bmRequestType_value;
169 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
172 temp = usb_ctrl_debug.bRequest_value;
174 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
177 temp = usb_ctrl_debug.ds_fail;
181 temp = usb_ctrl_debug.ss_fail;
187 #endif /* USB_REQ_DEBUG */
188 #endif /* USB_DEBUG */
190 /*------------------------------------------------------------------------*
191 * usbd_do_request_callback
193 * This function is the USB callback for generic USB Host control
195 *------------------------------------------------------------------------*/
197 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
199 ; /* workaround for a bug in "indent" */
201 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
203 switch (USB_GET_STATE(xfer)) {
205 usbd_transfer_submit(xfer);
208 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
213 /*------------------------------------------------------------------------*
214 * usb_do_clear_stall_callback
216 * This function is the USB callback for generic clear stall requests.
217 *------------------------------------------------------------------------*/
219 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
221 struct usb_device_request req;
222 struct usb_device *udev;
223 struct usb_endpoint *ep;
224 struct usb_endpoint *ep_end;
225 struct usb_endpoint *ep_first;
228 udev = xfer->xroot->udev;
230 USB_BUS_LOCK(udev->bus);
232 /* round robin endpoint clear stall */
235 ep_end = udev->endpoints + udev->endpoints_max;
236 ep_first = udev->endpoints;
237 to = udev->endpoints_max;
239 switch (USB_GET_STATE(xfer)) {
240 case USB_ST_TRANSFERRED:
242 /* reset error counter */
243 udev->clear_stall_errors = 0;
246 goto tr_setup; /* device was unconfigured */
251 /* some hardware needs a callback to clear the data toggle */
252 usbd_clear_stall_locked(udev, ep);
253 /* start up the current or next transfer, if any */
254 usb_command_wrapper(&ep->endpoint_q,
255 ep->endpoint_q.curr);
262 break; /* no endpoints - nothing to do */
263 if ((ep < ep_first) || (ep >= ep_end))
264 ep = ep_first; /* endpoint wrapped around */
268 /* setup a clear-stall packet */
270 req.bmRequestType = UT_WRITE_ENDPOINT;
271 req.bRequest = UR_CLEAR_FEATURE;
272 USETW(req.wValue, UF_ENDPOINT_HALT);
273 req.wIndex[0] = ep->edesc->bEndpointAddress;
275 USETW(req.wLength, 0);
277 /* copy in the transfer */
279 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
282 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
284 USB_BUS_UNLOCK(udev->bus);
286 usbd_transfer_submit(xfer);
288 USB_BUS_LOCK(udev->bus);
296 if (error == USB_ERR_CANCELLED)
299 DPRINTF("Clear stall failed.\n");
300 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
303 if (error == USB_ERR_TIMEOUT) {
304 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
305 DPRINTF("Trying to re-enumerate.\n");
306 usbd_start_re_enumerate(udev);
308 udev->clear_stall_errors++;
309 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
310 DPRINTF("Trying to re-enumerate.\n");
311 usbd_start_re_enumerate(udev);
317 /* store current endpoint */
319 USB_BUS_UNLOCK(udev->bus);
322 static usb_handle_req_t *
323 usbd_get_hr_func(struct usb_device *udev)
325 /* figure out if there is a Handle Request function */
326 if (udev->flags.usb_mode == USB_MODE_DEVICE)
327 return (usb_temp_get_desc_p);
328 else if (udev->parent_hub == NULL)
329 return (udev->bus->methods->roothub_exec);
334 /*------------------------------------------------------------------------*
335 * usbd_do_request_flags and usbd_do_request
337 * Description of arguments passed to these functions:
339 * "udev" - this is the "usb_device" structure pointer on which the
340 * request should be performed. It is possible to call this function
341 * in both Host Side mode and Device Side mode.
343 * "mtx" - if this argument is non-NULL the mutex pointed to by it
344 * will get dropped and picked up during the execution of this
345 * function, hence this function sometimes needs to sleep. If this
346 * argument is NULL it has no effect.
348 * "req" - this argument must always be non-NULL and points to an
349 * 8-byte structure holding the USB request to be done. The USB
350 * request structure has a bit telling the direction of the USB
351 * request, if it is a read or a write.
353 * "data" - if the "wLength" part of the structure pointed to by "req"
354 * is non-zero this argument must point to a valid kernel buffer which
355 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
358 * "flags" - here is a list of valid flags:
360 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
363 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
364 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
365 * sysctl. This flag is mostly useful for debugging.
367 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
370 * "actlen" - if non-NULL the actual transfer length will be stored in
371 * the 16-bit unsigned integer pointed to by "actlen". This
372 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
375 * "timeout" - gives the timeout for the control transfer in
376 * milliseconds. A "timeout" value less than 50 milliseconds is
377 * treated like a 50 millisecond timeout. A "timeout" value greater
378 * than 30 seconds is treated like a 30 second timeout. This USB stack
379 * does not allow control requests without a timeout.
381 * NOTE: This function is thread safe. All calls to
382 * "usbd_do_request_flags" will be serialised by the use of an
383 * internal "sx_lock".
388 *------------------------------------------------------------------------*/
390 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
391 struct usb_device_request *req, void *data, uint16_t flags,
392 uint16_t *actlen, usb_timeout_t timeout)
395 struct usb_ctrl_debug_bits dbg;
397 usb_handle_req_t *hr_func;
398 struct usb_xfer *xfer;
401 usb_ticks_t start_ticks;
402 usb_ticks_t delta_ticks;
403 usb_ticks_t max_ticks;
410 /* timeout is too small */
413 if (timeout > 30000) {
414 /* timeout is too big */
417 length = UGETW(req->wLength);
419 enum_locked = usbd_enum_is_locked(udev);
421 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
422 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
423 udev, req->bmRequestType, req->bRequest,
424 req->wValue[1], req->wValue[0],
425 req->wIndex[1], req->wIndex[0],
426 req->wLength[1], req->wLength[0]);
428 /* Check if the device is still alive */
429 if (udev->state < USB_STATE_POWERED) {
430 DPRINTF("usb device has gone\n");
431 return (USB_ERR_NOT_CONFIGURED);
435 * Set "actlen" to a known value in case the caller does not
436 * check the return value:
441 #if (USB_HAVE_USER_IO == 0)
442 if (flags & USB_USER_DATA_PTR)
443 return (USB_ERR_INVAL);
445 if ((mtx != NULL) && (mtx != &Giant)) {
447 mtx_assert(mtx, MA_NOTOWNED);
451 * We need to allow suspend and resume at this point, else the
452 * control transfer will timeout if the device is suspended!
455 usbd_sr_unlock(udev);
458 * Grab the default sx-lock so that serialisation
459 * is achieved when multiple threads are involved:
461 sx_xlock(&udev->ctrl_sx);
463 hr_func = usbd_get_hr_func(udev);
465 if (hr_func != NULL) {
466 DPRINTF("Handle Request function is set\n");
471 if (!(req->bmRequestType & UT_READ)) {
473 DPRINTFN(1, "The handle request function "
474 "does not support writing data!\n");
480 /* The root HUB code needs the BUS lock locked */
482 USB_BUS_LOCK(udev->bus);
483 err = (hr_func) (udev, req, &desc, &temp);
484 USB_BUS_UNLOCK(udev->bus);
490 if (!(flags & USB_SHORT_XFER_OK)) {
491 err = USB_ERR_SHORT_XFER;
501 if (flags & USB_USER_DATA_PTR) {
502 if (copyout(desc, data, length)) {
508 bcopy(desc, data, length);
510 goto done; /* success */
514 * Setup a new USB transfer or use the existing one, if any:
516 usbd_ctrl_transfer_setup(udev);
518 xfer = udev->ctrl_xfer[0];
520 /* most likely out of memory */
527 usbd_get_debug_bits(udev, req, &dbg);
529 /* Check for fault injection */
531 flags |= USB_DELAY_STATUS_STAGE;
535 if (flags & USB_DELAY_STATUS_STAGE)
536 xfer->flags.manual_status = 1;
538 xfer->flags.manual_status = 0;
540 if (flags & USB_SHORT_XFER_OK)
541 xfer->flags.short_xfer_ok = 1;
543 xfer->flags.short_xfer_ok = 0;
545 xfer->timeout = timeout;
549 max_ticks = USB_MS_TO_TICKS(timeout);
551 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
553 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
557 if (temp > usbd_xfer_max_len(xfer)) {
558 temp = usbd_xfer_max_len(xfer);
561 if (xfer->flags.manual_status) {
562 if (usbd_xfer_frame_len(xfer, 0) != 0) {
563 /* Execute data stage separately */
565 } else if (temp > 0) {
570 if (dbg.ds_delay > 0) {
572 xfer->xroot->xfer_mtx,
573 USB_MS_TO_TICKS(dbg.ds_delay));
574 /* make sure we don't time out */
580 usbd_xfer_set_frame_len(xfer, 1, temp);
583 if (!(req->bmRequestType & UT_READ)) {
585 if (flags & USB_USER_DATA_PTR) {
586 USB_XFER_UNLOCK(xfer);
587 err = usbd_copy_in_user(xfer->frbuffers + 1,
596 usbd_copy_in(xfer->frbuffers + 1,
599 usbd_xfer_set_frames(xfer, 2);
601 if (usbd_xfer_frame_len(xfer, 0) == 0) {
602 if (xfer->flags.manual_status) {
608 if (dbg.ss_delay > 0) {
610 xfer->xroot->xfer_mtx,
611 USB_MS_TO_TICKS(dbg.ss_delay));
612 /* make sure we don't time out */
616 xfer->flags.manual_status = 0;
621 usbd_xfer_set_frames(xfer, 1);
624 usbd_transfer_start(xfer);
626 while (usbd_transfer_pending(xfer)) {
627 cv_wait(&udev->ctrlreq_cv,
628 xfer->xroot->xfer_mtx);
637 /* get actual length of DATA stage */
639 if (xfer->aframes < 2) {
642 acttemp = usbd_xfer_frame_len(xfer, 1);
645 /* check for short packet */
647 if (temp > acttemp) {
652 if (req->bmRequestType & UT_READ) {
654 if (flags & USB_USER_DATA_PTR) {
655 USB_XFER_UNLOCK(xfer);
656 err = usbd_copy_out_user(xfer->frbuffers + 1,
665 usbd_copy_out(xfer->frbuffers + 1,
670 * Clear "frlengths[0]" so that we don't send the setup
673 usbd_xfer_set_frame_len(xfer, 0, 0);
675 /* update length and data pointer */
677 data = USB_ADD_BYTES(data, temp);
682 /* check for timeout */
684 delta_ticks = ticks - start_ticks;
685 if (delta_ticks > max_ticks) {
687 err = USB_ERR_TIMEOUT;
697 * Make sure that the control endpoint is no longer
698 * blocked in case of a non-transfer related error:
700 usbd_transfer_stop(xfer);
702 USB_XFER_UNLOCK(xfer);
705 sx_xunlock(&udev->ctrl_sx);
710 if ((mtx != NULL) && (mtx != &Giant))
713 return ((usb_error_t)err);
716 /*------------------------------------------------------------------------*
717 * usbd_do_request_proc - factored out code
719 * This function is factored out code. It does basically the same like
720 * usbd_do_request_flags, except it will check the status of the
721 * passed process argument before doing the USB request. If the
722 * process is draining the USB_ERR_IOERROR code will be returned. It
723 * is assumed that the mutex associated with the process is locked
724 * when calling this function.
725 *------------------------------------------------------------------------*/
727 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
728 struct usb_device_request *req, void *data, uint16_t flags,
729 uint16_t *actlen, usb_timeout_t timeout)
734 /* get request data length */
735 len = UGETW(req->wLength);
737 /* check if the device is being detached */
738 if (usb_proc_is_gone(pproc)) {
739 err = USB_ERR_IOERROR;
743 /* forward the USB request */
744 err = usbd_do_request_flags(udev, pproc->up_mtx,
745 req, data, flags, actlen, timeout);
748 /* on failure we zero the data */
749 /* on short packet we zero the unused data */
750 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
752 memset(data, 0, len);
753 else if (actlen && *actlen != len)
754 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
759 /*------------------------------------------------------------------------*
760 * usbd_req_reset_port
762 * This function will instruct a USB HUB to perform a reset sequence
763 * on the specified port number.
766 * 0: Success. The USB device should now be at address zero.
767 * Else: Failure. No USB device is present and the USB port should be
769 *------------------------------------------------------------------------*/
771 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
773 struct usb_port_status ps;
778 uint16_t pr_poll_delay;
779 uint16_t pr_recovery_delay;
782 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
787 /* range check input parameters */
788 pr_poll_delay = usb_pr_poll_delay;
789 if (pr_poll_delay < 1) {
791 } else if (pr_poll_delay > 1000) {
792 pr_poll_delay = 1000;
794 pr_recovery_delay = usb_pr_recovery_delay;
795 if (pr_recovery_delay > 1000) {
796 pr_recovery_delay = 1000;
802 /* wait for the device to recover from reset */
803 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
806 /* wait for the device to recover from reset */
807 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
808 n += USB_PORT_RESET_DELAY;
810 err = usbd_req_get_port_status(udev, mtx, &ps, port);
814 /* if the device disappeared, just give up */
815 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
818 /* check if reset is complete */
819 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
822 /* check for timeout */
829 /* clear port reset first */
830 err = usbd_req_clear_port_feature(
831 udev, mtx, port, UHF_C_PORT_RESET);
835 /* check for timeout */
837 err = USB_ERR_TIMEOUT;
841 /* wait for the device to recover from reset */
842 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
844 /* wait for the device to recover from reset */
845 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
849 DPRINTFN(2, "port %d reset returning error=%s\n",
850 port, usbd_errstr(err));
854 /*------------------------------------------------------------------------*
855 * usbd_req_warm_reset_port
857 * This function will instruct an USB HUB to perform a warm reset
858 * sequence on the specified port number. This kind of reset is not
859 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
860 * for SUPER-speed USB HUBs.
863 * 0: Success. The USB device should now be available again.
864 * Else: Failure. No USB device is present and the USB port should be
866 *------------------------------------------------------------------------*/
868 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
870 struct usb_port_status ps;
875 uint16_t pr_poll_delay;
876 uint16_t pr_recovery_delay;
879 err = usbd_req_set_port_feature(udev, mtx, port, UHF_BH_PORT_RESET);
884 /* range check input parameters */
885 pr_poll_delay = usb_pr_poll_delay;
886 if (pr_poll_delay < 1) {
888 } else if (pr_poll_delay > 1000) {
889 pr_poll_delay = 1000;
891 pr_recovery_delay = usb_pr_recovery_delay;
892 if (pr_recovery_delay > 1000) {
893 pr_recovery_delay = 1000;
899 /* wait for the device to recover from reset */
900 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
903 /* wait for the device to recover from reset */
904 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
905 n += USB_PORT_RESET_DELAY;
907 err = usbd_req_get_port_status(udev, mtx, &ps, port);
911 /* if the device disappeared, just give up */
912 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
915 /* check if reset is complete */
916 if (UGETW(ps.wPortChange) & UPS_C_BH_PORT_RESET) {
919 /* check for timeout */
926 /* clear port reset first */
927 err = usbd_req_clear_port_feature(
928 udev, mtx, port, UHF_C_BH_PORT_RESET);
932 /* check for timeout */
934 err = USB_ERR_TIMEOUT;
938 /* wait for the device to recover from reset */
939 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
941 /* wait for the device to recover from reset */
942 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
946 DPRINTFN(2, "port %d warm reset returning error=%s\n",
947 port, usbd_errstr(err));
951 /*------------------------------------------------------------------------*
954 * This function can be used to retrieve USB descriptors. It contains
955 * some additional logic like zeroing of missing descriptor bytes and
956 * retrying an USB descriptor in case of failure. The "min_len"
957 * argument specifies the minimum descriptor length. The "max_len"
958 * argument specifies the maximum descriptor length. If the real
959 * descriptor length is less than the minimum length the missing
960 * byte(s) will be zeroed. The type field, the second byte of the USB
961 * descriptor, will get forced to the correct type. If the "actlen"
962 * pointer is non-NULL, the actual length of the transfer will get
963 * stored in the 16-bit unsigned integer which it is pointing to. The
964 * first byte of the descriptor will not get updated. If the "actlen"
965 * pointer is NULL the first byte of the descriptor will get updated
966 * to reflect the actual length instead. If "min_len" is not equal to
967 * "max_len" then this function will try to retrive the beginning of
968 * the descriptor and base the maximum length on the first byte of the
974 *------------------------------------------------------------------------*/
976 usbd_req_get_desc(struct usb_device *udev,
977 struct mtx *mtx, uint16_t *actlen, void *desc,
978 uint16_t min_len, uint16_t max_len,
979 uint16_t id, uint8_t type, uint8_t index,
982 struct usb_device_request req;
986 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
987 id, type, index, max_len);
989 req.bmRequestType = UT_READ_DEVICE;
990 req.bRequest = UR_GET_DESCRIPTOR;
991 USETW2(req.wValue, type, index);
992 USETW(req.wIndex, id);
996 if ((min_len < 2) || (max_len < 2)) {
1000 USETW(req.wLength, min_len);
1002 err = usbd_do_request_flags(udev, mtx, &req,
1003 desc, 0, NULL, 1000);
1011 usb_pause_mtx(mtx, hz / 5);
1017 if (min_len == max_len) {
1019 /* enforce correct length */
1020 if ((buf[0] > min_len) && (actlen == NULL))
1023 /* enforce correct type */
1030 if (max_len > buf[0]) {
1033 /* zero minimum data */
1035 while (min_len > max_len) {
1040 /* set new minimum length */
1045 if (actlen != NULL) {
1054 /*------------------------------------------------------------------------*
1055 * usbd_req_get_string_any
1057 * This function will return the string given by "string_index"
1058 * using the first language ID. The maximum length "len" includes
1059 * the terminating zero. The "len" argument should be twice as
1060 * big pluss 2 bytes, compared with the actual maximum string length !
1065 *------------------------------------------------------------------------*/
1067 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1068 uint16_t len, uint8_t string_index)
1079 /* should not happen */
1080 return (USB_ERR_NORMAL_COMPLETION);
1082 if (string_index == 0) {
1083 /* this is the language table */
1085 return (USB_ERR_INVAL);
1087 if (udev->flags.no_strings) {
1089 return (USB_ERR_STALLED);
1091 err = usbd_req_get_string_desc
1092 (udev, mtx, buf, len, udev->langid, string_index);
1097 temp = (uint8_t *)buf;
1100 /* string length is too short */
1102 return (USB_ERR_INVAL);
1104 /* reserve one byte for terminating zero */
1107 /* find maximum length */
1109 n = (temp[0] / 2) - 1;
1113 /* skip descriptor header */
1116 /* reset swap state */
1119 /* convert and filter */
1120 for (i = 0; (i != n); i++) {
1121 c = UGETW(temp + (2 * i));
1123 /* convert from Unicode, handle buggy strings */
1124 if (((c & 0xff00) == 0) && (swap & 1)) {
1125 /* Little Endian, default */
1128 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1133 /* silently skip bad character */
1138 * Filter by default - We only allow alphanumerical
1139 * and a few more to avoid any problems with scripts
1152 /* silently skip bad character */
1154 *s = 0; /* zero terminate resulting string */
1155 return (USB_ERR_NORMAL_COMPLETION);
1158 /*------------------------------------------------------------------------*
1159 * usbd_req_get_string_desc
1161 * If you don't know the language ID, consider using
1162 * "usbd_req_get_string_any()".
1167 *------------------------------------------------------------------------*/
1169 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1170 uint16_t max_len, uint16_t lang_id,
1171 uint8_t string_index)
1173 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1174 UDESC_STRING, string_index, 0));
1177 /*------------------------------------------------------------------------*
1178 * usbd_req_get_config_desc_ptr
1180 * This function is used in device side mode to retrieve the pointer
1181 * to the generated config descriptor. This saves allocating space for
1182 * an additional config descriptor when setting the configuration.
1187 *------------------------------------------------------------------------*/
1189 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1190 struct usb_config_descriptor **ppcd, uint16_t wValue)
1192 struct usb_device_request req;
1193 usb_handle_req_t *hr_func;
1198 req.bmRequestType = UT_READ_DEVICE;
1199 req.bRequest = UR_GET_DESCRIPTOR;
1200 USETW(req.wValue, wValue);
1201 USETW(req.wIndex, 0);
1202 USETW(req.wLength, 0);
1207 hr_func = usbd_get_hr_func(udev);
1209 if (hr_func == NULL)
1210 err = USB_ERR_INVAL;
1212 USB_BUS_LOCK(udev->bus);
1213 err = (hr_func) (udev, &req, &ptr, &len);
1214 USB_BUS_UNLOCK(udev->bus);
1219 else if (ptr == NULL)
1220 err = USB_ERR_INVAL;
1222 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1227 /*------------------------------------------------------------------------*
1228 * usbd_req_get_config_desc
1233 *------------------------------------------------------------------------*/
1235 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1236 struct usb_config_descriptor *d, uint8_t conf_index)
1240 DPRINTFN(4, "confidx=%d\n", conf_index);
1242 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1243 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1247 /* Extra sanity checking */
1248 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1249 err = USB_ERR_INVAL;
1255 /*------------------------------------------------------------------------*
1256 * usbd_req_get_config_desc_full
1258 * This function gets the complete USB configuration descriptor and
1259 * ensures that "wTotalLength" is correct.
1264 *------------------------------------------------------------------------*/
1266 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1267 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1270 struct usb_config_descriptor cd;
1271 struct usb_config_descriptor *cdesc;
1275 DPRINTFN(4, "index=%d\n", index);
1279 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1283 /* get full descriptor */
1284 len = UGETW(cd.wTotalLength);
1285 if (len < sizeof(*cdesc)) {
1286 /* corrupt descriptor */
1287 return (USB_ERR_INVAL);
1289 cdesc = malloc(len, mtype, M_WAITOK);
1290 if (cdesc == NULL) {
1291 return (USB_ERR_NOMEM);
1293 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1294 UDESC_CONFIG, index, 3);
1299 /* make sure that the device is not fooling us: */
1300 USETW(cdesc->wTotalLength, len);
1304 return (0); /* success */
1307 /*------------------------------------------------------------------------*
1308 * usbd_req_get_device_desc
1313 *------------------------------------------------------------------------*/
1315 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1316 struct usb_device_descriptor *d)
1319 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1320 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1323 /*------------------------------------------------------------------------*
1324 * usbd_req_get_alt_interface_no
1329 *------------------------------------------------------------------------*/
1331 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1332 uint8_t *alt_iface_no, uint8_t iface_index)
1334 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1335 struct usb_device_request req;
1337 if ((iface == NULL) || (iface->idesc == NULL))
1338 return (USB_ERR_INVAL);
1340 req.bmRequestType = UT_READ_INTERFACE;
1341 req.bRequest = UR_GET_INTERFACE;
1342 USETW(req.wValue, 0);
1343 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1345 USETW(req.wLength, 1);
1346 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1349 /*------------------------------------------------------------------------*
1350 * usbd_req_set_alt_interface_no
1355 *------------------------------------------------------------------------*/
1357 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1358 uint8_t iface_index, uint8_t alt_no)
1360 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1361 struct usb_device_request req;
1363 if ((iface == NULL) || (iface->idesc == NULL))
1364 return (USB_ERR_INVAL);
1366 req.bmRequestType = UT_WRITE_INTERFACE;
1367 req.bRequest = UR_SET_INTERFACE;
1368 req.wValue[0] = alt_no;
1370 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1372 USETW(req.wLength, 0);
1373 return (usbd_do_request(udev, mtx, &req, 0));
1376 /*------------------------------------------------------------------------*
1377 * usbd_req_get_device_status
1382 *------------------------------------------------------------------------*/
1384 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1385 struct usb_status *st)
1387 struct usb_device_request req;
1389 req.bmRequestType = UT_READ_DEVICE;
1390 req.bRequest = UR_GET_STATUS;
1391 USETW(req.wValue, 0);
1392 USETW(req.wIndex, 0);
1393 USETW(req.wLength, sizeof(*st));
1394 return (usbd_do_request(udev, mtx, &req, st));
1397 /*------------------------------------------------------------------------*
1398 * usbd_req_get_hub_descriptor
1403 *------------------------------------------------------------------------*/
1405 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1406 struct usb_hub_descriptor *hd, uint8_t nports)
1408 struct usb_device_request req;
1409 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1411 req.bmRequestType = UT_READ_CLASS_DEVICE;
1412 req.bRequest = UR_GET_DESCRIPTOR;
1413 USETW2(req.wValue, UDESC_HUB, 0);
1414 USETW(req.wIndex, 0);
1415 USETW(req.wLength, len);
1416 return (usbd_do_request(udev, mtx, &req, hd));
1419 /*------------------------------------------------------------------------*
1420 * usbd_req_get_ss_hub_descriptor
1425 *------------------------------------------------------------------------*/
1427 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1428 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1430 struct usb_device_request req;
1431 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1433 req.bmRequestType = UT_READ_CLASS_DEVICE;
1434 req.bRequest = UR_GET_DESCRIPTOR;
1435 USETW2(req.wValue, UDESC_SS_HUB, 0);
1436 USETW(req.wIndex, 0);
1437 USETW(req.wLength, len);
1438 return (usbd_do_request(udev, mtx, &req, hd));
1441 /*------------------------------------------------------------------------*
1442 * usbd_req_get_hub_status
1447 *------------------------------------------------------------------------*/
1449 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1450 struct usb_hub_status *st)
1452 struct usb_device_request req;
1454 req.bmRequestType = UT_READ_CLASS_DEVICE;
1455 req.bRequest = UR_GET_STATUS;
1456 USETW(req.wValue, 0);
1457 USETW(req.wIndex, 0);
1458 USETW(req.wLength, sizeof(struct usb_hub_status));
1459 return (usbd_do_request(udev, mtx, &req, st));
1462 /*------------------------------------------------------------------------*
1463 * usbd_req_set_address
1465 * This function is used to set the address for an USB device. After
1466 * port reset the USB device will respond at address zero.
1471 *------------------------------------------------------------------------*/
1473 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1475 struct usb_device_request req;
1478 DPRINTFN(6, "setting device address=%d\n", addr);
1480 req.bmRequestType = UT_WRITE_DEVICE;
1481 req.bRequest = UR_SET_ADDRESS;
1482 USETW(req.wValue, addr);
1483 USETW(req.wIndex, 0);
1484 USETW(req.wLength, 0);
1486 err = USB_ERR_INVAL;
1488 /* check if USB controller handles set address */
1489 if (udev->bus->methods->set_address != NULL)
1490 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1492 if (err != USB_ERR_INVAL)
1495 /* Setting the address should not take more than 1 second ! */
1496 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1497 USB_DELAY_STATUS_STAGE, NULL, 1000);
1500 /* allow device time to set new address */
1502 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1507 /*------------------------------------------------------------------------*
1508 * usbd_req_get_port_status
1513 *------------------------------------------------------------------------*/
1515 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1516 struct usb_port_status *ps, uint8_t port)
1518 struct usb_device_request req;
1520 req.bmRequestType = UT_READ_CLASS_OTHER;
1521 req.bRequest = UR_GET_STATUS;
1522 USETW(req.wValue, 0);
1523 req.wIndex[0] = port;
1525 USETW(req.wLength, sizeof *ps);
1526 return (usbd_do_request(udev, mtx, &req, ps));
1529 /*------------------------------------------------------------------------*
1530 * usbd_req_clear_hub_feature
1535 *------------------------------------------------------------------------*/
1537 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1540 struct usb_device_request req;
1542 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1543 req.bRequest = UR_CLEAR_FEATURE;
1544 USETW(req.wValue, sel);
1545 USETW(req.wIndex, 0);
1546 USETW(req.wLength, 0);
1547 return (usbd_do_request(udev, mtx, &req, 0));
1550 /*------------------------------------------------------------------------*
1551 * usbd_req_set_hub_feature
1556 *------------------------------------------------------------------------*/
1558 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1561 struct usb_device_request req;
1563 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1564 req.bRequest = UR_SET_FEATURE;
1565 USETW(req.wValue, sel);
1566 USETW(req.wIndex, 0);
1567 USETW(req.wLength, 0);
1568 return (usbd_do_request(udev, mtx, &req, 0));
1571 /*------------------------------------------------------------------------*
1572 * usbd_req_set_hub_u1_timeout
1577 *------------------------------------------------------------------------*/
1579 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1580 uint8_t port, uint8_t timeout)
1582 struct usb_device_request req;
1584 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1585 req.bRequest = UR_SET_FEATURE;
1586 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1587 req.wIndex[0] = port;
1588 req.wIndex[1] = timeout;
1589 USETW(req.wLength, 0);
1590 return (usbd_do_request(udev, mtx, &req, 0));
1593 /*------------------------------------------------------------------------*
1594 * usbd_req_set_hub_u2_timeout
1599 *------------------------------------------------------------------------*/
1601 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1602 uint8_t port, uint8_t timeout)
1604 struct usb_device_request req;
1606 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1607 req.bRequest = UR_SET_FEATURE;
1608 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1609 req.wIndex[0] = port;
1610 req.wIndex[1] = timeout;
1611 USETW(req.wLength, 0);
1612 return (usbd_do_request(udev, mtx, &req, 0));
1615 /*------------------------------------------------------------------------*
1616 * usbd_req_set_hub_depth
1621 *------------------------------------------------------------------------*/
1623 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1626 struct usb_device_request req;
1628 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1629 req.bRequest = UR_SET_HUB_DEPTH;
1630 USETW(req.wValue, depth);
1631 USETW(req.wIndex, 0);
1632 USETW(req.wLength, 0);
1633 return (usbd_do_request(udev, mtx, &req, 0));
1636 /*------------------------------------------------------------------------*
1637 * usbd_req_clear_port_feature
1642 *------------------------------------------------------------------------*/
1644 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1645 uint8_t port, uint16_t sel)
1647 struct usb_device_request req;
1649 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1650 req.bRequest = UR_CLEAR_FEATURE;
1651 USETW(req.wValue, sel);
1652 req.wIndex[0] = port;
1654 USETW(req.wLength, 0);
1655 return (usbd_do_request(udev, mtx, &req, 0));
1658 /*------------------------------------------------------------------------*
1659 * usbd_req_set_port_feature
1664 *------------------------------------------------------------------------*/
1666 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1667 uint8_t port, uint16_t sel)
1669 struct usb_device_request req;
1671 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1672 req.bRequest = UR_SET_FEATURE;
1673 USETW(req.wValue, sel);
1674 req.wIndex[0] = port;
1676 USETW(req.wLength, 0);
1677 return (usbd_do_request(udev, mtx, &req, 0));
1680 /*------------------------------------------------------------------------*
1681 * usbd_req_set_protocol
1686 *------------------------------------------------------------------------*/
1688 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1689 uint8_t iface_index, uint16_t report)
1691 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1692 struct usb_device_request req;
1694 if ((iface == NULL) || (iface->idesc == NULL)) {
1695 return (USB_ERR_INVAL);
1697 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1698 iface, report, iface->idesc->bInterfaceNumber);
1700 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1701 req.bRequest = UR_SET_PROTOCOL;
1702 USETW(req.wValue, report);
1703 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1705 USETW(req.wLength, 0);
1706 return (usbd_do_request(udev, mtx, &req, 0));
1709 /*------------------------------------------------------------------------*
1710 * usbd_req_set_report
1715 *------------------------------------------------------------------------*/
1717 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1718 uint8_t iface_index, uint8_t type, uint8_t id)
1720 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1721 struct usb_device_request req;
1723 if ((iface == NULL) || (iface->idesc == NULL)) {
1724 return (USB_ERR_INVAL);
1726 DPRINTFN(5, "len=%d\n", len);
1728 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1729 req.bRequest = UR_SET_REPORT;
1730 USETW2(req.wValue, type, id);
1731 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1733 USETW(req.wLength, len);
1734 return (usbd_do_request(udev, mtx, &req, data));
1737 /*------------------------------------------------------------------------*
1738 * usbd_req_get_report
1743 *------------------------------------------------------------------------*/
1745 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1746 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1748 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1749 struct usb_device_request req;
1751 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1752 return (USB_ERR_INVAL);
1754 DPRINTFN(5, "len=%d\n", len);
1756 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1757 req.bRequest = UR_GET_REPORT;
1758 USETW2(req.wValue, type, id);
1759 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1761 USETW(req.wLength, len);
1762 return (usbd_do_request(udev, mtx, &req, data));
1765 /*------------------------------------------------------------------------*
1771 *------------------------------------------------------------------------*/
1773 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1774 uint8_t iface_index, uint8_t duration, uint8_t id)
1776 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1777 struct usb_device_request req;
1779 if ((iface == NULL) || (iface->idesc == NULL)) {
1780 return (USB_ERR_INVAL);
1782 DPRINTFN(5, "%d %d\n", duration, id);
1784 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1785 req.bRequest = UR_SET_IDLE;
1786 USETW2(req.wValue, duration, id);
1787 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1789 USETW(req.wLength, 0);
1790 return (usbd_do_request(udev, mtx, &req, 0));
1793 /*------------------------------------------------------------------------*
1794 * usbd_req_get_report_descriptor
1799 *------------------------------------------------------------------------*/
1801 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1802 void *d, uint16_t size, uint8_t iface_index)
1804 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1805 struct usb_device_request req;
1807 if ((iface == NULL) || (iface->idesc == NULL)) {
1808 return (USB_ERR_INVAL);
1810 req.bmRequestType = UT_READ_INTERFACE;
1811 req.bRequest = UR_GET_DESCRIPTOR;
1812 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1813 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1815 USETW(req.wLength, size);
1816 return (usbd_do_request(udev, mtx, &req, d));
1819 /*------------------------------------------------------------------------*
1820 * usbd_req_set_config
1822 * This function is used to select the current configuration number in
1823 * both USB device side mode and USB host side mode. When setting the
1824 * configuration the function of the interfaces can change.
1829 *------------------------------------------------------------------------*/
1831 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1833 struct usb_device_request req;
1835 DPRINTF("setting config %d\n", conf);
1837 /* do "set configuration" request */
1839 req.bmRequestType = UT_WRITE_DEVICE;
1840 req.bRequest = UR_SET_CONFIG;
1841 req.wValue[0] = conf;
1843 USETW(req.wIndex, 0);
1844 USETW(req.wLength, 0);
1845 return (usbd_do_request(udev, mtx, &req, 0));
1848 /*------------------------------------------------------------------------*
1849 * usbd_req_get_config
1854 *------------------------------------------------------------------------*/
1856 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1858 struct usb_device_request req;
1860 req.bmRequestType = UT_READ_DEVICE;
1861 req.bRequest = UR_GET_CONFIG;
1862 USETW(req.wValue, 0);
1863 USETW(req.wIndex, 0);
1864 USETW(req.wLength, 1);
1865 return (usbd_do_request(udev, mtx, &req, pconf));
1868 /*------------------------------------------------------------------------*
1869 * usbd_setup_device_desc
1870 *------------------------------------------------------------------------*/
1872 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1877 * Get the first 8 bytes of the device descriptor !
1879 * NOTE: "usbd_do_request()" will check the device descriptor
1880 * next time we do a request to see if the maximum packet size
1881 * changed! The 8 first bytes of the device descriptor
1882 * contains the maximum packet size to use on control endpoint
1883 * 0. If this value is different from "USB_MAX_IPACKET" a new
1884 * USB control request will be setup!
1886 switch (udev->speed) {
1887 case USB_SPEED_FULL:
1889 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1890 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1892 DPRINTFN(0, "getting device descriptor "
1893 "at addr %d failed, %s\n", udev->address,
1899 DPRINTF("Minimum MaxPacketSize is large enough "
1900 "to hold the complete device descriptor\n");
1904 /* get the full device descriptor */
1905 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1907 /* try one more time, if error */
1909 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1912 DPRINTF("addr=%d, getting full desc failed\n",
1917 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1918 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1919 udev->address, UGETW(udev->ddesc.bcdUSB),
1920 udev->ddesc.bDeviceClass,
1921 udev->ddesc.bDeviceSubClass,
1922 udev->ddesc.bDeviceProtocol,
1923 udev->ddesc.bMaxPacketSize,
1924 udev->ddesc.bLength,
1930 /*------------------------------------------------------------------------*
1931 * usbd_req_re_enumerate
1933 * NOTE: After this function returns the hardware is in the
1934 * unconfigured state! The application is responsible for setting a
1935 * new configuration.
1940 *------------------------------------------------------------------------*/
1942 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1944 struct usb_device *parent_hub;
1947 uint8_t do_retry = 1;
1949 if (udev->flags.usb_mode != USB_MODE_HOST) {
1950 return (USB_ERR_INVAL);
1952 old_addr = udev->address;
1953 parent_hub = udev->parent_hub;
1954 if (parent_hub == NULL) {
1955 return (USB_ERR_INVAL);
1959 * Try to reset the High Speed parent HUB of a LOW- or FULL-
1960 * speed device, if any.
1962 if (udev->parent_hs_hub != NULL &&
1963 udev->speed != USB_SPEED_HIGH) {
1964 DPRINTF("Trying to reset parent High Speed TT.\n");
1965 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
1968 DPRINTF("Resetting parent High "
1969 "Speed TT failed (%s).\n",
1974 /* Try to reset the parent HUB port. */
1975 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1977 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1978 old_addr, usbd_errstr(err));
1983 * After that the port has been reset our device should be at
1986 udev->address = USB_START_ADDR;
1988 /* reset "bMaxPacketSize" */
1989 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1991 /* reset USB state */
1992 usb_set_device_state(udev, USB_STATE_POWERED);
1995 * Restore device address:
1997 err = usbd_req_set_address(udev, mtx, old_addr);
1999 /* XXX ignore any errors! */
2000 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2001 old_addr, usbd_errstr(err));
2004 * Restore device address, if the controller driver did not
2007 if (udev->address == USB_START_ADDR)
2008 udev->address = old_addr;
2010 /* setup the device descriptor and the initial "wMaxPacketSize" */
2011 err = usbd_setup_device_desc(udev, mtx);
2014 if (err && do_retry) {
2015 /* give the USB firmware some time to load */
2016 usb_pause_mtx(mtx, hz / 2);
2017 /* no more retries after this retry */
2022 /* restore address */
2023 if (udev->address == USB_START_ADDR)
2024 udev->address = old_addr;
2025 /* update state, if successful */
2027 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2031 /*------------------------------------------------------------------------*
2032 * usbd_req_clear_device_feature
2037 *------------------------------------------------------------------------*/
2039 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2042 struct usb_device_request req;
2044 req.bmRequestType = UT_WRITE_DEVICE;
2045 req.bRequest = UR_CLEAR_FEATURE;
2046 USETW(req.wValue, sel);
2047 USETW(req.wIndex, 0);
2048 USETW(req.wLength, 0);
2049 return (usbd_do_request(udev, mtx, &req, 0));
2052 /*------------------------------------------------------------------------*
2053 * usbd_req_set_device_feature
2058 *------------------------------------------------------------------------*/
2060 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2063 struct usb_device_request req;
2065 req.bmRequestType = UT_WRITE_DEVICE;
2066 req.bRequest = UR_SET_FEATURE;
2067 USETW(req.wValue, sel);
2068 USETW(req.wIndex, 0);
2069 USETW(req.wLength, 0);
2070 return (usbd_do_request(udev, mtx, &req, 0));
2073 /*------------------------------------------------------------------------*
2079 *------------------------------------------------------------------------*/
2081 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2084 struct usb_device_request req;
2086 /* For single TT HUBs the port should be 1 */
2088 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2089 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2092 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2093 req.bRequest = UR_RESET_TT;
2094 USETW(req.wValue, 0);
2095 req.wIndex[0] = port;
2097 USETW(req.wLength, 0);
2098 return (usbd_do_request(udev, mtx, &req, 0));
2101 /*------------------------------------------------------------------------*
2102 * usbd_req_clear_tt_buffer
2104 * For single TT HUBs the port should be 1.
2109 *------------------------------------------------------------------------*/
2111 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2112 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2114 struct usb_device_request req;
2117 /* For single TT HUBs the port should be 1 */
2119 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2120 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2123 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2124 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2126 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2127 req.bRequest = UR_CLEAR_TT_BUFFER;
2128 USETW(req.wValue, wValue);
2129 req.wIndex[0] = port;
2131 USETW(req.wLength, 0);
2132 return (usbd_do_request(udev, mtx, &req, 0));