3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <bus/u4b/usb.h>
47 #include <bus/u4b/usbdi.h>
48 #include <bus/u4b/usbdi_util.h>
49 #include <bus/u4b/usb_ioctl.h>
50 #include <bus/u4b/usbhid.h>
52 #define USB_DEBUG_VAR usb_debug
54 #include <bus/u4b/usb_core.h>
55 #include <bus/u4b/usb_busdma.h>
56 #include <bus/u4b/usb_request.h>
57 #include <bus/u4b/usb_process.h>
58 #include <bus/u4b/usb_transfer.h>
59 #include <bus/u4b/usb_debug.h>
60 #include <bus/u4b/usb_device.h>
61 #include <bus/u4b/usb_util.h>
62 #include <bus/u4b/usb_dynamic.h>
64 #include <bus/u4b/usb_controller.h>
65 #include <bus/u4b/usb_bus.h>
66 #include <sys/ctype.h>
68 static int usb_no_cs_fail;
70 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
71 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
75 /* The following structures are used in connection to fault injection. */
76 struct usb_ctrl_debug {
77 int bus_index; /* target bus */
78 int dev_index; /* target address */
79 int ds_fail; /* fail data stage */
80 int ss_fail; /* fail data stage */
81 int ds_delay; /* data stage delay in ms */
82 int ss_delay; /* status stage delay in ms */
83 int bmRequestType_value;
87 struct usb_ctrl_debug_bits {
95 /* The default is to disable fault injection. */
97 static struct usb_ctrl_debug usb_ctrl_debug = {
100 .bmRequestType_value = -1,
101 .bRequest_value = -1,
104 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
105 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
106 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
107 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
108 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
109 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
110 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
111 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
112 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
113 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
114 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
115 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
117 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
119 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
121 /*------------------------------------------------------------------------*
122 * usbd_get_debug_bits
124 * This function is only useful in USB host mode.
125 *------------------------------------------------------------------------*/
127 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
128 struct usb_ctrl_debug_bits *dbg)
132 memset(dbg, 0, sizeof(*dbg));
134 /* Compute data stage delay */
136 temp = usb_ctrl_debug.ds_delay;
139 else if (temp > (16*1024))
142 dbg->ds_delay = temp;
144 /* Compute status stage delay */
146 temp = usb_ctrl_debug.ss_delay;
149 else if (temp > (16*1024))
152 dbg->ss_delay = temp;
154 /* Check if this control request should be failed */
156 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
159 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
162 temp = usb_ctrl_debug.bmRequestType_value;
164 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
167 temp = usb_ctrl_debug.bRequest_value;
169 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
172 temp = usb_ctrl_debug.ds_fail;
176 temp = usb_ctrl_debug.ss_fail;
182 #endif /* USB_REQ_DEBUG */
183 #endif /* USB_DEBUG */
185 /*------------------------------------------------------------------------*
186 * usbd_do_request_callback
188 * This function is the USB callback for generic USB Host control
190 *------------------------------------------------------------------------*/
192 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
194 ; /* workaround for a bug in "indent" */
196 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
198 switch (USB_GET_STATE(xfer)) {
200 usbd_transfer_submit(xfer);
203 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
208 /*------------------------------------------------------------------------*
209 * usb_do_clear_stall_callback
211 * This function is the USB callback for generic clear stall requests.
212 *------------------------------------------------------------------------*/
214 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
216 struct usb_device_request req;
217 struct usb_device *udev;
218 struct usb_endpoint *ep;
219 struct usb_endpoint *ep_end;
220 struct usb_endpoint *ep_first;
224 udev = xfer->xroot->udev;
226 USB_BUS_LOCK(udev->bus);
228 /* round robin endpoint clear stall */
231 ep_end = udev->endpoints + udev->endpoints_max;
232 ep_first = udev->endpoints;
233 to = udev->endpoints_max;
235 switch (USB_GET_STATE(xfer)) {
236 case USB_ST_TRANSFERRED:
238 /* reset error counter */
239 udev->clear_stall_errors = 0;
242 goto tr_setup; /* device was unconfigured */
247 /* some hardware needs a callback to clear the data toggle */
248 usbd_clear_stall_locked(udev, ep);
249 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
250 /* start the current or next transfer, if any */
251 usb_command_wrapper(&ep->endpoint_q[x],
252 ep->endpoint_q[x].curr);
260 break; /* no endpoints - nothing to do */
261 if ((ep < ep_first) || (ep >= ep_end))
262 ep = ep_first; /* endpoint wrapped around */
266 /* setup a clear-stall packet */
268 req.bmRequestType = UT_WRITE_ENDPOINT;
269 req.bRequest = UR_CLEAR_FEATURE;
270 USETW(req.wValue, UF_ENDPOINT_HALT);
271 req.wIndex[0] = ep->edesc->bEndpointAddress;
273 USETW(req.wLength, 0);
275 /* copy in the transfer */
277 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
280 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
282 USB_BUS_UNLOCK(udev->bus);
284 usbd_transfer_submit(xfer);
286 USB_BUS_LOCK(udev->bus);
294 if (error == USB_ERR_CANCELLED)
297 DPRINTF("Clear stall failed.\n");
300 * Some VMs like VirtualBox always return failure on
301 * clear-stall which we sometimes should just ignore.
305 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
308 if (error == USB_ERR_TIMEOUT) {
309 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
310 DPRINTF("Trying to re-enumerate.\n");
311 usbd_start_re_enumerate(udev);
313 udev->clear_stall_errors++;
314 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
315 DPRINTF("Trying to re-enumerate.\n");
316 usbd_start_re_enumerate(udev);
322 /* store current endpoint */
324 USB_BUS_UNLOCK(udev->bus);
327 static usb_handle_req_t *
328 usbd_get_hr_func(struct usb_device *udev)
330 /* figure out if there is a Handle Request function */
331 if (udev->flags.usb_mode == USB_MODE_DEVICE)
332 return (usb_temp_get_desc_p);
333 else if (udev->parent_hub == NULL)
334 return (udev->bus->methods->roothub_exec);
339 /*------------------------------------------------------------------------*
340 * usbd_do_request_flags and usbd_do_request
342 * Description of arguments passed to these functions:
344 * "udev" - this is the "usb_device" structure pointer on which the
345 * request should be performed. It is possible to call this function
346 * in both Host Side mode and Device Side mode.
348 * "mtx" - if this argument is non-NULL the mutex pointed to by it
349 * will get dropped and picked up during the execution of this
350 * function, hence this function sometimes needs to sleep. If this
351 * argument is NULL it has no effect.
353 * "req" - this argument must always be non-NULL and points to an
354 * 8-byte structure holding the USB request to be done. The USB
355 * request structure has a bit telling the direction of the USB
356 * request, if it is a read or a write.
358 * "data" - if the "wLength" part of the structure pointed to by "req"
359 * is non-zero this argument must point to a valid kernel buffer which
360 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
363 * "flags" - here is a list of valid flags:
365 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
368 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
369 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
370 * sysctl. This flag is mostly useful for debugging.
372 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
375 * "actlen" - if non-NULL the actual transfer length will be stored in
376 * the 16-bit unsigned integer pointed to by "actlen". This
377 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
380 * "timeout" - gives the timeout for the control transfer in
381 * milliseconds. A "timeout" value less than 50 milliseconds is
382 * treated like a 50 millisecond timeout. A "timeout" value greater
383 * than 30 seconds is treated like a 30 second timeout. This USB stack
384 * does not allow control requests without a timeout.
386 * NOTE: This function is thread safe. All calls to
387 * "usbd_do_request_flags" will be serialised by the use of an
388 * internal "sx_lock".
393 *------------------------------------------------------------------------*/
395 usbd_do_request_flags(struct usb_device *udev, struct lock *lock,
396 struct usb_device_request *req, void *data, uint16_t flags,
397 uint16_t *actlen, usb_timeout_t timeout)
400 struct usb_ctrl_debug_bits dbg;
402 usb_handle_req_t *hr_func;
403 struct usb_xfer *xfer;
406 usb_ticks_t start_ticks;
407 usb_ticks_t delta_ticks;
408 usb_ticks_t max_ticks;
415 /* timeout is too small */
418 if (timeout > 30000) {
419 /* timeout is too big */
422 length = UGETW(req->wLength);
424 enum_locked = usbd_enum_is_locked(udev);
426 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
427 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
428 udev, req->bmRequestType, req->bRequest,
429 req->wValue[1], req->wValue[0],
430 req->wIndex[1], req->wIndex[0],
431 req->wLength[1], req->wLength[0]);
433 /* Check if the device is still alive */
434 if (udev->state < USB_STATE_POWERED) {
435 DPRINTF("usb device has gone\n");
436 return (USB_ERR_NOT_CONFIGURED);
440 * Set "actlen" to a known value in case the caller does not
441 * check the return value:
446 #if (USB_HAVE_USER_IO == 0)
447 if (flags & USB_USER_DATA_PTR)
448 return (USB_ERR_INVAL);
451 if ((mtx != NULL) && (mtx != &Giant)) {
454 lockmgr(lock, LK_RELEASE);
455 KKASSERT(!lockowned(lock));
459 * We need to allow suspend and resume at this point, else the
460 * control transfer will timeout if the device is suspended!
463 usbd_sr_unlock(udev);
466 * Grab the default sx-lock so that serialisation
467 * is achieved when multiple threads are involved:
469 lockmgr(&udev->ctrl_lock, LK_EXCLUSIVE);
471 hr_func = usbd_get_hr_func(udev);
473 if (hr_func != NULL) {
474 DPRINTF("Handle Request function is set\n");
479 if (!(req->bmRequestType & UT_READ)) {
481 DPRINTFN(1, "The handle request function "
482 "does not support writing data!\n");
488 /* The root HUB code needs the BUS lock locked */
490 USB_BUS_LOCK(udev->bus);
491 err = (hr_func) (udev, req, &desc, &temp);
492 USB_BUS_UNLOCK(udev->bus);
498 if (!(flags & USB_SHORT_XFER_OK)) {
499 err = USB_ERR_SHORT_XFER;
509 if (flags & USB_USER_DATA_PTR) {
510 if (copyout(desc, data, length)) {
516 memcpy(data, desc, length);
518 goto done; /* success */
522 * Setup a new USB transfer or use the existing one, if any:
524 usbd_ctrl_transfer_setup(udev);
526 xfer = udev->ctrl_xfer[0];
528 /* most likely out of memory */
535 usbd_get_debug_bits(udev, req, &dbg);
537 /* Check for fault injection */
539 flags |= USB_DELAY_STATUS_STAGE;
543 if (flags & USB_DELAY_STATUS_STAGE)
544 xfer->flags.manual_status = 1;
546 xfer->flags.manual_status = 0;
548 if (flags & USB_SHORT_XFER_OK)
549 xfer->flags.short_xfer_ok = 1;
551 xfer->flags.short_xfer_ok = 0;
553 xfer->timeout = timeout;
557 max_ticks = USB_MS_TO_TICKS(timeout);
559 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
561 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
565 if (temp > usbd_xfer_max_len(xfer)) {
566 temp = usbd_xfer_max_len(xfer);
569 if (xfer->flags.manual_status) {
570 if (usbd_xfer_frame_len(xfer, 0) != 0) {
571 /* Execute data stage separately */
573 } else if (temp > 0) {
578 if (dbg.ds_delay > 0) {
580 xfer->xroot->xfer_lock,
581 USB_MS_TO_TICKS(dbg.ds_delay));
582 /* make sure we don't time out */
588 usbd_xfer_set_frame_len(xfer, 1, temp);
591 if (!(req->bmRequestType & UT_READ)) {
593 if (flags & USB_USER_DATA_PTR) {
594 USB_XFER_UNLOCK(xfer);
595 err = usbd_copy_in_user(xfer->frbuffers + 1,
604 usbd_copy_in(xfer->frbuffers + 1,
607 usbd_xfer_set_frames(xfer, 2);
609 if (usbd_xfer_frame_len(xfer, 0) == 0) {
610 if (xfer->flags.manual_status) {
616 if (dbg.ss_delay > 0) {
618 xfer->xroot->xfer_lock,
619 USB_MS_TO_TICKS(dbg.ss_delay));
620 /* make sure we don't time out */
624 xfer->flags.manual_status = 0;
629 usbd_xfer_set_frames(xfer, 1);
632 usbd_transfer_start(xfer);
634 while (usbd_transfer_pending(xfer)) {
635 cv_wait(&udev->ctrlreq_cv,
636 xfer->xroot->xfer_lock);
645 /* get actual length of DATA stage */
647 if (xfer->aframes < 2) {
650 acttemp = usbd_xfer_frame_len(xfer, 1);
653 /* check for short packet */
655 if (temp > acttemp) {
660 if (req->bmRequestType & UT_READ) {
662 if (flags & USB_USER_DATA_PTR) {
663 USB_XFER_UNLOCK(xfer);
664 err = usbd_copy_out_user(xfer->frbuffers + 1,
673 usbd_copy_out(xfer->frbuffers + 1,
678 * Clear "frlengths[0]" so that we don't send the setup
681 usbd_xfer_set_frame_len(xfer, 0, 0);
683 /* update length and data pointer */
685 data = USB_ADD_BYTES(data, temp);
690 /* check for timeout */
692 delta_ticks = ticks - start_ticks;
693 if (delta_ticks > max_ticks) {
695 err = USB_ERR_TIMEOUT;
705 * Make sure that the control endpoint is no longer
706 * blocked in case of a non-transfer related error:
708 usbd_transfer_stop(xfer);
710 USB_XFER_UNLOCK(xfer);
713 lockmgr(&udev->ctrl_lock, LK_RELEASE);
719 if ((mtx != NULL) && (mtx != &Giant))
722 lockmgr(lock, LK_EXCLUSIVE);
724 return ((usb_error_t)err);
727 /*------------------------------------------------------------------------*
728 * usbd_do_request_proc - factored out code
730 * This function is factored out code. It does basically the same like
731 * usbd_do_request_flags, except it will check the status of the
732 * passed process argument before doing the USB request. If the
733 * process is draining the USB_ERR_IOERROR code will be returned. It
734 * is assumed that the mutex associated with the process is locked
735 * when calling this function.
736 *------------------------------------------------------------------------*/
738 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
739 struct usb_device_request *req, void *data, uint16_t flags,
740 uint16_t *actlen, usb_timeout_t timeout)
745 /* get request data length */
746 len = UGETW(req->wLength);
748 /* check if the device is being detached */
749 if (usb_proc_is_gone(pproc)) {
750 err = USB_ERR_IOERROR;
754 /* forward the USB request */
755 err = usbd_do_request_flags(udev, pproc->up_lock,
756 req, data, flags, actlen, timeout);
759 /* on failure we zero the data */
760 /* on short packet we zero the unused data */
761 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
763 memset(data, 0, len);
764 else if (actlen && *actlen != len)
765 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
770 /*------------------------------------------------------------------------*
771 * usbd_req_reset_port
773 * This function will instruct a USB HUB to perform a reset sequence
774 * on the specified port number.
777 * 0: Success. The USB device should now be at address zero.
778 * Else: Failure. No USB device is present and the USB port should be
780 *------------------------------------------------------------------------*/
782 usbd_req_reset_port(struct usb_device *udev, struct lock *lock, uint8_t port)
784 struct usb_port_status ps;
792 /* clear any leftover port reset changes first */
793 usbd_req_clear_port_feature(
794 udev, lock, port, UHF_C_PORT_RESET);
796 /* assert port reset on the given port */
797 err = usbd_req_set_port_feature(
798 udev, lock, port, UHF_PORT_RESET);
800 /* check for errors */
806 /* wait for the device to recover from reset */
807 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_delay));
808 n += usb_port_reset_delay;
809 err = usbd_req_get_port_status(udev, lock, &ps, port);
813 status = UGETW(ps.wPortStatus);
814 change = UGETW(ps.wPortChange);
816 /* if the device disappeared, just give up */
817 if (!(status & UPS_CURRENT_CONNECT_STATUS))
820 /* check if reset is complete */
821 if (change & UPS_C_PORT_RESET)
825 * Some Virtual Machines like VirtualBox 4.x fail to
826 * generate a port reset change event. Check if reset
827 * is no longer asserted.
829 if (!(status & UPS_RESET))
832 /* check for timeout */
839 /* clear port reset first */
840 err = usbd_req_clear_port_feature(
841 udev, lock, port, UHF_C_PORT_RESET);
845 /* check for timeout */
847 err = USB_ERR_TIMEOUT;
850 /* wait for the device to recover from reset */
851 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
854 DPRINTFN(2, "port %d reset returning error=%s\n",
855 port, usbd_errstr(err));
859 /*------------------------------------------------------------------------*
860 * usbd_req_warm_reset_port
862 * This function will instruct an USB HUB to perform a warm reset
863 * sequence on the specified port number. This kind of reset is not
864 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
865 * for SUPER-speed USB HUBs.
868 * 0: Success. The USB device should now be available again.
869 * Else: Failure. No USB device is present and the USB port should be
871 *------------------------------------------------------------------------*/
873 usbd_req_warm_reset_port(struct usb_device *udev, struct lock *lock,
876 struct usb_port_status ps;
884 err = usbd_req_get_port_status(udev, lock, &ps, port);
888 status = UGETW(ps.wPortStatus);
890 switch (UPS_PORT_LINK_STATE_GET(status)) {
892 case UPS_PORT_LS_COMP_MODE:
893 case UPS_PORT_LS_LOOPBACK:
894 case UPS_PORT_LS_SS_INA:
897 DPRINTF("Wrong state for warm reset\n");
901 /* clear any leftover warm port reset changes first */
902 usbd_req_clear_port_feature(udev, lock,
903 port, UHF_C_BH_PORT_RESET);
905 /* set warm port reset */
906 err = usbd_req_set_port_feature(udev, lock,
907 port, UHF_BH_PORT_RESET);
913 /* wait for the device to recover from reset */
914 usb_pause_mtx(lock, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
915 n += USB_PORT_RESET_DELAY;
916 err = usbd_req_get_port_status(udev, lock, &ps, port);
920 status = UGETW(ps.wPortStatus);
921 change = UGETW(ps.wPortChange);
923 /* if the device disappeared, just give up */
924 if (!(status & UPS_CURRENT_CONNECT_STATUS))
927 /* check if reset is complete */
928 if (change & UPS_C_BH_PORT_RESET)
931 /* check for timeout */
938 /* clear port reset first */
939 err = usbd_req_clear_port_feature(
940 udev, lock, port, UHF_C_BH_PORT_RESET);
944 /* check for timeout */
946 err = USB_ERR_TIMEOUT;
949 /* wait for the device to recover from reset */
950 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_recovery));
953 DPRINTFN(2, "port %d warm reset returning error=%s\n",
954 port, usbd_errstr(err));
958 /*------------------------------------------------------------------------*
961 * This function can be used to retrieve USB descriptors. It contains
962 * some additional logic like zeroing of missing descriptor bytes and
963 * retrying an USB descriptor in case of failure. The "min_len"
964 * argument specifies the minimum descriptor length. The "max_len"
965 * argument specifies the maximum descriptor length. If the real
966 * descriptor length is less than the minimum length the missing
967 * byte(s) will be zeroed. The type field, the second byte of the USB
968 * descriptor, will get forced to the correct type. If the "actlen"
969 * pointer is non-NULL, the actual length of the transfer will get
970 * stored in the 16-bit unsigned integer which it is pointing to. The
971 * first byte of the descriptor will not get updated. If the "actlen"
972 * pointer is NULL the first byte of the descriptor will get updated
973 * to reflect the actual length instead. If "min_len" is not equal to
974 * "max_len" then this function will try to retrive the beginning of
975 * the descriptor and base the maximum length on the first byte of the
981 *------------------------------------------------------------------------*/
983 usbd_req_get_desc(struct usb_device *udev,
984 struct lock *lock, uint16_t *actlen, void *desc,
985 uint16_t min_len, uint16_t max_len,
986 uint16_t id, uint8_t type, uint8_t index,
989 struct usb_device_request req;
993 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
994 id, type, index, max_len);
996 req.bmRequestType = UT_READ_DEVICE;
997 req.bRequest = UR_GET_DESCRIPTOR;
998 USETW2(req.wValue, type, index);
999 USETW(req.wIndex, id);
1003 if ((min_len < 2) || (max_len < 2)) {
1004 err = USB_ERR_INVAL;
1007 USETW(req.wLength, min_len);
1009 err = usbd_do_request_flags(udev, lock, &req,
1010 desc, 0, NULL, 1000);
1018 usb_pause_mtx(lock, hz / 5);
1024 if (min_len == max_len) {
1026 /* enforce correct length */
1027 if ((buf[0] > min_len) && (actlen == NULL))
1030 /* enforce correct type */
1037 if (max_len > buf[0]) {
1040 /* zero minimum data */
1042 while (min_len > max_len) {
1047 /* set new minimum length */
1052 if (actlen != NULL) {
1061 /*------------------------------------------------------------------------*
1062 * usbd_req_get_string_any
1064 * This function will return the string given by "string_index"
1065 * using the first language ID. The maximum length "len" includes
1066 * the terminating zero. The "len" argument should be twice as
1067 * big pluss 2 bytes, compared with the actual maximum string length !
1072 *------------------------------------------------------------------------*/
1074 usbd_req_get_string_any(struct usb_device *udev, struct lock *lock, char *buf,
1075 uint16_t len, uint8_t string_index)
1086 /* should not happen */
1087 return (USB_ERR_NORMAL_COMPLETION);
1089 if (string_index == 0) {
1090 /* this is the language table */
1092 return (USB_ERR_INVAL);
1094 if (udev->flags.no_strings) {
1096 return (USB_ERR_STALLED);
1098 err = usbd_req_get_string_desc
1099 (udev, lock, buf, len, udev->langid, string_index);
1104 temp = (uint8_t *)buf;
1107 /* string length is too short */
1109 return (USB_ERR_INVAL);
1111 /* reserve one byte for terminating zero */
1114 /* find maximum length */
1116 n = (temp[0] / 2) - 1;
1120 /* skip descriptor header */
1123 /* reset swap state */
1126 /* convert and filter */
1127 for (i = 0; (i != n); i++) {
1128 c = UGETW(temp + (2 * i));
1130 /* convert from Unicode, handle buggy strings */
1131 if (((c & 0xff00) == 0) && (swap & 1)) {
1132 /* Little Endian, default */
1135 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1140 /* silently skip bad character */
1145 * Filter by default - We only allow alphanumerical
1146 * and a few more to avoid any problems with scripts
1159 /* silently skip bad character */
1161 *s = 0; /* zero terminate resulting string */
1162 return (USB_ERR_NORMAL_COMPLETION);
1165 /*------------------------------------------------------------------------*
1166 * usbd_req_get_string_desc
1168 * If you don't know the language ID, consider using
1169 * "usbd_req_get_string_any()".
1174 *------------------------------------------------------------------------*/
1176 usbd_req_get_string_desc(struct usb_device *udev, struct lock *lock, void *sdesc,
1177 uint16_t max_len, uint16_t lang_id,
1178 uint8_t string_index)
1180 return (usbd_req_get_desc(udev, lock, NULL, sdesc, 2, max_len, lang_id,
1181 UDESC_STRING, string_index, 0));
1184 /*------------------------------------------------------------------------*
1185 * usbd_req_get_config_desc_ptr
1187 * This function is used in device side mode to retrieve the pointer
1188 * to the generated config descriptor. This saves allocating space for
1189 * an additional config descriptor when setting the configuration.
1194 *------------------------------------------------------------------------*/
1196 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1197 struct usb_config_descriptor **ppcd, uint16_t wValue)
1199 struct usb_device_request req;
1200 usb_handle_req_t *hr_func;
1205 req.bmRequestType = UT_READ_DEVICE;
1206 req.bRequest = UR_GET_DESCRIPTOR;
1207 USETW(req.wValue, wValue);
1208 USETW(req.wIndex, 0);
1209 USETW(req.wLength, 0);
1214 hr_func = usbd_get_hr_func(udev);
1216 if (hr_func == NULL)
1217 err = USB_ERR_INVAL;
1219 USB_BUS_LOCK(udev->bus);
1220 err = (hr_func) (udev, &req, &ptr, &len);
1221 USB_BUS_UNLOCK(udev->bus);
1226 else if (ptr == NULL)
1227 err = USB_ERR_INVAL;
1229 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1234 /*------------------------------------------------------------------------*
1235 * usbd_req_get_config_desc
1240 *------------------------------------------------------------------------*/
1242 usbd_req_get_config_desc(struct usb_device *udev, struct lock *lock,
1243 struct usb_config_descriptor *d, uint8_t conf_index)
1247 DPRINTFN(4, "confidx=%d\n", conf_index);
1249 err = usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1250 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1254 /* Extra sanity checking */
1255 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1256 err = USB_ERR_INVAL;
1262 /*------------------------------------------------------------------------*
1263 * usbd_req_get_config_desc_full
1265 * This function gets the complete USB configuration descriptor and
1266 * ensures that "wTotalLength" is correct.
1271 *------------------------------------------------------------------------*/
1273 usbd_req_get_config_desc_full(struct usb_device *udev, struct lock *lock,
1274 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1277 struct usb_config_descriptor cd;
1278 struct usb_config_descriptor *cdesc;
1282 DPRINTFN(4, "index=%d\n", index);
1286 err = usbd_req_get_config_desc(udev, lock, &cd, index);
1290 /* get full descriptor */
1291 len = UGETW(cd.wTotalLength);
1292 if (len < sizeof(*cdesc)) {
1293 /* corrupt descriptor */
1294 return (USB_ERR_INVAL);
1296 cdesc = kmalloc(len, mtype, M_WAITOK);
1297 err = usbd_req_get_desc(udev, lock, NULL, cdesc, len, len, 0,
1298 UDESC_CONFIG, index, 3);
1300 kfree(cdesc, mtype);
1303 /* make sure that the device is not fooling us: */
1304 USETW(cdesc->wTotalLength, len);
1308 return (0); /* success */
1311 /*------------------------------------------------------------------------*
1312 * usbd_req_get_device_desc
1317 *------------------------------------------------------------------------*/
1319 usbd_req_get_device_desc(struct usb_device *udev, struct lock *lock,
1320 struct usb_device_descriptor *d)
1323 return (usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1324 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1327 /*------------------------------------------------------------------------*
1328 * usbd_req_get_alt_interface_no
1333 *------------------------------------------------------------------------*/
1335 usbd_req_get_alt_interface_no(struct usb_device *udev, struct lock *lock,
1336 uint8_t *alt_iface_no, uint8_t iface_index)
1338 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1339 struct usb_device_request req;
1341 if ((iface == NULL) || (iface->idesc == NULL))
1342 return (USB_ERR_INVAL);
1344 req.bmRequestType = UT_READ_INTERFACE;
1345 req.bRequest = UR_GET_INTERFACE;
1346 USETW(req.wValue, 0);
1347 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1349 USETW(req.wLength, 1);
1350 return (usbd_do_request(udev, lock, &req, alt_iface_no));
1353 /*------------------------------------------------------------------------*
1354 * usbd_req_set_alt_interface_no
1359 *------------------------------------------------------------------------*/
1361 usbd_req_set_alt_interface_no(struct usb_device *udev, struct lock *lock,
1362 uint8_t iface_index, uint8_t alt_no)
1364 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1365 struct usb_device_request req;
1367 if ((iface == NULL) || (iface->idesc == NULL))
1368 return (USB_ERR_INVAL);
1370 req.bmRequestType = UT_WRITE_INTERFACE;
1371 req.bRequest = UR_SET_INTERFACE;
1372 req.wValue[0] = alt_no;
1374 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1376 USETW(req.wLength, 0);
1377 return (usbd_do_request(udev, lock, &req, 0));
1380 /*------------------------------------------------------------------------*
1381 * usbd_req_get_device_status
1386 *------------------------------------------------------------------------*/
1388 usbd_req_get_device_status(struct usb_device *udev, struct lock *lock,
1389 struct usb_status *st)
1391 struct usb_device_request req;
1393 req.bmRequestType = UT_READ_DEVICE;
1394 req.bRequest = UR_GET_STATUS;
1395 USETW(req.wValue, 0);
1396 USETW(req.wIndex, 0);
1397 USETW(req.wLength, sizeof(*st));
1398 return (usbd_do_request(udev, lock, &req, st));
1401 /*------------------------------------------------------------------------*
1402 * usbd_req_get_hub_descriptor
1407 *------------------------------------------------------------------------*/
1409 usbd_req_get_hub_descriptor(struct usb_device *udev, struct lock *lock,
1410 struct usb_hub_descriptor *hd, uint8_t nports)
1412 struct usb_device_request req;
1413 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1415 req.bmRequestType = UT_READ_CLASS_DEVICE;
1416 req.bRequest = UR_GET_DESCRIPTOR;
1417 USETW2(req.wValue, UDESC_HUB, 0);
1418 USETW(req.wIndex, 0);
1419 USETW(req.wLength, len);
1420 return (usbd_do_request(udev, lock, &req, hd));
1423 /*------------------------------------------------------------------------*
1424 * usbd_req_get_ss_hub_descriptor
1429 *------------------------------------------------------------------------*/
1431 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct lock *lock,
1432 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1434 struct usb_device_request req;
1435 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1437 req.bmRequestType = UT_READ_CLASS_DEVICE;
1438 req.bRequest = UR_GET_DESCRIPTOR;
1439 USETW2(req.wValue, UDESC_SS_HUB, 0);
1440 USETW(req.wIndex, 0);
1441 USETW(req.wLength, len);
1442 return (usbd_do_request(udev, lock, &req, hd));
1445 /*------------------------------------------------------------------------*
1446 * usbd_req_get_hub_status
1451 *------------------------------------------------------------------------*/
1453 usbd_req_get_hub_status(struct usb_device *udev, struct lock *lock,
1454 struct usb_hub_status *st)
1456 struct usb_device_request req;
1458 req.bmRequestType = UT_READ_CLASS_DEVICE;
1459 req.bRequest = UR_GET_STATUS;
1460 USETW(req.wValue, 0);
1461 USETW(req.wIndex, 0);
1462 USETW(req.wLength, sizeof(struct usb_hub_status));
1463 return (usbd_do_request(udev, lock, &req, st));
1466 /*------------------------------------------------------------------------*
1467 * usbd_req_set_address
1469 * This function is used to set the address for an USB device. After
1470 * port reset the USB device will respond at address zero.
1475 *------------------------------------------------------------------------*/
1477 usbd_req_set_address(struct usb_device *udev, struct lock *lock, uint16_t addr)
1479 struct usb_device_request req;
1482 DPRINTFN(6, "setting device address=%d\n", addr);
1484 req.bmRequestType = UT_WRITE_DEVICE;
1485 req.bRequest = UR_SET_ADDRESS;
1486 USETW(req.wValue, addr);
1487 USETW(req.wIndex, 0);
1488 USETW(req.wLength, 0);
1490 err = USB_ERR_INVAL;
1492 /* check if USB controller handles set address */
1493 if (udev->bus->methods->set_address != NULL)
1494 err = (udev->bus->methods->set_address) (udev, lock, addr);
1496 if (err != USB_ERR_INVAL)
1499 /* Setting the address should not take more than 1 second ! */
1500 err = usbd_do_request_flags(udev, lock, &req, NULL,
1501 USB_DELAY_STATUS_STAGE, NULL, 1000);
1504 /* allow device time to set new address */
1506 USB_MS_TO_TICKS(usb_set_address_settle));
1511 /*------------------------------------------------------------------------*
1512 * usbd_req_get_port_status
1517 *------------------------------------------------------------------------*/
1519 usbd_req_get_port_status(struct usb_device *udev, struct lock *lock,
1520 struct usb_port_status *ps, uint8_t port)
1522 struct usb_device_request req;
1524 req.bmRequestType = UT_READ_CLASS_OTHER;
1525 req.bRequest = UR_GET_STATUS;
1526 USETW(req.wValue, 0);
1527 req.wIndex[0] = port;
1529 USETW(req.wLength, sizeof *ps);
1530 return (usbd_do_request(udev, lock, &req, ps));
1533 /*------------------------------------------------------------------------*
1534 * usbd_req_clear_hub_feature
1539 *------------------------------------------------------------------------*/
1541 usbd_req_clear_hub_feature(struct usb_device *udev, struct lock *lock,
1544 struct usb_device_request req;
1546 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1547 req.bRequest = UR_CLEAR_FEATURE;
1548 USETW(req.wValue, sel);
1549 USETW(req.wIndex, 0);
1550 USETW(req.wLength, 0);
1551 return (usbd_do_request(udev, lock, &req, 0));
1554 /*------------------------------------------------------------------------*
1555 * usbd_req_set_hub_feature
1560 *------------------------------------------------------------------------*/
1562 usbd_req_set_hub_feature(struct usb_device *udev, struct lock *lock,
1565 struct usb_device_request req;
1567 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1568 req.bRequest = UR_SET_FEATURE;
1569 USETW(req.wValue, sel);
1570 USETW(req.wIndex, 0);
1571 USETW(req.wLength, 0);
1572 return (usbd_do_request(udev, lock, &req, 0));
1575 /*------------------------------------------------------------------------*
1576 * usbd_req_set_hub_u1_timeout
1581 *------------------------------------------------------------------------*/
1583 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct lock *lock,
1584 uint8_t port, uint8_t timeout)
1586 struct usb_device_request req;
1588 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1589 req.bRequest = UR_SET_FEATURE;
1590 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1591 req.wIndex[0] = port;
1592 req.wIndex[1] = timeout;
1593 USETW(req.wLength, 0);
1594 return (usbd_do_request(udev, lock, &req, 0));
1597 /*------------------------------------------------------------------------*
1598 * usbd_req_set_hub_u2_timeout
1603 *------------------------------------------------------------------------*/
1605 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct lock *lock,
1606 uint8_t port, uint8_t timeout)
1608 struct usb_device_request req;
1610 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1611 req.bRequest = UR_SET_FEATURE;
1612 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1613 req.wIndex[0] = port;
1614 req.wIndex[1] = timeout;
1615 USETW(req.wLength, 0);
1616 return (usbd_do_request(udev, lock, &req, 0));
1619 /*------------------------------------------------------------------------*
1620 * usbd_req_set_hub_depth
1625 *------------------------------------------------------------------------*/
1627 usbd_req_set_hub_depth(struct usb_device *udev, struct lock *lock,
1630 struct usb_device_request req;
1632 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1633 req.bRequest = UR_SET_HUB_DEPTH;
1634 USETW(req.wValue, depth);
1635 USETW(req.wIndex, 0);
1636 USETW(req.wLength, 0);
1637 return (usbd_do_request(udev, lock, &req, 0));
1640 /*------------------------------------------------------------------------*
1641 * usbd_req_clear_port_feature
1646 *------------------------------------------------------------------------*/
1648 usbd_req_clear_port_feature(struct usb_device *udev, struct lock *lock,
1649 uint8_t port, uint16_t sel)
1651 struct usb_device_request req;
1653 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1654 req.bRequest = UR_CLEAR_FEATURE;
1655 USETW(req.wValue, sel);
1656 req.wIndex[0] = port;
1658 USETW(req.wLength, 0);
1659 return (usbd_do_request(udev, lock, &req, 0));
1662 /*------------------------------------------------------------------------*
1663 * usbd_req_set_port_feature
1668 *------------------------------------------------------------------------*/
1670 usbd_req_set_port_feature(struct usb_device *udev, struct lock *lock,
1671 uint8_t port, uint16_t sel)
1673 struct usb_device_request req;
1675 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1676 req.bRequest = UR_SET_FEATURE;
1677 USETW(req.wValue, sel);
1678 req.wIndex[0] = port;
1680 USETW(req.wLength, 0);
1681 return (usbd_do_request(udev, lock, &req, 0));
1684 /*------------------------------------------------------------------------*
1685 * usbd_req_set_protocol
1690 *------------------------------------------------------------------------*/
1692 usbd_req_set_protocol(struct usb_device *udev, struct lock *lock,
1693 uint8_t iface_index, uint16_t report)
1695 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1696 struct usb_device_request req;
1698 if ((iface == NULL) || (iface->idesc == NULL)) {
1699 return (USB_ERR_INVAL);
1701 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1702 iface, report, iface->idesc->bInterfaceNumber);
1704 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1705 req.bRequest = UR_SET_PROTOCOL;
1706 USETW(req.wValue, report);
1707 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1709 USETW(req.wLength, 0);
1710 return (usbd_do_request(udev, lock, &req, 0));
1713 /*------------------------------------------------------------------------*
1714 * usbd_req_set_report
1719 *------------------------------------------------------------------------*/
1721 usbd_req_set_report(struct usb_device *udev, struct lock *lock, void *data, uint16_t len,
1722 uint8_t iface_index, uint8_t type, uint8_t id)
1724 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1725 struct usb_device_request req;
1727 if ((iface == NULL) || (iface->idesc == NULL)) {
1728 return (USB_ERR_INVAL);
1730 DPRINTFN(5, "len=%d\n", len);
1732 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1733 req.bRequest = UR_SET_REPORT;
1734 USETW2(req.wValue, type, id);
1735 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1737 USETW(req.wLength, len);
1738 return (usbd_do_request(udev, lock, &req, data));
1741 /*------------------------------------------------------------------------*
1742 * usbd_req_get_report
1747 *------------------------------------------------------------------------*/
1749 usbd_req_get_report(struct usb_device *udev, struct lock *lock, void *data,
1750 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1752 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1753 struct usb_device_request req;
1755 if ((iface == NULL) || (iface->idesc == NULL)) {
1756 return (USB_ERR_INVAL);
1758 DPRINTFN(5, "len=%d\n", len);
1760 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1761 req.bRequest = UR_GET_REPORT;
1762 USETW2(req.wValue, type, id);
1763 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1765 USETW(req.wLength, len);
1766 return (usbd_do_request(udev, lock, &req, data));
1769 /*------------------------------------------------------------------------*
1775 *------------------------------------------------------------------------*/
1777 usbd_req_set_idle(struct usb_device *udev, struct lock *lock,
1778 uint8_t iface_index, uint8_t duration, uint8_t id)
1780 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1781 struct usb_device_request req;
1783 if ((iface == NULL) || (iface->idesc == NULL)) {
1784 return (USB_ERR_INVAL);
1786 DPRINTFN(5, "%d %d\n", duration, id);
1788 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1789 req.bRequest = UR_SET_IDLE;
1790 USETW2(req.wValue, duration, id);
1791 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1793 USETW(req.wLength, 0);
1794 return (usbd_do_request(udev, lock, &req, 0));
1797 /*------------------------------------------------------------------------*
1798 * usbd_req_get_report_descriptor
1803 *------------------------------------------------------------------------*/
1805 usbd_req_get_report_descriptor(struct usb_device *udev, struct lock *lock,
1806 void *d, uint16_t size, uint8_t iface_index)
1808 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1809 struct usb_device_request req;
1811 if ((iface == NULL) || (iface->idesc == NULL)) {
1812 return (USB_ERR_INVAL);
1814 req.bmRequestType = UT_READ_INTERFACE;
1815 req.bRequest = UR_GET_DESCRIPTOR;
1816 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1817 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1819 USETW(req.wLength, size);
1820 return (usbd_do_request(udev, lock, &req, d));
1823 /*------------------------------------------------------------------------*
1824 * usbd_req_set_config
1826 * This function is used to select the current configuration number in
1827 * both USB device side mode and USB host side mode. When setting the
1828 * configuration the function of the interfaces can change.
1833 *------------------------------------------------------------------------*/
1835 usbd_req_set_config(struct usb_device *udev, struct lock *lock, uint8_t conf)
1837 struct usb_device_request req;
1839 DPRINTF("setting config %d\n", conf);
1841 /* do "set configuration" request */
1843 req.bmRequestType = UT_WRITE_DEVICE;
1844 req.bRequest = UR_SET_CONFIG;
1845 req.wValue[0] = conf;
1847 USETW(req.wIndex, 0);
1848 USETW(req.wLength, 0);
1849 return (usbd_do_request(udev, lock, &req, 0));
1852 /*------------------------------------------------------------------------*
1853 * usbd_req_get_config
1858 *------------------------------------------------------------------------*/
1860 usbd_req_get_config(struct usb_device *udev, struct lock *lock, uint8_t *pconf)
1862 struct usb_device_request req;
1864 req.bmRequestType = UT_READ_DEVICE;
1865 req.bRequest = UR_GET_CONFIG;
1866 USETW(req.wValue, 0);
1867 USETW(req.wIndex, 0);
1868 USETW(req.wLength, 1);
1869 return (usbd_do_request(udev, lock, &req, pconf));
1872 /*------------------------------------------------------------------------*
1873 * usbd_setup_device_desc
1874 *------------------------------------------------------------------------*/
1876 usbd_setup_device_desc(struct usb_device *udev, struct lock *lock)
1881 * Get the first 8 bytes of the device descriptor !
1883 * NOTE: "usbd_do_request()" will check the device descriptor
1884 * next time we do a request to see if the maximum packet size
1885 * changed! The 8 first bytes of the device descriptor
1886 * contains the maximum packet size to use on control endpoint
1887 * 0. If this value is different from "USB_MAX_IPACKET" a new
1888 * USB control request will be setup!
1890 switch (udev->speed) {
1891 case USB_SPEED_FULL:
1893 err = usbd_req_get_desc(udev, lock, NULL, &udev->ddesc,
1894 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1896 DPRINTFN(0, "getting device descriptor "
1897 "at addr %d failed, %s\n", udev->address,
1903 DPRINTF("Minimum MaxPacketSize is large enough "
1904 "to hold the complete device descriptor\n");
1908 /* get the full device descriptor */
1909 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1911 /* try one more time, if error */
1913 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1916 DPRINTF("addr=%d, getting full desc failed\n",
1921 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1922 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1923 udev->address, UGETW(udev->ddesc.bcdUSB),
1924 udev->ddesc.bDeviceClass,
1925 udev->ddesc.bDeviceSubClass,
1926 udev->ddesc.bDeviceProtocol,
1927 udev->ddesc.bMaxPacketSize,
1928 udev->ddesc.bLength,
1934 /*------------------------------------------------------------------------*
1935 * usbd_req_re_enumerate
1937 * NOTE: After this function returns the hardware is in the
1938 * unconfigured state! The application is responsible for setting a
1939 * new configuration.
1944 *------------------------------------------------------------------------*/
1946 usbd_req_re_enumerate(struct usb_device *udev, struct lock *lock)
1948 struct usb_device *parent_hub;
1951 uint8_t do_retry = 1;
1953 if (udev->flags.usb_mode != USB_MODE_HOST) {
1954 return (USB_ERR_INVAL);
1956 old_addr = udev->address;
1957 parent_hub = udev->parent_hub;
1958 if (parent_hub == NULL) {
1959 return (USB_ERR_INVAL);
1963 * Try to reset the High Speed parent HUB of a LOW- or FULL-
1964 * speed device, if any.
1966 if (udev->parent_hs_hub != NULL &&
1967 udev->speed != USB_SPEED_HIGH) {
1968 DPRINTF("Trying to reset parent High Speed TT.\n");
1969 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
1972 DPRINTF("Resetting parent High "
1973 "Speed TT failed (%s).\n",
1978 /* Try to warm reset first */
1979 if (parent_hub->speed == USB_SPEED_SUPER)
1980 usbd_req_warm_reset_port(parent_hub, lock, udev->port_no);
1982 /* Try to reset the parent HUB port. */
1983 err = usbd_req_reset_port(parent_hub, lock, udev->port_no);
1985 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1986 old_addr, usbd_errstr(err));
1991 * After that the port has been reset our device should be at
1994 udev->address = USB_START_ADDR;
1996 /* reset "bMaxPacketSize" */
1997 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1999 /* reset USB state */
2000 usb_set_device_state(udev, USB_STATE_POWERED);
2003 * Restore device address:
2005 err = usbd_req_set_address(udev, lock, old_addr);
2007 /* XXX ignore any errors! */
2008 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2009 old_addr, usbd_errstr(err));
2012 * Restore device address, if the controller driver did not
2015 if (udev->address == USB_START_ADDR)
2016 udev->address = old_addr;
2018 /* setup the device descriptor and the initial "wMaxPacketSize" */
2019 err = usbd_setup_device_desc(udev, lock);
2022 if (err && do_retry) {
2023 /* give the USB firmware some time to load */
2024 usb_pause_mtx(lock, hz / 2);
2025 /* no more retries after this retry */
2030 /* restore address */
2031 if (udev->address == USB_START_ADDR)
2032 udev->address = old_addr;
2033 /* update state, if successful */
2035 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2039 /*------------------------------------------------------------------------*
2040 * usbd_req_clear_device_feature
2045 *------------------------------------------------------------------------*/
2047 usbd_req_clear_device_feature(struct usb_device *udev, struct lock *lock,
2050 struct usb_device_request req;
2052 req.bmRequestType = UT_WRITE_DEVICE;
2053 req.bRequest = UR_CLEAR_FEATURE;
2054 USETW(req.wValue, sel);
2055 USETW(req.wIndex, 0);
2056 USETW(req.wLength, 0);
2057 return (usbd_do_request(udev, lock, &req, 0));
2060 /*------------------------------------------------------------------------*
2061 * usbd_req_set_device_feature
2066 *------------------------------------------------------------------------*/
2068 usbd_req_set_device_feature(struct usb_device *udev, struct lock *lock,
2071 struct usb_device_request req;
2073 req.bmRequestType = UT_WRITE_DEVICE;
2074 req.bRequest = UR_SET_FEATURE;
2075 USETW(req.wValue, sel);
2076 USETW(req.wIndex, 0);
2077 USETW(req.wLength, 0);
2078 return (usbd_do_request(udev, lock, &req, 0));
2081 /*------------------------------------------------------------------------*
2087 *------------------------------------------------------------------------*/
2089 usbd_req_reset_tt(struct usb_device *udev, struct lock *lock,
2092 struct usb_device_request req;
2094 /* For single TT HUBs the port should be 1 */
2096 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2097 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2100 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2101 req.bRequest = UR_RESET_TT;
2102 USETW(req.wValue, 0);
2103 req.wIndex[0] = port;
2105 USETW(req.wLength, 0);
2106 return (usbd_do_request(udev, lock, &req, 0));
2109 /*------------------------------------------------------------------------*
2110 * usbd_req_clear_tt_buffer
2112 * For single TT HUBs the port should be 1.
2117 *------------------------------------------------------------------------*/
2119 usbd_req_clear_tt_buffer(struct usb_device *udev, struct lock *lock,
2120 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2122 struct usb_device_request req;
2125 /* For single TT HUBs the port should be 1 */
2127 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2128 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2131 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2132 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2134 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2135 req.bRequest = UR_CLEAR_TT_BUFFER;
2136 USETW(req.wValue, wValue);
2137 req.wIndex[0] = port;
2139 USETW(req.wLength, 0);
2140 return (usbd_do_request(udev, lock, &req, 0));
2143 /*------------------------------------------------------------------------*
2144 * usbd_req_set_port_link_state
2146 * USB 3.0 specific request
2151 *------------------------------------------------------------------------*/
2153 usbd_req_set_port_link_state(struct usb_device *udev, struct lock *lock,
2154 uint8_t port, uint8_t link_state)
2156 struct usb_device_request req;
2158 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2159 req.bRequest = UR_SET_FEATURE;
2160 USETW(req.wValue, UHF_PORT_LINK_STATE);
2161 req.wIndex[0] = port;
2162 req.wIndex[1] = link_state;
2163 USETW(req.wLength, 0);
2164 return (usbd_do_request(udev, lock, &req, 0));
2167 /*------------------------------------------------------------------------*
2168 * usbd_req_set_lpm_info
2170 * USB 2.0 specific request for Link Power Management.
2174 * USB_ERR_PENDING_REQUESTS: NYET
2175 * USB_ERR_TIMEOUT: TIMEOUT
2176 * USB_ERR_STALL: STALL
2178 *------------------------------------------------------------------------*/
2180 usbd_req_set_lpm_info(struct usb_device *udev, struct lock *lock,
2181 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2183 struct usb_device_request req;
2187 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2188 req.bRequest = UR_SET_AND_TEST;
2189 USETW(req.wValue, UHF_PORT_L1);
2190 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2191 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2192 USETW(req.wLength, sizeof(buf));
2194 /* set default value in case of short transfer */
2197 err = usbd_do_request(udev, lock, &req, buf);
2202 case 0x00: /* SUCCESS */
2204 case 0x10: /* NYET */
2205 err = USB_ERR_PENDING_REQUESTS;
2207 case 0x11: /* TIMEOUT */
2208 err = USB_ERR_TIMEOUT;
2210 case 0x30: /* STALL */
2211 err = USB_ERR_STALLED;
2213 default: /* reserved */
2214 err = USB_ERR_IOERROR;