usb_request.c revision 190633
1/* $FreeBSD: head/sys/dev/usb/usb_request.c 190633 2009-04-01 20:23:47Z piso $ */
2/*-
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <dev/usb/usb_mfunc.h>
30#include <dev/usb/usb_error.h>
31#include <dev/usb/usb.h>
32#include <dev/usb/usb_ioctl.h>
33#include <dev/usb/usbhid.h>
34
35#define	USB_DEBUG_VAR usb2_debug
36
37#include <dev/usb/usb_core.h>
38#include <dev/usb/usb_busdma.h>
39#include <dev/usb/usb_request.h>
40#include <dev/usb/usb_process.h>
41#include <dev/usb/usb_transfer.h>
42#include <dev/usb/usb_debug.h>
43#include <dev/usb/usb_device.h>
44#include <dev/usb/usb_util.h>
45#include <dev/usb/usb_dynamic.h>
46
47#include <dev/usb/usb_controller.h>
48#include <dev/usb/usb_bus.h>
49#include <sys/ctype.h>
50
51#if USB_DEBUG
52static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY;
53static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
54static int usb2_ss_delay = 0;
55
56SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
57    &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms");
58SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
59    &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
60SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW,
61    &usb2_ss_delay, 0, "USB status stage delay in ms");
62#endif
63
64/*------------------------------------------------------------------------*
65 *	usb2_do_request_callback
66 *
67 * This function is the USB callback for generic USB Host control
68 * transfers.
69 *------------------------------------------------------------------------*/
70void
71usb2_do_request_callback(struct usb2_xfer *xfer)
72{
73	;				/* workaround for a bug in "indent" */
74
75	DPRINTF("st=%u\n", USB_GET_STATE(xfer));
76
77	switch (USB_GET_STATE(xfer)) {
78	case USB_ST_SETUP:
79		usb2_start_hardware(xfer);
80		break;
81	default:
82		usb2_cv_signal(xfer->xroot->udev->default_cv);
83		break;
84	}
85}
86
87/*------------------------------------------------------------------------*
88 *	usb2_do_clear_stall_callback
89 *
90 * This function is the USB callback for generic clear stall requests.
91 *------------------------------------------------------------------------*/
92void
93usb2_do_clear_stall_callback(struct usb2_xfer *xfer)
94{
95	struct usb2_device_request req;
96	struct usb2_device *udev;
97	struct usb2_pipe *pipe;
98	struct usb2_pipe *pipe_end;
99	struct usb2_pipe *pipe_first;
100	uint8_t to = USB_EP_MAX;
101
102	udev = xfer->xroot->udev;
103
104	USB_BUS_LOCK(udev->bus);
105
106	/* round robin pipe clear stall */
107
108	pipe = udev->pipe_curr;
109	pipe_end = udev->pipes + USB_EP_MAX;
110	pipe_first = udev->pipes;
111	if (pipe == NULL) {
112		pipe = pipe_first;
113	}
114	switch (USB_GET_STATE(xfer)) {
115	case USB_ST_TRANSFERRED:
116		if (pipe->edesc &&
117		    pipe->is_stalled) {
118			pipe->toggle_next = 0;
119			pipe->is_stalled = 0;
120			/* start up the current or next transfer, if any */
121			usb2_command_wrapper(&pipe->pipe_q,
122			    pipe->pipe_q.curr);
123		}
124		pipe++;
125
126	case USB_ST_SETUP:
127tr_setup:
128		if (pipe == pipe_end) {
129			pipe = pipe_first;
130		}
131		if (pipe->edesc &&
132		    pipe->is_stalled) {
133
134			/* setup a clear-stall packet */
135
136			req.bmRequestType = UT_WRITE_ENDPOINT;
137			req.bRequest = UR_CLEAR_FEATURE;
138			USETW(req.wValue, UF_ENDPOINT_HALT);
139			req.wIndex[0] = pipe->edesc->bEndpointAddress;
140			req.wIndex[1] = 0;
141			USETW(req.wLength, 0);
142
143			/* copy in the transfer */
144
145			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
146
147			/* set length */
148			xfer->frlengths[0] = sizeof(req);
149			xfer->nframes = 1;
150			USB_BUS_UNLOCK(udev->bus);
151
152			usb2_start_hardware(xfer);
153
154			USB_BUS_LOCK(udev->bus);
155			break;
156		}
157		pipe++;
158		if (--to)
159			goto tr_setup;
160		break;
161
162	default:
163		if (xfer->error == USB_ERR_CANCELLED) {
164			break;
165		}
166		goto tr_setup;
167	}
168
169	/* store current pipe */
170	udev->pipe_curr = pipe;
171	USB_BUS_UNLOCK(udev->bus);
172}
173
174/*------------------------------------------------------------------------*
175 *	usb2_do_request_flags and usb2_do_request
176 *
177 * Description of arguments passed to these functions:
178 *
179 * "udev" - this is the "usb2_device" structure pointer on which the
180 * request should be performed. It is possible to call this function
181 * in both Host Side mode and Device Side mode.
182 *
183 * "mtx" - if this argument is non-NULL the mutex pointed to by it
184 * will get dropped and picked up during the execution of this
185 * function, hence this function sometimes needs to sleep. If this
186 * argument is NULL it has no effect.
187 *
188 * "req" - this argument must always be non-NULL and points to an
189 * 8-byte structure holding the USB request to be done. The USB
190 * request structure has a bit telling the direction of the USB
191 * request, if it is a read or a write.
192 *
193 * "data" - if the "wLength" part of the structure pointed to by "req"
194 * is non-zero this argument must point to a valid kernel buffer which
195 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
196 * be NULL.
197 *
198 * "flags" - here is a list of valid flags:
199 *
200 *  o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
201 *  specified
202 *
203 *  o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
204 *  at a later point in time. This is tunable by the "hw.usb.ss_delay"
205 *  sysctl. This flag is mostly useful for debugging.
206 *
207 *  o USB_USER_DATA_PTR: treat the "data" pointer like a userland
208 *  pointer.
209 *
210 * "actlen" - if non-NULL the actual transfer length will be stored in
211 * the 16-bit unsigned integer pointed to by "actlen". This
212 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
213 * used.
214 *
215 * "timeout" - gives the timeout for the control transfer in
216 * milliseconds. A "timeout" value less than 50 milliseconds is
217 * treated like a 50 millisecond timeout. A "timeout" value greater
218 * than 30 seconds is treated like a 30 second timeout. This USB stack
219 * does not allow control requests without a timeout.
220 *
221 * NOTE: This function is thread safe. All calls to
222 * "usb2_do_request_flags" will be serialised by the use of an
223 * internal "sx_lock".
224 *
225 * Returns:
226 *    0: Success
227 * Else: Failure
228 *------------------------------------------------------------------------*/
229usb2_error_t
230usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx,
231    struct usb2_device_request *req, void *data, uint16_t flags,
232    uint16_t *actlen, usb2_timeout_t timeout)
233{
234	struct usb2_xfer *xfer;
235	const void *desc;
236	int err = 0;
237	usb2_ticks_t start_ticks;
238	usb2_ticks_t delta_ticks;
239	usb2_ticks_t max_ticks;
240	uint16_t length;
241	uint16_t temp;
242
243	if (timeout < 50) {
244		/* timeout is too small */
245		timeout = 50;
246	}
247	if (timeout > 30000) {
248		/* timeout is too big */
249		timeout = 30000;
250	}
251	length = UGETW(req->wLength);
252
253	DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
254	    "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
255	    udev, req->bmRequestType, req->bRequest,
256	    req->wValue[1], req->wValue[0],
257	    req->wIndex[1], req->wIndex[0],
258	    req->wLength[1], req->wLength[0]);
259
260	/*
261	 * Set "actlen" to a known value in case the caller does not
262	 * check the return value:
263	 */
264	if (actlen) {
265		*actlen = 0;
266	}
267#if (USB_HAVE_USER_IO == 0)
268	if (flags & USB_USER_DATA_PTR)
269		return (USB_ERR_INVAL);
270#endif
271	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
272		DPRINTF("USB device mode\n");
273		(usb2_temp_get_desc_p) (udev, req, &desc, &temp);
274		if (length > temp) {
275			if (!(flags & USB_SHORT_XFER_OK)) {
276				return (USB_ERR_SHORT_XFER);
277			}
278			length = temp;
279		}
280		if (actlen) {
281			*actlen = length;
282		}
283		if (length > 0) {
284#if USB_HAVE_USER_IO
285			if (flags & USB_USER_DATA_PTR) {
286				if (copyout(desc, data, length)) {
287					return (USB_ERR_INVAL);
288				}
289			} else
290#endif
291				bcopy(desc, data, length);
292		}
293		return (0);		/* success */
294	}
295	if (mtx) {
296		mtx_unlock(mtx);
297		if (mtx != &Giant) {
298			mtx_assert(mtx, MA_NOTOWNED);
299		}
300	}
301	/*
302	 * Grab the default sx-lock so that serialisation
303	 * is achieved when multiple threads are involved:
304	 */
305
306	sx_xlock(udev->default_sx);
307
308	/*
309	 * Setup a new USB transfer or use the existing one, if any:
310	 */
311	usb2_default_transfer_setup(udev);
312
313	xfer = udev->default_xfer[0];
314	if (xfer == NULL) {
315		/* most likely out of memory */
316		err = USB_ERR_NOMEM;
317		goto done;
318	}
319	USB_XFER_LOCK(xfer);
320
321	if (flags & USB_DELAY_STATUS_STAGE) {
322		xfer->flags.manual_status = 1;
323	} else {
324		xfer->flags.manual_status = 0;
325	}
326
327	xfer->timeout = timeout;
328
329	start_ticks = ticks;
330
331	max_ticks = USB_MS_TO_TICKS(timeout);
332
333	usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
334
335	xfer->frlengths[0] = sizeof(*req);
336	xfer->nframes = 2;
337
338	while (1) {
339		temp = length;
340		if (temp > xfer->max_data_length) {
341			temp = xfer->max_data_length;
342		}
343		xfer->frlengths[1] = temp;
344
345		if (temp > 0) {
346			if (!(req->bmRequestType & UT_READ)) {
347#if USB_HAVE_USER_IO
348				if (flags & USB_USER_DATA_PTR) {
349					USB_XFER_UNLOCK(xfer);
350					err = usb2_copy_in_user(xfer->frbuffers + 1,
351					    0, data, temp);
352					USB_XFER_LOCK(xfer);
353					if (err) {
354						err = USB_ERR_INVAL;
355						break;
356					}
357				} else
358#endif
359					usb2_copy_in(xfer->frbuffers + 1,
360					    0, data, temp);
361			}
362			xfer->nframes = 2;
363		} else {
364			if (xfer->frlengths[0] == 0) {
365				if (xfer->flags.manual_status) {
366#if USB_DEBUG
367					int temp;
368
369					temp = usb2_ss_delay;
370					if (temp > 5000) {
371						temp = 5000;
372					}
373					if (temp > 0) {
374						usb2_pause_mtx(
375						    xfer->xroot->xfer_mtx,
376						    USB_MS_TO_TICKS(temp));
377					}
378#endif
379					xfer->flags.manual_status = 0;
380				} else {
381					break;
382				}
383			}
384			xfer->nframes = 1;
385		}
386
387		usb2_transfer_start(xfer);
388
389		while (usb2_transfer_pending(xfer)) {
390			usb2_cv_wait(udev->default_cv,
391			    xfer->xroot->xfer_mtx);
392		}
393
394		err = xfer->error;
395
396		if (err) {
397			break;
398		}
399		/* subtract length of SETUP packet, if any */
400
401		if (xfer->aframes > 0) {
402			xfer->actlen -= xfer->frlengths[0];
403		} else {
404			xfer->actlen = 0;
405		}
406
407		/* check for short packet */
408
409		if (temp > xfer->actlen) {
410			temp = xfer->actlen;
411			if (!(flags & USB_SHORT_XFER_OK)) {
412				err = USB_ERR_SHORT_XFER;
413			}
414			length = temp;
415		}
416		if (temp > 0) {
417			if (req->bmRequestType & UT_READ) {
418#if USB_HAVE_USER_IO
419				if (flags & USB_USER_DATA_PTR) {
420					USB_XFER_UNLOCK(xfer);
421					err = usb2_copy_out_user(xfer->frbuffers + 1,
422					    0, data, temp);
423					USB_XFER_LOCK(xfer);
424					if (err) {
425						err = USB_ERR_INVAL;
426						break;
427					}
428				} else
429#endif
430					usb2_copy_out(xfer->frbuffers + 1,
431					    0, data, temp);
432			}
433		}
434		/*
435		 * Clear "frlengths[0]" so that we don't send the setup
436		 * packet again:
437		 */
438		xfer->frlengths[0] = 0;
439
440		/* update length and data pointer */
441		length -= temp;
442		data = USB_ADD_BYTES(data, temp);
443
444		if (actlen) {
445			(*actlen) += temp;
446		}
447		/* check for timeout */
448
449		delta_ticks = ticks - start_ticks;
450		if (delta_ticks > max_ticks) {
451			if (!err) {
452				err = USB_ERR_TIMEOUT;
453			}
454		}
455		if (err) {
456			break;
457		}
458	}
459
460	if (err) {
461		/*
462		 * Make sure that the control endpoint is no longer
463		 * blocked in case of a non-transfer related error:
464		 */
465		usb2_transfer_stop(xfer);
466	}
467	USB_XFER_UNLOCK(xfer);
468
469done:
470	sx_xunlock(udev->default_sx);
471
472	if (mtx) {
473		mtx_lock(mtx);
474	}
475	return ((usb2_error_t)err);
476}
477
478/*------------------------------------------------------------------------*
479 *	usb2_do_request_proc - factored out code
480 *
481 * This function is factored out code. It does basically the same like
482 * usb2_do_request_flags, except it will check the status of the
483 * passed process argument before doing the USB request. If the
484 * process is draining the USB_ERR_IOERROR code will be returned. It
485 * is assumed that the mutex associated with the process is locked
486 * when calling this function.
487 *------------------------------------------------------------------------*/
488usb2_error_t
489usb2_do_request_proc(struct usb2_device *udev, struct usb2_process *pproc,
490    struct usb2_device_request *req, void *data, uint16_t flags,
491    uint16_t *actlen, usb2_timeout_t timeout)
492{
493	usb2_error_t err;
494	uint16_t len;
495
496	/* get request data length */
497	len = UGETW(req->wLength);
498
499	/* check if the device is being detached */
500	if (usb2_proc_is_gone(pproc)) {
501		err = USB_ERR_IOERROR;
502		goto done;
503	}
504
505	/* forward the USB request */
506	err = usb2_do_request_flags(udev, pproc->up_mtx,
507	    req, data, flags, actlen, timeout);
508
509done:
510	/* on failure we zero the data */
511	/* on short packet we zero the unused data */
512	if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
513		if (err)
514			memset(data, 0, len);
515		else if (actlen && *actlen != len)
516			memset(((uint8_t *)data) + *actlen, 0, len - *actlen);
517	}
518	return (err);
519}
520
521/*------------------------------------------------------------------------*
522 *	usb2_req_reset_port
523 *
524 * This function will instruct an USB HUB to perform a reset sequence
525 * on the specified port number.
526 *
527 * Returns:
528 *    0: Success. The USB device should now be at address zero.
529 * Else: Failure. No USB device is present and the USB port should be
530 *       disabled.
531 *------------------------------------------------------------------------*/
532usb2_error_t
533usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port)
534{
535	struct usb2_port_status ps;
536	usb2_error_t err;
537	uint16_t n;
538
539#if USB_DEBUG
540	uint16_t pr_poll_delay;
541	uint16_t pr_recovery_delay;
542
543#endif
544	err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
545	if (err) {
546		goto done;
547	}
548#if USB_DEBUG
549	/* range check input parameters */
550	pr_poll_delay = usb2_pr_poll_delay;
551	if (pr_poll_delay < 1) {
552		pr_poll_delay = 1;
553	} else if (pr_poll_delay > 1000) {
554		pr_poll_delay = 1000;
555	}
556	pr_recovery_delay = usb2_pr_recovery_delay;
557	if (pr_recovery_delay > 1000) {
558		pr_recovery_delay = 1000;
559	}
560#endif
561	n = 0;
562	while (1) {
563#if USB_DEBUG
564		/* wait for the device to recover from reset */
565		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay));
566		n += pr_poll_delay;
567#else
568		/* wait for the device to recover from reset */
569		usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
570		n += USB_PORT_RESET_DELAY;
571#endif
572		err = usb2_req_get_port_status(udev, mtx, &ps, port);
573		if (err) {
574			goto done;
575		}
576		/* if the device disappeared, just give up */
577		if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
578			goto done;
579		}
580		/* check if reset is complete */
581		if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
582			break;
583		}
584		/* check for timeout */
585		if (n > 1000) {
586			n = 0;
587			break;
588		}
589	}
590
591	/* clear port reset first */
592	err = usb2_req_clear_port_feature(
593	    udev, mtx, port, UHF_C_PORT_RESET);
594	if (err) {
595		goto done;
596	}
597	/* check for timeout */
598	if (n == 0) {
599		err = USB_ERR_TIMEOUT;
600		goto done;
601	}
602#if USB_DEBUG
603	/* wait for the device to recover from reset */
604	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay));
605#else
606	/* wait for the device to recover from reset */
607	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY));
608#endif
609
610done:
611	DPRINTFN(2, "port %d reset returning error=%s\n",
612	    port, usb2_errstr(err));
613	return (err);
614}
615
616/*------------------------------------------------------------------------*
617 *	usb2_req_get_desc
618 *
619 * This function can be used to retrieve USB descriptors. It contains
620 * some additional logic like zeroing of missing descriptor bytes and
621 * retrying an USB descriptor in case of failure. The "min_len"
622 * argument specifies the minimum descriptor length. The "max_len"
623 * argument specifies the maximum descriptor length. If the real
624 * descriptor length is less than the minimum length the missing
625 * byte(s) will be zeroed. The type field, the second byte of the USB
626 * descriptor, will get forced to the correct type. If the "actlen"
627 * pointer is non-NULL, the actual length of the transfer will get
628 * stored in the 16-bit unsigned integer which it is pointing to. The
629 * first byte of the descriptor will not get updated. If the "actlen"
630 * pointer is NULL the first byte of the descriptor will get updated
631 * to reflect the actual length instead. If "min_len" is not equal to
632 * "max_len" then this function will try to retrive the beginning of
633 * the descriptor and base the maximum length on the first byte of the
634 * descriptor.
635 *
636 * Returns:
637 *    0: Success
638 * Else: Failure
639 *------------------------------------------------------------------------*/
640usb2_error_t
641usb2_req_get_desc(struct usb2_device *udev,
642    struct mtx *mtx, uint16_t *actlen, void *desc,
643    uint16_t min_len, uint16_t max_len,
644    uint16_t id, uint8_t type, uint8_t index,
645    uint8_t retries)
646{
647	struct usb2_device_request req;
648	uint8_t *buf;
649	usb2_error_t err;
650
651	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
652	    id, type, index, max_len);
653
654	req.bmRequestType = UT_READ_DEVICE;
655	req.bRequest = UR_GET_DESCRIPTOR;
656	USETW2(req.wValue, type, index);
657	USETW(req.wIndex, id);
658
659	while (1) {
660
661		if ((min_len < 2) || (max_len < 2)) {
662			err = USB_ERR_INVAL;
663			goto done;
664		}
665		USETW(req.wLength, min_len);
666
667		err = usb2_do_request_flags(udev, mtx, &req,
668		    desc, 0, NULL, 1000);
669
670		if (err) {
671			if (!retries) {
672				goto done;
673			}
674			retries--;
675
676			usb2_pause_mtx(mtx, hz / 5);
677
678			continue;
679		}
680		buf = desc;
681
682		if (min_len == max_len) {
683
684			/* enforce correct length */
685			if ((buf[0] > min_len) && (actlen == NULL))
686				buf[0] = min_len;
687
688			/* enforce correct type */
689			buf[1] = type;
690
691			goto done;
692		}
693		/* range check */
694
695		if (max_len > buf[0]) {
696			max_len = buf[0];
697		}
698		/* zero minimum data */
699
700		while (min_len > max_len) {
701			min_len--;
702			buf[min_len] = 0;
703		}
704
705		/* set new minimum length */
706
707		min_len = max_len;
708	}
709done:
710	if (actlen != NULL) {
711		if (err)
712			*actlen = 0;
713		else
714			*actlen = min_len;
715	}
716	return (err);
717}
718
719/*------------------------------------------------------------------------*
720 *	usb2_req_get_string_any
721 *
722 * This function will return the string given by "string_index"
723 * using the first language ID. The maximum length "len" includes
724 * the terminating zero. The "len" argument should be twice as
725 * big pluss 2 bytes, compared with the actual maximum string length !
726 *
727 * Returns:
728 *    0: Success
729 * Else: Failure
730 *------------------------------------------------------------------------*/
731usb2_error_t
732usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf,
733    uint16_t len, uint8_t string_index)
734{
735	char *s;
736	uint8_t *temp;
737	uint16_t i;
738	uint16_t n;
739	uint16_t c;
740	uint8_t swap;
741	usb2_error_t err;
742
743	if (len == 0) {
744		/* should not happen */
745		return (USB_ERR_NORMAL_COMPLETION);
746	}
747	if (string_index == 0) {
748		/* this is the language table */
749		buf[0] = 0;
750		return (USB_ERR_INVAL);
751	}
752	if (udev->flags.no_strings) {
753		buf[0] = 0;
754		return (USB_ERR_STALLED);
755	}
756	err = usb2_req_get_string_desc
757	    (udev, mtx, buf, len, udev->langid, string_index);
758	if (err) {
759		buf[0] = 0;
760		return (err);
761	}
762	temp = (uint8_t *)buf;
763
764	if (temp[0] < 2) {
765		/* string length is too short */
766		buf[0] = 0;
767		return (USB_ERR_INVAL);
768	}
769	/* reserve one byte for terminating zero */
770	len--;
771
772	/* find maximum length */
773	s = buf;
774	n = (temp[0] / 2) - 1;
775	if (n > len) {
776		n = len;
777	}
778	/* skip descriptor header */
779	temp += 2;
780
781	/* reset swap state */
782	swap = 3;
783
784	/* convert and filter */
785	for (i = 0; (i != n); i++) {
786		c = UGETW(temp + (2 * i));
787
788		/* convert from Unicode, handle buggy strings */
789		if (((c & 0xff00) == 0) && (swap & 1)) {
790			/* Little Endian, default */
791			*s = c;
792			swap = 1;
793		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
794			/* Big Endian */
795			*s = c >> 8;
796			swap = 2;
797		} else {
798			/* silently skip bad character */
799			continue;
800		}
801
802		/*
803		 * Filter by default - we don't allow greater and less than
804		 * signs because they might confuse the dmesg printouts!
805		 */
806		if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
807			/* silently skip bad character */
808			continue;
809		}
810		s++;
811	}
812	*s = 0;				/* zero terminate resulting string */
813	return (USB_ERR_NORMAL_COMPLETION);
814}
815
816/*------------------------------------------------------------------------*
817 *	usb2_req_get_string_desc
818 *
819 * If you don't know the language ID, consider using
820 * "usb2_req_get_string_any()".
821 *
822 * Returns:
823 *    0: Success
824 * Else: Failure
825 *------------------------------------------------------------------------*/
826usb2_error_t
827usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc,
828    uint16_t max_len, uint16_t lang_id,
829    uint8_t string_index)
830{
831	return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
832	    UDESC_STRING, string_index, 0));
833}
834
835/*------------------------------------------------------------------------*
836 *	usb2_req_get_config_desc
837 *
838 * Returns:
839 *    0: Success
840 * Else: Failure
841 *------------------------------------------------------------------------*/
842usb2_error_t
843usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx,
844    struct usb2_config_descriptor *d, uint8_t conf_index)
845{
846	usb2_error_t err;
847
848	DPRINTFN(4, "confidx=%d\n", conf_index);
849
850	err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
851	    sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
852	if (err) {
853		goto done;
854	}
855	/* Extra sanity checking */
856	if (UGETW(d->wTotalLength) < sizeof(*d)) {
857		err = USB_ERR_INVAL;
858	}
859done:
860	return (err);
861}
862
863/*------------------------------------------------------------------------*
864 *	usb2_req_get_config_desc_full
865 *
866 * This function gets the complete USB configuration descriptor and
867 * ensures that "wTotalLength" is correct.
868 *
869 * Returns:
870 *    0: Success
871 * Else: Failure
872 *------------------------------------------------------------------------*/
873usb2_error_t
874usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx,
875    struct usb2_config_descriptor **ppcd, struct malloc_type *mtype,
876    uint8_t index)
877{
878	struct usb2_config_descriptor cd;
879	struct usb2_config_descriptor *cdesc;
880	uint16_t len;
881	usb2_error_t err;
882
883	DPRINTFN(4, "index=%d\n", index);
884
885	*ppcd = NULL;
886
887	err = usb2_req_get_config_desc(udev, mtx, &cd, index);
888	if (err) {
889		return (err);
890	}
891	/* get full descriptor */
892	len = UGETW(cd.wTotalLength);
893	if (len < sizeof(*cdesc)) {
894		/* corrupt descriptor */
895		return (USB_ERR_INVAL);
896	}
897	cdesc = malloc(len, mtype, M_WAITOK);
898	if (cdesc == NULL) {
899		return (USB_ERR_NOMEM);
900	}
901	err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
902	    UDESC_CONFIG, index, 3);
903	if (err) {
904		free(cdesc, mtype);
905		return (err);
906	}
907	/* make sure that the device is not fooling us: */
908	USETW(cdesc->wTotalLength, len);
909
910	*ppcd = cdesc;
911
912	return (0);			/* success */
913}
914
915/*------------------------------------------------------------------------*
916 *	usb2_req_get_device_desc
917 *
918 * Returns:
919 *    0: Success
920 * Else: Failure
921 *------------------------------------------------------------------------*/
922usb2_error_t
923usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx,
924    struct usb2_device_descriptor *d)
925{
926	DPRINTFN(4, "\n");
927	return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
928	    sizeof(*d), 0, UDESC_DEVICE, 0, 3));
929}
930
931/*------------------------------------------------------------------------*
932 *	usb2_req_get_alt_interface_no
933 *
934 * Returns:
935 *    0: Success
936 * Else: Failure
937 *------------------------------------------------------------------------*/
938usb2_error_t
939usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
940    uint8_t *alt_iface_no, uint8_t iface_index)
941{
942	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
943	struct usb2_device_request req;
944
945	if ((iface == NULL) || (iface->idesc == NULL)) {
946		return (USB_ERR_INVAL);
947	}
948	req.bmRequestType = UT_READ_INTERFACE;
949	req.bRequest = UR_GET_INTERFACE;
950	USETW(req.wValue, 0);
951	req.wIndex[0] = iface->idesc->bInterfaceNumber;
952	req.wIndex[1] = 0;
953	USETW(req.wLength, 1);
954	return (usb2_do_request(udev, mtx, &req, alt_iface_no));
955}
956
957/*------------------------------------------------------------------------*
958 *	usb2_req_set_alt_interface_no
959 *
960 * Returns:
961 *    0: Success
962 * Else: Failure
963 *------------------------------------------------------------------------*/
964usb2_error_t
965usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
966    uint8_t iface_index, uint8_t alt_no)
967{
968	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
969	struct usb2_device_request req;
970
971	if ((iface == NULL) || (iface->idesc == NULL)) {
972		return (USB_ERR_INVAL);
973	}
974	req.bmRequestType = UT_WRITE_INTERFACE;
975	req.bRequest = UR_SET_INTERFACE;
976	req.wValue[0] = alt_no;
977	req.wValue[1] = 0;
978	req.wIndex[0] = iface->idesc->bInterfaceNumber;
979	req.wIndex[1] = 0;
980	USETW(req.wLength, 0);
981	return (usb2_do_request(udev, mtx, &req, 0));
982}
983
984/*------------------------------------------------------------------------*
985 *	usb2_req_get_device_status
986 *
987 * Returns:
988 *    0: Success
989 * Else: Failure
990 *------------------------------------------------------------------------*/
991usb2_error_t
992usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx,
993    struct usb2_status *st)
994{
995	struct usb2_device_request req;
996
997	req.bmRequestType = UT_READ_DEVICE;
998	req.bRequest = UR_GET_STATUS;
999	USETW(req.wValue, 0);
1000	USETW(req.wIndex, 0);
1001	USETW(req.wLength, sizeof(*st));
1002	return (usb2_do_request(udev, mtx, &req, st));
1003}
1004
1005/*------------------------------------------------------------------------*
1006 *	usb2_req_get_hub_descriptor
1007 *
1008 * Returns:
1009 *    0: Success
1010 * Else: Failure
1011 *------------------------------------------------------------------------*/
1012usb2_error_t
1013usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx,
1014    struct usb2_hub_descriptor *hd, uint8_t nports)
1015{
1016	struct usb2_device_request req;
1017	uint16_t len = (nports + 7 + (8 * 8)) / 8;
1018
1019	req.bmRequestType = UT_READ_CLASS_DEVICE;
1020	req.bRequest = UR_GET_DESCRIPTOR;
1021	USETW2(req.wValue, UDESC_HUB, 0);
1022	USETW(req.wIndex, 0);
1023	USETW(req.wLength, len);
1024	return (usb2_do_request(udev, mtx, &req, hd));
1025}
1026
1027/*------------------------------------------------------------------------*
1028 *	usb2_req_get_hub_status
1029 *
1030 * Returns:
1031 *    0: Success
1032 * Else: Failure
1033 *------------------------------------------------------------------------*/
1034usb2_error_t
1035usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx,
1036    struct usb2_hub_status *st)
1037{
1038	struct usb2_device_request req;
1039
1040	req.bmRequestType = UT_READ_CLASS_DEVICE;
1041	req.bRequest = UR_GET_STATUS;
1042	USETW(req.wValue, 0);
1043	USETW(req.wIndex, 0);
1044	USETW(req.wLength, sizeof(struct usb2_hub_status));
1045	return (usb2_do_request(udev, mtx, &req, st));
1046}
1047
1048/*------------------------------------------------------------------------*
1049 *	usb2_req_set_address
1050 *
1051 * This function is used to set the address for an USB device. After
1052 * port reset the USB device will respond at address zero.
1053 *
1054 * Returns:
1055 *    0: Success
1056 * Else: Failure
1057 *------------------------------------------------------------------------*/
1058usb2_error_t
1059usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr)
1060{
1061	struct usb2_device_request req;
1062
1063	DPRINTFN(6, "setting device address=%d\n", addr);
1064
1065	req.bmRequestType = UT_WRITE_DEVICE;
1066	req.bRequest = UR_SET_ADDRESS;
1067	USETW(req.wValue, addr);
1068	USETW(req.wIndex, 0);
1069	USETW(req.wLength, 0);
1070
1071	/* Setting the address should not take more than 1 second ! */
1072	return (usb2_do_request_flags(udev, mtx, &req, NULL,
1073	    USB_DELAY_STATUS_STAGE, NULL, 1000));
1074}
1075
1076/*------------------------------------------------------------------------*
1077 *	usb2_req_get_port_status
1078 *
1079 * Returns:
1080 *    0: Success
1081 * Else: Failure
1082 *------------------------------------------------------------------------*/
1083usb2_error_t
1084usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx,
1085    struct usb2_port_status *ps, uint8_t port)
1086{
1087	struct usb2_device_request req;
1088
1089	req.bmRequestType = UT_READ_CLASS_OTHER;
1090	req.bRequest = UR_GET_STATUS;
1091	USETW(req.wValue, 0);
1092	req.wIndex[0] = port;
1093	req.wIndex[1] = 0;
1094	USETW(req.wLength, sizeof *ps);
1095	return (usb2_do_request(udev, mtx, &req, ps));
1096}
1097
1098/*------------------------------------------------------------------------*
1099 *	usb2_req_clear_hub_feature
1100 *
1101 * Returns:
1102 *    0: Success
1103 * Else: Failure
1104 *------------------------------------------------------------------------*/
1105usb2_error_t
1106usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1107    uint16_t sel)
1108{
1109	struct usb2_device_request req;
1110
1111	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1112	req.bRequest = UR_CLEAR_FEATURE;
1113	USETW(req.wValue, sel);
1114	USETW(req.wIndex, 0);
1115	USETW(req.wLength, 0);
1116	return (usb2_do_request(udev, mtx, &req, 0));
1117}
1118
1119/*------------------------------------------------------------------------*
1120 *	usb2_req_set_hub_feature
1121 *
1122 * Returns:
1123 *    0: Success
1124 * Else: Failure
1125 *------------------------------------------------------------------------*/
1126usb2_error_t
1127usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1128    uint16_t sel)
1129{
1130	struct usb2_device_request req;
1131
1132	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1133	req.bRequest = UR_SET_FEATURE;
1134	USETW(req.wValue, sel);
1135	USETW(req.wIndex, 0);
1136	USETW(req.wLength, 0);
1137	return (usb2_do_request(udev, mtx, &req, 0));
1138}
1139
1140/*------------------------------------------------------------------------*
1141 *	usb2_req_clear_port_feature
1142 *
1143 * Returns:
1144 *    0: Success
1145 * Else: Failure
1146 *------------------------------------------------------------------------*/
1147usb2_error_t
1148usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx,
1149    uint8_t port, uint16_t sel)
1150{
1151	struct usb2_device_request req;
1152
1153	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1154	req.bRequest = UR_CLEAR_FEATURE;
1155	USETW(req.wValue, sel);
1156	req.wIndex[0] = port;
1157	req.wIndex[1] = 0;
1158	USETW(req.wLength, 0);
1159	return (usb2_do_request(udev, mtx, &req, 0));
1160}
1161
1162/*------------------------------------------------------------------------*
1163 *	usb2_req_set_port_feature
1164 *
1165 * Returns:
1166 *    0: Success
1167 * Else: Failure
1168 *------------------------------------------------------------------------*/
1169usb2_error_t
1170usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx,
1171    uint8_t port, uint16_t sel)
1172{
1173	struct usb2_device_request req;
1174
1175	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1176	req.bRequest = UR_SET_FEATURE;
1177	USETW(req.wValue, sel);
1178	req.wIndex[0] = port;
1179	req.wIndex[1] = 0;
1180	USETW(req.wLength, 0);
1181	return (usb2_do_request(udev, mtx, &req, 0));
1182}
1183
1184/*------------------------------------------------------------------------*
1185 *	usb2_req_set_protocol
1186 *
1187 * Returns:
1188 *    0: Success
1189 * Else: Failure
1190 *------------------------------------------------------------------------*/
1191usb2_error_t
1192usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx,
1193    uint8_t iface_index, uint16_t report)
1194{
1195	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1196	struct usb2_device_request req;
1197
1198	if ((iface == NULL) || (iface->idesc == NULL)) {
1199		return (USB_ERR_INVAL);
1200	}
1201	DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1202	    iface, report, iface->idesc->bInterfaceNumber);
1203
1204	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1205	req.bRequest = UR_SET_PROTOCOL;
1206	USETW(req.wValue, report);
1207	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1208	req.wIndex[1] = 0;
1209	USETW(req.wLength, 0);
1210	return (usb2_do_request(udev, mtx, &req, 0));
1211}
1212
1213/*------------------------------------------------------------------------*
1214 *	usb2_req_set_report
1215 *
1216 * Returns:
1217 *    0: Success
1218 * Else: Failure
1219 *------------------------------------------------------------------------*/
1220usb2_error_t
1221usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len,
1222    uint8_t iface_index, uint8_t type, uint8_t id)
1223{
1224	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1225	struct usb2_device_request req;
1226
1227	if ((iface == NULL) || (iface->idesc == NULL)) {
1228		return (USB_ERR_INVAL);
1229	}
1230	DPRINTFN(5, "len=%d\n", len);
1231
1232	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1233	req.bRequest = UR_SET_REPORT;
1234	USETW2(req.wValue, type, id);
1235	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1236	req.wIndex[1] = 0;
1237	USETW(req.wLength, len);
1238	return (usb2_do_request(udev, mtx, &req, data));
1239}
1240
1241/*------------------------------------------------------------------------*
1242 *	usb2_req_get_report
1243 *
1244 * Returns:
1245 *    0: Success
1246 * Else: Failure
1247 *------------------------------------------------------------------------*/
1248usb2_error_t
1249usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data,
1250    uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1251{
1252	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1253	struct usb2_device_request req;
1254
1255	if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1256		return (USB_ERR_INVAL);
1257	}
1258	DPRINTFN(5, "len=%d\n", len);
1259
1260	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1261	req.bRequest = UR_GET_REPORT;
1262	USETW2(req.wValue, type, id);
1263	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1264	req.wIndex[1] = 0;
1265	USETW(req.wLength, len);
1266	return (usb2_do_request(udev, mtx, &req, data));
1267}
1268
1269/*------------------------------------------------------------------------*
1270 *	usb2_req_set_idle
1271 *
1272 * Returns:
1273 *    0: Success
1274 * Else: Failure
1275 *------------------------------------------------------------------------*/
1276usb2_error_t
1277usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx,
1278    uint8_t iface_index, uint8_t duration, uint8_t id)
1279{
1280	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1281	struct usb2_device_request req;
1282
1283	if ((iface == NULL) || (iface->idesc == NULL)) {
1284		return (USB_ERR_INVAL);
1285	}
1286	DPRINTFN(5, "%d %d\n", duration, id);
1287
1288	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1289	req.bRequest = UR_SET_IDLE;
1290	USETW2(req.wValue, duration, id);
1291	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1292	req.wIndex[1] = 0;
1293	USETW(req.wLength, 0);
1294	return (usb2_do_request(udev, mtx, &req, 0));
1295}
1296
1297/*------------------------------------------------------------------------*
1298 *	usb2_req_get_report_descriptor
1299 *
1300 * Returns:
1301 *    0: Success
1302 * Else: Failure
1303 *------------------------------------------------------------------------*/
1304usb2_error_t
1305usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx,
1306    void *d, uint16_t size, uint8_t iface_index)
1307{
1308	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1309	struct usb2_device_request req;
1310
1311	if ((iface == NULL) || (iface->idesc == NULL)) {
1312		return (USB_ERR_INVAL);
1313	}
1314	req.bmRequestType = UT_READ_INTERFACE;
1315	req.bRequest = UR_GET_DESCRIPTOR;
1316	USETW2(req.wValue, UDESC_REPORT, 0);	/* report id should be 0 */
1317	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1318	req.wIndex[1] = 0;
1319	USETW(req.wLength, size);
1320	return (usb2_do_request(udev, mtx, &req, d));
1321}
1322
1323/*------------------------------------------------------------------------*
1324 *	usb2_req_set_config
1325 *
1326 * This function is used to select the current configuration number in
1327 * both USB device side mode and USB host side mode. When setting the
1328 * configuration the function of the interfaces can change.
1329 *
1330 * Returns:
1331 *    0: Success
1332 * Else: Failure
1333 *------------------------------------------------------------------------*/
1334usb2_error_t
1335usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf)
1336{
1337	struct usb2_device_request req;
1338
1339	DPRINTF("setting config %d\n", conf);
1340
1341	/* do "set configuration" request */
1342
1343	req.bmRequestType = UT_WRITE_DEVICE;
1344	req.bRequest = UR_SET_CONFIG;
1345	req.wValue[0] = conf;
1346	req.wValue[1] = 0;
1347	USETW(req.wIndex, 0);
1348	USETW(req.wLength, 0);
1349	return (usb2_do_request(udev, mtx, &req, 0));
1350}
1351
1352/*------------------------------------------------------------------------*
1353 *	usb2_req_get_config
1354 *
1355 * Returns:
1356 *    0: Success
1357 * Else: Failure
1358 *------------------------------------------------------------------------*/
1359usb2_error_t
1360usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf)
1361{
1362	struct usb2_device_request req;
1363
1364	req.bmRequestType = UT_READ_DEVICE;
1365	req.bRequest = UR_GET_CONFIG;
1366	USETW(req.wValue, 0);
1367	USETW(req.wIndex, 0);
1368	USETW(req.wLength, 1);
1369	return (usb2_do_request(udev, mtx, &req, pconf));
1370}
1371
1372/*------------------------------------------------------------------------*
1373 *	usb2_req_re_enumerate
1374 *
1375 * NOTE: After this function returns the hardware is in the
1376 * unconfigured state! The application is responsible for setting a
1377 * new configuration.
1378 *
1379 * Returns:
1380 *    0: Success
1381 * Else: Failure
1382 *------------------------------------------------------------------------*/
1383usb2_error_t
1384usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx)
1385{
1386	struct usb2_device *parent_hub;
1387	usb2_error_t err;
1388	uint8_t old_addr;
1389	uint8_t do_retry = 1;
1390
1391	if (udev->flags.usb2_mode != USB_MODE_HOST) {
1392		return (USB_ERR_INVAL);
1393	}
1394	old_addr = udev->address;
1395	parent_hub = udev->parent_hub;
1396	if (parent_hub == NULL) {
1397		return (USB_ERR_INVAL);
1398	}
1399retry:
1400	err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
1401	if (err) {
1402		DPRINTFN(0, "addr=%d, port reset failed\n", old_addr);
1403		goto done;
1404	}
1405	/*
1406	 * After that the port has been reset our device should be at
1407	 * address zero:
1408	 */
1409	udev->address = USB_START_ADDR;
1410
1411	/* reset "bMaxPacketSize" */
1412	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1413
1414	/*
1415	 * Restore device address:
1416	 */
1417	err = usb2_req_set_address(udev, mtx, old_addr);
1418	if (err) {
1419		/* XXX ignore any errors! */
1420		DPRINTFN(0, "addr=%d, set address failed! (ignored)\n",
1421		    old_addr);
1422	}
1423	/* restore device address */
1424	udev->address = old_addr;
1425
1426	/* allow device time to set new address */
1427	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1428
1429	/* get the device descriptor */
1430	err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1431	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1432	if (err) {
1433		DPRINTFN(0, "getting device descriptor "
1434		    "at addr %d failed!\n", udev->address);
1435		goto done;
1436	}
1437	/* get the full device descriptor */
1438	err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc);
1439	if (err) {
1440		DPRINTFN(0, "addr=%d, getting device "
1441		    "descriptor failed!\n", old_addr);
1442		goto done;
1443	}
1444done:
1445	if (err && do_retry) {
1446		/* give the USB firmware some time to load */
1447		usb2_pause_mtx(mtx, hz / 2);
1448		/* no more retries after this retry */
1449		do_retry = 0;
1450		/* try again */
1451		goto retry;
1452	}
1453	/* restore address */
1454	udev->address = old_addr;
1455	return (err);
1456}
1457
1458/*------------------------------------------------------------------------*
1459 *	usb2_req_clear_device_feature
1460 *
1461 * Returns:
1462 *    0: Success
1463 * Else: Failure
1464 *------------------------------------------------------------------------*/
1465usb2_error_t
1466usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx,
1467    uint16_t sel)
1468{
1469	struct usb2_device_request req;
1470
1471	req.bmRequestType = UT_WRITE_DEVICE;
1472	req.bRequest = UR_CLEAR_FEATURE;
1473	USETW(req.wValue, sel);
1474	USETW(req.wIndex, 0);
1475	USETW(req.wLength, 0);
1476	return (usb2_do_request(udev, mtx, &req, 0));
1477}
1478
1479/*------------------------------------------------------------------------*
1480 *	usb2_req_set_device_feature
1481 *
1482 * Returns:
1483 *    0: Success
1484 * Else: Failure
1485 *------------------------------------------------------------------------*/
1486usb2_error_t
1487usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx,
1488    uint16_t sel)
1489{
1490	struct usb2_device_request req;
1491
1492	req.bmRequestType = UT_WRITE_DEVICE;
1493	req.bRequest = UR_SET_FEATURE;
1494	USETW(req.wValue, sel);
1495	USETW(req.wIndex, 0);
1496	USETW(req.wLength, 0);
1497	return (usb2_do_request(udev, mtx, &req, 0));
1498}
1499