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