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");
76 /* The following structures are used in connection to fault injection. */
77 struct usb_ctrl_debug {
78 int bus_index; /* target bus */
79 int dev_index; /* target address */
80 int ds_fail; /* fail data stage */
81 int ss_fail; /* fail status stage */
82 int ds_delay; /* data stage delay in ms */
83 int ss_delay; /* status stage delay in ms */
84 int bmRequestType_value;
88 struct usb_ctrl_debug_bits {
96 /* The default is to disable fault injection. */
98 static struct usb_ctrl_debug usb_ctrl_debug = {
101 .bmRequestType_value = -1,
102 .bRequest_value = -1,
105 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
106 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
107 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
108 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
109 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
110 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
111 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
112 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
113 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
114 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
115 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
116 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
117 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
118 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
119 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
120 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
122 /*------------------------------------------------------------------------*
123 * usbd_get_debug_bits
125 * This function is only useful in USB host mode.
126 *------------------------------------------------------------------------*/
128 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
129 struct usb_ctrl_debug_bits *dbg)
133 memset(dbg, 0, sizeof(*dbg));
135 /* Compute data stage delay */
137 temp = usb_ctrl_debug.ds_delay;
140 else if (temp > (16*1024))
143 dbg->ds_delay = temp;
145 /* Compute status stage delay */
147 temp = usb_ctrl_debug.ss_delay;
150 else if (temp > (16*1024))
153 dbg->ss_delay = temp;
155 /* Check if this control request should be failed */
157 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
160 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
163 temp = usb_ctrl_debug.bmRequestType_value;
165 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
168 temp = usb_ctrl_debug.bRequest_value;
170 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
173 temp = usb_ctrl_debug.ds_fail;
177 temp = usb_ctrl_debug.ss_fail;
183 #endif /* USB_REQ_DEBUG */
184 #endif /* USB_DEBUG */
186 /*------------------------------------------------------------------------*
187 * usbd_do_request_callback
189 * This function is the USB callback for generic USB Host control
191 *------------------------------------------------------------------------*/
193 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
195 ; /* workaround for a bug in "indent" */
197 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
199 switch (USB_GET_STATE(xfer)) {
201 usbd_transfer_submit(xfer);
204 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
209 /*------------------------------------------------------------------------*
210 * usb_do_clear_stall_callback
212 * This function is the USB callback for generic clear stall requests.
213 *------------------------------------------------------------------------*/
215 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
217 struct usb_device_request req;
218 struct usb_device *udev;
219 struct usb_endpoint *ep;
220 struct usb_endpoint *ep_end;
221 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 /* start up the current or next transfer, if any */
250 usb_command_wrapper(&ep->endpoint_q,
251 ep->endpoint_q.curr);
258 break; /* no endpoints - nothing to do */
259 if ((ep < ep_first) || (ep >= ep_end))
260 ep = ep_first; /* endpoint wrapped around */
264 /* setup a clear-stall packet */
266 req.bmRequestType = UT_WRITE_ENDPOINT;
267 req.bRequest = UR_CLEAR_FEATURE;
268 USETW(req.wValue, UF_ENDPOINT_HALT);
269 req.wIndex[0] = ep->edesc->bEndpointAddress;
271 USETW(req.wLength, 0);
273 /* copy in the transfer */
275 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
278 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
280 USB_BUS_UNLOCK(udev->bus);
282 usbd_transfer_submit(xfer);
284 USB_BUS_LOCK(udev->bus);
292 if (error == USB_ERR_CANCELLED)
295 DPRINTF("Clear stall failed.\n");
298 * Some VMs like VirtualBox always return failure on
299 * clear-stall which we sometimes should just ignore.
303 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
306 if (error == USB_ERR_TIMEOUT) {
307 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
308 DPRINTF("Trying to re-enumerate.\n");
309 usbd_start_re_enumerate(udev);
311 udev->clear_stall_errors++;
312 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
313 DPRINTF("Trying to re-enumerate.\n");
314 usbd_start_re_enumerate(udev);
320 /* store current endpoint */
322 USB_BUS_UNLOCK(udev->bus);
325 static usb_handle_req_t *
326 usbd_get_hr_func(struct usb_device *udev)
328 /* figure out if there is a Handle Request function */
329 if (udev->flags.usb_mode == USB_MODE_DEVICE)
330 return (usb_temp_get_desc_p);
331 else if (udev->parent_hub == NULL)
332 return (udev->bus->methods->roothub_exec);
337 /*------------------------------------------------------------------------*
338 * usbd_do_request_flags and usbd_do_request
340 * Description of arguments passed to these functions:
342 * "udev" - this is the "usb_device" structure pointer on which the
343 * request should be performed. It is possible to call this function
344 * in both Host Side mode and Device Side mode.
346 * "mtx" - if this argument is non-NULL the mutex pointed to by it
347 * will get dropped and picked up during the execution of this
348 * function, hence this function sometimes needs to sleep. If this
349 * argument is NULL it has no effect.
351 * "req" - this argument must always be non-NULL and points to an
352 * 8-byte structure holding the USB request to be done. The USB
353 * request structure has a bit telling the direction of the USB
354 * request, if it is a read or a write.
356 * "data" - if the "wLength" part of the structure pointed to by "req"
357 * is non-zero this argument must point to a valid kernel buffer which
358 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
361 * "flags" - here is a list of valid flags:
363 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
366 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
367 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
368 * sysctl. This flag is mostly useful for debugging.
370 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
373 * "actlen" - if non-NULL the actual transfer length will be stored in
374 * the 16-bit unsigned integer pointed to by "actlen". This
375 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
378 * "timeout" - gives the timeout for the control transfer in
379 * milliseconds. A "timeout" value less than 50 milliseconds is
380 * treated like a 50 millisecond timeout. A "timeout" value greater
381 * than 30 seconds is treated like a 30 second timeout. This USB stack
382 * does not allow control requests without a timeout.
384 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
385 * will be serialized by the use of the USB device enumeration lock.
390 *------------------------------------------------------------------------*/
392 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
393 struct usb_device_request *req, void *data, uint16_t flags,
394 uint16_t *actlen, usb_timeout_t timeout)
397 struct usb_ctrl_debug_bits dbg;
399 usb_handle_req_t *hr_func;
400 struct usb_xfer *xfer;
403 usb_ticks_t start_ticks;
404 usb_ticks_t delta_ticks;
405 usb_ticks_t max_ticks;
412 /* timeout is too small */
415 if (timeout > 30000) {
416 /* timeout is too big */
419 length = UGETW(req->wLength);
421 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
422 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
423 udev, req->bmRequestType, req->bRequest,
424 req->wValue[1], req->wValue[0],
425 req->wIndex[1], req->wIndex[0],
426 req->wLength[1], req->wLength[0]);
428 /* Check if the device is still alive */
429 if (udev->state < USB_STATE_POWERED) {
430 DPRINTF("usb device has gone\n");
431 return (USB_ERR_NOT_CONFIGURED);
435 * Set "actlen" to a known value in case the caller does not
436 * check the return value:
441 #if (USB_HAVE_USER_IO == 0)
442 if (flags & USB_USER_DATA_PTR)
443 return (USB_ERR_INVAL);
445 if ((mtx != NULL) && (mtx != &Giant)) {
447 mtx_assert(mtx, MA_NOTOWNED);
451 * Grab the USB device enumeration SX-lock serialization is
452 * achieved when multiple threads are involved:
454 do_unlock = usbd_enum_lock(udev);
457 * We need to allow suspend and resume at this point, else the
458 * control transfer will timeout if the device is suspended!
460 usbd_sr_unlock(udev);
462 hr_func = usbd_get_hr_func(udev);
464 if (hr_func != NULL) {
465 DPRINTF("Handle Request function is set\n");
470 if (!(req->bmRequestType & UT_READ)) {
472 DPRINTFN(1, "The handle request function "
473 "does not support writing data!\n");
479 /* The root HUB code needs the BUS lock locked */
481 USB_BUS_LOCK(udev->bus);
482 err = (hr_func) (udev, req, &desc, &temp);
483 USB_BUS_UNLOCK(udev->bus);
489 if (!(flags & USB_SHORT_XFER_OK)) {
490 err = USB_ERR_SHORT_XFER;
500 if (flags & USB_USER_DATA_PTR) {
501 if (copyout(desc, data, length)) {
507 memcpy(data, desc, length);
509 goto done; /* success */
513 * Setup a new USB transfer or use the existing one, if any:
515 usbd_ctrl_transfer_setup(udev);
517 xfer = udev->ctrl_xfer[0];
519 /* most likely out of memory */
526 usbd_get_debug_bits(udev, req, &dbg);
528 /* Check for fault injection */
530 flags |= USB_DELAY_STATUS_STAGE;
534 if (flags & USB_DELAY_STATUS_STAGE)
535 xfer->flags.manual_status = 1;
537 xfer->flags.manual_status = 0;
539 if (flags & USB_SHORT_XFER_OK)
540 xfer->flags.short_xfer_ok = 1;
542 xfer->flags.short_xfer_ok = 0;
544 xfer->timeout = timeout;
548 max_ticks = USB_MS_TO_TICKS(timeout);
550 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
552 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
556 if (temp > usbd_xfer_max_len(xfer)) {
557 temp = usbd_xfer_max_len(xfer);
560 if (xfer->flags.manual_status) {
561 if (usbd_xfer_frame_len(xfer, 0) != 0) {
562 /* Execute data stage separately */
564 } else if (temp > 0) {
569 if (dbg.ds_delay > 0) {
571 xfer->xroot->xfer_mtx,
572 USB_MS_TO_TICKS(dbg.ds_delay));
573 /* make sure we don't time out */
579 usbd_xfer_set_frame_len(xfer, 1, temp);
582 if (!(req->bmRequestType & UT_READ)) {
584 if (flags & USB_USER_DATA_PTR) {
585 USB_XFER_UNLOCK(xfer);
586 err = usbd_copy_in_user(xfer->frbuffers + 1,
595 usbd_copy_in(xfer->frbuffers + 1,
598 usbd_xfer_set_frames(xfer, 2);
600 if (usbd_xfer_frame_len(xfer, 0) == 0) {
601 if (xfer->flags.manual_status) {
607 if (dbg.ss_delay > 0) {
609 xfer->xroot->xfer_mtx,
610 USB_MS_TO_TICKS(dbg.ss_delay));
611 /* make sure we don't time out */
615 xfer->flags.manual_status = 0;
620 usbd_xfer_set_frames(xfer, 1);
623 usbd_transfer_start(xfer);
625 while (usbd_transfer_pending(xfer)) {
626 cv_wait(&udev->ctrlreq_cv,
627 xfer->xroot->xfer_mtx);
636 /* get actual length of DATA stage */
638 if (xfer->aframes < 2) {
641 acttemp = usbd_xfer_frame_len(xfer, 1);
644 /* check for short packet */
646 if (temp > acttemp) {
651 if (req->bmRequestType & UT_READ) {
653 if (flags & USB_USER_DATA_PTR) {
654 USB_XFER_UNLOCK(xfer);
655 err = usbd_copy_out_user(xfer->frbuffers + 1,
664 usbd_copy_out(xfer->frbuffers + 1,
669 * Clear "frlengths[0]" so that we don't send the setup
672 usbd_xfer_set_frame_len(xfer, 0, 0);
674 /* update length and data pointer */
676 data = USB_ADD_BYTES(data, temp);
681 /* check for timeout */
683 delta_ticks = ticks - start_ticks;
684 if (delta_ticks > max_ticks) {
686 err = USB_ERR_TIMEOUT;
696 * Make sure that the control endpoint is no longer
697 * blocked in case of a non-transfer related error:
699 usbd_transfer_stop(xfer);
701 USB_XFER_UNLOCK(xfer);
707 usbd_enum_unlock(udev);
709 if ((mtx != NULL) && (mtx != &Giant))
712 return ((usb_error_t)err);
715 /*------------------------------------------------------------------------*
716 * usbd_do_request_proc - factored out code
718 * This function is factored out code. It does basically the same like
719 * usbd_do_request_flags, except it will check the status of the
720 * passed process argument before doing the USB request. If the
721 * process is draining the USB_ERR_IOERROR code will be returned. It
722 * is assumed that the mutex associated with the process is locked
723 * when calling this function.
724 *------------------------------------------------------------------------*/
726 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
727 struct usb_device_request *req, void *data, uint16_t flags,
728 uint16_t *actlen, usb_timeout_t timeout)
733 /* get request data length */
734 len = UGETW(req->wLength);
736 /* check if the device is being detached */
737 if (usb_proc_is_gone(pproc)) {
738 err = USB_ERR_IOERROR;
742 /* forward the USB request */
743 err = usbd_do_request_flags(udev, pproc->up_mtx,
744 req, data, flags, actlen, timeout);
747 /* on failure we zero the data */
748 /* on short packet we zero the unused data */
749 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
751 memset(data, 0, len);
752 else if (actlen && *actlen != len)
753 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
758 /*------------------------------------------------------------------------*
759 * usbd_req_reset_port
761 * This function will instruct a USB HUB to perform a reset sequence
762 * on the specified port number.
765 * 0: Success. The USB device should now be at address zero.
766 * Else: Failure. No USB device is present and the USB port should be
768 *------------------------------------------------------------------------*/
770 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
772 struct usb_port_status ps;
780 /* clear any leftover port reset changes first */
781 usbd_req_clear_port_feature(
782 udev, mtx, port, UHF_C_PORT_RESET);
784 /* assert port reset on the given port */
785 err = usbd_req_set_port_feature(
786 udev, mtx, port, UHF_PORT_RESET);
788 /* check for errors */
795 /* wait for the device to recover from reset */
796 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
797 n += usb_port_reset_delay;
798 err = usbd_req_get_port_status(udev, mtx, &ps, port);
802 status = UGETW(ps.wPortStatus);
803 change = UGETW(ps.wPortChange);
805 /* if the device disappeared, just give up */
806 if (!(status & UPS_CURRENT_CONNECT_STATUS))
809 /* check if reset is complete */
810 if (change & UPS_C_PORT_RESET)
814 * Some Virtual Machines like VirtualBox 4.x fail to
815 * generate a port reset change event. Check if reset
816 * is no longer asserted.
818 if (!(status & UPS_RESET))
821 /* check for timeout */
828 /* clear port reset first */
829 err = usbd_req_clear_port_feature(
830 udev, mtx, port, UHF_C_PORT_RESET);
834 /* check for timeout */
836 err = USB_ERR_TIMEOUT;
839 /* wait for the device to recover from reset */
840 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
843 DPRINTFN(2, "port %d reset returning error=%s\n",
844 port, usbd_errstr(err));
848 /*------------------------------------------------------------------------*
849 * usbd_req_warm_reset_port
851 * This function will instruct an USB HUB to perform a warm reset
852 * sequence on the specified port number. This kind of reset is not
853 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
854 * for SUPER-speed USB HUBs.
857 * 0: Success. The USB device should now be available again.
858 * Else: Failure. No USB device is present and the USB port should be
860 *------------------------------------------------------------------------*/
862 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
865 struct usb_port_status ps;
873 err = usbd_req_get_port_status(udev, mtx, &ps, port);
877 status = UGETW(ps.wPortStatus);
879 switch (UPS_PORT_LINK_STATE_GET(status)) {
881 case UPS_PORT_LS_COMP_MODE:
882 case UPS_PORT_LS_LOOPBACK:
883 case UPS_PORT_LS_SS_INA:
886 DPRINTF("Wrong state for warm reset\n");
890 /* clear any leftover warm port reset changes first */
891 usbd_req_clear_port_feature(udev, mtx,
892 port, UHF_C_BH_PORT_RESET);
894 /* set warm port reset */
895 err = usbd_req_set_port_feature(udev, mtx,
896 port, UHF_BH_PORT_RESET);
902 /* wait for the device to recover from reset */
903 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
904 n += usb_port_reset_delay;
905 err = usbd_req_get_port_status(udev, mtx, &ps, port);
909 status = UGETW(ps.wPortStatus);
910 change = UGETW(ps.wPortChange);
912 /* if the device disappeared, just give up */
913 if (!(status & UPS_CURRENT_CONNECT_STATUS))
916 /* check if reset is complete */
917 if (change & UPS_C_BH_PORT_RESET)
920 /* check for timeout */
927 /* clear port reset first */
928 err = usbd_req_clear_port_feature(
929 udev, mtx, port, UHF_C_BH_PORT_RESET);
933 /* check for timeout */
935 err = USB_ERR_TIMEOUT;
938 /* wait for the device to recover from reset */
939 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
942 DPRINTFN(2, "port %d warm reset returning error=%s\n",
943 port, usbd_errstr(err));
947 /*------------------------------------------------------------------------*
950 * This function can be used to retrieve USB descriptors. It contains
951 * some additional logic like zeroing of missing descriptor bytes and
952 * retrying an USB descriptor in case of failure. The "min_len"
953 * argument specifies the minimum descriptor length. The "max_len"
954 * argument specifies the maximum descriptor length. If the real
955 * descriptor length is less than the minimum length the missing
956 * byte(s) will be zeroed. The type field, the second byte of the USB
957 * descriptor, will get forced to the correct type. If the "actlen"
958 * pointer is non-NULL, the actual length of the transfer will get
959 * stored in the 16-bit unsigned integer which it is pointing to. The
960 * first byte of the descriptor will not get updated. If the "actlen"
961 * pointer is NULL the first byte of the descriptor will get updated
962 * to reflect the actual length instead. If "min_len" is not equal to
963 * "max_len" then this function will try to retrive the beginning of
964 * the descriptor and base the maximum length on the first byte of the
970 *------------------------------------------------------------------------*/
972 usbd_req_get_desc(struct usb_device *udev,
973 struct mtx *mtx, uint16_t *actlen, void *desc,
974 uint16_t min_len, uint16_t max_len,
975 uint16_t id, uint8_t type, uint8_t index,
978 struct usb_device_request req;
982 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
983 id, type, index, max_len);
985 req.bmRequestType = UT_READ_DEVICE;
986 req.bRequest = UR_GET_DESCRIPTOR;
987 USETW2(req.wValue, type, index);
988 USETW(req.wIndex, id);
992 if ((min_len < 2) || (max_len < 2)) {
996 USETW(req.wLength, min_len);
998 err = usbd_do_request_flags(udev, mtx, &req,
999 desc, 0, NULL, 1000);
1007 usb_pause_mtx(mtx, hz / 5);
1013 if (min_len == max_len) {
1015 /* enforce correct length */
1016 if ((buf[0] > min_len) && (actlen == NULL))
1019 /* enforce correct type */
1026 if (max_len > buf[0]) {
1029 /* zero minimum data */
1031 while (min_len > max_len) {
1036 /* set new minimum length */
1041 if (actlen != NULL) {
1050 /*------------------------------------------------------------------------*
1051 * usbd_req_get_string_any
1053 * This function will return the string given by "string_index"
1054 * using the first language ID. The maximum length "len" includes
1055 * the terminating zero. The "len" argument should be twice as
1056 * big pluss 2 bytes, compared with the actual maximum string length !
1061 *------------------------------------------------------------------------*/
1063 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1064 uint16_t len, uint8_t string_index)
1075 /* should not happen */
1076 return (USB_ERR_NORMAL_COMPLETION);
1078 if (string_index == 0) {
1079 /* this is the language table */
1081 return (USB_ERR_INVAL);
1083 if (udev->flags.no_strings) {
1085 return (USB_ERR_STALLED);
1087 err = usbd_req_get_string_desc
1088 (udev, mtx, buf, len, udev->langid, string_index);
1093 temp = (uint8_t *)buf;
1096 /* string length is too short */
1098 return (USB_ERR_INVAL);
1100 /* reserve one byte for terminating zero */
1103 /* find maximum length */
1105 n = (temp[0] / 2) - 1;
1109 /* skip descriptor header */
1112 /* reset swap state */
1115 /* convert and filter */
1116 for (i = 0; (i != n); i++) {
1117 c = UGETW(temp + (2 * i));
1119 /* convert from Unicode, handle buggy strings */
1120 if (((c & 0xff00) == 0) && (swap & 1)) {
1121 /* Little Endian, default */
1124 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1129 /* silently skip bad character */
1134 * Filter by default - We only allow alphanumerical
1135 * and a few more to avoid any problems with scripts
1148 /* silently skip bad character */
1150 *s = 0; /* zero terminate resulting string */
1151 return (USB_ERR_NORMAL_COMPLETION);
1154 /*------------------------------------------------------------------------*
1155 * usbd_req_get_string_desc
1157 * If you don't know the language ID, consider using
1158 * "usbd_req_get_string_any()".
1163 *------------------------------------------------------------------------*/
1165 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1166 uint16_t max_len, uint16_t lang_id,
1167 uint8_t string_index)
1169 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1170 UDESC_STRING, string_index, 0));
1173 /*------------------------------------------------------------------------*
1174 * usbd_req_get_config_desc_ptr
1176 * This function is used in device side mode to retrieve the pointer
1177 * to the generated config descriptor. This saves allocating space for
1178 * an additional config descriptor when setting the configuration.
1183 *------------------------------------------------------------------------*/
1185 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1186 struct usb_config_descriptor **ppcd, uint16_t wValue)
1188 struct usb_device_request req;
1189 usb_handle_req_t *hr_func;
1194 req.bmRequestType = UT_READ_DEVICE;
1195 req.bRequest = UR_GET_DESCRIPTOR;
1196 USETW(req.wValue, wValue);
1197 USETW(req.wIndex, 0);
1198 USETW(req.wLength, 0);
1203 hr_func = usbd_get_hr_func(udev);
1205 if (hr_func == NULL)
1206 err = USB_ERR_INVAL;
1208 USB_BUS_LOCK(udev->bus);
1209 err = (hr_func) (udev, &req, &ptr, &len);
1210 USB_BUS_UNLOCK(udev->bus);
1215 else if (ptr == NULL)
1216 err = USB_ERR_INVAL;
1218 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1223 /*------------------------------------------------------------------------*
1224 * usbd_req_get_config_desc
1229 *------------------------------------------------------------------------*/
1231 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1232 struct usb_config_descriptor *d, uint8_t conf_index)
1236 DPRINTFN(4, "confidx=%d\n", conf_index);
1238 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1239 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1243 /* Extra sanity checking */
1244 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1245 err = USB_ERR_INVAL;
1251 /*------------------------------------------------------------------------*
1252 * usbd_req_get_config_desc_full
1254 * This function gets the complete USB configuration descriptor and
1255 * ensures that "wTotalLength" is correct.
1260 *------------------------------------------------------------------------*/
1262 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1263 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1266 struct usb_config_descriptor cd;
1267 struct usb_config_descriptor *cdesc;
1271 DPRINTFN(4, "index=%d\n", index);
1275 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1279 /* get full descriptor */
1280 len = UGETW(cd.wTotalLength);
1281 if (len < sizeof(*cdesc)) {
1282 /* corrupt descriptor */
1283 return (USB_ERR_INVAL);
1285 cdesc = malloc(len, mtype, M_WAITOK);
1286 if (cdesc == NULL) {
1287 return (USB_ERR_NOMEM);
1289 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1290 UDESC_CONFIG, index, 3);
1295 /* make sure that the device is not fooling us: */
1296 USETW(cdesc->wTotalLength, len);
1300 return (0); /* success */
1303 /*------------------------------------------------------------------------*
1304 * usbd_req_get_device_desc
1309 *------------------------------------------------------------------------*/
1311 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1312 struct usb_device_descriptor *d)
1315 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1316 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1319 /*------------------------------------------------------------------------*
1320 * usbd_req_get_alt_interface_no
1325 *------------------------------------------------------------------------*/
1327 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1328 uint8_t *alt_iface_no, uint8_t iface_index)
1330 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1331 struct usb_device_request req;
1333 if ((iface == NULL) || (iface->idesc == NULL))
1334 return (USB_ERR_INVAL);
1336 req.bmRequestType = UT_READ_INTERFACE;
1337 req.bRequest = UR_GET_INTERFACE;
1338 USETW(req.wValue, 0);
1339 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1341 USETW(req.wLength, 1);
1342 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1345 /*------------------------------------------------------------------------*
1346 * usbd_req_set_alt_interface_no
1351 *------------------------------------------------------------------------*/
1353 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1354 uint8_t iface_index, uint8_t alt_no)
1356 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1357 struct usb_device_request req;
1359 if ((iface == NULL) || (iface->idesc == NULL))
1360 return (USB_ERR_INVAL);
1362 req.bmRequestType = UT_WRITE_INTERFACE;
1363 req.bRequest = UR_SET_INTERFACE;
1364 req.wValue[0] = alt_no;
1366 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1368 USETW(req.wLength, 0);
1369 return (usbd_do_request(udev, mtx, &req, 0));
1372 /*------------------------------------------------------------------------*
1373 * usbd_req_get_device_status
1378 *------------------------------------------------------------------------*/
1380 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1381 struct usb_status *st)
1383 struct usb_device_request req;
1385 req.bmRequestType = UT_READ_DEVICE;
1386 req.bRequest = UR_GET_STATUS;
1387 USETW(req.wValue, 0);
1388 USETW(req.wIndex, 0);
1389 USETW(req.wLength, sizeof(*st));
1390 return (usbd_do_request(udev, mtx, &req, st));
1393 /*------------------------------------------------------------------------*
1394 * usbd_req_get_hub_descriptor
1399 *------------------------------------------------------------------------*/
1401 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1402 struct usb_hub_descriptor *hd, uint8_t nports)
1404 struct usb_device_request req;
1405 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1407 req.bmRequestType = UT_READ_CLASS_DEVICE;
1408 req.bRequest = UR_GET_DESCRIPTOR;
1409 USETW2(req.wValue, UDESC_HUB, 0);
1410 USETW(req.wIndex, 0);
1411 USETW(req.wLength, len);
1412 return (usbd_do_request(udev, mtx, &req, hd));
1415 /*------------------------------------------------------------------------*
1416 * usbd_req_get_ss_hub_descriptor
1421 *------------------------------------------------------------------------*/
1423 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1424 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1426 struct usb_device_request req;
1427 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1429 req.bmRequestType = UT_READ_CLASS_DEVICE;
1430 req.bRequest = UR_GET_DESCRIPTOR;
1431 USETW2(req.wValue, UDESC_SS_HUB, 0);
1432 USETW(req.wIndex, 0);
1433 USETW(req.wLength, len);
1434 return (usbd_do_request(udev, mtx, &req, hd));
1437 /*------------------------------------------------------------------------*
1438 * usbd_req_get_hub_status
1443 *------------------------------------------------------------------------*/
1445 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1446 struct usb_hub_status *st)
1448 struct usb_device_request req;
1450 req.bmRequestType = UT_READ_CLASS_DEVICE;
1451 req.bRequest = UR_GET_STATUS;
1452 USETW(req.wValue, 0);
1453 USETW(req.wIndex, 0);
1454 USETW(req.wLength, sizeof(struct usb_hub_status));
1455 return (usbd_do_request(udev, mtx, &req, st));
1458 /*------------------------------------------------------------------------*
1459 * usbd_req_set_address
1461 * This function is used to set the address for an USB device. After
1462 * port reset the USB device will respond at address zero.
1467 *------------------------------------------------------------------------*/
1469 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1471 struct usb_device_request req;
1474 DPRINTFN(6, "setting device address=%d\n", addr);
1476 req.bmRequestType = UT_WRITE_DEVICE;
1477 req.bRequest = UR_SET_ADDRESS;
1478 USETW(req.wValue, addr);
1479 USETW(req.wIndex, 0);
1480 USETW(req.wLength, 0);
1482 err = USB_ERR_INVAL;
1484 /* check if USB controller handles set address */
1485 if (udev->bus->methods->set_address != NULL)
1486 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1488 if (err != USB_ERR_INVAL)
1491 /* Setting the address should not take more than 1 second ! */
1492 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1493 USB_DELAY_STATUS_STAGE, NULL, 1000);
1496 /* allow device time to set new address */
1498 USB_MS_TO_TICKS(usb_set_address_settle));
1503 /*------------------------------------------------------------------------*
1504 * usbd_req_get_port_status
1509 *------------------------------------------------------------------------*/
1511 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1512 struct usb_port_status *ps, uint8_t port)
1514 struct usb_device_request req;
1516 req.bmRequestType = UT_READ_CLASS_OTHER;
1517 req.bRequest = UR_GET_STATUS;
1518 USETW(req.wValue, 0);
1519 req.wIndex[0] = port;
1521 USETW(req.wLength, sizeof *ps);
1522 return (usbd_do_request(udev, mtx, &req, ps));
1525 /*------------------------------------------------------------------------*
1526 * usbd_req_clear_hub_feature
1531 *------------------------------------------------------------------------*/
1533 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1536 struct usb_device_request req;
1538 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1539 req.bRequest = UR_CLEAR_FEATURE;
1540 USETW(req.wValue, sel);
1541 USETW(req.wIndex, 0);
1542 USETW(req.wLength, 0);
1543 return (usbd_do_request(udev, mtx, &req, 0));
1546 /*------------------------------------------------------------------------*
1547 * usbd_req_set_hub_feature
1552 *------------------------------------------------------------------------*/
1554 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1557 struct usb_device_request req;
1559 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1560 req.bRequest = UR_SET_FEATURE;
1561 USETW(req.wValue, sel);
1562 USETW(req.wIndex, 0);
1563 USETW(req.wLength, 0);
1564 return (usbd_do_request(udev, mtx, &req, 0));
1567 /*------------------------------------------------------------------------*
1568 * usbd_req_set_hub_u1_timeout
1573 *------------------------------------------------------------------------*/
1575 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1576 uint8_t port, uint8_t timeout)
1578 struct usb_device_request req;
1580 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1581 req.bRequest = UR_SET_FEATURE;
1582 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1583 req.wIndex[0] = port;
1584 req.wIndex[1] = timeout;
1585 USETW(req.wLength, 0);
1586 return (usbd_do_request(udev, mtx, &req, 0));
1589 /*------------------------------------------------------------------------*
1590 * usbd_req_set_hub_u2_timeout
1595 *------------------------------------------------------------------------*/
1597 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1598 uint8_t port, uint8_t timeout)
1600 struct usb_device_request req;
1602 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1603 req.bRequest = UR_SET_FEATURE;
1604 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1605 req.wIndex[0] = port;
1606 req.wIndex[1] = timeout;
1607 USETW(req.wLength, 0);
1608 return (usbd_do_request(udev, mtx, &req, 0));
1611 /*------------------------------------------------------------------------*
1612 * usbd_req_set_hub_depth
1617 *------------------------------------------------------------------------*/
1619 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1622 struct usb_device_request req;
1624 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1625 req.bRequest = UR_SET_HUB_DEPTH;
1626 USETW(req.wValue, depth);
1627 USETW(req.wIndex, 0);
1628 USETW(req.wLength, 0);
1629 return (usbd_do_request(udev, mtx, &req, 0));
1632 /*------------------------------------------------------------------------*
1633 * usbd_req_clear_port_feature
1638 *------------------------------------------------------------------------*/
1640 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1641 uint8_t port, uint16_t sel)
1643 struct usb_device_request req;
1645 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1646 req.bRequest = UR_CLEAR_FEATURE;
1647 USETW(req.wValue, sel);
1648 req.wIndex[0] = port;
1650 USETW(req.wLength, 0);
1651 return (usbd_do_request(udev, mtx, &req, 0));
1654 /*------------------------------------------------------------------------*
1655 * usbd_req_set_port_feature
1660 *------------------------------------------------------------------------*/
1662 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1663 uint8_t port, uint16_t sel)
1665 struct usb_device_request req;
1667 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1668 req.bRequest = UR_SET_FEATURE;
1669 USETW(req.wValue, sel);
1670 req.wIndex[0] = port;
1672 USETW(req.wLength, 0);
1673 return (usbd_do_request(udev, mtx, &req, 0));
1676 /*------------------------------------------------------------------------*
1677 * usbd_req_set_protocol
1682 *------------------------------------------------------------------------*/
1684 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1685 uint8_t iface_index, uint16_t report)
1687 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1688 struct usb_device_request req;
1690 if ((iface == NULL) || (iface->idesc == NULL)) {
1691 return (USB_ERR_INVAL);
1693 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1694 iface, report, iface->idesc->bInterfaceNumber);
1696 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1697 req.bRequest = UR_SET_PROTOCOL;
1698 USETW(req.wValue, report);
1699 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1701 USETW(req.wLength, 0);
1702 return (usbd_do_request(udev, mtx, &req, 0));
1705 /*------------------------------------------------------------------------*
1706 * usbd_req_set_report
1711 *------------------------------------------------------------------------*/
1713 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1714 uint8_t iface_index, uint8_t type, uint8_t id)
1716 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1717 struct usb_device_request req;
1719 if ((iface == NULL) || (iface->idesc == NULL)) {
1720 return (USB_ERR_INVAL);
1722 DPRINTFN(5, "len=%d\n", len);
1724 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1725 req.bRequest = UR_SET_REPORT;
1726 USETW2(req.wValue, type, id);
1727 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1729 USETW(req.wLength, len);
1730 return (usbd_do_request(udev, mtx, &req, data));
1733 /*------------------------------------------------------------------------*
1734 * usbd_req_get_report
1739 *------------------------------------------------------------------------*/
1741 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1742 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1744 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1745 struct usb_device_request req;
1747 if ((iface == NULL) || (iface->idesc == NULL)) {
1748 return (USB_ERR_INVAL);
1750 DPRINTFN(5, "len=%d\n", len);
1752 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1753 req.bRequest = UR_GET_REPORT;
1754 USETW2(req.wValue, type, id);
1755 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1757 USETW(req.wLength, len);
1758 return (usbd_do_request(udev, mtx, &req, data));
1761 /*------------------------------------------------------------------------*
1767 *------------------------------------------------------------------------*/
1769 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1770 uint8_t iface_index, uint8_t duration, uint8_t id)
1772 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1773 struct usb_device_request req;
1775 if ((iface == NULL) || (iface->idesc == NULL)) {
1776 return (USB_ERR_INVAL);
1778 DPRINTFN(5, "%d %d\n", duration, id);
1780 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1781 req.bRequest = UR_SET_IDLE;
1782 USETW2(req.wValue, duration, id);
1783 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1785 USETW(req.wLength, 0);
1786 return (usbd_do_request(udev, mtx, &req, 0));
1789 /*------------------------------------------------------------------------*
1790 * usbd_req_get_report_descriptor
1795 *------------------------------------------------------------------------*/
1797 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1798 void *d, uint16_t size, uint8_t iface_index)
1800 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1801 struct usb_device_request req;
1803 if ((iface == NULL) || (iface->idesc == NULL)) {
1804 return (USB_ERR_INVAL);
1806 req.bmRequestType = UT_READ_INTERFACE;
1807 req.bRequest = UR_GET_DESCRIPTOR;
1808 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1809 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1811 USETW(req.wLength, size);
1812 return (usbd_do_request(udev, mtx, &req, d));
1815 /*------------------------------------------------------------------------*
1816 * usbd_req_set_config
1818 * This function is used to select the current configuration number in
1819 * both USB device side mode and USB host side mode. When setting the
1820 * configuration the function of the interfaces can change.
1825 *------------------------------------------------------------------------*/
1827 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1829 struct usb_device_request req;
1831 DPRINTF("setting config %d\n", conf);
1833 /* do "set configuration" request */
1835 req.bmRequestType = UT_WRITE_DEVICE;
1836 req.bRequest = UR_SET_CONFIG;
1837 req.wValue[0] = conf;
1839 USETW(req.wIndex, 0);
1840 USETW(req.wLength, 0);
1841 return (usbd_do_request(udev, mtx, &req, 0));
1844 /*------------------------------------------------------------------------*
1845 * usbd_req_get_config
1850 *------------------------------------------------------------------------*/
1852 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1854 struct usb_device_request req;
1856 req.bmRequestType = UT_READ_DEVICE;
1857 req.bRequest = UR_GET_CONFIG;
1858 USETW(req.wValue, 0);
1859 USETW(req.wIndex, 0);
1860 USETW(req.wLength, 1);
1861 return (usbd_do_request(udev, mtx, &req, pconf));
1864 /*------------------------------------------------------------------------*
1865 * usbd_setup_device_desc
1866 *------------------------------------------------------------------------*/
1868 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1873 * Get the first 8 bytes of the device descriptor !
1875 * NOTE: "usbd_do_request()" will check the device descriptor
1876 * next time we do a request to see if the maximum packet size
1877 * changed! The 8 first bytes of the device descriptor
1878 * contains the maximum packet size to use on control endpoint
1879 * 0. If this value is different from "USB_MAX_IPACKET" a new
1880 * USB control request will be setup!
1882 switch (udev->speed) {
1883 case USB_SPEED_FULL:
1885 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1886 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1888 DPRINTFN(0, "getting device descriptor "
1889 "at addr %d failed, %s\n", udev->address,
1895 DPRINTF("Minimum MaxPacketSize is large enough "
1896 "to hold the complete device descriptor\n");
1900 /* get the full device descriptor */
1901 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1903 /* try one more time, if error */
1905 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1908 DPRINTF("addr=%d, getting full desc failed\n",
1913 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1914 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1915 udev->address, UGETW(udev->ddesc.bcdUSB),
1916 udev->ddesc.bDeviceClass,
1917 udev->ddesc.bDeviceSubClass,
1918 udev->ddesc.bDeviceProtocol,
1919 udev->ddesc.bMaxPacketSize,
1920 udev->ddesc.bLength,
1926 /*------------------------------------------------------------------------*
1927 * usbd_req_re_enumerate
1929 * NOTE: After this function returns the hardware is in the
1930 * unconfigured state! The application is responsible for setting a
1931 * new configuration.
1936 *------------------------------------------------------------------------*/
1938 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1940 struct usb_device *parent_hub;
1943 uint8_t do_retry = 1;
1945 if (udev->flags.usb_mode != USB_MODE_HOST) {
1946 return (USB_ERR_INVAL);
1948 old_addr = udev->address;
1949 parent_hub = udev->parent_hub;
1950 if (parent_hub == NULL) {
1951 return (USB_ERR_INVAL);
1955 * Try to reset the High Speed parent HUB of a LOW- or FULL-
1956 * speed device, if any.
1958 if (udev->parent_hs_hub != NULL &&
1959 udev->speed != USB_SPEED_HIGH) {
1960 DPRINTF("Trying to reset parent High Speed TT.\n");
1961 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL,
1964 DPRINTF("Resetting parent High "
1965 "Speed TT failed (%s).\n",
1970 /* Try to warm reset first */
1971 if (parent_hub->speed == USB_SPEED_SUPER)
1972 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
1974 /* Try to reset the parent HUB port. */
1975 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1977 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1978 old_addr, usbd_errstr(err));
1983 * After that the port has been reset our device should be at
1986 udev->address = USB_START_ADDR;
1988 /* reset "bMaxPacketSize" */
1989 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1991 /* reset USB state */
1992 usb_set_device_state(udev, USB_STATE_POWERED);
1995 * Restore device address:
1997 err = usbd_req_set_address(udev, mtx, old_addr);
1999 /* XXX ignore any errors! */
2000 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2001 old_addr, usbd_errstr(err));
2004 * Restore device address, if the controller driver did not
2007 if (udev->address == USB_START_ADDR)
2008 udev->address = old_addr;
2010 /* setup the device descriptor and the initial "wMaxPacketSize" */
2011 err = usbd_setup_device_desc(udev, mtx);
2014 if (err && do_retry) {
2015 /* give the USB firmware some time to load */
2016 usb_pause_mtx(mtx, hz / 2);
2017 /* no more retries after this retry */
2022 /* restore address */
2023 if (udev->address == USB_START_ADDR)
2024 udev->address = old_addr;
2025 /* update state, if successful */
2027 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2031 /*------------------------------------------------------------------------*
2032 * usbd_req_clear_device_feature
2037 *------------------------------------------------------------------------*/
2039 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2042 struct usb_device_request req;
2044 req.bmRequestType = UT_WRITE_DEVICE;
2045 req.bRequest = UR_CLEAR_FEATURE;
2046 USETW(req.wValue, sel);
2047 USETW(req.wIndex, 0);
2048 USETW(req.wLength, 0);
2049 return (usbd_do_request(udev, mtx, &req, 0));
2052 /*------------------------------------------------------------------------*
2053 * usbd_req_set_device_feature
2058 *------------------------------------------------------------------------*/
2060 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2063 struct usb_device_request req;
2065 req.bmRequestType = UT_WRITE_DEVICE;
2066 req.bRequest = UR_SET_FEATURE;
2067 USETW(req.wValue, sel);
2068 USETW(req.wIndex, 0);
2069 USETW(req.wLength, 0);
2070 return (usbd_do_request(udev, mtx, &req, 0));
2073 /*------------------------------------------------------------------------*
2079 *------------------------------------------------------------------------*/
2081 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2084 struct usb_device_request req;
2086 /* For single TT HUBs the port should be 1 */
2088 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2089 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2092 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2093 req.bRequest = UR_RESET_TT;
2094 USETW(req.wValue, 0);
2095 req.wIndex[0] = port;
2097 USETW(req.wLength, 0);
2098 return (usbd_do_request(udev, mtx, &req, 0));
2101 /*------------------------------------------------------------------------*
2102 * usbd_req_clear_tt_buffer
2104 * For single TT HUBs the port should be 1.
2109 *------------------------------------------------------------------------*/
2111 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2112 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2114 struct usb_device_request req;
2117 /* For single TT HUBs the port should be 1 */
2119 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2120 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2123 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2124 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2126 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2127 req.bRequest = UR_CLEAR_TT_BUFFER;
2128 USETW(req.wValue, wValue);
2129 req.wIndex[0] = port;
2131 USETW(req.wLength, 0);
2132 return (usbd_do_request(udev, mtx, &req, 0));
2135 /*------------------------------------------------------------------------*
2136 * usbd_req_set_port_link_state
2138 * USB 3.0 specific request
2143 *------------------------------------------------------------------------*/
2145 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2146 uint8_t port, uint8_t link_state)
2148 struct usb_device_request req;
2150 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2151 req.bRequest = UR_SET_FEATURE;
2152 USETW(req.wValue, UHF_PORT_LINK_STATE);
2153 req.wIndex[0] = port;
2154 req.wIndex[1] = link_state;
2155 USETW(req.wLength, 0);
2156 return (usbd_do_request(udev, mtx, &req, 0));
2159 /*------------------------------------------------------------------------*
2160 * usbd_req_set_lpm_info
2162 * USB 2.0 specific request for Link Power Management.
2166 * USB_ERR_PENDING_REQUESTS: NYET
2167 * USB_ERR_TIMEOUT: TIMEOUT
2168 * USB_ERR_STALL: STALL
2170 *------------------------------------------------------------------------*/
2172 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2173 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2175 struct usb_device_request req;
2179 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2180 req.bRequest = UR_SET_AND_TEST;
2181 USETW(req.wValue, UHF_PORT_L1);
2182 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2183 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2184 USETW(req.wLength, sizeof(buf));
2186 /* set default value in case of short transfer */
2189 err = usbd_do_request(udev, mtx, &req, buf);
2194 case 0x00: /* SUCCESS */
2196 case 0x10: /* NYET */
2197 err = USB_ERR_PENDING_REQUESTS;
2199 case 0x11: /* TIMEOUT */
2200 err = USB_ERR_TIMEOUT;
2202 case 0x30: /* STALL */
2203 err = USB_ERR_STALLED;
2205 default: /* reserved */
2206 err = USB_ERR_IOERROR;