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