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