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