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