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