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