xref: /freebsd/sys/dev/usb/usb_request.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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 type field, the second byte of the USB
619  * descriptor, will get forced to the correct type. If the "actlen"
620  * pointer is non-NULL, the actual length of the transfer will get
621  * stored in the 16-bit unsigned integer which it is pointing to. The
622  * first byte of the descriptor will not get updated. If the "actlen"
623  * pointer is NULL the first byte of the descriptor will get updated
624  * to reflect the actual length instead. If "min_len" is not equal to
625  * "max_len" then this function will try to retrive the beginning of
626  * the descriptor and base the maximum length on the first byte of the
627  * descriptor.
628  *
629  * Returns:
630  *    0: Success
631  * Else: Failure
632  *------------------------------------------------------------------------*/
633 usb2_error_t
634 usb2_req_get_desc(struct usb2_device *udev,
635     struct mtx *mtx, uint16_t *actlen, void *desc,
636     uint16_t min_len, uint16_t max_len,
637     uint16_t id, uint8_t type, uint8_t index,
638     uint8_t retries)
639 {
640 	struct usb2_device_request req;
641 	uint8_t *buf;
642 	usb2_error_t err;
643 
644 	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
645 	    id, type, index, max_len);
646 
647 	req.bmRequestType = UT_READ_DEVICE;
648 	req.bRequest = UR_GET_DESCRIPTOR;
649 	USETW2(req.wValue, type, index);
650 	USETW(req.wIndex, id);
651 
652 	while (1) {
653 
654 		if ((min_len < 2) || (max_len < 2)) {
655 			err = USB_ERR_INVAL;
656 			goto done;
657 		}
658 		USETW(req.wLength, min_len);
659 
660 		err = usb2_do_request_flags(udev, mtx, &req,
661 		    desc, 0, NULL, 1000);
662 
663 		if (err) {
664 			if (!retries) {
665 				goto done;
666 			}
667 			retries--;
668 
669 			usb2_pause_mtx(mtx, hz / 5);
670 
671 			continue;
672 		}
673 		buf = desc;
674 
675 		if (min_len == max_len) {
676 
677 			/* enforce correct length */
678 			if ((buf[0] > min_len) && (actlen == NULL))
679 				buf[0] = min_len;
680 
681 			/* enforce correct type */
682 			buf[1] = type;
683 
684 			goto done;
685 		}
686 		/* range check */
687 
688 		if (max_len > buf[0]) {
689 			max_len = buf[0];
690 		}
691 		/* zero minimum data */
692 
693 		while (min_len > max_len) {
694 			min_len--;
695 			buf[min_len] = 0;
696 		}
697 
698 		/* set new minimum length */
699 
700 		min_len = max_len;
701 	}
702 done:
703 	if (actlen != NULL) {
704 		if (err)
705 			*actlen = 0;
706 		else
707 			*actlen = min_len;
708 	}
709 	return (err);
710 }
711 
712 /*------------------------------------------------------------------------*
713  *	usb2_req_get_string_any
714  *
715  * This function will return the string given by "string_index"
716  * using the first language ID. The maximum length "len" includes
717  * the terminating zero. The "len" argument should be twice as
718  * big pluss 2 bytes, compared with the actual maximum string length !
719  *
720  * Returns:
721  *    0: Success
722  * Else: Failure
723  *------------------------------------------------------------------------*/
724 usb2_error_t
725 usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf,
726     uint16_t len, uint8_t string_index)
727 {
728 	char *s;
729 	uint8_t *temp;
730 	uint16_t i;
731 	uint16_t n;
732 	uint16_t c;
733 	uint8_t swap;
734 	usb2_error_t err;
735 
736 	if (len == 0) {
737 		/* should not happen */
738 		return (USB_ERR_NORMAL_COMPLETION);
739 	}
740 	if (string_index == 0) {
741 		/* this is the language table */
742 		buf[0] = 0;
743 		return (USB_ERR_INVAL);
744 	}
745 	if (udev->flags.no_strings) {
746 		buf[0] = 0;
747 		return (USB_ERR_STALLED);
748 	}
749 	err = usb2_req_get_string_desc
750 	    (udev, mtx, buf, len, udev->langid, string_index);
751 	if (err) {
752 		buf[0] = 0;
753 		return (err);
754 	}
755 	temp = (uint8_t *)buf;
756 
757 	if (temp[0] < 2) {
758 		/* string length is too short */
759 		buf[0] = 0;
760 		return (USB_ERR_INVAL);
761 	}
762 	/* reserve one byte for terminating zero */
763 	len--;
764 
765 	/* find maximum length */
766 	s = buf;
767 	n = (temp[0] / 2) - 1;
768 	if (n > len) {
769 		n = len;
770 	}
771 	/* skip descriptor header */
772 	temp += 2;
773 
774 	/* reset swap state */
775 	swap = 3;
776 
777 	/* convert and filter */
778 	for (i = 0; (i != n); i++) {
779 		c = UGETW(temp + (2 * i));
780 
781 		/* convert from Unicode, handle buggy strings */
782 		if (((c & 0xff00) == 0) && (swap & 1)) {
783 			/* Little Endian, default */
784 			*s = c;
785 			swap = 1;
786 		} else if (((c & 0x00ff) == 0) && (swap & 2)) {
787 			/* Big Endian */
788 			*s = c >> 8;
789 			swap = 2;
790 		} else {
791 			/* silently skip bad character */
792 			continue;
793 		}
794 
795 		/*
796 		 * Filter by default - we don't allow greater and less than
797 		 * signs because they might confuse the dmesg printouts!
798 		 */
799 		if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
800 			/* silently skip bad character */
801 			continue;
802 		}
803 		s++;
804 	}
805 	*s = 0;				/* zero terminate resulting string */
806 	return (USB_ERR_NORMAL_COMPLETION);
807 }
808 
809 /*------------------------------------------------------------------------*
810  *	usb2_req_get_string_desc
811  *
812  * If you don't know the language ID, consider using
813  * "usb2_req_get_string_any()".
814  *
815  * Returns:
816  *    0: Success
817  * Else: Failure
818  *------------------------------------------------------------------------*/
819 usb2_error_t
820 usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc,
821     uint16_t max_len, uint16_t lang_id,
822     uint8_t string_index)
823 {
824 	return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
825 	    UDESC_STRING, string_index, 0));
826 }
827 
828 /*------------------------------------------------------------------------*
829  *	usb2_req_get_config_desc
830  *
831  * Returns:
832  *    0: Success
833  * Else: Failure
834  *------------------------------------------------------------------------*/
835 usb2_error_t
836 usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx,
837     struct usb2_config_descriptor *d, uint8_t conf_index)
838 {
839 	usb2_error_t err;
840 
841 	DPRINTFN(4, "confidx=%d\n", conf_index);
842 
843 	err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
844 	    sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
845 	if (err) {
846 		goto done;
847 	}
848 	/* Extra sanity checking */
849 	if (UGETW(d->wTotalLength) < sizeof(*d)) {
850 		err = USB_ERR_INVAL;
851 	}
852 done:
853 	return (err);
854 }
855 
856 /*------------------------------------------------------------------------*
857  *	usb2_req_get_config_desc_full
858  *
859  * This function gets the complete USB configuration descriptor and
860  * ensures that "wTotalLength" is correct.
861  *
862  * Returns:
863  *    0: Success
864  * Else: Failure
865  *------------------------------------------------------------------------*/
866 usb2_error_t
867 usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx,
868     struct usb2_config_descriptor **ppcd, struct malloc_type *mtype,
869     uint8_t index)
870 {
871 	struct usb2_config_descriptor cd;
872 	struct usb2_config_descriptor *cdesc;
873 	uint16_t len;
874 	usb2_error_t err;
875 
876 	DPRINTFN(4, "index=%d\n", index);
877 
878 	*ppcd = NULL;
879 
880 	err = usb2_req_get_config_desc(udev, mtx, &cd, index);
881 	if (err) {
882 		return (err);
883 	}
884 	/* get full descriptor */
885 	len = UGETW(cd.wTotalLength);
886 	if (len < sizeof(*cdesc)) {
887 		/* corrupt descriptor */
888 		return (USB_ERR_INVAL);
889 	}
890 	cdesc = malloc(len, mtype, M_WAITOK);
891 	if (cdesc == NULL) {
892 		return (USB_ERR_NOMEM);
893 	}
894 	err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
895 	    UDESC_CONFIG, index, 3);
896 	if (err) {
897 		free(cdesc, mtype);
898 		return (err);
899 	}
900 	/* make sure that the device is not fooling us: */
901 	USETW(cdesc->wTotalLength, len);
902 
903 	*ppcd = cdesc;
904 
905 	return (0);			/* success */
906 }
907 
908 /*------------------------------------------------------------------------*
909  *	usb2_req_get_device_desc
910  *
911  * Returns:
912  *    0: Success
913  * Else: Failure
914  *------------------------------------------------------------------------*/
915 usb2_error_t
916 usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx,
917     struct usb2_device_descriptor *d)
918 {
919 	DPRINTFN(4, "\n");
920 	return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
921 	    sizeof(*d), 0, UDESC_DEVICE, 0, 3));
922 }
923 
924 /*------------------------------------------------------------------------*
925  *	usb2_req_get_alt_interface_no
926  *
927  * Returns:
928  *    0: Success
929  * Else: Failure
930  *------------------------------------------------------------------------*/
931 usb2_error_t
932 usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
933     uint8_t *alt_iface_no, uint8_t iface_index)
934 {
935 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
936 	struct usb2_device_request req;
937 
938 	if ((iface == NULL) || (iface->idesc == NULL)) {
939 		return (USB_ERR_INVAL);
940 	}
941 	req.bmRequestType = UT_READ_INTERFACE;
942 	req.bRequest = UR_GET_INTERFACE;
943 	USETW(req.wValue, 0);
944 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
945 	req.wIndex[1] = 0;
946 	USETW(req.wLength, 1);
947 	return (usb2_do_request(udev, mtx, &req, alt_iface_no));
948 }
949 
950 /*------------------------------------------------------------------------*
951  *	usb2_req_set_alt_interface_no
952  *
953  * Returns:
954  *    0: Success
955  * Else: Failure
956  *------------------------------------------------------------------------*/
957 usb2_error_t
958 usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
959     uint8_t iface_index, uint8_t alt_no)
960 {
961 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
962 	struct usb2_device_request req;
963 
964 	if ((iface == NULL) || (iface->idesc == NULL)) {
965 		return (USB_ERR_INVAL);
966 	}
967 	req.bmRequestType = UT_WRITE_INTERFACE;
968 	req.bRequest = UR_SET_INTERFACE;
969 	req.wValue[0] = alt_no;
970 	req.wValue[1] = 0;
971 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
972 	req.wIndex[1] = 0;
973 	USETW(req.wLength, 0);
974 	return (usb2_do_request(udev, mtx, &req, 0));
975 }
976 
977 /*------------------------------------------------------------------------*
978  *	usb2_req_get_device_status
979  *
980  * Returns:
981  *    0: Success
982  * Else: Failure
983  *------------------------------------------------------------------------*/
984 usb2_error_t
985 usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx,
986     struct usb2_status *st)
987 {
988 	struct usb2_device_request req;
989 
990 	req.bmRequestType = UT_READ_DEVICE;
991 	req.bRequest = UR_GET_STATUS;
992 	USETW(req.wValue, 0);
993 	USETW(req.wIndex, 0);
994 	USETW(req.wLength, sizeof(*st));
995 	return (usb2_do_request(udev, mtx, &req, st));
996 }
997 
998 /*------------------------------------------------------------------------*
999  *	usb2_req_get_hub_descriptor
1000  *
1001  * Returns:
1002  *    0: Success
1003  * Else: Failure
1004  *------------------------------------------------------------------------*/
1005 usb2_error_t
1006 usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx,
1007     struct usb2_hub_descriptor *hd, uint8_t nports)
1008 {
1009 	struct usb2_device_request req;
1010 	uint16_t len = (nports + 7 + (8 * 8)) / 8;
1011 
1012 	req.bmRequestType = UT_READ_CLASS_DEVICE;
1013 	req.bRequest = UR_GET_DESCRIPTOR;
1014 	USETW2(req.wValue, UDESC_HUB, 0);
1015 	USETW(req.wIndex, 0);
1016 	USETW(req.wLength, len);
1017 	return (usb2_do_request(udev, mtx, &req, hd));
1018 }
1019 
1020 /*------------------------------------------------------------------------*
1021  *	usb2_req_get_hub_status
1022  *
1023  * Returns:
1024  *    0: Success
1025  * Else: Failure
1026  *------------------------------------------------------------------------*/
1027 usb2_error_t
1028 usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx,
1029     struct usb2_hub_status *st)
1030 {
1031 	struct usb2_device_request req;
1032 
1033 	req.bmRequestType = UT_READ_CLASS_DEVICE;
1034 	req.bRequest = UR_GET_STATUS;
1035 	USETW(req.wValue, 0);
1036 	USETW(req.wIndex, 0);
1037 	USETW(req.wLength, sizeof(struct usb2_hub_status));
1038 	return (usb2_do_request(udev, mtx, &req, st));
1039 }
1040 
1041 /*------------------------------------------------------------------------*
1042  *	usb2_req_set_address
1043  *
1044  * This function is used to set the address for an USB device. After
1045  * port reset the USB device will respond at address zero.
1046  *
1047  * Returns:
1048  *    0: Success
1049  * Else: Failure
1050  *------------------------------------------------------------------------*/
1051 usb2_error_t
1052 usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr)
1053 {
1054 	struct usb2_device_request req;
1055 
1056 	DPRINTFN(6, "setting device address=%d\n", addr);
1057 
1058 	req.bmRequestType = UT_WRITE_DEVICE;
1059 	req.bRequest = UR_SET_ADDRESS;
1060 	USETW(req.wValue, addr);
1061 	USETW(req.wIndex, 0);
1062 	USETW(req.wLength, 0);
1063 
1064 	/* Setting the address should not take more than 1 second ! */
1065 	return (usb2_do_request_flags(udev, mtx, &req, NULL,
1066 	    USB_DELAY_STATUS_STAGE, NULL, 1000));
1067 }
1068 
1069 /*------------------------------------------------------------------------*
1070  *	usb2_req_get_port_status
1071  *
1072  * Returns:
1073  *    0: Success
1074  * Else: Failure
1075  *------------------------------------------------------------------------*/
1076 usb2_error_t
1077 usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx,
1078     struct usb2_port_status *ps, uint8_t port)
1079 {
1080 	struct usb2_device_request req;
1081 
1082 	req.bmRequestType = UT_READ_CLASS_OTHER;
1083 	req.bRequest = UR_GET_STATUS;
1084 	USETW(req.wValue, 0);
1085 	req.wIndex[0] = port;
1086 	req.wIndex[1] = 0;
1087 	USETW(req.wLength, sizeof *ps);
1088 	return (usb2_do_request(udev, mtx, &req, ps));
1089 }
1090 
1091 /*------------------------------------------------------------------------*
1092  *	usb2_req_clear_hub_feature
1093  *
1094  * Returns:
1095  *    0: Success
1096  * Else: Failure
1097  *------------------------------------------------------------------------*/
1098 usb2_error_t
1099 usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1100     uint16_t sel)
1101 {
1102 	struct usb2_device_request req;
1103 
1104 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1105 	req.bRequest = UR_CLEAR_FEATURE;
1106 	USETW(req.wValue, sel);
1107 	USETW(req.wIndex, 0);
1108 	USETW(req.wLength, 0);
1109 	return (usb2_do_request(udev, mtx, &req, 0));
1110 }
1111 
1112 /*------------------------------------------------------------------------*
1113  *	usb2_req_set_hub_feature
1114  *
1115  * Returns:
1116  *    0: Success
1117  * Else: Failure
1118  *------------------------------------------------------------------------*/
1119 usb2_error_t
1120 usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1121     uint16_t sel)
1122 {
1123 	struct usb2_device_request req;
1124 
1125 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1126 	req.bRequest = UR_SET_FEATURE;
1127 	USETW(req.wValue, sel);
1128 	USETW(req.wIndex, 0);
1129 	USETW(req.wLength, 0);
1130 	return (usb2_do_request(udev, mtx, &req, 0));
1131 }
1132 
1133 /*------------------------------------------------------------------------*
1134  *	usb2_req_clear_port_feature
1135  *
1136  * Returns:
1137  *    0: Success
1138  * Else: Failure
1139  *------------------------------------------------------------------------*/
1140 usb2_error_t
1141 usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx,
1142     uint8_t port, uint16_t sel)
1143 {
1144 	struct usb2_device_request req;
1145 
1146 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1147 	req.bRequest = UR_CLEAR_FEATURE;
1148 	USETW(req.wValue, sel);
1149 	req.wIndex[0] = port;
1150 	req.wIndex[1] = 0;
1151 	USETW(req.wLength, 0);
1152 	return (usb2_do_request(udev, mtx, &req, 0));
1153 }
1154 
1155 /*------------------------------------------------------------------------*
1156  *	usb2_req_set_port_feature
1157  *
1158  * Returns:
1159  *    0: Success
1160  * Else: Failure
1161  *------------------------------------------------------------------------*/
1162 usb2_error_t
1163 usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx,
1164     uint8_t port, uint16_t sel)
1165 {
1166 	struct usb2_device_request req;
1167 
1168 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
1169 	req.bRequest = UR_SET_FEATURE;
1170 	USETW(req.wValue, sel);
1171 	req.wIndex[0] = port;
1172 	req.wIndex[1] = 0;
1173 	USETW(req.wLength, 0);
1174 	return (usb2_do_request(udev, mtx, &req, 0));
1175 }
1176 
1177 /*------------------------------------------------------------------------*
1178  *	usb2_req_set_protocol
1179  *
1180  * Returns:
1181  *    0: Success
1182  * Else: Failure
1183  *------------------------------------------------------------------------*/
1184 usb2_error_t
1185 usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx,
1186     uint8_t iface_index, uint16_t report)
1187 {
1188 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1189 	struct usb2_device_request req;
1190 
1191 	if ((iface == NULL) || (iface->idesc == NULL)) {
1192 		return (USB_ERR_INVAL);
1193 	}
1194 	DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1195 	    iface, report, iface->idesc->bInterfaceNumber);
1196 
1197 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1198 	req.bRequest = UR_SET_PROTOCOL;
1199 	USETW(req.wValue, report);
1200 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1201 	req.wIndex[1] = 0;
1202 	USETW(req.wLength, 0);
1203 	return (usb2_do_request(udev, mtx, &req, 0));
1204 }
1205 
1206 /*------------------------------------------------------------------------*
1207  *	usb2_req_set_report
1208  *
1209  * Returns:
1210  *    0: Success
1211  * Else: Failure
1212  *------------------------------------------------------------------------*/
1213 usb2_error_t
1214 usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len,
1215     uint8_t iface_index, uint8_t type, uint8_t id)
1216 {
1217 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1218 	struct usb2_device_request req;
1219 
1220 	if ((iface == NULL) || (iface->idesc == NULL)) {
1221 		return (USB_ERR_INVAL);
1222 	}
1223 	DPRINTFN(5, "len=%d\n", len);
1224 
1225 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1226 	req.bRequest = UR_SET_REPORT;
1227 	USETW2(req.wValue, type, id);
1228 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1229 	req.wIndex[1] = 0;
1230 	USETW(req.wLength, len);
1231 	return (usb2_do_request(udev, mtx, &req, data));
1232 }
1233 
1234 /*------------------------------------------------------------------------*
1235  *	usb2_req_get_report
1236  *
1237  * Returns:
1238  *    0: Success
1239  * Else: Failure
1240  *------------------------------------------------------------------------*/
1241 usb2_error_t
1242 usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data,
1243     uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1244 {
1245 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1246 	struct usb2_device_request req;
1247 
1248 	if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1249 		return (USB_ERR_INVAL);
1250 	}
1251 	DPRINTFN(5, "len=%d\n", len);
1252 
1253 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1254 	req.bRequest = UR_GET_REPORT;
1255 	USETW2(req.wValue, type, id);
1256 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1257 	req.wIndex[1] = 0;
1258 	USETW(req.wLength, len);
1259 	return (usb2_do_request(udev, mtx, &req, data));
1260 }
1261 
1262 /*------------------------------------------------------------------------*
1263  *	usb2_req_set_idle
1264  *
1265  * Returns:
1266  *    0: Success
1267  * Else: Failure
1268  *------------------------------------------------------------------------*/
1269 usb2_error_t
1270 usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx,
1271     uint8_t iface_index, uint8_t duration, uint8_t id)
1272 {
1273 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1274 	struct usb2_device_request req;
1275 
1276 	if ((iface == NULL) || (iface->idesc == NULL)) {
1277 		return (USB_ERR_INVAL);
1278 	}
1279 	DPRINTFN(5, "%d %d\n", duration, id);
1280 
1281 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1282 	req.bRequest = UR_SET_IDLE;
1283 	USETW2(req.wValue, duration, id);
1284 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1285 	req.wIndex[1] = 0;
1286 	USETW(req.wLength, 0);
1287 	return (usb2_do_request(udev, mtx, &req, 0));
1288 }
1289 
1290 /*------------------------------------------------------------------------*
1291  *	usb2_req_get_report_descriptor
1292  *
1293  * Returns:
1294  *    0: Success
1295  * Else: Failure
1296  *------------------------------------------------------------------------*/
1297 usb2_error_t
1298 usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx,
1299     void *d, uint16_t size, uint8_t iface_index)
1300 {
1301 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1302 	struct usb2_device_request req;
1303 
1304 	if ((iface == NULL) || (iface->idesc == NULL)) {
1305 		return (USB_ERR_INVAL);
1306 	}
1307 	req.bmRequestType = UT_READ_INTERFACE;
1308 	req.bRequest = UR_GET_DESCRIPTOR;
1309 	USETW2(req.wValue, UDESC_REPORT, 0);	/* report id should be 0 */
1310 	req.wIndex[0] = iface->idesc->bInterfaceNumber;
1311 	req.wIndex[1] = 0;
1312 	USETW(req.wLength, size);
1313 	return (usb2_do_request(udev, mtx, &req, d));
1314 }
1315 
1316 /*------------------------------------------------------------------------*
1317  *	usb2_req_set_config
1318  *
1319  * This function is used to select the current configuration number in
1320  * both USB device side mode and USB host side mode. When setting the
1321  * configuration the function of the interfaces can change.
1322  *
1323  * Returns:
1324  *    0: Success
1325  * Else: Failure
1326  *------------------------------------------------------------------------*/
1327 usb2_error_t
1328 usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf)
1329 {
1330 	struct usb2_device_request req;
1331 
1332 	DPRINTF("setting config %d\n", conf);
1333 
1334 	/* do "set configuration" request */
1335 
1336 	req.bmRequestType = UT_WRITE_DEVICE;
1337 	req.bRequest = UR_SET_CONFIG;
1338 	req.wValue[0] = conf;
1339 	req.wValue[1] = 0;
1340 	USETW(req.wIndex, 0);
1341 	USETW(req.wLength, 0);
1342 	return (usb2_do_request(udev, mtx, &req, 0));
1343 }
1344 
1345 /*------------------------------------------------------------------------*
1346  *	usb2_req_get_config
1347  *
1348  * Returns:
1349  *    0: Success
1350  * Else: Failure
1351  *------------------------------------------------------------------------*/
1352 usb2_error_t
1353 usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf)
1354 {
1355 	struct usb2_device_request req;
1356 
1357 	req.bmRequestType = UT_READ_DEVICE;
1358 	req.bRequest = UR_GET_CONFIG;
1359 	USETW(req.wValue, 0);
1360 	USETW(req.wIndex, 0);
1361 	USETW(req.wLength, 1);
1362 	return (usb2_do_request(udev, mtx, &req, pconf));
1363 }
1364 
1365 /*------------------------------------------------------------------------*
1366  *	usb2_req_re_enumerate
1367  *
1368  * NOTE: After this function returns the hardware is in the
1369  * unconfigured state! The application is responsible for setting a
1370  * new configuration.
1371  *
1372  * Returns:
1373  *    0: Success
1374  * Else: Failure
1375  *------------------------------------------------------------------------*/
1376 usb2_error_t
1377 usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx)
1378 {
1379 	struct usb2_device *parent_hub;
1380 	usb2_error_t err;
1381 	uint8_t old_addr;
1382 	uint8_t do_retry = 1;
1383 
1384 	if (udev->flags.usb2_mode != USB_MODE_HOST) {
1385 		return (USB_ERR_INVAL);
1386 	}
1387 	old_addr = udev->address;
1388 	parent_hub = udev->parent_hub;
1389 	if (parent_hub == NULL) {
1390 		return (USB_ERR_INVAL);
1391 	}
1392 retry:
1393 	err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
1394 	if (err) {
1395 		DPRINTFN(0, "addr=%d, port reset failed\n", old_addr);
1396 		goto done;
1397 	}
1398 	/*
1399 	 * After that the port has been reset our device should be at
1400 	 * address zero:
1401 	 */
1402 	udev->address = USB_START_ADDR;
1403 
1404 	/* reset "bMaxPacketSize" */
1405 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1406 
1407 	/*
1408 	 * Restore device address:
1409 	 */
1410 	err = usb2_req_set_address(udev, mtx, old_addr);
1411 	if (err) {
1412 		/* XXX ignore any errors! */
1413 		DPRINTFN(0, "addr=%d, set address failed! (ignored)\n",
1414 		    old_addr);
1415 	}
1416 	/* restore device address */
1417 	udev->address = old_addr;
1418 
1419 	/* allow device time to set new address */
1420 	usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1421 
1422 	/* get the device descriptor */
1423 	err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1424 	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1425 	if (err) {
1426 		DPRINTFN(0, "getting device descriptor "
1427 		    "at addr %d failed!\n", udev->address);
1428 		goto done;
1429 	}
1430 	/* get the full device descriptor */
1431 	err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc);
1432 	if (err) {
1433 		DPRINTFN(0, "addr=%d, getting device "
1434 		    "descriptor failed!\n", old_addr);
1435 		goto done;
1436 	}
1437 done:
1438 	if (err && do_retry) {
1439 		/* give the USB firmware some time to load */
1440 		usb2_pause_mtx(mtx, hz / 2);
1441 		/* no more retries after this retry */
1442 		do_retry = 0;
1443 		/* try again */
1444 		goto retry;
1445 	}
1446 	/* restore address */
1447 	udev->address = old_addr;
1448 	return (err);
1449 }
1450 
1451 /*------------------------------------------------------------------------*
1452  *	usb2_req_clear_device_feature
1453  *
1454  * Returns:
1455  *    0: Success
1456  * Else: Failure
1457  *------------------------------------------------------------------------*/
1458 usb2_error_t
1459 usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx,
1460     uint16_t sel)
1461 {
1462 	struct usb2_device_request req;
1463 
1464 	req.bmRequestType = UT_WRITE_DEVICE;
1465 	req.bRequest = UR_CLEAR_FEATURE;
1466 	USETW(req.wValue, sel);
1467 	USETW(req.wIndex, 0);
1468 	USETW(req.wLength, 0);
1469 	return (usb2_do_request(udev, mtx, &req, 0));
1470 }
1471 
1472 /*------------------------------------------------------------------------*
1473  *	usb2_req_set_device_feature
1474  *
1475  * Returns:
1476  *    0: Success
1477  * Else: Failure
1478  *------------------------------------------------------------------------*/
1479 usb2_error_t
1480 usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx,
1481     uint16_t sel)
1482 {
1483 	struct usb2_device_request req;
1484 
1485 	req.bmRequestType = UT_WRITE_DEVICE;
1486 	req.bRequest = UR_SET_FEATURE;
1487 	USETW(req.wValue, sel);
1488 	USETW(req.wIndex, 0);
1489 	USETW(req.wLength, 0);
1490 	return (usb2_do_request(udev, mtx, &req, 0));
1491 }
1492