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