1 /* $FreeBSD: head/sys/dev/usb/usb_request.c 276701 2015-01-05 15:04:17Z hselasky $ */
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/stdint.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <bus/u4b/usb.h>
47 #include <bus/u4b/usbdi.h>
48 #include <bus/u4b/usbdi_util.h>
49 #include <bus/u4b/usb_ioctl.h>
50 #include <bus/u4b/usbhid.h>
52 #define USB_DEBUG_VAR usb_debug
54 #include <bus/u4b/usb_core.h>
55 #include <bus/u4b/usb_busdma.h>
56 #include <bus/u4b/usb_request.h>
57 #include <bus/u4b/usb_process.h>
58 #include <bus/u4b/usb_transfer.h>
59 #include <bus/u4b/usb_debug.h>
60 #include <bus/u4b/usb_device.h>
61 #include <bus/u4b/usb_util.h>
62 #include <bus/u4b/usb_dynamic.h>
64 #include <bus/u4b/usb_controller.h>
65 #include <bus/u4b/usb_bus.h>
66 #include <sys/ctype.h>
68 static int usb_no_cs_fail;
70 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
71 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
73 static int usb_full_ddesc;
75 SYSCTL_INT(_hw_usb, OID_AUTO, full_ddesc, CTLFLAG_RW,
76 &usb_full_ddesc, 0, "USB always read complete device descriptor, if set");
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 status 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);
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;
229 udev = xfer->xroot->udev;
231 USB_BUS_LOCK(udev->bus);
233 /* round robin endpoint clear stall */
236 ep_end = udev->endpoints + udev->endpoints_max;
237 ep_first = udev->endpoints;
238 to = udev->endpoints_max;
240 switch (USB_GET_STATE(xfer)) {
241 case USB_ST_TRANSFERRED:
243 /* reset error counter */
244 udev->clear_stall_errors = 0;
247 goto tr_setup; /* device was unconfigured */
252 /* some hardware needs a callback to clear the data toggle */
253 usbd_clear_stall_locked(udev, ep);
254 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
255 /* start the current or next transfer, if any */
256 usb_command_wrapper(&ep->endpoint_q[x],
257 ep->endpoint_q[x].curr);
265 break; /* no endpoints - nothing to do */
266 if ((ep < ep_first) || (ep >= ep_end))
267 ep = ep_first; /* endpoint wrapped around */
271 /* setup a clear-stall packet */
273 req.bmRequestType = UT_WRITE_ENDPOINT;
274 req.bRequest = UR_CLEAR_FEATURE;
275 USETW(req.wValue, UF_ENDPOINT_HALT);
276 req.wIndex[0] = ep->edesc->bEndpointAddress;
278 USETW(req.wLength, 0);
280 /* copy in the transfer */
282 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
285 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
287 USB_BUS_UNLOCK(udev->bus);
289 usbd_transfer_submit(xfer);
291 USB_BUS_LOCK(udev->bus);
299 if (error == USB_ERR_CANCELLED)
302 DPRINTF("Clear stall failed.\n");
305 * Some VMs like VirtualBox always return failure on
306 * clear-stall which we sometimes should just ignore.
310 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
313 if (error == USB_ERR_TIMEOUT) {
314 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
315 DPRINTF("Trying to re-enumerate.\n");
316 usbd_start_re_enumerate(udev);
318 udev->clear_stall_errors++;
319 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
320 DPRINTF("Trying to re-enumerate.\n");
321 usbd_start_re_enumerate(udev);
327 /* store current endpoint */
329 USB_BUS_UNLOCK(udev->bus);
332 static usb_handle_req_t *
333 usbd_get_hr_func(struct usb_device *udev)
335 /* figure out if there is a Handle Request function */
336 if (udev->flags.usb_mode == USB_MODE_DEVICE)
337 return (usb_temp_get_desc_p);
338 else if (udev->parent_hub == NULL)
339 return (udev->bus->methods->roothub_exec);
344 /*------------------------------------------------------------------------*
345 * usbd_do_request_flags and usbd_do_request
347 * Description of arguments passed to these functions:
349 * "udev" - this is the "usb_device" structure pointer on which the
350 * request should be performed. It is possible to call this function
351 * in both Host Side mode and Device Side mode.
353 * "mtx" - if this argument is non-NULL the mutex pointed to by it
354 * will get dropped and picked up during the execution of this
355 * function, hence this function sometimes needs to sleep. If this
356 * argument is NULL it has no effect.
358 * "req" - this argument must always be non-NULL and points to an
359 * 8-byte structure holding the USB request to be done. The USB
360 * request structure has a bit telling the direction of the USB
361 * request, if it is a read or a write.
363 * "data" - if the "wLength" part of the structure pointed to by "req"
364 * is non-zero this argument must point to a valid kernel buffer which
365 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
368 * "flags" - here is a list of valid flags:
370 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
373 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
374 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
375 * sysctl. This flag is mostly useful for debugging.
377 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
380 * "actlen" - if non-NULL the actual transfer length will be stored in
381 * the 16-bit unsigned integer pointed to by "actlen". This
382 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
385 * "timeout" - gives the timeout for the control transfer in
386 * milliseconds. A "timeout" value less than 50 milliseconds is
387 * treated like a 50 millisecond timeout. A "timeout" value greater
388 * than 30 seconds is treated like a 30 second timeout. This USB stack
389 * does not allow control requests without a timeout.
391 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
392 * will be serialized by the use of the USB device enumeration lock.
397 *------------------------------------------------------------------------*/
399 usbd_do_request_flags(struct usb_device *udev, struct lock *lock,
400 struct usb_device_request *req, void *data, uint16_t flags,
401 uint16_t *actlen, usb_timeout_t timeout)
404 struct usb_ctrl_debug_bits dbg;
406 usb_handle_req_t *hr_func;
407 struct usb_xfer *xfer;
410 usb_ticks_t start_ticks;
411 usb_ticks_t delta_ticks;
412 usb_ticks_t max_ticks;
419 /* timeout is too small */
422 if (timeout > 30000) {
423 /* timeout is too big */
426 length = UGETW(req->wLength);
428 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
429 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
430 udev, req->bmRequestType, req->bRequest,
431 req->wValue[1], req->wValue[0],
432 req->wIndex[1], req->wIndex[0],
433 req->wLength[1], req->wLength[0]);
435 /* Check if the device is still alive */
436 if (udev->state < USB_STATE_POWERED) {
437 DPRINTF("usb device has gone\n");
438 return (USB_ERR_NOT_CONFIGURED);
442 * Set "actlen" to a known value in case the caller does not
443 * check the return value:
448 #if (USB_HAVE_USER_IO == 0)
449 if (flags & USB_USER_DATA_PTR)
450 return (USB_ERR_INVAL);
453 if ((mtx != NULL) && (mtx != &Giant)) {
456 lockmgr(lock, LK_RELEASE);
457 KKASSERT(!lockowned(lock));
461 * Grab the USB device enumeration SX-lock serialization is
462 * achieved when multiple threads are involved:
464 do_unlock = usbd_enum_lock(udev);
467 * We need to allow suspend and resume at this point, else the
468 * control transfer will timeout if the device is suspended!
470 usbd_sr_unlock(udev);
472 hr_func = usbd_get_hr_func(udev);
474 if (hr_func != NULL) {
475 DPRINTF("Handle Request function is set\n");
480 if (!(req->bmRequestType & UT_READ)) {
482 DPRINTFN(1, "The handle request function "
483 "does not support writing data!\n");
489 /* The root HUB code needs the BUS lock locked */
491 USB_BUS_LOCK(udev->bus);
492 err = (hr_func) (udev, req, &desc, &temp);
493 USB_BUS_UNLOCK(udev->bus);
499 if (!(flags & USB_SHORT_XFER_OK)) {
500 err = USB_ERR_SHORT_XFER;
510 if (flags & USB_USER_DATA_PTR) {
511 if (copyout(desc, data, length)) {
517 memcpy(data, desc, length);
519 goto done; /* success */
523 * Setup a new USB transfer or use the existing one, if any:
525 usbd_ctrl_transfer_setup(udev);
527 xfer = udev->ctrl_xfer[0];
529 /* most likely out of memory */
536 usbd_get_debug_bits(udev, req, &dbg);
538 /* Check for fault injection */
540 flags |= USB_DELAY_STATUS_STAGE;
544 if (flags & USB_DELAY_STATUS_STAGE)
545 xfer->flags.manual_status = 1;
547 xfer->flags.manual_status = 0;
549 if (flags & USB_SHORT_XFER_OK)
550 xfer->flags.short_xfer_ok = 1;
552 xfer->flags.short_xfer_ok = 0;
554 xfer->timeout = timeout;
558 max_ticks = USB_MS_TO_TICKS(timeout);
560 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
562 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
566 if (temp > usbd_xfer_max_len(xfer)) {
567 temp = usbd_xfer_max_len(xfer);
570 if (xfer->flags.manual_status) {
571 if (usbd_xfer_frame_len(xfer, 0) != 0) {
572 /* Execute data stage separately */
574 } else if (temp > 0) {
579 if (dbg.ds_delay > 0) {
581 xfer->xroot->xfer_lock,
582 USB_MS_TO_TICKS(dbg.ds_delay));
583 /* make sure we don't time out */
589 usbd_xfer_set_frame_len(xfer, 1, temp);
592 if (!(req->bmRequestType & UT_READ)) {
594 if (flags & USB_USER_DATA_PTR) {
595 USB_XFER_UNLOCK(xfer);
596 err = usbd_copy_in_user(xfer->frbuffers + 1,
605 usbd_copy_in(xfer->frbuffers + 1,
608 usbd_xfer_set_frames(xfer, 2);
610 if (usbd_xfer_frame_len(xfer, 0) == 0) {
611 if (xfer->flags.manual_status) {
617 if (dbg.ss_delay > 0) {
619 xfer->xroot->xfer_lock,
620 USB_MS_TO_TICKS(dbg.ss_delay));
621 /* make sure we don't time out */
625 xfer->flags.manual_status = 0;
630 usbd_xfer_set_frames(xfer, 1);
633 usbd_transfer_start(xfer);
636 * XXX hack, the wakeup of xfer can race conditions which
637 * clear the pending status of the xfer.
639 while (usbd_transfer_pending(xfer)) {
640 lksleep(xfer, xfer->xroot->xfer_lock, 0, "WXFER", hz);
649 /* get actual length of DATA stage */
651 if (xfer->aframes < 2) {
654 acttemp = usbd_xfer_frame_len(xfer, 1);
657 /* check for short packet */
659 if (temp > acttemp) {
664 if (req->bmRequestType & UT_READ) {
666 if (flags & USB_USER_DATA_PTR) {
667 USB_XFER_UNLOCK(xfer);
668 err = usbd_copy_out_user(xfer->frbuffers + 1,
677 usbd_copy_out(xfer->frbuffers + 1,
682 * Clear "frlengths[0]" so that we don't send the setup
685 usbd_xfer_set_frame_len(xfer, 0, 0);
687 /* update length and data pointer */
689 data = USB_ADD_BYTES(data, temp);
694 /* check for timeout */
696 delta_ticks = ticks - start_ticks;
697 if (delta_ticks > max_ticks) {
699 err = USB_ERR_TIMEOUT;
709 * Make sure that the control endpoint is no longer
710 * blocked in case of a non-transfer related error:
712 usbd_transfer_stop(xfer);
714 USB_XFER_UNLOCK(xfer);
720 usbd_enum_unlock(udev);
723 if ((mtx != NULL) && (mtx != &Giant))
726 lockmgr(lock, LK_EXCLUSIVE);
729 case USB_ERR_NORMAL_COMPLETION:
730 case USB_ERR_SHORT_XFER:
731 case USB_ERR_STALLED:
732 case USB_ERR_CANCELLED:
735 DPRINTF("I/O error - waiting a bit for TT cleanup\n");
736 usb_pause_mtx(lock, hz / 16);
739 return ((usb_error_t)err);
742 /*------------------------------------------------------------------------*
743 * usbd_do_request_proc - factored out code
745 * This function is factored out code. It does basically the same like
746 * usbd_do_request_flags, except it will check the status of the
747 * passed process argument before doing the USB request. If the
748 * process is draining the USB_ERR_IOERROR code will be returned. It
749 * is assumed that the mutex associated with the process is locked
750 * when calling this function.
751 *------------------------------------------------------------------------*/
753 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
754 struct usb_device_request *req, void *data, uint16_t flags,
755 uint16_t *actlen, usb_timeout_t timeout)
760 /* get request data length */
761 len = UGETW(req->wLength);
763 /* check if the device is being detached */
764 if (usb_proc_is_gone(pproc)) {
765 err = USB_ERR_IOERROR;
769 /* forward the USB request */
770 err = usbd_do_request_flags(udev, pproc->up_lock,
771 req, data, flags, actlen, timeout);
774 /* on failure we zero the data */
775 /* on short packet we zero the unused data */
776 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
778 memset(data, 0, len);
779 else if (actlen && *actlen != len)
780 memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
785 /*------------------------------------------------------------------------*
786 * usbd_req_reset_port
788 * This function will instruct a USB HUB to perform a reset sequence
789 * on the specified port number.
792 * 0: Success. The USB device should now be at address zero.
793 * Else: Failure. No USB device is present and the USB port should be
795 *------------------------------------------------------------------------*/
797 usbd_req_reset_port(struct usb_device *udev, struct lock *lock, uint8_t port)
799 struct usb_port_status ps;
807 /* clear any leftover port reset changes first */
808 usbd_req_clear_port_feature(
809 udev, lock, port, UHF_C_PORT_RESET);
811 /* assert port reset on the given port */
812 err = usbd_req_set_port_feature(
813 udev, lock, port, UHF_PORT_RESET);
815 /* check for errors */
820 /* wait for the device to recover from reset */
821 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_delay));
822 n += usb_port_reset_delay;
823 err = usbd_req_get_port_status(udev, lock, &ps, port);
827 status = UGETW(ps.wPortStatus);
828 change = UGETW(ps.wPortChange);
830 /* if the device disappeared, just give up */
831 if (!(status & UPS_CURRENT_CONNECT_STATUS))
834 /* check if reset is complete */
835 if (change & UPS_C_PORT_RESET)
839 * Some Virtual Machines like VirtualBox 4.x fail to
840 * generate a port reset change event. Check if reset
841 * is no longer asserted.
843 if (!(status & UPS_RESET))
846 /* check for timeout */
853 /* clear port reset first */
854 err = usbd_req_clear_port_feature(
855 udev, lock, port, UHF_C_PORT_RESET);
859 /* check for timeout */
861 err = USB_ERR_TIMEOUT;
864 /* wait for the device to recover from reset */
865 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_recovery));
868 DPRINTFN(2, "port %d reset returning error=%s\n",
869 port, usbd_errstr(err));
873 /*------------------------------------------------------------------------*
874 * usbd_req_warm_reset_port
876 * This function will instruct an USB HUB to perform a warm reset
877 * sequence on the specified port number. This kind of reset is not
878 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
879 * for SUPER-speed USB HUBs.
882 * 0: Success. The USB device should now be available again.
883 * Else: Failure. No USB device is present and the USB port should be
885 *------------------------------------------------------------------------*/
887 usbd_req_warm_reset_port(struct usb_device *udev, struct lock *lock,
890 struct usb_port_status ps;
898 err = usbd_req_get_port_status(udev, lock, &ps, port);
902 status = UGETW(ps.wPortStatus);
904 switch (UPS_PORT_LINK_STATE_GET(status)) {
906 case UPS_PORT_LS_COMP_MODE:
907 case UPS_PORT_LS_LOOPBACK:
908 case UPS_PORT_LS_SS_INA:
911 DPRINTF("Wrong state for warm reset\n");
915 /* clear any leftover warm port reset changes first */
916 usbd_req_clear_port_feature(udev, lock,
917 port, UHF_C_BH_PORT_RESET);
919 /* set warm port reset */
920 err = usbd_req_set_port_feature(udev, lock,
921 port, UHF_BH_PORT_RESET);
927 /* wait for the device to recover from reset */
928 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_delay));
929 n += usb_port_reset_delay;
930 err = usbd_req_get_port_status(udev, lock, &ps, port);
934 status = UGETW(ps.wPortStatus);
935 change = UGETW(ps.wPortChange);
937 /* if the device disappeared, just give up */
938 if (!(status & UPS_CURRENT_CONNECT_STATUS))
941 /* check if reset is complete */
942 if (change & UPS_C_BH_PORT_RESET)
945 /* check for timeout */
952 /* clear port reset first */
953 err = usbd_req_clear_port_feature(
954 udev, lock, port, UHF_C_BH_PORT_RESET);
958 /* check for timeout */
960 err = USB_ERR_TIMEOUT;
963 /* wait for the device to recover from reset */
964 usb_pause_mtx(lock, USB_MS_TO_TICKS(usb_port_reset_recovery));
967 DPRINTFN(2, "port %d warm reset returning error=%s\n",
968 port, usbd_errstr(err));
972 /*------------------------------------------------------------------------*
975 * This function can be used to retrieve USB descriptors. It contains
976 * some additional logic like zeroing of missing descriptor bytes and
977 * retrying an USB descriptor in case of failure. The "min_len"
978 * argument specifies the minimum descriptor length. The "max_len"
979 * argument specifies the maximum descriptor length. If the real
980 * descriptor length is less than the minimum length the missing
981 * byte(s) will be zeroed. The type field, the second byte of the USB
982 * descriptor, will get forced to the correct type. If the "actlen"
983 * pointer is non-NULL, the actual length of the transfer will get
984 * stored in the 16-bit unsigned integer which it is pointing to. The
985 * first byte of the descriptor will not get updated. If the "actlen"
986 * pointer is NULL the first byte of the descriptor will get updated
987 * to reflect the actual length instead. If "min_len" is not equal to
988 * "max_len" then this function will try to retrive the beginning of
989 * the descriptor and base the maximum length on the first byte of the
995 *------------------------------------------------------------------------*/
997 usbd_req_get_desc(struct usb_device *udev,
998 struct lock *lock, uint16_t *actlen, void *desc,
999 uint16_t min_len, uint16_t max_len,
1000 uint16_t id, uint8_t type, uint8_t index,
1003 struct usb_device_request req;
1007 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
1008 id, type, index, max_len);
1010 req.bmRequestType = UT_READ_DEVICE;
1011 req.bRequest = UR_GET_DESCRIPTOR;
1012 USETW2(req.wValue, type, index);
1013 USETW(req.wIndex, id);
1017 if ((min_len < 2) || (max_len < 2)) {
1018 err = USB_ERR_INVAL;
1021 USETW(req.wLength, min_len);
1023 err = usbd_do_request_flags(udev, lock, &req,
1024 desc, 0, NULL, 1000 /* ms */);
1032 usb_pause_mtx(lock, hz / 5);
1038 if (min_len == max_len) {
1040 /* enforce correct length */
1041 if ((buf[0] > min_len) && (actlen == NULL))
1044 /* enforce correct type */
1051 if (max_len > buf[0]) {
1054 /* zero minimum data */
1056 while (min_len > max_len) {
1061 /* set new minimum length */
1066 if (actlen != NULL) {
1075 /*------------------------------------------------------------------------*
1076 * usbd_req_get_string_any
1078 * This function will return the string given by "string_index"
1079 * using the first language ID. The maximum length "len" includes
1080 * the terminating zero. The "len" argument should be twice as
1081 * big pluss 2 bytes, compared with the actual maximum string length !
1086 *------------------------------------------------------------------------*/
1088 usbd_req_get_string_any(struct usb_device *udev, struct lock *lock, char *buf,
1089 uint16_t len, uint8_t string_index)
1100 /* should not happen */
1101 return (USB_ERR_NORMAL_COMPLETION);
1103 if (string_index == 0) {
1104 /* this is the language table */
1106 return (USB_ERR_INVAL);
1108 if (udev->flags.no_strings) {
1110 return (USB_ERR_STALLED);
1112 err = usbd_req_get_string_desc
1113 (udev, lock, buf, len, udev->langid, string_index);
1118 temp = (uint8_t *)buf;
1121 /* string length is too short */
1123 return (USB_ERR_INVAL);
1125 /* reserve one byte for terminating zero */
1128 /* find maximum length */
1130 n = (temp[0] / 2) - 1;
1134 /* skip descriptor header */
1137 /* reset swap state */
1140 /* convert and filter */
1141 for (i = 0; (i != n); i++) {
1142 c = UGETW(temp + (2 * i));
1144 /* convert from Unicode, handle buggy strings */
1145 if (((c & 0xff00) == 0) && (swap & 1)) {
1146 /* Little Endian, default */
1149 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1154 /* silently skip bad character */
1159 * Filter by default - We only allow alphanumerical
1160 * and a few more to avoid any problems with scripts
1173 /* silently skip bad character */
1175 *s = 0; /* zero terminate resulting string */
1176 return (USB_ERR_NORMAL_COMPLETION);
1179 /*------------------------------------------------------------------------*
1180 * usbd_req_get_string_desc
1182 * If you don't know the language ID, consider using
1183 * "usbd_req_get_string_any()".
1188 *------------------------------------------------------------------------*/
1190 usbd_req_get_string_desc(struct usb_device *udev, struct lock *lock, void *sdesc,
1191 uint16_t max_len, uint16_t lang_id,
1192 uint8_t string_index)
1194 return (usbd_req_get_desc(udev, lock, NULL, sdesc, 2, max_len, lang_id,
1195 UDESC_STRING, string_index, 0));
1198 /*------------------------------------------------------------------------*
1199 * usbd_req_get_config_desc_ptr
1201 * This function is used in device side mode to retrieve the pointer
1202 * to the generated config descriptor. This saves allocating space for
1203 * an additional config descriptor when setting the configuration.
1208 *------------------------------------------------------------------------*/
1210 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1211 struct usb_config_descriptor **ppcd, uint16_t wValue)
1213 struct usb_device_request req;
1214 usb_handle_req_t *hr_func;
1219 req.bmRequestType = UT_READ_DEVICE;
1220 req.bRequest = UR_GET_DESCRIPTOR;
1221 USETW(req.wValue, wValue);
1222 USETW(req.wIndex, 0);
1223 USETW(req.wLength, 0);
1228 hr_func = usbd_get_hr_func(udev);
1230 if (hr_func == NULL)
1231 err = USB_ERR_INVAL;
1233 USB_BUS_LOCK(udev->bus);
1234 err = (hr_func) (udev, &req, &ptr, &len);
1235 USB_BUS_UNLOCK(udev->bus);
1240 else if (ptr == NULL)
1241 err = USB_ERR_INVAL;
1243 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1248 /*------------------------------------------------------------------------*
1249 * usbd_req_get_config_desc
1254 *------------------------------------------------------------------------*/
1256 usbd_req_get_config_desc(struct usb_device *udev, struct lock *lock,
1257 struct usb_config_descriptor *d, uint8_t conf_index)
1261 DPRINTFN(4, "confidx=%d\n", conf_index);
1263 err = usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1264 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1268 /* Extra sanity checking */
1269 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1270 err = USB_ERR_INVAL;
1276 /*------------------------------------------------------------------------*
1277 * usbd_alloc_config_desc
1279 * This function is used to allocate a zeroed configuration
1285 *------------------------------------------------------------------------*/
1287 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1289 if (size > USB_CONFIG_MAX) {
1290 DPRINTF("Configuration descriptor too big\n");
1293 #if (USB_HAVE_FIXED_CONFIG == 0)
1294 return (kmalloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1296 memset(udev->config_data, 0, sizeof(udev->config_data));
1297 return (udev->config_data);
1301 /*------------------------------------------------------------------------*
1302 * usbd_alloc_config_desc
1304 * This function is used to free a configuration descriptor.
1305 *------------------------------------------------------------------------*/
1307 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1309 #if (USB_HAVE_FIXED_CONFIG == 0)
1311 kfree(ptr, M_USBDEV);
1313 kprintf("usbd_free_config_desc: nullpointer\n");
1318 /*------------------------------------------------------------------------*
1319 * usbd_req_get_config_desc_full
1321 * This function gets the complete USB configuration descriptor and
1322 * ensures that "wTotalLength" is correct. The returned configuration
1323 * descriptor is freed by calling "usbd_free_config_desc()".
1328 *------------------------------------------------------------------------*/
1330 usbd_req_get_config_desc_full(struct usb_device *udev, struct lock *lock,
1331 struct usb_config_descriptor **ppcd, uint8_t index)
1333 struct usb_config_descriptor cd;
1334 struct usb_config_descriptor *cdesc;
1338 DPRINTFN(4, "index=%d\n", index);
1342 err = usbd_req_get_config_desc(udev, lock, &cd, index);
1346 /* get full descriptor */
1347 len = UGETW(cd.wTotalLength);
1348 if (len < (uint32_t)sizeof(*cdesc)) {
1349 /* corrupt descriptor */
1350 return (USB_ERR_INVAL);
1351 } else if (len > USB_CONFIG_MAX) {
1352 DPRINTF("Configuration descriptor was truncated\n");
1353 len = USB_CONFIG_MAX;
1355 cdesc = usbd_alloc_config_desc(udev, len);
1357 return (USB_ERR_NOMEM);
1358 err = usbd_req_get_desc(udev, lock, NULL, cdesc, len, len, 0,
1359 UDESC_CONFIG, index, 3);
1361 usbd_free_config_desc(udev, cdesc);
1364 /* make sure that the device is not fooling us: */
1365 USETW(cdesc->wTotalLength, len);
1369 return (0); /* success */
1372 /*------------------------------------------------------------------------*
1373 * usbd_req_get_device_desc
1378 *------------------------------------------------------------------------*/
1380 usbd_req_get_device_desc(struct usb_device *udev, struct lock *lock,
1381 struct usb_device_descriptor *d)
1384 return (usbd_req_get_desc(udev, lock, NULL, d, sizeof(*d),
1385 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1388 /*------------------------------------------------------------------------*
1389 * usbd_req_get_alt_interface_no
1394 *------------------------------------------------------------------------*/
1396 usbd_req_get_alt_interface_no(struct usb_device *udev, struct lock *lock,
1397 uint8_t *alt_iface_no, uint8_t iface_index)
1399 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1400 struct usb_device_request req;
1402 if ((iface == NULL) || (iface->idesc == NULL))
1403 return (USB_ERR_INVAL);
1405 req.bmRequestType = UT_READ_INTERFACE;
1406 req.bRequest = UR_GET_INTERFACE;
1407 USETW(req.wValue, 0);
1408 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1410 USETW(req.wLength, 1);
1411 return (usbd_do_request(udev, lock, &req, alt_iface_no));
1414 /*------------------------------------------------------------------------*
1415 * usbd_req_set_alt_interface_no
1420 *------------------------------------------------------------------------*/
1422 usbd_req_set_alt_interface_no(struct usb_device *udev, struct lock *lock,
1423 uint8_t iface_index, uint8_t alt_no)
1425 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1426 struct usb_device_request req;
1428 if ((iface == NULL) || (iface->idesc == NULL))
1429 return (USB_ERR_INVAL);
1431 req.bmRequestType = UT_WRITE_INTERFACE;
1432 req.bRequest = UR_SET_INTERFACE;
1433 req.wValue[0] = alt_no;
1435 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1437 USETW(req.wLength, 0);
1438 return (usbd_do_request(udev, lock, &req, 0));
1441 /*------------------------------------------------------------------------*
1442 * usbd_req_get_device_status
1447 *------------------------------------------------------------------------*/
1449 usbd_req_get_device_status(struct usb_device *udev, struct lock *lock,
1450 struct usb_status *st)
1452 struct usb_device_request req;
1454 req.bmRequestType = UT_READ_DEVICE;
1455 req.bRequest = UR_GET_STATUS;
1456 USETW(req.wValue, 0);
1457 USETW(req.wIndex, 0);
1458 USETW(req.wLength, sizeof(*st));
1459 return (usbd_do_request(udev, lock, &req, st));
1462 /*------------------------------------------------------------------------*
1463 * usbd_req_get_hub_descriptor
1468 *------------------------------------------------------------------------*/
1470 usbd_req_get_hub_descriptor(struct usb_device *udev, struct lock *lock,
1471 struct usb_hub_descriptor *hd, uint8_t nports)
1473 struct usb_device_request req;
1474 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1476 req.bmRequestType = UT_READ_CLASS_DEVICE;
1477 req.bRequest = UR_GET_DESCRIPTOR;
1478 USETW2(req.wValue, UDESC_HUB, 0);
1479 USETW(req.wIndex, 0);
1480 USETW(req.wLength, len);
1481 return (usbd_do_request(udev, lock, &req, hd));
1484 /*------------------------------------------------------------------------*
1485 * usbd_req_get_ss_hub_descriptor
1490 *------------------------------------------------------------------------*/
1492 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct lock *lock,
1493 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1495 struct usb_device_request req;
1496 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1498 req.bmRequestType = UT_READ_CLASS_DEVICE;
1499 req.bRequest = UR_GET_DESCRIPTOR;
1500 USETW2(req.wValue, UDESC_SS_HUB, 0);
1501 USETW(req.wIndex, 0);
1502 USETW(req.wLength, len);
1503 return (usbd_do_request(udev, lock, &req, hd));
1506 /*------------------------------------------------------------------------*
1507 * usbd_req_get_hub_status
1512 *------------------------------------------------------------------------*/
1514 usbd_req_get_hub_status(struct usb_device *udev, struct lock *lock,
1515 struct usb_hub_status *st)
1517 struct usb_device_request req;
1519 req.bmRequestType = UT_READ_CLASS_DEVICE;
1520 req.bRequest = UR_GET_STATUS;
1521 USETW(req.wValue, 0);
1522 USETW(req.wIndex, 0);
1523 USETW(req.wLength, sizeof(struct usb_hub_status));
1524 return (usbd_do_request(udev, lock, &req, st));
1527 /*------------------------------------------------------------------------*
1528 * usbd_req_set_address
1530 * This function is used to set the address for an USB device. After
1531 * port reset the USB device will respond at address zero.
1536 *------------------------------------------------------------------------*/
1538 usbd_req_set_address(struct usb_device *udev, struct lock *lock, uint16_t addr)
1540 struct usb_device_request req;
1543 DPRINTFN(6, "setting device address=%d\n", addr);
1545 req.bmRequestType = UT_WRITE_DEVICE;
1546 req.bRequest = UR_SET_ADDRESS;
1547 USETW(req.wValue, addr);
1548 USETW(req.wIndex, 0);
1549 USETW(req.wLength, 0);
1551 err = USB_ERR_INVAL;
1553 /* check if USB controller handles set address */
1554 if (udev->bus->methods->set_address != NULL)
1555 err = (udev->bus->methods->set_address) (udev, lock, addr);
1557 if (err != USB_ERR_INVAL)
1560 /* Setting the address should not take more than 1 second ! */
1561 err = usbd_do_request_flags(udev, lock, &req, NULL,
1562 USB_DELAY_STATUS_STAGE, NULL, 1000);
1565 /* allow device time to set new address */
1567 USB_MS_TO_TICKS(usb_set_address_settle));
1572 /*------------------------------------------------------------------------*
1573 * usbd_req_get_port_status
1578 *------------------------------------------------------------------------*/
1580 usbd_req_get_port_status(struct usb_device *udev, struct lock *lock,
1581 struct usb_port_status *ps, uint8_t port)
1583 struct usb_device_request req;
1585 req.bmRequestType = UT_READ_CLASS_OTHER;
1586 req.bRequest = UR_GET_STATUS;
1587 USETW(req.wValue, 0);
1588 req.wIndex[0] = port;
1590 USETW(req.wLength, sizeof *ps);
1591 return (usbd_do_request(udev, lock, &req, ps));
1594 /*------------------------------------------------------------------------*
1595 * usbd_req_clear_hub_feature
1600 *------------------------------------------------------------------------*/
1602 usbd_req_clear_hub_feature(struct usb_device *udev, struct lock *lock,
1605 struct usb_device_request req;
1607 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1608 req.bRequest = UR_CLEAR_FEATURE;
1609 USETW(req.wValue, sel);
1610 USETW(req.wIndex, 0);
1611 USETW(req.wLength, 0);
1612 return (usbd_do_request(udev, lock, &req, 0));
1615 /*------------------------------------------------------------------------*
1616 * usbd_req_set_hub_feature
1621 *------------------------------------------------------------------------*/
1623 usbd_req_set_hub_feature(struct usb_device *udev, struct lock *lock,
1626 struct usb_device_request req;
1628 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1629 req.bRequest = UR_SET_FEATURE;
1630 USETW(req.wValue, sel);
1631 USETW(req.wIndex, 0);
1632 USETW(req.wLength, 0);
1633 return (usbd_do_request(udev, lock, &req, 0));
1636 /*------------------------------------------------------------------------*
1637 * usbd_req_set_hub_u1_timeout
1642 *------------------------------------------------------------------------*/
1644 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct lock *lock,
1645 uint8_t port, uint8_t timeout)
1647 struct usb_device_request req;
1649 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1650 req.bRequest = UR_SET_FEATURE;
1651 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1652 req.wIndex[0] = port;
1653 req.wIndex[1] = timeout;
1654 USETW(req.wLength, 0);
1655 return (usbd_do_request(udev, lock, &req, 0));
1658 /*------------------------------------------------------------------------*
1659 * usbd_req_set_hub_u2_timeout
1664 *------------------------------------------------------------------------*/
1666 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct lock *lock,
1667 uint8_t port, uint8_t timeout)
1669 struct usb_device_request req;
1671 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1672 req.bRequest = UR_SET_FEATURE;
1673 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1674 req.wIndex[0] = port;
1675 req.wIndex[1] = timeout;
1676 USETW(req.wLength, 0);
1677 return (usbd_do_request(udev, lock, &req, 0));
1680 /*------------------------------------------------------------------------*
1681 * usbd_req_set_hub_depth
1686 *------------------------------------------------------------------------*/
1688 usbd_req_set_hub_depth(struct usb_device *udev, struct lock *lock,
1691 struct usb_device_request req;
1693 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1694 req.bRequest = UR_SET_HUB_DEPTH;
1695 USETW(req.wValue, depth);
1696 USETW(req.wIndex, 0);
1697 USETW(req.wLength, 0);
1698 return (usbd_do_request(udev, lock, &req, 0));
1701 /*------------------------------------------------------------------------*
1702 * usbd_req_clear_port_feature
1707 *------------------------------------------------------------------------*/
1709 usbd_req_clear_port_feature(struct usb_device *udev, struct lock *lock,
1710 uint8_t port, uint16_t sel)
1712 struct usb_device_request req;
1714 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1715 req.bRequest = UR_CLEAR_FEATURE;
1716 USETW(req.wValue, sel);
1717 req.wIndex[0] = port;
1719 USETW(req.wLength, 0);
1720 return (usbd_do_request(udev, lock, &req, 0));
1723 /*------------------------------------------------------------------------*
1724 * usbd_req_set_port_feature
1729 *------------------------------------------------------------------------*/
1731 usbd_req_set_port_feature(struct usb_device *udev, struct lock *lock,
1732 uint8_t port, uint16_t sel)
1734 struct usb_device_request req;
1736 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1737 req.bRequest = UR_SET_FEATURE;
1738 USETW(req.wValue, sel);
1739 req.wIndex[0] = port;
1741 USETW(req.wLength, 0);
1742 return (usbd_do_request(udev, lock, &req, 0));
1745 /*------------------------------------------------------------------------*
1746 * usbd_req_set_protocol
1751 *------------------------------------------------------------------------*/
1753 usbd_req_set_protocol(struct usb_device *udev, struct lock *lock,
1754 uint8_t iface_index, uint16_t report)
1756 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1757 struct usb_device_request req;
1759 if ((iface == NULL) || (iface->idesc == NULL)) {
1760 return (USB_ERR_INVAL);
1762 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1763 iface, report, iface->idesc->bInterfaceNumber);
1765 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1766 req.bRequest = UR_SET_PROTOCOL;
1767 USETW(req.wValue, report);
1768 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1770 USETW(req.wLength, 0);
1771 return (usbd_do_request(udev, lock, &req, 0));
1774 /*------------------------------------------------------------------------*
1775 * usbd_req_set_report
1780 *------------------------------------------------------------------------*/
1782 usbd_req_set_report(struct usb_device *udev, struct lock *lock, void *data, uint16_t len,
1783 uint8_t iface_index, uint8_t type, uint8_t id)
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 DPRINTFN(5, "len=%d\n", len);
1793 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1794 req.bRequest = UR_SET_REPORT;
1795 USETW2(req.wValue, type, id);
1796 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1798 USETW(req.wLength, len);
1799 return (usbd_do_request(udev, lock, &req, data));
1802 /*------------------------------------------------------------------------*
1803 * usbd_req_get_report
1808 *------------------------------------------------------------------------*/
1810 usbd_req_get_report(struct usb_device *udev, struct lock *lock, void *data,
1811 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1813 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1814 struct usb_device_request req;
1816 if ((iface == NULL) || (iface->idesc == NULL)) {
1817 return (USB_ERR_INVAL);
1819 DPRINTFN(5, "len=%d\n", len);
1821 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1822 req.bRequest = UR_GET_REPORT;
1823 USETW2(req.wValue, type, id);
1824 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1826 USETW(req.wLength, len);
1827 return (usbd_do_request(udev, lock, &req, data));
1830 /*------------------------------------------------------------------------*
1836 *------------------------------------------------------------------------*/
1838 usbd_req_set_idle(struct usb_device *udev, struct lock *lock,
1839 uint8_t iface_index, uint8_t duration, uint8_t id)
1841 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1842 struct usb_device_request req;
1844 if ((iface == NULL) || (iface->idesc == NULL)) {
1845 return (USB_ERR_INVAL);
1847 DPRINTFN(5, "%d %d\n", duration, id);
1849 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1850 req.bRequest = UR_SET_IDLE;
1851 USETW2(req.wValue, duration, id);
1852 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1854 USETW(req.wLength, 0);
1855 return (usbd_do_request(udev, lock, &req, 0));
1858 /*------------------------------------------------------------------------*
1859 * usbd_req_get_report_descriptor
1864 *------------------------------------------------------------------------*/
1866 usbd_req_get_report_descriptor(struct usb_device *udev, struct lock *lock,
1867 void *d, uint16_t size, uint8_t iface_index)
1869 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1870 struct usb_device_request req;
1872 if ((iface == NULL) || (iface->idesc == NULL)) {
1873 return (USB_ERR_INVAL);
1875 req.bmRequestType = UT_READ_INTERFACE;
1876 req.bRequest = UR_GET_DESCRIPTOR;
1877 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1878 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1880 USETW(req.wLength, size);
1881 return (usbd_do_request(udev, lock, &req, d));
1884 /*------------------------------------------------------------------------*
1885 * usbd_req_set_config
1887 * This function is used to select the current configuration number in
1888 * both USB device side mode and USB host side mode. When setting the
1889 * configuration the function of the interfaces can change.
1894 *------------------------------------------------------------------------*/
1896 usbd_req_set_config(struct usb_device *udev, struct lock *lock, uint8_t conf)
1898 struct usb_device_request req;
1900 DPRINTF("setting config %d\n", conf);
1902 /* do "set configuration" request */
1904 req.bmRequestType = UT_WRITE_DEVICE;
1905 req.bRequest = UR_SET_CONFIG;
1906 req.wValue[0] = conf;
1908 USETW(req.wIndex, 0);
1909 USETW(req.wLength, 0);
1910 return (usbd_do_request(udev, lock, &req, 0));
1913 /*------------------------------------------------------------------------*
1914 * usbd_req_get_config
1919 *------------------------------------------------------------------------*/
1921 usbd_req_get_config(struct usb_device *udev, struct lock *lock, uint8_t *pconf)
1923 struct usb_device_request req;
1925 req.bmRequestType = UT_READ_DEVICE;
1926 req.bRequest = UR_GET_CONFIG;
1927 USETW(req.wValue, 0);
1928 USETW(req.wIndex, 0);
1929 USETW(req.wLength, 1);
1930 return (usbd_do_request(udev, lock, &req, pconf));
1933 /*------------------------------------------------------------------------*
1934 * usbd_setup_device_desc
1935 *------------------------------------------------------------------------*/
1937 usbd_setup_device_desc(struct usb_device *udev, struct lock *lock)
1942 * Get the first 8 bytes of the device descriptor !
1944 * NOTE: "usbd_do_request()" will check the device descriptor
1945 * next time we do a request to see if the maximum packet size
1946 * changed! The 8 first bytes of the device descriptor
1947 * contains the maximum packet size to use on control endpoint
1948 * 0. If this value is different from "USB_MAX_IPACKET" a new
1949 * USB control request will be setup!
1951 switch (udev->speed) {
1952 case USB_SPEED_FULL:
1953 if (usb_full_ddesc != 0) {
1954 /* get full device descriptor */
1955 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1960 /* get partial device descriptor, some devices crash on this */
1961 err = usbd_req_get_desc(udev, lock, NULL, &udev->ddesc,
1962 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1966 /* get the full device descriptor */
1967 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1971 DPRINTF("Minimum bMaxPacketSize is large enough "
1972 "to hold the complete device descriptor or "
1973 "only one bMaxPacketSize choice\n");
1975 /* get the full device descriptor */
1976 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1978 /* try one more time, if error */
1980 err = usbd_req_get_device_desc(udev, lock, &udev->ddesc);
1985 DPRINTFN(0, "getting device descriptor "
1986 "at addr %d failed, %s\n", udev->address,
1991 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1992 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1993 udev->address, UGETW(udev->ddesc.bcdUSB),
1994 udev->ddesc.bDeviceClass,
1995 udev->ddesc.bDeviceSubClass,
1996 udev->ddesc.bDeviceProtocol,
1997 udev->ddesc.bMaxPacketSize,
1998 udev->ddesc.bLength,
2004 /*------------------------------------------------------------------------*
2005 * usbd_req_re_enumerate
2007 * NOTE: After this function returns the hardware is in the
2008 * unconfigured state! The application is responsible for setting a
2009 * new configuration.
2014 *------------------------------------------------------------------------*/
2016 usbd_req_re_enumerate(struct usb_device *udev, struct lock *lock)
2018 struct usb_device *parent_hub;
2021 uint8_t do_retry = 1;
2023 if (udev->flags.usb_mode != USB_MODE_HOST) {
2024 return (USB_ERR_INVAL);
2026 old_addr = udev->address;
2027 parent_hub = udev->parent_hub;
2028 if (parent_hub == NULL) {
2029 return (USB_ERR_INVAL);
2032 #if USB_HAVE_TT_SUPPORT
2034 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2035 * speed device, if any.
2037 if (udev->parent_hs_hub != NULL &&
2038 udev->speed != USB_SPEED_HIGH) {
2039 DPRINTF("Trying to reset parent High Speed TT.\n");
2040 if (udev->parent_hs_hub == parent_hub &&
2041 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2042 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2043 /* we can reset the whole TT */
2044 err = usbd_req_reset_tt(parent_hub, NULL,
2047 /* only reset a particular device and endpoint */
2048 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2049 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2052 DPRINTF("Resetting parent High "
2053 "Speed TT failed (%s).\n",
2058 /* Try to warm reset first */
2059 if (parent_hub->speed == USB_SPEED_SUPER)
2060 usbd_req_warm_reset_port(parent_hub, lock, udev->port_no);
2062 /* Try to reset the parent HUB port. */
2063 err = usbd_req_reset_port(parent_hub, lock, udev->port_no);
2065 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2066 old_addr, usbd_errstr(err));
2071 * After that the port has been reset our device should be at
2074 udev->address = USB_START_ADDR;
2076 /* reset "bMaxPacketSize" */
2077 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2079 /* reset USB state */
2080 usb_set_device_state(udev, USB_STATE_POWERED);
2083 * Restore device address:
2085 err = usbd_req_set_address(udev, lock, old_addr);
2087 /* XXX ignore any errors! */
2088 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2089 old_addr, usbd_errstr(err));
2092 * Restore device address, if the controller driver did not
2095 if (udev->address == USB_START_ADDR)
2096 udev->address = old_addr;
2098 /* setup the device descriptor and the initial "wMaxPacketSize" */
2099 err = usbd_setup_device_desc(udev, lock);
2102 if (err && do_retry) {
2103 /* give the USB firmware some time to load */
2104 usb_pause_mtx(lock, hz / 2);
2105 /* no more retries after this retry */
2110 /* restore address */
2111 if (udev->address == USB_START_ADDR)
2112 udev->address = old_addr;
2113 /* update state, if successful */
2115 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2119 /*------------------------------------------------------------------------*
2120 * usbd_req_clear_device_feature
2125 *------------------------------------------------------------------------*/
2127 usbd_req_clear_device_feature(struct usb_device *udev, struct lock *lock,
2130 struct usb_device_request req;
2132 req.bmRequestType = UT_WRITE_DEVICE;
2133 req.bRequest = UR_CLEAR_FEATURE;
2134 USETW(req.wValue, sel);
2135 USETW(req.wIndex, 0);
2136 USETW(req.wLength, 0);
2137 return (usbd_do_request(udev, lock, &req, 0));
2140 /*------------------------------------------------------------------------*
2141 * usbd_req_set_device_feature
2146 *------------------------------------------------------------------------*/
2148 usbd_req_set_device_feature(struct usb_device *udev, struct lock *lock,
2151 struct usb_device_request req;
2153 req.bmRequestType = UT_WRITE_DEVICE;
2154 req.bRequest = UR_SET_FEATURE;
2155 USETW(req.wValue, sel);
2156 USETW(req.wIndex, 0);
2157 USETW(req.wLength, 0);
2158 return (usbd_do_request(udev, lock, &req, 0));
2161 /*------------------------------------------------------------------------*
2167 *------------------------------------------------------------------------*/
2169 usbd_req_reset_tt(struct usb_device *udev, struct lock *lock,
2172 struct usb_device_request req;
2174 /* For single TT HUBs the port should be 1 */
2176 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2177 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2180 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2181 req.bRequest = UR_RESET_TT;
2182 USETW(req.wValue, 0);
2183 req.wIndex[0] = port;
2185 USETW(req.wLength, 0);
2186 return (usbd_do_request(udev, lock, &req, 0));
2189 /*------------------------------------------------------------------------*
2190 * usbd_req_clear_tt_buffer
2192 * For single TT HUBs the port should be 1.
2197 *------------------------------------------------------------------------*/
2199 usbd_req_clear_tt_buffer(struct usb_device *udev, struct lock *lock,
2200 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2202 struct usb_device_request req;
2205 /* For single TT HUBs the port should be 1 */
2207 if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2208 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2211 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2212 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2214 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2215 req.bRequest = UR_CLEAR_TT_BUFFER;
2216 USETW(req.wValue, wValue);
2217 req.wIndex[0] = port;
2219 USETW(req.wLength, 0);
2220 return (usbd_do_request(udev, lock, &req, 0));
2223 /*------------------------------------------------------------------------*
2224 * usbd_req_set_port_link_state
2226 * USB 3.0 specific request
2231 *------------------------------------------------------------------------*/
2233 usbd_req_set_port_link_state(struct usb_device *udev, struct lock *lock,
2234 uint8_t port, uint8_t link_state)
2236 struct usb_device_request req;
2238 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2239 req.bRequest = UR_SET_FEATURE;
2240 USETW(req.wValue, UHF_PORT_LINK_STATE);
2241 req.wIndex[0] = port;
2242 req.wIndex[1] = link_state;
2243 USETW(req.wLength, 0);
2244 return (usbd_do_request(udev, lock, &req, 0));
2247 /*------------------------------------------------------------------------*
2248 * usbd_req_set_lpm_info
2250 * USB 2.0 specific request for Link Power Management.
2254 * USB_ERR_PENDING_REQUESTS: NYET
2255 * USB_ERR_TIMEOUT: TIMEOUT
2256 * USB_ERR_STALL: STALL
2258 *------------------------------------------------------------------------*/
2260 usbd_req_set_lpm_info(struct usb_device *udev, struct lock *lock,
2261 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2263 struct usb_device_request req;
2267 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2268 req.bRequest = UR_SET_AND_TEST;
2269 USETW(req.wValue, UHF_PORT_L1);
2270 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2271 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2272 USETW(req.wLength, sizeof(buf));
2274 /* set default value in case of short transfer */
2277 err = usbd_do_request(udev, lock, &req, buf);
2282 case 0x00: /* SUCCESS */
2284 case 0x10: /* NYET */
2285 err = USB_ERR_PENDING_REQUESTS;
2287 case 0x11: /* TIMEOUT */
2288 err = USB_ERR_TIMEOUT;
2290 case 0x30: /* STALL */
2291 err = USB_ERR_STALLED;
2293 default: /* reserved */
2294 err = USB_ERR_IOERROR;