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