xref: /freebsd/sys/dev/usb/usb_request.c (revision bdc081c60ff09707a85971b94c9177d16cc72033)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
402ac6454SAndrew Thompson  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
502ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
3002ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
3102ac6454SAndrew Thompson #include <dev/usb/usb.h>
3202ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
3302ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
3402ac6454SAndrew Thompson 
3502ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_debug
3602ac6454SAndrew Thompson 
3702ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
4002ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
4102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
4402ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4502ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
4602ac6454SAndrew Thompson 
4702ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
4802ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
4902ac6454SAndrew Thompson #include <sys/ctype.h>
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson #if USB_DEBUG
5202ac6454SAndrew Thompson static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY;
5302ac6454SAndrew Thompson static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
5402ac6454SAndrew Thompson static int usb2_ss_delay = 0;
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
5702ac6454SAndrew Thompson     &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms");
5802ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
5902ac6454SAndrew Thompson     &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
6002ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW,
6102ac6454SAndrew Thompson     &usb2_ss_delay, 0, "USB status stage delay in ms");
6202ac6454SAndrew Thompson #endif
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
6502ac6454SAndrew Thompson  *	usb2_do_request_callback
6602ac6454SAndrew Thompson  *
6702ac6454SAndrew Thompson  * This function is the USB callback for generic USB Host control
6802ac6454SAndrew Thompson  * transfers.
6902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
7002ac6454SAndrew Thompson void
7102ac6454SAndrew Thompson usb2_do_request_callback(struct usb2_xfer *xfer)
7202ac6454SAndrew Thompson {
7302ac6454SAndrew Thompson 	;				/* workaround for a bug in "indent" */
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson 	DPRINTF("st=%u\n", USB_GET_STATE(xfer));
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
7802ac6454SAndrew Thompson 	case USB_ST_SETUP:
7902ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
8002ac6454SAndrew Thompson 		break;
8102ac6454SAndrew Thompson 	default:
8202ac6454SAndrew Thompson 		usb2_cv_signal(xfer->xroot->udev->default_cv);
8302ac6454SAndrew Thompson 		break;
8402ac6454SAndrew Thompson 	}
8502ac6454SAndrew Thompson }
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
8802ac6454SAndrew Thompson  *	usb2_do_clear_stall_callback
8902ac6454SAndrew Thompson  *
9002ac6454SAndrew Thompson  * This function is the USB callback for generic clear stall requests.
9102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
9202ac6454SAndrew Thompson void
9302ac6454SAndrew Thompson usb2_do_clear_stall_callback(struct usb2_xfer *xfer)
9402ac6454SAndrew Thompson {
9502ac6454SAndrew Thompson 	struct usb2_device_request req;
9602ac6454SAndrew Thompson 	struct usb2_device *udev;
9702ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
9802ac6454SAndrew Thompson 	struct usb2_pipe *pipe_end;
9902ac6454SAndrew Thompson 	struct usb2_pipe *pipe_first;
10002ac6454SAndrew Thompson 	uint8_t to = USB_EP_MAX;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson 	udev = xfer->xroot->udev;
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson 	/* round robin pipe clear stall */
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 	pipe = udev->pipe_curr;
10902ac6454SAndrew Thompson 	pipe_end = udev->pipes + USB_EP_MAX;
11002ac6454SAndrew Thompson 	pipe_first = udev->pipes;
11102ac6454SAndrew Thompson 	if (pipe == NULL) {
11202ac6454SAndrew Thompson 		pipe = pipe_first;
11302ac6454SAndrew Thompson 	}
11402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
11502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
11602ac6454SAndrew Thompson 		if (pipe->edesc &&
11702ac6454SAndrew Thompson 		    pipe->is_stalled) {
11802ac6454SAndrew Thompson 			pipe->toggle_next = 0;
11902ac6454SAndrew Thompson 			pipe->is_stalled = 0;
12002ac6454SAndrew Thompson 			/* start up the current or next transfer, if any */
12102ac6454SAndrew Thompson 			usb2_command_wrapper(&pipe->pipe_q,
12202ac6454SAndrew Thompson 			    pipe->pipe_q.curr);
12302ac6454SAndrew Thompson 		}
12402ac6454SAndrew Thompson 		pipe++;
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson 	case USB_ST_SETUP:
12702ac6454SAndrew Thompson tr_setup:
12802ac6454SAndrew Thompson 		if (pipe == pipe_end) {
12902ac6454SAndrew Thompson 			pipe = pipe_first;
13002ac6454SAndrew Thompson 		}
13102ac6454SAndrew Thompson 		if (pipe->edesc &&
13202ac6454SAndrew Thompson 		    pipe->is_stalled) {
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson 			/* setup a clear-stall packet */
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_ENDPOINT;
13702ac6454SAndrew Thompson 			req.bRequest = UR_CLEAR_FEATURE;
13802ac6454SAndrew Thompson 			USETW(req.wValue, UF_ENDPOINT_HALT);
13902ac6454SAndrew Thompson 			req.wIndex[0] = pipe->edesc->bEndpointAddress;
14002ac6454SAndrew Thompson 			req.wIndex[1] = 0;
14102ac6454SAndrew Thompson 			USETW(req.wLength, 0);
14202ac6454SAndrew Thompson 
14302ac6454SAndrew Thompson 			/* copy in the transfer */
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson 			/* set length */
14802ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
14902ac6454SAndrew Thompson 			xfer->nframes = 1;
15002ac6454SAndrew Thompson 			USB_BUS_UNLOCK(udev->bus);
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
15302ac6454SAndrew Thompson 
15402ac6454SAndrew Thompson 			USB_BUS_LOCK(udev->bus);
15502ac6454SAndrew Thompson 			break;
15602ac6454SAndrew Thompson 		}
15702ac6454SAndrew Thompson 		pipe++;
15802ac6454SAndrew Thompson 		if (--to)
15902ac6454SAndrew Thompson 			goto tr_setup;
16002ac6454SAndrew Thompson 		break;
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson 	default:
16302ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
16402ac6454SAndrew Thompson 			break;
16502ac6454SAndrew Thompson 		}
16602ac6454SAndrew Thompson 		goto tr_setup;
16702ac6454SAndrew Thompson 	}
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson 	/* store current pipe */
17002ac6454SAndrew Thompson 	udev->pipe_curr = pipe;
17102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
17202ac6454SAndrew Thompson }
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
17502ac6454SAndrew Thompson  *	usb2_do_request_flags and usb2_do_request
17602ac6454SAndrew Thompson  *
17702ac6454SAndrew Thompson  * Description of arguments passed to these functions:
17802ac6454SAndrew Thompson  *
17902ac6454SAndrew Thompson  * "udev" - this is the "usb2_device" structure pointer on which the
18002ac6454SAndrew Thompson  * request should be performed. It is possible to call this function
18102ac6454SAndrew Thompson  * in both Host Side mode and Device Side mode.
18202ac6454SAndrew Thompson  *
18302ac6454SAndrew Thompson  * "mtx" - if this argument is non-NULL the mutex pointed to by it
18402ac6454SAndrew Thompson  * will get dropped and picked up during the execution of this
18502ac6454SAndrew Thompson  * function, hence this function sometimes needs to sleep. If this
18602ac6454SAndrew Thompson  * argument is NULL it has no effect.
18702ac6454SAndrew Thompson  *
18802ac6454SAndrew Thompson  * "req" - this argument must always be non-NULL and points to an
18902ac6454SAndrew Thompson  * 8-byte structure holding the USB request to be done. The USB
19002ac6454SAndrew Thompson  * request structure has a bit telling the direction of the USB
19102ac6454SAndrew Thompson  * request, if it is a read or a write.
19202ac6454SAndrew Thompson  *
19302ac6454SAndrew Thompson  * "data" - if the "wLength" part of the structure pointed to by "req"
19402ac6454SAndrew Thompson  * is non-zero this argument must point to a valid kernel buffer which
19502ac6454SAndrew Thompson  * can hold at least "wLength" bytes. If "wLength" is zero "data" can
19602ac6454SAndrew Thompson  * be NULL.
19702ac6454SAndrew Thompson  *
19802ac6454SAndrew Thompson  * "flags" - here is a list of valid flags:
19902ac6454SAndrew Thompson  *
20002ac6454SAndrew Thompson  *  o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
20102ac6454SAndrew Thompson  *  specified
20202ac6454SAndrew Thompson  *
20302ac6454SAndrew Thompson  *  o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
20402ac6454SAndrew Thompson  *  at a later point in time. This is tunable by the "hw.usb.ss_delay"
20502ac6454SAndrew Thompson  *  sysctl. This flag is mostly useful for debugging.
20602ac6454SAndrew Thompson  *
20702ac6454SAndrew Thompson  *  o USB_USER_DATA_PTR: treat the "data" pointer like a userland
20802ac6454SAndrew Thompson  *  pointer.
20902ac6454SAndrew Thompson  *
21002ac6454SAndrew Thompson  * "actlen" - if non-NULL the actual transfer length will be stored in
21102ac6454SAndrew Thompson  * the 16-bit unsigned integer pointed to by "actlen". This
21202ac6454SAndrew Thompson  * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
21302ac6454SAndrew Thompson  * used.
21402ac6454SAndrew Thompson  *
21502ac6454SAndrew Thompson  * "timeout" - gives the timeout for the control transfer in
21602ac6454SAndrew Thompson  * milliseconds. A "timeout" value less than 50 milliseconds is
21702ac6454SAndrew Thompson  * treated like a 50 millisecond timeout. A "timeout" value greater
21802ac6454SAndrew Thompson  * than 30 seconds is treated like a 30 second timeout. This USB stack
21902ac6454SAndrew Thompson  * does not allow control requests without a timeout.
22002ac6454SAndrew Thompson  *
22102ac6454SAndrew Thompson  * NOTE: This function is thread safe. All calls to
22202ac6454SAndrew Thompson  * "usb2_do_request_flags" will be serialised by the use of an
22302ac6454SAndrew Thompson  * internal "sx_lock".
22402ac6454SAndrew Thompson  *
22502ac6454SAndrew Thompson  * Returns:
22602ac6454SAndrew Thompson  *    0: Success
22702ac6454SAndrew Thompson  * Else: Failure
22802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
22902ac6454SAndrew Thompson usb2_error_t
23002ac6454SAndrew Thompson usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx,
23102ac6454SAndrew Thompson     struct usb2_device_request *req, void *data, uint32_t flags,
23202ac6454SAndrew Thompson     uint16_t *actlen, uint32_t timeout)
23302ac6454SAndrew Thompson {
23402ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
23502ac6454SAndrew Thompson 	const void *desc;
23602ac6454SAndrew Thompson 	int err = 0;
23702ac6454SAndrew Thompson 	uint32_t start_ticks;
23802ac6454SAndrew Thompson 	uint32_t delta_ticks;
23902ac6454SAndrew Thompson 	uint32_t max_ticks;
24002ac6454SAndrew Thompson 	uint16_t length;
24102ac6454SAndrew Thompson 	uint16_t temp;
24202ac6454SAndrew Thompson 
24302ac6454SAndrew Thompson 	if (timeout < 50) {
24402ac6454SAndrew Thompson 		/* timeout is too small */
24502ac6454SAndrew Thompson 		timeout = 50;
24602ac6454SAndrew Thompson 	}
24702ac6454SAndrew Thompson 	if (timeout > 30000) {
24802ac6454SAndrew Thompson 		/* timeout is too big */
24902ac6454SAndrew Thompson 		timeout = 30000;
25002ac6454SAndrew Thompson 	}
25102ac6454SAndrew Thompson 	length = UGETW(req->wLength);
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson 	DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
25402ac6454SAndrew Thompson 	    "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
25502ac6454SAndrew Thompson 	    udev, req->bmRequestType, req->bRequest,
25602ac6454SAndrew Thompson 	    req->wValue[1], req->wValue[0],
25702ac6454SAndrew Thompson 	    req->wIndex[1], req->wIndex[0],
25802ac6454SAndrew Thompson 	    req->wLength[1], req->wLength[0]);
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	/*
26102ac6454SAndrew Thompson 	 * Set "actlen" to a known value in case the caller does not
26202ac6454SAndrew Thompson 	 * check the return value:
26302ac6454SAndrew Thompson 	 */
26402ac6454SAndrew Thompson 	if (actlen) {
26502ac6454SAndrew Thompson 		*actlen = 0;
26602ac6454SAndrew Thompson 	}
267bdc081c6SAndrew Thompson #if (USB_HAVE_USER_IO == 0)
268bdc081c6SAndrew Thompson 	if (flags & USB_USER_DATA_PTR)
269bdc081c6SAndrew Thompson 		return (USB_ERR_INVAL);
270bdc081c6SAndrew Thompson #endif
27102ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
27202ac6454SAndrew Thompson 		DPRINTF("USB device mode\n");
27302ac6454SAndrew Thompson 		(usb2_temp_get_desc_p) (udev, req, &desc, &temp);
27402ac6454SAndrew Thompson 		if (length > temp) {
27502ac6454SAndrew Thompson 			if (!(flags & USB_SHORT_XFER_OK)) {
27602ac6454SAndrew Thompson 				return (USB_ERR_SHORT_XFER);
27702ac6454SAndrew Thompson 			}
27802ac6454SAndrew Thompson 			length = temp;
27902ac6454SAndrew Thompson 		}
28002ac6454SAndrew Thompson 		if (actlen) {
28102ac6454SAndrew Thompson 			*actlen = length;
28202ac6454SAndrew Thompson 		}
28302ac6454SAndrew Thompson 		if (length > 0) {
284bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
28502ac6454SAndrew Thompson 			if (flags & USB_USER_DATA_PTR) {
28602ac6454SAndrew Thompson 				if (copyout(desc, data, length)) {
28702ac6454SAndrew Thompson 					return (USB_ERR_INVAL);
28802ac6454SAndrew Thompson 				}
289bdc081c6SAndrew Thompson 			} else
290bdc081c6SAndrew Thompson #endif
29102ac6454SAndrew Thompson 				bcopy(desc, data, length);
29202ac6454SAndrew Thompson 		}
29302ac6454SAndrew Thompson 		return (0);		/* success */
29402ac6454SAndrew Thompson 	}
29502ac6454SAndrew Thompson 	if (mtx) {
29602ac6454SAndrew Thompson 		mtx_unlock(mtx);
29702ac6454SAndrew Thompson 		if (mtx != &Giant) {
29802ac6454SAndrew Thompson 			mtx_assert(mtx, MA_NOTOWNED);
29902ac6454SAndrew Thompson 		}
30002ac6454SAndrew Thompson 	}
30102ac6454SAndrew Thompson 	/*
30202ac6454SAndrew Thompson 	 * Grab the default sx-lock so that serialisation
30302ac6454SAndrew Thompson 	 * is achieved when multiple threads are involved:
30402ac6454SAndrew Thompson 	 */
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	sx_xlock(udev->default_sx);
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson 	/*
30902ac6454SAndrew Thompson 	 * Setup a new USB transfer or use the existing one, if any:
31002ac6454SAndrew Thompson 	 */
31102ac6454SAndrew Thompson 	usb2_default_transfer_setup(udev);
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	xfer = udev->default_xfer[0];
31402ac6454SAndrew Thompson 	if (xfer == NULL) {
31502ac6454SAndrew Thompson 		/* most likely out of memory */
31602ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
31702ac6454SAndrew Thompson 		goto done;
31802ac6454SAndrew Thompson 	}
31902ac6454SAndrew Thompson 	USB_XFER_LOCK(xfer);
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	if (flags & USB_DELAY_STATUS_STAGE) {
32202ac6454SAndrew Thompson 		xfer->flags.manual_status = 1;
32302ac6454SAndrew Thompson 	} else {
32402ac6454SAndrew Thompson 		xfer->flags.manual_status = 0;
32502ac6454SAndrew Thompson 	}
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson 	xfer->timeout = timeout;
32802ac6454SAndrew Thompson 
32902ac6454SAndrew Thompson 	start_ticks = ticks;
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	max_ticks = USB_MS_TO_TICKS(timeout);
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson 	usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson 	xfer->frlengths[0] = sizeof(*req);
33602ac6454SAndrew Thompson 	xfer->nframes = 2;
33702ac6454SAndrew Thompson 
33802ac6454SAndrew Thompson 	while (1) {
33902ac6454SAndrew Thompson 		temp = length;
34002ac6454SAndrew Thompson 		if (temp > xfer->max_data_length) {
34102ac6454SAndrew Thompson 			temp = xfer->max_data_length;
34202ac6454SAndrew Thompson 		}
34302ac6454SAndrew Thompson 		xfer->frlengths[1] = temp;
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 		if (temp > 0) {
34602ac6454SAndrew Thompson 			if (!(req->bmRequestType & UT_READ)) {
347bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
34802ac6454SAndrew Thompson 				if (flags & USB_USER_DATA_PTR) {
34902ac6454SAndrew Thompson 					USB_XFER_UNLOCK(xfer);
35002ac6454SAndrew Thompson 					err = usb2_copy_in_user(xfer->frbuffers + 1,
35102ac6454SAndrew Thompson 					    0, data, temp);
35202ac6454SAndrew Thompson 					USB_XFER_LOCK(xfer);
35302ac6454SAndrew Thompson 					if (err) {
35402ac6454SAndrew Thompson 						err = USB_ERR_INVAL;
35502ac6454SAndrew Thompson 						break;
35602ac6454SAndrew Thompson 					}
357bdc081c6SAndrew Thompson 				} else
358bdc081c6SAndrew Thompson #endif
359bdc081c6SAndrew Thompson 					usb2_copy_in(xfer->frbuffers + 1,
360bdc081c6SAndrew Thompson 					    0, data, temp);
36102ac6454SAndrew Thompson 			}
36202ac6454SAndrew Thompson 			xfer->nframes = 2;
36302ac6454SAndrew Thompson 		} else {
36402ac6454SAndrew Thompson 			if (xfer->frlengths[0] == 0) {
36502ac6454SAndrew Thompson 				if (xfer->flags.manual_status) {
36602ac6454SAndrew Thompson #if USB_DEBUG
36702ac6454SAndrew Thompson 					int temp;
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 					temp = usb2_ss_delay;
37002ac6454SAndrew Thompson 					if (temp > 5000) {
37102ac6454SAndrew Thompson 						temp = 5000;
37202ac6454SAndrew Thompson 					}
37302ac6454SAndrew Thompson 					if (temp > 0) {
37402ac6454SAndrew Thompson 						usb2_pause_mtx(
37502ac6454SAndrew Thompson 						    xfer->xroot->xfer_mtx,
37602ac6454SAndrew Thompson 						    USB_MS_TO_TICKS(temp));
37702ac6454SAndrew Thompson 					}
37802ac6454SAndrew Thompson #endif
37902ac6454SAndrew Thompson 					xfer->flags.manual_status = 0;
38002ac6454SAndrew Thompson 				} else {
38102ac6454SAndrew Thompson 					break;
38202ac6454SAndrew Thompson 				}
38302ac6454SAndrew Thompson 			}
38402ac6454SAndrew Thompson 			xfer->nframes = 1;
38502ac6454SAndrew Thompson 		}
38602ac6454SAndrew Thompson 
38702ac6454SAndrew Thompson 		usb2_transfer_start(xfer);
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 		while (usb2_transfer_pending(xfer)) {
39002ac6454SAndrew Thompson 			usb2_cv_wait(udev->default_cv,
39102ac6454SAndrew Thompson 			    xfer->xroot->xfer_mtx);
39202ac6454SAndrew Thompson 		}
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 		err = xfer->error;
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 		if (err) {
39702ac6454SAndrew Thompson 			break;
39802ac6454SAndrew Thompson 		}
39902ac6454SAndrew Thompson 		/* subtract length of SETUP packet, if any */
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson 		if (xfer->aframes > 0) {
40202ac6454SAndrew Thompson 			xfer->actlen -= xfer->frlengths[0];
40302ac6454SAndrew Thompson 		} else {
40402ac6454SAndrew Thompson 			xfer->actlen = 0;
40502ac6454SAndrew Thompson 		}
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 		/* check for short packet */
40802ac6454SAndrew Thompson 
40902ac6454SAndrew Thompson 		if (temp > xfer->actlen) {
41002ac6454SAndrew Thompson 			temp = xfer->actlen;
41102ac6454SAndrew Thompson 			if (!(flags & USB_SHORT_XFER_OK)) {
41202ac6454SAndrew Thompson 				err = USB_ERR_SHORT_XFER;
41302ac6454SAndrew Thompson 			}
41402ac6454SAndrew Thompson 			length = temp;
41502ac6454SAndrew Thompson 		}
41602ac6454SAndrew Thompson 		if (temp > 0) {
41702ac6454SAndrew Thompson 			if (req->bmRequestType & UT_READ) {
418bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
41902ac6454SAndrew Thompson 				if (flags & USB_USER_DATA_PTR) {
42002ac6454SAndrew Thompson 					USB_XFER_UNLOCK(xfer);
42102ac6454SAndrew Thompson 					err = usb2_copy_out_user(xfer->frbuffers + 1,
42202ac6454SAndrew Thompson 					    0, data, temp);
42302ac6454SAndrew Thompson 					USB_XFER_LOCK(xfer);
42402ac6454SAndrew Thompson 					if (err) {
42502ac6454SAndrew Thompson 						err = USB_ERR_INVAL;
42602ac6454SAndrew Thompson 						break;
42702ac6454SAndrew Thompson 					}
428bdc081c6SAndrew Thompson 				} else
429bdc081c6SAndrew Thompson #endif
43002ac6454SAndrew Thompson 					usb2_copy_out(xfer->frbuffers + 1,
43102ac6454SAndrew Thompson 					    0, data, temp);
43202ac6454SAndrew Thompson 			}
43302ac6454SAndrew Thompson 		}
43402ac6454SAndrew Thompson 		/*
43502ac6454SAndrew Thompson 		 * Clear "frlengths[0]" so that we don't send the setup
43602ac6454SAndrew Thompson 		 * packet again:
43702ac6454SAndrew Thompson 		 */
43802ac6454SAndrew Thompson 		xfer->frlengths[0] = 0;
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 		/* update length and data pointer */
44102ac6454SAndrew Thompson 		length -= temp;
44202ac6454SAndrew Thompson 		data = USB_ADD_BYTES(data, temp);
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 		if (actlen) {
44502ac6454SAndrew Thompson 			(*actlen) += temp;
44602ac6454SAndrew Thompson 		}
44702ac6454SAndrew Thompson 		/* check for timeout */
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 		delta_ticks = ticks - start_ticks;
45002ac6454SAndrew Thompson 		if (delta_ticks > max_ticks) {
45102ac6454SAndrew Thompson 			if (!err) {
45202ac6454SAndrew Thompson 				err = USB_ERR_TIMEOUT;
45302ac6454SAndrew Thompson 			}
45402ac6454SAndrew Thompson 		}
45502ac6454SAndrew Thompson 		if (err) {
45602ac6454SAndrew Thompson 			break;
45702ac6454SAndrew Thompson 		}
45802ac6454SAndrew Thompson 	}
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson 	if (err) {
46102ac6454SAndrew Thompson 		/*
46202ac6454SAndrew Thompson 		 * Make sure that the control endpoint is no longer
46302ac6454SAndrew Thompson 		 * blocked in case of a non-transfer related error:
46402ac6454SAndrew Thompson 		 */
46502ac6454SAndrew Thompson 		usb2_transfer_stop(xfer);
46602ac6454SAndrew Thompson 	}
46702ac6454SAndrew Thompson 	USB_XFER_UNLOCK(xfer);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson done:
47002ac6454SAndrew Thompson 	sx_xunlock(udev->default_sx);
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	if (mtx) {
47302ac6454SAndrew Thompson 		mtx_lock(mtx);
47402ac6454SAndrew Thompson 	}
47502ac6454SAndrew Thompson 	return ((usb2_error_t)err);
47602ac6454SAndrew Thompson }
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
47902ac6454SAndrew Thompson  *	usb2_do_request_proc - factored out code
48002ac6454SAndrew Thompson  *
48102ac6454SAndrew Thompson  * This function is factored out code. It does basically the same like
48202ac6454SAndrew Thompson  * usb2_do_request_flags, except it will check the status of the
48302ac6454SAndrew Thompson  * passed process argument before doing the USB request. If the
48402ac6454SAndrew Thompson  * process is draining the USB_ERR_IOERROR code will be returned. It
48502ac6454SAndrew Thompson  * is assumed that the mutex associated with the process is locked
48602ac6454SAndrew Thompson  * when calling this function.
48702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
48802ac6454SAndrew Thompson usb2_error_t
48902ac6454SAndrew Thompson usb2_do_request_proc(struct usb2_device *udev, struct usb2_process *pproc,
49002ac6454SAndrew Thompson     struct usb2_device_request *req, void *data, uint32_t flags,
49102ac6454SAndrew Thompson     uint16_t *actlen, uint32_t timeout)
49202ac6454SAndrew Thompson {
49302ac6454SAndrew Thompson 	usb2_error_t err;
49402ac6454SAndrew Thompson 	uint16_t len;
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 	/* get request data length */
49702ac6454SAndrew Thompson 	len = UGETW(req->wLength);
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson 	/* check if the device is being detached */
50002ac6454SAndrew Thompson 	if (usb2_proc_is_gone(pproc)) {
50102ac6454SAndrew Thompson 		err = USB_ERR_IOERROR;
50202ac6454SAndrew Thompson 		goto done;
50302ac6454SAndrew Thompson 	}
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson 	/* forward the USB request */
50602ac6454SAndrew Thompson 	err = usb2_do_request_flags(udev, pproc->up_mtx,
50702ac6454SAndrew Thompson 	    req, data, flags, actlen, timeout);
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson done:
51002ac6454SAndrew Thompson 	/* on failure we zero the data */
51102ac6454SAndrew Thompson 	/* on short packet we zero the unused data */
51202ac6454SAndrew Thompson 	if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
51302ac6454SAndrew Thompson 		if (err)
51402ac6454SAndrew Thompson 			memset(data, 0, len);
51502ac6454SAndrew Thompson 		else if (actlen && *actlen != len)
51602ac6454SAndrew Thompson 			memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
51702ac6454SAndrew Thompson 	}
51802ac6454SAndrew Thompson 	return (err);
51902ac6454SAndrew Thompson }
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
52202ac6454SAndrew Thompson  *	usb2_req_reset_port
52302ac6454SAndrew Thompson  *
52402ac6454SAndrew Thompson  * This function will instruct an USB HUB to perform a reset sequence
52502ac6454SAndrew Thompson  * on the specified port number.
52602ac6454SAndrew Thompson  *
52702ac6454SAndrew Thompson  * Returns:
52802ac6454SAndrew Thompson  *    0: Success. The USB device should now be at address zero.
52902ac6454SAndrew Thompson  * Else: Failure. No USB device is present and the USB port should be
53002ac6454SAndrew Thompson  *       disabled.
53102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
53202ac6454SAndrew Thompson usb2_error_t
53302ac6454SAndrew Thompson usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port)
53402ac6454SAndrew Thompson {
53502ac6454SAndrew Thompson 	struct usb2_port_status ps;
53602ac6454SAndrew Thompson 	usb2_error_t err;
53702ac6454SAndrew Thompson 	uint16_t n;
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson #if USB_DEBUG
54002ac6454SAndrew Thompson 	uint16_t pr_poll_delay;
54102ac6454SAndrew Thompson 	uint16_t pr_recovery_delay;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson #endif
54402ac6454SAndrew Thompson 	err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
54502ac6454SAndrew Thompson 	if (err) {
54602ac6454SAndrew Thompson 		goto done;
54702ac6454SAndrew Thompson 	}
54802ac6454SAndrew Thompson #if USB_DEBUG
54902ac6454SAndrew Thompson 	/* range check input parameters */
55002ac6454SAndrew Thompson 	pr_poll_delay = usb2_pr_poll_delay;
55102ac6454SAndrew Thompson 	if (pr_poll_delay < 1) {
55202ac6454SAndrew Thompson 		pr_poll_delay = 1;
55302ac6454SAndrew Thompson 	} else if (pr_poll_delay > 1000) {
55402ac6454SAndrew Thompson 		pr_poll_delay = 1000;
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson 	pr_recovery_delay = usb2_pr_recovery_delay;
55702ac6454SAndrew Thompson 	if (pr_recovery_delay > 1000) {
55802ac6454SAndrew Thompson 		pr_recovery_delay = 1000;
55902ac6454SAndrew Thompson 	}
56002ac6454SAndrew Thompson #endif
56102ac6454SAndrew Thompson 	n = 0;
56202ac6454SAndrew Thompson 	while (1) {
56302ac6454SAndrew Thompson #if USB_DEBUG
56402ac6454SAndrew Thompson 		/* wait for the device to recover from reset */
56502ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
56602ac6454SAndrew Thompson 		n += pr_poll_delay;
56702ac6454SAndrew Thompson #else
56802ac6454SAndrew Thompson 		/* wait for the device to recover from reset */
56902ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
57002ac6454SAndrew Thompson 		n += USB_PORT_RESET_DELAY;
57102ac6454SAndrew Thompson #endif
57202ac6454SAndrew Thompson 		err = usb2_req_get_port_status(udev, mtx, &ps, port);
57302ac6454SAndrew Thompson 		if (err) {
57402ac6454SAndrew Thompson 			goto done;
57502ac6454SAndrew Thompson 		}
57602ac6454SAndrew Thompson 		/* if the device disappeared, just give up */
57702ac6454SAndrew Thompson 		if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
57802ac6454SAndrew Thompson 			goto done;
57902ac6454SAndrew Thompson 		}
58002ac6454SAndrew Thompson 		/* check if reset is complete */
58102ac6454SAndrew Thompson 		if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
58202ac6454SAndrew Thompson 			break;
58302ac6454SAndrew Thompson 		}
58402ac6454SAndrew Thompson 		/* check for timeout */
58502ac6454SAndrew Thompson 		if (n > 1000) {
58602ac6454SAndrew Thompson 			n = 0;
58702ac6454SAndrew Thompson 			break;
58802ac6454SAndrew Thompson 		}
58902ac6454SAndrew Thompson 	}
59002ac6454SAndrew Thompson 
59102ac6454SAndrew Thompson 	/* clear port reset first */
59202ac6454SAndrew Thompson 	err = usb2_req_clear_port_feature(
59302ac6454SAndrew Thompson 	    udev, mtx, port, UHF_C_PORT_RESET);
59402ac6454SAndrew Thompson 	if (err) {
59502ac6454SAndrew Thompson 		goto done;
59602ac6454SAndrew Thompson 	}
59702ac6454SAndrew Thompson 	/* check for timeout */
59802ac6454SAndrew Thompson 	if (n == 0) {
59902ac6454SAndrew Thompson 		err = USB_ERR_TIMEOUT;
60002ac6454SAndrew Thompson 		goto done;
60102ac6454SAndrew Thompson 	}
60202ac6454SAndrew Thompson #if USB_DEBUG
60302ac6454SAndrew Thompson 	/* wait for the device to recover from reset */
60402ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
60502ac6454SAndrew Thompson #else
60602ac6454SAndrew Thompson 	/* wait for the device to recover from reset */
60702ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
60802ac6454SAndrew Thompson #endif
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson done:
61102ac6454SAndrew Thompson 	DPRINTFN(2, "port %d reset returning error=%s\n",
61202ac6454SAndrew Thompson 	    port, usb2_errstr(err));
61302ac6454SAndrew Thompson 	return (err);
61402ac6454SAndrew Thompson }
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
61702ac6454SAndrew Thompson  *	usb2_req_get_desc
61802ac6454SAndrew Thompson  *
61902ac6454SAndrew Thompson  * This function can be used to retrieve USB descriptors. It contains
62002ac6454SAndrew Thompson  * some additional logic like zeroing of missing descriptor bytes and
62102ac6454SAndrew Thompson  * retrying an USB descriptor in case of failure. The "min_len"
62202ac6454SAndrew Thompson  * argument specifies the minimum descriptor length. The "max_len"
62302ac6454SAndrew Thompson  * argument specifies the maximum descriptor length. If the real
62402ac6454SAndrew Thompson  * descriptor length is less than the minimum length the missing
62516589beaSAndrew Thompson  * byte(s) will be zeroed. The type field, the second byte of the USB
62616589beaSAndrew Thompson  * descriptor, will get forced to the correct type. If the "actlen"
62716589beaSAndrew Thompson  * pointer is non-NULL, the actual length of the transfer will get
62816589beaSAndrew Thompson  * stored in the 16-bit unsigned integer which it is pointing to. The
62916589beaSAndrew Thompson  * first byte of the descriptor will not get updated. If the "actlen"
63016589beaSAndrew Thompson  * pointer is NULL the first byte of the descriptor will get updated
63116589beaSAndrew Thompson  * to reflect the actual length instead. If "min_len" is not equal to
63216589beaSAndrew Thompson  * "max_len" then this function will try to retrive the beginning of
63316589beaSAndrew Thompson  * the descriptor and base the maximum length on the first byte of the
63416589beaSAndrew Thompson  * descriptor.
63502ac6454SAndrew Thompson  *
63602ac6454SAndrew Thompson  * Returns:
63702ac6454SAndrew Thompson  *    0: Success
63802ac6454SAndrew Thompson  * Else: Failure
63902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
64002ac6454SAndrew Thompson usb2_error_t
64116589beaSAndrew Thompson usb2_req_get_desc(struct usb2_device *udev,
64216589beaSAndrew Thompson     struct mtx *mtx, uint16_t *actlen, void *desc,
64302ac6454SAndrew Thompson     uint16_t min_len, uint16_t max_len,
64402ac6454SAndrew Thompson     uint16_t id, uint8_t type, uint8_t index,
64502ac6454SAndrew Thompson     uint8_t retries)
64602ac6454SAndrew Thompson {
64702ac6454SAndrew Thompson 	struct usb2_device_request req;
64802ac6454SAndrew Thompson 	uint8_t *buf;
64902ac6454SAndrew Thompson 	usb2_error_t err;
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
65202ac6454SAndrew Thompson 	    id, type, index, max_len);
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
65502ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
65602ac6454SAndrew Thompson 	USETW2(req.wValue, type, index);
65702ac6454SAndrew Thompson 	USETW(req.wIndex, id);
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson 	while (1) {
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 		if ((min_len < 2) || (max_len < 2)) {
66202ac6454SAndrew Thompson 			err = USB_ERR_INVAL;
66302ac6454SAndrew Thompson 			goto done;
66402ac6454SAndrew Thompson 		}
66502ac6454SAndrew Thompson 		USETW(req.wLength, min_len);
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 		err = usb2_do_request_flags(udev, mtx, &req,
66802ac6454SAndrew Thompson 		    desc, 0, NULL, 1000);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 		if (err) {
67102ac6454SAndrew Thompson 			if (!retries) {
67202ac6454SAndrew Thompson 				goto done;
67302ac6454SAndrew Thompson 			}
67402ac6454SAndrew Thompson 			retries--;
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 			usb2_pause_mtx(mtx, hz / 5);
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson 			continue;
67902ac6454SAndrew Thompson 		}
68002ac6454SAndrew Thompson 		buf = desc;
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson 		if (min_len == max_len) {
68302ac6454SAndrew Thompson 
68416589beaSAndrew Thompson 			/* enforce correct length */
68516589beaSAndrew Thompson 			if ((buf[0] > min_len) && (actlen == NULL))
68602ac6454SAndrew Thompson 				buf[0] = min_len;
68716589beaSAndrew Thompson 
68816589beaSAndrew Thompson 			/* enforce correct type */
68902ac6454SAndrew Thompson 			buf[1] = type;
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 			goto done;
69202ac6454SAndrew Thompson 		}
69302ac6454SAndrew Thompson 		/* range check */
69402ac6454SAndrew Thompson 
69502ac6454SAndrew Thompson 		if (max_len > buf[0]) {
69602ac6454SAndrew Thompson 			max_len = buf[0];
69702ac6454SAndrew Thompson 		}
69802ac6454SAndrew Thompson 		/* zero minimum data */
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 		while (min_len > max_len) {
70102ac6454SAndrew Thompson 			min_len--;
70202ac6454SAndrew Thompson 			buf[min_len] = 0;
70302ac6454SAndrew Thompson 		}
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 		/* set new minimum length */
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 		min_len = max_len;
70802ac6454SAndrew Thompson 	}
70902ac6454SAndrew Thompson done:
71016589beaSAndrew Thompson 	if (actlen != NULL) {
71116589beaSAndrew Thompson 		if (err)
71216589beaSAndrew Thompson 			*actlen = 0;
71316589beaSAndrew Thompson 		else
71416589beaSAndrew Thompson 			*actlen = min_len;
71516589beaSAndrew Thompson 	}
71602ac6454SAndrew Thompson 	return (err);
71702ac6454SAndrew Thompson }
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
72002ac6454SAndrew Thompson  *	usb2_req_get_string_any
72102ac6454SAndrew Thompson  *
72202ac6454SAndrew Thompson  * This function will return the string given by "string_index"
72302ac6454SAndrew Thompson  * using the first language ID. The maximum length "len" includes
72402ac6454SAndrew Thompson  * the terminating zero. The "len" argument should be twice as
72502ac6454SAndrew Thompson  * big pluss 2 bytes, compared with the actual maximum string length !
72602ac6454SAndrew Thompson  *
72702ac6454SAndrew Thompson  * Returns:
72802ac6454SAndrew Thompson  *    0: Success
72902ac6454SAndrew Thompson  * Else: Failure
73002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
73102ac6454SAndrew Thompson usb2_error_t
73202ac6454SAndrew Thompson usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf,
73302ac6454SAndrew Thompson     uint16_t len, uint8_t string_index)
73402ac6454SAndrew Thompson {
73502ac6454SAndrew Thompson 	char *s;
73602ac6454SAndrew Thompson 	uint8_t *temp;
73702ac6454SAndrew Thompson 	uint16_t i;
73802ac6454SAndrew Thompson 	uint16_t n;
73902ac6454SAndrew Thompson 	uint16_t c;
74002ac6454SAndrew Thompson 	uint8_t swap;
74102ac6454SAndrew Thompson 	usb2_error_t err;
74202ac6454SAndrew Thompson 
74302ac6454SAndrew Thompson 	if (len == 0) {
74402ac6454SAndrew Thompson 		/* should not happen */
74502ac6454SAndrew Thompson 		return (USB_ERR_NORMAL_COMPLETION);
74602ac6454SAndrew Thompson 	}
74702ac6454SAndrew Thompson 	if (string_index == 0) {
74802ac6454SAndrew Thompson 		/* this is the language table */
74902ac6454SAndrew Thompson 		buf[0] = 0;
75002ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
75102ac6454SAndrew Thompson 	}
75202ac6454SAndrew Thompson 	if (udev->flags.no_strings) {
75302ac6454SAndrew Thompson 		buf[0] = 0;
75402ac6454SAndrew Thompson 		return (USB_ERR_STALLED);
75502ac6454SAndrew Thompson 	}
75602ac6454SAndrew Thompson 	err = usb2_req_get_string_desc
75702ac6454SAndrew Thompson 	    (udev, mtx, buf, len, udev->langid, string_index);
75802ac6454SAndrew Thompson 	if (err) {
75902ac6454SAndrew Thompson 		buf[0] = 0;
76002ac6454SAndrew Thompson 		return (err);
76102ac6454SAndrew Thompson 	}
76202ac6454SAndrew Thompson 	temp = (uint8_t *)buf;
76302ac6454SAndrew Thompson 
76402ac6454SAndrew Thompson 	if (temp[0] < 2) {
76502ac6454SAndrew Thompson 		/* string length is too short */
76602ac6454SAndrew Thompson 		buf[0] = 0;
76702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
76802ac6454SAndrew Thompson 	}
76902ac6454SAndrew Thompson 	/* reserve one byte for terminating zero */
77002ac6454SAndrew Thompson 	len--;
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 	/* find maximum length */
77302ac6454SAndrew Thompson 	s = buf;
77402ac6454SAndrew Thompson 	n = (temp[0] / 2) - 1;
77502ac6454SAndrew Thompson 	if (n > len) {
77602ac6454SAndrew Thompson 		n = len;
77702ac6454SAndrew Thompson 	}
77802ac6454SAndrew Thompson 	/* skip descriptor header */
77902ac6454SAndrew Thompson 	temp += 2;
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 	/* reset swap state */
78202ac6454SAndrew Thompson 	swap = 3;
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	/* convert and filter */
78502ac6454SAndrew Thompson 	for (i = 0; (i != n); i++) {
78602ac6454SAndrew Thompson 		c = UGETW(temp + (2 * i));
78702ac6454SAndrew Thompson 
78802ac6454SAndrew Thompson 		/* convert from Unicode, handle buggy strings */
78902ac6454SAndrew Thompson 		if (((c & 0xff00) == 0) && (swap & 1)) {
79002ac6454SAndrew Thompson 			/* Little Endian, default */
79102ac6454SAndrew Thompson 			*s = c;
79202ac6454SAndrew Thompson 			swap = 1;
79302ac6454SAndrew Thompson 		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
79402ac6454SAndrew Thompson 			/* Big Endian */
79502ac6454SAndrew Thompson 			*s = c >> 8;
79602ac6454SAndrew Thompson 			swap = 2;
79702ac6454SAndrew Thompson 		} else {
79802ac6454SAndrew Thompson 			/* silently skip bad character */
79902ac6454SAndrew Thompson 			continue;
80002ac6454SAndrew Thompson 		}
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 		/*
80302ac6454SAndrew Thompson 		 * Filter by default - we don't allow greater and less than
80402ac6454SAndrew Thompson 		 * signs because they might confuse the dmesg printouts!
80502ac6454SAndrew Thompson 		 */
80602ac6454SAndrew Thompson 		if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
80702ac6454SAndrew Thompson 			/* silently skip bad character */
80802ac6454SAndrew Thompson 			continue;
80902ac6454SAndrew Thompson 		}
81002ac6454SAndrew Thompson 		s++;
81102ac6454SAndrew Thompson 	}
81202ac6454SAndrew Thompson 	*s = 0;				/* zero terminate resulting string */
81302ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
81402ac6454SAndrew Thompson }
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
81702ac6454SAndrew Thompson  *	usb2_req_get_string_desc
81802ac6454SAndrew Thompson  *
81902ac6454SAndrew Thompson  * If you don't know the language ID, consider using
82002ac6454SAndrew Thompson  * "usb2_req_get_string_any()".
82102ac6454SAndrew Thompson  *
82202ac6454SAndrew Thompson  * Returns:
82302ac6454SAndrew Thompson  *    0: Success
82402ac6454SAndrew Thompson  * Else: Failure
82502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
82602ac6454SAndrew Thompson usb2_error_t
82702ac6454SAndrew Thompson usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc,
82802ac6454SAndrew Thompson     uint16_t max_len, uint16_t lang_id,
82902ac6454SAndrew Thompson     uint8_t string_index)
83002ac6454SAndrew Thompson {
83116589beaSAndrew Thompson 	return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
83202ac6454SAndrew Thompson 	    UDESC_STRING, string_index, 0));
83302ac6454SAndrew Thompson }
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
83602ac6454SAndrew Thompson  *	usb2_req_get_config_desc
83702ac6454SAndrew Thompson  *
83802ac6454SAndrew Thompson  * Returns:
83902ac6454SAndrew Thompson  *    0: Success
84002ac6454SAndrew Thompson  * Else: Failure
84102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
84202ac6454SAndrew Thompson usb2_error_t
84302ac6454SAndrew Thompson usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx,
84402ac6454SAndrew Thompson     struct usb2_config_descriptor *d, uint8_t conf_index)
84502ac6454SAndrew Thompson {
84602ac6454SAndrew Thompson 	usb2_error_t err;
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	DPRINTFN(4, "confidx=%d\n", conf_index);
84902ac6454SAndrew Thompson 
85016589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
85102ac6454SAndrew Thompson 	    sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
85202ac6454SAndrew Thompson 	if (err) {
85302ac6454SAndrew Thompson 		goto done;
85402ac6454SAndrew Thompson 	}
85502ac6454SAndrew Thompson 	/* Extra sanity checking */
85602ac6454SAndrew Thompson 	if (UGETW(d->wTotalLength) < sizeof(*d)) {
85702ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
85802ac6454SAndrew Thompson 	}
85902ac6454SAndrew Thompson done:
86002ac6454SAndrew Thompson 	return (err);
86102ac6454SAndrew Thompson }
86202ac6454SAndrew Thompson 
86302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
86402ac6454SAndrew Thompson  *	usb2_req_get_config_desc_full
86502ac6454SAndrew Thompson  *
86602ac6454SAndrew Thompson  * This function gets the complete USB configuration descriptor and
86702ac6454SAndrew Thompson  * ensures that "wTotalLength" is correct.
86802ac6454SAndrew Thompson  *
86902ac6454SAndrew Thompson  * Returns:
87002ac6454SAndrew Thompson  *    0: Success
87102ac6454SAndrew Thompson  * Else: Failure
87202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
87302ac6454SAndrew Thompson usb2_error_t
87402ac6454SAndrew Thompson usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx,
87502ac6454SAndrew Thompson     struct usb2_config_descriptor **ppcd, struct malloc_type *mtype,
87602ac6454SAndrew Thompson     uint8_t index)
87702ac6454SAndrew Thompson {
87802ac6454SAndrew Thompson 	struct usb2_config_descriptor cd;
87902ac6454SAndrew Thompson 	struct usb2_config_descriptor *cdesc;
88002ac6454SAndrew Thompson 	uint16_t len;
88102ac6454SAndrew Thompson 	usb2_error_t err;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	DPRINTFN(4, "index=%d\n", index);
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 	*ppcd = NULL;
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 	err = usb2_req_get_config_desc(udev, mtx, &cd, index);
88802ac6454SAndrew Thompson 	if (err) {
88902ac6454SAndrew Thompson 		return (err);
89002ac6454SAndrew Thompson 	}
89102ac6454SAndrew Thompson 	/* get full descriptor */
89202ac6454SAndrew Thompson 	len = UGETW(cd.wTotalLength);
89302ac6454SAndrew Thompson 	if (len < sizeof(*cdesc)) {
89402ac6454SAndrew Thompson 		/* corrupt descriptor */
89502ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
89602ac6454SAndrew Thompson 	}
89702ac6454SAndrew Thompson 	cdesc = malloc(len, mtype, M_WAITOK);
89802ac6454SAndrew Thompson 	if (cdesc == NULL) {
89902ac6454SAndrew Thompson 		return (USB_ERR_NOMEM);
90002ac6454SAndrew Thompson 	}
90116589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
90202ac6454SAndrew Thompson 	    UDESC_CONFIG, index, 3);
90302ac6454SAndrew Thompson 	if (err) {
90402ac6454SAndrew Thompson 		free(cdesc, mtype);
90502ac6454SAndrew Thompson 		return (err);
90602ac6454SAndrew Thompson 	}
90702ac6454SAndrew Thompson 	/* make sure that the device is not fooling us: */
90802ac6454SAndrew Thompson 	USETW(cdesc->wTotalLength, len);
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson 	*ppcd = cdesc;
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson 	return (0);			/* success */
91302ac6454SAndrew Thompson }
91402ac6454SAndrew Thompson 
91502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
91602ac6454SAndrew Thompson  *	usb2_req_get_device_desc
91702ac6454SAndrew Thompson  *
91802ac6454SAndrew Thompson  * Returns:
91902ac6454SAndrew Thompson  *    0: Success
92002ac6454SAndrew Thompson  * Else: Failure
92102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
92202ac6454SAndrew Thompson usb2_error_t
92302ac6454SAndrew Thompson usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx,
92402ac6454SAndrew Thompson     struct usb2_device_descriptor *d)
92502ac6454SAndrew Thompson {
92602ac6454SAndrew Thompson 	DPRINTFN(4, "\n");
92716589beaSAndrew Thompson 	return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
92802ac6454SAndrew Thompson 	    sizeof(*d), 0, UDESC_DEVICE, 0, 3));
92902ac6454SAndrew Thompson }
93002ac6454SAndrew Thompson 
93102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
93202ac6454SAndrew Thompson  *	usb2_req_get_alt_interface_no
93302ac6454SAndrew Thompson  *
93402ac6454SAndrew Thompson  * Returns:
93502ac6454SAndrew Thompson  *    0: Success
93602ac6454SAndrew Thompson  * Else: Failure
93702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
93802ac6454SAndrew Thompson usb2_error_t
93902ac6454SAndrew Thompson usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
94002ac6454SAndrew Thompson     uint8_t *alt_iface_no, uint8_t iface_index)
94102ac6454SAndrew Thompson {
94202ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
94302ac6454SAndrew Thompson 	struct usb2_device_request req;
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
94602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
94702ac6454SAndrew Thompson 	}
94802ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_INTERFACE;
94902ac6454SAndrew Thompson 	req.bRequest = UR_GET_INTERFACE;
95002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
95102ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
95202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
95302ac6454SAndrew Thompson 	USETW(req.wLength, 1);
95402ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, alt_iface_no));
95502ac6454SAndrew Thompson }
95602ac6454SAndrew Thompson 
95702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
95802ac6454SAndrew Thompson  *	usb2_req_set_alt_interface_no
95902ac6454SAndrew Thompson  *
96002ac6454SAndrew Thompson  * Returns:
96102ac6454SAndrew Thompson  *    0: Success
96202ac6454SAndrew Thompson  * Else: Failure
96302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
96402ac6454SAndrew Thompson usb2_error_t
96502ac6454SAndrew Thompson usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
96602ac6454SAndrew Thompson     uint8_t iface_index, uint8_t alt_no)
96702ac6454SAndrew Thompson {
96802ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
96902ac6454SAndrew Thompson 	struct usb2_device_request req;
97002ac6454SAndrew Thompson 
97102ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
97202ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
97302ac6454SAndrew Thompson 	}
97402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_INTERFACE;
97502ac6454SAndrew Thompson 	req.bRequest = UR_SET_INTERFACE;
97602ac6454SAndrew Thompson 	req.wValue[0] = alt_no;
97702ac6454SAndrew Thompson 	req.wValue[1] = 0;
97802ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
97902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
98002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
98102ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
98202ac6454SAndrew Thompson }
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
98502ac6454SAndrew Thompson  *	usb2_req_get_device_status
98602ac6454SAndrew Thompson  *
98702ac6454SAndrew Thompson  * Returns:
98802ac6454SAndrew Thompson  *    0: Success
98902ac6454SAndrew Thompson  * Else: Failure
99002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
99102ac6454SAndrew Thompson usb2_error_t
99202ac6454SAndrew Thompson usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx,
99302ac6454SAndrew Thompson     struct usb2_status *st)
99402ac6454SAndrew Thompson {
99502ac6454SAndrew Thompson 	struct usb2_device_request req;
99602ac6454SAndrew Thompson 
99702ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
99802ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
99902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
100002ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
100102ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(*st));
100202ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, st));
100302ac6454SAndrew Thompson }
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
100602ac6454SAndrew Thompson  *	usb2_req_get_hub_descriptor
100702ac6454SAndrew Thompson  *
100802ac6454SAndrew Thompson  * Returns:
100902ac6454SAndrew Thompson  *    0: Success
101002ac6454SAndrew Thompson  * Else: Failure
101102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
101202ac6454SAndrew Thompson usb2_error_t
101302ac6454SAndrew Thompson usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx,
101402ac6454SAndrew Thompson     struct usb2_hub_descriptor *hd, uint8_t nports)
101502ac6454SAndrew Thompson {
101602ac6454SAndrew Thompson 	struct usb2_device_request req;
101702ac6454SAndrew Thompson 	uint16_t len = (nports + 7 + (8 * 8)) / 8;
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_DEVICE;
102002ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
102102ac6454SAndrew Thompson 	USETW2(req.wValue, UDESC_HUB, 0);
102202ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
102302ac6454SAndrew Thompson 	USETW(req.wLength, len);
102402ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, hd));
102502ac6454SAndrew Thompson }
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
102802ac6454SAndrew Thompson  *	usb2_req_get_hub_status
102902ac6454SAndrew Thompson  *
103002ac6454SAndrew Thompson  * Returns:
103102ac6454SAndrew Thompson  *    0: Success
103202ac6454SAndrew Thompson  * Else: Failure
103302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
103402ac6454SAndrew Thompson usb2_error_t
103502ac6454SAndrew Thompson usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx,
103602ac6454SAndrew Thompson     struct usb2_hub_status *st)
103702ac6454SAndrew Thompson {
103802ac6454SAndrew Thompson 	struct usb2_device_request req;
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_DEVICE;
104102ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
104202ac6454SAndrew Thompson 	USETW(req.wValue, 0);
104302ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
104402ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(struct usb2_hub_status));
104502ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, st));
104602ac6454SAndrew Thompson }
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
104902ac6454SAndrew Thompson  *	usb2_req_set_address
105002ac6454SAndrew Thompson  *
105102ac6454SAndrew Thompson  * This function is used to set the address for an USB device. After
105202ac6454SAndrew Thompson  * port reset the USB device will respond at address zero.
105302ac6454SAndrew Thompson  *
105402ac6454SAndrew Thompson  * Returns:
105502ac6454SAndrew Thompson  *    0: Success
105602ac6454SAndrew Thompson  * Else: Failure
105702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
105802ac6454SAndrew Thompson usb2_error_t
105902ac6454SAndrew Thompson usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr)
106002ac6454SAndrew Thompson {
106102ac6454SAndrew Thompson 	struct usb2_device_request req;
106202ac6454SAndrew Thompson 
106302ac6454SAndrew Thompson 	DPRINTFN(6, "setting device address=%d\n", addr);
106402ac6454SAndrew Thompson 
106502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
106602ac6454SAndrew Thompson 	req.bRequest = UR_SET_ADDRESS;
106702ac6454SAndrew Thompson 	USETW(req.wValue, addr);
106802ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
106902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
107002ac6454SAndrew Thompson 
107102ac6454SAndrew Thompson 	/* Setting the address should not take more than 1 second ! */
107202ac6454SAndrew Thompson 	return (usb2_do_request_flags(udev, mtx, &req, NULL,
107302ac6454SAndrew Thompson 	    USB_DELAY_STATUS_STAGE, NULL, 1000));
107402ac6454SAndrew Thompson }
107502ac6454SAndrew Thompson 
107602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
107702ac6454SAndrew Thompson  *	usb2_req_get_port_status
107802ac6454SAndrew Thompson  *
107902ac6454SAndrew Thompson  * Returns:
108002ac6454SAndrew Thompson  *    0: Success
108102ac6454SAndrew Thompson  * Else: Failure
108202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108302ac6454SAndrew Thompson usb2_error_t
108402ac6454SAndrew Thompson usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx,
108502ac6454SAndrew Thompson     struct usb2_port_status *ps, uint8_t port)
108602ac6454SAndrew Thompson {
108702ac6454SAndrew Thompson 	struct usb2_device_request req;
108802ac6454SAndrew Thompson 
108902ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_OTHER;
109002ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
109102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
109202ac6454SAndrew Thompson 	req.wIndex[0] = port;
109302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
109402ac6454SAndrew Thompson 	USETW(req.wLength, sizeof *ps);
109502ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, ps));
109602ac6454SAndrew Thompson }
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
109902ac6454SAndrew Thompson  *	usb2_req_clear_hub_feature
110002ac6454SAndrew Thompson  *
110102ac6454SAndrew Thompson  * Returns:
110202ac6454SAndrew Thompson  *    0: Success
110302ac6454SAndrew Thompson  * Else: Failure
110402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
110502ac6454SAndrew Thompson usb2_error_t
110602ac6454SAndrew Thompson usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx,
110702ac6454SAndrew Thompson     uint16_t sel)
110802ac6454SAndrew Thompson {
110902ac6454SAndrew Thompson 	struct usb2_device_request req;
111002ac6454SAndrew Thompson 
111102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
111202ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
111302ac6454SAndrew Thompson 	USETW(req.wValue, sel);
111402ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
111502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
111602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
111702ac6454SAndrew Thompson }
111802ac6454SAndrew Thompson 
111902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
112002ac6454SAndrew Thompson  *	usb2_req_set_hub_feature
112102ac6454SAndrew Thompson  *
112202ac6454SAndrew Thompson  * Returns:
112302ac6454SAndrew Thompson  *    0: Success
112402ac6454SAndrew Thompson  * Else: Failure
112502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
112602ac6454SAndrew Thompson usb2_error_t
112702ac6454SAndrew Thompson usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx,
112802ac6454SAndrew Thompson     uint16_t sel)
112902ac6454SAndrew Thompson {
113002ac6454SAndrew Thompson 	struct usb2_device_request req;
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
113302ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
113402ac6454SAndrew Thompson 	USETW(req.wValue, sel);
113502ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
113602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
113702ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
113802ac6454SAndrew Thompson }
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
114102ac6454SAndrew Thompson  *	usb2_req_clear_port_feature
114202ac6454SAndrew Thompson  *
114302ac6454SAndrew Thompson  * Returns:
114402ac6454SAndrew Thompson  *    0: Success
114502ac6454SAndrew Thompson  * Else: Failure
114602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
114702ac6454SAndrew Thompson usb2_error_t
114802ac6454SAndrew Thompson usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx,
114902ac6454SAndrew Thompson     uint8_t port, uint16_t sel)
115002ac6454SAndrew Thompson {
115102ac6454SAndrew Thompson 	struct usb2_device_request req;
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
115402ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
115502ac6454SAndrew Thompson 	USETW(req.wValue, sel);
115602ac6454SAndrew Thompson 	req.wIndex[0] = port;
115702ac6454SAndrew Thompson 	req.wIndex[1] = 0;
115802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
115902ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
116002ac6454SAndrew Thompson }
116102ac6454SAndrew Thompson 
116202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
116302ac6454SAndrew Thompson  *	usb2_req_set_port_feature
116402ac6454SAndrew Thompson  *
116502ac6454SAndrew Thompson  * Returns:
116602ac6454SAndrew Thompson  *    0: Success
116702ac6454SAndrew Thompson  * Else: Failure
116802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
116902ac6454SAndrew Thompson usb2_error_t
117002ac6454SAndrew Thompson usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx,
117102ac6454SAndrew Thompson     uint8_t port, uint16_t sel)
117202ac6454SAndrew Thompson {
117302ac6454SAndrew Thompson 	struct usb2_device_request req;
117402ac6454SAndrew Thompson 
117502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
117602ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
117702ac6454SAndrew Thompson 	USETW(req.wValue, sel);
117802ac6454SAndrew Thompson 	req.wIndex[0] = port;
117902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
118002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
118102ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
118202ac6454SAndrew Thompson }
118302ac6454SAndrew Thompson 
118402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
118502ac6454SAndrew Thompson  *	usb2_req_set_protocol
118602ac6454SAndrew Thompson  *
118702ac6454SAndrew Thompson  * Returns:
118802ac6454SAndrew Thompson  *    0: Success
118902ac6454SAndrew Thompson  * Else: Failure
119002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
119102ac6454SAndrew Thompson usb2_error_t
119202ac6454SAndrew Thompson usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx,
119302ac6454SAndrew Thompson     uint8_t iface_index, uint16_t report)
119402ac6454SAndrew Thompson {
119502ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
119602ac6454SAndrew Thompson 	struct usb2_device_request req;
119702ac6454SAndrew Thompson 
119802ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
119902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
120002ac6454SAndrew Thompson 	}
120102ac6454SAndrew Thompson 	DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
120202ac6454SAndrew Thompson 	    iface, report, iface->idesc->bInterfaceNumber);
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
120502ac6454SAndrew Thompson 	req.bRequest = UR_SET_PROTOCOL;
120602ac6454SAndrew Thompson 	USETW(req.wValue, report);
120702ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
120802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
120902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
121002ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
121102ac6454SAndrew Thompson }
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
121402ac6454SAndrew Thompson  *	usb2_req_set_report
121502ac6454SAndrew Thompson  *
121602ac6454SAndrew Thompson  * Returns:
121702ac6454SAndrew Thompson  *    0: Success
121802ac6454SAndrew Thompson  * Else: Failure
121902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
122002ac6454SAndrew Thompson usb2_error_t
122102ac6454SAndrew Thompson usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len,
122202ac6454SAndrew Thompson     uint8_t iface_index, uint8_t type, uint8_t id)
122302ac6454SAndrew Thompson {
122402ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
122502ac6454SAndrew Thompson 	struct usb2_device_request req;
122602ac6454SAndrew Thompson 
122702ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
122802ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
122902ac6454SAndrew Thompson 	}
123002ac6454SAndrew Thompson 	DPRINTFN(5, "len=%d\n", len);
123102ac6454SAndrew Thompson 
123202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
123302ac6454SAndrew Thompson 	req.bRequest = UR_SET_REPORT;
123402ac6454SAndrew Thompson 	USETW2(req.wValue, type, id);
123502ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
123602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
123702ac6454SAndrew Thompson 	USETW(req.wLength, len);
123802ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, data));
123902ac6454SAndrew Thompson }
124002ac6454SAndrew Thompson 
124102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
124202ac6454SAndrew Thompson  *	usb2_req_get_report
124302ac6454SAndrew Thompson  *
124402ac6454SAndrew Thompson  * Returns:
124502ac6454SAndrew Thompson  *    0: Success
124602ac6454SAndrew Thompson  * Else: Failure
124702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
124802ac6454SAndrew Thompson usb2_error_t
124902ac6454SAndrew Thompson usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data,
125002ac6454SAndrew Thompson     uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
125102ac6454SAndrew Thompson {
125202ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
125302ac6454SAndrew Thompson 	struct usb2_device_request req;
125402ac6454SAndrew Thompson 
125502ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
125602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
125702ac6454SAndrew Thompson 	}
125802ac6454SAndrew Thompson 	DPRINTFN(5, "len=%d\n", len);
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
126102ac6454SAndrew Thompson 	req.bRequest = UR_GET_REPORT;
126202ac6454SAndrew Thompson 	USETW2(req.wValue, type, id);
126302ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
126402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
126502ac6454SAndrew Thompson 	USETW(req.wLength, len);
126602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, data));
126702ac6454SAndrew Thompson }
126802ac6454SAndrew Thompson 
126902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
127002ac6454SAndrew Thompson  *	usb2_req_set_idle
127102ac6454SAndrew Thompson  *
127202ac6454SAndrew Thompson  * Returns:
127302ac6454SAndrew Thompson  *    0: Success
127402ac6454SAndrew Thompson  * Else: Failure
127502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
127602ac6454SAndrew Thompson usb2_error_t
127702ac6454SAndrew Thompson usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx,
127802ac6454SAndrew Thompson     uint8_t iface_index, uint8_t duration, uint8_t id)
127902ac6454SAndrew Thompson {
128002ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
128102ac6454SAndrew Thompson 	struct usb2_device_request req;
128202ac6454SAndrew Thompson 
128302ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
128402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
128502ac6454SAndrew Thompson 	}
128602ac6454SAndrew Thompson 	DPRINTFN(5, "%d %d\n", duration, id);
128702ac6454SAndrew Thompson 
128802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
128902ac6454SAndrew Thompson 	req.bRequest = UR_SET_IDLE;
129002ac6454SAndrew Thompson 	USETW2(req.wValue, duration, id);
129102ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
129202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
129302ac6454SAndrew Thompson 	USETW(req.wLength, 0);
129402ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
129502ac6454SAndrew Thompson }
129602ac6454SAndrew Thompson 
129702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
129802ac6454SAndrew Thompson  *	usb2_req_get_report_descriptor
129902ac6454SAndrew Thompson  *
130002ac6454SAndrew Thompson  * Returns:
130102ac6454SAndrew Thompson  *    0: Success
130202ac6454SAndrew Thompson  * Else: Failure
130302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
130402ac6454SAndrew Thompson usb2_error_t
130502ac6454SAndrew Thompson usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx,
130602ac6454SAndrew Thompson     void *d, uint16_t size, uint8_t iface_index)
130702ac6454SAndrew Thompson {
130802ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
130902ac6454SAndrew Thompson 	struct usb2_device_request req;
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
131202ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
131302ac6454SAndrew Thompson 	}
131402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_INTERFACE;
131502ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
131602ac6454SAndrew Thompson 	USETW2(req.wValue, UDESC_REPORT, 0);	/* report id should be 0 */
131702ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
131802ac6454SAndrew Thompson 	req.wIndex[1] = 0;
131902ac6454SAndrew Thompson 	USETW(req.wLength, size);
132002ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, d));
132102ac6454SAndrew Thompson }
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
132402ac6454SAndrew Thompson  *	usb2_req_set_config
132502ac6454SAndrew Thompson  *
132602ac6454SAndrew Thompson  * This function is used to select the current configuration number in
132702ac6454SAndrew Thompson  * both USB device side mode and USB host side mode. When setting the
132802ac6454SAndrew Thompson  * configuration the function of the interfaces can change.
132902ac6454SAndrew Thompson  *
133002ac6454SAndrew Thompson  * Returns:
133102ac6454SAndrew Thompson  *    0: Success
133202ac6454SAndrew Thompson  * Else: Failure
133302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
133402ac6454SAndrew Thompson usb2_error_t
133502ac6454SAndrew Thompson usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf)
133602ac6454SAndrew Thompson {
133702ac6454SAndrew Thompson 	struct usb2_device_request req;
133802ac6454SAndrew Thompson 
133902ac6454SAndrew Thompson 	DPRINTF("setting config %d\n", conf);
134002ac6454SAndrew Thompson 
134102ac6454SAndrew Thompson 	/* do "set configuration" request */
134202ac6454SAndrew Thompson 
134302ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
134402ac6454SAndrew Thompson 	req.bRequest = UR_SET_CONFIG;
134502ac6454SAndrew Thompson 	req.wValue[0] = conf;
134602ac6454SAndrew Thompson 	req.wValue[1] = 0;
134702ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
134802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
134902ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
135002ac6454SAndrew Thompson }
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
135302ac6454SAndrew Thompson  *	usb2_req_get_config
135402ac6454SAndrew Thompson  *
135502ac6454SAndrew Thompson  * Returns:
135602ac6454SAndrew Thompson  *    0: Success
135702ac6454SAndrew Thompson  * Else: Failure
135802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
135902ac6454SAndrew Thompson usb2_error_t
136002ac6454SAndrew Thompson usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf)
136102ac6454SAndrew Thompson {
136202ac6454SAndrew Thompson 	struct usb2_device_request req;
136302ac6454SAndrew Thompson 
136402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
136502ac6454SAndrew Thompson 	req.bRequest = UR_GET_CONFIG;
136602ac6454SAndrew Thompson 	USETW(req.wValue, 0);
136702ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
136802ac6454SAndrew Thompson 	USETW(req.wLength, 1);
136902ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, pconf));
137002ac6454SAndrew Thompson }
137102ac6454SAndrew Thompson 
137202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
137302ac6454SAndrew Thompson  *	usb2_req_re_enumerate
137402ac6454SAndrew Thompson  *
137502ac6454SAndrew Thompson  * NOTE: After this function returns the hardware is in the
137602ac6454SAndrew Thompson  * unconfigured state! The application is responsible for setting a
137702ac6454SAndrew Thompson  * new configuration.
137802ac6454SAndrew Thompson  *
137902ac6454SAndrew Thompson  * Returns:
138002ac6454SAndrew Thompson  *    0: Success
138102ac6454SAndrew Thompson  * Else: Failure
138202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
138302ac6454SAndrew Thompson usb2_error_t
138402ac6454SAndrew Thompson usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx)
138502ac6454SAndrew Thompson {
138602ac6454SAndrew Thompson 	struct usb2_device *parent_hub;
138702ac6454SAndrew Thompson 	usb2_error_t err;
138802ac6454SAndrew Thompson 	uint8_t old_addr;
138902ac6454SAndrew Thompson 	uint8_t do_retry = 1;
139002ac6454SAndrew Thompson 
139102ac6454SAndrew Thompson 	if (udev->flags.usb2_mode != USB_MODE_HOST) {
139202ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
139302ac6454SAndrew Thompson 	}
139402ac6454SAndrew Thompson 	old_addr = udev->address;
139502ac6454SAndrew Thompson 	parent_hub = udev->parent_hub;
139602ac6454SAndrew Thompson 	if (parent_hub == NULL) {
139702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
139802ac6454SAndrew Thompson 	}
139902ac6454SAndrew Thompson retry:
140002ac6454SAndrew Thompson 	err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
140102ac6454SAndrew Thompson 	if (err) {
140202ac6454SAndrew Thompson 		DPRINTFN(0, "addr=%d, port reset failed\n", old_addr);
140302ac6454SAndrew Thompson 		goto done;
140402ac6454SAndrew Thompson 	}
140502ac6454SAndrew Thompson 	/*
140602ac6454SAndrew Thompson 	 * After that the port has been reset our device should be at
140702ac6454SAndrew Thompson 	 * address zero:
140802ac6454SAndrew Thompson 	 */
140902ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;
141002ac6454SAndrew Thompson 
141102ac6454SAndrew Thompson 	/* reset "bMaxPacketSize" */
141202ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
141302ac6454SAndrew Thompson 
141402ac6454SAndrew Thompson 	/*
141502ac6454SAndrew Thompson 	 * Restore device address:
141602ac6454SAndrew Thompson 	 */
141702ac6454SAndrew Thompson 	err = usb2_req_set_address(udev, mtx, old_addr);
141802ac6454SAndrew Thompson 	if (err) {
141902ac6454SAndrew Thompson 		/* XXX ignore any errors! */
142002ac6454SAndrew Thompson 		DPRINTFN(0, "addr=%d, set address failed! (ignored)\n",
142102ac6454SAndrew Thompson 		    old_addr);
142202ac6454SAndrew Thompson 	}
142302ac6454SAndrew Thompson 	/* restore device address */
142402ac6454SAndrew Thompson 	udev->address = old_addr;
142502ac6454SAndrew Thompson 
142602ac6454SAndrew Thompson 	/* allow device time to set new address */
142702ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
142802ac6454SAndrew Thompson 
142902ac6454SAndrew Thompson 	/* get the device descriptor */
143016589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc,
143102ac6454SAndrew Thompson 	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
143202ac6454SAndrew Thompson 	if (err) {
143302ac6454SAndrew Thompson 		DPRINTFN(0, "getting device descriptor "
143402ac6454SAndrew Thompson 		    "at addr %d failed!\n", udev->address);
143502ac6454SAndrew Thompson 		goto done;
143602ac6454SAndrew Thompson 	}
143702ac6454SAndrew Thompson 	/* get the full device descriptor */
143802ac6454SAndrew Thompson 	err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc);
143902ac6454SAndrew Thompson 	if (err) {
144002ac6454SAndrew Thompson 		DPRINTFN(0, "addr=%d, getting device "
144102ac6454SAndrew Thompson 		    "descriptor failed!\n", old_addr);
144202ac6454SAndrew Thompson 		goto done;
144302ac6454SAndrew Thompson 	}
144402ac6454SAndrew Thompson done:
144502ac6454SAndrew Thompson 	if (err && do_retry) {
144602ac6454SAndrew Thompson 		/* give the USB firmware some time to load */
144702ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, hz / 2);
144802ac6454SAndrew Thompson 		/* no more retries after this retry */
144902ac6454SAndrew Thompson 		do_retry = 0;
145002ac6454SAndrew Thompson 		/* try again */
145102ac6454SAndrew Thompson 		goto retry;
145202ac6454SAndrew Thompson 	}
145302ac6454SAndrew Thompson 	/* restore address */
145402ac6454SAndrew Thompson 	udev->address = old_addr;
145502ac6454SAndrew Thompson 	return (err);
145602ac6454SAndrew Thompson }
145702ac6454SAndrew Thompson 
145802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
145902ac6454SAndrew Thompson  *	usb2_req_clear_device_feature
146002ac6454SAndrew Thompson  *
146102ac6454SAndrew Thompson  * Returns:
146202ac6454SAndrew Thompson  *    0: Success
146302ac6454SAndrew Thompson  * Else: Failure
146402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
146502ac6454SAndrew Thompson usb2_error_t
146602ac6454SAndrew Thompson usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx,
146702ac6454SAndrew Thompson     uint16_t sel)
146802ac6454SAndrew Thompson {
146902ac6454SAndrew Thompson 	struct usb2_device_request req;
147002ac6454SAndrew Thompson 
147102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
147202ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
147302ac6454SAndrew Thompson 	USETW(req.wValue, sel);
147402ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
147502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
147602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
147702ac6454SAndrew Thompson }
147802ac6454SAndrew Thompson 
147902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
148002ac6454SAndrew Thompson  *	usb2_req_set_device_feature
148102ac6454SAndrew Thompson  *
148202ac6454SAndrew Thompson  * Returns:
148302ac6454SAndrew Thompson  *    0: Success
148402ac6454SAndrew Thompson  * Else: Failure
148502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
148602ac6454SAndrew Thompson usb2_error_t
148702ac6454SAndrew Thompson usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx,
148802ac6454SAndrew Thompson     uint16_t sel)
148902ac6454SAndrew Thompson {
149002ac6454SAndrew Thompson 	struct usb2_device_request req;
149102ac6454SAndrew Thompson 
149202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
149302ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
149402ac6454SAndrew Thompson 	USETW(req.wValue, sel);
149502ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
149602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
149702ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
149802ac6454SAndrew Thompson }
1499