1 /* $FreeBSD: releng/12.2/sys/dev/usb/usb_request.c 366693 2020-10-14 06:25:55Z martymac $ */
2 /*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include "implementation/global_implementation.h"
32
33 static int usb_no_cs_fail;
34 static int usb_full_ddesc;
35
36 #define usb_port_reset_recovery_max 2000 /* ms */
37 static uint32_t usb_reset_port_flag[8]; /* 256 bits for reset port flag refer to USB_MAX_PORTS */
38 #define PORTNO_TO_NBIT(portno, i) (1U << (portno - (i << 5)))
39
40 static void
usb_reset_port_flag_set(uint8_t portno)41 usb_reset_port_flag_set(uint8_t portno)
42 {
43 uint32_t i = portno >> 5;
44
45 usb_reset_port_flag[i] |= PORTNO_TO_NBIT(portno, i);
46 }
47
48 static void
usb_reset_port_flag_clear(uint8_t portno)49 usb_reset_port_flag_clear(uint8_t portno)
50 {
51 uint32_t i = portno >> 5;
52
53 usb_reset_port_flag[i] &= ~PORTNO_TO_NBIT(portno, i);
54 }
55
56 static bool
usb_reset_port_flag_is_set(uint8_t portno)57 usb_reset_port_flag_is_set(uint8_t portno)
58 {
59 uint32_t i = portno >> 5;
60
61 if (usb_reset_port_flag[i] & PORTNO_TO_NBIT(portno, i)) {
62 return true;
63 } else {
64 return false;
65 }
66 }
67
68 #undef USB_DEBUG_VAR
69 #define USB_DEBUG_VAR usb_debug
70 #ifdef LOSCFG_USB_DEBUG
71 #ifdef USB_REQ_DEBUG
72 /* The following structures are used in connection to fault injection. */
73 struct usb_ctrl_debug {
74 int bus_index; /* target bus */
75 int dev_index; /* target address */
76 int ds_fail; /* fail data stage */
77 int ss_fail; /* fail status stage */
78 int ds_delay; /* data stage delay in ms */
79 int ss_delay; /* status stage delay in ms */
80 int bmRequestType_value;
81 int bRequest_value;
82 };
83
84 struct usb_ctrl_debug_bits {
85 uint16_t ds_delay;
86 uint16_t ss_delay;
87 uint8_t ds_fail:1;
88 uint8_t ss_fail:1;
89 uint8_t enabled:1;
90 };
91
92 /* The default is to disable fault injection. */
93
94 static struct usb_ctrl_debug usb_ctrl_debug = {
95 .bus_index = -1,
96 .dev_index = -1,
97 .bmRequestType_value = -1,
98 .bRequest_value = -1,
99 };
100
101 /*------------------------------------------------------------------------*
102 * usbd_get_debug_bits
103 *
104 * This function is only useful in USB host mode.
105 *------------------------------------------------------------------------*/
106 static void
usbd_get_debug_bits(struct usb_device * udev,struct usb_device_request * req,struct usb_ctrl_debug_bits * dbg)107 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req,
108 struct usb_ctrl_debug_bits *dbg)
109 {
110 int temp;
111
112 (void)memset_s(dbg, sizeof(*dbg), 0, sizeof(*dbg));
113
114 /* Compute data stage delay */
115
116 temp = usb_ctrl_debug.ds_delay;
117 if (temp < 0)
118 temp = 0;
119 else if (temp > (16*1024))
120 temp = (16*1024);
121
122 dbg->ds_delay = temp;
123
124 /* Compute status stage delay */
125
126 temp = usb_ctrl_debug.ss_delay;
127 if (temp < 0)
128 temp = 0;
129 else if (temp > (16*1024))
130 temp = (16*1024);
131
132 dbg->ss_delay = temp;
133
134 /* Check if this control request should be failed */
135
136 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index)
137 return;
138
139 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index)
140 return;
141
142 temp = usb_ctrl_debug.bmRequestType_value;
143
144 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255))
145 return;
146
147 temp = usb_ctrl_debug.bRequest_value;
148
149 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255))
150 return;
151
152 temp = usb_ctrl_debug.ds_fail;
153 if (temp)
154 dbg->ds_fail = 1;
155
156 temp = usb_ctrl_debug.ss_fail;
157 if (temp)
158 dbg->ss_fail = 1;
159
160 dbg->enabled = 1;
161 }
162 #endif /* USB_REQ_DEBUG */
163 #endif /* LOSCFG_USB_DEBUG */
164
165 /*------------------------------------------------------------------------*
166 * usbd_do_request_callback
167 *
168 * This function is the USB callback for generic USB Host control
169 * transfers.
170 *------------------------------------------------------------------------*/
171 void
usbd_do_request_callback(struct usb_xfer * xfer,usb_error_t error)172 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error)
173 {
174 ; /* workaround for a bug in "indent" */
175
176 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
177
178 switch (USB_GET_STATE(xfer)) {
179 case USB_ST_SETUP:
180 usbd_transfer_submit(xfer);
181 break;
182 default:
183 (void)cv_signal(&xfer->xroot->udev->ctrlreq_cv);
184 break;
185 }
186 }
187
188 /*------------------------------------------------------------------------*
189 * usb_do_clear_stall_callback
190 *
191 * This function is the USB callback for generic clear stall requests.
192 *------------------------------------------------------------------------*/
193 void
usb_do_clear_stall_callback(struct usb_xfer * xfer,usb_error_t error)194 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error)
195 {
196 struct usb_device_request req;
197 struct usb_device *udev;
198 struct usb_endpoint *ep;
199 struct usb_endpoint *ep_end;
200 struct usb_endpoint *ep_first;
201 usb_stream_t x;
202 uint8_t to;
203
204 udev = xfer->xroot->udev;
205
206 USB_BUS_LOCK(udev->bus);
207
208 /* round robin endpoint clear stall */
209
210 ep = udev->ep_curr;
211 ep_end = udev->endpoints + udev->endpoints_max;
212 ep_first = udev->endpoints;
213 to = udev->endpoints_max;
214
215 switch (USB_GET_STATE(xfer)) {
216 case USB_ST_TRANSFERRED:
217 tr_transferred:
218 /* reset error counter */
219 udev->clear_stall_errors = 0;
220
221 if (ep == NULL)
222 goto tr_setup; /* device was unconfigured */
223 if (ep->edesc &&
224 ep->is_stalled) {
225 ep->toggle_next = 0;
226 ep->is_stalled = 0;
227 /* some hardware needs a callback to clear the data toggle */
228 usbd_clear_stall_locked(udev, ep);
229 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
230 /* start the current or next transfer, if any */
231 usb_command_wrapper(&ep->endpoint_q[x],
232 ep->endpoint_q[x].curr);
233 }
234 }
235 ep++;
236
237 case USB_ST_SETUP:
238 tr_setup:
239 if (to == 0)
240 break; /* no endpoints - nothing to do */
241 if ((ep < ep_first) || (ep >= ep_end))
242 ep = ep_first; /* endpoint wrapped around */
243 if (ep->edesc &&
244 ep->is_stalled) {
245
246 /* setup a clear-stall packet */
247
248 req.bmRequestType = UT_WRITE_ENDPOINT;
249 req.bRequest = UR_CLEAR_FEATURE;
250 USETW(req.wValue, UF_ENDPOINT_HALT);
251 req.wIndex[0] = ep->edesc->bEndpointAddress;
252 req.wIndex[1] = 0;
253 USETW(req.wLength, 0);
254
255 /* copy in the transfer */
256
257 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
258
259 /* set length */
260 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
261 xfer->nframes = 1;
262 USB_BUS_UNLOCK(udev->bus);
263
264 usbd_transfer_submit(xfer);
265
266 USB_BUS_LOCK(udev->bus);
267 break;
268 }
269 ep++;
270 to--;
271 goto tr_setup;
272
273 default:
274 if (error == USB_ERR_CANCELLED)
275 break;
276
277 DPRINTF("Clear stall failed.\n");
278
279 /*
280 * Some VMs like VirtualBox always return failure on
281 * clear-stall which we sometimes should just ignore.
282 */
283 if (usb_no_cs_fail)
284 goto tr_transferred;
285 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT)
286 goto tr_setup;
287
288 if (error == USB_ERR_TIMEOUT) {
289 udev->clear_stall_errors = USB_CS_RESET_LIMIT;
290 DPRINTF("Trying to re-enumerate.\n");
291 usbd_start_re_enumerate(udev);
292 } else {
293 udev->clear_stall_errors++;
294 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) {
295 DPRINTF("Trying to re-enumerate.\n");
296 usbd_start_re_enumerate(udev);
297 }
298 }
299 goto tr_setup;
300 }
301
302 /* store current endpoint */
303 udev->ep_curr = ep;
304 USB_BUS_UNLOCK(udev->bus);
305 }
306
307 static usb_handle_req_t *
usbd_get_hr_func(struct usb_device * udev)308 usbd_get_hr_func(struct usb_device *udev)
309 {
310 /* figure out if there is a Handle Request function */
311 if (udev->flags.usb_mode == USB_MODE_DEVICE)
312 return (usb_temp_get_desc_p);
313 else if (udev->parent_hub == NULL)
314 return (udev->bus->methods->roothub_exec);
315 else
316 return (NULL);
317 }
318
319 /*------------------------------------------------------------------------*
320 * usbd_do_request_flags and usbd_do_request
321 *
322 * Description of arguments passed to these functions:
323 *
324 * "udev" - this is the "usb_device" structure pointer on which the
325 * request should be performed. It is possible to call this function
326 * in both Host Side mode and Device Side mode.
327 *
328 * "mtx" - if this argument is non-NULL the mutex pointed to by it
329 * will get dropped and picked up during the execution of this
330 * function, hence this function sometimes needs to sleep. If this
331 * argument is NULL it has no effect.
332 *
333 * "req" - this argument must always be non-NULL and points to an
334 * 8-byte structure holding the USB request to be done. The USB
335 * request structure has a bit telling the direction of the USB
336 * request, if it is a read or a write.
337 *
338 * "data" - if the "wLength" part of the structure pointed to by "req"
339 * is non-zero this argument must point to a valid kernel buffer which
340 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
341 * be NULL.
342 *
343 * "flags" - here is a list of valid flags:
344 *
345 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
346 * specified
347 *
348 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
349 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
350 * sysctl. This flag is mostly useful for debugging.
351 *
352 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
353 * pointer.
354 *
355 * "actlen" - if non-NULL the actual transfer length will be stored in
356 * the 16-bit unsigned integer pointed to by "actlen". This
357 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
358 * used.
359 *
360 * "timeout" - gives the timeout for the control transfer in
361 * milliseconds. A "timeout" value less than 50 milliseconds is
362 * treated like a 50 millisecond timeout. A "timeout" value greater
363 * than 30 seconds is treated like a 30 second timeout. This USB stack
364 * does not allow control requests without a timeout.
365 *
366 * NOTE: This function is thread safe. All calls to "usbd_do_request_flags"
367 * will be serialized by the use of the USB device enumeration lock.
368 *
369 * Returns:
370 * 0: Success
371 * Else: Failure
372 *------------------------------------------------------------------------*/
373 usb_error_t
usbd_do_request_flags(struct usb_device * udev,struct mtx * mtx,struct usb_device_request * req,void * data,uint16_t flags,uint16_t * actlen,usb_timeout_t timeout)374 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
375 struct usb_device_request *req, void *data, uint16_t flags,
376 uint16_t *actlen, usb_timeout_t timeout)
377 {
378 #ifdef USB_REQ_DEBUG
379 struct usb_ctrl_debug_bits dbg;
380 #endif
381 usb_handle_req_t *hr_func;
382 struct usb_xfer *xfer;
383 const void *desc;
384 int err = 0;
385 usb_ticks_t start_ticks;
386 usb_ticks_t delta_ticks;
387 usb_ticks_t max_ticks;
388 uint16_t length;
389 uint16_t temp;
390 uint16_t acttemp;
391 uint8_t do_unlock;
392
393 if (timeout < 50) {
394 /* timeout is too small */
395 timeout = 50;
396 }
397 if (timeout > 30000) {
398 /* timeout is too big */
399 timeout = 30000;
400 }
401 length = UGETW(req->wLength);
402
403 DPRINTFN(6, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
404 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
405 udev, req->bmRequestType, req->bRequest,
406 req->wValue[1], req->wValue[0],
407 req->wIndex[1], req->wIndex[0],
408 req->wLength[1], req->wLength[0]);
409
410 /* Check if the device is still alive */
411 if (udev->state < USB_STATE_POWERED) {
412 DPRINTF("usb device has gone\n");
413 return (USB_ERR_NOT_CONFIGURED);
414 }
415
416 /*
417 * Set "actlen" to a known value in case the caller does not
418 * check the return value:
419 */
420 if (actlen)
421 *actlen = 0;
422
423 #if (USB_HAVE_USER_IO == 0)
424 if (flags & USB_USER_DATA_PTR)
425 return (USB_ERR_INVAL);
426 #endif
427 if ((mtx != NULL) && (mtx != &Giant)) {
428 USB_MTX_UNLOCK(mtx);
429 USB_MTX_ASSERT(mtx, MA_NOTOWNED);
430 }
431
432 /*
433 * Serialize access to this function:
434 */
435 do_unlock = usbd_ctrl_lock(udev);
436
437 hr_func = usbd_get_hr_func(udev);
438 if (hr_func != NULL) {
439 DPRINTF("Handle Request function is set\n");
440
441 desc = NULL;
442 temp = 0;
443
444 if (!(req->bmRequestType & UT_READ)) {
445 if (length != 0) {
446 DPRINTFN(1, "The handle request function "
447 "does not support writing data!\n");
448 err = USB_ERR_INVAL;
449 goto done;
450 }
451 }
452
453 /* The root HUB code needs the BUS lock locked */
454
455 USB_BUS_LOCK(udev->bus);
456 err = (hr_func) (udev, req, &desc, &temp);
457 USB_BUS_UNLOCK(udev->bus);
458
459 if (err)
460 goto done;
461
462 if (length > temp) {
463 if (!(flags & USB_SHORT_XFER_OK)) {
464 err = USB_ERR_SHORT_XFER;
465 goto done;
466 }
467 length = temp;
468 }
469 if (actlen)
470 *actlen = length;
471
472 if (length > 0) {
473 #if USB_HAVE_USER_IO
474 if (flags & USB_USER_DATA_PTR) {
475 if (copyout(desc, data, length)) {
476 err = USB_ERR_INVAL;
477 goto done;
478 }
479 } else
480 #endif
481 (void)memcpy_s(data, length, desc, length);
482 }
483 goto done; /* success */
484 }
485
486 /*
487 * Setup a new USB transfer or use the existing one, if any:
488 */
489 usbd_ctrl_transfer_setup(udev);
490
491 xfer = udev->ctrl_xfer[0];
492 if (xfer == NULL) {
493 /* most likely out of memory */
494 err = USB_ERR_NOMEM;
495 goto done;
496 }
497
498 #ifdef USB_REQ_DEBUG
499 /* Get debug bits */
500 usbd_get_debug_bits(udev, req, &dbg);
501
502 /* Check for fault injection */
503 if (dbg.enabled)
504 flags |= USB_DELAY_STATUS_STAGE;
505 #endif
506 USB_XFER_LOCK(xfer);
507
508 if (flags & USB_DELAY_STATUS_STAGE)
509 xfer->flags.manual_status = 1;
510 else
511 xfer->flags.manual_status = 0;
512
513 if (flags & USB_SHORT_XFER_OK)
514 xfer->flags.short_xfer_ok = 1;
515 else
516 xfer->flags.short_xfer_ok = 0;
517
518 xfer->timeout = timeout;
519
520 start_ticks = CUR_TICKS;
521
522 max_ticks = USB_MS_TO_TICKS(timeout);
523
524 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
525
526 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req));
527
528 while (1) {
529 temp = length;
530 if (temp > usbd_xfer_max_len(xfer)) {
531 temp = usbd_xfer_max_len(xfer);
532 }
533 #ifdef USB_REQ_DEBUG
534 if (xfer->flags.manual_status) {
535 if (usbd_xfer_frame_len(xfer, 0) != 0) {
536 /* Execute data stage separately */
537 temp = 0;
538 } else if (temp > 0) {
539 if (dbg.ds_fail) {
540 err = USB_ERR_INVAL;
541 break;
542 }
543 if (dbg.ds_delay > 0) {
544 usb_pause_mtx(
545 xfer->xroot->xfer_mtx,
546 USB_MS_TO_TICKS(dbg.ds_delay));
547 /* make sure we don't time out */
548 start_ticks = CUR_TICKS;
549 }
550 }
551 }
552 #endif
553 usbd_xfer_set_frame_len(xfer, 1, temp);
554
555 if (temp > 0) {
556 if (!(req->bmRequestType & UT_READ)) {
557 #if USB_HAVE_USER_IO
558 if (flags & USB_USER_DATA_PTR) {
559 USB_XFER_UNLOCK(xfer);
560 err = usbd_copy_in_user(xfer->frbuffers + 1,
561 0, data, temp);
562 USB_XFER_LOCK(xfer);
563 if (err) {
564 err = USB_ERR_INVAL;
565 break;
566 }
567 } else
568 #endif
569 usbd_copy_in(xfer->frbuffers + 1,
570 0, data, temp);
571 }
572 usbd_xfer_set_frames(xfer, 2);
573 } else {
574 if (usbd_xfer_frame_len(xfer, 0) == 0) {
575 if (xfer->flags.manual_status) {
576 #ifdef USB_REQ_DEBUG
577 if (dbg.ss_fail) {
578 err = USB_ERR_INVAL;
579 break;
580 }
581 if (dbg.ss_delay > 0) {
582 usb_pause_mtx(
583 xfer->xroot->xfer_mtx,
584 USB_MS_TO_TICKS(dbg.ss_delay));
585 /* make sure we don't time out */
586 start_ticks = CUR_TICKS;
587 }
588 #endif
589 xfer->flags.manual_status = 0;
590 } else {
591 break;
592 }
593 }
594 usbd_xfer_set_frames(xfer, 1);
595 }
596
597 usbd_transfer_start(xfer);
598
599 while (usbd_transfer_pending(xfer)) {
600 (void)cv_wait(&udev->ctrlreq_cv,
601 xfer->xroot->xfer_mtx);
602 }
603
604 err = xfer->error;
605
606 if (err) {
607 break;
608 }
609
610 /* get actual length of DATA stage */
611
612 if (xfer->aframes < 2) {
613 acttemp = 0;
614 } else {
615 acttemp = usbd_xfer_frame_len(xfer, 1);
616 }
617
618 /* check for short packet */
619
620 if (temp > acttemp) {
621 temp = acttemp;
622 length = temp;
623 }
624 if (temp > 0) {
625 if (req->bmRequestType & UT_READ) {
626 #if USB_HAVE_USER_IO
627 if (flags & USB_USER_DATA_PTR) {
628 USB_XFER_UNLOCK(xfer);
629 err = usbd_copy_out_user(xfer->frbuffers + 1,
630 0, data, temp);
631 USB_XFER_LOCK(xfer);
632 if (err) {
633 err = USB_ERR_INVAL;
634 break;
635 }
636 } else
637 #endif
638 usbd_copy_out(xfer->frbuffers + 1,
639 0, data, temp);
640 }
641 }
642 /*
643 * Clear "frlengths[0]" so that we don't send the setup
644 * packet again:
645 */
646 usbd_xfer_set_frame_len(xfer, 0, 0);
647
648 /* update length and data pointer */
649 length -= temp;
650 data = USB_ADD_BYTES(data, temp);
651
652 if (actlen) {
653 (*actlen) += temp;
654 }
655 /* check for timeout */
656
657 delta_ticks = CUR_TICKS - start_ticks;
658 if (delta_ticks > max_ticks) {
659 if (!err) {
660 err = USB_ERR_TIMEOUT;
661 }
662 }
663 if (err) {
664 break;
665 }
666 }
667
668 if (err) {
669 /*
670 * Make sure that the control endpoint is no longer
671 * blocked in case of a non-transfer related error:
672 */
673 usbd_transfer_stop(xfer);
674 }
675 USB_XFER_UNLOCK(xfer);
676
677 done:
678 if (do_unlock)
679 usbd_ctrl_unlock(udev);
680
681 if ((mtx != NULL) && (mtx != &Giant))
682 USB_MTX_LOCK(mtx);
683
684 switch (err) {
685 case USB_ERR_NORMAL_COMPLETION:
686 case USB_ERR_SHORT_XFER:
687 case USB_ERR_STALLED:
688 case USB_ERR_CANCELLED:
689 break;
690 default:
691 DPRINTF("I/O error - waiting a bit for TT cleanup\n");
692 usb_pause_mtx(mtx, hz / 16);
693 break;
694 }
695 return ((usb_error_t)err);
696 }
697
698 /*------------------------------------------------------------------------*
699 * usbd_do_request_proc - factored out code
700 *
701 * This function is factored out code. It does basically the same like
702 * usbd_do_request_flags, except it will check the status of the
703 * passed process argument before doing the USB request. If the
704 * process is draining the USB_ERR_IOERROR code will be returned. It
705 * is assumed that the mutex associated with the process is locked
706 * when calling this function.
707 *------------------------------------------------------------------------*/
708 usb_error_t
usbd_do_request_proc(struct usb_device * udev,struct usb_process * pproc,struct usb_device_request * req,void * data,uint16_t flags,uint16_t * actlen,usb_timeout_t timeout)709 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc,
710 struct usb_device_request *req, void *data, uint16_t flags,
711 uint16_t *actlen, usb_timeout_t timeout)
712 {
713 usb_error_t err;
714 uint16_t len;
715
716 /* get request data length */
717 len = UGETW(req->wLength);
718
719 /* check if the device is being detached */
720 if (usb_proc_is_gone(pproc)) {
721 err = USB_ERR_IOERROR;
722 goto done;
723 }
724
725 /* forward the USB request */
726 err = usbd_do_request_flags(udev, pproc->up_mtx,
727 req, data, flags, actlen, timeout);
728
729 done:
730 /* on failure we zero the data */
731 /* on short packet we zero the unused data */
732 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) {
733 if (err)
734 (void)memset_s(data, len, 0, len);
735 else if (actlen && *actlen != len)
736 (void)memset_s(((uint8_t *)data) + *actlen, len - *actlen, 0, len - *actlen);
737 }
738 return (err);
739 }
740
741 /*------------------------------------------------------------------------*
742 * usbd_req_reset_port
743 *
744 * This function will instruct a USB HUB to perform a reset sequence
745 * on the specified port number.
746 *
747 * Returns:
748 * 0: Success. The USB device should now be at address zero.
749 * Else: Failure. No USB device is present and the USB port should be
750 * disabled.
751 *------------------------------------------------------------------------*/
752 usb_error_t
usbd_req_reset_port(struct usb_device * udev,struct mtx * mtx,uint8_t port)753 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port)
754 {
755 struct usb_port_status ps;
756 usb_error_t err;
757 uint16_t n;
758 uint16_t status;
759 uint16_t change;
760
761 DPRINTF("\n");
762
763 /* clear any leftover port reset changes first */
764 (void)usbd_req_clear_port_feature(
765 udev, mtx, port, UHF_C_PORT_RESET);
766
767 /* assert port reset on the given port */
768 err = usbd_req_set_port_feature(
769 udev, mtx, port, UHF_PORT_RESET);
770
771 /* check for errors */
772 if (err)
773 goto done;
774 n = 0;
775 while (1) {
776 /* wait for the device to recover from reset */
777 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
778 n += usb_port_reset_delay;
779 err = usbd_req_get_port_status(udev, mtx, &ps, port);
780 if (err)
781 goto done;
782
783 status = UGETW(ps.wPortStatus);
784 change = UGETW(ps.wPortChange);
785
786 /* The port state is unknown until the reset completes.
787 * On top of that, some chips may require additional time to re-establish a connection after the reset is complete,
788 * so also wait for the connection to be re-established.
789 */
790 if (!(status & UPS_RESET) && (status & UPS_CURRENT_CONNECT_STATUS))
791 break;
792
793 /* check for timeout */
794 if (n > 1000) {
795 n = 0;
796 break;
797 }
798 }
799
800 /* clear port reset first */
801 err = usbd_req_clear_port_feature(
802 udev, mtx, port, UHF_C_PORT_RESET);
803 if (err)
804 goto done;
805
806 if (change & UPS_CURRENT_CONNECT_STATUS) {
807 usb_reset_port_flag_set(port);
808 return (USB_ERR_IOERROR);
809 }
810
811 if ((udev->speed == USB_SPEED_SUPER) &&
812 (change & UHF_C_BH_PORT_RESET)) {
813 /* try to clear port warm reset */
814 err = usbd_req_clear_port_feature(
815 udev, mtx, port, UHF_C_BH_PORT_RESET);
816 if (err)
817 goto done;
818 }
819
820 /* check for timeout */
821 if (n == 0) {
822 err = USB_ERR_TIMEOUT;
823 goto done;
824 }
825
826 /* wait for the device to recover from reset */
827 if (usb_reset_port_flag_is_set(port) == true) {
828 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery_max));
829 usb_reset_port_flag_clear(port);
830 } else {
831 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
832 }
833
834 return (USB_ERR_NORMAL_COMPLETION);
835
836 done:
837 DPRINTFN(0, "port %d reset returning error=%s\n",
838 port, usbd_errstr(err));
839 return (err);
840 }
841
842 /*------------------------------------------------------------------------*
843 * usbd_req_warm_reset_port
844 *
845 * This function will instruct an USB HUB to perform a warm reset
846 * sequence on the specified port number. This kind of reset is not
847 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted
848 * for SUPER-speed USB HUBs.
849 *
850 * Returns:
851 * 0: Success. The USB device should now be available again.
852 * Else: Failure. No USB device is present and the USB port should be
853 * disabled.
854 *------------------------------------------------------------------------*/
855 usb_error_t
usbd_req_warm_reset_port(struct usb_device * udev,struct mtx * mtx,uint8_t port)856 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx,
857 uint8_t port)
858 {
859 struct usb_port_status ps;
860 usb_error_t err;
861 uint16_t n;
862 uint16_t status;
863 uint16_t change;
864
865 DPRINTF("\n");
866
867 err = usbd_req_get_port_status(udev, mtx, &ps, port);
868 if (err)
869 goto done;
870
871 status = UGETW(ps.wPortStatus);
872
873 switch (UPS_PORT_LINK_STATE_GET(status)) {
874 case UPS_PORT_LS_U3:
875 case UPS_PORT_LS_COMP_MODE:
876 case UPS_PORT_LS_LOOPBACK:
877 case UPS_PORT_LS_SS_INA:
878 break;
879 default:
880 DPRINTF("Wrong state for warm reset\n");
881 return (USB_ERR_NORMAL_COMPLETION);
882 }
883
884 /* clear any leftover warm port reset changes first */
885 (void)usbd_req_clear_port_feature(udev, mtx,
886 port, UHF_C_BH_PORT_RESET);
887
888 /* set warm port reset */
889 err = usbd_req_set_port_feature(udev, mtx,
890 port, UHF_BH_PORT_RESET);
891 if (err)
892 goto done;
893
894 n = 0;
895 while (1) {
896 /* wait for the device to recover from reset */
897 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay));
898 n += usb_port_reset_delay;
899 err = usbd_req_get_port_status(udev, mtx, &ps, port);
900 if (err)
901 goto done;
902
903 status = UGETW(ps.wPortStatus);
904 change = UGETW(ps.wPortChange);
905
906 /* if the device disappeared, just give up */
907 if (!(status & UPS_CURRENT_CONNECT_STATUS))
908 goto done;
909
910 /* check if reset is complete */
911 if (change & UPS_C_BH_PORT_RESET)
912 break;
913
914 /* check for timeout */
915 if (n > 1000) {
916 n = 0;
917 break;
918 }
919 }
920
921 /* clear port reset first */
922 err = usbd_req_clear_port_feature(
923 udev, mtx, port, UHF_C_BH_PORT_RESET);
924 if (err)
925 goto done;
926
927 /* check for timeout */
928 if (n == 0) {
929 err = USB_ERR_TIMEOUT;
930 goto done;
931 }
932 /* wait for the device to recover from reset */
933 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery));
934
935 done:
936 DPRINTFN(2, "port %d warm reset returning error=%s\n",
937 port, usbd_errstr(err));
938 return (err);
939 }
940
941 /*------------------------------------------------------------------------*
942 * usbd_req_get_desc
943 *
944 * This function can be used to retrieve USB descriptors. It contains
945 * some additional logic like zeroing of missing descriptor bytes and
946 * retrying an USB descriptor in case of failure. The "min_len"
947 * argument specifies the minimum descriptor length. The "max_len"
948 * argument specifies the maximum descriptor length. If the real
949 * descriptor length is less than the minimum length the missing
950 * byte(s) will be zeroed. The type field, the second byte of the USB
951 * descriptor, will get forced to the correct type. If the "actlen"
952 * pointer is non-NULL, the actual length of the transfer will get
953 * stored in the 16-bit unsigned integer which it is pointing to. The
954 * first byte of the descriptor will not get updated. If the "actlen"
955 * pointer is NULL the first byte of the descriptor will get updated
956 * to reflect the actual length instead. If "min_len" is not equal to
957 * "max_len" then this function will try to retrive the beginning of
958 * the descriptor and base the maximum length on the first byte of the
959 * descriptor.
960 *
961 * Returns:
962 * 0: Success
963 * Else: Failure
964 *------------------------------------------------------------------------*/
965 usb_error_t
usbd_req_get_desc(struct usb_device * udev,struct mtx * mtx,uint16_t * actlen,void * desc,uint16_t min_len,uint16_t max_len,uint16_t id,uint8_t type,uint8_t index,uint8_t retries)966 usbd_req_get_desc(struct usb_device *udev,
967 struct mtx *mtx, uint16_t *actlen, void *desc,
968 uint16_t min_len, uint16_t max_len,
969 uint16_t id, uint8_t type, uint8_t index,
970 uint8_t retries)
971 {
972 struct usb_device_request req;
973 uint8_t *buf;
974 usb_error_t err;
975
976 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
977 id, type, index, max_len);
978
979 req.bmRequestType = UT_READ_DEVICE;
980 req.bRequest = UR_GET_DESCRIPTOR;
981 USETW2(req.wValue, type, index);
982 USETW(req.wIndex, id);
983
984 while (1) {
985
986 if ((min_len < 2) || (max_len < 2)) {
987 err = USB_ERR_INVAL;
988 goto done;
989 }
990 USETW(req.wLength, min_len);
991
992 err = usbd_do_request_flags(udev, mtx, &req,
993 desc, 0, NULL, 500 /* ms */);
994
995 if (err) {
996 if (!retries) {
997 goto done;
998 }
999 retries--;
1000
1001 usb_pause_mtx(mtx, hz / 5);
1002
1003 continue;
1004 }
1005 buf = desc;
1006
1007 if (min_len == max_len) {
1008
1009 /* enforce correct length */
1010 if ((buf[0] > min_len) && (actlen == NULL))
1011 buf[0] = min_len;
1012
1013 /* enforce correct type */
1014 buf[1] = type;
1015
1016 goto done;
1017 }
1018 /* range check */
1019
1020 if (max_len > buf[0]) {
1021 max_len = buf[0];
1022 }
1023 /* zero minimum data */
1024
1025 while (min_len > max_len) {
1026 min_len--;
1027 buf[min_len] = 0;
1028 }
1029
1030 /* set new minimum length */
1031
1032 min_len = max_len;
1033 }
1034 done:
1035 if (actlen != NULL) {
1036 if (err)
1037 *actlen = 0;
1038 else
1039 *actlen = min_len;
1040 }
1041 return (err);
1042 }
1043
1044 /*------------------------------------------------------------------------*
1045 * usbd_req_get_string_any
1046 *
1047 * This function will return the string given by "string_index"
1048 * using the first language ID. The maximum length "len" includes
1049 * the terminating zero. The "len" argument should be twice as
1050 * big pluss 2 bytes, compared with the actual maximum string length !
1051 *
1052 * Returns:
1053 * 0: Success
1054 * Else: Failure
1055 *------------------------------------------------------------------------*/
1056 usb_error_t
usbd_req_get_string_any(struct usb_device * udev,struct mtx * mtx,char * buf,uint16_t len,uint8_t string_index)1057 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf,
1058 uint16_t len, uint8_t string_index)
1059 {
1060 char *s;
1061 uint8_t *temp;
1062 uint16_t i;
1063 uint16_t n;
1064 uint16_t c;
1065 uint8_t swap;
1066 usb_error_t err;
1067
1068 if (len == 0) {
1069 /* should not happen */
1070 return (USB_ERR_NORMAL_COMPLETION);
1071 }
1072 if (string_index == 0) {
1073 /* this is the language table */
1074 buf[0] = 0;
1075 return (USB_ERR_INVAL);
1076 }
1077 if (udev->flags.no_strings) {
1078 buf[0] = 0;
1079 return (USB_ERR_STALLED);
1080 }
1081 err = usbd_req_get_string_desc
1082 (udev, mtx, buf, len, udev->langid, string_index);
1083 if (err) {
1084 buf[0] = 0;
1085 return (err);
1086 }
1087 temp = (uint8_t *)buf;
1088
1089 if (temp[0] < 2) {
1090 /* string length is too short */
1091 buf[0] = 0;
1092 return (USB_ERR_INVAL);
1093 }
1094 /* reserve one byte for terminating zero */
1095 len--;
1096
1097 /* find maximum length */
1098 s = buf;
1099 n = (temp[0] / 2) - 1;
1100 if (n > len) {
1101 n = len;
1102 }
1103 /* skip descriptor header */
1104 temp += 2;
1105
1106 /* reset swap state */
1107 swap = 3;
1108
1109 /* convert and filter */
1110 for (i = 0; (i != n); i++) {
1111 c = UGETW(temp + (2 * i));
1112
1113 /* convert from Unicode, handle buggy strings */
1114 if (((c & 0xff00) == 0) && (swap & 1)) {
1115 /* Little Endian, default */
1116 *s = c;
1117 swap = 1;
1118 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
1119 /* Big Endian */
1120 *s = c >> 8;
1121 swap = 2;
1122 } else {
1123 /* silently skip bad character */
1124 continue;
1125 }
1126
1127 /*
1128 * Filter by default - We only allow alphanumerical
1129 * and a few more to avoid any problems with scripts
1130 * and daemons.
1131 */
1132 if (isalpha(*s) ||
1133 isdigit(*s) ||
1134 *s == '-' ||
1135 *s == '+' ||
1136 *s == ' ' ||
1137 *s == '.' ||
1138 *s == ',') {
1139 /* allowed */
1140 s++;
1141 }
1142 /* silently skip bad character */
1143 }
1144 *s = 0; /* zero terminate resulting string */
1145 return (USB_ERR_NORMAL_COMPLETION);
1146 }
1147
1148 /*------------------------------------------------------------------------*
1149 * usbd_req_get_string_desc
1150 *
1151 * If you don't know the language ID, consider using
1152 * "usbd_req_get_string_any()".
1153 *
1154 * Returns:
1155 * 0: Success
1156 * Else: Failure
1157 *------------------------------------------------------------------------*/
1158 usb_error_t
usbd_req_get_string_desc(struct usb_device * udev,struct mtx * mtx,void * sdesc,uint16_t max_len,uint16_t lang_id,uint8_t string_index)1159 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc,
1160 uint16_t max_len, uint16_t lang_id,
1161 uint8_t string_index)
1162 {
1163 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id,
1164 UDESC_STRING, string_index, 0));
1165 }
1166
1167 /*------------------------------------------------------------------------*
1168 * usbd_req_get_config_desc_ptr
1169 *
1170 * This function is used in device side mode to retrieve the pointer
1171 * to the generated config descriptor. This saves allocating space for
1172 * an additional config descriptor when setting the configuration.
1173 *
1174 * Returns:
1175 * 0: Success
1176 * Else: Failure
1177 *------------------------------------------------------------------------*/
1178 usb_error_t
usbd_req_get_descriptor_ptr(struct usb_device * udev,struct usb_config_descriptor ** ppcd,uint16_t wValue)1179 usbd_req_get_descriptor_ptr(struct usb_device *udev,
1180 struct usb_config_descriptor **ppcd, uint16_t wValue)
1181 {
1182 struct usb_device_request req;
1183 usb_handle_req_t *hr_func;
1184 const void *ptr;
1185 uint16_t len;
1186 usb_error_t err;
1187
1188 req.bmRequestType = UT_READ_DEVICE;
1189 req.bRequest = UR_GET_DESCRIPTOR;
1190 USETW(req.wValue, wValue);
1191 USETW(req.wIndex, 0);
1192 USETW(req.wLength, 0);
1193
1194 ptr = NULL;
1195 len = 0;
1196
1197 hr_func = usbd_get_hr_func(udev);
1198
1199 if (hr_func == NULL)
1200 err = USB_ERR_INVAL;
1201 else {
1202 USB_BUS_LOCK(udev->bus);
1203 err = (hr_func) (udev, &req, &ptr, &len);
1204 USB_BUS_UNLOCK(udev->bus);
1205 }
1206
1207 if (err)
1208 ptr = NULL;
1209 else if (ptr == NULL)
1210 err = USB_ERR_INVAL;
1211
1212 *ppcd = __DECONST(struct usb_config_descriptor *, ptr);
1213
1214 return (err);
1215 }
1216
1217 /*------------------------------------------------------------------------*
1218 * usbd_req_get_config_desc
1219 *
1220 * Returns:
1221 * 0: Success
1222 * Else: Failure
1223 *------------------------------------------------------------------------*/
1224 usb_error_t
usbd_req_get_config_desc(struct usb_device * udev,struct mtx * mtx,struct usb_config_descriptor * d,uint8_t conf_index)1225 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx,
1226 struct usb_config_descriptor *d, uint8_t conf_index)
1227 {
1228 usb_error_t err;
1229
1230 DPRINTFN(4, "confidx=%d\n", conf_index);
1231
1232 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1233 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
1234 if (err) {
1235 goto done;
1236 }
1237 /* Extra sanity checking */
1238 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) {
1239 err = USB_ERR_INVAL;
1240 }
1241 done:
1242 return (err);
1243 }
1244
1245 /*------------------------------------------------------------------------*
1246 * usbd_alloc_config_desc
1247 *
1248 * This function is used to allocate a zeroed configuration
1249 * descriptor.
1250 *
1251 * Returns:
1252 * NULL: Failure
1253 * Else: Success
1254 *------------------------------------------------------------------------*/
1255 void *
usbd_alloc_config_desc(struct usb_device * udev,uint32_t size)1256 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size)
1257 {
1258 if (size > USB_CONFIG_MAX) {
1259 DPRINTF("Configuration descriptor too big\n");
1260 return (NULL);
1261 }
1262 #if (USB_HAVE_FIXED_CONFIG == 0)
1263 return (bsd_malloc(size, M_USBDEV, M_ZERO | M_WAITOK));
1264 #else
1265 (void)memset_s(udev->config_data, sizeof(udev->config_data), 0, sizeof(udev->config_data));
1266 return (udev->config_data);
1267 #endif
1268 }
1269
1270 /*------------------------------------------------------------------------*
1271 * usbd_alloc_config_desc
1272 *
1273 * This function is used to free a configuration descriptor.
1274 *------------------------------------------------------------------------*/
1275 void
usbd_free_config_desc(struct usb_device * udev,void * ptr)1276 usbd_free_config_desc(struct usb_device *udev, void *ptr)
1277 {
1278 #if (USB_HAVE_FIXED_CONFIG == 0)
1279 bsd_free(ptr, M_USBDEV);
1280 #endif
1281 }
1282
1283 /*------------------------------------------------------------------------*
1284 * usbd_req_get_config_desc_full
1285 *
1286 * This function gets the complete USB configuration descriptor and
1287 * ensures that "wTotalLength" is correct. The returned configuration
1288 * descriptor is freed by calling "usbd_free_config_desc()".
1289 *
1290 * Returns:
1291 * 0: Success
1292 * Else: Failure
1293 *------------------------------------------------------------------------*/
1294 usb_error_t
usbd_req_get_config_desc_full(struct usb_device * udev,struct mtx * mtx,struct usb_config_descriptor ** ppcd,uint8_t index)1295 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx,
1296 struct usb_config_descriptor **ppcd, uint8_t index)
1297 {
1298 struct usb_config_descriptor cd;
1299 struct usb_config_descriptor *cdesc;
1300 uint32_t len;
1301 usb_error_t err;
1302
1303 DPRINTFN(4, "index=%d\n", index);
1304
1305 *ppcd = NULL;
1306
1307 err = usbd_req_get_config_desc(udev, mtx, &cd, index);
1308 if (err)
1309 return (err);
1310
1311 /* get full descriptor */
1312 len = UGETW(cd.wTotalLength);
1313 if (len < (uint32_t)sizeof(*cdesc)) {
1314 /* corrupt descriptor */
1315 return (USB_ERR_INVAL);
1316 } else if (len > USB_CONFIG_MAX) {
1317 DPRINTF("Configuration descriptor was truncated\n");
1318 len = USB_CONFIG_MAX;
1319 }
1320 cdesc = usbd_alloc_config_desc(udev, len);
1321 if (cdesc == NULL)
1322 return (USB_ERR_NOMEM);
1323 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0,
1324 UDESC_CONFIG, index, 3);
1325 if (err) {
1326 usbd_free_config_desc(udev, cdesc);
1327 return (err);
1328 }
1329 /* make sure that the device is not fooling us: */
1330 USETW(cdesc->wTotalLength, len);
1331
1332 *ppcd = cdesc;
1333
1334 return (USB_ERR_NORMAL_COMPLETION); /* success */
1335 }
1336
1337 /*------------------------------------------------------------------------*
1338 * usbd_req_get_device_desc
1339 *
1340 * Returns:
1341 * 0: Success
1342 * Else: Failure
1343 *------------------------------------------------------------------------*/
1344 usb_error_t
usbd_req_get_device_desc(struct usb_device * udev,struct mtx * mtx,struct usb_device_descriptor * d)1345 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx,
1346 struct usb_device_descriptor *d)
1347 {
1348 DPRINTFN(4, "\n");
1349 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d),
1350 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
1351 }
1352
1353 /*------------------------------------------------------------------------*
1354 * usbd_req_get_alt_interface_no
1355 *
1356 * Returns:
1357 * 0: Success
1358 * Else: Failure
1359 *------------------------------------------------------------------------*/
1360 usb_error_t
usbd_req_get_alt_interface_no(struct usb_device * udev,struct mtx * mtx,uint8_t * alt_iface_no,uint8_t iface_index)1361 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1362 uint8_t *alt_iface_no, uint8_t iface_index)
1363 {
1364 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1365 struct usb_device_request req;
1366
1367 if ((iface == NULL) || (iface->idesc == NULL))
1368 return (USB_ERR_INVAL);
1369
1370 req.bmRequestType = UT_READ_INTERFACE;
1371 req.bRequest = UR_GET_INTERFACE;
1372 USETW(req.wValue, 0);
1373 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1374 req.wIndex[1] = 0;
1375 USETW(req.wLength, 1);
1376 return (usbd_do_request(udev, mtx, &req, alt_iface_no));
1377 }
1378
1379 /*------------------------------------------------------------------------*
1380 * usbd_req_set_alt_interface_no
1381 *
1382 * Returns:
1383 * 0: Success
1384 * Else: Failure
1385 *------------------------------------------------------------------------*/
1386 usb_error_t
usbd_req_set_alt_interface_no(struct usb_device * udev,struct mtx * mtx,uint8_t iface_index,uint8_t alt_no)1387 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx,
1388 uint8_t iface_index, uint8_t alt_no)
1389 {
1390 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1391 struct usb_device_request req;
1392
1393 if ((iface == NULL) || (iface->idesc == NULL))
1394 return (USB_ERR_INVAL);
1395
1396 req.bmRequestType = UT_WRITE_INTERFACE;
1397 req.bRequest = UR_SET_INTERFACE;
1398 req.wValue[0] = alt_no;
1399 req.wValue[1] = 0;
1400 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1401 req.wIndex[1] = 0;
1402 USETW(req.wLength, 0);
1403 return (usbd_do_request(udev, mtx, &req, 0));
1404 }
1405
1406 /*------------------------------------------------------------------------*
1407 * usbd_req_get_device_status
1408 *
1409 * Returns:
1410 * 0: Success
1411 * Else: Failure
1412 *------------------------------------------------------------------------*/
1413 usb_error_t
usbd_req_get_device_status(struct usb_device * udev,struct mtx * mtx,struct usb_status * st)1414 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx,
1415 struct usb_status *st)
1416 {
1417 struct usb_device_request req;
1418
1419 req.bmRequestType = UT_READ_DEVICE;
1420 req.bRequest = UR_GET_STATUS;
1421 USETW(req.wValue, 0);
1422 USETW(req.wIndex, 0);
1423 USETW(req.wLength, sizeof(*st));
1424 return (usbd_do_request(udev, mtx, &req, st));
1425 }
1426
1427 /*------------------------------------------------------------------------*
1428 * usbd_req_get_hub_descriptor
1429 *
1430 * Returns:
1431 * 0: Success
1432 * Else: Failure
1433 *------------------------------------------------------------------------*/
1434 usb_error_t
usbd_req_get_hub_descriptor(struct usb_device * udev,struct mtx * mtx,struct usb_hub_descriptor * hd,uint8_t nports)1435 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1436 struct usb_hub_descriptor *hd, uint8_t nports)
1437 {
1438 struct usb_device_request req;
1439 uint16_t len = (nports + 7 + (8 * 8)) / 8;
1440
1441 req.bmRequestType = UT_READ_CLASS_DEVICE;
1442 req.bRequest = UR_GET_DESCRIPTOR;
1443 USETW2(req.wValue, UDESC_HUB, 0);
1444 USETW(req.wIndex, 0);
1445 USETW(req.wLength, len);
1446 return (usbd_do_request(udev, mtx, &req, hd));
1447 }
1448
1449 /*------------------------------------------------------------------------*
1450 * usbd_req_get_ss_hub_descriptor
1451 *
1452 * Returns:
1453 * 0: Success
1454 * Else: Failure
1455 *------------------------------------------------------------------------*/
1456 usb_error_t
usbd_req_get_ss_hub_descriptor(struct usb_device * udev,struct mtx * mtx,struct usb_hub_ss_descriptor * hd,uint8_t nports)1457 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx,
1458 struct usb_hub_ss_descriptor *hd, uint8_t nports)
1459 {
1460 struct usb_device_request req;
1461 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8);
1462
1463 req.bmRequestType = UT_READ_CLASS_DEVICE;
1464 req.bRequest = UR_GET_DESCRIPTOR;
1465 USETW2(req.wValue, UDESC_SS_HUB, 0);
1466 USETW(req.wIndex, 0);
1467 USETW(req.wLength, len);
1468 return (usbd_do_request(udev, mtx, &req, hd));
1469 }
1470
1471 /*------------------------------------------------------------------------*
1472 * usbd_req_get_hub_status
1473 *
1474 * Returns:
1475 * 0: Success
1476 * Else: Failure
1477 *------------------------------------------------------------------------*/
1478 usb_error_t
usbd_req_get_hub_status(struct usb_device * udev,struct mtx * mtx,struct usb_hub_status * st)1479 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx,
1480 struct usb_hub_status *st)
1481 {
1482 struct usb_device_request req;
1483
1484 req.bmRequestType = UT_READ_CLASS_DEVICE;
1485 req.bRequest = UR_GET_STATUS;
1486 USETW(req.wValue, 0);
1487 USETW(req.wIndex, 0);
1488 USETW(req.wLength, sizeof(struct usb_hub_status));
1489 return (usbd_do_request(udev, mtx, &req, st));
1490 }
1491
1492 /*------------------------------------------------------------------------*
1493 * usbd_req_set_address
1494 *
1495 * This function is used to set the address for an USB device. After
1496 * port reset the USB device will respond at address zero.
1497 *
1498 * Returns:
1499 * 0: Success
1500 * Else: Failure
1501 *------------------------------------------------------------------------*/
1502 usb_error_t
usbd_req_set_address(struct usb_device * udev,struct mtx * mtx,uint16_t addr)1503 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr)
1504 {
1505 struct usb_device_request req;
1506 usb_error_t err;
1507
1508 DPRINTF("setting device address=%d\n", addr);
1509
1510 req.bmRequestType = UT_WRITE_DEVICE;
1511 req.bRequest = UR_SET_ADDRESS;
1512 USETW(req.wValue, addr);
1513 USETW(req.wIndex, 0);
1514 USETW(req.wLength, 0);
1515
1516 err = USB_ERR_INVAL;
1517
1518 /* check if USB controller handles set address */
1519 if (udev->bus->methods->set_address != NULL)
1520 err = (udev->bus->methods->set_address) (udev, mtx, addr);
1521
1522 if (err != USB_ERR_INVAL)
1523 goto done;
1524
1525 /* Setting the address should not take more than 1 second ! */
1526 err = usbd_do_request_flags(udev, mtx, &req, NULL,
1527 USB_DELAY_STATUS_STAGE, NULL, 1000);
1528
1529 done:
1530 /* allow device time to set new address */
1531 usb_pause_mtx(mtx,
1532 USB_MS_TO_TICKS(usb_set_address_settle));
1533
1534 return (err);
1535 }
1536
1537 /*------------------------------------------------------------------------*
1538 * usbd_req_get_port_status
1539 *
1540 * Returns:
1541 * 0: Success
1542 * Else: Failure
1543 *------------------------------------------------------------------------*/
1544 usb_error_t
usbd_req_get_port_status(struct usb_device * udev,struct mtx * mtx,struct usb_port_status * ps,uint8_t port)1545 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx,
1546 struct usb_port_status *ps, uint8_t port)
1547 {
1548 struct usb_device_request req;
1549
1550 req.bmRequestType = UT_READ_CLASS_OTHER;
1551 req.bRequest = UR_GET_STATUS;
1552 USETW(req.wValue, 0);
1553 req.wIndex[0] = port;
1554 req.wIndex[1] = 0;
1555 USETW(req.wLength, sizeof *ps);
1556 return (usbd_do_request(udev, mtx, &req, ps));
1557 }
1558
1559 /*------------------------------------------------------------------------*
1560 * usbd_req_clear_hub_feature
1561 *
1562 * Returns:
1563 * 0: Success
1564 * Else: Failure
1565 *------------------------------------------------------------------------*/
1566 usb_error_t
usbd_req_clear_hub_feature(struct usb_device * udev,struct mtx * mtx,uint16_t sel)1567 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx,
1568 uint16_t sel)
1569 {
1570 struct usb_device_request req;
1571
1572 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1573 req.bRequest = UR_CLEAR_FEATURE;
1574 USETW(req.wValue, sel);
1575 USETW(req.wIndex, 0);
1576 USETW(req.wLength, 0);
1577 return (usbd_do_request(udev, mtx, &req, 0));
1578 }
1579
1580 /*------------------------------------------------------------------------*
1581 * usbd_req_set_hub_feature
1582 *
1583 * Returns:
1584 * 0: Success
1585 * Else: Failure
1586 *------------------------------------------------------------------------*/
1587 usb_error_t
usbd_req_set_hub_feature(struct usb_device * udev,struct mtx * mtx,uint16_t sel)1588 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx,
1589 uint16_t sel)
1590 {
1591 struct usb_device_request req;
1592
1593 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1594 req.bRequest = UR_SET_FEATURE;
1595 USETW(req.wValue, sel);
1596 USETW(req.wIndex, 0);
1597 USETW(req.wLength, 0);
1598 return (usbd_do_request(udev, mtx, &req, 0));
1599 }
1600
1601 /*------------------------------------------------------------------------*
1602 * usbd_req_set_hub_u1_timeout
1603 *
1604 * Returns:
1605 * 0: Success
1606 * Else: Failure
1607 *------------------------------------------------------------------------*/
1608 usb_error_t
usbd_req_set_hub_u1_timeout(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint8_t timeout)1609 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx,
1610 uint8_t port, uint8_t timeout)
1611 {
1612 struct usb_device_request req;
1613
1614 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1615 req.bRequest = UR_SET_FEATURE;
1616 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
1617 req.wIndex[0] = port;
1618 req.wIndex[1] = timeout;
1619 USETW(req.wLength, 0);
1620 return (usbd_do_request(udev, mtx, &req, 0));
1621 }
1622
1623 /*------------------------------------------------------------------------*
1624 * usbd_req_set_hub_u2_timeout
1625 *
1626 * Returns:
1627 * 0: Success
1628 * Else: Failure
1629 *------------------------------------------------------------------------*/
1630 usb_error_t
usbd_req_set_hub_u2_timeout(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint8_t timeout)1631 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx,
1632 uint8_t port, uint8_t timeout)
1633 {
1634 struct usb_device_request req;
1635
1636 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1637 req.bRequest = UR_SET_FEATURE;
1638 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
1639 req.wIndex[0] = port;
1640 req.wIndex[1] = timeout;
1641 USETW(req.wLength, 0);
1642 return (usbd_do_request(udev, mtx, &req, 0));
1643 }
1644
1645 /*------------------------------------------------------------------------*
1646 * usbd_req_set_hub_depth
1647 *
1648 * Returns:
1649 * 0: Success
1650 * Else: Failure
1651 *------------------------------------------------------------------------*/
1652 usb_error_t
usbd_req_set_hub_depth(struct usb_device * udev,struct mtx * mtx,uint16_t depth)1653 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx,
1654 uint16_t depth)
1655 {
1656 struct usb_device_request req;
1657
1658 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1659 req.bRequest = UR_SET_HUB_DEPTH;
1660 USETW(req.wValue, depth);
1661 USETW(req.wIndex, 0);
1662 USETW(req.wLength, 0);
1663 return (usbd_do_request(udev, mtx, &req, 0));
1664 }
1665
1666 /*------------------------------------------------------------------------*
1667 * usbd_req_clear_port_feature
1668 *
1669 * Returns:
1670 * 0: Success
1671 * Else: Failure
1672 *------------------------------------------------------------------------*/
1673 usb_error_t
usbd_req_clear_port_feature(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint16_t sel)1674 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx,
1675 uint8_t port, uint16_t sel)
1676 {
1677 struct usb_device_request req;
1678
1679 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1680 req.bRequest = UR_CLEAR_FEATURE;
1681 USETW(req.wValue, sel);
1682 req.wIndex[0] = port;
1683 req.wIndex[1] = 0;
1684 USETW(req.wLength, 0);
1685 return (usbd_do_request(udev, mtx, &req, 0));
1686 }
1687
1688 /*------------------------------------------------------------------------*
1689 * usbd_req_set_port_feature
1690 *
1691 * Returns:
1692 * 0: Success
1693 * Else: Failure
1694 *------------------------------------------------------------------------*/
1695 usb_error_t
usbd_req_set_port_feature(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint16_t sel)1696 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx,
1697 uint8_t port, uint16_t sel)
1698 {
1699 struct usb_device_request req;
1700
1701 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1702 req.bRequest = UR_SET_FEATURE;
1703 USETW(req.wValue, sel);
1704 req.wIndex[0] = port;
1705 req.wIndex[1] = 0;
1706 USETW(req.wLength, 0);
1707 return (usbd_do_request(udev, mtx, &req, 0));
1708 }
1709
1710 /*------------------------------------------------------------------------*
1711 * usbd_req_set_protocol
1712 *
1713 * Returns:
1714 * 0: Success
1715 * Else: Failure
1716 *------------------------------------------------------------------------*/
1717 usb_error_t
usbd_req_set_protocol(struct usb_device * udev,struct mtx * mtx,uint8_t iface_index,uint16_t report)1718 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx,
1719 uint8_t iface_index, uint16_t report)
1720 {
1721 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1722 struct usb_device_request req;
1723
1724 if ((iface == NULL) || (iface->idesc == NULL)) {
1725 return (USB_ERR_INVAL);
1726 }
1727 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1728 iface, report, iface->idesc->bInterfaceNumber);
1729
1730 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1731 req.bRequest = UR_SET_PROTOCOL;
1732 USETW(req.wValue, report);
1733 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1734 req.wIndex[1] = 0;
1735 USETW(req.wLength, 0);
1736 return (usbd_do_request(udev, mtx, &req, 0));
1737 }
1738
1739 /*------------------------------------------------------------------------*
1740 * usbd_req_set_report
1741 *
1742 * Returns:
1743 * 0: Success
1744 * Else: Failure
1745 *------------------------------------------------------------------------*/
1746 usb_error_t
usbd_req_set_report(struct usb_device * udev,struct mtx * mtx,void * data,uint16_t len,uint8_t iface_index,uint8_t type,uint8_t id)1747 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len,
1748 uint8_t iface_index, uint8_t type, uint8_t id)
1749 {
1750 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1751 struct usb_device_request req;
1752
1753 if ((iface == NULL) || (iface->idesc == NULL)) {
1754 return (USB_ERR_INVAL);
1755 }
1756 DPRINTFN(5, "len=%d\n", len);
1757
1758 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1759 req.bRequest = UR_SET_REPORT;
1760 USETW2(req.wValue, type, id);
1761 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1762 req.wIndex[1] = 0;
1763 USETW(req.wLength, len);
1764 return (usbd_do_request(udev, mtx, &req, data));
1765 }
1766
1767 /*------------------------------------------------------------------------*
1768 * usbd_req_get_report
1769 *
1770 * Returns:
1771 * 0: Success
1772 * Else: Failure
1773 *------------------------------------------------------------------------*/
1774 usb_error_t
usbd_req_get_report(struct usb_device * udev,struct mtx * mtx,void * data,uint16_t len,uint8_t iface_index,uint8_t type,uint8_t id)1775 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data,
1776 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1777 {
1778 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1779 struct usb_device_request req;
1780
1781 if ((iface == NULL) || (iface->idesc == NULL)) {
1782 return (USB_ERR_INVAL);
1783 }
1784 DPRINTFN(5, "len=%d\n", len);
1785
1786 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1787 req.bRequest = UR_GET_REPORT;
1788 USETW2(req.wValue, type, id);
1789 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1790 req.wIndex[1] = 0;
1791 USETW(req.wLength, len);
1792 return (usbd_do_request(udev, mtx, &req, data));
1793 }
1794
1795 /*------------------------------------------------------------------------*
1796 * usbd_req_set_idle
1797 *
1798 * Returns:
1799 * 0: Success
1800 * Else: Failure
1801 *------------------------------------------------------------------------*/
1802 usb_error_t
usbd_req_set_idle(struct usb_device * udev,struct mtx * mtx,uint8_t iface_index,uint8_t duration,uint8_t id)1803 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx,
1804 uint8_t iface_index, uint8_t duration, uint8_t id)
1805 {
1806 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1807 struct usb_device_request req;
1808
1809 if ((iface == NULL) || (iface->idesc == NULL)) {
1810 return (USB_ERR_INVAL);
1811 }
1812 DPRINTFN(5, "%d %d\n", duration, id);
1813
1814 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1815 req.bRequest = UR_SET_IDLE;
1816 USETW2(req.wValue, duration, id);
1817 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1818 req.wIndex[1] = 0;
1819 USETW(req.wLength, 0);
1820 return (usbd_do_request(udev, mtx, &req, 0));
1821 }
1822
1823 /*------------------------------------------------------------------------*
1824 * usbd_req_get_report_descriptor
1825 *
1826 * Returns:
1827 * 0: Success
1828 * Else: Failure
1829 *------------------------------------------------------------------------*/
1830 usb_error_t
usbd_req_get_report_descriptor(struct usb_device * udev,struct mtx * mtx,void * d,uint16_t size,uint8_t iface_index)1831 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx,
1832 void *d, uint16_t size, uint8_t iface_index)
1833 {
1834 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1835 struct usb_device_request req;
1836
1837 if ((iface == NULL) || (iface->idesc == NULL)) {
1838 return (USB_ERR_INVAL);
1839 }
1840 req.bmRequestType = UT_READ_INTERFACE;
1841 req.bRequest = UR_GET_DESCRIPTOR;
1842 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1843 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1844 req.wIndex[1] = 0;
1845 USETW(req.wLength, size);
1846 return (usbd_do_request(udev, mtx, &req, d));
1847 }
1848
1849 /*------------------------------------------------------------------------*
1850 * usbd_req_set_config
1851 *
1852 * This function is used to select the current configuration number in
1853 * both USB device side mode and USB host side mode. When setting the
1854 * configuration the function of the interfaces can change.
1855 *
1856 * Returns:
1857 * 0: Success
1858 * Else: Failure
1859 *------------------------------------------------------------------------*/
1860 usb_error_t
usbd_req_set_config(struct usb_device * udev,struct mtx * mtx,uint8_t conf)1861 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf)
1862 {
1863 struct usb_device_request req;
1864
1865 DPRINTF("setting config %d\n", conf);
1866
1867 /* do "set configuration" request */
1868
1869 req.bmRequestType = UT_WRITE_DEVICE;
1870 req.bRequest = UR_SET_CONFIG;
1871 req.wValue[0] = conf;
1872 req.wValue[1] = 0;
1873 USETW(req.wIndex, 0);
1874 USETW(req.wLength, 0);
1875 return (usbd_do_request(udev, mtx, &req, 0));
1876 }
1877
1878 /*------------------------------------------------------------------------*
1879 * usbd_req_get_config
1880 *
1881 * Returns:
1882 * 0: Success
1883 * Else: Failure
1884 *------------------------------------------------------------------------*/
1885 usb_error_t
usbd_req_get_config(struct usb_device * udev,struct mtx * mtx,uint8_t * pconf)1886 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf)
1887 {
1888 struct usb_device_request req;
1889
1890 req.bmRequestType = UT_READ_DEVICE;
1891 req.bRequest = UR_GET_CONFIG;
1892 USETW(req.wValue, 0);
1893 USETW(req.wIndex, 0);
1894 USETW(req.wLength, 1);
1895 return (usbd_do_request(udev, mtx, &req, pconf));
1896 }
1897
1898 /*------------------------------------------------------------------------*
1899 * usbd_setup_device_desc
1900 *------------------------------------------------------------------------*/
1901 usb_error_t
usbd_setup_device_desc(struct usb_device * udev,struct mtx * mtx)1902 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx)
1903 {
1904 usb_error_t err;
1905
1906 /*
1907 * Get the first 8 bytes of the device descriptor !
1908 *
1909 * NOTE: "usbd_do_request()" will check the device descriptor
1910 * next time we do a request to see if the maximum packet size
1911 * changed! The 8 first bytes of the device descriptor
1912 * contains the maximum packet size to use on control endpoint
1913 * 0. If this value is different from "USB_MAX_IPACKET" a new
1914 * USB control request will be setup!
1915 */
1916 switch (udev->speed) {
1917 case USB_SPEED_FULL:
1918 if (usb_full_ddesc != 0) {
1919 /* get full device descriptor */
1920 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1921 if (err == 0)
1922 break;
1923 }
1924
1925 /* get partial device descriptor, some devices crash on this */
1926 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc,
1927 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1928 if (err != 0)
1929 break;
1930
1931 /* get the full device descriptor */
1932 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1933 break;
1934
1935 default:
1936 DPRINTF("Minimum bMaxPacketSize is large enough "
1937 "to hold the complete device descriptor or "
1938 "only one bMaxPacketSize choice\n");
1939
1940 /* get the full device descriptor */
1941 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1942
1943 /* try one more time, if error */
1944 if (err != 0)
1945 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc);
1946 break;
1947 }
1948
1949 if (err != 0) {
1950 DPRINTFN(0, "getting device descriptor "
1951 "at addr %d failed, %s\n", udev->address,
1952 usbd_errstr(err));
1953 return (err);
1954 }
1955
1956 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1957 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1958 udev->address, UGETW(udev->ddesc.bcdUSB),
1959 udev->ddesc.bDeviceClass,
1960 udev->ddesc.bDeviceSubClass,
1961 udev->ddesc.bDeviceProtocol,
1962 udev->ddesc.bMaxPacketSize,
1963 udev->ddesc.bLength,
1964 udev->speed);
1965
1966 return (err);
1967 }
1968
1969 /*------------------------------------------------------------------------*
1970 * usbd_req_re_enumerate
1971 *
1972 * NOTE: After this function returns the hardware is in the
1973 * unconfigured state! The application is responsible for setting a
1974 * new configuration.
1975 *
1976 * Returns:
1977 * 0: Success
1978 * Else: Failure
1979 *------------------------------------------------------------------------*/
1980 usb_error_t
usbd_req_re_enumerate(struct usb_device * udev,struct mtx * mtx)1981 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx)
1982 {
1983 struct usb_device *parent_hub;
1984 usb_error_t err;
1985 uint8_t old_addr;
1986 uint8_t do_retry = 1;
1987
1988 if (udev->flags.usb_mode != USB_MODE_HOST) {
1989 return (USB_ERR_INVAL);
1990 }
1991 DPRINTFN(5, "try to enumerate device\n");
1992 old_addr = udev->address;
1993 parent_hub = udev->parent_hub;
1994 if (parent_hub == NULL) {
1995 return (USB_ERR_INVAL);
1996 }
1997 retry:
1998 #if USB_HAVE_TT_SUPPORT
1999 /*
2000 * Try to reset the High Speed parent HUB of a LOW- or FULL-
2001 * speed device, if any.
2002 */
2003 if ((udev->parent_hs_hub != NULL) &&
2004 (udev->speed != USB_SPEED_HIGH)) {
2005 DPRINTF("Trying to reset parent High Speed TT.\n");
2006 if ((udev->parent_hs_hub == parent_hub) &&
2007 ((uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) +
2008 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1)) {
2009 /* we can reset the whole TT */
2010 err = usbd_req_reset_tt(parent_hub, NULL,
2011 udev->hs_port_no);
2012 } else {
2013 /* only reset a particular device and endpoint */
2014 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL,
2015 udev->hs_port_no, old_addr, UE_CONTROL, 0);
2016 }
2017 if (err) {
2018 DPRINTF("Resetting parent High "
2019 "Speed TT failed (%s).\n",
2020 usbd_errstr(err));
2021 }
2022 }
2023 #endif
2024 /* Try to warm reset first */
2025 if (parent_hub->speed == USB_SPEED_SUPER)
2026 (void)usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no);
2027
2028 /* Try to reset the parent HUB port. */
2029 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no);
2030 if (err) {
2031 DPRINTFN(0, "addr=%d, port reset failed, %s\n",
2032 old_addr, usbd_errstr(err));
2033 goto done;
2034 }
2035
2036 /*
2037 * After that the port has been reset our device should be at
2038 * address zero:
2039 */
2040 udev->address = USB_START_ADDR;
2041
2042 /* reset "bMaxPacketSize" */
2043 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
2044
2045 /* reset USB state */
2046 usb_set_device_state(udev, USB_STATE_POWERED);
2047
2048 /*
2049 * Restore device address:
2050 */
2051 err = usbd_req_set_address(udev, mtx, old_addr);
2052 if (err) {
2053 /* XXX ignore any errors! */
2054 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n",
2055 old_addr, usbd_errstr(err));
2056 }
2057 /*
2058 * Restore device address, if the controller driver did not
2059 * set a new one:
2060 */
2061 if (udev->address == USB_START_ADDR)
2062 udev->address = old_addr;
2063
2064 /* setup the device descriptor and the initial "wMaxPacketSize" */
2065 err = usbd_setup_device_desc(udev, mtx);
2066
2067 done:
2068 if (err && do_retry) {
2069 /* give the USB firmware some time to load */
2070 usb_pause_mtx(mtx, hz / 2);
2071 /* no more retries after this retry */
2072 do_retry = 0;
2073 /* try again */
2074 goto retry;
2075 }
2076 /* restore address */
2077 if (udev->address == USB_START_ADDR)
2078 udev->address = old_addr;
2079 /* update state, if successful */
2080 if (err == 0)
2081 usb_set_device_state(udev, USB_STATE_ADDRESSED);
2082 return (err);
2083 }
2084
2085 /*------------------------------------------------------------------------*
2086 * usbd_req_clear_device_feature
2087 *
2088 * Returns:
2089 * 0: Success
2090 * Else: Failure
2091 *------------------------------------------------------------------------*/
2092 usb_error_t
usbd_req_clear_device_feature(struct usb_device * udev,struct mtx * mtx,uint16_t sel)2093 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx,
2094 uint16_t sel)
2095 {
2096 struct usb_device_request req;
2097
2098 req.bmRequestType = UT_WRITE_DEVICE;
2099 req.bRequest = UR_CLEAR_FEATURE;
2100 USETW(req.wValue, sel);
2101 USETW(req.wIndex, 0);
2102 USETW(req.wLength, 0);
2103 return (usbd_do_request(udev, mtx, &req, 0));
2104 }
2105
2106 /*------------------------------------------------------------------------*
2107 * usbd_req_set_device_feature
2108 *
2109 * Returns:
2110 * 0: Success
2111 * Else: Failure
2112 *------------------------------------------------------------------------*/
2113 usb_error_t
usbd_req_set_device_feature(struct usb_device * udev,struct mtx * mtx,uint16_t sel)2114 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx,
2115 uint16_t sel)
2116 {
2117 struct usb_device_request req;
2118
2119 req.bmRequestType = UT_WRITE_DEVICE;
2120 req.bRequest = UR_SET_FEATURE;
2121 USETW(req.wValue, sel);
2122 USETW(req.wIndex, 0);
2123 USETW(req.wLength, 0);
2124 return (usbd_do_request(udev, mtx, &req, 0));
2125 }
2126
2127 /*------------------------------------------------------------------------*
2128 * usbd_req_reset_tt
2129 *
2130 * Returns:
2131 * 0: Success
2132 * Else: Failure
2133 *------------------------------------------------------------------------*/
2134 usb_error_t
usbd_req_reset_tt(struct usb_device * udev,struct mtx * mtx,uint8_t port)2135 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx,
2136 uint8_t port)
2137 {
2138 struct usb_device_request req;
2139
2140 /* For single TT HUBs the port should be 1 */
2141
2142 if ((udev->ddesc.bDeviceClass == UDCLASS_HUB) &&
2143 (udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT))
2144 port = 1;
2145
2146 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2147 req.bRequest = UR_RESET_TT;
2148 USETW(req.wValue, 0);
2149 req.wIndex[0] = port;
2150 req.wIndex[1] = 0;
2151 USETW(req.wLength, 0);
2152 return (usbd_do_request(udev, mtx, &req, 0));
2153 }
2154
2155 /*------------------------------------------------------------------------*
2156 * usbd_req_clear_tt_buffer
2157 *
2158 * For single TT HUBs the port should be 1.
2159 *
2160 * Returns:
2161 * 0: Success
2162 * Else: Failure
2163 *------------------------------------------------------------------------*/
2164 usb_error_t
usbd_req_clear_tt_buffer(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint8_t addr,uint8_t type,uint8_t endpoint)2165 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx,
2166 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint)
2167 {
2168 struct usb_device_request req;
2169 uint16_t wValue;
2170
2171 /* For single TT HUBs the port should be 1 */
2172
2173 if ((udev->ddesc.bDeviceClass == UDCLASS_HUB) &&
2174 (udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT))
2175 port = 1;
2176
2177 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) |
2178 ((endpoint & 0x80) << 8) | ((type & 3) << 12);
2179
2180 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2181 req.bRequest = UR_CLEAR_TT_BUFFER;
2182 USETW(req.wValue, wValue);
2183 req.wIndex[0] = port;
2184 req.wIndex[1] = 0;
2185 USETW(req.wLength, 0);
2186 return (usbd_do_request(udev, mtx, &req, 0));
2187 }
2188
2189 /*------------------------------------------------------------------------*
2190 * usbd_req_set_port_link_state
2191 *
2192 * USB 3.0 specific request
2193 *
2194 * Returns:
2195 * 0: Success
2196 * Else: Failure
2197 *------------------------------------------------------------------------*/
2198 usb_error_t
usbd_req_set_port_link_state(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint8_t link_state)2199 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx,
2200 uint8_t port, uint8_t link_state)
2201 {
2202 struct usb_device_request req;
2203
2204 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2205 req.bRequest = UR_SET_FEATURE;
2206 USETW(req.wValue, UHF_PORT_LINK_STATE);
2207 req.wIndex[0] = port;
2208 req.wIndex[1] = link_state;
2209 USETW(req.wLength, 0);
2210 return (usbd_do_request(udev, mtx, &req, 0));
2211 }
2212
2213 /*------------------------------------------------------------------------*
2214 * usbd_req_set_lpm_info
2215 *
2216 * USB 2.0 specific request for Link Power Management.
2217 *
2218 * Returns:
2219 * 0: Success
2220 * USB_ERR_PENDING_REQUESTS: NYET
2221 * USB_ERR_TIMEOUT: TIMEOUT
2222 * USB_ERR_STALL: STALL
2223 * Else: Failure
2224 *------------------------------------------------------------------------*/
2225 usb_error_t
usbd_req_set_lpm_info(struct usb_device * udev,struct mtx * mtx,uint8_t port,uint8_t besl,uint8_t addr,uint8_t rwe)2226 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx,
2227 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe)
2228 {
2229 struct usb_device_request req;
2230 usb_error_t err;
2231 uint8_t buf[1];
2232
2233 req.bmRequestType = UT_WRITE_CLASS_OTHER;
2234 req.bRequest = UR_SET_AND_TEST;
2235 USETW(req.wValue, UHF_PORT_L1);
2236 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4);
2237 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00);
2238 USETW(req.wLength, sizeof(buf));
2239
2240 /* set default value in case of short transfer */
2241 buf[0] = 0x00;
2242
2243 err = usbd_do_request(udev, mtx, &req, buf);
2244 if (err)
2245 return (err);
2246
2247 switch (buf[0]) {
2248 case 0x00: /* SUCCESS */
2249 break;
2250 case 0x10: /* NYET */
2251 err = USB_ERR_PENDING_REQUESTS;
2252 break;
2253 case 0x11: /* TIMEOUT */
2254 err = USB_ERR_TIMEOUT;
2255 break;
2256 case 0x30: /* STALL */
2257 err = USB_ERR_STALLED;
2258 break;
2259 default: /* reserved */
2260 err = USB_ERR_IOERROR;
2261 break;
2262 }
2263 return (err);
2264 }
2265
2266 #undef USB_DEBUG_VAR
2267