usb_request.c revision 260589
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_request.c 260589 2014-01-13 15:21:11Z hselasky $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4184610Salfred * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6184610Salfred *
7184610Salfred * Redistribution and use in source and binary forms, with or without
8184610Salfred * modification, are permitted provided that the following conditions
9184610Salfred * are met:
10184610Salfred * 1. Redistributions of source code must retain the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer.
12184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
13184610Salfred *    notice, this list of conditions and the following disclaimer in the
14184610Salfred *    documentation and/or other materials provided with the distribution.
15184610Salfred *
16184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26184610Salfred * SUCH DAMAGE.
27190754Sthompsa */
28184610Salfred
29246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE
30246122Shselasky#include USB_GLOBAL_INCLUDE_FILE
31246122Shselasky#else
32194677Sthompsa#include <sys/stdint.h>
33194677Sthompsa#include <sys/stddef.h>
34194677Sthompsa#include <sys/param.h>
35194677Sthompsa#include <sys/queue.h>
36194677Sthompsa#include <sys/types.h>
37194677Sthompsa#include <sys/systm.h>
38194677Sthompsa#include <sys/kernel.h>
39194677Sthompsa#include <sys/bus.h>
40194677Sthompsa#include <sys/module.h>
41194677Sthompsa#include <sys/lock.h>
42194677Sthompsa#include <sys/mutex.h>
43194677Sthompsa#include <sys/condvar.h>
44194677Sthompsa#include <sys/sysctl.h>
45194677Sthompsa#include <sys/sx.h>
46194677Sthompsa#include <sys/unistd.h>
47194677Sthompsa#include <sys/callout.h>
48194677Sthompsa#include <sys/malloc.h>
49194677Sthompsa#include <sys/priv.h>
50194677Sthompsa
51188942Sthompsa#include <dev/usb/usb.h>
52194677Sthompsa#include <dev/usb/usbdi.h>
53194677Sthompsa#include <dev/usb/usbdi_util.h>
54188942Sthompsa#include <dev/usb/usbhid.h>
55184610Salfred
56194228Sthompsa#define	USB_DEBUG_VAR usb_debug
57184610Salfred
58188942Sthompsa#include <dev/usb/usb_core.h>
59188942Sthompsa#include <dev/usb/usb_busdma.h>
60188942Sthompsa#include <dev/usb/usb_request.h>
61188942Sthompsa#include <dev/usb/usb_process.h>
62188942Sthompsa#include <dev/usb/usb_transfer.h>
63188942Sthompsa#include <dev/usb/usb_debug.h>
64188942Sthompsa#include <dev/usb/usb_device.h>
65188942Sthompsa#include <dev/usb/usb_util.h>
66188942Sthompsa#include <dev/usb/usb_dynamic.h>
67184610Salfred
68188942Sthompsa#include <dev/usb/usb_controller.h>
69188942Sthompsa#include <dev/usb/usb_bus.h>
70184610Salfred#include <sys/ctype.h>
71246122Shselasky#endif			/* USB_GLOBAL_INCLUDE_FILE */
72184610Salfred
73225000Shselaskystatic int usb_no_cs_fail;
74225000Shselasky
75225000ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW,
76225000Shselasky    &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set");
77225000Shselasky
78248246Shselaskystatic int usb_full_ddesc;
79248246Shselasky
80248246ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, full_ddesc, CTLFLAG_RW,
81248246Shselasky    &usb_full_ddesc, 0, "USB always read complete device descriptor, if set");
82248246Shselasky
83207077Sthompsa#ifdef USB_DEBUG
84208018Sthompsa#ifdef USB_REQ_DEBUG
85208018Sthompsa/* The following structures are used in connection to fault injection. */
86208018Sthompsastruct usb_ctrl_debug {
87208018Sthompsa	int bus_index;		/* target bus */
88208018Sthompsa	int dev_index;		/* target address */
89208018Sthompsa	int ds_fail;		/* fail data stage */
90240750Shselasky	int ss_fail;		/* fail status stage */
91208018Sthompsa	int ds_delay;		/* data stage delay in ms */
92208018Sthompsa	int ss_delay;		/* status stage delay in ms */
93208018Sthompsa	int bmRequestType_value;
94208018Sthompsa	int bRequest_value;
95208018Sthompsa};
96208018Sthompsa
97208018Sthompsastruct usb_ctrl_debug_bits {
98208018Sthompsa	uint16_t ds_delay;
99208018Sthompsa	uint16_t ss_delay;
100208018Sthompsa	uint8_t ds_fail:1;
101208018Sthompsa	uint8_t ss_fail:1;
102208018Sthompsa	uint8_t enabled:1;
103208018Sthompsa};
104208018Sthompsa
105208018Sthompsa/* The default is to disable fault injection. */
106208018Sthompsa
107208018Sthompsastatic struct usb_ctrl_debug usb_ctrl_debug = {
108208018Sthompsa	.bus_index = -1,
109208018Sthompsa	.dev_index = -1,
110208018Sthompsa	.bmRequestType_value = -1,
111208018Sthompsa	.bRequest_value = -1,
112208018Sthompsa};
113208018Sthompsa
114208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW,
115208018Sthompsa    &usb_ctrl_debug.bus_index, 0, "USB controller index to fail");
116208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW,
117208018Sthompsa    &usb_ctrl_debug.dev_index, 0, "USB device address to fail");
118208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW,
119208018Sthompsa    &usb_ctrl_debug.ds_fail, 0, "USB fail data stage");
120208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW,
121208018Sthompsa    &usb_ctrl_debug.ss_fail, 0, "USB fail status stage");
122208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW,
123208018Sthompsa    &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms");
124208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW,
125208018Sthompsa    &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms");
126208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW,
127208018Sthompsa    &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail");
128208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW,
129208018Sthompsa    &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail");
130208018Sthompsa
131184610Salfred/*------------------------------------------------------------------------*
132208018Sthompsa *	usbd_get_debug_bits
133208018Sthompsa *
134208018Sthompsa * This function is only useful in USB host mode.
135208018Sthompsa *------------------------------------------------------------------------*/
136208018Sthompsastatic void
137208018Sthompsausbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
138208018Sthompsa    struct usb_ctrl_debug_bits *dbg)
139208018Sthompsa{
140208018Sthompsa	int temp;
141208018Sthompsa
142208018Sthompsa	memset(dbg, 0, sizeof(*dbg));
143208018Sthompsa
144208018Sthompsa	/* Compute data stage delay */
145208018Sthompsa
146208018Sthompsa	temp = usb_ctrl_debug.ds_delay;
147208018Sthompsa	if (temp < 0)
148208018Sthompsa		temp = 0;
149208018Sthompsa	else if (temp > (16*1024))
150208018Sthompsa		temp = (16*1024);
151208018Sthompsa
152208018Sthompsa	dbg->ds_delay = temp;
153208018Sthompsa
154208018Sthompsa	/* Compute status stage delay */
155208018Sthompsa
156208018Sthompsa	temp = usb_ctrl_debug.ss_delay;
157208018Sthompsa	if (temp < 0)
158208018Sthompsa		temp = 0;
159208018Sthompsa	else if (temp > (16*1024))
160208018Sthompsa		temp = (16*1024);
161208018Sthompsa
162208018Sthompsa	dbg->ss_delay = temp;
163208018Sthompsa
164208018Sthompsa	/* Check if this control request should be failed */
165208018Sthompsa
166208018Sthompsa	if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
167208018Sthompsa		return;
168208018Sthompsa
169208018Sthompsa	if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
170208018Sthompsa		return;
171208018Sthompsa
172208018Sthompsa	temp = usb_ctrl_debug.bmRequestType_value;
173208018Sthompsa
174208018Sthompsa	if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
175208018Sthompsa		return;
176208018Sthompsa
177208018Sthompsa	temp = usb_ctrl_debug.bRequest_value;
178208018Sthompsa
179208018Sthompsa	if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
180208018Sthompsa		return;
181208018Sthompsa
182208018Sthompsa	temp = usb_ctrl_debug.ds_fail;
183208018Sthompsa	if (temp)
184208018Sthompsa		dbg->ds_fail = 1;
185208018Sthompsa
186208018Sthompsa	temp = usb_ctrl_debug.ss_fail;
187208018Sthompsa	if (temp)
188208018Sthompsa		dbg->ss_fail = 1;
189208018Sthompsa
190208018Sthompsa	dbg->enabled = 1;
191208018Sthompsa}
192208018Sthompsa#endif	/* USB_REQ_DEBUG */
193208018Sthompsa#endif	/* USB_DEBUG */
194208018Sthompsa
195208018Sthompsa/*------------------------------------------------------------------------*
196194228Sthompsa *	usbd_do_request_callback
197184610Salfred *
198184610Salfred * This function is the USB callback for generic USB Host control
199184610Salfred * transfers.
200184610Salfred *------------------------------------------------------------------------*/
201184610Salfredvoid
202194677Sthompsausbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
203184610Salfred{
204184610Salfred	;				/* workaround for a bug in "indent" */
205184610Salfred
206184610Salfred	DPRINTF("st=%u\n", USB_GET_STATE(xfer));
207184610Salfred
208184610Salfred	switch (USB_GET_STATE(xfer)) {
209184610Salfred	case USB_ST_SETUP:
210194228Sthompsa		usbd_transfer_submit(xfer);
211184610Salfred		break;
212184610Salfred	default:
213207079Sthompsa		cv_signal(&xfer->xroot->udev->ctrlreq_cv);
214184610Salfred		break;
215184610Salfred	}
216184610Salfred}
217184610Salfred
218184610Salfred/*------------------------------------------------------------------------*
219194228Sthompsa *	usb_do_clear_stall_callback
220184610Salfred *
221184610Salfred * This function is the USB callback for generic clear stall requests.
222184610Salfred *------------------------------------------------------------------------*/
223184610Salfredvoid
224194677Sthompsausb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
225184610Salfred{
226192984Sthompsa	struct usb_device_request req;
227192984Sthompsa	struct usb_device *udev;
228193644Sthompsa	struct usb_endpoint *ep;
229193644Sthompsa	struct usb_endpoint *ep_end;
230193644Sthompsa	struct usb_endpoint *ep_first;
231239214Shselasky	usb_stream_t x;
232190731Sthompsa	uint8_t to;
233184610Salfred
234187173Sthompsa	udev = xfer->xroot->udev;
235184610Salfred
236187173Sthompsa	USB_BUS_LOCK(udev->bus);
237187173Sthompsa
238193644Sthompsa	/* round robin endpoint clear stall */
239184610Salfred
240193644Sthompsa	ep = udev->ep_curr;
241193644Sthompsa	ep_end = udev->endpoints + udev->endpoints_max;
242193644Sthompsa	ep_first = udev->endpoints;
243193644Sthompsa	to = udev->endpoints_max;
244193318Sthompsa
245184610Salfred	switch (USB_GET_STATE(xfer)) {
246184610Salfred	case USB_ST_TRANSFERRED:
247225000Shselaskytr_transferred:
248222786Shselasky		/* reset error counter */
249222786Shselasky		udev->clear_stall_errors = 0;
250222786Shselasky
251193644Sthompsa		if (ep == NULL)
252193318Sthompsa			goto tr_setup;		/* device was unconfigured */
253193644Sthompsa		if (ep->edesc &&
254193644Sthompsa		    ep->is_stalled) {
255193644Sthompsa			ep->toggle_next = 0;
256193644Sthompsa			ep->is_stalled = 0;
257213435Shselasky			/* some hardware needs a callback to clear the data toggle */
258213435Shselasky			usbd_clear_stall_locked(udev, ep);
259239214Shselasky			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
260239214Shselasky				/* start the current or next transfer, if any */
261239214Shselasky				usb_command_wrapper(&ep->endpoint_q[x],
262239214Shselasky				    ep->endpoint_q[x].curr);
263239214Shselasky			}
264184610Salfred		}
265193644Sthompsa		ep++;
266184610Salfred
267184610Salfred	case USB_ST_SETUP:
268184610Salfredtr_setup:
269193318Sthompsa		if (to == 0)
270193644Sthompsa			break;			/* no endpoints - nothing to do */
271193644Sthompsa		if ((ep < ep_first) || (ep >= ep_end))
272193644Sthompsa			ep = ep_first;	/* endpoint wrapped around */
273193644Sthompsa		if (ep->edesc &&
274193644Sthompsa		    ep->is_stalled) {
275184610Salfred
276184610Salfred			/* setup a clear-stall packet */
277184610Salfred
278184610Salfred			req.bmRequestType = UT_WRITE_ENDPOINT;
279184610Salfred			req.bRequest = UR_CLEAR_FEATURE;
280184610Salfred			USETW(req.wValue, UF_ENDPOINT_HALT);
281193644Sthompsa			req.wIndex[0] = ep->edesc->bEndpointAddress;
282184610Salfred			req.wIndex[1] = 0;
283184610Salfred			USETW(req.wLength, 0);
284184610Salfred
285184610Salfred			/* copy in the transfer */
286184610Salfred
287194228Sthompsa			usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
288184610Salfred
289184610Salfred			/* set length */
290194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
291184610Salfred			xfer->nframes = 1;
292187173Sthompsa			USB_BUS_UNLOCK(udev->bus);
293184610Salfred
294194228Sthompsa			usbd_transfer_submit(xfer);
295184610Salfred
296187173Sthompsa			USB_BUS_LOCK(udev->bus);
297184610Salfred			break;
298184610Salfred		}
299193644Sthompsa		ep++;
300193318Sthompsa		to--;
301193318Sthompsa		goto tr_setup;
302184610Salfred
303184610Salfred	default:
304222786Shselasky		if (error == USB_ERR_CANCELLED)
305184610Salfred			break;
306222786Shselasky
307222786Shselasky		DPRINTF("Clear stall failed.\n");
308225000Shselasky
309225000Shselasky		/*
310225000Shselasky		 * Some VMs like VirtualBox always return failure on
311225000Shselasky		 * clear-stall which we sometimes should just ignore.
312225000Shselasky		 */
313225000Shselasky		if (usb_no_cs_fail)
314225000Shselasky			goto tr_transferred;
315222786Shselasky		if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
316222786Shselasky			goto tr_setup;
317222786Shselasky
318222786Shselasky		if (error == USB_ERR_TIMEOUT) {
319222786Shselasky			udev->clear_stall_errors = USB_CS_RESET_LIMIT;
320222786Shselasky			DPRINTF("Trying to re-enumerate.\n");
321222786Shselasky			usbd_start_re_enumerate(udev);
322222786Shselasky		} else {
323222786Shselasky			udev->clear_stall_errors++;
324222786Shselasky			if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
325222786Shselasky				DPRINTF("Trying to re-enumerate.\n");
326222786Shselasky				usbd_start_re_enumerate(udev);
327222786Shselasky			}
328184610Salfred		}
329184610Salfred		goto tr_setup;
330184610Salfred	}
331184610Salfred
332193644Sthompsa	/* store current endpoint */
333193644Sthompsa	udev->ep_curr = ep;
334187173Sthompsa	USB_BUS_UNLOCK(udev->bus);
335184610Salfred}
336184610Salfred
337193045Sthompsastatic usb_handle_req_t *
338194228Sthompsausbd_get_hr_func(struct usb_device *udev)
339191402Sthompsa{
340191402Sthompsa	/* figure out if there is a Handle Request function */
341192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE)
342194228Sthompsa		return (usb_temp_get_desc_p);
343191402Sthompsa	else if (udev->parent_hub == NULL)
344191402Sthompsa		return (udev->bus->methods->roothub_exec);
345191402Sthompsa	else
346191402Sthompsa		return (NULL);
347191402Sthompsa}
348191402Sthompsa
349184610Salfred/*------------------------------------------------------------------------*
350194228Sthompsa *	usbd_do_request_flags and usbd_do_request
351184610Salfred *
352184610Salfred * Description of arguments passed to these functions:
353184610Salfred *
354192984Sthompsa * "udev" - this is the "usb_device" structure pointer on which the
355184610Salfred * request should be performed. It is possible to call this function
356184610Salfred * in both Host Side mode and Device Side mode.
357184610Salfred *
358184610Salfred * "mtx" - if this argument is non-NULL the mutex pointed to by it
359184610Salfred * will get dropped and picked up during the execution of this
360184610Salfred * function, hence this function sometimes needs to sleep. If this
361184610Salfred * argument is NULL it has no effect.
362184610Salfred *
363184610Salfred * "req" - this argument must always be non-NULL and points to an
364184610Salfred * 8-byte structure holding the USB request to be done. The USB
365184610Salfred * request structure has a bit telling the direction of the USB
366184610Salfred * request, if it is a read or a write.
367184610Salfred *
368184610Salfred * "data" - if the "wLength" part of the structure pointed to by "req"
369184610Salfred * is non-zero this argument must point to a valid kernel buffer which
370184610Salfred * can hold at least "wLength" bytes. If "wLength" is zero "data" can
371184610Salfred * be NULL.
372184610Salfred *
373184610Salfred * "flags" - here is a list of valid flags:
374184610Salfred *
375184610Salfred *  o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
376184610Salfred *  specified
377184610Salfred *
378184610Salfred *  o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
379184610Salfred *  at a later point in time. This is tunable by the "hw.usb.ss_delay"
380184610Salfred *  sysctl. This flag is mostly useful for debugging.
381184610Salfred *
382184610Salfred *  o USB_USER_DATA_PTR: treat the "data" pointer like a userland
383184610Salfred *  pointer.
384184610Salfred *
385184610Salfred * "actlen" - if non-NULL the actual transfer length will be stored in
386184610Salfred * the 16-bit unsigned integer pointed to by "actlen". This
387184610Salfred * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
388184610Salfred * used.
389184610Salfred *
390184610Salfred * "timeout" - gives the timeout for the control transfer in
391184610Salfred * milliseconds. A "timeout" value less than 50 milliseconds is
392184610Salfred * treated like a 50 millisecond timeout. A "timeout" value greater
393184610Salfred * than 30 seconds is treated like a 30 second timeout. This USB stack
394184610Salfred * does not allow control requests without a timeout.
395184610Salfred *
396246759Shselasky * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
397246759Shselasky * will be serialized by the use of the USB device enumeration lock.
398184610Salfred *
399184610Salfred * Returns:
400184610Salfred *    0: Success
401184610Salfred * Else: Failure
402184610Salfred *------------------------------------------------------------------------*/
403193045Sthompsausb_error_t
404194228Sthompsausbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
405192984Sthompsa    struct usb_device_request *req, void *data, uint16_t flags,
406193045Sthompsa    uint16_t *actlen, usb_timeout_t timeout)
407184610Salfred{
408208018Sthompsa#ifdef USB_REQ_DEBUG
409208018Sthompsa	struct usb_ctrl_debug_bits dbg;
410208018Sthompsa#endif
411193045Sthompsa	usb_handle_req_t *hr_func;
412192984Sthompsa	struct usb_xfer *xfer;
413184610Salfred	const void *desc;
414184610Salfred	int err = 0;
415193045Sthompsa	usb_ticks_t start_ticks;
416193045Sthompsa	usb_ticks_t delta_ticks;
417193045Sthompsa	usb_ticks_t max_ticks;
418184610Salfred	uint16_t length;
419184610Salfred	uint16_t temp;
420208018Sthompsa	uint16_t acttemp;
421246759Shselasky	uint8_t do_unlock;
422184610Salfred
423184610Salfred	if (timeout < 50) {
424184610Salfred		/* timeout is too small */
425184610Salfred		timeout = 50;
426184610Salfred	}
427184610Salfred	if (timeout > 30000) {
428184610Salfred		/* timeout is too big */
429184610Salfred		timeout = 30000;
430184610Salfred	}
431184610Salfred	length = UGETW(req->wLength);
432184610Salfred
433184610Salfred	DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
434184610Salfred	    "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
435184610Salfred	    udev, req->bmRequestType, req->bRequest,
436184610Salfred	    req->wValue[1], req->wValue[0],
437184610Salfred	    req->wIndex[1], req->wIndex[0],
438184610Salfred	    req->wLength[1], req->wLength[0]);
439184610Salfred
440191494Sthompsa	/* Check if the device is still alive */
441191494Sthompsa	if (udev->state < USB_STATE_POWERED) {
442191494Sthompsa		DPRINTF("usb device has gone\n");
443191494Sthompsa		return (USB_ERR_NOT_CONFIGURED);
444191494Sthompsa	}
445191494Sthompsa
446184610Salfred	/*
447184610Salfred	 * Set "actlen" to a known value in case the caller does not
448184610Salfred	 * check the return value:
449184610Salfred	 */
450190735Sthompsa	if (actlen)
451184610Salfred		*actlen = 0;
452190735Sthompsa
453190180Sthompsa#if (USB_HAVE_USER_IO == 0)
454190180Sthompsa	if (flags & USB_USER_DATA_PTR)
455190180Sthompsa		return (USB_ERR_INVAL);
456190180Sthompsa#endif
457208008Sthompsa	if ((mtx != NULL) && (mtx != &Giant)) {
458184610Salfred		mtx_unlock(mtx);
459208008Sthompsa		mtx_assert(mtx, MA_NOTOWNED);
460184610Salfred	}
461208008Sthompsa
462184610Salfred	/*
463246759Shselasky	 * Grab the USB device enumeration SX-lock serialization is
464246759Shselasky	 * achieved when multiple threads are involved:
465208008Sthompsa	 */
466246759Shselasky	do_unlock = usbd_enum_lock(udev);
467208008Sthompsa
468208008Sthompsa	/*
469246759Shselasky	 * We need to allow suspend and resume at this point, else the
470246759Shselasky	 * control transfer will timeout if the device is suspended!
471184610Salfred	 */
472246759Shselasky	usbd_sr_unlock(udev);
473184610Salfred
474194228Sthompsa	hr_func = usbd_get_hr_func(udev);
475190735Sthompsa
476191402Sthompsa	if (hr_func != NULL) {
477191402Sthompsa		DPRINTF("Handle Request function is set\n");
478190735Sthompsa
479191402Sthompsa		desc = NULL;
480191402Sthompsa		temp = 0;
481191402Sthompsa
482191402Sthompsa		if (!(req->bmRequestType & UT_READ)) {
483190735Sthompsa			if (length != 0) {
484191402Sthompsa				DPRINTFN(1, "The handle request function "
485191402Sthompsa				    "does not support writing data!\n");
486191402Sthompsa				err = USB_ERR_INVAL;
487191402Sthompsa				goto done;
488190735Sthompsa			}
489190735Sthompsa		}
490190735Sthompsa
491191402Sthompsa		/* The root HUB code needs the BUS lock locked */
492191402Sthompsa
493190735Sthompsa		USB_BUS_LOCK(udev->bus);
494191402Sthompsa		err = (hr_func) (udev, req, &desc, &temp);
495190735Sthompsa		USB_BUS_UNLOCK(udev->bus);
496190735Sthompsa
497190735Sthompsa		if (err)
498190735Sthompsa			goto done;
499190735Sthompsa
500191402Sthompsa		if (length > temp) {
501190735Sthompsa			if (!(flags & USB_SHORT_XFER_OK)) {
502190735Sthompsa				err = USB_ERR_SHORT_XFER;
503190735Sthompsa				goto done;
504190735Sthompsa			}
505191402Sthompsa			length = temp;
506190735Sthompsa		}
507190735Sthompsa		if (actlen)
508190735Sthompsa			*actlen = length;
509190735Sthompsa
510190735Sthompsa		if (length > 0) {
511190735Sthompsa#if USB_HAVE_USER_IO
512190735Sthompsa			if (flags & USB_USER_DATA_PTR) {
513191402Sthompsa				if (copyout(desc, data, length)) {
514190735Sthompsa					err = USB_ERR_INVAL;
515190735Sthompsa					goto done;
516190735Sthompsa				}
517190735Sthompsa			} else
518190735Sthompsa#endif
519227461Shselasky				memcpy(data, desc, length);
520190735Sthompsa		}
521191402Sthompsa		goto done;		/* success */
522190735Sthompsa	}
523190735Sthompsa
524184610Salfred	/*
525184610Salfred	 * Setup a new USB transfer or use the existing one, if any:
526184610Salfred	 */
527207080Sthompsa	usbd_ctrl_transfer_setup(udev);
528184610Salfred
529207080Sthompsa	xfer = udev->ctrl_xfer[0];
530184610Salfred	if (xfer == NULL) {
531184610Salfred		/* most likely out of memory */
532184610Salfred		err = USB_ERR_NOMEM;
533184610Salfred		goto done;
534184610Salfred	}
535208018Sthompsa
536208018Sthompsa#ifdef USB_REQ_DEBUG
537208018Sthompsa	/* Get debug bits */
538208018Sthompsa	usbd_get_debug_bits(udev, req, &dbg);
539208018Sthompsa
540208018Sthompsa	/* Check for fault injection */
541208018Sthompsa	if (dbg.enabled)
542208018Sthompsa		flags |= USB_DELAY_STATUS_STAGE;
543208018Sthompsa#endif
544184824Sthompsa	USB_XFER_LOCK(xfer);
545184610Salfred
546190734Sthompsa	if (flags & USB_DELAY_STATUS_STAGE)
547184610Salfred		xfer->flags.manual_status = 1;
548190734Sthompsa	else
549184610Salfred		xfer->flags.manual_status = 0;
550184610Salfred
551190734Sthompsa	if (flags & USB_SHORT_XFER_OK)
552190734Sthompsa		xfer->flags.short_xfer_ok = 1;
553190734Sthompsa	else
554190734Sthompsa		xfer->flags.short_xfer_ok = 0;
555190734Sthompsa
556184610Salfred	xfer->timeout = timeout;
557184610Salfred
558184610Salfred	start_ticks = ticks;
559184610Salfred
560184610Salfred	max_ticks = USB_MS_TO_TICKS(timeout);
561184610Salfred
562194228Sthompsa	usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
563184610Salfred
564194677Sthompsa	usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
565184610Salfred
566184610Salfred	while (1) {
567184610Salfred		temp = length;
568208018Sthompsa		if (temp > usbd_xfer_max_len(xfer)) {
569194677Sthompsa			temp = usbd_xfer_max_len(xfer);
570184610Salfred		}
571208018Sthompsa#ifdef USB_REQ_DEBUG
572208018Sthompsa		if (xfer->flags.manual_status) {
573208018Sthompsa			if (usbd_xfer_frame_len(xfer, 0) != 0) {
574208018Sthompsa				/* Execute data stage separately */
575208018Sthompsa				temp = 0;
576208018Sthompsa			} else if (temp > 0) {
577208018Sthompsa				if (dbg.ds_fail) {
578208018Sthompsa					err = USB_ERR_INVAL;
579208018Sthompsa					break;
580208018Sthompsa				}
581208018Sthompsa				if (dbg.ds_delay > 0) {
582208018Sthompsa					usb_pause_mtx(
583208018Sthompsa					    xfer->xroot->xfer_mtx,
584208018Sthompsa				            USB_MS_TO_TICKS(dbg.ds_delay));
585208018Sthompsa					/* make sure we don't time out */
586208018Sthompsa					start_ticks = ticks;
587208018Sthompsa				}
588208018Sthompsa			}
589208018Sthompsa		}
590208018Sthompsa#endif
591194677Sthompsa		usbd_xfer_set_frame_len(xfer, 1, temp);
592184610Salfred
593184610Salfred		if (temp > 0) {
594184610Salfred			if (!(req->bmRequestType & UT_READ)) {
595190180Sthompsa#if USB_HAVE_USER_IO
596184610Salfred				if (flags & USB_USER_DATA_PTR) {
597184824Sthompsa					USB_XFER_UNLOCK(xfer);
598194228Sthompsa					err = usbd_copy_in_user(xfer->frbuffers + 1,
599184610Salfred					    0, data, temp);
600184824Sthompsa					USB_XFER_LOCK(xfer);
601184610Salfred					if (err) {
602184610Salfred						err = USB_ERR_INVAL;
603184610Salfred						break;
604184610Salfred					}
605190180Sthompsa				} else
606190180Sthompsa#endif
607194228Sthompsa					usbd_copy_in(xfer->frbuffers + 1,
608190180Sthompsa					    0, data, temp);
609184610Salfred			}
610208018Sthompsa			usbd_xfer_set_frames(xfer, 2);
611184610Salfred		} else {
612208018Sthompsa			if (usbd_xfer_frame_len(xfer, 0) == 0) {
613184610Salfred				if (xfer->flags.manual_status) {
614208018Sthompsa#ifdef USB_REQ_DEBUG
615208018Sthompsa					if (dbg.ss_fail) {
616208018Sthompsa						err = USB_ERR_INVAL;
617208018Sthompsa						break;
618184610Salfred					}
619208018Sthompsa					if (dbg.ss_delay > 0) {
620194228Sthompsa						usb_pause_mtx(
621187173Sthompsa						    xfer->xroot->xfer_mtx,
622208018Sthompsa						    USB_MS_TO_TICKS(dbg.ss_delay));
623208018Sthompsa						/* make sure we don't time out */
624208018Sthompsa						start_ticks = ticks;
625184610Salfred					}
626184610Salfred#endif
627184610Salfred					xfer->flags.manual_status = 0;
628184610Salfred				} else {
629184610Salfred					break;
630184610Salfred				}
631184610Salfred			}
632208018Sthompsa			usbd_xfer_set_frames(xfer, 1);
633184610Salfred		}
634184610Salfred
635194228Sthompsa		usbd_transfer_start(xfer);
636184610Salfred
637194228Sthompsa		while (usbd_transfer_pending(xfer)) {
638207079Sthompsa			cv_wait(&udev->ctrlreq_cv,
639188983Sthompsa			    xfer->xroot->xfer_mtx);
640184610Salfred		}
641184610Salfred
642184610Salfred		err = xfer->error;
643184610Salfred
644184610Salfred		if (err) {
645184610Salfred			break;
646184610Salfred		}
647184610Salfred
648208018Sthompsa		/* get actual length of DATA stage */
649208018Sthompsa
650208018Sthompsa		if (xfer->aframes < 2) {
651208018Sthompsa			acttemp = 0;
652184610Salfred		} else {
653208018Sthompsa			acttemp = usbd_xfer_frame_len(xfer, 1);
654184610Salfred		}
655184610Salfred
656184610Salfred		/* check for short packet */
657184610Salfred
658208018Sthompsa		if (temp > acttemp) {
659208018Sthompsa			temp = acttemp;
660184610Salfred			length = temp;
661184610Salfred		}
662184610Salfred		if (temp > 0) {
663184610Salfred			if (req->bmRequestType & UT_READ) {
664190180Sthompsa#if USB_HAVE_USER_IO
665184610Salfred				if (flags & USB_USER_DATA_PTR) {
666184824Sthompsa					USB_XFER_UNLOCK(xfer);
667194228Sthompsa					err = usbd_copy_out_user(xfer->frbuffers + 1,
668184610Salfred					    0, data, temp);
669184824Sthompsa					USB_XFER_LOCK(xfer);
670184610Salfred					if (err) {
671184610Salfred						err = USB_ERR_INVAL;
672184610Salfred						break;
673184610Salfred					}
674190180Sthompsa				} else
675190180Sthompsa#endif
676194228Sthompsa					usbd_copy_out(xfer->frbuffers + 1,
677184610Salfred					    0, data, temp);
678184610Salfred			}
679184610Salfred		}
680184610Salfred		/*
681184610Salfred		 * Clear "frlengths[0]" so that we don't send the setup
682184610Salfred		 * packet again:
683184610Salfred		 */
684194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, 0);
685184610Salfred
686184610Salfred		/* update length and data pointer */
687184610Salfred		length -= temp;
688184610Salfred		data = USB_ADD_BYTES(data, temp);
689184610Salfred
690184610Salfred		if (actlen) {
691184610Salfred			(*actlen) += temp;
692184610Salfred		}
693184610Salfred		/* check for timeout */
694184610Salfred
695184610Salfred		delta_ticks = ticks - start_ticks;
696184610Salfred		if (delta_ticks > max_ticks) {
697184610Salfred			if (!err) {
698184610Salfred				err = USB_ERR_TIMEOUT;
699184610Salfred			}
700184610Salfred		}
701184610Salfred		if (err) {
702184610Salfred			break;
703184610Salfred		}
704184610Salfred	}
705184610Salfred
706184610Salfred	if (err) {
707184610Salfred		/*
708184610Salfred		 * Make sure that the control endpoint is no longer
709184610Salfred		 * blocked in case of a non-transfer related error:
710184610Salfred		 */
711194228Sthompsa		usbd_transfer_stop(xfer);
712184610Salfred	}
713184824Sthompsa	USB_XFER_UNLOCK(xfer);
714184610Salfred
715184610Salfreddone:
716246759Shselasky	usbd_sr_lock(udev);
717184610Salfred
718246759Shselasky	if (do_unlock)
719246759Shselasky		usbd_enum_unlock(udev);
720208008Sthompsa
721208008Sthompsa	if ((mtx != NULL) && (mtx != &Giant))
722184610Salfred		mtx_lock(mtx);
723208008Sthompsa
724260589Shselasky	switch (err) {
725260589Shselasky	case USB_ERR_NORMAL_COMPLETION:
726260589Shselasky	case USB_ERR_SHORT_XFER:
727260589Shselasky	case USB_ERR_STALLED:
728260589Shselasky	case USB_ERR_CANCELLED:
729260589Shselasky		break;
730260589Shselasky	default:
731260589Shselasky		DPRINTF("I/O error - waiting a bit for TT cleanup\n");
732260589Shselasky		usb_pause_mtx(mtx, hz / 16);
733260589Shselasky		break;
734260589Shselasky	}
735193045Sthompsa	return ((usb_error_t)err);
736184610Salfred}
737184610Salfred
738184610Salfred/*------------------------------------------------------------------------*
739194228Sthompsa *	usbd_do_request_proc - factored out code
740188411Sthompsa *
741188411Sthompsa * This function is factored out code. It does basically the same like
742194228Sthompsa * usbd_do_request_flags, except it will check the status of the
743188411Sthompsa * passed process argument before doing the USB request. If the
744188411Sthompsa * process is draining the USB_ERR_IOERROR code will be returned. It
745188411Sthompsa * is assumed that the mutex associated with the process is locked
746188411Sthompsa * when calling this function.
747188411Sthompsa *------------------------------------------------------------------------*/
748193045Sthompsausb_error_t
749194228Sthompsausbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
750192984Sthompsa    struct usb_device_request *req, void *data, uint16_t flags,
751193045Sthompsa    uint16_t *actlen, usb_timeout_t timeout)
752188411Sthompsa{
753193045Sthompsa	usb_error_t err;
754188411Sthompsa	uint16_t len;
755188411Sthompsa
756188411Sthompsa	/* get request data length */
757188411Sthompsa	len = UGETW(req->wLength);
758188411Sthompsa
759188411Sthompsa	/* check if the device is being detached */
760194228Sthompsa	if (usb_proc_is_gone(pproc)) {
761188411Sthompsa		err = USB_ERR_IOERROR;
762188411Sthompsa		goto done;
763188411Sthompsa	}
764188411Sthompsa
765188411Sthompsa	/* forward the USB request */
766194228Sthompsa	err = usbd_do_request_flags(udev, pproc->up_mtx,
767188411Sthompsa	    req, data, flags, actlen, timeout);
768188411Sthompsa
769188411Sthompsadone:
770188411Sthompsa	/* on failure we zero the data */
771188411Sthompsa	/* on short packet we zero the unused data */
772188411Sthompsa	if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
773188411Sthompsa		if (err)
774188411Sthompsa			memset(data, 0, len);
775188411Sthompsa		else if (actlen && *actlen != len)
776188411Sthompsa			memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
777188411Sthompsa	}
778188411Sthompsa	return (err);
779188411Sthompsa}
780188411Sthompsa
781188411Sthompsa/*------------------------------------------------------------------------*
782194228Sthompsa *	usbd_req_reset_port
783184610Salfred *
784214804Shselasky * This function will instruct a USB HUB to perform a reset sequence
785184610Salfred * on the specified port number.
786184610Salfred *
787184610Salfred * Returns:
788184610Salfred *    0: Success. The USB device should now be at address zero.
789184610Salfred * Else: Failure. No USB device is present and the USB port should be
790184610Salfred *       disabled.
791184610Salfred *------------------------------------------------------------------------*/
792193045Sthompsausb_error_t
793194228Sthompsausbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
794184610Salfred{
795192984Sthompsa	struct usb_port_status ps;
796193045Sthompsa	usb_error_t err;
797184610Salfred	uint16_t n;
798230091Shselasky	uint16_t status;
799230091Shselasky	uint16_t change;
800184610Salfred
801230091Shselasky	DPRINTF("\n");
802230091Shselasky
803224095Shselasky	/* clear any leftover port reset changes first */
804224095Shselasky	usbd_req_clear_port_feature(
805224095Shselasky	    udev, mtx, port, UHF_C_PORT_RESET);
806224095Shselasky
807224095Shselasky	/* assert port reset on the given port */
808224095Shselasky	err = usbd_req_set_port_feature(
809224095Shselasky	    udev, mtx, port, UHF_PORT_RESET);
810224095Shselasky
811224095Shselasky	/* check for errors */
812224095Shselasky	if (err)
813184610Salfred		goto done;
814184610Salfred	n = 0;
815184610Salfred	while (1) {
816184610Salfred		/* wait for the device to recover from reset */
817241987Shselasky		usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
818241987Shselasky		n += usb_port_reset_delay;
819194228Sthompsa		err = usbd_req_get_port_status(udev, mtx, &ps, port);
820230091Shselasky		if (err)
821184610Salfred			goto done;
822230091Shselasky
823224095Shselasky		status = UGETW(ps.wPortStatus);
824224095Shselasky		change = UGETW(ps.wPortChange);
825224095Shselasky
826216249Shselasky		/* if the device disappeared, just give up */
827224095Shselasky		if (!(status & UPS_CURRENT_CONNECT_STATUS))
828216249Shselasky			goto done;
829224095Shselasky
830214804Shselasky		/* check if reset is complete */
831224095Shselasky		if (change & UPS_C_PORT_RESET)
832214804Shselasky			break;
833224095Shselasky
834224095Shselasky		/*
835224095Shselasky		 * Some Virtual Machines like VirtualBox 4.x fail to
836224095Shselasky		 * generate a port reset change event. Check if reset
837224095Shselasky		 * is no longer asserted.
838224095Shselasky		 */
839224095Shselasky		if (!(status & UPS_RESET))
840224095Shselasky			break;
841224095Shselasky
842214804Shselasky		/* check for timeout */
843214804Shselasky		if (n > 1000) {
844214804Shselasky			n = 0;
845214804Shselasky			break;
846214804Shselasky		}
847214804Shselasky	}
848214804Shselasky
849214804Shselasky	/* clear port reset first */
850214804Shselasky	err = usbd_req_clear_port_feature(
851214804Shselasky	    udev, mtx, port, UHF_C_PORT_RESET);
852230091Shselasky	if (err)
853214804Shselasky		goto done;
854230091Shselasky
855214804Shselasky	/* check for timeout */
856214804Shselasky	if (n == 0) {
857214804Shselasky		err = USB_ERR_TIMEOUT;
858214804Shselasky		goto done;
859214804Shselasky	}
860214804Shselasky	/* wait for the device to recover from reset */
861241987Shselasky	usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
862214804Shselasky
863214804Shselaskydone:
864214804Shselasky	DPRINTFN(2, "port %d reset returning error=%s\n",
865214804Shselasky	    port, usbd_errstr(err));
866214804Shselasky	return (err);
867214804Shselasky}
868214804Shselasky
869214804Shselasky/*------------------------------------------------------------------------*
870214804Shselasky *	usbd_req_warm_reset_port
871214804Shselasky *
872214804Shselasky * This function will instruct an USB HUB to perform a warm reset
873214804Shselasky * sequence on the specified port number. This kind of reset is not
874214804Shselasky * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
875214804Shselasky * for SUPER-speed USB HUBs.
876214804Shselasky *
877214804Shselasky * Returns:
878214804Shselasky *    0: Success. The USB device should now be available again.
879214804Shselasky * Else: Failure. No USB device is present and the USB port should be
880214804Shselasky *       disabled.
881214804Shselasky *------------------------------------------------------------------------*/
882214804Shselaskyusb_error_t
883230091Shselaskyusbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
884230091Shselasky    uint8_t port)
885214804Shselasky{
886214804Shselasky	struct usb_port_status ps;
887214804Shselasky	usb_error_t err;
888214804Shselasky	uint16_t n;
889230091Shselasky	uint16_t status;
890230091Shselasky	uint16_t change;
891214804Shselasky
892230091Shselasky	DPRINTF("\n");
893230091Shselasky
894230091Shselasky	err = usbd_req_get_port_status(udev, mtx, &ps, port);
895230091Shselasky	if (err)
896214804Shselasky		goto done;
897230091Shselasky
898230091Shselasky	status = UGETW(ps.wPortStatus);
899230091Shselasky
900230091Shselasky	switch (UPS_PORT_LINK_STATE_GET(status)) {
901230091Shselasky	case UPS_PORT_LS_U3:
902230091Shselasky	case UPS_PORT_LS_COMP_MODE:
903230091Shselasky	case UPS_PORT_LS_LOOPBACK:
904230091Shselasky	case UPS_PORT_LS_SS_INA:
905230091Shselasky		break;
906230091Shselasky	default:
907230091Shselasky		DPRINTF("Wrong state for warm reset\n");
908230091Shselasky		return (0);
909214804Shselasky	}
910230091Shselasky
911230091Shselasky	/* clear any leftover warm port reset changes first */
912230091Shselasky	usbd_req_clear_port_feature(udev, mtx,
913230091Shselasky	    port, UHF_C_BH_PORT_RESET);
914230091Shselasky
915230091Shselasky	/* set warm port reset */
916230091Shselasky	err = usbd_req_set_port_feature(udev, mtx,
917230091Shselasky	    port, UHF_BH_PORT_RESET);
918230091Shselasky	if (err)
919230091Shselasky		goto done;
920230091Shselasky
921214804Shselasky	n = 0;
922214804Shselasky	while (1) {
923214804Shselasky		/* wait for the device to recover from reset */
924241987Shselasky		usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
925241987Shselasky		n += usb_port_reset_delay;
926214804Shselasky		err = usbd_req_get_port_status(udev, mtx, &ps, port);
927230091Shselasky		if (err)
928214804Shselasky			goto done;
929230091Shselasky
930230091Shselasky		status = UGETW(ps.wPortStatus);
931230091Shselasky		change = UGETW(ps.wPortChange);
932230091Shselasky
933184610Salfred		/* if the device disappeared, just give up */
934230091Shselasky		if (!(status & UPS_CURRENT_CONNECT_STATUS))
935184610Salfred			goto done;
936230091Shselasky
937184610Salfred		/* check if reset is complete */
938230091Shselasky		if (change & UPS_C_BH_PORT_RESET)
939184610Salfred			break;
940230091Shselasky
941184610Salfred		/* check for timeout */
942184610Salfred		if (n > 1000) {
943184610Salfred			n = 0;
944184610Salfred			break;
945184610Salfred		}
946184610Salfred	}
947184610Salfred
948184610Salfred	/* clear port reset first */
949194228Sthompsa	err = usbd_req_clear_port_feature(
950214804Shselasky	    udev, mtx, port, UHF_C_BH_PORT_RESET);
951230091Shselasky	if (err)
952184610Salfred		goto done;
953230091Shselasky
954184610Salfred	/* check for timeout */
955184610Salfred	if (n == 0) {
956184610Salfred		err = USB_ERR_TIMEOUT;
957184610Salfred		goto done;
958184610Salfred	}
959184610Salfred	/* wait for the device to recover from reset */
960241987Shselasky	usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
961184610Salfred
962184610Salfreddone:
963214804Shselasky	DPRINTFN(2, "port %d warm reset returning error=%s\n",
964194228Sthompsa	    port, usbd_errstr(err));
965184610Salfred	return (err);
966184610Salfred}
967184610Salfred
968184610Salfred/*------------------------------------------------------------------------*
969194228Sthompsa *	usbd_req_get_desc
970184610Salfred *
971184610Salfred * This function can be used to retrieve USB descriptors. It contains
972184610Salfred * some additional logic like zeroing of missing descriptor bytes and
973184610Salfred * retrying an USB descriptor in case of failure. The "min_len"
974184610Salfred * argument specifies the minimum descriptor length. The "max_len"
975184610Salfred * argument specifies the maximum descriptor length. If the real
976184610Salfred * descriptor length is less than the minimum length the missing
977188985Sthompsa * byte(s) will be zeroed. The type field, the second byte of the USB
978188985Sthompsa * descriptor, will get forced to the correct type. If the "actlen"
979188985Sthompsa * pointer is non-NULL, the actual length of the transfer will get
980188985Sthompsa * stored in the 16-bit unsigned integer which it is pointing to. The
981188985Sthompsa * first byte of the descriptor will not get updated. If the "actlen"
982188985Sthompsa * pointer is NULL the first byte of the descriptor will get updated
983188985Sthompsa * to reflect the actual length instead. If "min_len" is not equal to
984188985Sthompsa * "max_len" then this function will try to retrive the beginning of
985188985Sthompsa * the descriptor and base the maximum length on the first byte of the
986188985Sthompsa * descriptor.
987184610Salfred *
988184610Salfred * Returns:
989184610Salfred *    0: Success
990184610Salfred * Else: Failure
991184610Salfred *------------------------------------------------------------------------*/
992193045Sthompsausb_error_t
993194228Sthompsausbd_req_get_desc(struct usb_device *udev,
994188985Sthompsa    struct mtx *mtx, uint16_t *actlen, void *desc,
995184610Salfred    uint16_t min_len, uint16_t max_len,
996184610Salfred    uint16_t id, uint8_t type, uint8_t index,
997184610Salfred    uint8_t retries)
998184610Salfred{
999192984Sthompsa	struct usb_device_request req;
1000184610Salfred	uint8_t *buf;
1001193045Sthompsa	usb_error_t err;
1002184610Salfred
1003184610Salfred	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
1004184610Salfred	    id, type, index, max_len);
1005184610Salfred
1006184610Salfred	req.bmRequestType = UT_READ_DEVICE;
1007184610Salfred	req.bRequest = UR_GET_DESCRIPTOR;
1008184610Salfred	USETW2(req.wValue, type, index);
1009184610Salfred	USETW(req.wIndex, id);
1010184610Salfred
1011184610Salfred	while (1) {
1012184610Salfred
1013184610Salfred		if ((min_len < 2) || (max_len < 2)) {
1014184610Salfred			err = USB_ERR_INVAL;
1015184610Salfred			goto done;
1016184610Salfred		}
1017184610Salfred		USETW(req.wLength, min_len);
1018184610Salfred
1019194228Sthompsa		err = usbd_do_request_flags(udev, mtx, &req,
1020248246Shselasky		    desc, 0, NULL, 500 /* ms */);
1021184610Salfred
1022184610Salfred		if (err) {
1023184610Salfred			if (!retries) {
1024184610Salfred				goto done;
1025184610Salfred			}
1026184610Salfred			retries--;
1027184610Salfred
1028194228Sthompsa			usb_pause_mtx(mtx, hz / 5);
1029184610Salfred
1030184610Salfred			continue;
1031184610Salfred		}
1032184610Salfred		buf = desc;
1033184610Salfred
1034184610Salfred		if (min_len == max_len) {
1035184610Salfred
1036188985Sthompsa			/* enforce correct length */
1037188985Sthompsa			if ((buf[0] > min_len) && (actlen == NULL))
1038188985Sthompsa				buf[0] = min_len;
1039184610Salfred
1040188985Sthompsa			/* enforce correct type */
1041184610Salfred			buf[1] = type;
1042184610Salfred
1043184610Salfred			goto done;
1044184610Salfred		}
1045184610Salfred		/* range check */
1046184610Salfred
1047184610Salfred		if (max_len > buf[0]) {
1048184610Salfred			max_len = buf[0];
1049184610Salfred		}
1050184610Salfred		/* zero minimum data */
1051184610Salfred
1052184610Salfred		while (min_len > max_len) {
1053184610Salfred			min_len--;
1054184610Salfred			buf[min_len] = 0;
1055184610Salfred		}
1056184610Salfred
1057184610Salfred		/* set new minimum length */
1058184610Salfred
1059184610Salfred		min_len = max_len;
1060184610Salfred	}
1061184610Salfreddone:
1062188985Sthompsa	if (actlen != NULL) {
1063188985Sthompsa		if (err)
1064188985Sthompsa			*actlen = 0;
1065188985Sthompsa		else
1066188985Sthompsa			*actlen = min_len;
1067188985Sthompsa	}
1068184610Salfred	return (err);
1069184610Salfred}
1070184610Salfred
1071184610Salfred/*------------------------------------------------------------------------*
1072194228Sthompsa *	usbd_req_get_string_any
1073184610Salfred *
1074184610Salfred * This function will return the string given by "string_index"
1075184610Salfred * using the first language ID. The maximum length "len" includes
1076184610Salfred * the terminating zero. The "len" argument should be twice as
1077184610Salfred * big pluss 2 bytes, compared with the actual maximum string length !
1078184610Salfred *
1079184610Salfred * Returns:
1080184610Salfred *    0: Success
1081184610Salfred * Else: Failure
1082184610Salfred *------------------------------------------------------------------------*/
1083193045Sthompsausb_error_t
1084194228Sthompsausbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1085184610Salfred    uint16_t len, uint8_t string_index)
1086184610Salfred{
1087184610Salfred	char *s;
1088184610Salfred	uint8_t *temp;
1089184610Salfred	uint16_t i;
1090184610Salfred	uint16_t n;
1091184610Salfred	uint16_t c;
1092184610Salfred	uint8_t swap;
1093193045Sthompsa	usb_error_t err;
1094184610Salfred
1095184610Salfred	if (len == 0) {
1096184610Salfred		/* should not happen */
1097184610Salfred		return (USB_ERR_NORMAL_COMPLETION);
1098184610Salfred	}
1099184610Salfred	if (string_index == 0) {
1100184610Salfred		/* this is the language table */
1101185087Salfred		buf[0] = 0;
1102184610Salfred		return (USB_ERR_INVAL);
1103184610Salfred	}
1104184610Salfred	if (udev->flags.no_strings) {
1105185087Salfred		buf[0] = 0;
1106184610Salfred		return (USB_ERR_STALLED);
1107184610Salfred	}
1108194228Sthompsa	err = usbd_req_get_string_desc
1109184610Salfred	    (udev, mtx, buf, len, udev->langid, string_index);
1110184610Salfred	if (err) {
1111185087Salfred		buf[0] = 0;
1112184610Salfred		return (err);
1113184610Salfred	}
1114184610Salfred	temp = (uint8_t *)buf;
1115184610Salfred
1116184610Salfred	if (temp[0] < 2) {
1117184610Salfred		/* string length is too short */
1118185087Salfred		buf[0] = 0;
1119184610Salfred		return (USB_ERR_INVAL);
1120184610Salfred	}
1121184610Salfred	/* reserve one byte for terminating zero */
1122184610Salfred	len--;
1123184610Salfred
1124184610Salfred	/* find maximum length */
1125184610Salfred	s = buf;
1126184610Salfred	n = (temp[0] / 2) - 1;
1127184610Salfred	if (n > len) {
1128184610Salfred		n = len;
1129184610Salfred	}
1130184610Salfred	/* skip descriptor header */
1131184610Salfred	temp += 2;
1132184610Salfred
1133184610Salfred	/* reset swap state */
1134184610Salfred	swap = 3;
1135184610Salfred
1136184610Salfred	/* convert and filter */
1137184610Salfred	for (i = 0; (i != n); i++) {
1138184610Salfred		c = UGETW(temp + (2 * i));
1139184610Salfred
1140184610Salfred		/* convert from Unicode, handle buggy strings */
1141184610Salfred		if (((c & 0xff00) == 0) && (swap & 1)) {
1142184610Salfred			/* Little Endian, default */
1143184610Salfred			*s = c;
1144184610Salfred			swap = 1;
1145184610Salfred		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
1146184610Salfred			/* Big Endian */
1147184610Salfred			*s = c >> 8;
1148184610Salfred			swap = 2;
1149184610Salfred		} else {
1150185087Salfred			/* silently skip bad character */
1151185087Salfred			continue;
1152184610Salfred		}
1153184610Salfred
1154184610Salfred		/*
1155213433Shselasky		 * Filter by default - We only allow alphanumerical
1156213433Shselasky		 * and a few more to avoid any problems with scripts
1157213433Shselasky		 * and daemons.
1158184610Salfred		 */
1159213433Shselasky		if (isalpha(*s) ||
1160213433Shselasky		    isdigit(*s) ||
1161213433Shselasky		    *s == '-' ||
1162213433Shselasky		    *s == '+' ||
1163213433Shselasky		    *s == ' ' ||
1164213433Shselasky		    *s == '.' ||
1165213433Shselasky		    *s == ',') {
1166213433Shselasky			/* allowed */
1167213433Shselasky			s++;
1168184610Salfred		}
1169213433Shselasky		/* silently skip bad character */
1170184610Salfred	}
1171185087Salfred	*s = 0;				/* zero terminate resulting string */
1172184610Salfred	return (USB_ERR_NORMAL_COMPLETION);
1173184610Salfred}
1174184610Salfred
1175184610Salfred/*------------------------------------------------------------------------*
1176194228Sthompsa *	usbd_req_get_string_desc
1177184610Salfred *
1178184610Salfred * If you don't know the language ID, consider using
1179194228Sthompsa * "usbd_req_get_string_any()".
1180184610Salfred *
1181184610Salfred * Returns:
1182184610Salfred *    0: Success
1183184610Salfred * Else: Failure
1184184610Salfred *------------------------------------------------------------------------*/
1185193045Sthompsausb_error_t
1186194228Sthompsausbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1187184610Salfred    uint16_t max_len, uint16_t lang_id,
1188184610Salfred    uint8_t string_index)
1189184610Salfred{
1190194228Sthompsa	return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1191184610Salfred	    UDESC_STRING, string_index, 0));
1192184610Salfred}
1193184610Salfred
1194184610Salfred/*------------------------------------------------------------------------*
1195194228Sthompsa *	usbd_req_get_config_desc_ptr
1196190727Sthompsa *
1197190727Sthompsa * This function is used in device side mode to retrieve the pointer
1198190727Sthompsa * to the generated config descriptor. This saves allocating space for
1199190727Sthompsa * an additional config descriptor when setting the configuration.
1200190727Sthompsa *
1201190727Sthompsa * Returns:
1202190727Sthompsa *    0: Success
1203190727Sthompsa * Else: Failure
1204190727Sthompsa *------------------------------------------------------------------------*/
1205193045Sthompsausb_error_t
1206194228Sthompsausbd_req_get_descriptor_ptr(struct usb_device *udev,
1207192984Sthompsa    struct usb_config_descriptor **ppcd, uint16_t wValue)
1208190727Sthompsa{
1209192984Sthompsa	struct usb_device_request req;
1210193045Sthompsa	usb_handle_req_t *hr_func;
1211191402Sthompsa	const void *ptr;
1212190727Sthompsa	uint16_t len;
1213193045Sthompsa	usb_error_t err;
1214190727Sthompsa
1215190731Sthompsa	req.bmRequestType = UT_READ_DEVICE;
1216190727Sthompsa	req.bRequest = UR_GET_DESCRIPTOR;
1217191402Sthompsa	USETW(req.wValue, wValue);
1218190727Sthompsa	USETW(req.wIndex, 0);
1219190727Sthompsa	USETW(req.wLength, 0);
1220190727Sthompsa
1221191402Sthompsa	ptr = NULL;
1222191402Sthompsa	len = 0;
1223190727Sthompsa
1224194228Sthompsa	hr_func = usbd_get_hr_func(udev);
1225191402Sthompsa
1226191402Sthompsa	if (hr_func == NULL)
1227191402Sthompsa		err = USB_ERR_INVAL;
1228191402Sthompsa	else {
1229191402Sthompsa		USB_BUS_LOCK(udev->bus);
1230191402Sthompsa		err = (hr_func) (udev, &req, &ptr, &len);
1231191402Sthompsa		USB_BUS_UNLOCK(udev->bus);
1232191402Sthompsa	}
1233191402Sthompsa
1234191402Sthompsa	if (err)
1235191402Sthompsa		ptr = NULL;
1236191402Sthompsa	else if (ptr == NULL)
1237191402Sthompsa		err = USB_ERR_INVAL;
1238191402Sthompsa
1239192984Sthompsa	*ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1240191402Sthompsa
1241191402Sthompsa	return (err);
1242190727Sthompsa}
1243190727Sthompsa
1244190727Sthompsa/*------------------------------------------------------------------------*
1245194228Sthompsa *	usbd_req_get_config_desc
1246184610Salfred *
1247184610Salfred * Returns:
1248184610Salfred *    0: Success
1249184610Salfred * Else: Failure
1250184610Salfred *------------------------------------------------------------------------*/
1251193045Sthompsausb_error_t
1252194228Sthompsausbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1253192984Sthompsa    struct usb_config_descriptor *d, uint8_t conf_index)
1254184610Salfred{
1255193045Sthompsa	usb_error_t err;
1256184610Salfred
1257184610Salfred	DPRINTFN(4, "confidx=%d\n", conf_index);
1258184610Salfred
1259194228Sthompsa	err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1260184610Salfred	    sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1261184610Salfred	if (err) {
1262184610Salfred		goto done;
1263184610Salfred	}
1264184610Salfred	/* Extra sanity checking */
1265233774Shselasky	if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1266184610Salfred		err = USB_ERR_INVAL;
1267184610Salfred	}
1268184610Salfreddone:
1269184610Salfred	return (err);
1270184610Salfred}
1271184610Salfred
1272184610Salfred/*------------------------------------------------------------------------*
1273250207Shselasky *	usbd_alloc_config_desc
1274250207Shselasky *
1275250207Shselasky * This function is used to allocate a zeroed configuration
1276250207Shselasky * descriptor.
1277250207Shselasky *
1278250207Shselasky * Returns:
1279250207Shselasky * NULL: Failure
1280250207Shselasky * Else: Success
1281250207Shselasky *------------------------------------------------------------------------*/
1282250207Shselaskyvoid *
1283250207Shselaskyusbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1284250207Shselasky{
1285250207Shselasky	if (size > USB_CONFIG_MAX) {
1286250207Shselasky		DPRINTF("Configuration descriptor too big\n");
1287250207Shselasky		return (NULL);
1288250207Shselasky	}
1289250207Shselasky#if (USB_HAVE_FIXED_CONFIG == 0)
1290250207Shselasky	return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1291250207Shselasky#else
1292250207Shselasky	memset(udev->config_data, 0, sizeof(udev->config_data));
1293250207Shselasky	return (udev->config_data);
1294250207Shselasky#endif
1295250207Shselasky}
1296250207Shselasky
1297250207Shselasky/*------------------------------------------------------------------------*
1298250207Shselasky *	usbd_alloc_config_desc
1299250207Shselasky *
1300250207Shselasky * This function is used to free a configuration descriptor.
1301250207Shselasky *------------------------------------------------------------------------*/
1302250207Shselaskyvoid
1303250207Shselaskyusbd_free_config_desc(struct usb_device *udev, void *ptr)
1304250207Shselasky{
1305250207Shselasky#if (USB_HAVE_FIXED_CONFIG == 0)
1306250207Shselasky	free(ptr, M_USBDEV);
1307250207Shselasky#endif
1308250207Shselasky}
1309250207Shselasky
1310250207Shselasky/*------------------------------------------------------------------------*
1311194228Sthompsa *	usbd_req_get_config_desc_full
1312184610Salfred *
1313184610Salfred * This function gets the complete USB configuration descriptor and
1314250207Shselasky * ensures that "wTotalLength" is correct. The returned configuration
1315250207Shselasky * descriptor is freed by calling "usbd_free_config_desc()".
1316184610Salfred *
1317184610Salfred * Returns:
1318184610Salfred *    0: Success
1319184610Salfred * Else: Failure
1320184610Salfred *------------------------------------------------------------------------*/
1321193045Sthompsausb_error_t
1322194228Sthompsausbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1323250207Shselasky    struct usb_config_descriptor **ppcd, uint8_t index)
1324184610Salfred{
1325192984Sthompsa	struct usb_config_descriptor cd;
1326192984Sthompsa	struct usb_config_descriptor *cdesc;
1327250204Shselasky	uint32_t len;
1328193045Sthompsa	usb_error_t err;
1329184610Salfred
1330184610Salfred	DPRINTFN(4, "index=%d\n", index);
1331184610Salfred
1332184610Salfred	*ppcd = NULL;
1333184610Salfred
1334194228Sthompsa	err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1335250204Shselasky	if (err)
1336184610Salfred		return (err);
1337250204Shselasky
1338184610Salfred	/* get full descriptor */
1339184610Salfred	len = UGETW(cd.wTotalLength);
1340250204Shselasky	if (len < (uint32_t)sizeof(*cdesc)) {
1341184610Salfred		/* corrupt descriptor */
1342184610Salfred		return (USB_ERR_INVAL);
1343250204Shselasky	} else if (len > USB_CONFIG_MAX) {
1344250204Shselasky		DPRINTF("Configuration descriptor was truncated\n");
1345250204Shselasky		len = USB_CONFIG_MAX;
1346184610Salfred	}
1347250207Shselasky	cdesc = usbd_alloc_config_desc(udev, len);
1348250204Shselasky	if (cdesc == NULL)
1349184610Salfred		return (USB_ERR_NOMEM);
1350194228Sthompsa	err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1351184610Salfred	    UDESC_CONFIG, index, 3);
1352184610Salfred	if (err) {
1353250207Shselasky		usbd_free_config_desc(udev, cdesc);
1354184610Salfred		return (err);
1355184610Salfred	}
1356184610Salfred	/* make sure that the device is not fooling us: */
1357184610Salfred	USETW(cdesc->wTotalLength, len);
1358184610Salfred
1359184610Salfred	*ppcd = cdesc;
1360184610Salfred
1361184610Salfred	return (0);			/* success */
1362184610Salfred}
1363184610Salfred
1364184610Salfred/*------------------------------------------------------------------------*
1365194228Sthompsa *	usbd_req_get_device_desc
1366184610Salfred *
1367184610Salfred * Returns:
1368184610Salfred *    0: Success
1369184610Salfred * Else: Failure
1370184610Salfred *------------------------------------------------------------------------*/
1371193045Sthompsausb_error_t
1372194228Sthompsausbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1373192984Sthompsa    struct usb_device_descriptor *d)
1374184610Salfred{
1375184610Salfred	DPRINTFN(4, "\n");
1376194228Sthompsa	return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1377184610Salfred	    sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1378184610Salfred}
1379184610Salfred
1380184610Salfred/*------------------------------------------------------------------------*
1381194228Sthompsa *	usbd_req_get_alt_interface_no
1382184610Salfred *
1383184610Salfred * Returns:
1384184610Salfred *    0: Success
1385184610Salfred * Else: Failure
1386184610Salfred *------------------------------------------------------------------------*/
1387193045Sthompsausb_error_t
1388194228Sthompsausbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1389184610Salfred    uint8_t *alt_iface_no, uint8_t iface_index)
1390184610Salfred{
1391194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1392192984Sthompsa	struct usb_device_request req;
1393184610Salfred
1394195963Salfred	if ((iface == NULL) || (iface->idesc == NULL))
1395184610Salfred		return (USB_ERR_INVAL);
1396195963Salfred
1397184610Salfred	req.bmRequestType = UT_READ_INTERFACE;
1398184610Salfred	req.bRequest = UR_GET_INTERFACE;
1399184610Salfred	USETW(req.wValue, 0);
1400184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1401184610Salfred	req.wIndex[1] = 0;
1402184610Salfred	USETW(req.wLength, 1);
1403194228Sthompsa	return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1404184610Salfred}
1405184610Salfred
1406184610Salfred/*------------------------------------------------------------------------*
1407194228Sthompsa *	usbd_req_set_alt_interface_no
1408184610Salfred *
1409184610Salfred * Returns:
1410184610Salfred *    0: Success
1411184610Salfred * Else: Failure
1412184610Salfred *------------------------------------------------------------------------*/
1413193045Sthompsausb_error_t
1414194228Sthompsausbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1415184610Salfred    uint8_t iface_index, uint8_t alt_no)
1416184610Salfred{
1417194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1418192984Sthompsa	struct usb_device_request req;
1419184610Salfred
1420195963Salfred	if ((iface == NULL) || (iface->idesc == NULL))
1421184610Salfred		return (USB_ERR_INVAL);
1422195963Salfred
1423184610Salfred	req.bmRequestType = UT_WRITE_INTERFACE;
1424184610Salfred	req.bRequest = UR_SET_INTERFACE;
1425184610Salfred	req.wValue[0] = alt_no;
1426184610Salfred	req.wValue[1] = 0;
1427184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1428184610Salfred	req.wIndex[1] = 0;
1429184610Salfred	USETW(req.wLength, 0);
1430194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1431184610Salfred}
1432184610Salfred
1433184610Salfred/*------------------------------------------------------------------------*
1434194228Sthompsa *	usbd_req_get_device_status
1435184610Salfred *
1436184610Salfred * Returns:
1437184610Salfred *    0: Success
1438184610Salfred * Else: Failure
1439184610Salfred *------------------------------------------------------------------------*/
1440193045Sthompsausb_error_t
1441194228Sthompsausbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1442192984Sthompsa    struct usb_status *st)
1443184610Salfred{
1444192984Sthompsa	struct usb_device_request req;
1445184610Salfred
1446184610Salfred	req.bmRequestType = UT_READ_DEVICE;
1447184610Salfred	req.bRequest = UR_GET_STATUS;
1448184610Salfred	USETW(req.wValue, 0);
1449184610Salfred	USETW(req.wIndex, 0);
1450184610Salfred	USETW(req.wLength, sizeof(*st));
1451194228Sthompsa	return (usbd_do_request(udev, mtx, &req, st));
1452184610Salfred}
1453184610Salfred
1454184610Salfred/*------------------------------------------------------------------------*
1455194228Sthompsa *	usbd_req_get_hub_descriptor
1456184610Salfred *
1457184610Salfred * Returns:
1458184610Salfred *    0: Success
1459184610Salfred * Else: Failure
1460184610Salfred *------------------------------------------------------------------------*/
1461193045Sthompsausb_error_t
1462194228Sthompsausbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1463192984Sthompsa    struct usb_hub_descriptor *hd, uint8_t nports)
1464184610Salfred{
1465192984Sthompsa	struct usb_device_request req;
1466184610Salfred	uint16_t len = (nports + 7 + (8 * 8)) / 8;
1467184610Salfred
1468184610Salfred	req.bmRequestType = UT_READ_CLASS_DEVICE;
1469184610Salfred	req.bRequest = UR_GET_DESCRIPTOR;
1470184610Salfred	USETW2(req.wValue, UDESC_HUB, 0);
1471184610Salfred	USETW(req.wIndex, 0);
1472184610Salfred	USETW(req.wLength, len);
1473194228Sthompsa	return (usbd_do_request(udev, mtx, &req, hd));
1474184610Salfred}
1475184610Salfred
1476184610Salfred/*------------------------------------------------------------------------*
1477213435Shselasky *	usbd_req_get_ss_hub_descriptor
1478213435Shselasky *
1479213435Shselasky * Returns:
1480213435Shselasky *    0: Success
1481213435Shselasky * Else: Failure
1482213435Shselasky *------------------------------------------------------------------------*/
1483213435Shselaskyusb_error_t
1484213435Shselaskyusbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1485213435Shselasky    struct usb_hub_ss_descriptor *hd, uint8_t nports)
1486213435Shselasky{
1487213435Shselasky	struct usb_device_request req;
1488213435Shselasky	uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1489213435Shselasky
1490213435Shselasky	req.bmRequestType = UT_READ_CLASS_DEVICE;
1491213435Shselasky	req.bRequest = UR_GET_DESCRIPTOR;
1492213435Shselasky	USETW2(req.wValue, UDESC_SS_HUB, 0);
1493213435Shselasky	USETW(req.wIndex, 0);
1494213435Shselasky	USETW(req.wLength, len);
1495213435Shselasky	return (usbd_do_request(udev, mtx, &req, hd));
1496213435Shselasky}
1497213435Shselasky
1498213435Shselasky/*------------------------------------------------------------------------*
1499194228Sthompsa *	usbd_req_get_hub_status
1500184610Salfred *
1501184610Salfred * Returns:
1502184610Salfred *    0: Success
1503184610Salfred * Else: Failure
1504184610Salfred *------------------------------------------------------------------------*/
1505193045Sthompsausb_error_t
1506194228Sthompsausbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1507192984Sthompsa    struct usb_hub_status *st)
1508184610Salfred{
1509192984Sthompsa	struct usb_device_request req;
1510184610Salfred
1511184610Salfred	req.bmRequestType = UT_READ_CLASS_DEVICE;
1512184610Salfred	req.bRequest = UR_GET_STATUS;
1513184610Salfred	USETW(req.wValue, 0);
1514184610Salfred	USETW(req.wIndex, 0);
1515192984Sthompsa	USETW(req.wLength, sizeof(struct usb_hub_status));
1516194228Sthompsa	return (usbd_do_request(udev, mtx, &req, st));
1517184610Salfred}
1518184610Salfred
1519184610Salfred/*------------------------------------------------------------------------*
1520194228Sthompsa *	usbd_req_set_address
1521184610Salfred *
1522184610Salfred * This function is used to set the address for an USB device. After
1523184610Salfred * port reset the USB device will respond at address zero.
1524184610Salfred *
1525184610Salfred * Returns:
1526184610Salfred *    0: Success
1527184610Salfred * Else: Failure
1528184610Salfred *------------------------------------------------------------------------*/
1529193045Sthompsausb_error_t
1530194228Sthompsausbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1531184610Salfred{
1532192984Sthompsa	struct usb_device_request req;
1533213435Shselasky	usb_error_t err;
1534184610Salfred
1535184610Salfred	DPRINTFN(6, "setting device address=%d\n", addr);
1536184610Salfred
1537184610Salfred	req.bmRequestType = UT_WRITE_DEVICE;
1538184610Salfred	req.bRequest = UR_SET_ADDRESS;
1539184610Salfred	USETW(req.wValue, addr);
1540184610Salfred	USETW(req.wIndex, 0);
1541184610Salfred	USETW(req.wLength, 0);
1542184610Salfred
1543213435Shselasky	err = USB_ERR_INVAL;
1544213435Shselasky
1545213435Shselasky	/* check if USB controller handles set address */
1546213435Shselasky	if (udev->bus->methods->set_address != NULL)
1547213435Shselasky		err = (udev->bus->methods->set_address) (udev, mtx, addr);
1548213435Shselasky
1549213435Shselasky	if (err != USB_ERR_INVAL)
1550213435Shselasky		goto done;
1551213435Shselasky
1552184610Salfred	/* Setting the address should not take more than 1 second ! */
1553213435Shselasky	err = usbd_do_request_flags(udev, mtx, &req, NULL,
1554213435Shselasky	    USB_DELAY_STATUS_STAGE, NULL, 1000);
1555213435Shselasky
1556213435Shselaskydone:
1557213435Shselasky	/* allow device time to set new address */
1558213435Shselasky	usb_pause_mtx(mtx,
1559241987Shselasky	    USB_MS_TO_TICKS(usb_set_address_settle));
1560213435Shselasky
1561213435Shselasky	return (err);
1562184610Salfred}
1563184610Salfred
1564184610Salfred/*------------------------------------------------------------------------*
1565194228Sthompsa *	usbd_req_get_port_status
1566184610Salfred *
1567184610Salfred * Returns:
1568184610Salfred *    0: Success
1569184610Salfred * Else: Failure
1570184610Salfred *------------------------------------------------------------------------*/
1571193045Sthompsausb_error_t
1572194228Sthompsausbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1573192984Sthompsa    struct usb_port_status *ps, uint8_t port)
1574184610Salfred{
1575192984Sthompsa	struct usb_device_request req;
1576184610Salfred
1577184610Salfred	req.bmRequestType = UT_READ_CLASS_OTHER;
1578184610Salfred	req.bRequest = UR_GET_STATUS;
1579184610Salfred	USETW(req.wValue, 0);
1580184610Salfred	req.wIndex[0] = port;
1581184610Salfred	req.wIndex[1] = 0;
1582184610Salfred	USETW(req.wLength, sizeof *ps);
1583194228Sthompsa	return (usbd_do_request(udev, mtx, &req, ps));
1584184610Salfred}
1585184610Salfred
1586184610Salfred/*------------------------------------------------------------------------*
1587194228Sthompsa *	usbd_req_clear_hub_feature
1588184610Salfred *
1589184610Salfred * Returns:
1590184610Salfred *    0: Success
1591184610Salfred * Else: Failure
1592184610Salfred *------------------------------------------------------------------------*/
1593193045Sthompsausb_error_t
1594194228Sthompsausbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1595184610Salfred    uint16_t sel)
1596184610Salfred{
1597192984Sthompsa	struct usb_device_request req;
1598184610Salfred
1599184610Salfred	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1600184610Salfred	req.bRequest = UR_CLEAR_FEATURE;
1601184610Salfred	USETW(req.wValue, sel);
1602184610Salfred	USETW(req.wIndex, 0);
1603184610Salfred	USETW(req.wLength, 0);
1604194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1605184610Salfred}
1606184610Salfred
1607184610Salfred/*------------------------------------------------------------------------*
1608194228Sthompsa *	usbd_req_set_hub_feature
1609184610Salfred *
1610184610Salfred * Returns:
1611184610Salfred *    0: Success
1612184610Salfred * Else: Failure
1613184610Salfred *------------------------------------------------------------------------*/
1614193045Sthompsausb_error_t
1615194228Sthompsausbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1616184610Salfred    uint16_t sel)
1617184610Salfred{
1618192984Sthompsa	struct usb_device_request req;
1619184610Salfred
1620184610Salfred	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1621184610Salfred	req.bRequest = UR_SET_FEATURE;
1622184610Salfred	USETW(req.wValue, sel);
1623184610Salfred	USETW(req.wIndex, 0);
1624184610Salfred	USETW(req.wLength, 0);
1625194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1626184610Salfred}
1627184610Salfred
1628184610Salfred/*------------------------------------------------------------------------*
1629213435Shselasky *	usbd_req_set_hub_u1_timeout
1630213435Shselasky *
1631213435Shselasky * Returns:
1632213435Shselasky *    0: Success
1633213435Shselasky * Else: Failure
1634213435Shselasky *------------------------------------------------------------------------*/
1635213435Shselaskyusb_error_t
1636213435Shselaskyusbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1637213435Shselasky    uint8_t port, uint8_t timeout)
1638213435Shselasky{
1639213435Shselasky	struct usb_device_request req;
1640213435Shselasky
1641213435Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1642213435Shselasky	req.bRequest = UR_SET_FEATURE;
1643213435Shselasky	USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1644213435Shselasky	req.wIndex[0] = port;
1645213435Shselasky	req.wIndex[1] = timeout;
1646213435Shselasky	USETW(req.wLength, 0);
1647213435Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
1648213435Shselasky}
1649213435Shselasky
1650213435Shselasky/*------------------------------------------------------------------------*
1651213435Shselasky *	usbd_req_set_hub_u2_timeout
1652213435Shselasky *
1653213435Shselasky * Returns:
1654213435Shselasky *    0: Success
1655213435Shselasky * Else: Failure
1656213435Shselasky *------------------------------------------------------------------------*/
1657213435Shselaskyusb_error_t
1658213435Shselaskyusbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1659213435Shselasky    uint8_t port, uint8_t timeout)
1660213435Shselasky{
1661213435Shselasky	struct usb_device_request req;
1662213435Shselasky
1663213435Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1664213435Shselasky	req.bRequest = UR_SET_FEATURE;
1665213435Shselasky	USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1666213435Shselasky	req.wIndex[0] = port;
1667213435Shselasky	req.wIndex[1] = timeout;
1668213435Shselasky	USETW(req.wLength, 0);
1669213435Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
1670213435Shselasky}
1671213435Shselasky
1672213435Shselasky/*------------------------------------------------------------------------*
1673213435Shselasky *	usbd_req_set_hub_depth
1674213435Shselasky *
1675213435Shselasky * Returns:
1676213435Shselasky *    0: Success
1677213435Shselasky * Else: Failure
1678213435Shselasky *------------------------------------------------------------------------*/
1679213435Shselaskyusb_error_t
1680213435Shselaskyusbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1681213435Shselasky    uint16_t depth)
1682213435Shselasky{
1683213435Shselasky	struct usb_device_request req;
1684213435Shselasky
1685213435Shselasky	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1686213435Shselasky	req.bRequest = UR_SET_HUB_DEPTH;
1687213435Shselasky	USETW(req.wValue, depth);
1688213435Shselasky	USETW(req.wIndex, 0);
1689213435Shselasky	USETW(req.wLength, 0);
1690213435Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
1691213435Shselasky}
1692213435Shselasky
1693213435Shselasky/*------------------------------------------------------------------------*
1694194228Sthompsa *	usbd_req_clear_port_feature
1695184610Salfred *
1696184610Salfred * Returns:
1697184610Salfred *    0: Success
1698184610Salfred * Else: Failure
1699184610Salfred *------------------------------------------------------------------------*/
1700193045Sthompsausb_error_t
1701194228Sthompsausbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1702184610Salfred    uint8_t port, uint16_t sel)
1703184610Salfred{
1704192984Sthompsa	struct usb_device_request req;
1705184610Salfred
1706184610Salfred	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1707184610Salfred	req.bRequest = UR_CLEAR_FEATURE;
1708184610Salfred	USETW(req.wValue, sel);
1709184610Salfred	req.wIndex[0] = port;
1710184610Salfred	req.wIndex[1] = 0;
1711184610Salfred	USETW(req.wLength, 0);
1712194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1713184610Salfred}
1714184610Salfred
1715184610Salfred/*------------------------------------------------------------------------*
1716194228Sthompsa *	usbd_req_set_port_feature
1717184610Salfred *
1718184610Salfred * Returns:
1719184610Salfred *    0: Success
1720184610Salfred * Else: Failure
1721184610Salfred *------------------------------------------------------------------------*/
1722193045Sthompsausb_error_t
1723194228Sthompsausbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1724184610Salfred    uint8_t port, uint16_t sel)
1725184610Salfred{
1726192984Sthompsa	struct usb_device_request req;
1727184610Salfred
1728184610Salfred	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1729184610Salfred	req.bRequest = UR_SET_FEATURE;
1730184610Salfred	USETW(req.wValue, sel);
1731184610Salfred	req.wIndex[0] = port;
1732184610Salfred	req.wIndex[1] = 0;
1733184610Salfred	USETW(req.wLength, 0);
1734194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1735184610Salfred}
1736184610Salfred
1737184610Salfred/*------------------------------------------------------------------------*
1738194228Sthompsa *	usbd_req_set_protocol
1739184610Salfred *
1740184610Salfred * Returns:
1741184610Salfred *    0: Success
1742184610Salfred * Else: Failure
1743184610Salfred *------------------------------------------------------------------------*/
1744193045Sthompsausb_error_t
1745194228Sthompsausbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1746184610Salfred    uint8_t iface_index, uint16_t report)
1747184610Salfred{
1748194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1749192984Sthompsa	struct usb_device_request req;
1750184610Salfred
1751184610Salfred	if ((iface == NULL) || (iface->idesc == NULL)) {
1752184610Salfred		return (USB_ERR_INVAL);
1753184610Salfred	}
1754184610Salfred	DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1755184610Salfred	    iface, report, iface->idesc->bInterfaceNumber);
1756184610Salfred
1757184610Salfred	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1758184610Salfred	req.bRequest = UR_SET_PROTOCOL;
1759184610Salfred	USETW(req.wValue, report);
1760184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1761184610Salfred	req.wIndex[1] = 0;
1762184610Salfred	USETW(req.wLength, 0);
1763194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1764184610Salfred}
1765184610Salfred
1766184610Salfred/*------------------------------------------------------------------------*
1767194228Sthompsa *	usbd_req_set_report
1768184610Salfred *
1769184610Salfred * Returns:
1770184610Salfred *    0: Success
1771184610Salfred * Else: Failure
1772184610Salfred *------------------------------------------------------------------------*/
1773193045Sthompsausb_error_t
1774194228Sthompsausbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1775184610Salfred    uint8_t iface_index, uint8_t type, uint8_t id)
1776184610Salfred{
1777194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1778192984Sthompsa	struct usb_device_request req;
1779184610Salfred
1780184610Salfred	if ((iface == NULL) || (iface->idesc == NULL)) {
1781184610Salfred		return (USB_ERR_INVAL);
1782184610Salfred	}
1783184610Salfred	DPRINTFN(5, "len=%d\n", len);
1784184610Salfred
1785184610Salfred	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1786184610Salfred	req.bRequest = UR_SET_REPORT;
1787184610Salfred	USETW2(req.wValue, type, id);
1788184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1789184610Salfred	req.wIndex[1] = 0;
1790184610Salfred	USETW(req.wLength, len);
1791194228Sthompsa	return (usbd_do_request(udev, mtx, &req, data));
1792184610Salfred}
1793184610Salfred
1794184610Salfred/*------------------------------------------------------------------------*
1795194228Sthompsa *	usbd_req_get_report
1796184610Salfred *
1797184610Salfred * Returns:
1798184610Salfred *    0: Success
1799184610Salfred * Else: Failure
1800184610Salfred *------------------------------------------------------------------------*/
1801193045Sthompsausb_error_t
1802194228Sthompsausbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1803184610Salfred    uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1804184610Salfred{
1805194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1806192984Sthompsa	struct usb_device_request req;
1807184610Salfred
1808224728Smav	if ((iface == NULL) || (iface->idesc == NULL)) {
1809184610Salfred		return (USB_ERR_INVAL);
1810184610Salfred	}
1811184610Salfred	DPRINTFN(5, "len=%d\n", len);
1812184610Salfred
1813184610Salfred	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1814184610Salfred	req.bRequest = UR_GET_REPORT;
1815184610Salfred	USETW2(req.wValue, type, id);
1816184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1817184610Salfred	req.wIndex[1] = 0;
1818184610Salfred	USETW(req.wLength, len);
1819194228Sthompsa	return (usbd_do_request(udev, mtx, &req, data));
1820184610Salfred}
1821184610Salfred
1822184610Salfred/*------------------------------------------------------------------------*
1823194228Sthompsa *	usbd_req_set_idle
1824184610Salfred *
1825184610Salfred * Returns:
1826184610Salfred *    0: Success
1827184610Salfred * Else: Failure
1828184610Salfred *------------------------------------------------------------------------*/
1829193045Sthompsausb_error_t
1830194228Sthompsausbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1831184610Salfred    uint8_t iface_index, uint8_t duration, uint8_t id)
1832184610Salfred{
1833194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1834192984Sthompsa	struct usb_device_request req;
1835184610Salfred
1836184610Salfred	if ((iface == NULL) || (iface->idesc == NULL)) {
1837184610Salfred		return (USB_ERR_INVAL);
1838184610Salfred	}
1839184610Salfred	DPRINTFN(5, "%d %d\n", duration, id);
1840184610Salfred
1841184610Salfred	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1842184610Salfred	req.bRequest = UR_SET_IDLE;
1843184610Salfred	USETW2(req.wValue, duration, id);
1844184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1845184610Salfred	req.wIndex[1] = 0;
1846184610Salfred	USETW(req.wLength, 0);
1847194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1848184610Salfred}
1849184610Salfred
1850184610Salfred/*------------------------------------------------------------------------*
1851194228Sthompsa *	usbd_req_get_report_descriptor
1852184610Salfred *
1853184610Salfred * Returns:
1854184610Salfred *    0: Success
1855184610Salfred * Else: Failure
1856184610Salfred *------------------------------------------------------------------------*/
1857193045Sthompsausb_error_t
1858194228Sthompsausbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1859184610Salfred    void *d, uint16_t size, uint8_t iface_index)
1860184610Salfred{
1861194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1862192984Sthompsa	struct usb_device_request req;
1863184610Salfred
1864184610Salfred	if ((iface == NULL) || (iface->idesc == NULL)) {
1865184610Salfred		return (USB_ERR_INVAL);
1866184610Salfred	}
1867184610Salfred	req.bmRequestType = UT_READ_INTERFACE;
1868184610Salfred	req.bRequest = UR_GET_DESCRIPTOR;
1869184610Salfred	USETW2(req.wValue, UDESC_REPORT, 0);	/* report id should be 0 */
1870184610Salfred	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1871184610Salfred	req.wIndex[1] = 0;
1872184610Salfred	USETW(req.wLength, size);
1873194228Sthompsa	return (usbd_do_request(udev, mtx, &req, d));
1874184610Salfred}
1875184610Salfred
1876184610Salfred/*------------------------------------------------------------------------*
1877194228Sthompsa *	usbd_req_set_config
1878184610Salfred *
1879184610Salfred * This function is used to select the current configuration number in
1880184610Salfred * both USB device side mode and USB host side mode. When setting the
1881184610Salfred * configuration the function of the interfaces can change.
1882184610Salfred *
1883184610Salfred * Returns:
1884184610Salfred *    0: Success
1885184610Salfred * Else: Failure
1886184610Salfred *------------------------------------------------------------------------*/
1887193045Sthompsausb_error_t
1888194228Sthompsausbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1889184610Salfred{
1890192984Sthompsa	struct usb_device_request req;
1891184610Salfred
1892184610Salfred	DPRINTF("setting config %d\n", conf);
1893184610Salfred
1894184610Salfred	/* do "set configuration" request */
1895184610Salfred
1896184610Salfred	req.bmRequestType = UT_WRITE_DEVICE;
1897184610Salfred	req.bRequest = UR_SET_CONFIG;
1898184610Salfred	req.wValue[0] = conf;
1899184610Salfred	req.wValue[1] = 0;
1900184610Salfred	USETW(req.wIndex, 0);
1901184610Salfred	USETW(req.wLength, 0);
1902194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
1903184610Salfred}
1904184610Salfred
1905184610Salfred/*------------------------------------------------------------------------*
1906194228Sthompsa *	usbd_req_get_config
1907184610Salfred *
1908184610Salfred * Returns:
1909184610Salfred *    0: Success
1910184610Salfred * Else: Failure
1911184610Salfred *------------------------------------------------------------------------*/
1912193045Sthompsausb_error_t
1913194228Sthompsausbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1914184610Salfred{
1915192984Sthompsa	struct usb_device_request req;
1916184610Salfred
1917184610Salfred	req.bmRequestType = UT_READ_DEVICE;
1918184610Salfred	req.bRequest = UR_GET_CONFIG;
1919184610Salfred	USETW(req.wValue, 0);
1920184610Salfred	USETW(req.wIndex, 0);
1921184610Salfred	USETW(req.wLength, 1);
1922194228Sthompsa	return (usbd_do_request(udev, mtx, &req, pconf));
1923184610Salfred}
1924184610Salfred
1925184610Salfred/*------------------------------------------------------------------------*
1926213435Shselasky *	usbd_setup_device_desc
1927213435Shselasky *------------------------------------------------------------------------*/
1928213435Shselaskyusb_error_t
1929213435Shselaskyusbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1930213435Shselasky{
1931213435Shselasky	usb_error_t err;
1932213435Shselasky
1933213435Shselasky	/*
1934213435Shselasky	 * Get the first 8 bytes of the device descriptor !
1935213435Shselasky	 *
1936213435Shselasky	 * NOTE: "usbd_do_request()" will check the device descriptor
1937213435Shselasky	 * next time we do a request to see if the maximum packet size
1938213435Shselasky	 * changed! The 8 first bytes of the device descriptor
1939213435Shselasky	 * contains the maximum packet size to use on control endpoint
1940213435Shselasky	 * 0. If this value is different from "USB_MAX_IPACKET" a new
1941213435Shselasky	 * USB control request will be setup!
1942213435Shselasky	 */
1943213435Shselasky	switch (udev->speed) {
1944213435Shselasky	case USB_SPEED_FULL:
1945248246Shselasky		if (usb_full_ddesc != 0) {
1946248246Shselasky			/* get full device descriptor */
1947248246Shselasky			err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1948248246Shselasky			if (err == 0)
1949248246Shselasky				break;
1950248246Shselasky		}
1951248246Shselasky
1952248246Shselasky		/* get partial device descriptor, some devices crash on this */
1953213435Shselasky		err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1954213435Shselasky		    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1955248246Shselasky		if (err != 0)
1956248246Shselasky			break;
1957248246Shselasky
1958248246Shselasky		/* get the full device descriptor */
1959248246Shselasky		err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1960213435Shselasky		break;
1961248246Shselasky
1962213435Shselasky	default:
1963248247Shselasky		DPRINTF("Minimum bMaxPacketSize is large enough "
1964248246Shselasky		    "to hold the complete device descriptor or "
1965248247Shselasky		    "only one bMaxPacketSize choice\n");
1966248246Shselasky
1967248246Shselasky		/* get the full device descriptor */
1968248246Shselasky		err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1969248246Shselasky
1970248246Shselasky		/* try one more time, if error */
1971248246Shselasky		if (err != 0)
1972248246Shselasky			err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1973213435Shselasky		break;
1974213435Shselasky	}
1975213435Shselasky
1976248246Shselasky	if (err != 0) {
1977248246Shselasky		DPRINTFN(0, "getting device descriptor "
1978248246Shselasky		    "at addr %d failed, %s\n", udev->address,
1979248246Shselasky		    usbd_errstr(err));
1980213435Shselasky		return (err);
1981213435Shselasky	}
1982213435Shselasky
1983213435Shselasky	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1984213435Shselasky	    "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1985213435Shselasky	    udev->address, UGETW(udev->ddesc.bcdUSB),
1986213435Shselasky	    udev->ddesc.bDeviceClass,
1987213435Shselasky	    udev->ddesc.bDeviceSubClass,
1988213435Shselasky	    udev->ddesc.bDeviceProtocol,
1989213435Shselasky	    udev->ddesc.bMaxPacketSize,
1990213435Shselasky	    udev->ddesc.bLength,
1991213435Shselasky	    udev->speed);
1992213435Shselasky
1993213435Shselasky	return (err);
1994213435Shselasky}
1995213435Shselasky
1996213435Shselasky/*------------------------------------------------------------------------*
1997194228Sthompsa *	usbd_req_re_enumerate
1998184610Salfred *
1999185087Salfred * NOTE: After this function returns the hardware is in the
2000185087Salfred * unconfigured state! The application is responsible for setting a
2001185087Salfred * new configuration.
2002185087Salfred *
2003184610Salfred * Returns:
2004184610Salfred *    0: Success
2005184610Salfred * Else: Failure
2006184610Salfred *------------------------------------------------------------------------*/
2007193045Sthompsausb_error_t
2008194228Sthompsausbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
2009184610Salfred{
2010192984Sthompsa	struct usb_device *parent_hub;
2011193045Sthompsa	usb_error_t err;
2012184610Salfred	uint8_t old_addr;
2013186730Salfred	uint8_t do_retry = 1;
2014184610Salfred
2015192499Sthompsa	if (udev->flags.usb_mode != USB_MODE_HOST) {
2016185290Salfred		return (USB_ERR_INVAL);
2017185290Salfred	}
2018184610Salfred	old_addr = udev->address;
2019184610Salfred	parent_hub = udev->parent_hub;
2020184610Salfred	if (parent_hub == NULL) {
2021185290Salfred		return (USB_ERR_INVAL);
2022184610Salfred	}
2023186730Salfredretry:
2024260589Shselasky#if USB_HAVE_TT_SUPPORT
2025222786Shselasky	/*
2026222786Shselasky	 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2027222786Shselasky	 * speed device, if any.
2028222786Shselasky	 */
2029222786Shselasky	if (udev->parent_hs_hub != NULL &&
2030222786Shselasky	    udev->speed != USB_SPEED_HIGH) {
2031222786Shselasky		DPRINTF("Trying to reset parent High Speed TT.\n");
2032260589Shselasky		if (udev->parent_hs_hub == parent_hub &&
2033260589Shselasky		    (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2034260589Shselasky		     uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) {
2035260589Shselasky			/* we can reset the whole TT */
2036260589Shselasky			err = usbd_req_reset_tt(parent_hub, NULL,
2037260589Shselasky			    udev->hs_port_no);
2038260589Shselasky		} else {
2039260589Shselasky			/* only reset a particular device and endpoint */
2040260589Shselasky			err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2041260589Shselasky			    udev->hs_port_no, old_addr, UE_CONTROL, 0);
2042260589Shselasky		}
2043222786Shselasky		if (err) {
2044222786Shselasky			DPRINTF("Resetting parent High "
2045222786Shselasky			    "Speed TT failed (%s).\n",
2046222786Shselasky			    usbd_errstr(err));
2047222786Shselasky		}
2048222786Shselasky	}
2049260589Shselasky#endif
2050230091Shselasky	/* Try to warm reset first */
2051230091Shselasky	if (parent_hub->speed == USB_SPEED_SUPER)
2052230091Shselasky		usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2053230091Shselasky
2054222786Shselasky	/* Try to reset the parent HUB port. */
2055194228Sthompsa	err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2056184610Salfred	if (err) {
2057190739Sthompsa		DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2058194228Sthompsa		    old_addr, usbd_errstr(err));
2059184610Salfred		goto done;
2060184610Salfred	}
2061213435Shselasky
2062184610Salfred	/*
2063184610Salfred	 * After that the port has been reset our device should be at
2064184610Salfred	 * address zero:
2065184610Salfred	 */
2066184610Salfred	udev->address = USB_START_ADDR;
2067184610Salfred
2068185290Salfred	/* reset "bMaxPacketSize" */
2069185290Salfred	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2070185290Salfred
2071213435Shselasky	/* reset USB state */
2072213435Shselasky	usb_set_device_state(udev, USB_STATE_POWERED);
2073213435Shselasky
2074184610Salfred	/*
2075184610Salfred	 * Restore device address:
2076184610Salfred	 */
2077194228Sthompsa	err = usbd_req_set_address(udev, mtx, old_addr);
2078184610Salfred	if (err) {
2079184610Salfred		/* XXX ignore any errors! */
2080190739Sthompsa		DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2081194228Sthompsa		    old_addr, usbd_errstr(err));
2082184610Salfred	}
2083213435Shselasky	/*
2084213435Shselasky	 * Restore device address, if the controller driver did not
2085213435Shselasky	 * set a new one:
2086213435Shselasky	 */
2087213435Shselasky	if (udev->address == USB_START_ADDR)
2088213435Shselasky		udev->address = old_addr;
2089184610Salfred
2090213435Shselasky	/* setup the device descriptor and the initial "wMaxPacketSize" */
2091213435Shselasky	err = usbd_setup_device_desc(udev, mtx);
2092184610Salfred
2093184610Salfreddone:
2094186730Salfred	if (err && do_retry) {
2095186730Salfred		/* give the USB firmware some time to load */
2096194228Sthompsa		usb_pause_mtx(mtx, hz / 2);
2097186730Salfred		/* no more retries after this retry */
2098186730Salfred		do_retry = 0;
2099186730Salfred		/* try again */
2100186730Salfred		goto retry;
2101186730Salfred	}
2102184610Salfred	/* restore address */
2103213435Shselasky	if (udev->address == USB_START_ADDR)
2104213435Shselasky		udev->address = old_addr;
2105213435Shselasky	/* update state, if successful */
2106213435Shselasky	if (err == 0)
2107213435Shselasky		usb_set_device_state(udev, USB_STATE_ADDRESSED);
2108184610Salfred	return (err);
2109184610Salfred}
2110186730Salfred
2111186730Salfred/*------------------------------------------------------------------------*
2112194228Sthompsa *	usbd_req_clear_device_feature
2113186730Salfred *
2114186730Salfred * Returns:
2115186730Salfred *    0: Success
2116186730Salfred * Else: Failure
2117186730Salfred *------------------------------------------------------------------------*/
2118193045Sthompsausb_error_t
2119194228Sthompsausbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2120186730Salfred    uint16_t sel)
2121186730Salfred{
2122192984Sthompsa	struct usb_device_request req;
2123186730Salfred
2124186730Salfred	req.bmRequestType = UT_WRITE_DEVICE;
2125186730Salfred	req.bRequest = UR_CLEAR_FEATURE;
2126186730Salfred	USETW(req.wValue, sel);
2127186730Salfred	USETW(req.wIndex, 0);
2128186730Salfred	USETW(req.wLength, 0);
2129194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
2130186730Salfred}
2131186730Salfred
2132186730Salfred/*------------------------------------------------------------------------*
2133194228Sthompsa *	usbd_req_set_device_feature
2134186730Salfred *
2135186730Salfred * Returns:
2136186730Salfred *    0: Success
2137186730Salfred * Else: Failure
2138186730Salfred *------------------------------------------------------------------------*/
2139193045Sthompsausb_error_t
2140194228Sthompsausbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2141186730Salfred    uint16_t sel)
2142186730Salfred{
2143192984Sthompsa	struct usb_device_request req;
2144186730Salfred
2145186730Salfred	req.bmRequestType = UT_WRITE_DEVICE;
2146186730Salfred	req.bRequest = UR_SET_FEATURE;
2147186730Salfred	USETW(req.wValue, sel);
2148186730Salfred	USETW(req.wIndex, 0);
2149186730Salfred	USETW(req.wLength, 0);
2150194228Sthompsa	return (usbd_do_request(udev, mtx, &req, 0));
2151186730Salfred}
2152222786Shselasky
2153222786Shselasky/*------------------------------------------------------------------------*
2154222786Shselasky *	usbd_req_reset_tt
2155222786Shselasky *
2156222786Shselasky * Returns:
2157222786Shselasky *    0: Success
2158222786Shselasky * Else: Failure
2159222786Shselasky *------------------------------------------------------------------------*/
2160222786Shselaskyusb_error_t
2161222786Shselaskyusbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2162222786Shselasky    uint8_t port)
2163222786Shselasky{
2164222786Shselasky	struct usb_device_request req;
2165222786Shselasky
2166222786Shselasky	/* For single TT HUBs the port should be 1 */
2167222786Shselasky
2168222786Shselasky	if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2169222786Shselasky	    udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2170222786Shselasky		port = 1;
2171222786Shselasky
2172222786Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
2173222786Shselasky	req.bRequest = UR_RESET_TT;
2174222786Shselasky	USETW(req.wValue, 0);
2175222786Shselasky	req.wIndex[0] = port;
2176222786Shselasky	req.wIndex[1] = 0;
2177222786Shselasky	USETW(req.wLength, 0);
2178222786Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
2179222786Shselasky}
2180222786Shselasky
2181222786Shselasky/*------------------------------------------------------------------------*
2182222786Shselasky *	usbd_req_clear_tt_buffer
2183222786Shselasky *
2184222786Shselasky * For single TT HUBs the port should be 1.
2185222786Shselasky *
2186222786Shselasky * Returns:
2187222786Shselasky *    0: Success
2188222786Shselasky * Else: Failure
2189222786Shselasky *------------------------------------------------------------------------*/
2190222786Shselaskyusb_error_t
2191222786Shselaskyusbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2192222786Shselasky    uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2193222786Shselasky{
2194222786Shselasky	struct usb_device_request req;
2195222786Shselasky	uint16_t wValue;
2196222786Shselasky
2197222786Shselasky	/* For single TT HUBs the port should be 1 */
2198222786Shselasky
2199222786Shselasky	if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
2200222786Shselasky	    udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
2201222786Shselasky		port = 1;
2202222786Shselasky
2203222786Shselasky	wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2204222786Shselasky	    ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2205222786Shselasky
2206222786Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
2207222786Shselasky	req.bRequest = UR_CLEAR_TT_BUFFER;
2208222786Shselasky	USETW(req.wValue, wValue);
2209222786Shselasky	req.wIndex[0] = port;
2210222786Shselasky	req.wIndex[1] = 0;
2211222786Shselasky	USETW(req.wLength, 0);
2212222786Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
2213222786Shselasky}
2214230032Shselasky
2215230032Shselasky/*------------------------------------------------------------------------*
2216230032Shselasky *	usbd_req_set_port_link_state
2217230032Shselasky *
2218230032Shselasky * USB 3.0 specific request
2219230032Shselasky *
2220230032Shselasky * Returns:
2221230032Shselasky *    0: Success
2222230032Shselasky * Else: Failure
2223230032Shselasky *------------------------------------------------------------------------*/
2224230032Shselaskyusb_error_t
2225230032Shselaskyusbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2226230032Shselasky    uint8_t port, uint8_t link_state)
2227230032Shselasky{
2228230032Shselasky	struct usb_device_request req;
2229230032Shselasky
2230230032Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
2231230032Shselasky	req.bRequest = UR_SET_FEATURE;
2232230032Shselasky	USETW(req.wValue, UHF_PORT_LINK_STATE);
2233230032Shselasky	req.wIndex[0] = port;
2234230032Shselasky	req.wIndex[1] = link_state;
2235230032Shselasky	USETW(req.wLength, 0);
2236230032Shselasky	return (usbd_do_request(udev, mtx, &req, 0));
2237230032Shselasky}
2238233771Shselasky
2239233771Shselasky/*------------------------------------------------------------------------*
2240233771Shselasky *		usbd_req_set_lpm_info
2241233771Shselasky *
2242233771Shselasky * USB 2.0 specific request for Link Power Management.
2243233771Shselasky *
2244233771Shselasky * Returns:
2245233771Shselasky * 0:				Success
2246233771Shselasky * USB_ERR_PENDING_REQUESTS:	NYET
2247233771Shselasky * USB_ERR_TIMEOUT:		TIMEOUT
2248233771Shselasky * USB_ERR_STALL:		STALL
2249233771Shselasky * Else:			Failure
2250233771Shselasky *------------------------------------------------------------------------*/
2251233771Shselaskyusb_error_t
2252233771Shselaskyusbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2253233771Shselasky    uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2254233771Shselasky{
2255233771Shselasky	struct usb_device_request req;
2256233771Shselasky	usb_error_t err;
2257233771Shselasky	uint8_t buf[1];
2258233771Shselasky
2259233771Shselasky	req.bmRequestType = UT_WRITE_CLASS_OTHER;
2260233771Shselasky	req.bRequest = UR_SET_AND_TEST;
2261233771Shselasky	USETW(req.wValue, UHF_PORT_L1);
2262233771Shselasky	req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2263233771Shselasky	req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2264233771Shselasky	USETW(req.wLength, sizeof(buf));
2265233771Shselasky
2266233771Shselasky	/* set default value in case of short transfer */
2267233771Shselasky	buf[0] = 0x00;
2268233771Shselasky
2269233771Shselasky	err = usbd_do_request(udev, mtx, &req, buf);
2270233771Shselasky	if (err)
2271233771Shselasky		return (err);
2272233771Shselasky
2273233771Shselasky	switch (buf[0]) {
2274233771Shselasky	case 0x00:	/* SUCCESS */
2275233771Shselasky		break;
2276233771Shselasky	case 0x10:	/* NYET */
2277233771Shselasky		err = USB_ERR_PENDING_REQUESTS;
2278233771Shselasky		break;
2279233771Shselasky	case 0x11:	/* TIMEOUT */
2280233771Shselasky		err = USB_ERR_TIMEOUT;
2281233771Shselasky		break;
2282233771Shselasky	case 0x30:	/* STALL */
2283233771Shselasky		err = USB_ERR_STALLED;
2284233771Shselasky		break;
2285233771Shselasky	default:	/* reserved */
2286233771Shselasky		err = USB_ERR_IOERROR;
2287233771Shselasky		break;
2288233771Shselasky	}
2289233771Shselasky	return (err);
2290233771Shselasky}
2291233771Shselasky
2292