xref: /freebsd/sys/dev/usb/usb_request.c (revision ae60fdfba263901c34490262054e1dd39878b8f8)
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 
569360ae40SAndrew Thompson SYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
5702ac6454SAndrew Thompson     &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms");
589360ae40SAndrew Thompson SYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
5902ac6454SAndrew Thompson     &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
609360ae40SAndrew Thompson SYSCTL_INT(_hw_usb, 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
71760bc48eSAndrew Thompson usb2_do_request_callback(struct usb_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
93760bc48eSAndrew Thompson usb2_do_clear_stall_callback(struct usb_xfer *xfer)
9402ac6454SAndrew Thompson {
95760bc48eSAndrew Thompson 	struct usb_device_request req;
96760bc48eSAndrew Thompson 	struct usb_device *udev;
97ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep;
98ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_end;
99ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_first;
10063521bbcSAndrew Thompson 	uint8_t to;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson 	udev = xfer->xroot->udev;
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
10502ac6454SAndrew Thompson 
106ae60fdfbSAndrew Thompson 	/* round robin endpoint clear stall */
10702ac6454SAndrew Thompson 
108ae60fdfbSAndrew Thompson 	ep = udev->ep_curr;
109ae60fdfbSAndrew Thompson 	ep_end = udev->endpoints + udev->endpoints_max;
110ae60fdfbSAndrew Thompson 	ep_first = udev->endpoints;
111ae60fdfbSAndrew Thompson 	to = udev->endpoints_max;
112115df0b6SAndrew Thompson 
11302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
11402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
115ae60fdfbSAndrew Thompson 		if (ep == NULL)
116115df0b6SAndrew Thompson 			goto tr_setup;		/* device was unconfigured */
117ae60fdfbSAndrew Thompson 		if (ep->edesc &&
118ae60fdfbSAndrew Thompson 		    ep->is_stalled) {
119ae60fdfbSAndrew Thompson 			ep->toggle_next = 0;
120ae60fdfbSAndrew Thompson 			ep->is_stalled = 0;
12102ac6454SAndrew Thompson 			/* start up the current or next transfer, if any */
122ae60fdfbSAndrew Thompson 			usb2_command_wrapper(&ep->endpoint_q,
123ae60fdfbSAndrew Thompson 			    ep->endpoint_q.curr);
12402ac6454SAndrew Thompson 		}
125ae60fdfbSAndrew Thompson 		ep++;
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson 	case USB_ST_SETUP:
12802ac6454SAndrew Thompson tr_setup:
129115df0b6SAndrew Thompson 		if (to == 0)
130ae60fdfbSAndrew Thompson 			break;			/* no endpoints - nothing to do */
131ae60fdfbSAndrew Thompson 		if ((ep < ep_first) || (ep >= ep_end))
132ae60fdfbSAndrew Thompson 			ep = ep_first;	/* endpoint wrapped around */
133ae60fdfbSAndrew Thompson 		if (ep->edesc &&
134ae60fdfbSAndrew Thompson 		    ep->is_stalled) {
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson 			/* setup a clear-stall packet */
13702ac6454SAndrew Thompson 
13802ac6454SAndrew Thompson 			req.bmRequestType = UT_WRITE_ENDPOINT;
13902ac6454SAndrew Thompson 			req.bRequest = UR_CLEAR_FEATURE;
14002ac6454SAndrew Thompson 			USETW(req.wValue, UF_ENDPOINT_HALT);
141ae60fdfbSAndrew Thompson 			req.wIndex[0] = ep->edesc->bEndpointAddress;
14202ac6454SAndrew Thompson 			req.wIndex[1] = 0;
14302ac6454SAndrew Thompson 			USETW(req.wLength, 0);
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 			/* copy in the transfer */
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
14802ac6454SAndrew Thompson 
14902ac6454SAndrew Thompson 			/* set length */
15002ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
15102ac6454SAndrew Thompson 			xfer->nframes = 1;
15202ac6454SAndrew Thompson 			USB_BUS_UNLOCK(udev->bus);
15302ac6454SAndrew Thompson 
15402ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson 			USB_BUS_LOCK(udev->bus);
15702ac6454SAndrew Thompson 			break;
15802ac6454SAndrew Thompson 		}
159ae60fdfbSAndrew Thompson 		ep++;
160115df0b6SAndrew Thompson 		to--;
16102ac6454SAndrew Thompson 		goto tr_setup;
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 	default:
16402ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_CANCELLED) {
16502ac6454SAndrew Thompson 			break;
16602ac6454SAndrew Thompson 		}
16702ac6454SAndrew Thompson 		goto tr_setup;
16802ac6454SAndrew Thompson 	}
16902ac6454SAndrew Thompson 
170ae60fdfbSAndrew Thompson 	/* store current endpoint */
171ae60fdfbSAndrew Thompson 	udev->ep_curr = ep;
17202ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
17302ac6454SAndrew Thompson }
17402ac6454SAndrew Thompson 
175e0a69b51SAndrew Thompson static usb_handle_req_t *
176760bc48eSAndrew Thompson usb2_get_hr_func(struct usb_device *udev)
177459d369eSAndrew Thompson {
178459d369eSAndrew Thompson 	/* figure out if there is a Handle Request function */
179f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE)
180459d369eSAndrew Thompson 		return (usb2_temp_get_desc_p);
181459d369eSAndrew Thompson 	else if (udev->parent_hub == NULL)
182459d369eSAndrew Thompson 		return (udev->bus->methods->roothub_exec);
183459d369eSAndrew Thompson 	else
184459d369eSAndrew Thompson 		return (NULL);
185459d369eSAndrew Thompson }
186459d369eSAndrew Thompson 
18702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
18802ac6454SAndrew Thompson  *	usb2_do_request_flags and usb2_do_request
18902ac6454SAndrew Thompson  *
19002ac6454SAndrew Thompson  * Description of arguments passed to these functions:
19102ac6454SAndrew Thompson  *
192760bc48eSAndrew Thompson  * "udev" - this is the "usb_device" structure pointer on which the
19302ac6454SAndrew Thompson  * request should be performed. It is possible to call this function
19402ac6454SAndrew Thompson  * in both Host Side mode and Device Side mode.
19502ac6454SAndrew Thompson  *
19602ac6454SAndrew Thompson  * "mtx" - if this argument is non-NULL the mutex pointed to by it
19702ac6454SAndrew Thompson  * will get dropped and picked up during the execution of this
19802ac6454SAndrew Thompson  * function, hence this function sometimes needs to sleep. If this
19902ac6454SAndrew Thompson  * argument is NULL it has no effect.
20002ac6454SAndrew Thompson  *
20102ac6454SAndrew Thompson  * "req" - this argument must always be non-NULL and points to an
20202ac6454SAndrew Thompson  * 8-byte structure holding the USB request to be done. The USB
20302ac6454SAndrew Thompson  * request structure has a bit telling the direction of the USB
20402ac6454SAndrew Thompson  * request, if it is a read or a write.
20502ac6454SAndrew Thompson  *
20602ac6454SAndrew Thompson  * "data" - if the "wLength" part of the structure pointed to by "req"
20702ac6454SAndrew Thompson  * is non-zero this argument must point to a valid kernel buffer which
20802ac6454SAndrew Thompson  * can hold at least "wLength" bytes. If "wLength" is zero "data" can
20902ac6454SAndrew Thompson  * be NULL.
21002ac6454SAndrew Thompson  *
21102ac6454SAndrew Thompson  * "flags" - here is a list of valid flags:
21202ac6454SAndrew Thompson  *
21302ac6454SAndrew Thompson  *  o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
21402ac6454SAndrew Thompson  *  specified
21502ac6454SAndrew Thompson  *
21602ac6454SAndrew Thompson  *  o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
21702ac6454SAndrew Thompson  *  at a later point in time. This is tunable by the "hw.usb.ss_delay"
21802ac6454SAndrew Thompson  *  sysctl. This flag is mostly useful for debugging.
21902ac6454SAndrew Thompson  *
22002ac6454SAndrew Thompson  *  o USB_USER_DATA_PTR: treat the "data" pointer like a userland
22102ac6454SAndrew Thompson  *  pointer.
22202ac6454SAndrew Thompson  *
22302ac6454SAndrew Thompson  * "actlen" - if non-NULL the actual transfer length will be stored in
22402ac6454SAndrew Thompson  * the 16-bit unsigned integer pointed to by "actlen". This
22502ac6454SAndrew Thompson  * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
22602ac6454SAndrew Thompson  * used.
22702ac6454SAndrew Thompson  *
22802ac6454SAndrew Thompson  * "timeout" - gives the timeout for the control transfer in
22902ac6454SAndrew Thompson  * milliseconds. A "timeout" value less than 50 milliseconds is
23002ac6454SAndrew Thompson  * treated like a 50 millisecond timeout. A "timeout" value greater
23102ac6454SAndrew Thompson  * than 30 seconds is treated like a 30 second timeout. This USB stack
23202ac6454SAndrew Thompson  * does not allow control requests without a timeout.
23302ac6454SAndrew Thompson  *
23402ac6454SAndrew Thompson  * NOTE: This function is thread safe. All calls to
23502ac6454SAndrew Thompson  * "usb2_do_request_flags" will be serialised by the use of an
23602ac6454SAndrew Thompson  * internal "sx_lock".
23702ac6454SAndrew Thompson  *
23802ac6454SAndrew Thompson  * Returns:
23902ac6454SAndrew Thompson  *    0: Success
24002ac6454SAndrew Thompson  * Else: Failure
24102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
242e0a69b51SAndrew Thompson usb_error_t
243760bc48eSAndrew Thompson usb2_do_request_flags(struct usb_device *udev, struct mtx *mtx,
244760bc48eSAndrew Thompson     struct usb_device_request *req, void *data, uint16_t flags,
245e0a69b51SAndrew Thompson     uint16_t *actlen, usb_timeout_t timeout)
24602ac6454SAndrew Thompson {
247e0a69b51SAndrew Thompson 	usb_handle_req_t *hr_func;
248760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
24902ac6454SAndrew Thompson 	const void *desc;
25002ac6454SAndrew Thompson 	int err = 0;
251e0a69b51SAndrew Thompson 	usb_ticks_t start_ticks;
252e0a69b51SAndrew Thompson 	usb_ticks_t delta_ticks;
253e0a69b51SAndrew Thompson 	usb_ticks_t max_ticks;
25402ac6454SAndrew Thompson 	uint16_t length;
25502ac6454SAndrew Thompson 	uint16_t temp;
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson 	if (timeout < 50) {
25802ac6454SAndrew Thompson 		/* timeout is too small */
25902ac6454SAndrew Thompson 		timeout = 50;
26002ac6454SAndrew Thompson 	}
26102ac6454SAndrew Thompson 	if (timeout > 30000) {
26202ac6454SAndrew Thompson 		/* timeout is too big */
26302ac6454SAndrew Thompson 		timeout = 30000;
26402ac6454SAndrew Thompson 	}
26502ac6454SAndrew Thompson 	length = UGETW(req->wLength);
26602ac6454SAndrew Thompson 
26702ac6454SAndrew Thompson 	DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
26802ac6454SAndrew Thompson 	    "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
26902ac6454SAndrew Thompson 	    udev, req->bmRequestType, req->bRequest,
27002ac6454SAndrew Thompson 	    req->wValue[1], req->wValue[0],
27102ac6454SAndrew Thompson 	    req->wIndex[1], req->wIndex[0],
27202ac6454SAndrew Thompson 	    req->wLength[1], req->wLength[0]);
27302ac6454SAndrew Thompson 
274bd216778SAndrew Thompson 	/* Check if the device is still alive */
275bd216778SAndrew Thompson 	if (udev->state < USB_STATE_POWERED) {
276bd216778SAndrew Thompson 		DPRINTF("usb device has gone\n");
277bd216778SAndrew Thompson 		return (USB_ERR_NOT_CONFIGURED);
278bd216778SAndrew Thompson 	}
279bd216778SAndrew Thompson 
28002ac6454SAndrew Thompson 	/*
28102ac6454SAndrew Thompson 	 * Set "actlen" to a known value in case the caller does not
28202ac6454SAndrew Thompson 	 * check the return value:
28302ac6454SAndrew Thompson 	 */
28439307315SAndrew Thompson 	if (actlen)
28502ac6454SAndrew Thompson 		*actlen = 0;
28639307315SAndrew Thompson 
287bdc081c6SAndrew Thompson #if (USB_HAVE_USER_IO == 0)
288bdc081c6SAndrew Thompson 	if (flags & USB_USER_DATA_PTR)
289bdc081c6SAndrew Thompson 		return (USB_ERR_INVAL);
290bdc081c6SAndrew Thompson #endif
29102ac6454SAndrew Thompson 	if (mtx) {
29202ac6454SAndrew Thompson 		mtx_unlock(mtx);
29302ac6454SAndrew Thompson 		if (mtx != &Giant) {
29402ac6454SAndrew Thompson 			mtx_assert(mtx, MA_NOTOWNED);
29502ac6454SAndrew Thompson 		}
29602ac6454SAndrew Thompson 	}
29702ac6454SAndrew Thompson 	/*
29802ac6454SAndrew Thompson 	 * Grab the default sx-lock so that serialisation
29902ac6454SAndrew Thompson 	 * is achieved when multiple threads are involved:
30002ac6454SAndrew Thompson 	 */
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	sx_xlock(udev->default_sx);
30302ac6454SAndrew Thompson 
304459d369eSAndrew Thompson 	hr_func = usb2_get_hr_func(udev);
30539307315SAndrew Thompson 
306459d369eSAndrew Thompson 	if (hr_func != NULL) {
307459d369eSAndrew Thompson 		DPRINTF("Handle Request function is set\n");
30839307315SAndrew Thompson 
309459d369eSAndrew Thompson 		desc = NULL;
310459d369eSAndrew Thompson 		temp = 0;
311459d369eSAndrew Thompson 
312459d369eSAndrew Thompson 		if (!(req->bmRequestType & UT_READ)) {
31339307315SAndrew Thompson 			if (length != 0) {
314459d369eSAndrew Thompson 				DPRINTFN(1, "The handle request function "
315459d369eSAndrew Thompson 				    "does not support writing data!\n");
31639307315SAndrew Thompson 				err = USB_ERR_INVAL;
31739307315SAndrew Thompson 				goto done;
31839307315SAndrew Thompson 			}
31939307315SAndrew Thompson 		}
320459d369eSAndrew Thompson 
321459d369eSAndrew Thompson 		/* The root HUB code needs the BUS lock locked */
32239307315SAndrew Thompson 
32339307315SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
324459d369eSAndrew Thompson 		err = (hr_func) (udev, req, &desc, &temp);
32539307315SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
32639307315SAndrew Thompson 
32739307315SAndrew Thompson 		if (err)
32839307315SAndrew Thompson 			goto done;
32939307315SAndrew Thompson 
330459d369eSAndrew Thompson 		if (length > temp) {
33139307315SAndrew Thompson 			if (!(flags & USB_SHORT_XFER_OK)) {
33239307315SAndrew Thompson 				err = USB_ERR_SHORT_XFER;
33339307315SAndrew Thompson 				goto done;
33439307315SAndrew Thompson 			}
335459d369eSAndrew Thompson 			length = temp;
33639307315SAndrew Thompson 		}
33739307315SAndrew Thompson 		if (actlen)
33839307315SAndrew Thompson 			*actlen = length;
33939307315SAndrew Thompson 
34039307315SAndrew Thompson 		if (length > 0) {
34139307315SAndrew Thompson #if USB_HAVE_USER_IO
34239307315SAndrew Thompson 			if (flags & USB_USER_DATA_PTR) {
343459d369eSAndrew Thompson 				if (copyout(desc, data, length)) {
34439307315SAndrew Thompson 					err = USB_ERR_INVAL;
34539307315SAndrew Thompson 					goto done;
34639307315SAndrew Thompson 				}
34739307315SAndrew Thompson 			} else
34839307315SAndrew Thompson #endif
349459d369eSAndrew Thompson 				bcopy(desc, data, length);
35039307315SAndrew Thompson 		}
351459d369eSAndrew Thompson 		goto done;		/* success */
35239307315SAndrew Thompson 	}
35339307315SAndrew Thompson 
35402ac6454SAndrew Thompson 	/*
35502ac6454SAndrew Thompson 	 * Setup a new USB transfer or use the existing one, if any:
35602ac6454SAndrew Thompson 	 */
35702ac6454SAndrew Thompson 	usb2_default_transfer_setup(udev);
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson 	xfer = udev->default_xfer[0];
36002ac6454SAndrew Thompson 	if (xfer == NULL) {
36102ac6454SAndrew Thompson 		/* most likely out of memory */
36202ac6454SAndrew Thompson 		err = USB_ERR_NOMEM;
36302ac6454SAndrew Thompson 		goto done;
36402ac6454SAndrew Thompson 	}
36502ac6454SAndrew Thompson 	USB_XFER_LOCK(xfer);
36602ac6454SAndrew Thompson 
3674eae601eSAndrew Thompson 	if (flags & USB_DELAY_STATUS_STAGE)
36802ac6454SAndrew Thompson 		xfer->flags.manual_status = 1;
3694eae601eSAndrew Thompson 	else
37002ac6454SAndrew Thompson 		xfer->flags.manual_status = 0;
3714eae601eSAndrew Thompson 
3724eae601eSAndrew Thompson 	if (flags & USB_SHORT_XFER_OK)
3734eae601eSAndrew Thompson 		xfer->flags.short_xfer_ok = 1;
3744eae601eSAndrew Thompson 	else
3754eae601eSAndrew Thompson 		xfer->flags.short_xfer_ok = 0;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	xfer->timeout = timeout;
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	start_ticks = ticks;
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	max_ticks = USB_MS_TO_TICKS(timeout);
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 	usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson 	xfer->frlengths[0] = sizeof(*req);
38602ac6454SAndrew Thompson 	xfer->nframes = 2;
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 	while (1) {
38902ac6454SAndrew Thompson 		temp = length;
39002ac6454SAndrew Thompson 		if (temp > xfer->max_data_length) {
39102ac6454SAndrew Thompson 			temp = xfer->max_data_length;
39202ac6454SAndrew Thompson 		}
39302ac6454SAndrew Thompson 		xfer->frlengths[1] = temp;
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 		if (temp > 0) {
39602ac6454SAndrew Thompson 			if (!(req->bmRequestType & UT_READ)) {
397bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
39802ac6454SAndrew Thompson 				if (flags & USB_USER_DATA_PTR) {
39902ac6454SAndrew Thompson 					USB_XFER_UNLOCK(xfer);
40002ac6454SAndrew Thompson 					err = usb2_copy_in_user(xfer->frbuffers + 1,
40102ac6454SAndrew Thompson 					    0, data, temp);
40202ac6454SAndrew Thompson 					USB_XFER_LOCK(xfer);
40302ac6454SAndrew Thompson 					if (err) {
40402ac6454SAndrew Thompson 						err = USB_ERR_INVAL;
40502ac6454SAndrew Thompson 						break;
40602ac6454SAndrew Thompson 					}
407bdc081c6SAndrew Thompson 				} else
408bdc081c6SAndrew Thompson #endif
409bdc081c6SAndrew Thompson 					usb2_copy_in(xfer->frbuffers + 1,
410bdc081c6SAndrew Thompson 					    0, data, temp);
41102ac6454SAndrew Thompson 			}
41202ac6454SAndrew Thompson 			xfer->nframes = 2;
41302ac6454SAndrew Thompson 		} else {
41402ac6454SAndrew Thompson 			if (xfer->frlengths[0] == 0) {
41502ac6454SAndrew Thompson 				if (xfer->flags.manual_status) {
41602ac6454SAndrew Thompson #if USB_DEBUG
41702ac6454SAndrew Thompson 					int temp;
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 					temp = usb2_ss_delay;
42002ac6454SAndrew Thompson 					if (temp > 5000) {
42102ac6454SAndrew Thompson 						temp = 5000;
42202ac6454SAndrew Thompson 					}
42302ac6454SAndrew Thompson 					if (temp > 0) {
42402ac6454SAndrew Thompson 						usb2_pause_mtx(
42502ac6454SAndrew Thompson 						    xfer->xroot->xfer_mtx,
42602ac6454SAndrew Thompson 						    USB_MS_TO_TICKS(temp));
42702ac6454SAndrew Thompson 					}
42802ac6454SAndrew Thompson #endif
42902ac6454SAndrew Thompson 					xfer->flags.manual_status = 0;
43002ac6454SAndrew Thompson 				} else {
43102ac6454SAndrew Thompson 					break;
43202ac6454SAndrew Thompson 				}
43302ac6454SAndrew Thompson 			}
43402ac6454SAndrew Thompson 			xfer->nframes = 1;
43502ac6454SAndrew Thompson 		}
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 		usb2_transfer_start(xfer);
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson 		while (usb2_transfer_pending(xfer)) {
44002ac6454SAndrew Thompson 			usb2_cv_wait(udev->default_cv,
44102ac6454SAndrew Thompson 			    xfer->xroot->xfer_mtx);
44202ac6454SAndrew Thompson 		}
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 		err = xfer->error;
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson 		if (err) {
44702ac6454SAndrew Thompson 			break;
44802ac6454SAndrew Thompson 		}
44902ac6454SAndrew Thompson 		/* subtract length of SETUP packet, if any */
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 		if (xfer->aframes > 0) {
45202ac6454SAndrew Thompson 			xfer->actlen -= xfer->frlengths[0];
45302ac6454SAndrew Thompson 		} else {
45402ac6454SAndrew Thompson 			xfer->actlen = 0;
45502ac6454SAndrew Thompson 		}
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 		/* check for short packet */
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 		if (temp > xfer->actlen) {
46002ac6454SAndrew Thompson 			temp = xfer->actlen;
46102ac6454SAndrew Thompson 			length = temp;
46202ac6454SAndrew Thompson 		}
46302ac6454SAndrew Thompson 		if (temp > 0) {
46402ac6454SAndrew Thompson 			if (req->bmRequestType & UT_READ) {
465bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
46602ac6454SAndrew Thompson 				if (flags & USB_USER_DATA_PTR) {
46702ac6454SAndrew Thompson 					USB_XFER_UNLOCK(xfer);
46802ac6454SAndrew Thompson 					err = usb2_copy_out_user(xfer->frbuffers + 1,
46902ac6454SAndrew Thompson 					    0, data, temp);
47002ac6454SAndrew Thompson 					USB_XFER_LOCK(xfer);
47102ac6454SAndrew Thompson 					if (err) {
47202ac6454SAndrew Thompson 						err = USB_ERR_INVAL;
47302ac6454SAndrew Thompson 						break;
47402ac6454SAndrew Thompson 					}
475bdc081c6SAndrew Thompson 				} else
476bdc081c6SAndrew Thompson #endif
47702ac6454SAndrew Thompson 					usb2_copy_out(xfer->frbuffers + 1,
47802ac6454SAndrew Thompson 					    0, data, temp);
47902ac6454SAndrew Thompson 			}
48002ac6454SAndrew Thompson 		}
48102ac6454SAndrew Thompson 		/*
48202ac6454SAndrew Thompson 		 * Clear "frlengths[0]" so that we don't send the setup
48302ac6454SAndrew Thompson 		 * packet again:
48402ac6454SAndrew Thompson 		 */
48502ac6454SAndrew Thompson 		xfer->frlengths[0] = 0;
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 		/* update length and data pointer */
48802ac6454SAndrew Thompson 		length -= temp;
48902ac6454SAndrew Thompson 		data = USB_ADD_BYTES(data, temp);
49002ac6454SAndrew Thompson 
49102ac6454SAndrew Thompson 		if (actlen) {
49202ac6454SAndrew Thompson 			(*actlen) += temp;
49302ac6454SAndrew Thompson 		}
49402ac6454SAndrew Thompson 		/* check for timeout */
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 		delta_ticks = ticks - start_ticks;
49702ac6454SAndrew Thompson 		if (delta_ticks > max_ticks) {
49802ac6454SAndrew Thompson 			if (!err) {
49902ac6454SAndrew Thompson 				err = USB_ERR_TIMEOUT;
50002ac6454SAndrew Thompson 			}
50102ac6454SAndrew Thompson 		}
50202ac6454SAndrew Thompson 		if (err) {
50302ac6454SAndrew Thompson 			break;
50402ac6454SAndrew Thompson 		}
50502ac6454SAndrew Thompson 	}
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 	if (err) {
50802ac6454SAndrew Thompson 		/*
50902ac6454SAndrew Thompson 		 * Make sure that the control endpoint is no longer
51002ac6454SAndrew Thompson 		 * blocked in case of a non-transfer related error:
51102ac6454SAndrew Thompson 		 */
51202ac6454SAndrew Thompson 		usb2_transfer_stop(xfer);
51302ac6454SAndrew Thompson 	}
51402ac6454SAndrew Thompson 	USB_XFER_UNLOCK(xfer);
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson done:
51702ac6454SAndrew Thompson 	sx_xunlock(udev->default_sx);
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 	if (mtx) {
52002ac6454SAndrew Thompson 		mtx_lock(mtx);
52102ac6454SAndrew Thompson 	}
522e0a69b51SAndrew Thompson 	return ((usb_error_t)err);
52302ac6454SAndrew Thompson }
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
52602ac6454SAndrew Thompson  *	usb2_do_request_proc - factored out code
52702ac6454SAndrew Thompson  *
52802ac6454SAndrew Thompson  * This function is factored out code. It does basically the same like
52902ac6454SAndrew Thompson  * usb2_do_request_flags, except it will check the status of the
53002ac6454SAndrew Thompson  * passed process argument before doing the USB request. If the
53102ac6454SAndrew Thompson  * process is draining the USB_ERR_IOERROR code will be returned. It
53202ac6454SAndrew Thompson  * is assumed that the mutex associated with the process is locked
53302ac6454SAndrew Thompson  * when calling this function.
53402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
535e0a69b51SAndrew Thompson usb_error_t
536760bc48eSAndrew Thompson usb2_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
537760bc48eSAndrew Thompson     struct usb_device_request *req, void *data, uint16_t flags,
538e0a69b51SAndrew Thompson     uint16_t *actlen, usb_timeout_t timeout)
53902ac6454SAndrew Thompson {
540e0a69b51SAndrew Thompson 	usb_error_t err;
54102ac6454SAndrew Thompson 	uint16_t len;
54202ac6454SAndrew Thompson 
54302ac6454SAndrew Thompson 	/* get request data length */
54402ac6454SAndrew Thompson 	len = UGETW(req->wLength);
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson 	/* check if the device is being detached */
54702ac6454SAndrew Thompson 	if (usb2_proc_is_gone(pproc)) {
54802ac6454SAndrew Thompson 		err = USB_ERR_IOERROR;
54902ac6454SAndrew Thompson 		goto done;
55002ac6454SAndrew Thompson 	}
55102ac6454SAndrew Thompson 
55202ac6454SAndrew Thompson 	/* forward the USB request */
55302ac6454SAndrew Thompson 	err = usb2_do_request_flags(udev, pproc->up_mtx,
55402ac6454SAndrew Thompson 	    req, data, flags, actlen, timeout);
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson done:
55702ac6454SAndrew Thompson 	/* on failure we zero the data */
55802ac6454SAndrew Thompson 	/* on short packet we zero the unused data */
55902ac6454SAndrew Thompson 	if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
56002ac6454SAndrew Thompson 		if (err)
56102ac6454SAndrew Thompson 			memset(data, 0, len);
56202ac6454SAndrew Thompson 		else if (actlen && *actlen != len)
56302ac6454SAndrew Thompson 			memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
56402ac6454SAndrew Thompson 	}
56502ac6454SAndrew Thompson 	return (err);
56602ac6454SAndrew Thompson }
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
56902ac6454SAndrew Thompson  *	usb2_req_reset_port
57002ac6454SAndrew Thompson  *
57102ac6454SAndrew Thompson  * This function will instruct an USB HUB to perform a reset sequence
57202ac6454SAndrew Thompson  * on the specified port number.
57302ac6454SAndrew Thompson  *
57402ac6454SAndrew Thompson  * Returns:
57502ac6454SAndrew Thompson  *    0: Success. The USB device should now be at address zero.
57602ac6454SAndrew Thompson  * Else: Failure. No USB device is present and the USB port should be
57702ac6454SAndrew Thompson  *       disabled.
57802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
579e0a69b51SAndrew Thompson usb_error_t
580760bc48eSAndrew Thompson usb2_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
58102ac6454SAndrew Thompson {
582760bc48eSAndrew Thompson 	struct usb_port_status ps;
583e0a69b51SAndrew Thompson 	usb_error_t err;
58402ac6454SAndrew Thompson 	uint16_t n;
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson #if USB_DEBUG
58702ac6454SAndrew Thompson 	uint16_t pr_poll_delay;
58802ac6454SAndrew Thompson 	uint16_t pr_recovery_delay;
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson #endif
59102ac6454SAndrew Thompson 	err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
59202ac6454SAndrew Thompson 	if (err) {
59302ac6454SAndrew Thompson 		goto done;
59402ac6454SAndrew Thompson 	}
59502ac6454SAndrew Thompson #if USB_DEBUG
59602ac6454SAndrew Thompson 	/* range check input parameters */
59702ac6454SAndrew Thompson 	pr_poll_delay = usb2_pr_poll_delay;
59802ac6454SAndrew Thompson 	if (pr_poll_delay < 1) {
59902ac6454SAndrew Thompson 		pr_poll_delay = 1;
60002ac6454SAndrew Thompson 	} else if (pr_poll_delay > 1000) {
60102ac6454SAndrew Thompson 		pr_poll_delay = 1000;
60202ac6454SAndrew Thompson 	}
60302ac6454SAndrew Thompson 	pr_recovery_delay = usb2_pr_recovery_delay;
60402ac6454SAndrew Thompson 	if (pr_recovery_delay > 1000) {
60502ac6454SAndrew Thompson 		pr_recovery_delay = 1000;
60602ac6454SAndrew Thompson 	}
60702ac6454SAndrew Thompson #endif
60802ac6454SAndrew Thompson 	n = 0;
60902ac6454SAndrew Thompson 	while (1) {
61002ac6454SAndrew Thompson #if USB_DEBUG
61102ac6454SAndrew Thompson 		/* wait for the device to recover from reset */
61202ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
61302ac6454SAndrew Thompson 		n += pr_poll_delay;
61402ac6454SAndrew Thompson #else
61502ac6454SAndrew Thompson 		/* wait for the device to recover from reset */
61602ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
61702ac6454SAndrew Thompson 		n += USB_PORT_RESET_DELAY;
61802ac6454SAndrew Thompson #endif
61902ac6454SAndrew Thompson 		err = usb2_req_get_port_status(udev, mtx, &ps, port);
62002ac6454SAndrew Thompson 		if (err) {
62102ac6454SAndrew Thompson 			goto done;
62202ac6454SAndrew Thompson 		}
62302ac6454SAndrew Thompson 		/* if the device disappeared, just give up */
62402ac6454SAndrew Thompson 		if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
62502ac6454SAndrew Thompson 			goto done;
62602ac6454SAndrew Thompson 		}
62702ac6454SAndrew Thompson 		/* check if reset is complete */
62802ac6454SAndrew Thompson 		if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
62902ac6454SAndrew Thompson 			break;
63002ac6454SAndrew Thompson 		}
63102ac6454SAndrew Thompson 		/* check for timeout */
63202ac6454SAndrew Thompson 		if (n > 1000) {
63302ac6454SAndrew Thompson 			n = 0;
63402ac6454SAndrew Thompson 			break;
63502ac6454SAndrew Thompson 		}
63602ac6454SAndrew Thompson 	}
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	/* clear port reset first */
63902ac6454SAndrew Thompson 	err = usb2_req_clear_port_feature(
64002ac6454SAndrew Thompson 	    udev, mtx, port, UHF_C_PORT_RESET);
64102ac6454SAndrew Thompson 	if (err) {
64202ac6454SAndrew Thompson 		goto done;
64302ac6454SAndrew Thompson 	}
64402ac6454SAndrew Thompson 	/* check for timeout */
64502ac6454SAndrew Thompson 	if (n == 0) {
64602ac6454SAndrew Thompson 		err = USB_ERR_TIMEOUT;
64702ac6454SAndrew Thompson 		goto done;
64802ac6454SAndrew Thompson 	}
64902ac6454SAndrew Thompson #if USB_DEBUG
65002ac6454SAndrew Thompson 	/* wait for the device to recover from reset */
65102ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
65202ac6454SAndrew Thompson #else
65302ac6454SAndrew Thompson 	/* wait for the device to recover from reset */
65402ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
65502ac6454SAndrew Thompson #endif
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson done:
65802ac6454SAndrew Thompson 	DPRINTFN(2, "port %d reset returning error=%s\n",
65902ac6454SAndrew Thompson 	    port, usb2_errstr(err));
66002ac6454SAndrew Thompson 	return (err);
66102ac6454SAndrew Thompson }
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
66402ac6454SAndrew Thompson  *	usb2_req_get_desc
66502ac6454SAndrew Thompson  *
66602ac6454SAndrew Thompson  * This function can be used to retrieve USB descriptors. It contains
66702ac6454SAndrew Thompson  * some additional logic like zeroing of missing descriptor bytes and
66802ac6454SAndrew Thompson  * retrying an USB descriptor in case of failure. The "min_len"
66902ac6454SAndrew Thompson  * argument specifies the minimum descriptor length. The "max_len"
67002ac6454SAndrew Thompson  * argument specifies the maximum descriptor length. If the real
67102ac6454SAndrew Thompson  * descriptor length is less than the minimum length the missing
67216589beaSAndrew Thompson  * byte(s) will be zeroed. The type field, the second byte of the USB
67316589beaSAndrew Thompson  * descriptor, will get forced to the correct type. If the "actlen"
67416589beaSAndrew Thompson  * pointer is non-NULL, the actual length of the transfer will get
67516589beaSAndrew Thompson  * stored in the 16-bit unsigned integer which it is pointing to. The
67616589beaSAndrew Thompson  * first byte of the descriptor will not get updated. If the "actlen"
67716589beaSAndrew Thompson  * pointer is NULL the first byte of the descriptor will get updated
67816589beaSAndrew Thompson  * to reflect the actual length instead. If "min_len" is not equal to
67916589beaSAndrew Thompson  * "max_len" then this function will try to retrive the beginning of
68016589beaSAndrew Thompson  * the descriptor and base the maximum length on the first byte of the
68116589beaSAndrew Thompson  * descriptor.
68202ac6454SAndrew Thompson  *
68302ac6454SAndrew Thompson  * Returns:
68402ac6454SAndrew Thompson  *    0: Success
68502ac6454SAndrew Thompson  * Else: Failure
68602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
687e0a69b51SAndrew Thompson usb_error_t
688760bc48eSAndrew Thompson usb2_req_get_desc(struct usb_device *udev,
68916589beaSAndrew Thompson     struct mtx *mtx, uint16_t *actlen, void *desc,
69002ac6454SAndrew Thompson     uint16_t min_len, uint16_t max_len,
69102ac6454SAndrew Thompson     uint16_t id, uint8_t type, uint8_t index,
69202ac6454SAndrew Thompson     uint8_t retries)
69302ac6454SAndrew Thompson {
694760bc48eSAndrew Thompson 	struct usb_device_request req;
69502ac6454SAndrew Thompson 	uint8_t *buf;
696e0a69b51SAndrew Thompson 	usb_error_t err;
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
69902ac6454SAndrew Thompson 	    id, type, index, max_len);
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
70202ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
70302ac6454SAndrew Thompson 	USETW2(req.wValue, type, index);
70402ac6454SAndrew Thompson 	USETW(req.wIndex, id);
70502ac6454SAndrew Thompson 
70602ac6454SAndrew Thompson 	while (1) {
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 		if ((min_len < 2) || (max_len < 2)) {
70902ac6454SAndrew Thompson 			err = USB_ERR_INVAL;
71002ac6454SAndrew Thompson 			goto done;
71102ac6454SAndrew Thompson 		}
71202ac6454SAndrew Thompson 		USETW(req.wLength, min_len);
71302ac6454SAndrew Thompson 
71402ac6454SAndrew Thompson 		err = usb2_do_request_flags(udev, mtx, &req,
71502ac6454SAndrew Thompson 		    desc, 0, NULL, 1000);
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 		if (err) {
71802ac6454SAndrew Thompson 			if (!retries) {
71902ac6454SAndrew Thompson 				goto done;
72002ac6454SAndrew Thompson 			}
72102ac6454SAndrew Thompson 			retries--;
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson 			usb2_pause_mtx(mtx, hz / 5);
72402ac6454SAndrew Thompson 
72502ac6454SAndrew Thompson 			continue;
72602ac6454SAndrew Thompson 		}
72702ac6454SAndrew Thompson 		buf = desc;
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 		if (min_len == max_len) {
73002ac6454SAndrew Thompson 
73116589beaSAndrew Thompson 			/* enforce correct length */
73216589beaSAndrew Thompson 			if ((buf[0] > min_len) && (actlen == NULL))
73302ac6454SAndrew Thompson 				buf[0] = min_len;
73416589beaSAndrew Thompson 
73516589beaSAndrew Thompson 			/* enforce correct type */
73602ac6454SAndrew Thompson 			buf[1] = type;
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 			goto done;
73902ac6454SAndrew Thompson 		}
74002ac6454SAndrew Thompson 		/* range check */
74102ac6454SAndrew Thompson 
74202ac6454SAndrew Thompson 		if (max_len > buf[0]) {
74302ac6454SAndrew Thompson 			max_len = buf[0];
74402ac6454SAndrew Thompson 		}
74502ac6454SAndrew Thompson 		/* zero minimum data */
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson 		while (min_len > max_len) {
74802ac6454SAndrew Thompson 			min_len--;
74902ac6454SAndrew Thompson 			buf[min_len] = 0;
75002ac6454SAndrew Thompson 		}
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson 		/* set new minimum length */
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson 		min_len = max_len;
75502ac6454SAndrew Thompson 	}
75602ac6454SAndrew Thompson done:
75716589beaSAndrew Thompson 	if (actlen != NULL) {
75816589beaSAndrew Thompson 		if (err)
75916589beaSAndrew Thompson 			*actlen = 0;
76016589beaSAndrew Thompson 		else
76116589beaSAndrew Thompson 			*actlen = min_len;
76216589beaSAndrew Thompson 	}
76302ac6454SAndrew Thompson 	return (err);
76402ac6454SAndrew Thompson }
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
76702ac6454SAndrew Thompson  *	usb2_req_get_string_any
76802ac6454SAndrew Thompson  *
76902ac6454SAndrew Thompson  * This function will return the string given by "string_index"
77002ac6454SAndrew Thompson  * using the first language ID. The maximum length "len" includes
77102ac6454SAndrew Thompson  * the terminating zero. The "len" argument should be twice as
77202ac6454SAndrew Thompson  * big pluss 2 bytes, compared with the actual maximum string length !
77302ac6454SAndrew Thompson  *
77402ac6454SAndrew Thompson  * Returns:
77502ac6454SAndrew Thompson  *    0: Success
77602ac6454SAndrew Thompson  * Else: Failure
77702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
778e0a69b51SAndrew Thompson usb_error_t
779760bc48eSAndrew Thompson usb2_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
78002ac6454SAndrew Thompson     uint16_t len, uint8_t string_index)
78102ac6454SAndrew Thompson {
78202ac6454SAndrew Thompson 	char *s;
78302ac6454SAndrew Thompson 	uint8_t *temp;
78402ac6454SAndrew Thompson 	uint16_t i;
78502ac6454SAndrew Thompson 	uint16_t n;
78602ac6454SAndrew Thompson 	uint16_t c;
78702ac6454SAndrew Thompson 	uint8_t swap;
788e0a69b51SAndrew Thompson 	usb_error_t err;
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	if (len == 0) {
79102ac6454SAndrew Thompson 		/* should not happen */
79202ac6454SAndrew Thompson 		return (USB_ERR_NORMAL_COMPLETION);
79302ac6454SAndrew Thompson 	}
79402ac6454SAndrew Thompson 	if (string_index == 0) {
79502ac6454SAndrew Thompson 		/* this is the language table */
79602ac6454SAndrew Thompson 		buf[0] = 0;
79702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
79802ac6454SAndrew Thompson 	}
79902ac6454SAndrew Thompson 	if (udev->flags.no_strings) {
80002ac6454SAndrew Thompson 		buf[0] = 0;
80102ac6454SAndrew Thompson 		return (USB_ERR_STALLED);
80202ac6454SAndrew Thompson 	}
80302ac6454SAndrew Thompson 	err = usb2_req_get_string_desc
80402ac6454SAndrew Thompson 	    (udev, mtx, buf, len, udev->langid, string_index);
80502ac6454SAndrew Thompson 	if (err) {
80602ac6454SAndrew Thompson 		buf[0] = 0;
80702ac6454SAndrew Thompson 		return (err);
80802ac6454SAndrew Thompson 	}
80902ac6454SAndrew Thompson 	temp = (uint8_t *)buf;
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson 	if (temp[0] < 2) {
81202ac6454SAndrew Thompson 		/* string length is too short */
81302ac6454SAndrew Thompson 		buf[0] = 0;
81402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
81502ac6454SAndrew Thompson 	}
81602ac6454SAndrew Thompson 	/* reserve one byte for terminating zero */
81702ac6454SAndrew Thompson 	len--;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	/* find maximum length */
82002ac6454SAndrew Thompson 	s = buf;
82102ac6454SAndrew Thompson 	n = (temp[0] / 2) - 1;
82202ac6454SAndrew Thompson 	if (n > len) {
82302ac6454SAndrew Thompson 		n = len;
82402ac6454SAndrew Thompson 	}
82502ac6454SAndrew Thompson 	/* skip descriptor header */
82602ac6454SAndrew Thompson 	temp += 2;
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 	/* reset swap state */
82902ac6454SAndrew Thompson 	swap = 3;
83002ac6454SAndrew Thompson 
83102ac6454SAndrew Thompson 	/* convert and filter */
83202ac6454SAndrew Thompson 	for (i = 0; (i != n); i++) {
83302ac6454SAndrew Thompson 		c = UGETW(temp + (2 * i));
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 		/* convert from Unicode, handle buggy strings */
83602ac6454SAndrew Thompson 		if (((c & 0xff00) == 0) && (swap & 1)) {
83702ac6454SAndrew Thompson 			/* Little Endian, default */
83802ac6454SAndrew Thompson 			*s = c;
83902ac6454SAndrew Thompson 			swap = 1;
84002ac6454SAndrew Thompson 		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
84102ac6454SAndrew Thompson 			/* Big Endian */
84202ac6454SAndrew Thompson 			*s = c >> 8;
84302ac6454SAndrew Thompson 			swap = 2;
84402ac6454SAndrew Thompson 		} else {
84502ac6454SAndrew Thompson 			/* silently skip bad character */
84602ac6454SAndrew Thompson 			continue;
84702ac6454SAndrew Thompson 		}
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 		/*
85002ac6454SAndrew Thompson 		 * Filter by default - we don't allow greater and less than
85102ac6454SAndrew Thompson 		 * signs because they might confuse the dmesg printouts!
85202ac6454SAndrew Thompson 		 */
85302ac6454SAndrew Thompson 		if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
85402ac6454SAndrew Thompson 			/* silently skip bad character */
85502ac6454SAndrew Thompson 			continue;
85602ac6454SAndrew Thompson 		}
85702ac6454SAndrew Thompson 		s++;
85802ac6454SAndrew Thompson 	}
85902ac6454SAndrew Thompson 	*s = 0;				/* zero terminate resulting string */
86002ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
86102ac6454SAndrew Thompson }
86202ac6454SAndrew Thompson 
86302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
86402ac6454SAndrew Thompson  *	usb2_req_get_string_desc
86502ac6454SAndrew Thompson  *
86602ac6454SAndrew Thompson  * If you don't know the language ID, consider using
86702ac6454SAndrew Thompson  * "usb2_req_get_string_any()".
86802ac6454SAndrew Thompson  *
86902ac6454SAndrew Thompson  * Returns:
87002ac6454SAndrew Thompson  *    0: Success
87102ac6454SAndrew Thompson  * Else: Failure
87202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
873e0a69b51SAndrew Thompson usb_error_t
874760bc48eSAndrew Thompson usb2_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
87502ac6454SAndrew Thompson     uint16_t max_len, uint16_t lang_id,
87602ac6454SAndrew Thompson     uint8_t string_index)
87702ac6454SAndrew Thompson {
87816589beaSAndrew Thompson 	return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
87902ac6454SAndrew Thompson 	    UDESC_STRING, string_index, 0));
88002ac6454SAndrew Thompson }
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
8837efaaa9aSAndrew Thompson  *	usb2_req_get_config_desc_ptr
8847efaaa9aSAndrew Thompson  *
8857efaaa9aSAndrew Thompson  * This function is used in device side mode to retrieve the pointer
8867efaaa9aSAndrew Thompson  * to the generated config descriptor. This saves allocating space for
8877efaaa9aSAndrew Thompson  * an additional config descriptor when setting the configuration.
8887efaaa9aSAndrew Thompson  *
8897efaaa9aSAndrew Thompson  * Returns:
8907efaaa9aSAndrew Thompson  *    0: Success
8917efaaa9aSAndrew Thompson  * Else: Failure
8927efaaa9aSAndrew Thompson  *------------------------------------------------------------------------*/
893e0a69b51SAndrew Thompson usb_error_t
894760bc48eSAndrew Thompson usb2_req_get_descriptor_ptr(struct usb_device *udev,
895760bc48eSAndrew Thompson     struct usb_config_descriptor **ppcd, uint16_t wValue)
8967efaaa9aSAndrew Thompson {
897760bc48eSAndrew Thompson 	struct usb_device_request req;
898e0a69b51SAndrew Thompson 	usb_handle_req_t *hr_func;
899459d369eSAndrew Thompson 	const void *ptr;
900459d369eSAndrew Thompson 	uint16_t len;
901e0a69b51SAndrew Thompson 	usb_error_t err;
9027efaaa9aSAndrew Thompson 
90363521bbcSAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
9047efaaa9aSAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
905459d369eSAndrew Thompson 	USETW(req.wValue, wValue);
9067efaaa9aSAndrew Thompson 	USETW(req.wIndex, 0);
9077efaaa9aSAndrew Thompson 	USETW(req.wLength, 0);
9087efaaa9aSAndrew Thompson 
909459d369eSAndrew Thompson 	ptr = NULL;
910459d369eSAndrew Thompson 	len = 0;
9117efaaa9aSAndrew Thompson 
912459d369eSAndrew Thompson 	hr_func = usb2_get_hr_func(udev);
913459d369eSAndrew Thompson 
914459d369eSAndrew Thompson 	if (hr_func == NULL)
915459d369eSAndrew Thompson 		err = USB_ERR_INVAL;
916459d369eSAndrew Thompson 	else {
917459d369eSAndrew Thompson 		USB_BUS_LOCK(udev->bus);
918459d369eSAndrew Thompson 		err = (hr_func) (udev, &req, &ptr, &len);
919459d369eSAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
920459d369eSAndrew Thompson 	}
921459d369eSAndrew Thompson 
922459d369eSAndrew Thompson 	if (err)
923459d369eSAndrew Thompson 		ptr = NULL;
924459d369eSAndrew Thompson 	else if (ptr == NULL)
925459d369eSAndrew Thompson 		err = USB_ERR_INVAL;
926459d369eSAndrew Thompson 
927760bc48eSAndrew Thompson 	*ppcd = __DECONST(struct usb_config_descriptor *, ptr);
928459d369eSAndrew Thompson 
929459d369eSAndrew Thompson 	return (err);
9307efaaa9aSAndrew Thompson }
9317efaaa9aSAndrew Thompson 
9327efaaa9aSAndrew Thompson /*------------------------------------------------------------------------*
93302ac6454SAndrew Thompson  *	usb2_req_get_config_desc
93402ac6454SAndrew Thompson  *
93502ac6454SAndrew Thompson  * Returns:
93602ac6454SAndrew Thompson  *    0: Success
93702ac6454SAndrew Thompson  * Else: Failure
93802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
939e0a69b51SAndrew Thompson usb_error_t
940760bc48eSAndrew Thompson usb2_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
941760bc48eSAndrew Thompson     struct usb_config_descriptor *d, uint8_t conf_index)
94202ac6454SAndrew Thompson {
943e0a69b51SAndrew Thompson 	usb_error_t err;
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	DPRINTFN(4, "confidx=%d\n", conf_index);
94602ac6454SAndrew Thompson 
94716589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
94802ac6454SAndrew Thompson 	    sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
94902ac6454SAndrew Thompson 	if (err) {
95002ac6454SAndrew Thompson 		goto done;
95102ac6454SAndrew Thompson 	}
95202ac6454SAndrew Thompson 	/* Extra sanity checking */
95302ac6454SAndrew Thompson 	if (UGETW(d->wTotalLength) < sizeof(*d)) {
95402ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
95502ac6454SAndrew Thompson 	}
95602ac6454SAndrew Thompson done:
95702ac6454SAndrew Thompson 	return (err);
95802ac6454SAndrew Thompson }
95902ac6454SAndrew Thompson 
96002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
96102ac6454SAndrew Thompson  *	usb2_req_get_config_desc_full
96202ac6454SAndrew Thompson  *
96302ac6454SAndrew Thompson  * This function gets the complete USB configuration descriptor and
96402ac6454SAndrew Thompson  * ensures that "wTotalLength" is correct.
96502ac6454SAndrew Thompson  *
96602ac6454SAndrew Thompson  * Returns:
96702ac6454SAndrew Thompson  *    0: Success
96802ac6454SAndrew Thompson  * Else: Failure
96902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
970e0a69b51SAndrew Thompson usb_error_t
971760bc48eSAndrew Thompson usb2_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
972760bc48eSAndrew Thompson     struct usb_config_descriptor **ppcd, struct malloc_type *mtype,
97302ac6454SAndrew Thompson     uint8_t index)
97402ac6454SAndrew Thompson {
975760bc48eSAndrew Thompson 	struct usb_config_descriptor cd;
976760bc48eSAndrew Thompson 	struct usb_config_descriptor *cdesc;
97702ac6454SAndrew Thompson 	uint16_t len;
978e0a69b51SAndrew Thompson 	usb_error_t err;
97902ac6454SAndrew Thompson 
98002ac6454SAndrew Thompson 	DPRINTFN(4, "index=%d\n", index);
98102ac6454SAndrew Thompson 
98202ac6454SAndrew Thompson 	*ppcd = NULL;
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	err = usb2_req_get_config_desc(udev, mtx, &cd, index);
98502ac6454SAndrew Thompson 	if (err) {
98602ac6454SAndrew Thompson 		return (err);
98702ac6454SAndrew Thompson 	}
98802ac6454SAndrew Thompson 	/* get full descriptor */
98902ac6454SAndrew Thompson 	len = UGETW(cd.wTotalLength);
99002ac6454SAndrew Thompson 	if (len < sizeof(*cdesc)) {
99102ac6454SAndrew Thompson 		/* corrupt descriptor */
99202ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
99302ac6454SAndrew Thompson 	}
99402ac6454SAndrew Thompson 	cdesc = malloc(len, mtype, M_WAITOK);
99502ac6454SAndrew Thompson 	if (cdesc == NULL) {
99602ac6454SAndrew Thompson 		return (USB_ERR_NOMEM);
99702ac6454SAndrew Thompson 	}
99816589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
99902ac6454SAndrew Thompson 	    UDESC_CONFIG, index, 3);
100002ac6454SAndrew Thompson 	if (err) {
100102ac6454SAndrew Thompson 		free(cdesc, mtype);
100202ac6454SAndrew Thompson 		return (err);
100302ac6454SAndrew Thompson 	}
100402ac6454SAndrew Thompson 	/* make sure that the device is not fooling us: */
100502ac6454SAndrew Thompson 	USETW(cdesc->wTotalLength, len);
100602ac6454SAndrew Thompson 
100702ac6454SAndrew Thompson 	*ppcd = cdesc;
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson 	return (0);			/* success */
101002ac6454SAndrew Thompson }
101102ac6454SAndrew Thompson 
101202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
101302ac6454SAndrew Thompson  *	usb2_req_get_device_desc
101402ac6454SAndrew Thompson  *
101502ac6454SAndrew Thompson  * Returns:
101602ac6454SAndrew Thompson  *    0: Success
101702ac6454SAndrew Thompson  * Else: Failure
101802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1019e0a69b51SAndrew Thompson usb_error_t
1020760bc48eSAndrew Thompson usb2_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1021760bc48eSAndrew Thompson     struct usb_device_descriptor *d)
102202ac6454SAndrew Thompson {
102302ac6454SAndrew Thompson 	DPRINTFN(4, "\n");
102416589beaSAndrew Thompson 	return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
102502ac6454SAndrew Thompson 	    sizeof(*d), 0, UDESC_DEVICE, 0, 3));
102602ac6454SAndrew Thompson }
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
102902ac6454SAndrew Thompson  *	usb2_req_get_alt_interface_no
103002ac6454SAndrew Thompson  *
103102ac6454SAndrew Thompson  * Returns:
103202ac6454SAndrew Thompson  *    0: Success
103302ac6454SAndrew Thompson  * Else: Failure
103402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1035e0a69b51SAndrew Thompson usb_error_t
1036760bc48eSAndrew Thompson usb2_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
103702ac6454SAndrew Thompson     uint8_t *alt_iface_no, uint8_t iface_index)
103802ac6454SAndrew Thompson {
1039760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1040760bc48eSAndrew Thompson 	struct usb_device_request req;
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
104302ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
104402ac6454SAndrew Thompson 	}
104502ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_INTERFACE;
104602ac6454SAndrew Thompson 	req.bRequest = UR_GET_INTERFACE;
104702ac6454SAndrew Thompson 	USETW(req.wValue, 0);
104802ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
104902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
105002ac6454SAndrew Thompson 	USETW(req.wLength, 1);
105102ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, alt_iface_no));
105202ac6454SAndrew Thompson }
105302ac6454SAndrew Thompson 
105402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
105502ac6454SAndrew Thompson  *	usb2_req_set_alt_interface_no
105602ac6454SAndrew Thompson  *
105702ac6454SAndrew Thompson  * Returns:
105802ac6454SAndrew Thompson  *    0: Success
105902ac6454SAndrew Thompson  * Else: Failure
106002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1061e0a69b51SAndrew Thompson usb_error_t
1062760bc48eSAndrew Thompson usb2_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
106302ac6454SAndrew Thompson     uint8_t iface_index, uint8_t alt_no)
106402ac6454SAndrew Thompson {
1065760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1066760bc48eSAndrew Thompson 	struct usb_device_request req;
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
106902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
107002ac6454SAndrew Thompson 	}
107102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_INTERFACE;
107202ac6454SAndrew Thompson 	req.bRequest = UR_SET_INTERFACE;
107302ac6454SAndrew Thompson 	req.wValue[0] = alt_no;
107402ac6454SAndrew Thompson 	req.wValue[1] = 0;
107502ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
107602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
107702ac6454SAndrew Thompson 	USETW(req.wLength, 0);
107802ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
107902ac6454SAndrew Thompson }
108002ac6454SAndrew Thompson 
108102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
108202ac6454SAndrew Thompson  *	usb2_req_get_device_status
108302ac6454SAndrew Thompson  *
108402ac6454SAndrew Thompson  * Returns:
108502ac6454SAndrew Thompson  *    0: Success
108602ac6454SAndrew Thompson  * Else: Failure
108702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1088e0a69b51SAndrew Thompson usb_error_t
1089760bc48eSAndrew Thompson usb2_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1090760bc48eSAndrew Thompson     struct usb_status *st)
109102ac6454SAndrew Thompson {
1092760bc48eSAndrew Thompson 	struct usb_device_request req;
109302ac6454SAndrew Thompson 
109402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
109502ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
109602ac6454SAndrew Thompson 	USETW(req.wValue, 0);
109702ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
109802ac6454SAndrew Thompson 	USETW(req.wLength, sizeof(*st));
109902ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, st));
110002ac6454SAndrew Thompson }
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
110302ac6454SAndrew Thompson  *	usb2_req_get_hub_descriptor
110402ac6454SAndrew Thompson  *
110502ac6454SAndrew Thompson  * Returns:
110602ac6454SAndrew Thompson  *    0: Success
110702ac6454SAndrew Thompson  * Else: Failure
110802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1109e0a69b51SAndrew Thompson usb_error_t
1110760bc48eSAndrew Thompson usb2_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1111760bc48eSAndrew Thompson     struct usb_hub_descriptor *hd, uint8_t nports)
111202ac6454SAndrew Thompson {
1113760bc48eSAndrew Thompson 	struct usb_device_request req;
111402ac6454SAndrew Thompson 	uint16_t len = (nports + 7 + (8 * 8)) / 8;
111502ac6454SAndrew Thompson 
111602ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_DEVICE;
111702ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
111802ac6454SAndrew Thompson 	USETW2(req.wValue, UDESC_HUB, 0);
111902ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
112002ac6454SAndrew Thompson 	USETW(req.wLength, len);
112102ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, hd));
112202ac6454SAndrew Thompson }
112302ac6454SAndrew Thompson 
112402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
112502ac6454SAndrew Thompson  *	usb2_req_get_hub_status
112602ac6454SAndrew Thompson  *
112702ac6454SAndrew Thompson  * Returns:
112802ac6454SAndrew Thompson  *    0: Success
112902ac6454SAndrew Thompson  * Else: Failure
113002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1131e0a69b51SAndrew Thompson usb_error_t
1132760bc48eSAndrew Thompson usb2_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1133760bc48eSAndrew Thompson     struct usb_hub_status *st)
113402ac6454SAndrew Thompson {
1135760bc48eSAndrew Thompson 	struct usb_device_request req;
113602ac6454SAndrew Thompson 
113702ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_DEVICE;
113802ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
113902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
114002ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
1141760bc48eSAndrew Thompson 	USETW(req.wLength, sizeof(struct usb_hub_status));
114202ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, st));
114302ac6454SAndrew Thompson }
114402ac6454SAndrew Thompson 
114502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
114602ac6454SAndrew Thompson  *	usb2_req_set_address
114702ac6454SAndrew Thompson  *
114802ac6454SAndrew Thompson  * This function is used to set the address for an USB device. After
114902ac6454SAndrew Thompson  * port reset the USB device will respond at address zero.
115002ac6454SAndrew Thompson  *
115102ac6454SAndrew Thompson  * Returns:
115202ac6454SAndrew Thompson  *    0: Success
115302ac6454SAndrew Thompson  * Else: Failure
115402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1155e0a69b51SAndrew Thompson usb_error_t
1156760bc48eSAndrew Thompson usb2_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
115702ac6454SAndrew Thompson {
1158760bc48eSAndrew Thompson 	struct usb_device_request req;
115902ac6454SAndrew Thompson 
116002ac6454SAndrew Thompson 	DPRINTFN(6, "setting device address=%d\n", addr);
116102ac6454SAndrew Thompson 
116202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
116302ac6454SAndrew Thompson 	req.bRequest = UR_SET_ADDRESS;
116402ac6454SAndrew Thompson 	USETW(req.wValue, addr);
116502ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
116602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
116702ac6454SAndrew Thompson 
116802ac6454SAndrew Thompson 	/* Setting the address should not take more than 1 second ! */
116902ac6454SAndrew Thompson 	return (usb2_do_request_flags(udev, mtx, &req, NULL,
117002ac6454SAndrew Thompson 	    USB_DELAY_STATUS_STAGE, NULL, 1000));
117102ac6454SAndrew Thompson }
117202ac6454SAndrew Thompson 
117302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
117402ac6454SAndrew Thompson  *	usb2_req_get_port_status
117502ac6454SAndrew Thompson  *
117602ac6454SAndrew Thompson  * Returns:
117702ac6454SAndrew Thompson  *    0: Success
117802ac6454SAndrew Thompson  * Else: Failure
117902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1180e0a69b51SAndrew Thompson usb_error_t
1181760bc48eSAndrew Thompson usb2_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1182760bc48eSAndrew Thompson     struct usb_port_status *ps, uint8_t port)
118302ac6454SAndrew Thompson {
1184760bc48eSAndrew Thompson 	struct usb_device_request req;
118502ac6454SAndrew Thompson 
118602ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_OTHER;
118702ac6454SAndrew Thompson 	req.bRequest = UR_GET_STATUS;
118802ac6454SAndrew Thompson 	USETW(req.wValue, 0);
118902ac6454SAndrew Thompson 	req.wIndex[0] = port;
119002ac6454SAndrew Thompson 	req.wIndex[1] = 0;
119102ac6454SAndrew Thompson 	USETW(req.wLength, sizeof *ps);
119202ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, ps));
119302ac6454SAndrew Thompson }
119402ac6454SAndrew Thompson 
119502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
119602ac6454SAndrew Thompson  *	usb2_req_clear_hub_feature
119702ac6454SAndrew Thompson  *
119802ac6454SAndrew Thompson  * Returns:
119902ac6454SAndrew Thompson  *    0: Success
120002ac6454SAndrew Thompson  * Else: Failure
120102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1202e0a69b51SAndrew Thompson usb_error_t
1203760bc48eSAndrew Thompson usb2_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
120402ac6454SAndrew Thompson     uint16_t sel)
120502ac6454SAndrew Thompson {
1206760bc48eSAndrew Thompson 	struct usb_device_request req;
120702ac6454SAndrew Thompson 
120802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
120902ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
121002ac6454SAndrew Thompson 	USETW(req.wValue, sel);
121102ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
121202ac6454SAndrew Thompson 	USETW(req.wLength, 0);
121302ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
121402ac6454SAndrew Thompson }
121502ac6454SAndrew Thompson 
121602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
121702ac6454SAndrew Thompson  *	usb2_req_set_hub_feature
121802ac6454SAndrew Thompson  *
121902ac6454SAndrew Thompson  * Returns:
122002ac6454SAndrew Thompson  *    0: Success
122102ac6454SAndrew Thompson  * Else: Failure
122202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1223e0a69b51SAndrew Thompson usb_error_t
1224760bc48eSAndrew Thompson usb2_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
122502ac6454SAndrew Thompson     uint16_t sel)
122602ac6454SAndrew Thompson {
1227760bc48eSAndrew Thompson 	struct usb_device_request req;
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
123002ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
123102ac6454SAndrew Thompson 	USETW(req.wValue, sel);
123202ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
123302ac6454SAndrew Thompson 	USETW(req.wLength, 0);
123402ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
123502ac6454SAndrew Thompson }
123602ac6454SAndrew Thompson 
123702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
123802ac6454SAndrew Thompson  *	usb2_req_clear_port_feature
123902ac6454SAndrew Thompson  *
124002ac6454SAndrew Thompson  * Returns:
124102ac6454SAndrew Thompson  *    0: Success
124202ac6454SAndrew Thompson  * Else: Failure
124302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1244e0a69b51SAndrew Thompson usb_error_t
1245760bc48eSAndrew Thompson usb2_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
124602ac6454SAndrew Thompson     uint8_t port, uint16_t sel)
124702ac6454SAndrew Thompson {
1248760bc48eSAndrew Thompson 	struct usb_device_request req;
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
125102ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
125202ac6454SAndrew Thompson 	USETW(req.wValue, sel);
125302ac6454SAndrew Thompson 	req.wIndex[0] = port;
125402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
125502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
125602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
125702ac6454SAndrew Thompson }
125802ac6454SAndrew Thompson 
125902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
126002ac6454SAndrew Thompson  *	usb2_req_set_port_feature
126102ac6454SAndrew Thompson  *
126202ac6454SAndrew Thompson  * Returns:
126302ac6454SAndrew Thompson  *    0: Success
126402ac6454SAndrew Thompson  * Else: Failure
126502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1266e0a69b51SAndrew Thompson usb_error_t
1267760bc48eSAndrew Thompson usb2_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
126802ac6454SAndrew Thompson     uint8_t port, uint16_t sel)
126902ac6454SAndrew Thompson {
1270760bc48eSAndrew Thompson 	struct usb_device_request req;
127102ac6454SAndrew Thompson 
127202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
127302ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
127402ac6454SAndrew Thompson 	USETW(req.wValue, sel);
127502ac6454SAndrew Thompson 	req.wIndex[0] = port;
127602ac6454SAndrew Thompson 	req.wIndex[1] = 0;
127702ac6454SAndrew Thompson 	USETW(req.wLength, 0);
127802ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
127902ac6454SAndrew Thompson }
128002ac6454SAndrew Thompson 
128102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
128202ac6454SAndrew Thompson  *	usb2_req_set_protocol
128302ac6454SAndrew Thompson  *
128402ac6454SAndrew Thompson  * Returns:
128502ac6454SAndrew Thompson  *    0: Success
128602ac6454SAndrew Thompson  * Else: Failure
128702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1288e0a69b51SAndrew Thompson usb_error_t
1289760bc48eSAndrew Thompson usb2_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
129002ac6454SAndrew Thompson     uint8_t iface_index, uint16_t report)
129102ac6454SAndrew Thompson {
1292760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1293760bc48eSAndrew Thompson 	struct usb_device_request req;
129402ac6454SAndrew Thompson 
129502ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
129602ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
129702ac6454SAndrew Thompson 	}
129802ac6454SAndrew Thompson 	DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
129902ac6454SAndrew Thompson 	    iface, report, iface->idesc->bInterfaceNumber);
130002ac6454SAndrew Thompson 
130102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
130202ac6454SAndrew Thompson 	req.bRequest = UR_SET_PROTOCOL;
130302ac6454SAndrew Thompson 	USETW(req.wValue, report);
130402ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
130502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
130602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
130702ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
130802ac6454SAndrew Thompson }
130902ac6454SAndrew Thompson 
131002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
131102ac6454SAndrew Thompson  *	usb2_req_set_report
131202ac6454SAndrew Thompson  *
131302ac6454SAndrew Thompson  * Returns:
131402ac6454SAndrew Thompson  *    0: Success
131502ac6454SAndrew Thompson  * Else: Failure
131602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1317e0a69b51SAndrew Thompson usb_error_t
1318760bc48eSAndrew Thompson usb2_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
131902ac6454SAndrew Thompson     uint8_t iface_index, uint8_t type, uint8_t id)
132002ac6454SAndrew Thompson {
1321760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1322760bc48eSAndrew Thompson 	struct usb_device_request req;
132302ac6454SAndrew Thompson 
132402ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
132502ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
132602ac6454SAndrew Thompson 	}
132702ac6454SAndrew Thompson 	DPRINTFN(5, "len=%d\n", len);
132802ac6454SAndrew Thompson 
132902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
133002ac6454SAndrew Thompson 	req.bRequest = UR_SET_REPORT;
133102ac6454SAndrew Thompson 	USETW2(req.wValue, type, id);
133202ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
133302ac6454SAndrew Thompson 	req.wIndex[1] = 0;
133402ac6454SAndrew Thompson 	USETW(req.wLength, len);
133502ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, data));
133602ac6454SAndrew Thompson }
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
133902ac6454SAndrew Thompson  *	usb2_req_get_report
134002ac6454SAndrew Thompson  *
134102ac6454SAndrew Thompson  * Returns:
134202ac6454SAndrew Thompson  *    0: Success
134302ac6454SAndrew Thompson  * Else: Failure
134402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1345e0a69b51SAndrew Thompson usb_error_t
1346760bc48eSAndrew Thompson usb2_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
134702ac6454SAndrew Thompson     uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
134802ac6454SAndrew Thompson {
1349760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1350760bc48eSAndrew Thompson 	struct usb_device_request req;
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
135302ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
135402ac6454SAndrew Thompson 	}
135502ac6454SAndrew Thompson 	DPRINTFN(5, "len=%d\n", len);
135602ac6454SAndrew Thompson 
135702ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
135802ac6454SAndrew Thompson 	req.bRequest = UR_GET_REPORT;
135902ac6454SAndrew Thompson 	USETW2(req.wValue, type, id);
136002ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
136102ac6454SAndrew Thompson 	req.wIndex[1] = 0;
136202ac6454SAndrew Thompson 	USETW(req.wLength, len);
136302ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, data));
136402ac6454SAndrew Thompson }
136502ac6454SAndrew Thompson 
136602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
136702ac6454SAndrew Thompson  *	usb2_req_set_idle
136802ac6454SAndrew Thompson  *
136902ac6454SAndrew Thompson  * Returns:
137002ac6454SAndrew Thompson  *    0: Success
137102ac6454SAndrew Thompson  * Else: Failure
137202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1373e0a69b51SAndrew Thompson usb_error_t
1374760bc48eSAndrew Thompson usb2_req_set_idle(struct usb_device *udev, struct mtx *mtx,
137502ac6454SAndrew Thompson     uint8_t iface_index, uint8_t duration, uint8_t id)
137602ac6454SAndrew Thompson {
1377760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1378760bc48eSAndrew Thompson 	struct usb_device_request req;
137902ac6454SAndrew Thompson 
138002ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
138102ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
138202ac6454SAndrew Thompson 	}
138302ac6454SAndrew Thompson 	DPRINTFN(5, "%d %d\n", duration, id);
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
138602ac6454SAndrew Thompson 	req.bRequest = UR_SET_IDLE;
138702ac6454SAndrew Thompson 	USETW2(req.wValue, duration, id);
138802ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
138902ac6454SAndrew Thompson 	req.wIndex[1] = 0;
139002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
139102ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
139202ac6454SAndrew Thompson }
139302ac6454SAndrew Thompson 
139402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
139502ac6454SAndrew Thompson  *	usb2_req_get_report_descriptor
139602ac6454SAndrew Thompson  *
139702ac6454SAndrew Thompson  * Returns:
139802ac6454SAndrew Thompson  *    0: Success
139902ac6454SAndrew Thompson  * Else: Failure
140002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1401e0a69b51SAndrew Thompson usb_error_t
1402760bc48eSAndrew Thompson usb2_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
140302ac6454SAndrew Thompson     void *d, uint16_t size, uint8_t iface_index)
140402ac6454SAndrew Thompson {
1405760bc48eSAndrew Thompson 	struct usb_interface *iface = usb2_get_iface(udev, iface_index);
1406760bc48eSAndrew Thompson 	struct usb_device_request req;
140702ac6454SAndrew Thompson 
140802ac6454SAndrew Thompson 	if ((iface == NULL) || (iface->idesc == NULL)) {
140902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
141002ac6454SAndrew Thompson 	}
141102ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_INTERFACE;
141202ac6454SAndrew Thompson 	req.bRequest = UR_GET_DESCRIPTOR;
141302ac6454SAndrew Thompson 	USETW2(req.wValue, UDESC_REPORT, 0);	/* report id should be 0 */
141402ac6454SAndrew Thompson 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
141502ac6454SAndrew Thompson 	req.wIndex[1] = 0;
141602ac6454SAndrew Thompson 	USETW(req.wLength, size);
141702ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, d));
141802ac6454SAndrew Thompson }
141902ac6454SAndrew Thompson 
142002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
142102ac6454SAndrew Thompson  *	usb2_req_set_config
142202ac6454SAndrew Thompson  *
142302ac6454SAndrew Thompson  * This function is used to select the current configuration number in
142402ac6454SAndrew Thompson  * both USB device side mode and USB host side mode. When setting the
142502ac6454SAndrew Thompson  * configuration the function of the interfaces can change.
142602ac6454SAndrew Thompson  *
142702ac6454SAndrew Thompson  * Returns:
142802ac6454SAndrew Thompson  *    0: Success
142902ac6454SAndrew Thompson  * Else: Failure
143002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1431e0a69b51SAndrew Thompson usb_error_t
1432760bc48eSAndrew Thompson usb2_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
143302ac6454SAndrew Thompson {
1434760bc48eSAndrew Thompson 	struct usb_device_request req;
143502ac6454SAndrew Thompson 
143602ac6454SAndrew Thompson 	DPRINTF("setting config %d\n", conf);
143702ac6454SAndrew Thompson 
143802ac6454SAndrew Thompson 	/* do "set configuration" request */
143902ac6454SAndrew Thompson 
144002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
144102ac6454SAndrew Thompson 	req.bRequest = UR_SET_CONFIG;
144202ac6454SAndrew Thompson 	req.wValue[0] = conf;
144302ac6454SAndrew Thompson 	req.wValue[1] = 0;
144402ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
144502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
144602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
144702ac6454SAndrew Thompson }
144802ac6454SAndrew Thompson 
144902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
145002ac6454SAndrew Thompson  *	usb2_req_get_config
145102ac6454SAndrew Thompson  *
145202ac6454SAndrew Thompson  * Returns:
145302ac6454SAndrew Thompson  *    0: Success
145402ac6454SAndrew Thompson  * Else: Failure
145502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1456e0a69b51SAndrew Thompson usb_error_t
1457760bc48eSAndrew Thompson usb2_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
145802ac6454SAndrew Thompson {
1459760bc48eSAndrew Thompson 	struct usb_device_request req;
146002ac6454SAndrew Thompson 
146102ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_DEVICE;
146202ac6454SAndrew Thompson 	req.bRequest = UR_GET_CONFIG;
146302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
146402ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
146502ac6454SAndrew Thompson 	USETW(req.wLength, 1);
146602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, pconf));
146702ac6454SAndrew Thompson }
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
147002ac6454SAndrew Thompson  *	usb2_req_re_enumerate
147102ac6454SAndrew Thompson  *
147202ac6454SAndrew Thompson  * NOTE: After this function returns the hardware is in the
147302ac6454SAndrew Thompson  * unconfigured state! The application is responsible for setting a
147402ac6454SAndrew Thompson  * new configuration.
147502ac6454SAndrew Thompson  *
147602ac6454SAndrew Thompson  * Returns:
147702ac6454SAndrew Thompson  *    0: Success
147802ac6454SAndrew Thompson  * Else: Failure
147902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1480e0a69b51SAndrew Thompson usb_error_t
1481760bc48eSAndrew Thompson usb2_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
148202ac6454SAndrew Thompson {
1483760bc48eSAndrew Thompson 	struct usb_device *parent_hub;
1484e0a69b51SAndrew Thompson 	usb_error_t err;
148502ac6454SAndrew Thompson 	uint8_t old_addr;
148602ac6454SAndrew Thompson 	uint8_t do_retry = 1;
148702ac6454SAndrew Thompson 
1488f29a0724SAndrew Thompson 	if (udev->flags.usb_mode != USB_MODE_HOST) {
148902ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
149002ac6454SAndrew Thompson 	}
149102ac6454SAndrew Thompson 	old_addr = udev->address;
149202ac6454SAndrew Thompson 	parent_hub = udev->parent_hub;
149302ac6454SAndrew Thompson 	if (parent_hub == NULL) {
149402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
149502ac6454SAndrew Thompson 	}
149602ac6454SAndrew Thompson retry:
149702ac6454SAndrew Thompson 	err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
149802ac6454SAndrew Thompson 	if (err) {
149903797f33SAndrew Thompson 		DPRINTFN(0, "addr=%d, port reset failed, %s\n",
150003797f33SAndrew Thompson 		    old_addr, usb2_errstr(err));
150102ac6454SAndrew Thompson 		goto done;
150202ac6454SAndrew Thompson 	}
150302ac6454SAndrew Thompson 	/*
150402ac6454SAndrew Thompson 	 * After that the port has been reset our device should be at
150502ac6454SAndrew Thompson 	 * address zero:
150602ac6454SAndrew Thompson 	 */
150702ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;
150802ac6454SAndrew Thompson 
150902ac6454SAndrew Thompson 	/* reset "bMaxPacketSize" */
151002ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 	/*
151302ac6454SAndrew Thompson 	 * Restore device address:
151402ac6454SAndrew Thompson 	 */
151502ac6454SAndrew Thompson 	err = usb2_req_set_address(udev, mtx, old_addr);
151602ac6454SAndrew Thompson 	if (err) {
151702ac6454SAndrew Thompson 		/* XXX ignore any errors! */
151803797f33SAndrew Thompson 		DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
151903797f33SAndrew Thompson 		    old_addr, usb2_errstr(err));
152002ac6454SAndrew Thompson 	}
152102ac6454SAndrew Thompson 	/* restore device address */
152202ac6454SAndrew Thompson 	udev->address = old_addr;
152302ac6454SAndrew Thompson 
152402ac6454SAndrew Thompson 	/* allow device time to set new address */
152502ac6454SAndrew Thompson 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
152602ac6454SAndrew Thompson 
152702ac6454SAndrew Thompson 	/* get the device descriptor */
152816589beaSAndrew Thompson 	err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc,
152902ac6454SAndrew Thompson 	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
153002ac6454SAndrew Thompson 	if (err) {
153102ac6454SAndrew Thompson 		DPRINTFN(0, "getting device descriptor "
153203797f33SAndrew Thompson 		    "at addr %d failed, %s!\n", udev->address,
153303797f33SAndrew Thompson 		    usb2_errstr(err));
153402ac6454SAndrew Thompson 		goto done;
153502ac6454SAndrew Thompson 	}
153602ac6454SAndrew Thompson 	/* get the full device descriptor */
153702ac6454SAndrew Thompson 	err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc);
153802ac6454SAndrew Thompson 	if (err) {
153902ac6454SAndrew Thompson 		DPRINTFN(0, "addr=%d, getting device "
154003797f33SAndrew Thompson 		    "descriptor failed, %s!\n", old_addr,
154103797f33SAndrew Thompson 		    usb2_errstr(err));
154202ac6454SAndrew Thompson 		goto done;
154302ac6454SAndrew Thompson 	}
154402ac6454SAndrew Thompson done:
154502ac6454SAndrew Thompson 	if (err && do_retry) {
154602ac6454SAndrew Thompson 		/* give the USB firmware some time to load */
154702ac6454SAndrew Thompson 		usb2_pause_mtx(mtx, hz / 2);
154802ac6454SAndrew Thompson 		/* no more retries after this retry */
154902ac6454SAndrew Thompson 		do_retry = 0;
155002ac6454SAndrew Thompson 		/* try again */
155102ac6454SAndrew Thompson 		goto retry;
155202ac6454SAndrew Thompson 	}
155302ac6454SAndrew Thompson 	/* restore address */
155402ac6454SAndrew Thompson 	udev->address = old_addr;
155502ac6454SAndrew Thompson 	return (err);
155602ac6454SAndrew Thompson }
155702ac6454SAndrew Thompson 
155802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
155902ac6454SAndrew Thompson  *	usb2_req_clear_device_feature
156002ac6454SAndrew Thompson  *
156102ac6454SAndrew Thompson  * Returns:
156202ac6454SAndrew Thompson  *    0: Success
156302ac6454SAndrew Thompson  * Else: Failure
156402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1565e0a69b51SAndrew Thompson usb_error_t
1566760bc48eSAndrew Thompson usb2_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
156702ac6454SAndrew Thompson     uint16_t sel)
156802ac6454SAndrew Thompson {
1569760bc48eSAndrew Thompson 	struct usb_device_request req;
157002ac6454SAndrew Thompson 
157102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
157202ac6454SAndrew Thompson 	req.bRequest = UR_CLEAR_FEATURE;
157302ac6454SAndrew Thompson 	USETW(req.wValue, sel);
157402ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
157502ac6454SAndrew Thompson 	USETW(req.wLength, 0);
157602ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
157702ac6454SAndrew Thompson }
157802ac6454SAndrew Thompson 
157902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
158002ac6454SAndrew Thompson  *	usb2_req_set_device_feature
158102ac6454SAndrew Thompson  *
158202ac6454SAndrew Thompson  * Returns:
158302ac6454SAndrew Thompson  *    0: Success
158402ac6454SAndrew Thompson  * Else: Failure
158502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1586e0a69b51SAndrew Thompson usb_error_t
1587760bc48eSAndrew Thompson usb2_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
158802ac6454SAndrew Thompson     uint16_t sel)
158902ac6454SAndrew Thompson {
1590760bc48eSAndrew Thompson 	struct usb_device_request req;
159102ac6454SAndrew Thompson 
159202ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_DEVICE;
159302ac6454SAndrew Thompson 	req.bRequest = UR_SET_FEATURE;
159402ac6454SAndrew Thompson 	USETW(req.wValue, sel);
159502ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
159602ac6454SAndrew Thompson 	USETW(req.wLength, 0);
159702ac6454SAndrew Thompson 	return (usb2_do_request(udev, mtx, &req, 0));
159802ac6454SAndrew Thompson }
1599