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