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/linker_set.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usb_ioctl.h>
53 #include <dev/usb/usbhid.h>
55 #define USB_DEBUG_VAR usb_debug
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_busdma.h>
59 #include <dev/usb/usb_request.h>
60 #include <dev/usb/usb_process.h>
61 #include <dev/usb/usb_transfer.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_util.h>
65 #include <dev/usb/usb_dynamic.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <sys/ctype.h>
72 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
73 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
75 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
76 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
77 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
78 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
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 data 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 goto tr_setup; /* device was unconfigured */
248 /* some hardware needs a callback to clear the data toggle */
249 usbd_clear_stall_locked(udev, ep);
250 /* start up the current or next transfer, if any */
251 usb_command_wrapper(&ep->endpoint_q,
252 ep->endpoint_q.curr);
259 break; /* no endpoints - nothing to do */
260 if ((ep < ep_first) || (ep >= ep_end))
261 ep = ep_first; /* endpoint wrapped around */
265 /* setup a clear-stall packet */
267 req.bmRequestType = UT_WRITE_ENDPOINT;
268 req.bRequest = UR_CLEAR_FEATURE;
269 USETW(req.wValue, UF_ENDPOINT_HALT);
270 req.wIndex[0] = ep->edesc->bEndpointAddress;
272 USETW(req.wLength, 0);
274 /* copy in the transfer */
276 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
279 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
281 USB_BUS_UNLOCK(udev->bus);
283 usbd_transfer_submit(xfer);
285 USB_BUS_LOCK(udev->bus);
293 if (xfer->error == USB_ERR_CANCELLED) {
299 /* store current endpoint */
301 USB_BUS_UNLOCK(udev->bus);
304 static usb_handle_req_t *
305 usbd_get_hr_func(struct usb_device *udev)
307 /* figure out if there is a Handle Request function */
308 if (udev->flags.usb_mode == USB_MODE_DEVICE)
309 return (usb_temp_get_desc_p);
310 else if (udev->parent_hub == NULL)
311 return (udev->bus->methods->roothub_exec);
316 /*------------------------------------------------------------------------*
317 * usbd_do_request_flags and usbd_do_request
319 * Description of arguments passed to these functions:
321 * "udev" - this is the "usb_device" structure pointer on which the
322 * request should be performed. It is possible to call this function
323 * in both Host Side mode and Device Side mode.
325 * "mtx" - if this argument is non-NULL the mutex pointed to by it
326 * will get dropped and picked up during the execution of this
327 * function, hence this function sometimes needs to sleep. If this
328 * argument is NULL it has no effect.
330 * "req" - this argument must always be non-NULL and points to an
331 * 8-byte structure holding the USB request to be done. The USB
332 * request structure has a bit telling the direction of the USB
333 * request, if it is a read or a write.
335 * "data" - if the "wLength" part of the structure pointed to by "req"
336 * is non-zero this argument must point to a valid kernel buffer which
337 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
340 * "flags" - here is a list of valid flags:
342 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
345 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
346 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
347 * sysctl. This flag is mostly useful for debugging.
349 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
352 * "actlen" - if non-NULL the actual transfer length will be stored in
353 * the 16-bit unsigned integer pointed to by "actlen". This
354 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
357 * "timeout" - gives the timeout for the control transfer in
358 * milliseconds. A "timeout" value less than 50 milliseconds is
359 * treated like a 50 millisecond timeout. A "timeout" value greater
360 * than 30 seconds is treated like a 30 second timeout. This USB stack
361 * does not allow control requests without a timeout.
363 * NOTE: This function is thread safe. All calls to
364 * "usbd_do_request_flags" will be serialised by the use of an
365 * internal "sx_lock".
370 *------------------------------------------------------------------------*/
372 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
373 struct usb_device_request *req, void *data, uint16_t flags,
374 uint16_t *actlen, usb_timeout_t timeout)
377 struct usb_ctrl_debug_bits dbg;
379 usb_handle_req_t *hr_func;
380 struct usb_xfer *xfer;
383 usb_ticks_t start_ticks;
384 usb_ticks_t delta_ticks;
385 usb_ticks_t max_ticks;
392 /* timeout is too small */
395 if (timeout > 30000) {
396 /* timeout is too big */
399 length = UGETW(req->wLength);
401 enum_locked = usbd_enum_is_locked(udev);
403 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
404 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
405 udev, req->bmRequestType, req->bRequest,
406 req->wValue[1], req->wValue[0],
407 req->wIndex[1], req->wIndex[0],
408 req->wLength[1], req->wLength[0]);
410 /* Check if the device is still alive */
411 if (udev->state < USB_STATE_POWERED) {
412 DPRINTF("usb device has gone\n");
413 return (USB_ERR_NOT_CONFIGURED);
417 * Set "actlen" to a known value in case the caller does not
418 * check the return value:
423 #if (USB_HAVE_USER_IO == 0)
424 if (flags & USB_USER_DATA_PTR)
425 return (USB_ERR_INVAL);
427 if ((mtx != NULL) && (mtx != &Giant)) {
429 mtx_assert(mtx, MA_NOTOWNED);
433 * We need to allow suspend and resume at this point, else the
434 * control transfer will timeout if the device is suspended!
437 usbd_sr_unlock(udev);
440 * Grab the default sx-lock so that serialisation
441 * is achieved when multiple threads are involved:
443 sx_xlock(&udev->ctrl_sx);
445 hr_func = usbd_get_hr_func(udev);
447 if (hr_func != NULL) {
448 DPRINTF("Handle Request function is set\n");
453 if (!(req->bmRequestType & UT_READ)) {
455 DPRINTFN(1, "The handle request function "
456 "does not support writing data!\n");
462 /* The root HUB code needs the BUS lock locked */
464 USB_BUS_LOCK(udev->bus);
465 err = (hr_func) (udev, req, &desc, &temp);
466 USB_BUS_UNLOCK(udev->bus);
472 if (!(flags & USB_SHORT_XFER_OK)) {
473 err = USB_ERR_SHORT_XFER;
483 if (flags & USB_USER_DATA_PTR) {
484 if (copyout(desc, data, length)) {
490 bcopy(desc, data, length);
492 goto done; /* success */
496 * Setup a new USB transfer or use the existing one, if any:
498 usbd_ctrl_transfer_setup(udev);
500 xfer = udev->ctrl_xfer[0];
502 /* most likely out of memory */
509 usbd_get_debug_bits(udev, req, &dbg);
511 /* Check for fault injection */
513 flags |= USB_DELAY_STATUS_STAGE;
517 if (flags & USB_DELAY_STATUS_STAGE)
518 xfer->flags.manual_status = 1;
520 xfer->flags.manual_status = 0;
522 if (flags & USB_SHORT_XFER_OK)
523 xfer->flags.short_xfer_ok = 1;
525 xfer->flags.short_xfer_ok = 0;
527 xfer->timeout = timeout;
531 max_ticks = USB_MS_TO_TICKS(timeout);
533 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
535 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
539 if (temp > usbd_xfer_max_len(xfer)) {
540 temp = usbd_xfer_max_len(xfer);
543 if (xfer->flags.manual_status) {
544 if (usbd_xfer_frame_len(xfer, 0) != 0) {
545 /* Execute data stage separately */
547 } else if (temp > 0) {
552 if (dbg.ds_delay > 0) {
554 xfer->xroot->xfer_mtx,
555 USB_MS_TO_TICKS(dbg.ds_delay));
556 /* make sure we don't time out */
562 usbd_xfer_set_frame_len(xfer, 1, temp);
565 if (!(req->bmRequestType & UT_READ)) {
567 if (flags & USB_USER_DATA_PTR) {
568 USB_XFER_UNLOCK(xfer);
569 err = usbd_copy_in_user(xfer->frbuffers + 1,
578 usbd_copy_in(xfer->frbuffers + 1,
581 usbd_xfer_set_frames(xfer, 2);
583 if (usbd_xfer_frame_len(xfer, 0) == 0) {
584 if (xfer->flags.manual_status) {
590 if (dbg.ss_delay > 0) {
592 xfer->xroot->xfer_mtx,
593 USB_MS_TO_TICKS(dbg.ss_delay));
594 /* make sure we don't time out */
598 xfer->flags.manual_status = 0;
603 usbd_xfer_set_frames(xfer, 1);
606 usbd_transfer_start(xfer);
608 while (usbd_transfer_pending(xfer)) {
609 cv_wait(&udev->ctrlreq_cv,
610 xfer->xroot->xfer_mtx);
619 /* get actual length of DATA stage */
621 if (xfer->aframes < 2) {
624 acttemp = usbd_xfer_frame_len(xfer, 1);
627 /* check for short packet */
629 if (temp > acttemp) {
634 if (req->bmRequestType & UT_READ) {
636 if (flags & USB_USER_DATA_PTR) {
637 USB_XFER_UNLOCK(xfer);
638 err = usbd_copy_out_user(xfer->frbuffers + 1,
647 usbd_copy_out(xfer->frbuffers + 1,
652 * Clear "frlengths[0]" so that we don't send the setup
655 usbd_xfer_set_frame_len(xfer, 0, 0);
657 /* update length and data pointer */
659 data = USB_ADD_BYTES(data, temp);
664 /* check for timeout */
666 delta_ticks = ticks - start_ticks;
667 if (delta_ticks > max_ticks) {
669 err = USB_ERR_TIMEOUT;
679 * Make sure that the control endpoint is no longer
680 * blocked in case of a non-transfer related error:
682 usbd_transfer_stop(xfer);
684 USB_XFER_UNLOCK(xfer);
687 sx_xunlock(&udev->ctrl_sx);
692 if ((mtx != NULL) && (mtx != &Giant))
695 return ((usb_error_t)err);
698 /*------------------------------------------------------------------------*
699 * usbd_do_request_proc - factored out code
701 * This function is factored out code. It does basically the same like
702 * usbd_do_request_flags, except it will check the status of the
703 * passed process argument before doing the USB request. If the
704 * process is draining the USB_ERR_IOERROR code will be returned. It
705 * is assumed that the mutex associated with the process is locked
706 * when calling this function.
707 *------------------------------------------------------------------------*/
709 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
710 struct usb_device_request *req, void *data, uint16_t flags,
711 uint16_t *actlen, usb_timeout_t timeout)
716 /* get request data length */
717 len = UGETW(req->wLength);
719 /* check if the device is being detached */
720 if (usb_proc_is_gone(pproc)) {
721 err = USB_ERR_IOERROR;
725 /* forward the USB request */
726 err = usbd_do_request_flags(udev, pproc->up_mtx,
727 req, data, flags, actlen, timeout);
730 /* on failure we zero the data */
731 /* on short packet we zero the unused data */
732 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
734 memset(data, 0, len);
735 else if (actlen && *actlen != len)
736 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
741 /*------------------------------------------------------------------------*
742 * usbd_req_reset_port
744 * This function will instruct a USB HUB to perform a reset sequence
745 * on the specified port number.
748 * 0: Success. The USB device should now be at address zero.
749 * Else: Failure. No USB device is present and the USB port should be
751 *------------------------------------------------------------------------*/
753 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
755 struct usb_port_status ps;
760 uint16_t pr_poll_delay;
761 uint16_t pr_recovery_delay;
764 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
769 /* range check input parameters */
770 pr_poll_delay = usb_pr_poll_delay;
771 if (pr_poll_delay < 1) {
773 } else if (pr_poll_delay > 1000) {
774 pr_poll_delay = 1000;
776 pr_recovery_delay = usb_pr_recovery_delay;
777 if (pr_recovery_delay > 1000) {
778 pr_recovery_delay = 1000;
784 /* wait for the device to recover from reset */
785 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
788 /* wait for the device to recover from reset */
789 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
790 n += USB_PORT_RESET_DELAY;
792 err = usbd_req_get_port_status(udev, mtx, &ps, port);
796 /* if the device disappeared, just give up */
797 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
800 /* check if reset is complete */
801 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
804 /* check for timeout */
811 /* clear port reset first */
812 err = usbd_req_clear_port_feature(
813 udev, mtx, port, UHF_C_PORT_RESET);
817 /* check for timeout */
819 err = USB_ERR_TIMEOUT;
823 /* wait for the device to recover from reset */
824 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
826 /* wait for the device to recover from reset */
827 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
831 DPRINTFN(2, "port %d reset returning error=%s\n",
832 port, usbd_errstr(err));
836 /*------------------------------------------------------------------------*
837 * usbd_req_warm_reset_port
839 * This function will instruct an USB HUB to perform a warm reset
840 * sequence on the specified port number. This kind of reset is not
841 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
842 * for SUPER-speed USB HUBs.
845 * 0: Success. The USB device should now be available again.
846 * Else: Failure. No USB device is present and the USB port should be
848 *------------------------------------------------------------------------*/
850 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
852 struct usb_port_status ps;
857 uint16_t pr_poll_delay;
858 uint16_t pr_recovery_delay;
861 err = usbd_req_set_port_feature(udev, mtx, port, UHF_BH_PORT_RESET);
866 /* range check input parameters */
867 pr_poll_delay = usb_pr_poll_delay;
868 if (pr_poll_delay < 1) {
870 } else if (pr_poll_delay > 1000) {
871 pr_poll_delay = 1000;
873 pr_recovery_delay = usb_pr_recovery_delay;
874 if (pr_recovery_delay > 1000) {
875 pr_recovery_delay = 1000;
881 /* wait for the device to recover from reset */
882 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
885 /* wait for the device to recover from reset */
886 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
887 n += USB_PORT_RESET_DELAY;
889 err = usbd_req_get_port_status(udev, mtx, &ps, port);
893 /* if the device disappeared, just give up */
894 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
897 /* check if reset is complete */
898 if (UGETW(ps.wPortChange) & UPS_C_BH_PORT_RESET) {
901 /* check for timeout */
908 /* clear port reset first */
909 err = usbd_req_clear_port_feature(
910 udev, mtx, port, UHF_C_BH_PORT_RESET);
914 /* check for timeout */
916 err = USB_ERR_TIMEOUT;
920 /* wait for the device to recover from reset */
921 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
923 /* wait for the device to recover from reset */
924 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
928 DPRINTFN(2, "port %d warm reset returning error=%s\n",
929 port, usbd_errstr(err));
933 /*------------------------------------------------------------------------*
936 * This function can be used to retrieve USB descriptors. It contains
937 * some additional logic like zeroing of missing descriptor bytes and
938 * retrying an USB descriptor in case of failure. The "min_len"
939 * argument specifies the minimum descriptor length. The "max_len"
940 * argument specifies the maximum descriptor length. If the real
941 * descriptor length is less than the minimum length the missing
942 * byte(s) will be zeroed. The type field, the second byte of the USB
943 * descriptor, will get forced to the correct type. If the "actlen"
944 * pointer is non-NULL, the actual length of the transfer will get
945 * stored in the 16-bit unsigned integer which it is pointing to. The
946 * first byte of the descriptor will not get updated. If the "actlen"
947 * pointer is NULL the first byte of the descriptor will get updated
948 * to reflect the actual length instead. If "min_len" is not equal to
949 * "max_len" then this function will try to retrive the beginning of
950 * the descriptor and base the maximum length on the first byte of the
956 *------------------------------------------------------------------------*/
958 usbd_req_get_desc(struct usb_device *udev,
959 struct mtx *mtx, uint16_t *actlen, void *desc,
960 uint16_t min_len, uint16_t max_len,
961 uint16_t id, uint8_t type, uint8_t index,
964 struct usb_device_request req;
968 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
969 id, type, index, max_len);
971 req.bmRequestType = UT_READ_DEVICE;
972 req.bRequest = UR_GET_DESCRIPTOR;
973 USETW2(req.wValue, type, index);
974 USETW(req.wIndex, id);
978 if ((min_len < 2) || (max_len < 2)) {
982 USETW(req.wLength, min_len);
984 err = usbd_do_request_flags(udev, mtx, &req,
985 desc, 0, NULL, 1000);
993 usb_pause_mtx(mtx, hz / 5);
999 if (min_len == max_len) {
1001 /* enforce correct length */
1002 if ((buf[0] > min_len) && (actlen == NULL))
1005 /* enforce correct type */
1012 if (max_len > buf[0]) {
1015 /* zero minimum data */
1017 while (min_len > max_len) {
1022 /* set new minimum length */
1027 if (actlen != NULL) {
1036 /*------------------------------------------------------------------------*
1037 * usbd_req_get_string_any
1039 * This function will return the string given by "string_index"
1040 * using the first language ID. The maximum length "len" includes
1041 * the terminating zero. The "len" argument should be twice as
1042 * big pluss 2 bytes, compared with the actual maximum string length !
1047 *------------------------------------------------------------------------*/
1049 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1050 uint16_t len, uint8_t string_index)
1061 /* should not happen */
1062 return (USB_ERR_NORMAL_COMPLETION);
1064 if (string_index == 0) {
1065 /* this is the language table */
1067 return (USB_ERR_INVAL);
1069 if (udev->flags.no_strings) {
1071 return (USB_ERR_STALLED);
1073 err = usbd_req_get_string_desc
1074 (udev, mtx, buf, len, udev->langid, string_index);
1079 temp = (uint8_t *)buf;
1082 /* string length is too short */
1084 return (USB_ERR_INVAL);
1086 /* reserve one byte for terminating zero */
1089 /* find maximum length */
1091 n = (temp[0] / 2) - 1;
1095 /* skip descriptor header */
1098 /* reset swap state */
1101 /* convert and filter */
1102 for (i = 0; (i != n); i++) {
1103 c = UGETW(temp + (2 * i));
1105 /* convert from Unicode, handle buggy strings */
1106 if (((c & 0xff00) == 0) && (swap & 1)) {
1107 /* Little Endian, default */
1110 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1115 /* silently skip bad character */
1120 * Filter by default - We only allow alphanumerical
1121 * and a few more to avoid any problems with scripts
1134 /* silently skip bad character */
1136 *s = 0; /* zero terminate resulting string */
1137 return (USB_ERR_NORMAL_COMPLETION);
1140 /*------------------------------------------------------------------------*
1141 * usbd_req_get_string_desc
1143 * If you don't know the language ID, consider using
1144 * "usbd_req_get_string_any()".
1149 *------------------------------------------------------------------------*/
1151 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1152 uint16_t max_len, uint16_t lang_id,
1153 uint8_t string_index)
1155 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1156 UDESC_STRING, string_index, 0));
1159 /*------------------------------------------------------------------------*
1160 * usbd_req_get_config_desc_ptr
1162 * This function is used in device side mode to retrieve the pointer
1163 * to the generated config descriptor. This saves allocating space for
1164 * an additional config descriptor when setting the configuration.
1169 *------------------------------------------------------------------------*/
1171 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1172 struct usb_config_descriptor **ppcd, uint16_t wValue)
1174 struct usb_device_request req;
1175 usb_handle_req_t *hr_func;
1180 req.bmRequestType = UT_READ_DEVICE;
1181 req.bRequest = UR_GET_DESCRIPTOR;
1182 USETW(req.wValue, wValue);
1183 USETW(req.wIndex, 0);
1184 USETW(req.wLength, 0);
1189 hr_func = usbd_get_hr_func(udev);
1191 if (hr_func == NULL)
1192 err = USB_ERR_INVAL;
1194 USB_BUS_LOCK(udev->bus);
1195 err = (hr_func) (udev, &req, &ptr, &len);
1196 USB_BUS_UNLOCK(udev->bus);
1201 else if (ptr == NULL)
1202 err = USB_ERR_INVAL;
1204 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1209 /*------------------------------------------------------------------------*
1210 * usbd_req_get_config_desc
1215 *------------------------------------------------------------------------*/
1217 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1218 struct usb_config_descriptor *d, uint8_t conf_index)
1222 DPRINTFN(4, "confidx=%d\n", conf_index);
1224 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1225 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1229 /* Extra sanity checking */
1230 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1231 err = USB_ERR_INVAL;
1237 /*------------------------------------------------------------------------*
1238 * usbd_req_get_config_desc_full
1240 * This function gets the complete USB configuration descriptor and
1241 * ensures that "wTotalLength" is correct.
1246 *------------------------------------------------------------------------*/
1248 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1249 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1252 struct usb_config_descriptor cd;
1253 struct usb_config_descriptor *cdesc;
1257 DPRINTFN(4, "index=%d\n", index);
1261 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1265 /* get full descriptor */
1266 len = UGETW(cd.wTotalLength);
1267 if (len < sizeof(*cdesc)) {
1268 /* corrupt descriptor */
1269 return (USB_ERR_INVAL);
1271 cdesc = malloc(len, mtype, M_WAITOK);
1272 if (cdesc == NULL) {
1273 return (USB_ERR_NOMEM);
1275 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1276 UDESC_CONFIG, index, 3);
1281 /* make sure that the device is not fooling us: */
1282 USETW(cdesc->wTotalLength, len);
1286 return (0); /* success */
1289 /*------------------------------------------------------------------------*
1290 * usbd_req_get_device_desc
1295 *------------------------------------------------------------------------*/
1297 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1298 struct usb_device_descriptor *d)
1301 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1302 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1305 /*------------------------------------------------------------------------*
1306 * usbd_req_get_alt_interface_no
1311 *------------------------------------------------------------------------*/
1313 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1314 uint8_t *alt_iface_no, uint8_t iface_index)
1316 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1317 struct usb_device_request req;
1319 if ((iface == NULL) || (iface->idesc == NULL))
1320 return (USB_ERR_INVAL);
1322 req.bmRequestType = UT_READ_INTERFACE;
1323 req.bRequest = UR_GET_INTERFACE;
1324 USETW(req.wValue, 0);
1325 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1327 USETW(req.wLength, 1);
1328 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1331 /*------------------------------------------------------------------------*
1332 * usbd_req_set_alt_interface_no
1337 *------------------------------------------------------------------------*/
1339 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1340 uint8_t iface_index, uint8_t alt_no)
1342 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1343 struct usb_device_request req;
1345 if ((iface == NULL) || (iface->idesc == NULL))
1346 return (USB_ERR_INVAL);
1348 req.bmRequestType = UT_WRITE_INTERFACE;
1349 req.bRequest = UR_SET_INTERFACE;
1350 req.wValue[0] = alt_no;
1352 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1354 USETW(req.wLength, 0);
1355 return (usbd_do_request(udev, mtx, &req, 0));
1358 /*------------------------------------------------------------------------*
1359 * usbd_req_get_device_status
1364 *------------------------------------------------------------------------*/
1366 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1367 struct usb_status *st)
1369 struct usb_device_request req;
1371 req.bmRequestType = UT_READ_DEVICE;
1372 req.bRequest = UR_GET_STATUS;
1373 USETW(req.wValue, 0);
1374 USETW(req.wIndex, 0);
1375 USETW(req.wLength, sizeof(*st));
1376 return (usbd_do_request(udev, mtx, &req, st));
1379 /*------------------------------------------------------------------------*
1380 * usbd_req_get_hub_descriptor
1385 *------------------------------------------------------------------------*/
1387 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1388 struct usb_hub_descriptor *hd, uint8_t nports)
1390 struct usb_device_request req;
1391 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1393 req.bmRequestType = UT_READ_CLASS_DEVICE;
1394 req.bRequest = UR_GET_DESCRIPTOR;
1395 USETW2(req.wValue, UDESC_HUB, 0);
1396 USETW(req.wIndex, 0);
1397 USETW(req.wLength, len);
1398 return (usbd_do_request(udev, mtx, &req, hd));
1401 /*------------------------------------------------------------------------*
1402 * usbd_req_get_ss_hub_descriptor
1407 *------------------------------------------------------------------------*/
1409 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1410 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1412 struct usb_device_request req;
1413 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1415 req.bmRequestType = UT_READ_CLASS_DEVICE;
1416 req.bRequest = UR_GET_DESCRIPTOR;
1417 USETW2(req.wValue, UDESC_SS_HUB, 0);
1418 USETW(req.wIndex, 0);
1419 USETW(req.wLength, len);
1420 return (usbd_do_request(udev, mtx, &req, hd));
1423 /*------------------------------------------------------------------------*
1424 * usbd_req_get_hub_status
1429 *------------------------------------------------------------------------*/
1431 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1432 struct usb_hub_status *st)
1434 struct usb_device_request req;
1436 req.bmRequestType = UT_READ_CLASS_DEVICE;
1437 req.bRequest = UR_GET_STATUS;
1438 USETW(req.wValue, 0);
1439 USETW(req.wIndex, 0);
1440 USETW(req.wLength, sizeof(struct usb_hub_status));
1441 return (usbd_do_request(udev, mtx, &req, st));
1444 /*------------------------------------------------------------------------*
1445 * usbd_req_set_address
1447 * This function is used to set the address for an USB device. After
1448 * port reset the USB device will respond at address zero.
1453 *------------------------------------------------------------------------*/
1455 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1457 struct usb_device_request req;
1460 DPRINTFN(6, "setting device address=%d\n", addr);
1462 req.bmRequestType = UT_WRITE_DEVICE;
1463 req.bRequest = UR_SET_ADDRESS;
1464 USETW(req.wValue, addr);
1465 USETW(req.wIndex, 0);
1466 USETW(req.wLength, 0);
1468 err = USB_ERR_INVAL;
1470 /* check if USB controller handles set address */
1471 if (udev->bus->methods->set_address != NULL)
1472 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1474 if (err != USB_ERR_INVAL)
1477 /* Setting the address should not take more than 1 second ! */
1478 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1479 USB_DELAY_STATUS_STAGE, NULL, 1000);
1482 /* allow device time to set new address */
1484 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1489 /*------------------------------------------------------------------------*
1490 * usbd_req_get_port_status
1495 *------------------------------------------------------------------------*/
1497 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1498 struct usb_port_status *ps, uint8_t port)
1500 struct usb_device_request req;
1502 req.bmRequestType = UT_READ_CLASS_OTHER;
1503 req.bRequest = UR_GET_STATUS;
1504 USETW(req.wValue, 0);
1505 req.wIndex[0] = port;
1507 USETW(req.wLength, sizeof *ps);
1508 return (usbd_do_request(udev, mtx, &req, ps));
1511 /*------------------------------------------------------------------------*
1512 * usbd_req_clear_hub_feature
1517 *------------------------------------------------------------------------*/
1519 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1522 struct usb_device_request req;
1524 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1525 req.bRequest = UR_CLEAR_FEATURE;
1526 USETW(req.wValue, sel);
1527 USETW(req.wIndex, 0);
1528 USETW(req.wLength, 0);
1529 return (usbd_do_request(udev, mtx, &req, 0));
1532 /*------------------------------------------------------------------------*
1533 * usbd_req_set_hub_feature
1538 *------------------------------------------------------------------------*/
1540 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1543 struct usb_device_request req;
1545 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1546 req.bRequest = UR_SET_FEATURE;
1547 USETW(req.wValue, sel);
1548 USETW(req.wIndex, 0);
1549 USETW(req.wLength, 0);
1550 return (usbd_do_request(udev, mtx, &req, 0));
1553 /*------------------------------------------------------------------------*
1554 * usbd_req_set_hub_u1_timeout
1559 *------------------------------------------------------------------------*/
1561 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1562 uint8_t port, uint8_t timeout)
1564 struct usb_device_request req;
1566 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1567 req.bRequest = UR_SET_FEATURE;
1568 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1569 req.wIndex[0] = port;
1570 req.wIndex[1] = timeout;
1571 USETW(req.wLength, 0);
1572 return (usbd_do_request(udev, mtx, &req, 0));
1575 /*------------------------------------------------------------------------*
1576 * usbd_req_set_hub_u2_timeout
1581 *------------------------------------------------------------------------*/
1583 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1584 uint8_t port, uint8_t timeout)
1586 struct usb_device_request req;
1588 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1589 req.bRequest = UR_SET_FEATURE;
1590 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1591 req.wIndex[0] = port;
1592 req.wIndex[1] = timeout;
1593 USETW(req.wLength, 0);
1594 return (usbd_do_request(udev, mtx, &req, 0));
1597 /*------------------------------------------------------------------------*
1598 * usbd_req_set_hub_depth
1603 *------------------------------------------------------------------------*/
1605 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1608 struct usb_device_request req;
1610 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1611 req.bRequest = UR_SET_HUB_DEPTH;
1612 USETW(req.wValue, depth);
1613 USETW(req.wIndex, 0);
1614 USETW(req.wLength, 0);
1615 return (usbd_do_request(udev, mtx, &req, 0));
1618 /*------------------------------------------------------------------------*
1619 * usbd_req_clear_port_feature
1624 *------------------------------------------------------------------------*/
1626 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1627 uint8_t port, uint16_t sel)
1629 struct usb_device_request req;
1631 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1632 req.bRequest = UR_CLEAR_FEATURE;
1633 USETW(req.wValue, sel);
1634 req.wIndex[0] = port;
1636 USETW(req.wLength, 0);
1637 return (usbd_do_request(udev, mtx, &req, 0));
1640 /*------------------------------------------------------------------------*
1641 * usbd_req_set_port_feature
1646 *------------------------------------------------------------------------*/
1648 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1649 uint8_t port, uint16_t sel)
1651 struct usb_device_request req;
1653 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1654 req.bRequest = UR_SET_FEATURE;
1655 USETW(req.wValue, sel);
1656 req.wIndex[0] = port;
1658 USETW(req.wLength, 0);
1659 return (usbd_do_request(udev, mtx, &req, 0));
1662 /*------------------------------------------------------------------------*
1663 * usbd_req_set_protocol
1668 *------------------------------------------------------------------------*/
1670 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1671 uint8_t iface_index, uint16_t report)
1673 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1674 struct usb_device_request req;
1676 if ((iface == NULL) || (iface->idesc == NULL)) {
1677 return (USB_ERR_INVAL);
1679 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1680 iface, report, iface->idesc->bInterfaceNumber);
1682 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1683 req.bRequest = UR_SET_PROTOCOL;
1684 USETW(req.wValue, report);
1685 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1687 USETW(req.wLength, 0);
1688 return (usbd_do_request(udev, mtx, &req, 0));
1691 /*------------------------------------------------------------------------*
1692 * usbd_req_set_report
1697 *------------------------------------------------------------------------*/
1699 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1700 uint8_t iface_index, uint8_t type, uint8_t id)
1702 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1703 struct usb_device_request req;
1705 if ((iface == NULL) || (iface->idesc == NULL)) {
1706 return (USB_ERR_INVAL);
1708 DPRINTFN(5, "len=%d\n", len);
1710 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1711 req.bRequest = UR_SET_REPORT;
1712 USETW2(req.wValue, type, id);
1713 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1715 USETW(req.wLength, len);
1716 return (usbd_do_request(udev, mtx, &req, data));
1719 /*------------------------------------------------------------------------*
1720 * usbd_req_get_report
1725 *------------------------------------------------------------------------*/
1727 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1728 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1730 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1731 struct usb_device_request req;
1733 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1734 return (USB_ERR_INVAL);
1736 DPRINTFN(5, "len=%d\n", len);
1738 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1739 req.bRequest = UR_GET_REPORT;
1740 USETW2(req.wValue, type, id);
1741 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1743 USETW(req.wLength, len);
1744 return (usbd_do_request(udev, mtx, &req, data));
1747 /*------------------------------------------------------------------------*
1753 *------------------------------------------------------------------------*/
1755 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1756 uint8_t iface_index, uint8_t duration, uint8_t id)
1758 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1759 struct usb_device_request req;
1761 if ((iface == NULL) || (iface->idesc == NULL)) {
1762 return (USB_ERR_INVAL);
1764 DPRINTFN(5, "%d %d\n", duration, id);
1766 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1767 req.bRequest = UR_SET_IDLE;
1768 USETW2(req.wValue, duration, id);
1769 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1771 USETW(req.wLength, 0);
1772 return (usbd_do_request(udev, mtx, &req, 0));
1775 /*------------------------------------------------------------------------*
1776 * usbd_req_get_report_descriptor
1781 *------------------------------------------------------------------------*/
1783 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1784 void *d, uint16_t size, uint8_t iface_index)
1786 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1787 struct usb_device_request req;
1789 if ((iface == NULL) || (iface->idesc == NULL)) {
1790 return (USB_ERR_INVAL);
1792 req.bmRequestType = UT_READ_INTERFACE;
1793 req.bRequest = UR_GET_DESCRIPTOR;
1794 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1795 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1797 USETW(req.wLength, size);
1798 return (usbd_do_request(udev, mtx, &req, d));
1801 /*------------------------------------------------------------------------*
1802 * usbd_req_set_config
1804 * This function is used to select the current configuration number in
1805 * both USB device side mode and USB host side mode. When setting the
1806 * configuration the function of the interfaces can change.
1811 *------------------------------------------------------------------------*/
1813 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1815 struct usb_device_request req;
1817 DPRINTF("setting config %d\n", conf);
1819 /* do "set configuration" request */
1821 req.bmRequestType = UT_WRITE_DEVICE;
1822 req.bRequest = UR_SET_CONFIG;
1823 req.wValue[0] = conf;
1825 USETW(req.wIndex, 0);
1826 USETW(req.wLength, 0);
1827 return (usbd_do_request(udev, mtx, &req, 0));
1830 /*------------------------------------------------------------------------*
1831 * usbd_req_get_config
1836 *------------------------------------------------------------------------*/
1838 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1840 struct usb_device_request req;
1842 req.bmRequestType = UT_READ_DEVICE;
1843 req.bRequest = UR_GET_CONFIG;
1844 USETW(req.wValue, 0);
1845 USETW(req.wIndex, 0);
1846 USETW(req.wLength, 1);
1847 return (usbd_do_request(udev, mtx, &req, pconf));
1850 /*------------------------------------------------------------------------*
1851 * usbd_setup_device_desc
1852 *------------------------------------------------------------------------*/
1854 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1859 * Get the first 8 bytes of the device descriptor !
1861 * NOTE: "usbd_do_request()" will check the device descriptor
1862 * next time we do a request to see if the maximum packet size
1863 * changed! The 8 first bytes of the device descriptor
1864 * contains the maximum packet size to use on control endpoint
1865 * 0. If this value is different from "USB_MAX_IPACKET" a new
1866 * USB control request will be setup!
1868 switch (udev->speed) {
1869 case USB_SPEED_FULL:
1871 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1872 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1874 DPRINTFN(0, "getting device descriptor "
1875 "at addr %d failed, %s\n", udev->address,
1881 DPRINTF("Minimum MaxPacketSize is large enough "
1882 "to hold the complete device descriptor\n");
1886 /* get the full device descriptor */
1887 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1889 /* try one more time, if error */
1891 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1894 DPRINTF("addr=%d, getting full desc failed\n",
1899 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1900 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1901 udev->address, UGETW(udev->ddesc.bcdUSB),
1902 udev->ddesc.bDeviceClass,
1903 udev->ddesc.bDeviceSubClass,
1904 udev->ddesc.bDeviceProtocol,
1905 udev->ddesc.bMaxPacketSize,
1906 udev->ddesc.bLength,
1912 /*------------------------------------------------------------------------*
1913 * usbd_req_re_enumerate
1915 * NOTE: After this function returns the hardware is in the
1916 * unconfigured state! The application is responsible for setting a
1917 * new configuration.
1922 *------------------------------------------------------------------------*/
1924 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1926 struct usb_device *parent_hub;
1929 uint8_t do_retry = 1;
1931 if (udev->flags.usb_mode != USB_MODE_HOST) {
1932 return (USB_ERR_INVAL);
1934 old_addr = udev->address;
1935 parent_hub = udev->parent_hub;
1936 if (parent_hub == NULL) {
1937 return (USB_ERR_INVAL);
1940 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1942 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1943 old_addr, usbd_errstr(err));
1948 * After that the port has been reset our device should be at
1951 udev->address = USB_START_ADDR;
1953 /* reset "bMaxPacketSize" */
1954 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1956 /* reset USB state */
1957 usb_set_device_state(udev, USB_STATE_POWERED);
1960 * Restore device address:
1962 err = usbd_req_set_address(udev, mtx, old_addr);
1964 /* XXX ignore any errors! */
1965 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1966 old_addr, usbd_errstr(err));
1969 * Restore device address, if the controller driver did not
1972 if (udev->address == USB_START_ADDR)
1973 udev->address = old_addr;
1975 /* setup the device descriptor and the initial "wMaxPacketSize" */
1976 err = usbd_setup_device_desc(udev, mtx);
1979 if (err && do_retry) {
1980 /* give the USB firmware some time to load */
1981 usb_pause_mtx(mtx, hz / 2);
1982 /* no more retries after this retry */
1987 /* restore address */
1988 if (udev->address == USB_START_ADDR)
1989 udev->address = old_addr;
1990 /* update state, if successful */
1992 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1996 /*------------------------------------------------------------------------*
1997 * usbd_req_clear_device_feature
2002 *------------------------------------------------------------------------*/
2004 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2007 struct usb_device_request req;
2009 req.bmRequestType = UT_WRITE_DEVICE;
2010 req.bRequest = UR_CLEAR_FEATURE;
2011 USETW(req.wValue, sel);
2012 USETW(req.wIndex, 0);
2013 USETW(req.wLength, 0);
2014 return (usbd_do_request(udev, mtx, &req, 0));
2017 /*------------------------------------------------------------------------*
2018 * usbd_req_set_device_feature
2023 *------------------------------------------------------------------------*/
2025 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2028 struct usb_device_request req;
2030 req.bmRequestType = UT_WRITE_DEVICE;
2031 req.bRequest = UR_SET_FEATURE;
2032 USETW(req.wValue, sel);
2033 USETW(req.wIndex, 0);
2034 USETW(req.wLength, 0);
2035 return (usbd_do_request(udev, mtx, &req, 0));