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