3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usb_ioctl.h>
52 #include <dev/usb/usbhid.h>
54 #define USB_DEBUG_VAR usb_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_request.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_transfer.h>
61 #include <dev/usb/usb_debug.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_util.h>
64 #include <dev/usb/usb_dynamic.h>
66 #include <dev/usb/usb_controller.h>
67 #include <dev/usb/usb_bus.h>
68 #include <sys/ctype.h>
71 static int usb_pr_poll_delay = USB_PORT_RESET_DELAY;
72 static int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
74 SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
75 &usb_pr_poll_delay, 0, "USB port reset poll delay in ms");
76 SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
77 &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
80 /* The following structures are used in connection to fault injection. */
81 struct usb_ctrl_debug {
82 int bus_index; /* target bus */
83 int dev_index; /* target address */
84 int ds_fail; /* fail data stage */
85 int ss_fail; /* fail data stage */
86 int ds_delay; /* data stage delay in ms */
87 int ss_delay; /* status stage delay in ms */
88 int bmRequestType_value;
92 struct usb_ctrl_debug_bits {
100 /* The default is to disable fault injection. */
102 static struct usb_ctrl_debug usb_ctrl_debug = {
105 .bmRequestType_value = -1,
106 .bRequest_value = -1,
109 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
110 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
111 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
112 &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
113 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
114 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
115 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
116 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
117 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
118 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
119 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
120 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
121 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
122 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
123 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
124 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
126 /*------------------------------------------------------------------------*
127 * usbd_get_debug_bits
129 * This function is only useful in USB host mode.
130 *------------------------------------------------------------------------*/
132 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
133 struct usb_ctrl_debug_bits *dbg)
137 memset(dbg, 0, sizeof(*dbg));
139 /* Compute data stage delay */
141 temp = usb_ctrl_debug.ds_delay;
144 else if (temp > (16*1024))
147 dbg->ds_delay = temp;
149 /* Compute status stage delay */
151 temp = usb_ctrl_debug.ss_delay;
154 else if (temp > (16*1024))
157 dbg->ss_delay = temp;
159 /* Check if this control request should be failed */
161 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
164 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
167 temp = usb_ctrl_debug.bmRequestType_value;
169 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
172 temp = usb_ctrl_debug.bRequest_value;
174 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
177 temp = usb_ctrl_debug.ds_fail;
181 temp = usb_ctrl_debug.ss_fail;
187 #endif /* USB_REQ_DEBUG */
188 #endif /* USB_DEBUG */
190 /*------------------------------------------------------------------------*
191 * usbd_do_request_callback
193 * This function is the USB callback for generic USB Host control
195 *------------------------------------------------------------------------*/
197 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
199 ; /* workaround for a bug in "indent" */
201 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
203 switch (USB_GET_STATE(xfer)) {
205 usbd_transfer_submit(xfer);
208 cv_signal(&xfer->xroot->udev->ctrlreq_cv);
213 /*------------------------------------------------------------------------*
214 * usb_do_clear_stall_callback
216 * This function is the USB callback for generic clear stall requests.
217 *------------------------------------------------------------------------*/
219 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
221 struct usb_device_request req;
222 struct usb_device *udev;
223 struct usb_endpoint *ep;
224 struct usb_endpoint *ep_end;
225 struct usb_endpoint *ep_first;
228 udev = xfer->xroot->udev;
230 USB_BUS_LOCK(udev->bus);
232 /* round robin endpoint clear stall */
235 ep_end = udev->endpoints + udev->endpoints_max;
236 ep_first = udev->endpoints;
237 to = udev->endpoints_max;
239 switch (USB_GET_STATE(xfer)) {
240 case USB_ST_TRANSFERRED:
242 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 (xfer->error == USB_ERR_CANCELLED) {
298 /* store current endpoint */
300 USB_BUS_UNLOCK(udev->bus);
303 static usb_handle_req_t *
304 usbd_get_hr_func(struct usb_device *udev)
306 /* figure out if there is a Handle Request function */
307 if (udev->flags.usb_mode == USB_MODE_DEVICE)
308 return (usb_temp_get_desc_p);
309 else if (udev->parent_hub == NULL)
310 return (udev->bus->methods->roothub_exec);
315 /*------------------------------------------------------------------------*
316 * usbd_do_request_flags and usbd_do_request
318 * Description of arguments passed to these functions:
320 * "udev" - this is the "usb_device" structure pointer on which the
321 * request should be performed. It is possible to call this function
322 * in both Host Side mode and Device Side mode.
324 * "mtx" - if this argument is non-NULL the mutex pointed to by it
325 * will get dropped and picked up during the execution of this
326 * function, hence this function sometimes needs to sleep. If this
327 * argument is NULL it has no effect.
329 * "req" - this argument must always be non-NULL and points to an
330 * 8-byte structure holding the USB request to be done. The USB
331 * request structure has a bit telling the direction of the USB
332 * request, if it is a read or a write.
334 * "data" - if the "wLength" part of the structure pointed to by "req"
335 * is non-zero this argument must point to a valid kernel buffer which
336 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
339 * "flags" - here is a list of valid flags:
341 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
344 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
345 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
346 * sysctl. This flag is mostly useful for debugging.
348 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
351 * "actlen" - if non-NULL the actual transfer length will be stored in
352 * the 16-bit unsigned integer pointed to by "actlen". This
353 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
356 * "timeout" - gives the timeout for the control transfer in
357 * milliseconds. A "timeout" value less than 50 milliseconds is
358 * treated like a 50 millisecond timeout. A "timeout" value greater
359 * than 30 seconds is treated like a 30 second timeout. This USB stack
360 * does not allow control requests without a timeout.
362 * NOTE: This function is thread safe. All calls to
363 * "usbd_do_request_flags" will be serialised by the use of an
364 * internal "sx_lock".
369 *------------------------------------------------------------------------*/
371 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
372 struct usb_device_request *req, void *data, uint16_t flags,
373 uint16_t *actlen, usb_timeout_t timeout)
376 struct usb_ctrl_debug_bits dbg;
378 usb_handle_req_t *hr_func;
379 struct usb_xfer *xfer;
382 usb_ticks_t start_ticks;
383 usb_ticks_t delta_ticks;
384 usb_ticks_t max_ticks;
391 /* timeout is too small */
394 if (timeout > 30000) {
395 /* timeout is too big */
398 length = UGETW(req->wLength);
400 enum_locked = usbd_enum_is_locked(udev);
402 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
403 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
404 udev, req->bmRequestType, req->bRequest,
405 req->wValue[1], req->wValue[0],
406 req->wIndex[1], req->wIndex[0],
407 req->wLength[1], req->wLength[0]);
409 /* Check if the device is still alive */
410 if (udev->state < USB_STATE_POWERED) {
411 DPRINTF("usb device has gone\n");
412 return (USB_ERR_NOT_CONFIGURED);
416 * Set "actlen" to a known value in case the caller does not
417 * check the return value:
422 #if (USB_HAVE_USER_IO == 0)
423 if (flags & USB_USER_DATA_PTR)
424 return (USB_ERR_INVAL);
426 if ((mtx != NULL) && (mtx != &Giant)) {
428 mtx_assert(mtx, MA_NOTOWNED);
432 * We need to allow suspend and resume at this point, else the
433 * control transfer will timeout if the device is suspended!
436 usbd_sr_unlock(udev);
439 * Grab the default sx-lock so that serialisation
440 * is achieved when multiple threads are involved:
442 sx_xlock(&udev->ctrl_sx);
444 hr_func = usbd_get_hr_func(udev);
446 if (hr_func != NULL) {
447 DPRINTF("Handle Request function is set\n");
452 if (!(req->bmRequestType & UT_READ)) {
454 DPRINTFN(1, "The handle request function "
455 "does not support writing data!\n");
461 /* The root HUB code needs the BUS lock locked */
463 USB_BUS_LOCK(udev->bus);
464 err = (hr_func) (udev, req, &desc, &temp);
465 USB_BUS_UNLOCK(udev->bus);
471 if (!(flags & USB_SHORT_XFER_OK)) {
472 err = USB_ERR_SHORT_XFER;
482 if (flags & USB_USER_DATA_PTR) {
483 if (copyout(desc, data, length)) {
489 bcopy(desc, data, length);
491 goto done; /* success */
495 * Setup a new USB transfer or use the existing one, if any:
497 usbd_ctrl_transfer_setup(udev);
499 xfer = udev->ctrl_xfer[0];
501 /* most likely out of memory */
508 usbd_get_debug_bits(udev, req, &dbg);
510 /* Check for fault injection */
512 flags |= USB_DELAY_STATUS_STAGE;
516 if (flags & USB_DELAY_STATUS_STAGE)
517 xfer->flags.manual_status = 1;
519 xfer->flags.manual_status = 0;
521 if (flags & USB_SHORT_XFER_OK)
522 xfer->flags.short_xfer_ok = 1;
524 xfer->flags.short_xfer_ok = 0;
526 xfer->timeout = timeout;
530 max_ticks = USB_MS_TO_TICKS(timeout);
532 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
534 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
538 if (temp > usbd_xfer_max_len(xfer)) {
539 temp = usbd_xfer_max_len(xfer);
542 if (xfer->flags.manual_status) {
543 if (usbd_xfer_frame_len(xfer, 0) != 0) {
544 /* Execute data stage separately */
546 } else if (temp > 0) {
551 if (dbg.ds_delay > 0) {
553 xfer->xroot->xfer_mtx,
554 USB_MS_TO_TICKS(dbg.ds_delay));
555 /* make sure we don't time out */
561 usbd_xfer_set_frame_len(xfer, 1, temp);
564 if (!(req->bmRequestType & UT_READ)) {
566 if (flags & USB_USER_DATA_PTR) {
567 USB_XFER_UNLOCK(xfer);
568 err = usbd_copy_in_user(xfer->frbuffers + 1,
577 usbd_copy_in(xfer->frbuffers + 1,
580 usbd_xfer_set_frames(xfer, 2);
582 if (usbd_xfer_frame_len(xfer, 0) == 0) {
583 if (xfer->flags.manual_status) {
589 if (dbg.ss_delay > 0) {
591 xfer->xroot->xfer_mtx,
592 USB_MS_TO_TICKS(dbg.ss_delay));
593 /* make sure we don't time out */
597 xfer->flags.manual_status = 0;
602 usbd_xfer_set_frames(xfer, 1);
605 usbd_transfer_start(xfer);
607 while (usbd_transfer_pending(xfer)) {
608 cv_wait(&udev->ctrlreq_cv,
609 xfer->xroot->xfer_mtx);
618 /* get actual length of DATA stage */
620 if (xfer->aframes < 2) {
623 acttemp = usbd_xfer_frame_len(xfer, 1);
626 /* check for short packet */
628 if (temp > acttemp) {
633 if (req->bmRequestType & UT_READ) {
635 if (flags & USB_USER_DATA_PTR) {
636 USB_XFER_UNLOCK(xfer);
637 err = usbd_copy_out_user(xfer->frbuffers + 1,
646 usbd_copy_out(xfer->frbuffers + 1,
651 * Clear "frlengths[0]" so that we don't send the setup
654 usbd_xfer_set_frame_len(xfer, 0, 0);
656 /* update length and data pointer */
658 data = USB_ADD_BYTES(data, temp);
663 /* check for timeout */
665 delta_ticks = ticks - start_ticks;
666 if (delta_ticks > max_ticks) {
668 err = USB_ERR_TIMEOUT;
678 * Make sure that the control endpoint is no longer
679 * blocked in case of a non-transfer related error:
681 usbd_transfer_stop(xfer);
683 USB_XFER_UNLOCK(xfer);
686 sx_xunlock(&udev->ctrl_sx);
691 if ((mtx != NULL) && (mtx != &Giant))
694 return ((usb_error_t)err);
697 /*------------------------------------------------------------------------*
698 * usbd_do_request_proc - factored out code
700 * This function is factored out code. It does basically the same like
701 * usbd_do_request_flags, except it will check the status of the
702 * passed process argument before doing the USB request. If the
703 * process is draining the USB_ERR_IOERROR code will be returned. It
704 * is assumed that the mutex associated with the process is locked
705 * when calling this function.
706 *------------------------------------------------------------------------*/
708 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
709 struct usb_device_request *req, void *data, uint16_t flags,
710 uint16_t *actlen, usb_timeout_t timeout)
715 /* get request data length */
716 len = UGETW(req->wLength);
718 /* check if the device is being detached */
719 if (usb_proc_is_gone(pproc)) {
720 err = USB_ERR_IOERROR;
724 /* forward the USB request */
725 err = usbd_do_request_flags(udev, pproc->up_mtx,
726 req, data, flags, actlen, timeout);
729 /* on failure we zero the data */
730 /* on short packet we zero the unused data */
731 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
733 memset(data, 0, len);
734 else if (actlen && *actlen != len)
735 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
740 /*------------------------------------------------------------------------*
741 * usbd_req_reset_port
743 * This function will instruct a USB HUB to perform a reset sequence
744 * on the specified port number.
747 * 0: Success. The USB device should now be at address zero.
748 * Else: Failure. No USB device is present and the USB port should be
750 *------------------------------------------------------------------------*/
752 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
754 struct usb_port_status ps;
759 uint16_t pr_poll_delay;
760 uint16_t pr_recovery_delay;
763 err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
768 /* range check input parameters */
769 pr_poll_delay = usb_pr_poll_delay;
770 if (pr_poll_delay < 1) {
772 } else if (pr_poll_delay > 1000) {
773 pr_poll_delay = 1000;
775 pr_recovery_delay = usb_pr_recovery_delay;
776 if (pr_recovery_delay > 1000) {
777 pr_recovery_delay = 1000;
783 /* wait for the device to recover from reset */
784 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
787 /* wait for the device to recover from reset */
788 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
789 n += USB_PORT_RESET_DELAY;
791 err = usbd_req_get_port_status(udev, mtx, &ps, port);
795 /* if the device disappeared, just give up */
796 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
799 /* check if reset is complete */
800 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
803 /* check for timeout */
810 /* clear port reset first */
811 err = usbd_req_clear_port_feature(
812 udev, mtx, port, UHF_C_PORT_RESET);
816 /* check for timeout */
818 err = USB_ERR_TIMEOUT;
822 /* wait for the device to recover from reset */
823 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
825 /* wait for the device to recover from reset */
826 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
830 DPRINTFN(2, "port %d reset returning error=%s\n",
831 port, usbd_errstr(err));
835 /*------------------------------------------------------------------------*
836 * usbd_req_warm_reset_port
838 * This function will instruct an USB HUB to perform a warm reset
839 * sequence on the specified port number. This kind of reset is not
840 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
841 * for SUPER-speed USB HUBs.
844 * 0: Success. The USB device should now be available again.
845 * Else: Failure. No USB device is present and the USB port should be
847 *------------------------------------------------------------------------*/
849 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
851 struct usb_port_status ps;
856 uint16_t pr_poll_delay;
857 uint16_t pr_recovery_delay;
860 err = usbd_req_set_port_feature(udev, mtx, port, UHF_BH_PORT_RESET);
865 /* range check input parameters */
866 pr_poll_delay = usb_pr_poll_delay;
867 if (pr_poll_delay < 1) {
869 } else if (pr_poll_delay > 1000) {
870 pr_poll_delay = 1000;
872 pr_recovery_delay = usb_pr_recovery_delay;
873 if (pr_recovery_delay > 1000) {
874 pr_recovery_delay = 1000;
880 /* wait for the device to recover from reset */
881 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
884 /* wait for the device to recover from reset */
885 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
886 n += USB_PORT_RESET_DELAY;
888 err = usbd_req_get_port_status(udev, mtx, &ps, port);
892 /* if the device disappeared, just give up */
893 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
896 /* check if reset is complete */
897 if (UGETW(ps.wPortChange) & UPS_C_BH_PORT_RESET) {
900 /* check for timeout */
907 /* clear port reset first */
908 err = usbd_req_clear_port_feature(
909 udev, mtx, port, UHF_C_BH_PORT_RESET);
913 /* check for timeout */
915 err = USB_ERR_TIMEOUT;
919 /* wait for the device to recover from reset */
920 usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
922 /* wait for the device to recover from reset */
923 usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
927 DPRINTFN(2, "port %d warm reset returning error=%s\n",
928 port, usbd_errstr(err));
932 /*------------------------------------------------------------------------*
935 * This function can be used to retrieve USB descriptors. It contains
936 * some additional logic like zeroing of missing descriptor bytes and
937 * retrying an USB descriptor in case of failure. The "min_len"
938 * argument specifies the minimum descriptor length. The "max_len"
939 * argument specifies the maximum descriptor length. If the real
940 * descriptor length is less than the minimum length the missing
941 * byte(s) will be zeroed. The type field, the second byte of the USB
942 * descriptor, will get forced to the correct type. If the "actlen"
943 * pointer is non-NULL, the actual length of the transfer will get
944 * stored in the 16-bit unsigned integer which it is pointing to. The
945 * first byte of the descriptor will not get updated. If the "actlen"
946 * pointer is NULL the first byte of the descriptor will get updated
947 * to reflect the actual length instead. If "min_len" is not equal to
948 * "max_len" then this function will try to retrive the beginning of
949 * the descriptor and base the maximum length on the first byte of the
955 *------------------------------------------------------------------------*/
957 usbd_req_get_desc(struct usb_device *udev,
958 struct mtx *mtx, uint16_t *actlen, void *desc,
959 uint16_t min_len, uint16_t max_len,
960 uint16_t id, uint8_t type, uint8_t index,
963 struct usb_device_request req;
967 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
968 id, type, index, max_len);
970 req.bmRequestType = UT_READ_DEVICE;
971 req.bRequest = UR_GET_DESCRIPTOR;
972 USETW2(req.wValue, type, index);
973 USETW(req.wIndex, id);
977 if ((min_len < 2) || (max_len < 2)) {
981 USETW(req.wLength, min_len);
983 err = usbd_do_request_flags(udev, mtx, &req,
984 desc, 0, NULL, 1000);
992 usb_pause_mtx(mtx, hz / 5);
998 if (min_len == max_len) {
1000 /* enforce correct length */
1001 if ((buf[0] > min_len) && (actlen == NULL))
1004 /* enforce correct type */
1011 if (max_len > buf[0]) {
1014 /* zero minimum data */
1016 while (min_len > max_len) {
1021 /* set new minimum length */
1026 if (actlen != NULL) {
1035 /*------------------------------------------------------------------------*
1036 * usbd_req_get_string_any
1038 * This function will return the string given by "string_index"
1039 * using the first language ID. The maximum length "len" includes
1040 * the terminating zero. The "len" argument should be twice as
1041 * big pluss 2 bytes, compared with the actual maximum string length !
1046 *------------------------------------------------------------------------*/
1048 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1049 uint16_t len, uint8_t string_index)
1060 /* should not happen */
1061 return (USB_ERR_NORMAL_COMPLETION);
1063 if (string_index == 0) {
1064 /* this is the language table */
1066 return (USB_ERR_INVAL);
1068 if (udev->flags.no_strings) {
1070 return (USB_ERR_STALLED);
1072 err = usbd_req_get_string_desc
1073 (udev, mtx, buf, len, udev->langid, string_index);
1078 temp = (uint8_t *)buf;
1081 /* string length is too short */
1083 return (USB_ERR_INVAL);
1085 /* reserve one byte for terminating zero */
1088 /* find maximum length */
1090 n = (temp[0] / 2) - 1;
1094 /* skip descriptor header */
1097 /* reset swap state */
1100 /* convert and filter */
1101 for (i = 0; (i != n); i++) {
1102 c = UGETW(temp + (2 * i));
1104 /* convert from Unicode, handle buggy strings */
1105 if (((c & 0xff00) == 0) && (swap & 1)) {
1106 /* Little Endian, default */
1109 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1114 /* silently skip bad character */
1119 * Filter by default - We only allow alphanumerical
1120 * and a few more to avoid any problems with scripts
1133 /* silently skip bad character */
1135 *s = 0; /* zero terminate resulting string */
1136 return (USB_ERR_NORMAL_COMPLETION);
1139 /*------------------------------------------------------------------------*
1140 * usbd_req_get_string_desc
1142 * If you don't know the language ID, consider using
1143 * "usbd_req_get_string_any()".
1148 *------------------------------------------------------------------------*/
1150 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1151 uint16_t max_len, uint16_t lang_id,
1152 uint8_t string_index)
1154 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1155 UDESC_STRING, string_index, 0));
1158 /*------------------------------------------------------------------------*
1159 * usbd_req_get_config_desc_ptr
1161 * This function is used in device side mode to retrieve the pointer
1162 * to the generated config descriptor. This saves allocating space for
1163 * an additional config descriptor when setting the configuration.
1168 *------------------------------------------------------------------------*/
1170 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1171 struct usb_config_descriptor **ppcd, uint16_t wValue)
1173 struct usb_device_request req;
1174 usb_handle_req_t *hr_func;
1179 req.bmRequestType = UT_READ_DEVICE;
1180 req.bRequest = UR_GET_DESCRIPTOR;
1181 USETW(req.wValue, wValue);
1182 USETW(req.wIndex, 0);
1183 USETW(req.wLength, 0);
1188 hr_func = usbd_get_hr_func(udev);
1190 if (hr_func == NULL)
1191 err = USB_ERR_INVAL;
1193 USB_BUS_LOCK(udev->bus);
1194 err = (hr_func) (udev, &req, &ptr, &len);
1195 USB_BUS_UNLOCK(udev->bus);
1200 else if (ptr == NULL)
1201 err = USB_ERR_INVAL;
1203 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1208 /*------------------------------------------------------------------------*
1209 * usbd_req_get_config_desc
1214 *------------------------------------------------------------------------*/
1216 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1217 struct usb_config_descriptor *d, uint8_t conf_index)
1221 DPRINTFN(4, "confidx=%d\n", conf_index);
1223 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1224 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1228 /* Extra sanity checking */
1229 if (UGETW(d->wTotalLength) < sizeof(*d)) {
1230 err = USB_ERR_INVAL;
1236 /*------------------------------------------------------------------------*
1237 * usbd_req_get_config_desc_full
1239 * This function gets the complete USB configuration descriptor and
1240 * ensures that "wTotalLength" is correct.
1245 *------------------------------------------------------------------------*/
1247 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1248 struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
1251 struct usb_config_descriptor cd;
1252 struct usb_config_descriptor *cdesc;
1256 DPRINTFN(4, "index=%d\n", index);
1260 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1264 /* get full descriptor */
1265 len = UGETW(cd.wTotalLength);
1266 if (len < sizeof(*cdesc)) {
1267 /* corrupt descriptor */
1268 return (USB_ERR_INVAL);
1270 cdesc = malloc(len, mtype, M_WAITOK);
1271 if (cdesc == NULL) {
1272 return (USB_ERR_NOMEM);
1274 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1275 UDESC_CONFIG, index, 3);
1280 /* make sure that the device is not fooling us: */
1281 USETW(cdesc->wTotalLength, len);
1285 return (0); /* success */
1288 /*------------------------------------------------------------------------*
1289 * usbd_req_get_device_desc
1294 *------------------------------------------------------------------------*/
1296 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1297 struct usb_device_descriptor *d)
1300 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1301 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1304 /*------------------------------------------------------------------------*
1305 * usbd_req_get_alt_interface_no
1310 *------------------------------------------------------------------------*/
1312 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1313 uint8_t *alt_iface_no, uint8_t iface_index)
1315 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1316 struct usb_device_request req;
1318 if ((iface == NULL) || (iface->idesc == NULL))
1319 return (USB_ERR_INVAL);
1321 req.bmRequestType = UT_READ_INTERFACE;
1322 req.bRequest = UR_GET_INTERFACE;
1323 USETW(req.wValue, 0);
1324 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1326 USETW(req.wLength, 1);
1327 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1330 /*------------------------------------------------------------------------*
1331 * usbd_req_set_alt_interface_no
1336 *------------------------------------------------------------------------*/
1338 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1339 uint8_t iface_index, uint8_t alt_no)
1341 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1342 struct usb_device_request req;
1344 if ((iface == NULL) || (iface->idesc == NULL))
1345 return (USB_ERR_INVAL);
1347 req.bmRequestType = UT_WRITE_INTERFACE;
1348 req.bRequest = UR_SET_INTERFACE;
1349 req.wValue[0] = alt_no;
1351 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1353 USETW(req.wLength, 0);
1354 return (usbd_do_request(udev, mtx, &req, 0));
1357 /*------------------------------------------------------------------------*
1358 * usbd_req_get_device_status
1363 *------------------------------------------------------------------------*/
1365 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1366 struct usb_status *st)
1368 struct usb_device_request req;
1370 req.bmRequestType = UT_READ_DEVICE;
1371 req.bRequest = UR_GET_STATUS;
1372 USETW(req.wValue, 0);
1373 USETW(req.wIndex, 0);
1374 USETW(req.wLength, sizeof(*st));
1375 return (usbd_do_request(udev, mtx, &req, st));
1378 /*------------------------------------------------------------------------*
1379 * usbd_req_get_hub_descriptor
1384 *------------------------------------------------------------------------*/
1386 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1387 struct usb_hub_descriptor *hd, uint8_t nports)
1389 struct usb_device_request req;
1390 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1392 req.bmRequestType = UT_READ_CLASS_DEVICE;
1393 req.bRequest = UR_GET_DESCRIPTOR;
1394 USETW2(req.wValue, UDESC_HUB, 0);
1395 USETW(req.wIndex, 0);
1396 USETW(req.wLength, len);
1397 return (usbd_do_request(udev, mtx, &req, hd));
1400 /*------------------------------------------------------------------------*
1401 * usbd_req_get_ss_hub_descriptor
1406 *------------------------------------------------------------------------*/
1408 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1409 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1411 struct usb_device_request req;
1412 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1414 req.bmRequestType = UT_READ_CLASS_DEVICE;
1415 req.bRequest = UR_GET_DESCRIPTOR;
1416 USETW2(req.wValue, UDESC_SS_HUB, 0);
1417 USETW(req.wIndex, 0);
1418 USETW(req.wLength, len);
1419 return (usbd_do_request(udev, mtx, &req, hd));
1422 /*------------------------------------------------------------------------*
1423 * usbd_req_get_hub_status
1428 *------------------------------------------------------------------------*/
1430 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1431 struct usb_hub_status *st)
1433 struct usb_device_request req;
1435 req.bmRequestType = UT_READ_CLASS_DEVICE;
1436 req.bRequest = UR_GET_STATUS;
1437 USETW(req.wValue, 0);
1438 USETW(req.wIndex, 0);
1439 USETW(req.wLength, sizeof(struct usb_hub_status));
1440 return (usbd_do_request(udev, mtx, &req, st));
1443 /*------------------------------------------------------------------------*
1444 * usbd_req_set_address
1446 * This function is used to set the address for an USB device. After
1447 * port reset the USB device will respond at address zero.
1452 *------------------------------------------------------------------------*/
1454 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1456 struct usb_device_request req;
1459 DPRINTFN(6, "setting device address=%d\n", addr);
1461 req.bmRequestType = UT_WRITE_DEVICE;
1462 req.bRequest = UR_SET_ADDRESS;
1463 USETW(req.wValue, addr);
1464 USETW(req.wIndex, 0);
1465 USETW(req.wLength, 0);
1467 err = USB_ERR_INVAL;
1469 /* check if USB controller handles set address */
1470 if (udev->bus->methods->set_address != NULL)
1471 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1473 if (err != USB_ERR_INVAL)
1476 /* Setting the address should not take more than 1 second ! */
1477 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1478 USB_DELAY_STATUS_STAGE, NULL, 1000);
1481 /* allow device time to set new address */
1483 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1488 /*------------------------------------------------------------------------*
1489 * usbd_req_get_port_status
1494 *------------------------------------------------------------------------*/
1496 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1497 struct usb_port_status *ps, uint8_t port)
1499 struct usb_device_request req;
1501 req.bmRequestType = UT_READ_CLASS_OTHER;
1502 req.bRequest = UR_GET_STATUS;
1503 USETW(req.wValue, 0);
1504 req.wIndex[0] = port;
1506 USETW(req.wLength, sizeof *ps);
1507 return (usbd_do_request(udev, mtx, &req, ps));
1510 /*------------------------------------------------------------------------*
1511 * usbd_req_clear_hub_feature
1516 *------------------------------------------------------------------------*/
1518 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1521 struct usb_device_request req;
1523 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1524 req.bRequest = UR_CLEAR_FEATURE;
1525 USETW(req.wValue, sel);
1526 USETW(req.wIndex, 0);
1527 USETW(req.wLength, 0);
1528 return (usbd_do_request(udev, mtx, &req, 0));
1531 /*------------------------------------------------------------------------*
1532 * usbd_req_set_hub_feature
1537 *------------------------------------------------------------------------*/
1539 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1542 struct usb_device_request req;
1544 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1545 req.bRequest = UR_SET_FEATURE;
1546 USETW(req.wValue, sel);
1547 USETW(req.wIndex, 0);
1548 USETW(req.wLength, 0);
1549 return (usbd_do_request(udev, mtx, &req, 0));
1552 /*------------------------------------------------------------------------*
1553 * usbd_req_set_hub_u1_timeout
1558 *------------------------------------------------------------------------*/
1560 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1561 uint8_t port, uint8_t timeout)
1563 struct usb_device_request req;
1565 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1566 req.bRequest = UR_SET_FEATURE;
1567 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1568 req.wIndex[0] = port;
1569 req.wIndex[1] = timeout;
1570 USETW(req.wLength, 0);
1571 return (usbd_do_request(udev, mtx, &req, 0));
1574 /*------------------------------------------------------------------------*
1575 * usbd_req_set_hub_u2_timeout
1580 *------------------------------------------------------------------------*/
1582 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1583 uint8_t port, uint8_t timeout)
1585 struct usb_device_request req;
1587 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1588 req.bRequest = UR_SET_FEATURE;
1589 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1590 req.wIndex[0] = port;
1591 req.wIndex[1] = timeout;
1592 USETW(req.wLength, 0);
1593 return (usbd_do_request(udev, mtx, &req, 0));
1596 /*------------------------------------------------------------------------*
1597 * usbd_req_set_hub_depth
1602 *------------------------------------------------------------------------*/
1604 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1607 struct usb_device_request req;
1609 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1610 req.bRequest = UR_SET_HUB_DEPTH;
1611 USETW(req.wValue, depth);
1612 USETW(req.wIndex, 0);
1613 USETW(req.wLength, 0);
1614 return (usbd_do_request(udev, mtx, &req, 0));
1617 /*------------------------------------------------------------------------*
1618 * usbd_req_clear_port_feature
1623 *------------------------------------------------------------------------*/
1625 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1626 uint8_t port, uint16_t sel)
1628 struct usb_device_request req;
1630 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1631 req.bRequest = UR_CLEAR_FEATURE;
1632 USETW(req.wValue, sel);
1633 req.wIndex[0] = port;
1635 USETW(req.wLength, 0);
1636 return (usbd_do_request(udev, mtx, &req, 0));
1639 /*------------------------------------------------------------------------*
1640 * usbd_req_set_port_feature
1645 *------------------------------------------------------------------------*/
1647 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1648 uint8_t port, uint16_t sel)
1650 struct usb_device_request req;
1652 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1653 req.bRequest = UR_SET_FEATURE;
1654 USETW(req.wValue, sel);
1655 req.wIndex[0] = port;
1657 USETW(req.wLength, 0);
1658 return (usbd_do_request(udev, mtx, &req, 0));
1661 /*------------------------------------------------------------------------*
1662 * usbd_req_set_protocol
1667 *------------------------------------------------------------------------*/
1669 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1670 uint8_t iface_index, uint16_t report)
1672 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1673 struct usb_device_request req;
1675 if ((iface == NULL) || (iface->idesc == NULL)) {
1676 return (USB_ERR_INVAL);
1678 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1679 iface, report, iface->idesc->bInterfaceNumber);
1681 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1682 req.bRequest = UR_SET_PROTOCOL;
1683 USETW(req.wValue, report);
1684 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1686 USETW(req.wLength, 0);
1687 return (usbd_do_request(udev, mtx, &req, 0));
1690 /*------------------------------------------------------------------------*
1691 * usbd_req_set_report
1696 *------------------------------------------------------------------------*/
1698 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1699 uint8_t iface_index, uint8_t type, uint8_t id)
1701 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1702 struct usb_device_request req;
1704 if ((iface == NULL) || (iface->idesc == NULL)) {
1705 return (USB_ERR_INVAL);
1707 DPRINTFN(5, "len=%d\n", len);
1709 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1710 req.bRequest = UR_SET_REPORT;
1711 USETW2(req.wValue, type, id);
1712 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1714 USETW(req.wLength, len);
1715 return (usbd_do_request(udev, mtx, &req, data));
1718 /*------------------------------------------------------------------------*
1719 * usbd_req_get_report
1724 *------------------------------------------------------------------------*/
1726 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1727 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1729 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1730 struct usb_device_request req;
1732 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1733 return (USB_ERR_INVAL);
1735 DPRINTFN(5, "len=%d\n", len);
1737 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1738 req.bRequest = UR_GET_REPORT;
1739 USETW2(req.wValue, type, id);
1740 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1742 USETW(req.wLength, len);
1743 return (usbd_do_request(udev, mtx, &req, data));
1746 /*------------------------------------------------------------------------*
1752 *------------------------------------------------------------------------*/
1754 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1755 uint8_t iface_index, uint8_t duration, uint8_t id)
1757 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1758 struct usb_device_request req;
1760 if ((iface == NULL) || (iface->idesc == NULL)) {
1761 return (USB_ERR_INVAL);
1763 DPRINTFN(5, "%d %d\n", duration, id);
1765 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1766 req.bRequest = UR_SET_IDLE;
1767 USETW2(req.wValue, duration, id);
1768 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1770 USETW(req.wLength, 0);
1771 return (usbd_do_request(udev, mtx, &req, 0));
1774 /*------------------------------------------------------------------------*
1775 * usbd_req_get_report_descriptor
1780 *------------------------------------------------------------------------*/
1782 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1783 void *d, uint16_t size, uint8_t iface_index)
1785 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1786 struct usb_device_request req;
1788 if ((iface == NULL) || (iface->idesc == NULL)) {
1789 return (USB_ERR_INVAL);
1791 req.bmRequestType = UT_READ_INTERFACE;
1792 req.bRequest = UR_GET_DESCRIPTOR;
1793 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1794 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1796 USETW(req.wLength, size);
1797 return (usbd_do_request(udev, mtx, &req, d));
1800 /*------------------------------------------------------------------------*
1801 * usbd_req_set_config
1803 * This function is used to select the current configuration number in
1804 * both USB device side mode and USB host side mode. When setting the
1805 * configuration the function of the interfaces can change.
1810 *------------------------------------------------------------------------*/
1812 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1814 struct usb_device_request req;
1816 DPRINTF("setting config %d\n", conf);
1818 /* do "set configuration" request */
1820 req.bmRequestType = UT_WRITE_DEVICE;
1821 req.bRequest = UR_SET_CONFIG;
1822 req.wValue[0] = conf;
1824 USETW(req.wIndex, 0);
1825 USETW(req.wLength, 0);
1826 return (usbd_do_request(udev, mtx, &req, 0));
1829 /*------------------------------------------------------------------------*
1830 * usbd_req_get_config
1835 *------------------------------------------------------------------------*/
1837 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1839 struct usb_device_request req;
1841 req.bmRequestType = UT_READ_DEVICE;
1842 req.bRequest = UR_GET_CONFIG;
1843 USETW(req.wValue, 0);
1844 USETW(req.wIndex, 0);
1845 USETW(req.wLength, 1);
1846 return (usbd_do_request(udev, mtx, &req, pconf));
1849 /*------------------------------------------------------------------------*
1850 * usbd_setup_device_desc
1851 *------------------------------------------------------------------------*/
1853 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1858 * Get the first 8 bytes of the device descriptor !
1860 * NOTE: "usbd_do_request()" will check the device descriptor
1861 * next time we do a request to see if the maximum packet size
1862 * changed! The 8 first bytes of the device descriptor
1863 * contains the maximum packet size to use on control endpoint
1864 * 0. If this value is different from "USB_MAX_IPACKET" a new
1865 * USB control request will be setup!
1867 switch (udev->speed) {
1868 case USB_SPEED_FULL:
1870 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1871 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1873 DPRINTFN(0, "getting device descriptor "
1874 "at addr %d failed, %s\n", udev->address,
1880 DPRINTF("Minimum MaxPacketSize is large enough "
1881 "to hold the complete device descriptor\n");
1885 /* get the full device descriptor */
1886 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1888 /* try one more time, if error */
1890 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1893 DPRINTF("addr=%d, getting full desc failed\n",
1898 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1899 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1900 udev->address, UGETW(udev->ddesc.bcdUSB),
1901 udev->ddesc.bDeviceClass,
1902 udev->ddesc.bDeviceSubClass,
1903 udev->ddesc.bDeviceProtocol,
1904 udev->ddesc.bMaxPacketSize,
1905 udev->ddesc.bLength,
1911 /*------------------------------------------------------------------------*
1912 * usbd_req_re_enumerate
1914 * NOTE: After this function returns the hardware is in the
1915 * unconfigured state! The application is responsible for setting a
1916 * new configuration.
1921 *------------------------------------------------------------------------*/
1923 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1925 struct usb_device *parent_hub;
1928 uint8_t do_retry = 1;
1930 if (udev->flags.usb_mode != USB_MODE_HOST) {
1931 return (USB_ERR_INVAL);
1933 old_addr = udev->address;
1934 parent_hub = udev->parent_hub;
1935 if (parent_hub == NULL) {
1936 return (USB_ERR_INVAL);
1939 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
1941 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
1942 old_addr, usbd_errstr(err));
1947 * After that the port has been reset our device should be at
1950 udev->address = USB_START_ADDR;
1952 /* reset "bMaxPacketSize" */
1953 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1955 /* reset USB state */
1956 usb_set_device_state(udev, USB_STATE_POWERED);
1959 * Restore device address:
1961 err = usbd_req_set_address(udev, mtx, old_addr);
1963 /* XXX ignore any errors! */
1964 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
1965 old_addr, usbd_errstr(err));
1968 * Restore device address, if the controller driver did not
1971 if (udev->address == USB_START_ADDR)
1972 udev->address = old_addr;
1974 /* setup the device descriptor and the initial "wMaxPacketSize" */
1975 err = usbd_setup_device_desc(udev, mtx);
1978 if (err && do_retry) {
1979 /* give the USB firmware some time to load */
1980 usb_pause_mtx(mtx, hz / 2);
1981 /* no more retries after this retry */
1986 /* restore address */
1987 if (udev->address == USB_START_ADDR)
1988 udev->address = old_addr;
1989 /* update state, if successful */
1991 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1995 /*------------------------------------------------------------------------*
1996 * usbd_req_clear_device_feature
2001 *------------------------------------------------------------------------*/
2003 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2006 struct usb_device_request req;
2008 req.bmRequestType = UT_WRITE_DEVICE;
2009 req.bRequest = UR_CLEAR_FEATURE;
2010 USETW(req.wValue, sel);
2011 USETW(req.wIndex, 0);
2012 USETW(req.wLength, 0);
2013 return (usbd_do_request(udev, mtx, &req, 0));
2016 /*------------------------------------------------------------------------*
2017 * usbd_req_set_device_feature
2022 *------------------------------------------------------------------------*/
2024 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2027 struct usb_device_request req;
2029 req.bmRequestType = UT_WRITE_DEVICE;
2030 req.bRequest = UR_SET_FEATURE;
2031 USETW(req.wValue, sel);
2032 USETW(req.wIndex, 0);
2033 USETW(req.wLength, 0);
2034 return (usbd_do_request(udev, mtx, &req, 0));