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