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/usbhid.h>
53 #define USB_DEBUG_VAR usb_debug
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_request.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_transfer.h>
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_util.h>
63 #include <dev/usb/usb_dynamic.h>
65 #include <dev/usb/usb_controller.h>
66 #include <dev/usb/usb_bus.h>
67 #include <sys/ctype.h>
69 static int usb_no_cs_fail;
71 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
72 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
74 static int usb_full_ddesc;
76 SYSCTL_INT(_hw_usb, OID_AUTO, full_ddesc, CTLFLAG_RW,
77 &usb_full_ddesc, 0, "USB always read complete device descriptor, if set");
81 /* The following structures are used in connection to fault injection. */
82 struct usb_ctrl_debug {
83 int bus_index; /* target bus */
84 int dev_index; /* target address */
85 int ds_fail; /* fail data stage */
86 int ss_fail; /* fail status stage */
87 int ds_delay; /* data stage delay in ms */
88 int ss_delay; /* status stage delay in ms */
89 int bmRequestType_value;
93 struct usb_ctrl_debug_bits {
101 /* The default is to disable fault injection. */
103 static struct usb_ctrl_debug usb_ctrl_debug = {
106 .bmRequestType_value = -1,
107 .bRequest_value = -1,
110 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
111 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
112 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
113 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
114 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
115 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
117 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
119 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
120 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
121 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
122 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
123 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
124 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
125 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
127 /*------------------------------------------------------------------------*
128 * usbd_get_debug_bits
130 * This function is only useful in USB host mode.
131 *------------------------------------------------------------------------*/
133 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
134 struct usb_ctrl_debug_bits *dbg)
138 memset(dbg, 0, sizeof(*dbg));
140 /* Compute data stage delay */
142 temp = usb_ctrl_debug.ds_delay;
145 else if (temp > (16*1024))
148 dbg->ds_delay = temp;
150 /* Compute status stage delay */
152 temp = usb_ctrl_debug.ss_delay;
155 else if (temp > (16*1024))
158 dbg->ss_delay = temp;
160 /* Check if this control request should be failed */
162 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
165 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
168 temp = usb_ctrl_debug.bmRequestType_value;
170 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
173 temp = usb_ctrl_debug.bRequest_value;
175 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
178 temp = usb_ctrl_debug.ds_fail;
182 temp = usb_ctrl_debug.ss_fail;
188 #endif /* USB_REQ_DEBUG */
189 #endif /* USB_DEBUG */
191 /*------------------------------------------------------------------------*
192 * usbd_do_request_callback
194 * This function is the USB callback for generic USB Host control
196 *------------------------------------------------------------------------*/
198 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
200 ; /* workaround for a bug in "indent" */
202 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
204 switch (USB_GET_STATE(xfer)) {
206 usbd_transfer_submit(xfer);
209 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
214 /*------------------------------------------------------------------------*
215 * usb_do_clear_stall_callback
217 * This function is the USB callback for generic clear stall requests.
218 *------------------------------------------------------------------------*/
220 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
222 struct usb_device_request req;
223 struct usb_device *udev;
224 struct usb_endpoint *ep;
225 struct usb_endpoint *ep_end;
226 struct usb_endpoint *ep_first;
229 udev = xfer->xroot->udev;
231 USB_BUS_LOCK(udev->bus);
233 /* round robin endpoint clear stall */
236 ep_end = udev->endpoints + udev->endpoints_max;
237 ep_first = udev->endpoints;
238 to = udev->endpoints_max;
240 switch (USB_GET_STATE(xfer)) {
241 case USB_ST_TRANSFERRED:
243 /* reset error counter */
244 udev->clear_stall_errors = 0;
247 goto tr_setup; /* device was unconfigured */
252 /* some hardware needs a callback to clear the data toggle */
253 usbd_clear_stall_locked(udev, ep);
254 /* start up the current or next transfer, if any */
255 usb_command_wrapper(&ep->endpoint_q,
256 ep->endpoint_q.curr);
263 break; /* no endpoints - nothing to do */
264 if ((ep < ep_first) || (ep >= ep_end))
265 ep = ep_first; /* endpoint wrapped around */
269 /* setup a clear-stall packet */
271 req.bmRequestType = UT_WRITE_ENDPOINT;
272 req.bRequest = UR_CLEAR_FEATURE;
273 USETW(req.wValue, UF_ENDPOINT_HALT);
274 req.wIndex[0] = ep->edesc->bEndpointAddress;
276 USETW(req.wLength, 0);
278 /* copy in the transfer */
280 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
283 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
285 USB_BUS_UNLOCK(udev->bus);
287 usbd_transfer_submit(xfer);
289 USB_BUS_LOCK(udev->bus);
297 if (error == USB_ERR_CANCELLED)
300 DPRINTF("Clear stall failed.\n");
303 * Some VMs like VirtualBox always return failure on
304 * clear-stall which we sometimes should just ignore.
308 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
311 if (error == USB_ERR_TIMEOUT) {
312 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
313 DPRINTF("Trying to re-enumerate.\n");
314 usbd_start_re_enumerate(udev);
316 udev->clear_stall_errors++;
317 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
318 DPRINTF("Trying to re-enumerate.\n");
319 usbd_start_re_enumerate(udev);
325 /* store current endpoint */
327 USB_BUS_UNLOCK(udev->bus);
330 static usb_handle_req_t *
331 usbd_get_hr_func(struct usb_device *udev)
333 /* figure out if there is a Handle Request function */
334 if (udev->flags.usb_mode == USB_MODE_DEVICE)
335 return (usb_temp_get_desc_p);
336 else if (udev->parent_hub == NULL)
337 return (udev->bus->methods->roothub_exec);
342 /*------------------------------------------------------------------------*
343 * usbd_do_request_flags and usbd_do_request
345 * Description of arguments passed to these functions:
347 * "udev" - this is the "usb_device" structure pointer on which the
348 * request should be performed. It is possible to call this function
349 * in both Host Side mode and Device Side mode.
351 * "mtx" - if this argument is non-NULL the mutex pointed to by it
352 * will get dropped and picked up during the execution of this
353 * function, hence this function sometimes needs to sleep. If this
354 * argument is NULL it has no effect.
356 * "req" - this argument must always be non-NULL and points to an
357 * 8-byte structure holding the USB request to be done. The USB
358 * request structure has a bit telling the direction of the USB
359 * request, if it is a read or a write.
361 * "data" - if the "wLength" part of the structure pointed to by "req"
362 * is non-zero this argument must point to a valid kernel buffer which
363 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
366 * "flags" - here is a list of valid flags:
368 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
371 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
372 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
373 * sysctl. This flag is mostly useful for debugging.
375 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
378 * "actlen" - if non-NULL the actual transfer length will be stored in
379 * the 16-bit unsigned integer pointed to by "actlen". This
380 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
383 * "timeout" - gives the timeout for the control transfer in
384 * milliseconds. A "timeout" value less than 50 milliseconds is
385 * treated like a 50 millisecond timeout. A "timeout" value greater
386 * than 30 seconds is treated like a 30 second timeout. This USB stack
387 * does not allow control requests without a timeout.
389 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
390 * will be serialized by the use of the USB device enumeration lock.
395 *------------------------------------------------------------------------*/
397 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
398 struct usb_device_request *req, void *data, uint16_t flags,
399 uint16_t *actlen, usb_timeout_t timeout)
402 struct usb_ctrl_debug_bits dbg;
404 usb_handle_req_t *hr_func;
405 struct usb_xfer *xfer;
408 usb_ticks_t start_ticks;
409 usb_ticks_t delta_ticks;
410 usb_ticks_t max_ticks;
417 /* timeout is too small */
420 if (timeout > 30000) {
421 /* timeout is too big */
424 length = UGETW(req->wLength);
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);
450 if ((mtx != NULL) && (mtx != &Giant)) {
452 mtx_assert(mtx, MA_NOTOWNED);
456 * Serialize access to this function:
458 do_unlock = usbd_ctrl_lock(udev);
460 hr_func = usbd_get_hr_func(udev);
462 if (hr_func != NULL) {
463 DPRINTF("Handle Request function is set\n");
468 if (!(req->bmRequestType & UT_READ)) {
470 DPRINTFN(1, "The handle request function "
471 "does not support writing data!\n");
477 /* The root HUB code needs the BUS lock locked */
479 USB_BUS_LOCK(udev->bus);
480 err = (hr_func) (udev, req, &desc, &temp);
481 USB_BUS_UNLOCK(udev->bus);
487 if (!(flags & USB_SHORT_XFER_OK)) {
488 err = USB_ERR_SHORT_XFER;
498 if (flags & USB_USER_DATA_PTR) {
499 if (copyout(desc, data, length)) {
505 memcpy(data, desc, length);
507 goto done; /* success */
511 * Setup a new USB transfer or use the existing one, if any:
513 usbd_ctrl_transfer_setup(udev);
515 xfer = udev->ctrl_xfer[0];
517 /* most likely out of memory */
524 usbd_get_debug_bits(udev, req, &dbg);
526 /* Check for fault injection */
528 flags |= USB_DELAY_STATUS_STAGE;
532 if (flags & USB_DELAY_STATUS_STAGE)
533 xfer->flags.manual_status = 1;
535 xfer->flags.manual_status = 0;
537 if (flags & USB_SHORT_XFER_OK)
538 xfer->flags.short_xfer_ok = 1;
540 xfer->flags.short_xfer_ok = 0;
542 xfer->timeout = timeout;
546 max_ticks = USB_MS_TO_TICKS(timeout);
548 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
550 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
554 if (temp > usbd_xfer_max_len(xfer)) {
555 temp = usbd_xfer_max_len(xfer);
558 if (xfer->flags.manual_status) {
559 if (usbd_xfer_frame_len(xfer, 0) != 0) {
560 /* Execute data stage separately */
562 } else if (temp > 0) {
567 if (dbg.ds_delay > 0) {
569 xfer->xroot->xfer_mtx,
570 USB_MS_TO_TICKS(dbg.ds_delay));
571 /* make sure we don't time out */
577 usbd_xfer_set_frame_len(xfer, 1, temp);
580 if (!(req->bmRequestType & UT_READ)) {
582 if (flags & USB_USER_DATA_PTR) {
583 USB_XFER_UNLOCK(xfer);
584 err = usbd_copy_in_user(xfer->frbuffers + 1,
593 usbd_copy_in(xfer->frbuffers + 1,
596 usbd_xfer_set_frames(xfer, 2);
598 if (usbd_xfer_frame_len(xfer, 0) == 0) {
599 if (xfer->flags.manual_status) {
605 if (dbg.ss_delay > 0) {
607 xfer->xroot->xfer_mtx,
608 USB_MS_TO_TICKS(dbg.ss_delay));
609 /* make sure we don't time out */
613 xfer->flags.manual_status = 0;
618 usbd_xfer_set_frames(xfer, 1);
621 usbd_transfer_start(xfer);
623 while (usbd_transfer_pending(xfer)) {
624 cv_wait(&udev->ctrlreq_cv,
625 xfer->xroot->xfer_mtx);
634 /* get actual length of DATA stage */
636 if (xfer->aframes < 2) {
639 acttemp = usbd_xfer_frame_len(xfer, 1);
642 /* check for short packet */
644 if (temp > acttemp) {
649 if (req->bmRequestType & UT_READ) {
651 if (flags & USB_USER_DATA_PTR) {
652 USB_XFER_UNLOCK(xfer);
653 err = usbd_copy_out_user(xfer->frbuffers + 1,
662 usbd_copy_out(xfer->frbuffers + 1,
667 * Clear "frlengths[0]" so that we don't send the setup
670 usbd_xfer_set_frame_len(xfer, 0, 0);
672 /* update length and data pointer */
674 data = USB_ADD_BYTES(data, temp);
679 /* check for timeout */
681 delta_ticks = ticks - start_ticks;
682 if (delta_ticks > max_ticks) {
684 err = USB_ERR_TIMEOUT;
694 * Make sure that the control endpoint is no longer
695 * blocked in case of a non-transfer related error:
697 usbd_transfer_stop(xfer);
699 USB_XFER_UNLOCK(xfer);
703 usbd_ctrl_unlock(udev);
705 if ((mtx != NULL) && (mtx != &Giant))
709 case USB_ERR_NORMAL_COMPLETION:
710 case USB_ERR_SHORT_XFER:
711 case USB_ERR_STALLED:
712 case USB_ERR_CANCELLED:
715 DPRINTF("I/O error - waiting a bit for TT cleanup\n");
716 usb_pause_mtx(mtx, hz / 16);
719 return ((usb_error_t)err);
722 /*------------------------------------------------------------------------*
723 * usbd_do_request_proc - factored out code
725 * This function is factored out code. It does basically the same like
726 * usbd_do_request_flags, except it will check the status of the
727 * passed process argument before doing the USB request. If the
728 * process is draining the USB_ERR_IOERROR code will be returned. It
729 * is assumed that the mutex associated with the process is locked
730 * when calling this function.
731 *------------------------------------------------------------------------*/
733 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
734 struct usb_device_request *req, void *data, uint16_t flags,
735 uint16_t *actlen, usb_timeout_t timeout)
740 /* get request data length */
741 len = UGETW(req->wLength);
743 /* check if the device is being detached */
744 if (usb_proc_is_gone(pproc)) {
745 err = USB_ERR_IOERROR;
749 /* forward the USB request */
750 err = usbd_do_request_flags(udev, pproc->up_mtx,
751 req, data, flags, actlen, timeout);
754 /* on failure we zero the data */
755 /* on short packet we zero the unused data */
756 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
758 memset(data, 0, len);
759 else if (actlen && *actlen != len)
760 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
765 /*------------------------------------------------------------------------*
766 * usbd_req_reset_port
768 * This function will instruct a USB HUB to perform a reset sequence
769 * on the specified port number.
772 * 0: Success. The USB device should now be at address zero.
773 * Else: Failure. No USB device is present and the USB port should be
775 *------------------------------------------------------------------------*/
777 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
779 struct usb_port_status ps;
787 /* clear any leftover port reset changes first */
788 usbd_req_clear_port_feature(
789 udev, mtx, port, UHF_C_PORT_RESET);
791 /* assert port reset on the given port */
792 err = usbd_req_set_port_feature(
793 udev, mtx, port, UHF_PORT_RESET);
795 /* check for errors */
800 /* wait for the device to recover from reset */
801 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
802 n += usb_port_reset_delay;
803 err = usbd_req_get_port_status(udev, mtx, &ps, port);
807 status = UGETW(ps.wPortStatus);
808 change = UGETW(ps.wPortChange);
810 /* if the device disappeared, just give up */
811 if (!(status & UPS_CURRENT_CONNECT_STATUS))
814 /* check if reset is complete */
815 if (change & UPS_C_PORT_RESET)
819 * Some Virtual Machines like VirtualBox 4.x fail to
820 * generate a port reset change event. Check if reset
821 * is no longer asserted.
823 if (!(status & UPS_RESET))
826 /* check for timeout */
833 /* clear port reset first */
834 err = usbd_req_clear_port_feature(
835 udev, mtx, port, UHF_C_PORT_RESET);
839 /* check for timeout */
841 err = USB_ERR_TIMEOUT;
844 /* wait for the device to recover from reset */
845 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
848 DPRINTFN(2, "port %d reset returning error=%s\n",
849 port, usbd_errstr(err));
853 /*------------------------------------------------------------------------*
854 * usbd_req_warm_reset_port
856 * This function will instruct an USB HUB to perform a warm reset
857 * sequence on the specified port number. This kind of reset is not
858 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
859 * for SUPER-speed USB HUBs.
862 * 0: Success. The USB device should now be available again.
863 * Else: Failure. No USB device is present and the USB port should be
865 *------------------------------------------------------------------------*/
867 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
870 struct usb_port_status ps;
878 err = usbd_req_get_port_status(udev, mtx, &ps, port);
882 status = UGETW(ps.wPortStatus);
884 switch (UPS_PORT_LINK_STATE_GET(status)) {
886 case UPS_PORT_LS_COMP_MODE:
887 case UPS_PORT_LS_LOOPBACK:
888 case UPS_PORT_LS_SS_INA:
891 DPRINTF("Wrong state for warm reset\n");
895 /* clear any leftover warm port reset changes first */
896 usbd_req_clear_port_feature(udev, mtx,
897 port, UHF_C_BH_PORT_RESET);
899 /* set warm port reset */
900 err = usbd_req_set_port_feature(udev, mtx,
901 port, UHF_BH_PORT_RESET);
907 /* wait for the device to recover from reset */
908 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
909 n += usb_port_reset_delay;
910 err = usbd_req_get_port_status(udev, mtx, &ps, port);
914 status = UGETW(ps.wPortStatus);
915 change = UGETW(ps.wPortChange);
917 /* if the device disappeared, just give up */
918 if (!(status & UPS_CURRENT_CONNECT_STATUS))
921 /* check if reset is complete */
922 if (change & UPS_C_BH_PORT_RESET)
925 /* check for timeout */
932 /* clear port reset first */
933 err = usbd_req_clear_port_feature(
934 udev, mtx, port, UHF_C_BH_PORT_RESET);
938 /* check for timeout */
940 err = USB_ERR_TIMEOUT;
943 /* wait for the device to recover from reset */
944 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
947 DPRINTFN(2, "port %d warm reset returning error=%s\n",
948 port, usbd_errstr(err));
952 /*------------------------------------------------------------------------*
955 * This function can be used to retrieve USB descriptors. It contains
956 * some additional logic like zeroing of missing descriptor bytes and
957 * retrying an USB descriptor in case of failure. The "min_len"
958 * argument specifies the minimum descriptor length. The "max_len"
959 * argument specifies the maximum descriptor length. If the real
960 * descriptor length is less than the minimum length the missing
961 * byte(s) will be zeroed. The type field, the second byte of the USB
962 * descriptor, will get forced to the correct type. If the "actlen"
963 * pointer is non-NULL, the actual length of the transfer will get
964 * stored in the 16-bit unsigned integer which it is pointing to. The
965 * first byte of the descriptor will not get updated. If the "actlen"
966 * pointer is NULL the first byte of the descriptor will get updated
967 * to reflect the actual length instead. If "min_len" is not equal to
968 * "max_len" then this function will try to retrive the beginning of
969 * the descriptor and base the maximum length on the first byte of the
975 *------------------------------------------------------------------------*/
977 usbd_req_get_desc(struct usb_device *udev,
978 struct mtx *mtx, uint16_t *actlen, void *desc,
979 uint16_t min_len, uint16_t max_len,
980 uint16_t id, uint8_t type, uint8_t index,
983 struct usb_device_request req;
987 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
988 id, type, index, max_len);
990 req.bmRequestType = UT_READ_DEVICE;
991 req.bRequest = UR_GET_DESCRIPTOR;
992 USETW2(req.wValue, type, index);
993 USETW(req.wIndex, id);
997 if ((min_len < 2) || (max_len < 2)) {
1001 USETW(req.wLength, min_len);
1003 err = usbd_do_request_flags(udev, mtx, &req,
1004 desc, 0, NULL, 500 /* ms */);
1006 if (err != 0 && err != USB_ERR_TIMEOUT &&
1007 min_len != max_len) {
1008 /* clear descriptor data */
1009 memset(desc, 0, max_len);
1011 /* try to read full descriptor length */
1012 USETW(req.wLength, max_len);
1014 err = usbd_do_request_flags(udev, mtx, &req,
1015 desc, USB_SHORT_XFER_OK, NULL, 500 /* ms */);
1019 if (buf[0] > max_len)
1021 else if (buf[0] < 2)
1022 err = USB_ERR_INVAL;
1026 /* enforce descriptor type */
1038 usb_pause_mtx(mtx, hz / 5);
1043 if (min_len == max_len) {
1045 /* enforce correct length */
1046 if ((buf[0] > min_len) && (actlen == NULL))
1049 /* enforce correct type */
1056 if (max_len > buf[0]) {
1059 /* zero minimum data */
1061 while (min_len > max_len) {
1066 /* set new minimum length */
1071 if (actlen != NULL) {
1080 /*------------------------------------------------------------------------*
1081 * usbd_req_get_string_any
1083 * This function will return the string given by "string_index"
1084 * using the first language ID. The maximum length "len" includes
1085 * the terminating zero. The "len" argument should be twice as
1086 * big pluss 2 bytes, compared with the actual maximum string length !
1091 *------------------------------------------------------------------------*/
1093 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1094 uint16_t len, uint8_t string_index)
1105 /* should not happen */
1106 return (USB_ERR_NORMAL_COMPLETION);
1108 if (string_index == 0) {
1109 /* this is the language table */
1111 return (USB_ERR_INVAL);
1113 if (udev->flags.no_strings) {
1115 return (USB_ERR_STALLED);
1117 err = usbd_req_get_string_desc
1118 (udev, mtx, buf, len, udev->langid, string_index);
1123 temp = (uint8_t *)buf;
1126 /* string length is too short */
1128 return (USB_ERR_INVAL);
1130 /* reserve one byte for terminating zero */
1133 /* find maximum length */
1135 n = (temp[0] / 2) - 1;
1139 /* skip descriptor header */
1142 /* reset swap state */
1145 /* convert and filter */
1146 for (i = 0; (i != n); i++) {
1147 c = UGETW(temp + (2 * i));
1149 /* convert from Unicode, handle buggy strings */
1150 if (((c & 0xff00) == 0) && (swap & 1)) {
1151 /* Little Endian, default */
1154 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1159 /* silently skip bad character */
1164 * Filter by default - We only allow alphanumerical
1165 * and a few more to avoid any problems with scripts
1178 /* silently skip bad character */
1180 *s = 0; /* zero terminate resulting string */
1181 return (USB_ERR_NORMAL_COMPLETION);
1184 /*------------------------------------------------------------------------*
1185 * usbd_req_get_string_desc
1187 * If you don't know the language ID, consider using
1188 * "usbd_req_get_string_any()".
1193 *------------------------------------------------------------------------*/
1195 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1196 uint16_t max_len, uint16_t lang_id,
1197 uint8_t string_index)
1199 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1200 UDESC_STRING, string_index, 0));
1203 /*------------------------------------------------------------------------*
1204 * usbd_req_get_config_desc_ptr
1206 * This function is used in device side mode to retrieve the pointer
1207 * to the generated config descriptor. This saves allocating space for
1208 * an additional config descriptor when setting the configuration.
1213 *------------------------------------------------------------------------*/
1215 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1216 struct usb_config_descriptor **ppcd, uint16_t wValue)
1218 struct usb_device_request req;
1219 usb_handle_req_t *hr_func;
1224 req.bmRequestType = UT_READ_DEVICE;
1225 req.bRequest = UR_GET_DESCRIPTOR;
1226 USETW(req.wValue, wValue);
1227 USETW(req.wIndex, 0);
1228 USETW(req.wLength, 0);
1233 hr_func = usbd_get_hr_func(udev);
1235 if (hr_func == NULL)
1236 err = USB_ERR_INVAL;
1238 USB_BUS_LOCK(udev->bus);
1239 err = (hr_func) (udev, &req, &ptr, &len);
1240 USB_BUS_UNLOCK(udev->bus);
1245 else if (ptr == NULL)
1246 err = USB_ERR_INVAL;
1248 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1253 /*------------------------------------------------------------------------*
1254 * usbd_req_get_config_desc
1259 *------------------------------------------------------------------------*/
1261 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1262 struct usb_config_descriptor *d, uint8_t conf_index)
1266 DPRINTFN(4, "confidx=%d\n", conf_index);
1268 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1269 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1273 /* Extra sanity checking */
1274 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1275 err = USB_ERR_INVAL;
1281 /*------------------------------------------------------------------------*
1282 * usbd_req_get_config_desc_full
1284 * This function gets the complete USB configuration descriptor and
1285 * ensures that "wTotalLength" is correct.
1290 *------------------------------------------------------------------------*/
1292 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1293 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1296 struct usb_config_descriptor cd;
1297 struct usb_config_descriptor *cdesc;
1301 DPRINTFN(4, "index=%d\n", index);
1305 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1309 /* get full descriptor */
1310 len = UGETW(cd.wTotalLength);
1311 if (len < sizeof(*cdesc)) {
1312 /* corrupt descriptor */
1313 return (USB_ERR_INVAL);
1315 cdesc = malloc(len, mtype, M_WAITOK);
1316 if (cdesc == NULL) {
1317 return (USB_ERR_NOMEM);
1319 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1320 UDESC_CONFIG, index, 3);
1325 /* make sure that the device is not fooling us: */
1326 USETW(cdesc->wTotalLength, len);
1330 return (0); /* success */
1333 /*------------------------------------------------------------------------*
1334 * usbd_req_get_device_desc
1339 *------------------------------------------------------------------------*/
1341 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1342 struct usb_device_descriptor *d)
1345 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1346 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1349 /*------------------------------------------------------------------------*
1350 * usbd_req_get_alt_interface_no
1355 *------------------------------------------------------------------------*/
1357 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1358 uint8_t *alt_iface_no, uint8_t iface_index)
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_READ_INTERFACE;
1367 req.bRequest = UR_GET_INTERFACE;
1368 USETW(req.wValue, 0);
1369 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1371 USETW(req.wLength, 1);
1372 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1375 /*------------------------------------------------------------------------*
1376 * usbd_req_set_alt_interface_no
1381 *------------------------------------------------------------------------*/
1383 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1384 uint8_t iface_index, uint8_t alt_no)
1386 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1387 struct usb_device_request req;
1389 if ((iface == NULL) || (iface->idesc == NULL))
1390 return (USB_ERR_INVAL);
1392 req.bmRequestType = UT_WRITE_INTERFACE;
1393 req.bRequest = UR_SET_INTERFACE;
1394 req.wValue[0] = alt_no;
1396 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1398 USETW(req.wLength, 0);
1399 return (usbd_do_request(udev, mtx, &req, 0));
1402 /*------------------------------------------------------------------------*
1403 * usbd_req_get_device_status
1408 *------------------------------------------------------------------------*/
1410 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1411 struct usb_status *st)
1413 struct usb_device_request req;
1415 req.bmRequestType = UT_READ_DEVICE;
1416 req.bRequest = UR_GET_STATUS;
1417 USETW(req.wValue, 0);
1418 USETW(req.wIndex, 0);
1419 USETW(req.wLength, sizeof(*st));
1420 return (usbd_do_request(udev, mtx, &req, st));
1423 /*------------------------------------------------------------------------*
1424 * usbd_req_get_hub_descriptor
1429 *------------------------------------------------------------------------*/
1431 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1432 struct usb_hub_descriptor *hd, uint8_t nports)
1434 struct usb_device_request req;
1435 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1437 req.bmRequestType = UT_READ_CLASS_DEVICE;
1438 req.bRequest = UR_GET_DESCRIPTOR;
1439 USETW2(req.wValue, UDESC_HUB, 0);
1440 USETW(req.wIndex, 0);
1441 USETW(req.wLength, len);
1442 return (usbd_do_request(udev, mtx, &req, hd));
1445 /*------------------------------------------------------------------------*
1446 * usbd_req_get_ss_hub_descriptor
1451 *------------------------------------------------------------------------*/
1453 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1454 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1456 struct usb_device_request req;
1457 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1459 req.bmRequestType = UT_READ_CLASS_DEVICE;
1460 req.bRequest = UR_GET_DESCRIPTOR;
1461 USETW2(req.wValue, UDESC_SS_HUB, 0);
1462 USETW(req.wIndex, 0);
1463 USETW(req.wLength, len);
1464 return (usbd_do_request(udev, mtx, &req, hd));
1467 /*------------------------------------------------------------------------*
1468 * usbd_req_get_hub_status
1473 *------------------------------------------------------------------------*/
1475 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1476 struct usb_hub_status *st)
1478 struct usb_device_request req;
1480 req.bmRequestType = UT_READ_CLASS_DEVICE;
1481 req.bRequest = UR_GET_STATUS;
1482 USETW(req.wValue, 0);
1483 USETW(req.wIndex, 0);
1484 USETW(req.wLength, sizeof(struct usb_hub_status));
1485 return (usbd_do_request(udev, mtx, &req, st));
1488 /*------------------------------------------------------------------------*
1489 * usbd_req_set_address
1491 * This function is used to set the address for an USB device. After
1492 * port reset the USB device will respond at address zero.
1497 *------------------------------------------------------------------------*/
1499 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1501 struct usb_device_request req;
1504 DPRINTFN(6, "setting device address=%d\n", addr);
1506 req.bmRequestType = UT_WRITE_DEVICE;
1507 req.bRequest = UR_SET_ADDRESS;
1508 USETW(req.wValue, addr);
1509 USETW(req.wIndex, 0);
1510 USETW(req.wLength, 0);
1512 err = USB_ERR_INVAL;
1514 /* check if USB controller handles set address */
1515 if (udev->bus->methods->set_address != NULL)
1516 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1518 if (err != USB_ERR_INVAL)
1521 /* Setting the address should not take more than 1 second ! */
1522 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1523 USB_DELAY_STATUS_STAGE, NULL, 1000);
1526 /* allow device time to set new address */
1528 USB_MS_TO_TICKS(usb_set_address_settle));
1533 /*------------------------------------------------------------------------*
1534 * usbd_req_get_port_status
1539 *------------------------------------------------------------------------*/
1541 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1542 struct usb_port_status *ps, uint8_t port)
1544 struct usb_device_request req;
1546 req.bmRequestType = UT_READ_CLASS_OTHER;
1547 req.bRequest = UR_GET_STATUS;
1548 USETW(req.wValue, 0);
1549 req.wIndex[0] = port;
1551 USETW(req.wLength, sizeof *ps);
1552 return (usbd_do_request(udev, mtx, &req, ps));
1555 /*------------------------------------------------------------------------*
1556 * usbd_req_clear_hub_feature
1561 *------------------------------------------------------------------------*/
1563 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1566 struct usb_device_request req;
1568 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1569 req.bRequest = UR_CLEAR_FEATURE;
1570 USETW(req.wValue, sel);
1571 USETW(req.wIndex, 0);
1572 USETW(req.wLength, 0);
1573 return (usbd_do_request(udev, mtx, &req, 0));
1576 /*------------------------------------------------------------------------*
1577 * usbd_req_set_hub_feature
1582 *------------------------------------------------------------------------*/
1584 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1587 struct usb_device_request req;
1589 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1590 req.bRequest = UR_SET_FEATURE;
1591 USETW(req.wValue, sel);
1592 USETW(req.wIndex, 0);
1593 USETW(req.wLength, 0);
1594 return (usbd_do_request(udev, mtx, &req, 0));
1597 /*------------------------------------------------------------------------*
1598 * usbd_req_set_hub_u1_timeout
1603 *------------------------------------------------------------------------*/
1605 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
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_U1_TIMEOUT);
1613 req.wIndex[0] = port;
1614 req.wIndex[1] = timeout;
1615 USETW(req.wLength, 0);
1616 return (usbd_do_request(udev, mtx, &req, 0));
1619 /*------------------------------------------------------------------------*
1620 * usbd_req_set_hub_u2_timeout
1625 *------------------------------------------------------------------------*/
1627 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1628 uint8_t port, uint8_t timeout)
1630 struct usb_device_request req;
1632 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1633 req.bRequest = UR_SET_FEATURE;
1634 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1635 req.wIndex[0] = port;
1636 req.wIndex[1] = timeout;
1637 USETW(req.wLength, 0);
1638 return (usbd_do_request(udev, mtx, &req, 0));
1641 /*------------------------------------------------------------------------*
1642 * usbd_req_set_hub_depth
1647 *------------------------------------------------------------------------*/
1649 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1652 struct usb_device_request req;
1654 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1655 req.bRequest = UR_SET_HUB_DEPTH;
1656 USETW(req.wValue, depth);
1657 USETW(req.wIndex, 0);
1658 USETW(req.wLength, 0);
1659 return (usbd_do_request(udev, mtx, &req, 0));
1662 /*------------------------------------------------------------------------*
1663 * usbd_req_clear_port_feature
1668 *------------------------------------------------------------------------*/
1670 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1671 uint8_t port, uint16_t sel)
1673 struct usb_device_request req;
1675 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1676 req.bRequest = UR_CLEAR_FEATURE;
1677 USETW(req.wValue, sel);
1678 req.wIndex[0] = port;
1680 USETW(req.wLength, 0);
1681 return (usbd_do_request(udev, mtx, &req, 0));
1684 /*------------------------------------------------------------------------*
1685 * usbd_req_set_port_feature
1690 *------------------------------------------------------------------------*/
1692 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1693 uint8_t port, uint16_t sel)
1695 struct usb_device_request req;
1697 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1698 req.bRequest = UR_SET_FEATURE;
1699 USETW(req.wValue, sel);
1700 req.wIndex[0] = port;
1702 USETW(req.wLength, 0);
1703 return (usbd_do_request(udev, mtx, &req, 0));
1706 /*------------------------------------------------------------------------*
1707 * usbd_req_set_protocol
1712 *------------------------------------------------------------------------*/
1714 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1715 uint8_t iface_index, uint16_t report)
1717 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1718 struct usb_device_request req;
1720 if ((iface == NULL) || (iface->idesc == NULL)) {
1721 return (USB_ERR_INVAL);
1723 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1724 iface, report, iface->idesc->bInterfaceNumber);
1726 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1727 req.bRequest = UR_SET_PROTOCOL;
1728 USETW(req.wValue, report);
1729 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1731 USETW(req.wLength, 0);
1732 return (usbd_do_request(udev, mtx, &req, 0));
1735 /*------------------------------------------------------------------------*
1736 * usbd_req_set_report
1741 *------------------------------------------------------------------------*/
1743 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1744 uint8_t iface_index, uint8_t type, uint8_t id)
1746 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1747 struct usb_device_request req;
1749 if ((iface == NULL) || (iface->idesc == NULL)) {
1750 return (USB_ERR_INVAL);
1752 DPRINTFN(5, "len=%d\n", len);
1754 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1755 req.bRequest = UR_SET_REPORT;
1756 USETW2(req.wValue, type, id);
1757 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1759 USETW(req.wLength, len);
1760 return (usbd_do_request(udev, mtx, &req, data));
1763 /*------------------------------------------------------------------------*
1764 * usbd_req_get_report
1769 *------------------------------------------------------------------------*/
1771 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1772 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1774 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1775 struct usb_device_request req;
1777 if ((iface == NULL) || (iface->idesc == NULL)) {
1778 return (USB_ERR_INVAL);
1780 DPRINTFN(5, "len=%d\n", len);
1782 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1783 req.bRequest = UR_GET_REPORT;
1784 USETW2(req.wValue, type, id);
1785 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1787 USETW(req.wLength, len);
1788 return (usbd_do_request(udev, mtx, &req, data));
1791 /*------------------------------------------------------------------------*
1797 *------------------------------------------------------------------------*/
1799 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1800 uint8_t iface_index, uint8_t duration, uint8_t id)
1802 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1803 struct usb_device_request req;
1805 if ((iface == NULL) || (iface->idesc == NULL)) {
1806 return (USB_ERR_INVAL);
1808 DPRINTFN(5, "%d %d\n", duration, id);
1810 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1811 req.bRequest = UR_SET_IDLE;
1812 USETW2(req.wValue, duration, id);
1813 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1815 USETW(req.wLength, 0);
1816 return (usbd_do_request(udev, mtx, &req, 0));
1819 /*------------------------------------------------------------------------*
1820 * usbd_req_get_report_descriptor
1825 *------------------------------------------------------------------------*/
1827 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1828 void *d, uint16_t size, uint8_t iface_index)
1830 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1831 struct usb_device_request req;
1833 if ((iface == NULL) || (iface->idesc == NULL)) {
1834 return (USB_ERR_INVAL);
1836 req.bmRequestType = UT_READ_INTERFACE;
1837 req.bRequest = UR_GET_DESCRIPTOR;
1838 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1839 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1841 USETW(req.wLength, size);
1842 return (usbd_do_request(udev, mtx, &req, d));
1845 /*------------------------------------------------------------------------*
1846 * usbd_req_set_config
1848 * This function is used to select the current configuration number in
1849 * both USB device side mode and USB host side mode. When setting the
1850 * configuration the function of the interfaces can change.
1855 *------------------------------------------------------------------------*/
1857 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1859 struct usb_device_request req;
1861 DPRINTF("setting config %d\n", conf);
1863 /* do "set configuration" request */
1865 req.bmRequestType = UT_WRITE_DEVICE;
1866 req.bRequest = UR_SET_CONFIG;
1867 req.wValue[0] = conf;
1869 USETW(req.wIndex, 0);
1870 USETW(req.wLength, 0);
1871 return (usbd_do_request(udev, mtx, &req, 0));
1874 /*------------------------------------------------------------------------*
1875 * usbd_req_get_config
1880 *------------------------------------------------------------------------*/
1882 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1884 struct usb_device_request req;
1886 req.bmRequestType = UT_READ_DEVICE;
1887 req.bRequest = UR_GET_CONFIG;
1888 USETW(req.wValue, 0);
1889 USETW(req.wIndex, 0);
1890 USETW(req.wLength, 1);
1891 return (usbd_do_request(udev, mtx, &req, pconf));
1894 /*------------------------------------------------------------------------*
1895 * usbd_setup_device_desc
1896 *------------------------------------------------------------------------*/
1898 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1903 * Get the first 8 bytes of the device descriptor !
1905 * NOTE: "usbd_do_request()" will check the device descriptor
1906 * next time we do a request to see if the maximum packet size
1907 * changed! The 8 first bytes of the device descriptor
1908 * contains the maximum packet size to use on control endpoint
1909 * 0. If this value is different from "USB_MAX_IPACKET" a new
1910 * USB control request will be setup!
1912 switch (udev->speed) {
1913 case USB_SPEED_FULL:
1914 if (usb_full_ddesc != 0) {
1915 /* get full device descriptor */
1916 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1921 /* get partial device descriptor, some devices crash on this */
1922 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1923 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1927 /* get the full device descriptor */
1928 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1932 DPRINTF("Minimum MaxPacketSize is large enough "
1933 "to hold the complete device descriptor or "
1934 "only once MaxPacketSize choice\n");
1936 /* get the full device descriptor */
1937 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1939 /* try one more time, if error */
1941 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1946 DPRINTFN(0, "getting device descriptor "
1947 "at addr %d failed, %s\n", udev->address,
1952 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1953 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1954 udev->address, UGETW(udev->ddesc.bcdUSB),
1955 udev->ddesc.bDeviceClass,
1956 udev->ddesc.bDeviceSubClass,
1957 udev->ddesc.bDeviceProtocol,
1958 udev->ddesc.bMaxPacketSize,
1959 udev->ddesc.bLength,
1965 /*------------------------------------------------------------------------*
1966 * usbd_req_re_enumerate
1968 * NOTE: After this function returns the hardware is in the
1969 * unconfigured state! The application is responsible for setting a
1970 * new configuration.
1975 *------------------------------------------------------------------------*/
1977 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1979 struct usb_device *parent_hub;
1982 uint8_t do_retry = 1;
1984 if (udev->flags.usb_mode != USB_MODE_HOST) {
1985 return (USB_ERR_INVAL);
1987 old_addr = udev->address;
1988 parent_hub = udev->parent_hub;
1989 if (parent_hub == NULL) {
1990 return (USB_ERR_INVAL);
1993 #if USB_HAVE_TT_SUPPORT
1995 * Try to reset the High Speed parent HUB of a LOW- or FULL-
1996 * speed device, if any.
1998 if (udev->parent_hs_hub != NULL &&
1999 udev->speed != USB_SPEED_HIGH) {
2000 DPRINTF("Trying to reset parent High Speed TT.\n");
2001 if (udev->parent_hs_hub == parent_hub &&
2002 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2003 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2004 /* we can reset the whole TT */
2005 err = usbd_req_reset_tt(parent_hub, NULL,
2008 /* only reset a particular device and endpoint */
2009 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2010 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2013 DPRINTF("Resetting parent High "
2014 "Speed TT failed (%s).\n",
2019 /* Try to warm reset first */
2020 if (parent_hub->speed == USB_SPEED_SUPER)
2021 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2023 /* Try to reset the parent HUB port. */
2024 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2026 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2027 old_addr, usbd_errstr(err));
2032 * After that the port has been reset our device should be at
2035 udev->address = USB_START_ADDR;
2037 /* reset "bMaxPacketSize" */
2038 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2040 /* reset USB state */
2041 usb_set_device_state(udev, USB_STATE_POWERED);
2044 * Restore device address:
2046 err = usbd_req_set_address(udev, mtx, old_addr);
2048 /* XXX ignore any errors! */
2049 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2050 old_addr, usbd_errstr(err));
2053 * Restore device address, if the controller driver did not
2056 if (udev->address == USB_START_ADDR)
2057 udev->address = old_addr;
2059 /* setup the device descriptor and the initial "wMaxPacketSize" */
2060 err = usbd_setup_device_desc(udev, mtx);
2063 if (err && do_retry) {
2064 /* give the USB firmware some time to load */
2065 usb_pause_mtx(mtx, hz / 2);
2066 /* no more retries after this retry */
2071 /* restore address */
2072 if (udev->address == USB_START_ADDR)
2073 udev->address = old_addr;
2074 /* update state, if successful */
2076 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2080 /*------------------------------------------------------------------------*
2081 * usbd_req_clear_device_feature
2086 *------------------------------------------------------------------------*/
2088 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2091 struct usb_device_request req;
2093 req.bmRequestType = UT_WRITE_DEVICE;
2094 req.bRequest = UR_CLEAR_FEATURE;
2095 USETW(req.wValue, sel);
2096 USETW(req.wIndex, 0);
2097 USETW(req.wLength, 0);
2098 return (usbd_do_request(udev, mtx, &req, 0));
2101 /*------------------------------------------------------------------------*
2102 * usbd_req_set_device_feature
2107 *------------------------------------------------------------------------*/
2109 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2112 struct usb_device_request req;
2114 req.bmRequestType = UT_WRITE_DEVICE;
2115 req.bRequest = UR_SET_FEATURE;
2116 USETW(req.wValue, sel);
2117 USETW(req.wIndex, 0);
2118 USETW(req.wLength, 0);
2119 return (usbd_do_request(udev, mtx, &req, 0));
2122 /*------------------------------------------------------------------------*
2128 *------------------------------------------------------------------------*/
2130 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2133 struct usb_device_request req;
2135 /* For single TT HUBs the port should be 1 */
2137 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2138 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2141 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2142 req.bRequest = UR_RESET_TT;
2143 USETW(req.wValue, 0);
2144 req.wIndex[0] = port;
2146 USETW(req.wLength, 0);
2147 return (usbd_do_request(udev, mtx, &req, 0));
2150 /*------------------------------------------------------------------------*
2151 * usbd_req_clear_tt_buffer
2153 * For single TT HUBs the port should be 1.
2158 *------------------------------------------------------------------------*/
2160 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2161 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2163 struct usb_device_request req;
2166 /* For single TT HUBs the port should be 1 */
2168 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2169 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2172 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2173 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2175 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2176 req.bRequest = UR_CLEAR_TT_BUFFER;
2177 USETW(req.wValue, wValue);
2178 req.wIndex[0] = port;
2180 USETW(req.wLength, 0);
2181 return (usbd_do_request(udev, mtx, &req, 0));
2184 /*------------------------------------------------------------------------*
2185 * usbd_req_set_port_link_state
2187 * USB 3.0 specific request
2192 *------------------------------------------------------------------------*/
2194 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2195 uint8_t port, uint8_t link_state)
2197 struct usb_device_request req;
2199 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2200 req.bRequest = UR_SET_FEATURE;
2201 USETW(req.wValue, UHF_PORT_LINK_STATE);
2202 req.wIndex[0] = port;
2203 req.wIndex[1] = link_state;
2204 USETW(req.wLength, 0);
2205 return (usbd_do_request(udev, mtx, &req, 0));
2208 /*------------------------------------------------------------------------*
2209 * usbd_req_set_lpm_info
2211 * USB 2.0 specific request for Link Power Management.
2215 * USB_ERR_PENDING_REQUESTS: NYET
2216 * USB_ERR_TIMEOUT: TIMEOUT
2217 * USB_ERR_STALL: STALL
2219 *------------------------------------------------------------------------*/
2221 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2222 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2224 struct usb_device_request req;
2228 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2229 req.bRequest = UR_SET_AND_TEST;
2230 USETW(req.wValue, UHF_PORT_L1);
2231 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2232 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2233 USETW(req.wLength, sizeof(buf));
2235 /* set default value in case of short transfer */
2238 err = usbd_do_request(udev, mtx, &req, buf);
2243 case 0x00: /* SUCCESS */
2245 case 0x10: /* NYET */
2246 err = USB_ERR_PENDING_REQUESTS;
2248 case 0x11: /* TIMEOUT */
2249 err = USB_ERR_TIMEOUT;
2251 case 0x30: /* STALL */
2252 err = USB_ERR_STALLED;
2254 default: /* reserved */
2255 err = USB_ERR_IOERROR;